Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Fix panic on invalid client data #122

Merged
merged 1 commit into from
Mar 30, 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 @@ -7,7 +7,10 @@ import (
"github.com/duo-labs/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 @@ -158,8 +161,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 @@ -188,11 +194,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")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can add a testcase for this?

}
Comment on lines 199 to +202
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
idLength := binary.BigEndian.Uint16(rawAuthData[53:55])
if len(rawAuthData) < int(55+idLength) {
return ErrBadRequest.WithDetails("Authenticator attestation data length too short")
}
idLength := binary.BigEndian.Uint16(rawAuthData[53:55])
if len(rawAuthData) < int(55+idLength) {
return ErrBadRequest.WithDetails("Authenticator attestation data length too short")
}
if idLength > 1023 {
return ErrBadRequest.WithDetails("Authenticator attestation data credential id length too long")
}

See https://w3c.github.io/webauthn/#attested-credential-data

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