Skip to content

Commit

Permalink
Move domain validation checks for URI/Username to service startup (#590)
Browse files Browse the repository at this point in the history
* Move domain validation checks for URI/Username to service startup

These checks were done once per request. These instead can be static
checks done on service startup. This enforces that the Username and URI
configs appropriately set the SubjectDomain and IssuerUrl fields, with
no scheme just for the SubjectDomain field for Username.

Signed-off-by: Hayden Blauzvern <hblauzvern@google.com>

* Fix linter

Signed-off-by: Hayden Blauzvern <hblauzvern@google.com>
  • Loading branch information
haydentherapper committed May 18, 2022
1 parent 331874d commit e1fe2f8
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 219 deletions.
8 changes: 4 additions & 4 deletions docs/oidc.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ The token must include the following claims:
}
```

Tokens must conform to the following:
The configuration must include `SPIFFETrustDomain`, for example `example.com`. Tokens must conform to the following:

* The SPIFFE trust domain from `sub` must match the issuer in the configuration.
* The trust domain of the configuration and hostname of `sub` must match exactly.

`sub` is included as a SAN URI.

Expand Down Expand Up @@ -144,7 +144,7 @@ The token must include the following claims:

Additionally, the configuration must include `SubjectDomain`, for example `https://example.com`. Tokens must conform to the following:

* The issuer of the token must partially match the domain in the configuration. The scheme, top level domain, and second level domain must match. The user who updates the Fulcio configuration must also have control over both the issuer and domain configuration fields (Verified either manually or through an ACME-style challenge).
* The issuer in the configuration must partially match the domain in the configuration. The scheme, top level domain, and second level domain must match. The user who updates the Fulcio configuration must also have control over both the issuer and domain configuration fields (Verified either manually or through an ACME-style challenge).
* The domain of the configuration and hostname of the subject of the token must match exactly.

`sub` is included as a SAN URI.
Expand All @@ -161,6 +161,6 @@ The token must include the following claims:

Additionally, the configuration must include `SubjectDomain`, for example `example.com`. Tokens must conform to the following:

* The issuer of the token must partially match the domain in the configuration. The top level domain and second level domain must match. The user who updates the Fulcio configuration must also have control over both the issuer and domain configuration fields (Verified either manually or through an ACME-style challenge).
* The issuer in the configuration must partially match the domain in the configuration. The top level domain and second level domain must match. The user who updates the Fulcio configuration must also have control over both the issuer and domain configuration fields (Verified either manually or through an ACME-style challenge).

`SubjectDomain` is appended to `sub` to form an email, `sub@SubjectDomain`, and included as a SAN email address.
75 changes: 1 addition & 74 deletions pkg/challenges/challenges.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ const (
UsernameValue
)

// All hostnames for subject and issuer OIDC claims must have at least a
// top-level and second-level domain
const minimumHostnameLength = 2

type AdditionalInfo int

// Additional information that can be added as a cert extension.
Expand Down Expand Up @@ -284,31 +280,11 @@ func uri(ctx context.Context, principal *oidc.IDToken) (identity.Principal, erro
return nil, errors.New("invalid configuration for OIDC ID Token issuer")
}

// The subject hostname must exactly match the subject domain from the configuration
uSubject, err := url.Parse(uriWithSubject)
if err != nil {
return nil, err
}

// The subject prefix URI must match the domain (excluding the subdomain) of the issuer
// In order to declare this configuration, a test must have been done to prove ownership
// over both the issuer and domain configuration values.
// Valid examples:
// * uriWithSubject = https://example.com/users/user1, issuer = https://accounts.example.com
// * uriWithSubject = https://accounts.example.com/users/user1, issuer = https://accounts.example.com
// * uriWithSubject = https://users.example.com/users/user1, issuer = https://accounts.example.com
uIssuer, err := url.Parse(cfg.IssuerURL)
if err != nil {
return nil, err
}

// Check that:
// * The URI schemes match
// * Either the hostnames exactly match or the top level and second level domains match
if err := isURISubjectAllowed(uSubject, uIssuer); err != nil {
return nil, err
}

// The subject hostname must exactly match the subject domain from the configuration
uDomain, err := url.Parse(cfg.SubjectDomain)
if err != nil {
return nil, err
Expand Down Expand Up @@ -345,21 +321,6 @@ func username(ctx context.Context, principal *oidc.IDToken) (identity.Principal,
return nil, errors.New("invalid configuration for OIDC ID Token issuer")
}

// The domain in the configuration must match the domain (excluding the subdomain) of the issuer
// In order to declare this configuration, a test must have been done to prove ownership
// over both the issuer and domain configuration values.
// Valid examples:
// * domain = https://example.com/users/user1, issuer = https://accounts.example.com
// * domain = https://accounts.example.com/users/user1, issuer = https://accounts.example.com
// * domain = https://users.example.com/users/user1, issuer = https://accounts.example.com
uIssuer, err := url.Parse(cfg.IssuerURL)
if err != nil {
return nil, err
}
if err := validateAllowedDomain(cfg.SubjectDomain, uIssuer.Hostname()); err != nil {
return nil, err
}

issuer, err := oauthflow.IssuerFromIDToken(principal, cfg.IssuerClaim)
if err != nil {
return nil, err
Expand Down Expand Up @@ -448,40 +409,6 @@ func workflowInfoFromIDToken(token *oidc.IDToken) (map[AdditionalInfo]string, er
GithubWorkflowRef: claims.Ref}, nil
}

// isURISubjectAllowed compares the subject and issuer URIs,
// returning an error if the scheme or the hostnames do not match
func isURISubjectAllowed(subject, issuer *url.URL) error {
if subject.Scheme != issuer.Scheme {
return fmt.Errorf("subject (%s) and issuer (%s) URI schemes do not match", subject.Scheme, issuer.Scheme)
}

return validateAllowedDomain(subject.Hostname(), issuer.Hostname())
}

// validateAllowedDomain compares two hostnames, returning an error if the
// top-level and second-level domains do not match
func validateAllowedDomain(subjectHostname, issuerHostname string) error {
// If the hostnames exactly match, return early
if subjectHostname == issuerHostname {
return nil
}

// Compare the top level and second level domains
sHostname := strings.Split(subjectHostname, ".")
iHostname := strings.Split(issuerHostname, ".")
if len(sHostname) < minimumHostnameLength {
return fmt.Errorf("URI hostname too short: %s", subjectHostname)
}
if len(iHostname) < minimumHostnameLength {
return fmt.Errorf("URI hostname too short: %s", issuerHostname)
}
if sHostname[len(sHostname)-1] == iHostname[len(iHostname)-1] &&
sHostname[len(sHostname)-2] == iHostname[len(iHostname)-2] {
return nil
}
return fmt.Errorf("hostname top-level and second-level domains do not match: %s, %s", subjectHostname, issuerHostname)
}

func PrincipalFromIDToken(ctx context.Context, tok *oidc.IDToken) (identity.Principal, error) {
iss, ok := config.FromContext(ctx).GetIssuer(tok.Issuer)
if !ok {
Expand Down
134 changes: 0 additions & 134 deletions pkg/challenges/challenges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,74 +380,6 @@ func TestUsernameInvalidChar(t *testing.T) {
}
}

func Test_isURISubjectAllowed(t *testing.T) {
tests := []struct {
name string
subject string // Parsed to url.URL
issuer string // Parsed to url.URL
want error
}{{
name: "match",
subject: "https://accounts.example.com",
issuer: "https://accounts.example.com",
want: nil,
}, {
name: "issuer subdomain",
subject: "https://example.com",
issuer: "https://accounts.example.com",
want: nil,
}, {
name: "subject subdomain",
subject: "https://profiles.example.com",
issuer: "https://example.com",
want: nil,
}, {
name: "subdomain mismatch",
subject: "https://profiles.example.com",
issuer: "https://accounts.example.com",
want: nil,
}, {
name: "scheme mismatch",
subject: "http://example.com",
issuer: "https://example.com",
want: fmt.Errorf("subject (http) and issuer (https) URI schemes do not match"),
}, {
name: "subject domain too short",
subject: "https://example",
issuer: "https://example.com",
want: fmt.Errorf("URI hostname too short: example"),
}, {
name: "issuer domain too short",
subject: "https://example.com",
issuer: "https://issuer",
want: fmt.Errorf("URI hostname too short: issuer"),
}, {
name: "domain mismatch",
subject: "https://example.com",
issuer: "https://otherexample.com",
want: fmt.Errorf("hostname top-level and second-level domains do not match: example.com, otherexample.com"),
}, {
name: "top level domain mismatch",
subject: "https://example.com",
issuer: "https://example.org",
want: fmt.Errorf("hostname top-level and second-level domains do not match: example.com, example.org"),
}}
for _, tt := range tests {
subject, _ := url.Parse(tt.subject)
issuer, _ := url.Parse(tt.issuer)
t.Run(tt.name, func(t *testing.T) {
got := isURISubjectAllowed(subject, issuer)
if got == nil && tt.want != nil ||
got != nil && tt.want == nil {
t.Errorf("isURISubjectAllowed() = %v, want %v", got, tt.want)
}
if got != nil && tt.want != nil && got.Error() != tt.want.Error() {
t.Errorf("isURISubjectAllowed() = %v, want %v", got, tt.want)
}
})
}
}

// reflect hack because "claims" field is unexported by oidc IDToken
// https://github.com/coreos/go-oidc/pull/329
func updateIDToken(idToken *oidc.IDToken, fieldName string, data []byte) {
Expand Down Expand Up @@ -611,72 +543,6 @@ func TestSpiffe(t *testing.T) {
}
}

func Test_validateAllowedDomain(t *testing.T) {
tests := []struct {
name string
subject string // Parsed to url.URL
issuer string // Parsed to url.URL
want error
}{{
name: "match",
subject: "accounts.example.com",
issuer: "accounts.example.com",
want: nil,
}, {
name: "issuer subdomain",
subject: "example.com",
issuer: "accounts.example.com",
want: nil,
}, {
name: "subject subdomain",
subject: "profiles.example.com",
issuer: "example.com",
want: nil,
}, {
name: "subdomain mismatch",
subject: "profiles.example.com",
issuer: "accounts.example.com",
want: nil,
}, {
name: "subject domain too short",
subject: "example",
issuer: "example.com",
want: fmt.Errorf("URI hostname too short: example"),
}, {
name: "issuer domain too short",
subject: "example.com",
issuer: "issuer",
want: fmt.Errorf("URI hostname too short: issuer"),
}, {
name: "domain mismatch",
subject: "example.com",
issuer: "otherexample.com",
want: fmt.Errorf("hostname top-level and second-level domains do not match: example.com, otherexample.com"),
}, {
name: "domain mismatch, subdomain match",
subject: "test.example.com",
issuer: "test.otherexample.com",
want: fmt.Errorf("hostname top-level and second-level domains do not match: test.example.com, test.otherexample.com"),
}, {
name: "top level domain mismatch",
subject: "example.com",
issuer: "example.org",
want: fmt.Errorf("hostname top-level and second-level domains do not match: example.com, example.org"),
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := validateAllowedDomain(tt.subject, tt.issuer)
if got == nil && tt.want != nil ||
got != nil && tt.want == nil {
t.Errorf("validateAllowedDomain() = %v, want %v", got, tt.want)
}
if got != nil && tt.want != nil && got.Error() != tt.want.Error() {
t.Errorf("validateAllowedDomain() = %v, want %v", got, tt.want)
}
})
}
}

func failErr(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit e1fe2f8

Please sign in to comment.