Repository navigation
JWT middleware: verify signatures, one algorithm family by default, 401 for every bad token - #24
Merged
Conversation
…hm family
mk_jwt_middleware now:
- passes options={"verify_signature": verify} (PyJWT 2 ignored the legacy "verify" key);
- takes algorithms= (keyword-only, default ("HS256",)) and raises ValueError when
HMAC and public-key algorithms are mixed under one secret, unless
allow_mixed_algorithm_families=True (the old ["HS256", "RS256"] default);
- answers every invalid token (expired, wrong algorithm, ...) with 401 instead of
letting non-DecodeError exceptions become 500s, and raises the 401 rather than
returning it (deprecated in aiohttp);
- no longer echoes the token back in the 401 body;
- requires the "Bearer " scheme;
- lets requests through without a token by aiohttp route name (public_routes=,
default ("ping", "openapi")), not by handler __name__, so a handler that happens
to be called "ping" is not public.
Closes #21
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t, 401 for unusable keys Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… generic text for key errors Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #21.
Changes to
mk_jwt_middlewareoptions={"verify": verify}(ignored by PyJWT 2)options={"verify_signature": verify}["HS256", "RS256"]with onesecretalgorithms=, default("HS256",)(asJWTPlugin)ValueErrorunlessallow_mixed_algorithm_families=Truejwt.DecodeErroronly → others became 500jwt.InvalidTokenError→ 401HTTPUnauthorized(deprecated in aiohttp)AuthorizationheaderBearerscheme required (case-insensitive)__name__in{"ping", "openapi"}— any handler namedpingwas publicpublic_routes=(default("ping", "openapi"))|
verify=False| legacy option ignored, so signatures were still checked | signatures and claims really are skipped: a forged or expired token's claims land onrequest.token(whatverify=Falsealways claimed to do) || bad config (
algorithms="HS256",["hs256"],"none", empty/non-str HMAC secret,public_routes="ping") | 500 on every request, or silently rejected every token |TypeError/ValueErrorwhen the middleware is built |Note: the 401 is now raised, so an outer middleware that turns every exception into a 500 will now also turn this 401 into a 500. That middleware would already break all of aiohttp's own HTTP exceptions.
verifystays the 2nd positional argument, somk_jwt_middleware(secret)andmk_jwt_middleware(secret, False)keep working.Opt-ins for the old behaviour
algorithms=["HS256", "RS256"], allow_mixed_algorithm_families=Truerestores the old algorithm list (documented as the algorithm-confusion setup).ping/openapimust now be registered with that routename=(or have its name added topublic_routes).Tests
py2http/tests/test_jwt_middleware.py(21, real aiohttp test server on an ephemeral localhost port): valid token decoded; missing/garbage/wrong-secret/alg: none/expired/disallowed-alg tokens → 401 without echo; public route by name vs a handler namedping; mixed-familyValueError; an HS256-with-public-key forgery against an RS256 config → 401;verify=False. 9 of the 12 fail on master. Full suite: 74 passed.Adversarial review
Fresh Opus reviewer, three passes.
alg: none/NONE, lowercasehs256, an HS256 token signed with the RSA public key,jwk/kid/crit/b64header tricks,Bearervariants, sub-paths, HEAD/OPTIONS, 404s and unnamed routes. Confirmed and fixed:verify=Falsechange: now in the table and the docstring.PyJWTError).algorithmsas a string, or in the wrong case: rejected at build.Noneinpublic_routesno longer opens unnamed routes.public_routesas a string is rejected.JWTPlugin(bottle) parity, andaudience/issuerpassthrough.Dependents gate (
priv test-dependents py2http --transitive): py2http and extrude pass.mk_jwt_middlewarehas no callers outside py2http.🤖 Generated with Claude Code