Skip to content

Commit 48b801d

Browse files
authored
Improve check for incorrect password when --force is used at init time #196
Improve check for incorrect password to avoid false report when transcrypt init is run with --force in a repo containing dirty files. Also added tests.
2 parents a96733f + 94627fe commit 48b801d

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ system, you must also run the `--upgrade` command in each repository:
3434

3535
## [Unreleased]
3636

37+
### Changed
38+
39+
- Improve check for incorrect password to avoid false report when transcrypt
40+
init is run with --force in a repo containing dirty files & add tests (#196)
41+
3742
### Fixed
3843

3944
- Fix pre-commit hook to use "fast" multi-threaded mode for Bash versions 5+ as

tests/_test_helper.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ function init_transcrypt {
4343
"$BATS_TEST_DIRNAME"/../transcrypt --cipher=aes-256-cbc --password='abc 123' --yes
4444
}
4545

46+
function uninstall_transcrypt {
47+
"$BATS_TEST_DIRNAME"/../transcrypt --uninstall --yes
48+
}
49+
4650
function encrypt_named_file {
4751
filename="$1"
4852
content=$2

tests/test_init.bats

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,46 @@ SETUP_SKIP_INIT_TRANSCRYPT=1
168168
[ "${lines[0]}" = "==> sensitive_file <==" ]
169169
[ "${lines[1]}" = "$SECRET_CONTENT_ENC" ]
170170
}
171+
172+
@test "crypt: warn on incorrect password as indicated by dirty files after init" {
173+
init_transcrypt
174+
175+
SECRET_CONTENT="My secret content"
176+
SECRET_CONTENT_ENC="U2FsdGVkX1/6ilR0PmJpAyCF7iG3+k4aBwbgVd48WaQXznsg42nXbQrlWsf/qiCg"
177+
178+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
179+
180+
# Clear the password and reset the repo
181+
uninstall_transcrypt
182+
git reset --hard
183+
184+
# Init transcrypt with wrong password, command fails with error message
185+
run "$BATS_TEST_DIRNAME"/../transcrypt --cipher=aes-256-cbc --password='WRONG' --yes
186+
[ "$status" -eq 1 ]
187+
[ "${lines[0]}" = "transcrypt: Unexpected new dirty files in the repository when configured by transcrypt, please check your password." ]
188+
}
189+
190+
@test "crypt: warn on incorrect password as indicated by dirty files after init when forced" {
191+
init_transcrypt
192+
193+
SECRET_CONTENT="My secret content"
194+
SECRET_CONTENT_ENC="U2FsdGVkX1/6ilR0PmJpAyCF7iG3+k4aBwbgVd48WaQXznsg42nXbQrlWsf/qiCg"
195+
196+
encrypt_named_file sensitive_file "$SECRET_CONTENT"
197+
198+
# Clear the password and reset the repo
199+
uninstall_transcrypt
200+
git reset --hard
201+
202+
# Dirty repo before init, to check pre- and post-init dirty files counts
203+
# work despite the pre-existing dirty file
204+
echo "Dirty file" > dirty_file
205+
git add dirty_file
206+
207+
# Force init transcrypt with wrong password, command fails with error message
208+
run "$BATS_TEST_DIRNAME"/../transcrypt --force --cipher=aes-256-cbc --password='WRONG' --yes
209+
[ "$status" -eq 1 ]
210+
[ "${lines[0]}" = "transcrypt: Unexpected new dirty files in the repository when configured by transcrypt, please check your password." ]
211+
212+
rm dirty_file
213+
}

transcrypt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,13 @@ elif [[ $cipher ]]; then
15961596
validate_cipher
15971597
fi
15981598

1599+
count_dirty_files() {
1600+
{ git diff-index --name-status HEAD -- || git diff-index --stat HEAD -- || git diff-index HEAD -- || true; } 2>/dev/null | grep $'\n' -c
1601+
}
1602+
1603+
# shellcheck disable=SC2155
1604+
readonly GIT_DIRTY_FILES_BEFORE_INIT="$(count_dirty_files)"
1605+
15991606
# perform function calls to configure transcrypt
16001607
get_cipher
16011608
get_password
@@ -1620,12 +1627,10 @@ if [[ ! -f $GIT_ATTRIBUTES ]]; then
16201627
printf '#pattern filter=crypt diff=crypt merge=crypt\n' >"$GIT_ATTRIBUTES"
16211628
fi
16221629

1623-
# check for modified (dirty) files after transcrypt configuration which could
1630+
# check for newly modified (dirty) files after transcrypt configuration which could
16241631
# indicate an incorrect password
1625-
#
1626-
# check if the repo is dirty
1627-
if git status --porcelain 2>/dev/null | grep "^ M" >/dev/null; then
1628-
die 1 'Unexpected dirty files in the repository when configured by transcrypt%s, check your password.\n' "$CONTEXT_DESCRIPTION"
1632+
if [[ ${GIT_DIRTY_FILES_BEFORE_INIT} -lt $(count_dirty_files) ]]; then
1633+
die 1 'Unexpected new dirty files in the repository when configured by transcrypt%s, please check your password.\n' "$CONTEXT_DESCRIPTION"
16291634
fi
16301635

16311636
printf 'The repository has been successfully configured by transcrypt%s.\n' "$CONTEXT_DESCRIPTION"

0 commit comments

Comments
 (0)