From dfb8bf17cffee5625f0afcd04b4b4ff43066793a Mon Sep 17 00:00:00 2001 From: Ulysses Zhan Date: Sat, 17 May 2025 04:33:36 -0700 Subject: [PATCH] update-source-version: escape special characters when replacing url MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- pkgs/common-updater/scripts/update-source-version | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkgs/common-updater/scripts/update-source-version b/pkgs/common-updater/scripts/update-source-version index 2a46fdf11137..a4eb31550f91 100755 --- a/pkgs/common-updater/scripts/update-source-version +++ b/pkgs/common-updater/scripts/update-source-version @@ -229,10 +229,11 @@ if [[ -n "$newUrl" ]]; then die "Couldn't evaluate source url from '$attr.$sourceKey'!" fi - # Escape regex metacharacter that are allowed in store path names - oldUrlEscaped=$(echo "$oldUrl" | sed -re 's|[${}.+?]|\\&|g') + # Escape regex metacharacter that may appear in URLs + oldUrlEscaped=$(echo "$oldUrl" | sed -e 's|[*.^$[\|]|\\&|g') + newUrlEscaped=$(echo "$newUrl" | sed -e 's|[&\|]|\\&|g') - sed -i.cmp "$nixFile" -re "s|\"$oldUrlEscaped\"|\"$newUrl\"|" + sed -i.cmp "$nixFile" -e "s|\"$oldUrlEscaped\"|\"$newUrlEscaped\"|" if cmp -s "$nixFile" "$nixFile.cmp"; then die "Failed to replace source URL '$oldUrl' to '$newUrl' in '$attr'!" fi