Skip to content

Commit e5f2878

Browse files
committed
Escape sed-special characters in rename_project.sh
Address Sourcery review: project names containing \, &, or / could corrupt sed substitutions. Add escape_sed helper to sanitize all positional parameters before interpolating into sed patterns.
1 parent dec885e commit e5f2878

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

scripts/rename_project.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
#!/bin/bash
22

3+
# Escape characters special in sed replacement strings (\, &, /)
4+
escape_sed() { printf '%s\n' "$1" | sed 's/[\\&/]/\\&/g'; }
5+
36
mv python_template "$1"
47

8+
ESCAPED_1=$(escape_sed "$1")
9+
510
# change project name in all files (exclude main devcontainer.json to protect template image URL)
6-
find . \( -type d -name .git -prune \) -o \( -type f -not -name 'tasks.json' -not -name 'update_from_template.sh' -not -name 'pixi.lock' -not -path './.devcontainer/devcontainer.json' \) -print0 | xargs -0 sed -i "s/python_template/$1/g"
11+
find . \( -type d -name .git -prune \) -o \( -type f -not -name 'tasks.json' -not -name 'update_from_template.sh' -not -name 'pixi.lock' -not -path './.devcontainer/devcontainer.json' \) -print0 | xargs -0 sed -i "s/python_template/$ESCAPED_1/g"
712

813
# update just the name field in devcontainer.json
9-
sed -i "s/\"name\": \"python_template\"/\"name\": \"$1\"/" .devcontainer/devcontainer.json
14+
sed -i "s/\"name\": \"python_template\"/\"name\": \"$ESCAPED_1\"/" .devcontainer/devcontainer.json
1015

1116
# regenerate lockfile to match renamed project
1217
pixi update
1318

1419
# author name
1520
if [ -n "$2" ]; then
16-
find . \( -type d -name .git -prune \) -o \( -type f -not -name 'tasks.json' -not -name 'update_from_template.sh' \) -print0 | xargs -0 sed -i "s/Austin Gregg-Smith/$2/g"
21+
ESCAPED_2=$(escape_sed "$2")
22+
find . \( -type d -name .git -prune \) -o \( -type f -not -name 'tasks.json' -not -name 'update_from_template.sh' \) -print0 | xargs -0 sed -i "s/Austin Gregg-Smith/$ESCAPED_2/g"
1723
fi
1824

1925
# author email
2026
if [ -n "$3" ]; then
21-
find . \( -type d -name .git -prune \) -o \( -type f -not -name 'tasks.json' -not -name 'update_from_template.sh' \) -print0 | xargs -0 sed -i "s/blooop@gmail.com/$3/g"
27+
ESCAPED_3=$(escape_sed "$3")
28+
find . \( -type d -name .git -prune \) -o \( -type f -not -name 'tasks.json' -not -name 'update_from_template.sh' \) -print0 | xargs -0 sed -i "s/blooop@gmail.com/$ESCAPED_3/g"
2229
fi
2330

2431
# github username (exclude main devcontainer.json to protect template image URL)
2532
if [ -n "$4" ]; then
26-
find . \( -type d -name .git -prune \) -o \( -type f -not -name 'setup_host.sh' -not -name 'update_from_template.sh' -not -path './.devcontainer/devcontainer.json' \) -print0 | xargs -0 sed -i "s/blooop/$4/g"
33+
ESCAPED_4=$(escape_sed "$4")
34+
find . \( -type d -name .git -prune \) -o \( -type f -not -name 'setup_host.sh' -not -name 'update_from_template.sh' -not -path './.devcontainer/devcontainer.json' \) -print0 | xargs -0 sed -i "s/blooop/$ESCAPED_4/g"
2735
fi

0 commit comments

Comments
 (0)