Skip to content

Commit dfb8bf1

Browse files
UlyssesZhjtojnar
andauthored
update-source-version: escape special characters when replacing url
URLs can contain characters that sed would consider special in the context of the regex pattern. Let’s switch the URL replacement `sed` command to use POSIX Basic Regular Expression syntax to reduce the number of characters that need to be escaped: https://www.gnu.org/software/sed/manual/html_node/BRE-syntax.html Then, let’s escape all BRE special characters, plus the separator character `|` of the `s` command in the old URL pattern. Similarly, the replacement part of the `s` command (new URL) can also contain special characters (e.g. the `&` character would be replaced with the whole matched expression), so we need to escape it as well: https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html Co-Authored-By: Jan Tojnar <[email protected]>
1 parent 49b6072 commit dfb8bf1

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

pkgs/common-updater/scripts/update-source-version

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,11 @@ if [[ -n "$newUrl" ]]; then
229229
die "Couldn't evaluate source url from '$attr.$sourceKey'!"
230230
fi
231231

232-
# Escape regex metacharacter that are allowed in store path names
233-
oldUrlEscaped=$(echo "$oldUrl" | sed -re 's|[${}.+?]|\\&|g')
232+
# Escape regex metacharacter that may appear in URLs
233+
oldUrlEscaped=$(echo "$oldUrl" | sed -e 's|[*.^$[\|]|\\&|g')
234+
newUrlEscaped=$(echo "$newUrl" | sed -e 's|[&\|]|\\&|g')
234235

235-
sed -i.cmp "$nixFile" -re "s|\"$oldUrlEscaped\"|\"$newUrl\"|"
236+
sed -i.cmp "$nixFile" -e "s|\"$oldUrlEscaped\"|\"$newUrlEscaped\"|"
236237
if cmp -s "$nixFile" "$nixFile.cmp"; then
237238
die "Failed to replace source URL '$oldUrl' to '$newUrl' in '$attr'!"
238239
fi

0 commit comments

Comments
 (0)