Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw committed Jun 17, 2024
1 parent 0b7c1a3 commit d6e57b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 15 additions & 0 deletions middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,21 @@ func validateAllowCredentials(
return errors.New("ong/middleware/cors: allowCredentials should not be used together with wildcard allowedHeaders")
}

if allowCredentials {
// Credentialed requests should not be used with 'http' scheme. Should require 'https'.
// https://jub0bs.com/posts/2023-02-08-fearless-cors/#disallow-insecure-origins-by-default
// https://portswigger.net/research/exploiting-cors-misconfigurations-for-bitcoins-and-bounties
for _, origin := range allowedOrigins {
u, err := url.Parse(origin)
if err != nil {
return err
}
if u.Scheme == "http" {
return fmt.Errorf("ong/middleware/cors: allowCredentials should not be used together with origin that uses unsecure scheme `%v`", origin)
}
}
}

return nil
}

Expand Down
11 changes: 10 additions & 1 deletion middleware/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ func TestValidateAllowCredentials(t *testing.T) {
{
name: "credentials no wildcard origin",
allowCredentials: true,
allowedOrigins: []string{"http://example.com"},
allowedOrigins: []string{"https://example.com"},
allowedMethods: nil,
allowedHeaders: nil,
succeeds: true,
Expand All @@ -954,6 +954,15 @@ func TestValidateAllowCredentials(t *testing.T) {
succeeds: false,
errMsg: "allowCredentials should not be used together with wildcard",
},
{
name: "insecure http scheme",
allowCredentials: true,
allowedOrigins: []string{"http://example.org"},
allowedMethods: nil,
allowedHeaders: nil,
succeeds: false,
errMsg: "allowCredentials should not be used together with origin that uses unsecure scheme",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit d6e57b0

Please sign in to comment.