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

fix(resolution): restore hex.Decode/Encode when loading/storing ciphered data from database #395

Merged
merged 2 commits into from
Nov 23, 2022
Merged
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
18 changes: 17 additions & 1 deletion models/resolution/resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resolution

import (
"bytes"
"encoding/hex"
"encoding/json"
"time"

Expand Down Expand Up @@ -166,6 +167,9 @@ func Create(dbp zesty.DBProvider, t *task.Task, resolverInputs map[string]interf
if err != nil {
return nil, err
}

dst := make([]byte, 0, hex.EncodedLen(len(encryptedSteps)))
Copy link

Choose a reason for hiding this comment

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

Suggested change
dst := make([]byte, 0, hex.EncodedLen(len(encryptedSteps)))
dst := make([]byte, hex.EncodedLen(len(encryptedSteps)))

From the official documentation, it uses the size of the buffer: https://pkg.go.dev/encoding/hex#Encode

Copy link
Contributor

@wI2L wI2L Nov 22, 2022

Choose a reason for hiding this comment

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

Note: (correcting myself) the functions uses the byte slice indexes, and not append, so the slice must be of the proper length, enough capacity with a 0 length won't work.

hex.Encode(dst, encryptedSteps)
r.EncryptedSteps = encryptedSteps

err = tt.ValidateResolverInputs(resolverInputs)
Expand Down Expand Up @@ -247,7 +251,17 @@ func load(dbp zesty.DBProvider, publicID string, locked bool, lockNoWait bool) (
return nil, err
}

compressedSteps, err := models.EncryptionKey.Decrypt(r.EncryptedSteps, []byte(r.PublicID))
dst := make([]byte, 0, hex.DecodedLen(len(r.EncryptedSteps)))
Copy link

Choose a reason for hiding this comment

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

Suggested change
dst := make([]byte, 0, hex.DecodedLen(len(r.EncryptedSteps)))
dst := make([]byte, hex.DecodedLen(len(r.EncryptedSteps)))


// if we can't hex Decode, we might be in the case of a Resolution row in database that was
// created between the v1.21.1 and v1.21.3 that was bugged, and failed to hex Encode/Decode the
// ciphered data. We need to keep backward compatibility for those, but this should not happen
// often.
// See https://github.com/ovh/utask/commit/bf23fbb10b62bb487ac4ea01b1e519f85480e58b and migration
// from symmecrypt.Key.DecryptMarshal to symmecrypt.Key.Decrypt
_, _ = hex.Decode(dst, r.EncryptedSteps)
Copy link

Choose a reason for hiding this comment

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

Should probably check returned error and set dst to r.EncryptedSteps if there was an error.


compressedSteps, err := models.EncryptionKey.Decrypt(dst, []byte(r.PublicID))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -377,6 +391,8 @@ func (r *Resolution) Update(dbp zesty.DBProvider) (err error) {
return err
}

dst := make([]byte, 0, hex.EncodedLen(len(compressedSteps)))
Copy link

Choose a reason for hiding this comment

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

Suggested change
dst := make([]byte, 0, hex.EncodedLen(len(compressedSteps)))
dst := make([]byte, hex.EncodedLen(len(compressedSteps)))

hex.Encode(dst, compressedSteps)
encryptedSteps, err := models.EncryptionKey.Encrypt(compressedSteps, []byte(r.PublicID))
if err != nil {
return err
Expand Down