-
-
Notifications
You must be signed in to change notification settings - Fork 584
Support cookie-backed API key security inference #3910
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
Merged
raphael
merged 3 commits into
goadesign:v3
from
CaliLuke:fix-3909-cookie-apikey-security
Mar 15, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package codegen | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "path/filepath" | ||
| "testing" | ||
| "text/template" | ||
|
|
||
| "github.com/getkin/kin-openapi/openapi2" | ||
| "github.com/getkin/kin-openapi/openapi3" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "goa.design/goa/v3/codegen" | ||
| "goa.design/goa/v3/dsl" | ||
| "goa.design/goa/v3/expr" | ||
| "goa.design/goa/v3/http/codegen/openapi" | ||
| openapiv2 "goa.design/goa/v3/http/codegen/openapi/v2" | ||
| openapiv3 "goa.design/goa/v3/http/codegen/openapi/v3" | ||
| ) | ||
|
|
||
| func TestCookieAPIKeySecurity(t *testing.T) { | ||
| t.Run("endpoint requirement uses cookie transport", func(t *testing.T) { | ||
| root := RunHTTPDSL(t, cookieAPIKeySecurityDSL) | ||
| endpoint := root.API.HTTP.Services[0].HTTPEndpoints[0] | ||
| require.Len(t, endpoint.Requirements, 1) | ||
| require.Len(t, endpoint.Requirements[0].Schemes, 1) | ||
|
|
||
| scheme := endpoint.Requirements[0].Schemes[0] | ||
| require.Equal(t, "cookie", scheme.In) | ||
| require.Equal(t, "__Host-ak_session", scheme.Name) | ||
|
|
||
| headers := expr.AsObject(endpoint.Headers.Type) | ||
| require.Zero(t, len(*headers), "cookie-backed api key must not synthesize an Authorization header") | ||
| }) | ||
|
|
||
| t.Run("openapi uses cookie security scheme", func(t *testing.T) { | ||
| root := RunHTTPDSL(t, cookieAPIKeySecurityDSL) | ||
| openapi.Definitions = make(map[string]*openapi.Schema) | ||
|
|
||
| v2JSON := renderOpenAPIJSON(t, openapiv2.Files, root) | ||
| var swagger openapi2.T | ||
| require.NoError(t, swagger.UnmarshalJSON(v2JSON)) | ||
| require.Len(t, swagger.SecurityDefinitions, 1) | ||
| require.Len(t, swagger.Paths, 1) | ||
| require.Contains(t, swagger.Paths, "/auth/profile") | ||
| require.NotNil(t, swagger.Paths["/auth/profile"].Get.Security) | ||
| require.Len(t, *swagger.Paths["/auth/profile"].Get.Security, 1) | ||
| for name, def := range swagger.SecurityDefinitions { | ||
| require.Equal(t, "apiKey", def.Type, name) | ||
| require.Equal(t, "cookie", def.In, name) | ||
| require.Equal(t, "__Host-ak_session", def.Name, name) | ||
| require.Contains(t, (*swagger.Paths["/auth/profile"].Get.Security)[0], name) | ||
| } | ||
|
|
||
| openapi.Definitions = make(map[string]*openapi.Schema) | ||
| v3JSON := renderOpenAPIJSON(t, openapiv3.Files, root) | ||
| loader := openapi3.NewLoader() | ||
| doc, err := loader.LoadFromData(v3JSON) | ||
| require.NoError(t, err) | ||
| require.NoError(t, doc.Validate(context.Background())) | ||
| require.Len(t, doc.Components.SecuritySchemes, 1) | ||
| require.NotNil(t, doc.Paths.Find("/auth/profile")) | ||
| require.NotNil(t, doc.Paths.Find("/auth/profile").Get.Security) | ||
| require.Len(t, *doc.Paths.Find("/auth/profile").Get.Security, 1) | ||
| for name, ref := range doc.Components.SecuritySchemes { | ||
| require.NotNil(t, ref.Value, name) | ||
| require.Equal(t, "apiKey", ref.Value.Type, name) | ||
| require.Equal(t, "cookie", ref.Value.In, name) | ||
| require.Equal(t, "__Host-ak_session", ref.Value.Name, name) | ||
| require.Contains(t, (*doc.Paths.Find("/auth/profile").Get.Security)[0], name) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("http codegen does not duplicate cookie-backed auth fields", func(t *testing.T) { | ||
| root := RunHTTPDSL(t, cookieAPIKeySecurityDSL) | ||
| services := CreateHTTPServices(root) | ||
|
|
||
| serverTypes := serverType("gen", root.API.HTTP.Services[0], services) | ||
| var serverTypesBuf bytes.Buffer | ||
| for _, section := range serverTypes.SectionTemplates[1:] { | ||
| require.NoError(t, section.Write(&serverTypesBuf)) | ||
| } | ||
| serverTypesCode := codegen.FormatTestCode(t, "package foo\n"+serverTypesBuf.String()) | ||
| require.Contains(t, serverTypesCode, "func NewProfilePayload(browserSession string)") | ||
| require.NotContains(t, serverTypesCode, "browserSession *string, browserSession *string") | ||
| require.NotContains(t, serverTypesCode, "browserSession string, browserSession string") | ||
|
|
||
| serverFiles := ServerFiles("", services) | ||
| require.Len(t, serverFiles, 2) | ||
| serverDecode := codegen.SectionCode(t, serverFiles[1].SectionTemplates[2]) | ||
| require.Contains(t, serverDecode, `r.Cookie("__Host-ak_session")`) | ||
| require.NotContains(t, serverDecode, "Authorization") | ||
| require.NotContains(t, serverDecode, "browserSession *string, browserSession *string") | ||
| require.NotContains(t, serverDecode, "browserSession string, browserSession string") | ||
|
|
||
| clientFiles := ClientFiles("", services) | ||
| require.Len(t, clientFiles, 2) | ||
| clientEncode := codegen.SectionCode(t, clientFiles[1].SectionTemplates[2]) | ||
| require.Contains(t, clientEncode, `req.AddCookie(&http.Cookie{`) | ||
| require.Contains(t, clientEncode, `Name: "__Host-ak_session"`) | ||
| require.NotContains(t, clientEncode, "Authorization") | ||
| }) | ||
| } | ||
|
|
||
| func renderOpenAPIJSON( | ||
| t *testing.T, | ||
| build func(*expr.RootExpr) ([]*codegen.File, error), | ||
| root *expr.RootExpr, | ||
| ) []byte { | ||
| t.Helper() | ||
|
|
||
| files, err := build(root) | ||
| require.NoError(t, err) | ||
| for _, f := range files { | ||
| if filepath.Ext(f.Path) != ".json" { | ||
| continue | ||
| } | ||
| require.Len(t, f.SectionTemplates, 1) | ||
| section := f.SectionTemplates[0] | ||
| require.NotEmpty(t, section.Source) | ||
| require.NotNil(t, section.Data) | ||
|
|
||
| var buf bytes.Buffer | ||
| tmpl := template.Must(template.New("openapi").Funcs(section.FuncMap).Parse(section.Source)) | ||
| require.NoError(t, tmpl.Execute(&buf, section.Data)) | ||
| return buf.Bytes() | ||
| } | ||
|
|
||
| t.Fatalf("no JSON OpenAPI file generated") | ||
| return nil | ||
| } | ||
|
|
||
| var cookieAPIKeySecurityDSL = func() { | ||
| var browserSessionCookie = dsl.APIKeySecurity("browser_session_cookie", func() { | ||
| dsl.Description("Browser session cookie") | ||
| }) | ||
|
|
||
| dsl.Service("cookieSecurity", func() { | ||
| dsl.Method("profile", func() { | ||
| dsl.Security(browserSessionCookie) | ||
| dsl.Payload(func() { | ||
| dsl.APIKey("browser_session_cookie", "browser_session", dsl.String, func() { | ||
| dsl.Description("Opaque browser session cookie") | ||
| }) | ||
| dsl.Required("browser_session") | ||
| }) | ||
| dsl.Result(dsl.Empty) | ||
| dsl.HTTP(func() { | ||
| dsl.GET("/auth/profile") | ||
| dsl.Cookie("browser_session:__Host-ak_session") | ||
| dsl.Response(dsl.StatusOK) | ||
| }) | ||
| }) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "goa.design/goa/v3/expr" | ||
| ) | ||
|
|
||
| // IsSecurityParameter returns true if the given HTTP transport element is used | ||
| // by one of the endpoint security schemes and should therefore not be emitted | ||
| // again as a regular OpenAPI parameter. | ||
| func IsSecurityParameter(endpoint *expr.HTTPEndpointExpr, in, name string) bool { | ||
| if endpoint == nil { | ||
| return false | ||
| } | ||
| for _, req := range endpoint.Requirements { | ||
| for _, scheme := range req.Schemes { | ||
| if scheme.In != in { | ||
| continue | ||
| } | ||
| if in == "header" { | ||
| if strings.EqualFold(scheme.Name, name) { | ||
| return true | ||
| } | ||
| continue | ||
| } | ||
| if scheme.Name == name { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to keep this private, this is probably not something that the openapi package should expose.