Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a definitive check for possible password errors. #121

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions security/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (v *Vault) encrypt(data []byte) (string, error) {
// User has deleted all the secrets. the file will be empty.
return "", nil
}
data = append(data, []byte("\nGAIA_CHECK=!CHECK_ME!")...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks a bit magic to me. What do you think about moving this to constant variable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! 😊

paddedPassword := v.pad(v.cert)
ci := base64.URLEncoding.EncodeToString(paddedPassword)
block, err := aes.NewCipher([]byte(ci[:aes.BlockSize]))
Expand Down Expand Up @@ -241,6 +242,10 @@ func (v *Vault) decrypt(data []byte) ([]byte, error) {
if err != nil {
return []byte{}, err
}

if !bytes.Contains(unpadMsg, []byte("!CHECK_ME!")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. A bit magic. Maybe move that to a separate constant with a tiny description? 😄

return []byte{}, errors.New("possible mistyped password")
}
return unpadMsg, nil
}

Expand All @@ -253,11 +258,8 @@ func (v *Vault) parseToMap(data []byte) error {
row := bytes.Split(data, []byte("\n"))
for _, r := range row {
d := bytes.Split(r, []byte("="))
if len(d) < 2 {
// It is possible that if there is a password failure it's not caught
// by the padding process. Here it will be caught because we can't
// marshal the data into proper k/v pairs.
return errors.New("possible mistyped password")
if bytes.Equal(d[0], []byte("GAIA_CHECK")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same like above

continue
}
v.data[string(d[0])] = d[1]
}
Expand Down
2 changes: 1 addition & 1 deletion security/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestCloseLoadSecretsWithInvalidPassword(t *testing.T) {
t.Fatal(err)
}
v.data = make(map[string][]byte, 0)
v.cert = []byte("invalid")
v.cert = []byte("tset")
err = v.LoadSecrets()
if err == nil {
t.Fatal("error should not have been nil.")
Expand Down