Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

Constrain crawler destinations, CMS redirects and calendar HTML (#132)

Brian Spectorf88516121e00
diff --git a/scripts/notfair-content-calendar.py b/scripts/notfair-content-calendar.py
index 45eaad25..f01f25b3 100644
--- a/scripts/notfair-content-calendar.py
+++ b/scripts/notfair-content-calendar.py
@@ -122,7 +122,7 @@ def render_page(cal: dict) -> str:
<div class="brand">NotFair · Content Calendar</div>
<div class="meta">
<span>Site: <strong>{html.escape(str(site))}</strong></span>
- <span>{lookback}d lookback · {horizon}w horizon</span>
+ <span>{html.escape(str(lookback))}d lookback · {html.escape(str(horizon))}w horizon</span>
<span>Generated: {html.escape(str(generated))}</span>
</div>
</header>
diff --git a/seo/broken-link-checker/WORKFLOW.md b/seo/broken-link-checker/WORKFLOW.md
index a2f73c21..b9d0b93f 100644
--- a/seo/broken-link-checker/WORKFLOW.md
+++ b/seo/broken-link-checker/WORKFLOW.md
@@ -56,3 +56,13 @@ The script will output a JSON report. Analyze the `broken_links` array:
- For external 404s: "The external site at [Target URL] is down or moved. Update or remove the link on [Source Page]."
If no broken links are found, congratulate the user on a healthy site!
+
+
+### Network boundary
+
+The local checker fetches only public HTTP/HTTPS destinations. Every DNS answer
+and redirect is checked, and connections use the validated addresses rather
+than resolving again. Private, loopback, link-local, shared, reserved and
+multicast targets are rejected. Ambient proxy configuration is disabled for
+this crawler. Use a separately reviewed tool for internal-network audits; do
+not add private-network exceptions to scraped links.
diff --git a/seo/broken-link-checker/scripts/checker.py b/seo/broken-link-checker/scripts/checker.py
index de5795d2..a4732fc8 100644
--- a/seo/broken-link-checker/scripts/checker.py
+++ b/seo/broken-link-checker/scripts/checker.py
@@ -9,6 +9,9 @@ Usage:
import argparse
import json
+import http.client
+import ipaddress
+import socket
import os
import sys
import urllib.parse
@@ -20,6 +23,79 @@ from collections import deque
from html.parser import HTMLParser
+def _validate_public_url(url):
+ try:
+ parsed = urllib.parse.urlsplit(url)
+ if parsed.scheme not in ("http", "https") or not parsed.hostname or parsed.username is not None or parsed.password is not None:
+ raise ValueError("only public HTTP/HTTPS URLs without credentials are allowed")
+ parsed.port # reject malformed/out-of-range ports before a connection
+ except ValueError as exc:
+ raise urllib.error.URLError(str(exc)) from exc
+
+
+def _public_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):
+ """Resolve once, validate every answer, then connect to those exact addresses."""
+ host, port = address
+ answers = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
+ if not answers:
+ raise urllib.error.URLError("hostname has no addresses")
+ for _, _, _, _, sockaddr in answers:
+ ip = ipaddress.ip_address(sockaddr[0].split("%", 1)[0])
+ if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped:
+ ip = ip.ipv4_mapped
+ if not ip.is_global or ip.is_multicast or ip.is_reserved:
+ raise urllib.error.URLError("blocked non-public destination")
+ last_error = None
+ for family, socktype, proto, _, sockaddr in answers:
+ sock = socket.socket(family, socktype, proto)
+ try:
+ if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
+ sock.settimeout(timeout)
+ if source_address:
+ sock.bind(source_address)
+ sock.connect(sockaddr)
+ return sock
+ except OSError as exc:
+ last_error = exc
+ sock.close()
+ raise last_error
+
+
+class _PublicHTTPConnection(http.client.HTTPConnection):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._create_connection = _public_connection
+
+
+class _PublicHTTPSConnection(http.client.HTTPSConnection):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._create_connection = _public_connection
+
+
+class _PublicHTTPHandler(urllib.request.HTTPHandler):
+ def http_open(self, req):
+ return self.do_open(_PublicHTTPConnection, req)
+
+
+class _PublicHTTPSHandler(urllib.request.HTTPSHandler):
+ def https_open(self, req):
+ return self.do_open(_PublicHTTPSConnection, req, context=self._context)
+
+
+class _PublicRedirectHandler(urllib.request.HTTPRedirectHandler):
+ def redirect_request(self, req, fp, code, msg, headers, newurl):
+ _validate_public_url(newurl)
+ return super().redirect_request(req, fp, code, msg, headers, newurl)
+
+
+def public_urlopen(request, timeout=10):
+ """Public-only transport for robots, crawling, link checks and every redirect."""
+ _validate_public_url(request.full_url if isinstance(request, urllib.request.Request) else request)
+ opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), _PublicHTTPHandler(), _PublicHTTPSHandler(), _PublicRedirectHandler())
+ return opener.open(request, timeout=timeout)
+
+
class LinkParser(HTMLParser):
def __init__(self, base_url):
super().__init__()
@@ -44,7 +120,7 @@ def check_url(url, timeout=10):
# Try HEAD first for efficiency
req = urllib.request.Request(url, method='HEAD', headers=headers)
try:
- with urllib.request.urlopen(req, timeout=timeout) as response:
+ with public_urlopen(req, timeout=timeout) as response:
return response.getcode(), None
except urllib.error.HTTPError as e:
# Many servers block HEAD, fall back to GET for accuracy
@@ -52,7 +128,7 @@ def check_url(url, timeout=10):
req = urllib.request.Request(url, method='GET', headers=headers)
try:
# We only need the headers/status, so we don't read the body here
- with urllib.request.urlopen(req, timeout=timeout) as response:
+ with public_urlopen(req, timeout=timeout) as response:
return response.getcode(), None
except urllib.error.HTTPError as e2:
return e2.code, str(e2.reason)
@@ -77,7 +153,7 @@ def crawl(start_url, max_pages=50):
robots_url,
headers={'User-Agent': 'NotFairBrokenLinkChecker/1.0'},
)
- with urllib.request.urlopen(robots_request, timeout=10) as response:
+ with public_urlopen(robots_request, timeout=10) as response:
robots_text = response.read().decode('utf-8', errors='ignore')
rp.set_url(robots_url)
rp.parse(robots_text.splitlines())
@@ -110,7 +186,7 @@ def crawl(start_url, max_pages=50):
try:
req = urllib.request.Request(current_url, headers={'User-Agent': 'NotFairBrokenLinkChecker/1.0'})
- with urllib.request.urlopen(req, timeout=10) as response:
+ with public_urlopen(req, timeout=10) as response:
status = response.getcode()
if status >= 400:
broken_links.append({"url": current_url, "status": status, "reason": "Page itself is broken"})
diff --git a/seo/seo-analysis/WORKFLOW.md b/seo/seo-analysis/WORKFLOW.md
index 458924cd..fdb07be2 100644
--- a/seo/seo-analysis/WORKFLOW.md
+++ b/seo/seo-analysis/WORKFLOW.md
@@ -1343,3 +1343,12 @@ Before finishing, verify:
- As agents complete, present each piece of content to the user with its metadata
- After all agents finish, provide a summary: what was generated, suggested
publishing order (highest impact first), and any cross-linking between new pages
+
+
+### Local CMS redirect boundary
+
+WordPress and Strapi script requests carrying Authorization follow only
+same-origin HTTP/HTTPS redirects (same scheme, hostname and effective port,
+without URL userinfo). Cross-origin redirects fail through the existing error
+path. Configure the final CMS base URL directly when a site redirects to another
+origin. This policy does not enable redirecting PUT writes.
diff --git a/seo/seo-analysis/scripts/_http.py b/seo/seo-analysis/scripts/_http.py
new file mode 100644
index 00000000..a4e76b88
--- /dev/null
+++ b/seo/seo-analysis/scripts/_http.py
@@ -0,0 +1,28 @@
+"""Local CMS transport: authenticated redirects stay within the configured origin."""
+import urllib.error
+import urllib.parse
+import urllib.request
+
+
+def _origin(url):
+ parsed = urllib.parse.urlsplit(url)
+ if parsed.scheme.lower() not in ("http", "https") or not parsed.hostname or parsed.username is not None or parsed.password is not None:
+ raise ValueError("invalid authenticated redirect destination")
+ port = parsed.port if parsed.port is not None else (443 if parsed.scheme.lower() == "https" else 80)
+ return parsed.scheme.lower(), parsed.hostname.lower(), port
+
+
+class SameOriginAuthRedirectHandler(urllib.request.HTTPRedirectHandler):
+ def redirect_request(self, req, fp, code, msg, headers, newurl):
+ if any(k.lower() == "authorization" for k, _ in req.header_items()):
+ try:
+ same_origin = _origin(req.full_url) == _origin(newurl)
+ except ValueError:
+ same_origin = False
+ if not same_origin:
+ raise urllib.error.HTTPError(req.full_url, code, "refusing authenticated cross-origin redirect", headers, fp)
+ return super().redirect_request(req, fp, code, msg, headers, newurl)
+
+
+def authenticated_urlopen(request, timeout=15):
+ return urllib.request.build_opener(SameOriginAuthRedirectHandler()).open(request, timeout=timeout)
diff --git a/seo/seo-analysis/scripts/fetch_strapi_content.py b/seo/seo-analysis/scripts/fetch_strapi_content.py
index 02e959dd..95d5a468 100644
--- a/seo/seo-analysis/scripts/fetch_strapi_content.py
+++ b/seo/seo-analysis/scripts/fetch_strapi_content.py
@@ -30,6 +30,7 @@ import time
import urllib.error
import urllib.parse
import urllib.request
+from _http import authenticated_urlopen
from _uid import portable_uid, secure_write_json
@@ -139,7 +140,7 @@ def strapi_get(base_url, api_key, path, params, timeout=30, retries=3):
last_exc = None
for attempt in range(retries):
try:
- with urllib.request.urlopen(req, timeout=timeout) as resp:
+ with authenticated_urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read())
except urllib.error.HTTPError as e:
if e.code in _RETRY_CODES and attempt < retries - 1:
diff --git a/seo/seo-analysis/scripts/fetch_wordpress_content.py b/seo/seo-analysis/scripts/fetch_wordpress_content.py
index 32e62079..47309541 100644
--- a/seo/seo-analysis/scripts/fetch_wordpress_content.py
+++ b/seo/seo-analysis/scripts/fetch_wordpress_content.py
@@ -30,6 +30,7 @@ import time
import urllib.error
import urllib.parse
import urllib.request
+from _http import authenticated_urlopen
from _uid import portable_uid, secure_write_json
@@ -142,7 +143,7 @@ def wp_get(base_url, auth_header, path, params, timeout=30, retries=3):
last_exc = None
for attempt in range(retries):
try:
- with urllib.request.urlopen(req, timeout=timeout) as resp:
+ with authenticated_urlopen(req, timeout=timeout) as resp:
body = json.loads(resp.read())
total = int(resp.headers.get("X-WP-Total", 0))
total_pages = int(resp.headers.get("X-WP-TotalPages", 1))
diff --git a/seo/seo-analysis/scripts/preflight_strapi.py b/seo/seo-analysis/scripts/preflight_strapi.py
index 948200dc..f5bbb0d3 100644
--- a/seo/seo-analysis/scripts/preflight_strapi.py
+++ b/seo/seo-analysis/scripts/preflight_strapi.py
@@ -22,6 +22,7 @@ import time
import urllib.error
import urllib.parse
import urllib.request
+from _http import authenticated_urlopen
# ── Config loading ────────────────────────────────────────────────────────────
@@ -147,7 +148,7 @@ def strapi_get(url, api_key, path, params=None, timeout=15, retries=3):
last_exc = None
for attempt in range(retries):
try:
- with urllib.request.urlopen(req, timeout=timeout) as resp:
+ with authenticated_urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read())
except urllib.error.HTTPError as e:
if e.code in _RETRY_CODES and attempt < retries - 1:
diff --git a/seo/seo-analysis/scripts/preflight_wordpress.py b/seo/seo-analysis/scripts/preflight_wordpress.py
index d41a719c..adc52265 100644
--- a/seo/seo-analysis/scripts/preflight_wordpress.py
+++ b/seo/seo-analysis/scripts/preflight_wordpress.py
@@ -24,6 +24,7 @@ import time
import urllib.error
import urllib.parse
import urllib.request
+from _http import authenticated_urlopen
_RETRY_CODES = {429, 502, 503, 504}
@@ -135,7 +136,7 @@ def wp_get(url, auth_header, path, params=None, timeout=15, retries=3):
last_exc = None
for attempt in range(retries):
try:
- with urllib.request.urlopen(req, timeout=timeout) as resp:
+ with authenticated_urlopen(req, timeout=timeout) as resp:
body = json.loads(resp.read())
total = int(resp.headers.get("X-WP-Total", 0))
total_pages = int(resp.headers.get("X-WP-TotalPages", 1))
diff --git a/seo/seo-analysis/scripts/push_strapi_seo.py b/seo/seo-analysis/scripts/push_strapi_seo.py
index 3af4de7b..38c31e86 100644
--- a/seo/seo-analysis/scripts/push_strapi_seo.py
+++ b/seo/seo-analysis/scripts/push_strapi_seo.py
@@ -53,6 +53,7 @@ import time
import urllib.error
import urllib.parse
import urllib.request
+from _http import authenticated_urlopen
_RETRY_CODES = {429, 502, 503, 504}
@@ -161,7 +162,7 @@ def strapi_get(base_url, api_key, path, params=None, timeout=15, retries=3):
last_exc = None
for attempt in range(retries):
try:
- with urllib.request.urlopen(req, timeout=timeout) as resp:
+ with authenticated_urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read())
except urllib.error.HTTPError as e:
if e.code in _RETRY_CODES and attempt < retries - 1:
@@ -187,7 +188,7 @@ def strapi_put(base_url, api_key, path, payload, timeout=30, retries=3):
last_exc = None
for attempt in range(retries):
try:
- with urllib.request.urlopen(req, timeout=timeout) as resp:
+ with authenticated_urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read())
except urllib.error.HTTPError as e:
if e.code in _RETRY_CODES and attempt < retries - 1:
diff --git a/test/unit/test_broken_link_checker.py b/test/unit/test_broken_link_checker.py
index 46f82050..2b636004 100644
--- a/test/unit/test_broken_link_checker.py
+++ b/test/unit/test_broken_link_checker.py
@@ -22,6 +22,10 @@ crawl = checker.crawl
class TestBrokenLinkChecker(unittest.TestCase):
@classmethod
def setUpClass(cls):
+ # The production transport forbids loopback; the local server tests exercise
+ # crawl/status semantics using a deliberately injected test-only transport.
+ cls.transport_patch = patch.object(checker, "public_urlopen", side_effect=urllib.request.urlopen)
+ cls.transport_patch.start()
# Find a free port dynamically
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 0))
@@ -82,6 +86,7 @@ class TestBrokenLinkChecker(unittest.TestCase):
@classmethod
def tearDownClass(cls):
+ cls.transport_patch.stop()
cls.httpd.shutdown()
cls.server_thread.join()
@@ -125,7 +130,7 @@ class TestBrokenLinkChecker(unittest.TestCase):
self.assertEqual(seen, ['NotFairBrokenLinkChecker/1.0'])
def test_robots_fetch_failure_is_fail_open(self):
- real_urlopen = urllib.request.urlopen
+ real_urlopen = checker.public_urlopen
def fail_robots(request, *args, **kwargs):
url = request.full_url if hasattr(request, 'full_url') else request
@@ -133,7 +138,7 @@ class TestBrokenLinkChecker(unittest.TestCase):
raise urllib.error.URLError('robots unavailable')
return real_urlopen(request, *args, **kwargs)
- with patch.object(urllib.request, 'urlopen', side_effect=fail_robots):
+ with patch.object(checker, 'public_urlopen', side_effect=fail_robots):
broken = crawl(self.base_url, max_pages=1)
targets = [entry.get('target') for entry in broken]
diff --git a/test/unit/test_security_boundaries.py b/test/unit/test_security_boundaries.py
new file mode 100644
index 00000000..97dcddf2
--- /dev/null
+++ b/test/unit/test_security_boundaries.py
@@ -0,0 +1,144 @@
+"""Offline security regressions; test-only routing never enables private crawls."""
+import contextlib
+import http.server
+import importlib.util
+import io
+import json
+from pathlib import Path
+import socket
+import sys
+import threading
+import unittest
+import urllib.error
+import urllib.request
+from unittest.mock import patch, MagicMock
+
+ROOT = Path(__file__).resolve().parents[2]
+CMS = ROOT / 'seo/seo-analysis/scripts'
+sys.path.insert(0, str(CMS))
+
+def load(name, path):
+ spec = importlib.util.spec_from_file_location(name, path)
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[name] = module
+ spec.loader.exec_module(module)
+ return module
+
+checker = load('security_checker', ROOT/'seo/broken-link-checker/scripts/checker.py')
+calendar = load('security_calendar', ROOT/'scripts/notfair-content-calendar.py')
+helpers = [load('security_'+p.stem, p) for p in [CMS/n for n in (
+ 'preflight_wordpress.py','fetch_wordpress_content.py','preflight_strapi.py',
+ 'fetch_strapi_content.py','push_strapi_seo.py')]]
+
+class PublicTransport(unittest.TestCase):
+ def test_private_aliases_and_nonpublic_addresses_never_connect(self):
+ for url in ['http://127.0.0.1/', 'http://2130706433/', 'http://0x7f000001/',
+ 'http://[::1]/', 'http://[::ffff:127.0.0.1]/',
+ 'http://169.254.169.254/', 'http://100.64.0.1/', 'http://224.0.0.1/']:
+ with self.subTest(url=url), patch.object(checker.socket,'socket') as sock:
+ status, reason=checker.check_url(url)
+ self.assertIsNone(status)
+ self.assertIn('non-public',reason)
+ sock.assert_not_called()
+
+ def test_all_dns_answers_validated_and_actual_connect_pinned(self):
+ public=(socket.AF_INET,socket.SOCK_STREAM,6,'',('93.184.216.34',80))
+ private=(socket.AF_INET,socket.SOCK_STREAM,6,'',('127.0.0.1',80))
+ with patch.object(checker.socket,'getaddrinfo',return_value=[public,private]), patch.object(checker.socket,'socket') as sock:
+ with self.assertRaises(urllib.error.URLError): checker._public_connection(('example.com',80))
+ sock.assert_not_called()
+ with patch.object(checker.socket,'getaddrinfo',return_value=[public]) as dns, patch.object(checker.socket,'socket') as sock:
+ checker._public_connection(('example.com',80),timeout=3)
+ dns.assert_called_once()
+ sock.return_value.connect.assert_called_once_with(('93.184.216.34',80))
+
+ def test_scheme_credentials_and_proxy_cannot_bypass(self):
+ for url in ['file:///etc/hosts','ftp://example.com/a','http://user:[email protected]/','http://example.com:99999/']:
+ with self.subTest(url=url), self.assertRaises(urllib.error.URLError): checker.public_urlopen(url)
+ with patch.object(checker.urllib.request,'build_opener') as build:
+ checker.public_urlopen('https://example.com/')
+ self.assertEqual(build.call_args.args[0].proxies,{})
+ redirect=checker._PublicRedirectHandler()
+ with self.assertRaises(urllib.error.URLError):
+ redirect.redirect_request(urllib.request.Request('http://example.com/'),None,302,'',{},'ftp://example.com/')
+
+ def test_redirects_recheck_destination_and_preserve_public_fetch(self):
+ seen=[]
+ class Handler(http.server.BaseHTTPRequestHandler):
+ def do_HEAD(self):
+ seen.append(self.path)
+ self.send_response(302 if self.path=='/redirect' else 200)
+ if self.path=='/redirect': self.send_header('Location','http://127.0.0.1:'+str(self.server.server_port)+'/forbidden')
+ self.end_headers()
+ def log_message(self,*args): pass
+ with http.server.ThreadingHTTPServer(('127.0.0.1',0),Handler) as server:
+ thread=threading.Thread(target=server.serve_forever,daemon=True);thread.start()
+ port=server.server_port; real_dns=socket.getaddrinfo; real_socket=socket.socket
+ def dns(host,*args,**kwargs):
+ if host=='example.test': return [(socket.AF_INET,socket.SOCK_STREAM,6,'',('93.184.216.34',port))]
+ return real_dns(host,*args,**kwargs)
+ class TestSocket(real_socket):
+ def connect(self,address):
+ if address[0]=='93.184.216.34': address=('127.0.0.1',address[1])
+ return super().connect(address)
+ try:
+ with patch.object(checker.socket,'getaddrinfo',side_effect=dns), patch.object(checker.socket,'socket',TestSocket):
+ self.assertEqual(checker.check_url(f'http://example.test:{port}/ok'),(200,None))
+ self.assertIsNone(checker.check_url(f'http://example.test:{port}/redirect')[0])
+ with contextlib.redirect_stderr(io.StringIO()): checker.crawl(f'http://127.0.0.1:{port}/',max_pages=1)
+ self.assertEqual(seen,['/ok','/redirect'])
+ finally: server.shutdown();thread.join()
+
+class CmsRedirects(unittest.TestCase):
+ def test_every_cms_get_preserves_same_origin_and_rejects_other_origin(self):
+ seen=[]; foreign=[]
+ class Target(http.server.BaseHTTPRequestHandler):
+ def do_GET(self):
+ foreign.append(self.headers.get('Authorization'));self.send_response(200);self.end_headers();self.wfile.write(b'{}')
+ def log_message(self,*args): pass
+ class Cms(http.server.BaseHTTPRequestHandler):
+ def do_GET(self):
+ if self.path.split('?')[0]=='/ok':
+ seen.append(self.headers.get('Authorization'));self.send_response(200);self.send_header('X-WP-Total','3');self.send_header('X-WP-TotalPages','2');self.end_headers();self.wfile.write(b'{}')
+ else:
+ self.send_response(int(self.path.split('?')[0].rsplit('/',1)[-1]));self.send_header('Location',('/ok' if self.path.startswith('/same') else f'http://127.0.0.1:{other.server_port}/'));self.end_headers()
+ def log_message(self,*args): pass
+ with http.server.ThreadingHTTPServer(('127.0.0.1',0),Target) as other, http.server.ThreadingHTTPServer(('127.0.0.1',0),Cms) as server:
+ threads=[threading.Thread(target=s.serve_forever,daemon=True) for s in [other,server]]
+ for thread in threads: thread.start()
+ base=f'http://127.0.0.1:{server.server_port}'
+ try:
+ for helper in helpers:
+ fn=getattr(helper,'wp_get',None) or helper.strapi_get
+ codes=[301,302,303,307]+([308] if hasattr(urllib.request.HTTPRedirectHandler,'http_error_308') else [])
+ for code in codes:
+ with self.subTest(helper=helper.__name__,code=code):
+ result=fn(base,'fixture-token',f'/same/{code}',params={},retries=1)
+ if isinstance(result,tuple): self.assertEqual(result,({},3,2))
+ with self.assertRaises((urllib.error.HTTPError,SystemExit)): fn(base,'fixture-token',f'/other/{code}',params={},retries=1)
+ self.assertEqual(len(seen),5*len(codes));self.assertTrue(all(seen));self.assertEqual(foreign,[])
+ finally:
+ for s in [other,server]:s.shutdown()
+ for thread in threads:thread.join()
+
+ def test_origin_normalization_scheme_changes_and_put_behavior(self):
+ from _http import SameOriginAuthRedirectHandler
+ handler=SameOriginAuthRedirectHandler();req=urllib.request.Request('https://example.com/a',headers={'aUThorization':'fixture-token'})
+ same=handler.redirect_request(req,None,302,'',{},'https://EXAMPLE.com:443/b')
+ self.assertEqual(same.get_header('Authorization'),'fixture-token')
+ for url in ['http://example.com/a','https://other.example/a','https://example.com:444/a','https://example.com:0/a','https://[email protected]/a']:
+ with self.assertRaises(urllib.error.HTTPError):handler.redirect_request(req,None,302,'',{},url)
+ with self.assertRaises(urllib.error.HTTPError):handler.redirect_request(urllib.request.Request('https://example.com/a',data=b'{}',method='PUT'),None,302,'',{},'https://example.com/b')
+
+class CalendarEscaping(unittest.TestCase):
+ def test_both_metadata_fields_are_text_for_every_json_type(self):
+ for key in ['horizonWeeks','lookbackDays']:
+ for value in ['<script>alert(1)</script>',{'x':'<img src=x onerror=alert(1)>'},['<svg onload=alert(1)>']]:
+ page=calendar.render_page({'_path':'fixture.json','topics':[],key:value})
+ self.assertNotIn('<script>alert(1)</script>',page)
+ self.assertNotIn('<img src=x onerror=alert(1)>',page)
+ self.assertNotIn('<svg onload=alert(1)>',page)
+ self.assertIn('&lt;',page)
+ self.assertIn('90d lookback · 12w horizon',calendar.render_page({'_path':'fixture.json','topics':[],'lookbackDays':90,'horizonWeeks':12}))
+
+if __name__=='__main__': unittest.main()