Skip to content

Accept uppercase letters and dots in uri-template variable names - #246

Merged
horejsek merged 1 commit into
horejsek:masterfrom
bardrop-oss:fix-uri-template-varnames
Nov 29, 2026
Merged

horejsek merged 1 commit into
horejsek:masterfrom
bardrop-oss:fix-uri-template-varnames

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What is wrong

The uri-template format rejects valid RFC 6570 templates when a variable name has an uppercase letter or a dot, or when a percent-encoding uses uppercase hex:

import fastjsonschema
v = fastjsonschema.compile({'$schema': 'http://json-schema.org/draft-07/schema', 'format': 'uri-template'})
v('http://example.com/{userId}')   # JsonSchemaValueException: data must be uri-template
v('{user.name}')                   # JsonSchemaValueException
v('http://example.com/%2F')        # JsonSchemaValueException (but '%2f' passes)

Expected: all three are valid. RFC 6570 section 2.3:

varname = varchar *( ["."] varchar )
varchar = ALPHA / DIGIT / "_" / pct-encoded

ALPHA is A-Z and a-z, and HEXDIG in pct-encoded includes A-F.

Cause

fastjsonschema/draft06.py, the uri-template regex uses [a-z0-9_] for a varchar, has no way to put . inside a name, and uses %[0-9a-f]{2} for percent-encoding (in literals and in names).

Fix

Use [a-zA-Z0-9_] and %[0-9a-fA-F]{2}, and allow a single . between varchars. The varspec part is pulled into a small constant so it is not written twice. Invalid names like {a.}, {a..b}, {} and {a:0} are still rejected. No backtracking problem: a 150 KB invalid template is checked in a few ms.

I also checked it with a small generator of RFC-valid templates (5000 each): before the fix 2897 of 4375 with uppercase names, 2164 of 4606 with uppercase hex and 2629 of 4580 with dotted names were rejected; after the fix 0. Lowercase-only templates were 0 rejected before and after.

Test

Added test_uri_template in tests/test_format.py (15 cases). 5 of them fail on master and all pass with the fix. Full suite: 5615 passed before, 5630 passed after. Added a CHANGELOG line.

RFC 6570 defines varchar as ALPHA / DIGIT / "_" / pct-encoded and
varname as varchar *( ["."] varchar ). The regex only allowed lowercase
letters, no dots and lowercase hex in percent-encoding, so templates such
as http://example.com/{userId}, {user.name} and %2F were rejected.
@horejsek
horejsek merged commit 7abe5ed into horejsek:master Nov 29, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants