Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid double-escaping in url_escape #30

Merged
merged 2 commits into from
Nov 30, 2023

Conversation

plobsing
Copy link
Contributor

@plobsing plobsing commented Nov 4, 2023

Because the escapes for other characters will put % into the output text, we need to perform replacements for % first. This ensures that all chars we replace this way were from the input text and not put there by some prior replacement step.

The prior implementation would produce double-escaped values for the following characters:

Input char Double-escaped Fixed
" " "%2520" "%20"
"!" "%2521" "%21"
'"' "%2522" "%22"
"#" "%2523" "%23"
"$" "%2524" "%24"

@plobsing plobsing force-pushed the patch-1 branch 2 times, most recently from 52378a8 to 8a9e26d Compare November 5, 2023 17:08
Because the escapes for other characters will put `%` into the output text, we need to perform replacements for `%` first. This ensures that all chars we replace this way were from the input text and not put there by some prior replacement step.

The prior implementation would produce double-escaped values for the following characters:

| Input char | Double-escaped | Fixed   |
| ---------- | -------------- | ------- |
| `" "`      | `"%2520"`      | `"%20"` |
| `"!"`      | `"%2521"`      | `"%21"` |
| `'"'`      | `"%2522"`      | `"%22"` |
| `"#"`      | `"%2523"`      | `"%23"` |
| `"$"`      | `"%2524"`      | `"%24"` |
@imjasonh imjasonh enabled auto-merge (squash) November 30, 2023 13:04
@imjasonh imjasonh merged commit 037a4c9 into chainguard-dev:main Nov 30, 2023
7 of 8 checks passed
plobsing added a commit to plobsing/bazel-lib that referenced this pull request Nov 22, 2024
This pair of utilities from Python's core are helpful when encoding or escaping strings.
Unlike the common alternative — repeated application of `str.replace` —
a `str.translate` implementation performs its work in a single pass.

This isn't principally about efficiency — although a single-pass
implementation may be more efficient — but rather about correctness.
Doing all the translation in a single pass
sidesteps the issue of double-encoding errors which are possible under
repeated-processing schemes when when substitution input/output aliasing
is present (i.e. some substitutions produce output that other
substitutions recognize as to-be-replaced input).
See chainguard-dev/rules_apko#30 for an concrete
example of a double-encoding issue resulting from a repeated-processing
translation implementation.
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