Skip to content

Commit d6beab0

Browse files
committed
fixup: added handling of trailing tabs/spaces in password files. Causes edge case failure for passwords with intentional trailing whitespace
Signed-off-by: kaldun-tech <tsmereka@protonmail.com>
1 parent c183c4b commit d6beab0

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

loopdb/secret.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ func (s *Secret) UnmarshalFlag(value string) error {
3131
return fmt.Errorf("failed to read secret file %s: %w",
3232
filePath, err)
3333
}
34-
// Trim trailing newlines. Trim both \r and \n to handle
35-
// files created on Windows (CRLF) or Unix (LF).
36-
*s = Secret(strings.TrimRight(string(content), "\r\n"))
34+
// Trim trailing whitespace (spaces, tabs, newlines) to handle
35+
// files created on Windows (CRLF) or Unix (LF), and to avoid
36+
// invisible trailing spaces causing authentication failures.
37+
*s = Secret(strings.TrimRight(string(content), " \t\r\n"))
3738

3839
return nil
3940
}

loopdb/secret_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ func TestSecretUnmarshalFlag(t *testing.T) {
7474
require.Equal(t, Secret("secretpassword"), s)
7575
})
7676

77+
t.Run("file with trailing whitespace", func(t *testing.T) {
78+
t.Parallel()
79+
80+
tmpDir := t.TempDir()
81+
passFile := filepath.Join(tmpDir, "password.txt")
82+
err := os.WriteFile(passFile, []byte("secretpassword \t\n"), 0600)
83+
require.NoError(t, err)
84+
85+
var s Secret
86+
err = s.UnmarshalFlag("@" + passFile)
87+
require.NoError(t, err)
88+
require.Equal(t, Secret("secretpassword"), s)
89+
})
90+
7791
t.Run("empty file", func(t *testing.T) {
7892
t.Parallel()
7993

0 commit comments

Comments
 (0)