Fix the URL path normalizer behind the fuzz test
Your task
An AI wrote a fuzz test for your URL path parser that only checks it does not crash. The fuzzer needs an oracle, a way to tell a right answer from a wrong one. So the team wants a normalizer that gives each path one standard form and never lets it climb above the root.
An assistant wrote normalize_path(raw). It is in the editor. It has a bug. Find it and fix it.
Input and output:
raw: the URL path as it arrived. It can contain percent escapes such as%2e.- Return the normalized path as a string, or
""to reject the path.
Rules the tests check:
- First decode two escapes:
%2ebecomes.and%2fbecomes/. The letter can be either case, so%2Eand%2Fcount too. Decode in one pass from left to right. Every other%sequence stays as it is. - After decoding, a path that does not start with
/is rejected. An empty input is rejected too. - Split the decoded path on
/. Drop empty segments (from//or a trailing/) and.segments. ..removes the last kept segment. A..with no kept segment to remove would climb above the root, so the whole path is rejected.- Keep every other segment exactly as it is, case included.
- Return
/followed by the kept segments joined with/. With no kept segments, return/.
Examples
normalize_path(raw: str) -> strExample 1
Input
normalize_path("/a/b/c")Output
"/a/b/c"Example 2
Input
normalize_path("//a//b/")Output
"/a/b"Example 3
Input
normalize_path("/a/../..")Output
""