Skip to content

Commit 56f8b2f

Browse files
Fix issue #135, allow whitespace in parent directories (#1210)
* Fix spaces in parent directory paths breaking gawk commands (issue #135) Refactor _gawk_inplace to pass arguments directly to gawk instead of constructing a command string for bash -c, which caused word-splitting on paths containing spaces. Update all callers to pass arguments directly instead of with single-quote wrapping for bash -c. Add test for git secret init in directory with spaces in parent path. Change TEST_DIR to "/tmp/git-secret-test/this dir has spaces" in utils/tests.sh so the entire test suite exercises paths with spaces. Replace the GPGTEST string variable with a _gpgtest function in _test_base.bash to properly handle --homedir quoting when the path contains spaces. Inline the bash -c gpg import logic directly. * Change cp command to use -R for recursive copy * Fix SC2005 lint: remove useless echo in install_fixture_full_key
1 parent 96c2555 commit 56f8b2f

7 files changed

Lines changed: 59 additions & 32 deletions

File tree

docs/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ POSTS_LOCATION='docs/_posts'
1111

1212

1313
function checkout_manuals {
14-
cp -r man/ docs/man
14+
cp -R man/ docs/man
1515
}
1616

1717

src/_utils/_git_secret_tools.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,11 @@ function _temporary_file {
202202

203203

204204
function _gawk_inplace {
205-
local parms="$*"
206-
local dest_file
207-
dest_file="$(echo "$parms" | gawk -v RS="'" -v FS="'" 'END{ gsub(/^\s+/,""); print $1 }')"
205+
local dest_file="${!#}" # last argument
208206

209207
_temporary_file
210208

211-
bash -c "gawk ${parms}" > "$temporary_filename"
209+
gawk "$@" > "$temporary_filename"
212210
mv "$temporary_filename" "$dest_file"
213211
}
214212

@@ -253,15 +251,15 @@ function _fsdb_rm_record {
253251
local key="$1" # required
254252
local fsdb="$2" # required
255253

256-
_gawk_inplace -v key="'$key'" "'$AWK_FSDB_RM_RECORD'" "$fsdb"
254+
_gawk_inplace -v "key=$key" "$AWK_FSDB_RM_RECORD" "$fsdb"
257255
}
258256

259257

260258
function _fsdb_clear_hashes {
261259
# First parameter is the path to fsdb
262260
local fsdb="$1" # required
263261

264-
_gawk_inplace "'$AWK_FSDB_CLEAR_HASHES'" "$fsdb"
262+
_gawk_inplace "$AWK_FSDB_CLEAR_HASHES" "$fsdb"
265263
}
266264

267265

src/commands/git_secret_hide.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function _fsdb_update_hash {
6464

6565
fsdb=$(_get_secrets_dir_paths_mapping)
6666

67-
_gawk_inplace -v key="'$key'" -v hash="$hash" "'$AWK_FSDB_UPDATE_HASH'" "$fsdb"
67+
_gawk_inplace -v "key=$key" -v "hash=$hash" "$AWK_FSDB_UPDATE_HASH" "$fsdb"
6868
}
6969

7070

src/commands/git_secret_init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function gitignore_add_pattern {
3636
gitignore_file_path=$(_prepend_root_path '.gitignore')
3737

3838
_maybe_create_gitignore
39-
_gawk_inplace -v pattern="$pattern" "'$AWK_ADD_TO_GITIGNORE'" "$gitignore_file_path"
39+
_gawk_inplace -v "pattern=$pattern" "$AWK_ADD_TO_GITIGNORE" "$gitignore_file_path"
4040
}
4141

4242
function init {

tests/_test_base.bash

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ function is_git_version_ge_2_28_0 { # based on code from github autopilot
5656
# GPG-based stuff:
5757
: "${SECRETS_GPG_COMMAND:='gpg'}"
5858

59-
# This command is used with absolute homedir set and disabled warnings:
60-
GPGTEST="$SECRETS_GPG_COMMAND --homedir=$TEST_GPG_HOMEDIR --no-permission-warning --batch"
59+
# This function is used with absolute homedir set and disabled warnings:
60+
function _gpgtest {
61+
"$SECRETS_GPG_COMMAND" --homedir "$TEST_GPG_HOMEDIR" --no-permission-warning --batch "$@"
62+
}
6163

6264
# Test key fixture data. Fixtures are at tests/fixtures/gpg/$email
6365

@@ -134,21 +136,11 @@ function stop_gpg_agent {
134136
}
135137

136138

137-
function get_gpgtest_prefix {
138-
if [[ $GPG_VER_21 -eq 1 ]]; then
139-
# shellcheck disable=SC2086
140-
echo "echo \"$(test_user_password $1)\" | "
141-
else
142-
echo ''
143-
fi
144-
}
145-
146-
147139
function get_gpg_fingerprint_by_email {
148140
local email="$1"
149141
local fingerprint
150142

151-
fingerprint=$($GPGTEST --with-fingerprint \
143+
fingerprint=$(_gpgtest --with-fingerprint \
152144
--with-colon \
153145
--list-secret-key "$email" | gawk "$AWK_GPG_GET_FP")
154146
echo "$fingerprint"
@@ -159,25 +151,27 @@ function install_fixture_key {
159151
local public_key="$BATS_TMPDIR/public-${1}.key"
160152

161153
cp "$FIXTURES_DIR/gpg/${1}/public.key" "$public_key"
162-
$GPGTEST --import "$public_key" >> "$TEST_OUTPUT_FILE" 2>&1
154+
_gpgtest --import "$public_key" >> "$TEST_OUTPUT_FILE" 2>&1
163155
rm -f "$public_key" || _abort "Couldn't delete public key: $public_key"
164156
}
165157

166158

167159
function install_fixture_full_key {
168160
local private_key="$BATS_TMPDIR/private-${1}.key"
169-
local gpgtest_prefix
170-
gpgtest_prefix=$(get_gpgtest_prefix "$1")
171-
local gpgtest_import="$gpgtest_prefix $GPGTEST"
172161
local email
173162
local fingerprint
174163

175164
email="$1"
176165

177166
cp "$FIXTURES_DIR/gpg/${1}/private.key" "$private_key"
178167

179-
bash -c "$gpgtest_import --allow-secret-key-import \
180-
--import \"$private_key\"" >> "${TEST_OUTPUT_FILE}" 2>&1
168+
if [[ "${GPG_VER_21:-0}" -eq 1 ]]; then
169+
test_user_password "$1" | _gpgtest --allow-secret-key-import \
170+
--import "$private_key" >> "${TEST_OUTPUT_FILE}" 2>&1
171+
else
172+
_gpgtest --allow-secret-key-import \
173+
--import "$private_key" >> "${TEST_OUTPUT_FILE}" 2>&1
174+
fi
181175

182176
# since 0.1.2 fingerprint is returned:
183177
fingerprint=$(get_gpg_fingerprint_by_email "$email")
@@ -194,7 +188,7 @@ function uninstall_fixture_key {
194188
local email
195189

196190
email="$1"
197-
$GPGTEST --yes --delete-key "$email" >> "$TEST_OUTPUT_FILE" 2>&1
191+
_gpgtest --yes --delete-key "$email" >> "$TEST_OUTPUT_FILE" 2>&1
198192
}
199193

200194

@@ -208,7 +202,7 @@ function uninstall_fixture_full_key {
208202
fingerprint=$(get_gpg_fingerprint_by_email "$email")
209203
fi
210204

211-
$GPGTEST --yes --delete-secret-keys "$fingerprint" >> "$TEST_OUTPUT_FILE" 2>&1
205+
_gpgtest --yes --delete-secret-keys "$fingerprint" >> "$TEST_OUTPUT_FILE" 2>&1
212206

213207
uninstall_fixture_key "$1"
214208
}

tests/test_init.bats

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,41 @@ function teardown {
8282
}
8383

8484

85+
@test "run 'init' in directory with spaces in parent path" {
86+
# This test covers this issue:
87+
# https://github.com/sobolevn/git-secret/issues/135
88+
89+
if [[ "$BATS_RUNNING_FROM_GIT" -eq 1 ]]; then
90+
skip "this test is skipped while 'git commit'. See #334"
91+
fi
92+
93+
local test_dir="$BATS_TMPDIR/path with spaces"
94+
local current_dir="$PWD"
95+
96+
mkdir -p "$test_dir"
97+
cd "$test_dir"
98+
99+
local has_initial_branch_option
100+
has_initial_branch_option=$(is_git_version_ge_2_28_0)
101+
if [[ "$has_initial_branch_option" == 0 ]]; then
102+
git init --initial-branch=main >> "$TEST_OUTPUT_FILE" 2>&1
103+
else
104+
git init >> "$TEST_OUTPUT_FILE" 2>&1
105+
fi
106+
107+
run git secret init
108+
[ "$status" -eq 0 ]
109+
110+
local secrets_dir
111+
secrets_dir=$(_get_secrets_dir)
112+
[[ -d "$secrets_dir" ]]
113+
114+
# Cleaning up:
115+
cd "$current_dir"
116+
rm -rf "$test_dir"
117+
}
118+
119+
85120
@test "run 'init' with '.gitsecret' already initialized" {
86121
local secrets_dir
87122
secrets_dir=$(_get_secrets_dir)

utils/tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
set -e
66

7-
TEST_DIR=/tmp/git-secret-test
7+
TEST_DIR="/tmp/git-secret-test/this dir has spaces"
88

99
rm -rf "${TEST_DIR}"
10-
mkdir "${TEST_DIR}"
10+
mkdir -p "${TEST_DIR}"
1111
echo "# created dir: ${TEST_DIR}"
1212

1313
chmod 0700 "${TEST_DIR}"

0 commit comments

Comments
 (0)