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: potential index out of range panic #8

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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: 14 additions & 4 deletions protocol/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"github.com/go-webauthn/webauthn/protocol/webauthncbor"
)

var minAuthDataLength = 37
var (
minAuthDataLength = 37
minAttestedAuthLength = 55
)

// Authenticators respond to Relying Party requests by returning an object derived from the
// AuthenticatorResponse interface. See §5.2. Authenticator Responses
Expand Down Expand Up @@ -159,8 +162,11 @@ func (a *AuthenticatorData) Unmarshal(rawAuthData []byte) error {
remaining := len(rawAuthData) - minAuthDataLength

if a.Flags.HasAttestedCredentialData() {
if len(rawAuthData) > minAuthDataLength {
a.unmarshalAttestedData(rawAuthData)
if len(rawAuthData) > minAttestedAuthLength {
validError := a.unmarshalAttestedData(rawAuthData)
if validError != nil {
return validError
}
attDataLen := len(a.AttData.AAGUID) + 2 + len(a.AttData.CredentialID) + len(a.AttData.CredentialPublicKey)
remaining = remaining - attDataLen
} else {
Expand Down Expand Up @@ -189,11 +195,15 @@ func (a *AuthenticatorData) Unmarshal(rawAuthData []byte) error {
}

// If Attestation Data is present, unmarshall that into the appropriate public key structure
func (a *AuthenticatorData) unmarshalAttestedData(rawAuthData []byte) {
func (a *AuthenticatorData) unmarshalAttestedData(rawAuthData []byte) error {
a.AttData.AAGUID = rawAuthData[37:53]
idLength := binary.BigEndian.Uint16(rawAuthData[53:55])
if len(rawAuthData) < int(55+idLength) {
return ErrBadRequest.WithDetails("Authenticator attestation data length too short")
}
a.AttData.CredentialID = rawAuthData[55 : 55+idLength]
a.AttData.CredentialPublicKey = unmarshalCredentialPublicKey(rawAuthData[55+idLength:])
return nil
}

// Unmarshall the credential's Public Key into CBOR encoding
Expand Down
11 changes: 7 additions & 4 deletions protocol/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ func TestAuthenticatorData_unmarshalAttestedData(t *testing.T) {
rawAuthData []byte
}
tests := []struct {
name string
fields fields
args args
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
Expand All @@ -199,7 +200,9 @@ func TestAuthenticatorData_unmarshalAttestedData(t *testing.T) {
AttData: tt.fields.AttData,
ExtData: tt.fields.ExtData,
}
a.unmarshalAttestedData(tt.args.rawAuthData)
if err := a.unmarshalAttestedData(tt.args.rawAuthData); (err != nil) != tt.wantErr {
t.Errorf("AuthenticatorData.unmarshalAttestedData() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Expand Down