feat(identity): enforce shared session and browser request security

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 11:33:40 +03:00
1 parent 7df521bd89
commit 86f562f5b6
23 files changed
+430 -121

No files matched your search

@@ -18,6 +18,39 @@ const (
sessionCSRFScheme = "sessionCsrf"
)
func TestProtectedContractOperationsRequireSessionCookie(t *testing.T) {
data, err := os.ReadFile(publicContractBundle)
if err != nil {
t.Fatalf("read the public contract bundle: %v", err)
}
spec, err := ogen.Parse(data)
if err != nil {
t.Fatalf("parse the public contract bundle: %v", err)
}
checked := 0
for path, item := range spec.Paths {
for method, operation := range contractOperations(item) {
requirements := operation.Security
if requirements == nil {
requirements = spec.Security
}
if len(requirements) == 0 {
continue
}
checked++
for _, requirement := range requirements {
if _, usesSession := requirement[sessionCookieScheme]; !usesSession {
t.Errorf("%s %s (%s) is protected without %s", method, path, operation.OperationID, sessionCookieScheme)
}
}
}
}
if checked == 0 {
t.Fatal("found no protected operation; the check would pass vacuously")
}
}
// A browser sends the session cookie with every same-site request, so an
// unsafe operation that accepts the session must also demand the
// session-bound CSRF token. A new operation that forgets sessionCsrf would
@@ -78,3 +111,25 @@ func unsafeOperations(item *ogen.PathItem) map[string]*ogen.Operation {
}
return operations
}
func contractOperations(item *ogen.PathItem) map[string]*ogen.Operation {
operations := map[string]*ogen.Operation{
http.MethodGet: item.Get,
http.MethodHead: item.Head,
http.MethodPost: item.Post,
http.MethodPut: item.Put,
http.MethodPatch: item.Patch,
http.MethodDelete: item.Delete,
http.MethodOptions: item.Options,
http.MethodTrace: item.Trace,
}
for method, operation := range item.AdditionalOperations {
operations[method] = operation
}
for method, operation := range operations {
if operation == nil {
delete(operations, method)
}
}
return operations
}