@@ -222,6 +222,7 @@ git_clean() {
222
222
fi
223
223
}
224
224
225
+ # shellcheck disable=SC2005,SC2155
225
226
git_smudge () {
226
227
tempfile=$( mktemp 2> /dev/null || mktemp -t tmp)
227
228
trap ' rm -f "$tempfile"' EXIT
@@ -230,7 +231,32 @@ git_smudge() {
230
231
cipher=$( git config --get --local " transcrypt${context_config_group} .cipher" )
231
232
password=$( load_password " $context_config_group " )
232
233
openssl_path=$( git config --get --local transcrypt.openssl-path)
233
- tee " $tempfile " | ENC_PASS=$password " $openssl_path " enc -d " -${cipher} " -md MD5 -pass env:ENC_PASS -a 2> /dev/null || cat " $tempfile "
234
+
235
+ # Write stdin to $tempfile, while skimming the first bytes at the same time
236
+ local firstbytes=$( tee " $tempfile " | head -c8 | LC_ALL=C tr -d ' \0' )
237
+ # If the first bytes are "Salted", then the file is encrypted
238
+ if [[ $firstbytes == " U2FsdGVk" ]]; then
239
+ # Fix for file mistakenly encrypted with double "Salted" prefixes due to #147
240
+ # that causes garbage characters at top of decrypted files.
241
+ #
242
+ # Check file header, which we already know starts with "Salted", to see if
243
+ # it has exactly the same "Salted__XYZ" prefix mistakenly repeated twice
244
+ local header_decoded=$( echo " $( head -c48 < " $tempfile " ) " | openssl base64 -d)
245
+ local first_salt_prefix=$( echo " $header_decoded " | cut -b 1-16) # First 16 bytes
246
+ local maybe_second_salt_prefix=$( echo " $header_decoded " | cut -b 17-32) # Second 16 bytes
247
+
248
+ # If the salted prefix is repeated -- and not empty, to avoid mistaken match if
249
+ # base64 decoding fails -- remove the first occurrence before decrypting...
250
+ if [[ " $first_salt_prefix " && " $first_salt_prefix " == " $maybe_second_salt_prefix " ]]; then
251
+ openssl base64 -d < " $tempfile " | tail -c+17 | ENC_PASS=$password " $openssl_path " enc -d " -${cipher} " -md MD5 -pass env:ENC_PASS 2> /dev/null
252
+ # ...otherwise decrypt as normal
253
+ else
254
+ ENC_PASS=$password " $openssl_path " enc -d -a " -${cipher} " -md MD5 -pass env:ENC_PASS < " $tempfile " 2> /dev/null
255
+ fi
256
+ # If the first bytes are not "Salted", the file is not encrypted so output it unchanged
257
+ else
258
+ cat " $tempfile "
259
+ fi
234
260
}
235
261
236
262
git_textconv () {
0 commit comments