diff --git a/edge/src/api.py b/edge/src/api.py index 39d4d7d..7bb7b8d 100644 --- a/edge/src/api.py +++ b/edge/src/api.py @@ -11,6 +11,10 @@ from flask_compress import Compress from . import analytics, cache as cache_mod, config app = Flask(__name__) +# Tolerate stray trailing slashes on every route, matching the generator. A +# hand-copied embed URL like `/alice/` should resolve like `/alice` instead of +# Flask's default strict_slashes 404. Set before the route decorators run. +app.url_map.strict_slashes = False Compress(app) cache_ext = cache_mod.build_cache(app) log = logging.getLogger("edge") diff --git a/edge/tests/test_api.py b/edge/tests/test_api.py index bb22e87..068c891 100644 --- a/edge/tests/test_api.py +++ b/edge/tests/test_api.py @@ -44,6 +44,17 @@ def test_origin_error_returns_503(client): assert r.status_code == 503 +@responses.activate +def test_trailing_slash_tolerated(client): + """A hand-copied `/alice/` must resolve like `/alice`, not 404.""" + responses.add(responses.GET, "http://gen:5002/api/alice", + body=b"ready", status=200, + headers={"X-Widget-Status": "ready", "Content-Type": "image/svg+xml"}) + r = client.get("/alice/") + assert r.status_code == 200 + assert r.data == b"ready" + + def test_health(client): r = client.get("/health") assert r.status_code == 200 diff --git a/generator/src/api.py b/generator/src/api.py index 48fa8cd..625bdc3 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -208,6 +208,14 @@ _STATIC_DIR = os.path.join(os.path.dirname(__file__), "static") app = Flask(__name__, static_folder=None) +# Tolerate stray trailing slashes on every route. README embeds are copied by +# hand from all over — upstream github-readme-stats even prints its canonical +# URL as `/api/top-langs/?username=X` (note the '/' before '?'). Flask's +# default strict_slashes turns any such URL into a hard 404 with no redirect, +# so a single misplaced slash silently breaks a user's widget. Set on the map +# before the route decorators run so every rule inherits the lenient default. +app.url_map.strict_slashes = False + if config.SECRET_KEY: app.secret_key = config.SECRET_KEY else: diff --git a/generator/tests/test_api.py b/generator/tests/test_api.py index ed6ee69..ca5fcd0 100644 --- a/generator/tests/test_api.py +++ b/generator/tests/test_api.py @@ -480,6 +480,45 @@ def test_top_langs_ignores_upstream_specific_query_params(client): assert b"langs" in r.data +# ---- Trailing-slash tolerance ----------------------------------------------- +# Flask's default strict_slashes turns `/api/top-langs/?username=X` — the exact +# shape upstream github-readme-stats prints (note the '/' before '?') — into a +# hard 404 with no redirect. Every /api route had the same latent bug: any +# copied URL carrying a stray trailing slash 404'd. These pin the lenient +# behavior so both forms resolve. + + +def test_top_langs_trailing_slash_before_query(client): + """Upstream's canonical URL is `/api/top-langs/?username=X`. The trailing + slash must not 404 — it's the single most common broken embed.""" + dbmod.enroll("alice", {"theme": "dark"}) + dbmod.put_widgets("alice", "h1", {"languages": "langs"}) + dbmod.point_current_widget("alice", "h1") + r = client.get("/api/top-langs/?username=alice") + assert r.status_code == 200 + assert b"langs" in r.data + + +def test_widget_first_trailing_slash(client): + """`/api/streak/alice/` must resolve to the same widget as `/api/streak/alice`.""" + dbmod.enroll("alice", {"theme": "dark"}) + dbmod.put_widgets("alice", "h1", {"streaks": "streaksvg"}) + dbmod.point_current_widget("alice", "h1") + r = client.get("/api/streak/alice/") + assert r.status_code == 200 + assert b"streaksvg" in r.data + + +def test_composite_trailing_slash(client): + """`/api/alice/` must resolve like `/api/alice`.""" + dbmod.enroll("alice", {"theme": "dark"}) + dbmod.put_widgets("alice", "h1", {"composite": "compositesvg"}) + dbmod.point_current_widget("alice", "h1") + r = client.get("/api/alice/") + assert r.status_code == 200 + assert b"compositesvg" in r.data + + def test_enroll_endpoint_is_gone(client): r = client.post("/api/enroll", json={"username": "alice"}, headers={"Origin": "https://gh-stats.com"})