Skip to content
New issue

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

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

Already on GitHub? # to your account

Possible code duplication #377

Closed
lukehinds opened this issue Dec 2, 2022 · 6 comments · Fixed by #380
Closed

Possible code duplication #377

lukehinds opened this issue Dec 2, 2022 · 6 comments · Fixed by #380

Comments

@lukehinds
Copy link

Was reading over the code and noticed the following:

func decodeSignature(s string) ([]byte, []error) {
var errs []error
rsig, err := base64.StdEncoding.DecodeString(s)
if err == nil {
// No error, return the value.
return rsig, nil
}
errs = append(errs, err)
rsig, err = base64.URLEncoding.DecodeString(s)
if err == nil {
// No error, return the value.
return rsig, nil
}
errs = append(errs, err)
return nil, errs
}

There maybe some logic in the duplication, if so this is invalid, but I thought it worth raising in case it's a mistake and causes some edge case bug.

@ianlewis
Copy link
Member

ianlewis commented Dec 2, 2022

@lukehinds Did you mean to close this? Even if the code is valid for some reason it's a bit unintuitive so perhaps could use a comment.

@ianlewis
Copy link
Member

ianlewis commented Dec 2, 2022

It looks like where decodeSignature is used is also strange as it returns an []error but that slice isn't checked.

rsig, es := decodeSignature(sig.Sig)
if err != nil {
errs = append(errs, es...)
continue
}

@ianlewis
Copy link
Member

ianlewis commented Dec 2, 2022

I'll reopen since this looks strange to me.

/cc @laurentsimon since it looks like this was added in #251

@ianlewis ianlewis reopened this Dec 2, 2022
@lukehinds
Copy link
Author

@lukehinds Did you mean to close this? Even if the code is valid for some reason it's a bit unintuitive so perhaps could use a comment.

I did, but perhaps it does merit some attention. My eyes did not make out URLEncoding and StdEncoding as different.

@laurentsimon
Copy link
Contributor

laurentsimon commented Dec 2, 2022

Let me explain. We try both encoding because, when verifying GCB provenance, it sometimes failed due to an encoding issue. So I added this "redundant-looking" base64 decoding to avoid the problem. I agree it's worth adding a comment, which I should have done :/

decodeSignature() is called

rsig, es := decodeSignature(sig.Sig)

and appended to a list of errors
errs = append(errs, es...)

If the loop (which iterates over all the signatures in the envelope) fails, we return all the errors we encountered

return fmt.Errorf("%w: %v", serrors.ErrorNoValidSignature, errs)
. The caller itself iterates over all the attestations, so this error is also appended by the caller
errs = append(errs, err)
which is eventually returned as a single error
return fmt.Errorf("%w: %v", serrors.ErrorNoValidSignature, errs)
.

The verifySignature() originates from

if err = prov.VerifySignature(); err != nil {
and it would fail at this line.

@ianlewis
Copy link
Member

ianlewis commented Dec 3, 2022

@lukehinds

I did, but perhaps it does merit some attention. My eyes did not make out URLEncoding and StdEncoding as different.

tbh, you're in good company. I didn't see that either... Yeah, probably a comment would have been good to make it clearer.

@laurentsimon

It looks like where decodeSignature is used is also strange as it returns an []error but that slice isn't checked.

rsig, es := decodeSignature(sig.Sig)
if err != nil {
errs = append(errs, es...)
continue
}

I think you want the error checking line to be

if len(es) != 0 {

perhaps?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants