Skip to content

fix(generator): an operation declaring security: [] still got the credential - #107

Open
giraffesyo wants to merge 1 commit into
canaryfrom
operation-security
Open

fix(generator): an operation declaring security: [] still got the credential#107
giraffesyo wants to merge 1 commit into
canaryfrom
operation-security

Conversation

@giraffesyo

Copy link
Copy Markdown
Member

Closes #105.

security:
  - bearer: []
paths:
  /public:
    get:
      security: []      # explicitly public
authorization headers: ["Bearer s3cr3t" "Bearer s3cr3t"]

Auth was applied in do for every request, and no per-operation security reached the IR at all. A bearer token went to an endpoint the spec documents as needing none, landing in that server's logs, and an unauthenticated endpoint is the one most likely to be served by something other than the API itself.

authorization headers: ["Bearer s3cr3t" ""]

Absent and empty are different declarations

libopenapi distinguishes them, so the analyzer can too: an absent security field arrives as nil and inherits the document's, an empty one arrives as a non-nil empty slice and overrides it. Only the second opts out. An operation naming a scheme explicitly still authenticates.

do takes an authenticated argument rather than reading anything global, so the decision is made per operation at generation time and is visible in the generated call.

Scope

Choosing between several schemes per operation, or requiring several at once, is deliberately untouched. The client holds one AuthProvider, and honoring per-operation selection means either several providers or a provider that knows which scheme it satisfies, which is a design question the opt-out does not depend on. ir.OperationDef.SecurityReqs remains unpopulated, as it was.

Tests

  • internal/analyzer/security_test.go: an operation that declares nothing inherits, one with security: [] opts out, one naming a scheme does not.
  • internal/generator/e2e_operation_security_test.go: compiles and runs against httptest, checking the inheriting operation still sends Bearer s3cr3t while the public one sends nothing, that opting out changes nothing else about the request (the body still goes), and that a client with no provider is unaffected.

gofmt, go vet ./..., and go test ./... pass.

…dential

Auth was applied in do for every request the client made, and no
per-operation security reached the IR, so an endpoint the spec documents as
needing no credential received one anyway: sent to that host, written to its
logs, and against what the spec plainly said.

An empty security requirement now reaches the operation and turns the
credential off for it. An absent one is a different declaration and still
inherits the document's, so only the explicit opt-out changes.

Choosing between several schemes per operation is left alone: the client
holds one AuthProvider, and honoring per-operation selection means either
several providers or one that knows which scheme it satisfies. The opt-out
does not depend on that being settled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An operation that declares security: [] still receives the client's credentials

1 participant