-
Notifications
You must be signed in to change notification settings - Fork 210
fix: safely interpolate variables inside rules regex literals #1889
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -245,6 +245,39 @@ export class Utils { | |||||
| return binary; | ||||||
| }; | ||||||
|
|
||||||
| // A `$VAR` that sits *inside* a regex literal on the RHS of `=~`/`!~` | ||||||
| // must be substituted with its value, not a quoted JS string literal. | ||||||
| // The general expansion below wraps every value in quotes (needed for | ||||||
| // `==` operands), which would otherwise leak `"` characters into the | ||||||
| // regex pattern, e.g. turning | ||||||
| // $PHP_VERSION =~ /^\$_TARGET_PHP$/ (with _TARGET_PHP=8.3) | ||||||
| // into the malformed pattern `^\"8.3"$` instead of `^8.3$`. | ||||||
| // In a regex context GitLab still expands an escaped `\$VAR`, dropping | ||||||
| // the escaping backslash. We do this as a pre-pass, before the general | ||||||
| // expansion, so that no `$VAR` remains inside literal regexes; this | ||||||
| // leaves the "RHS is a variable that holds a regex" case (e.g. | ||||||
| // `$TAG =~ $TAG_REGEX`) to the general quoted expansion as before. | ||||||
| // The substituted value is regex-escaped so it matches literally and a | ||||||
| // value containing regex metacharacters (a branch like `feat/x`, or a | ||||||
| // value carrying `/`, `|`, `(`, ...) cannot break out of the literal, | ||||||
| // close it early, or inject expression syntax into the eval. | ||||||
| const escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&"); | ||||||
| const regexLiteralRhs = /(?<op>=~|!~)(?<pre>\s*["']?)\/(?<pattern>(?:\\.|[^/\\])*)\//g; | ||||||
| evalStr = evalStr.replaceAll(regexLiteralRhs, (_match, op, pre, pattern) => { | ||||||
| const expandedPattern = pattern.replaceAll( | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Escaping breaks regex fragments: |
||||||
| /(\$\$)|\\?\$\{([a-zA-Z_]\w*)}|\\?\$([a-zA-Z_]\w*)/g, | ||||||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||||||
| (_m: string, escape: string, var1: string, var2: string) => { | ||||||
| // Leave a `$$` escape intact for the global unescape pass | ||||||
| // (`$$` -> literal `$`). Returning a single `$` here would | ||||||
| // re-expose the following name as a `$VAR` to that pass and | ||||||
| // expand it, defeating the escape. | ||||||
| if (escape !== undefined) return "$$"; | ||||||
| return escapeRegExp(envs[var1 || var2] ?? ""); | ||||||
| }, | ||||||
| ); | ||||||
| return `${op}${pre}/${expandedPattern}/`; | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unset var →
Suggested change
Still |
||||||
| }); | ||||||
|
|
||||||
| // Expand all variables | ||||||
| evalStr = this.expandTextWith(evalStr, { | ||||||
| unescape: JSON.stringify("$"), | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repo doesn't carry source comments