denial

package
v0.34.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package denial implements host-manager's single denial contract.

One question — "may this caller have this?" — gets one answer shape:

no credential presented                -> 401 + WWW-Authenticate
credential, fails identity or the scope -> 403 + insufficient_scope,
                                           when oauth2-protected

The qualifier is load-bearing: Write emits the insufficient_scope challenge only when Opts.ResourceMetadata is set, because RFC 6750 is an OAuth 2.0 bearer-token spec and there is no resource metadata to point a client at otherwise. A non-oauth2 resource gets a bare 403.

The two ways to fail a 403 — NoIdentity (fails the identity gate) and InsufficientScope (clears identity, fails the requirement) — stay distinct OUTCOMES for logs and metrics but render identically: on an oauth2 resource the client's fix is the same either way, so both carry the step-up. Emitting the challenge from NoIdentity too closed a regression (#194) where an under-scoped oauth2 client lost its discovery pointer whenever the operation scope equalled the identity gate.

No status is ever chosen to conceal that a resource exists. The anti-enumeration 404 this replaces concealed nothing: /-/openapi serves every Ready function's paths to anonymous callers with no entitlement check (internal/host/openapi.go), so enumeration was already cheaper by GET than by probing. If /-/openapi is ever gated or caller-filtered, that trade reverses and this package is what should be revisited.

That condition is ENFORCED, not merely written down: TestOpenAPIPublishesGatedFunctionPathsToAnonymousCallers (internal/host/openapi_test.go) asserts a gated function path is present in the document built for a caller who presented no credential. Gating or caller-filtering /-/openapi turns that test red, which is the signal to come back here.

This package owns statuses and challenges, never bodies and never redirects. Presentation belongs to the mux-wide unwrap layer (internal/host/feedback.go), which re-renders every >= 400 per Accept. Redirects belong to the page gate, because a redirect is an alternative to writing a status rather than a kind of status.

Design: docs/superpowers/specs/2026-08-28-denial-contract-design.md

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write

func Write(w http.ResponseWriter, r *http.Request, o Opts)

Write sets the status and, where the contract calls for one, the WWW-Authenticate header. It uses http.Error so the status text becomes unwrap's statusMsg; it never renders HTML itself.

Provenance

Every header below goes out through auth.PreserveHeader rather than Header().Set, so this process ASSERTS it authored them at the point it authors them.

unwrap (internal/host/feedback.go) wipes the whole header map before re-rendering a >= 400 as HTML, because a suppressed proxy body leaves a Content-Length describing bytes the client will never get. The challenge written here has to survive that wipe -- RFC 7235 makes it mandatory on a 401 -- but the header map it sits in is the SAME map ReverseProxy copied the upstream response headers into (errorResponseWriter embeds the ResponseWriter and does not override Header()). So the wipe cannot be an allow-list by NAME: a backend answering `401 WWW-Authenticate: Basic realm="Sign in"` would make the browser render a NATIVE CREDENTIAL PROMPT on the host's origin -- a phishing primitive. unwrap restores by PROVENANCE instead, and this is where that provenance is recorded.

It is recorded HERE, and not by a host-side wrapper reading the headers back out of the map afterwards, for the same reason the mechanism exists at all: a read-back cannot tell "the host issued this challenge" from "a KDexFunction backend returned one", so promoting whatever the map happens to hold would reopen the exact forge window auth.HeaderPreserver closes. On a NoIdentity outcome Write sets no challenge at all, and a read-back would have promoted any WWW-Authenticate already sitting in the map.

auth.PreserveHeader degrades to a plain Header().Set for a writer that cannot record provenance (an httptest.ResponseRecorder, a gate reached outside the mux), so this is safe to call unconditionally.

Types

type Checker

type Checker interface {
	ParseRequirements([]kdexv1alpha1.SecurityRequirement) entitlements.ParsedRequirements
	VerifyResourceParsedEntitlements(
		string, string,
		entitlements.ParsedEntitlements, entitlements.ParsedRequirements,
		...string,
	) (bool, error)
}

Checker is the subset of the host's authorization checker that Classify needs. Declared here (rather than imported) so the package depends on behaviour, not on the HostHandler.

GetParsedEntitlements is deliberately NOT in this set: Classify takes the caller's already-parsed entitlements instead of re-deriving them. See Classify.

type Opts

type Opts struct {
	Outcome Outcome
	// Issuer is the host's issuer address, used as the realm when the
	// resource is not oauth2-protected.
	Issuer string
	// ResourceMetadata is the full RFC 9728 metadata URL
	// (<issuer>/.well-known/oauth-protected-resource<basePath>).
	// Empty when the resource is not oauth2-protected.
	ResourceMetadata string
	// Scopes are the requirement's declared scopes, named in an
	// insufficient_scope challenge so a client can step up.
	Scopes []string
}

Opts carries everything Write needs to render a denial.

type Outcome

type Outcome int

Outcome is which of the three contract outcomes a denial fell into.

const (
	// Unauthenticated: no credential was presented at all.
	Unauthenticated Outcome = iota
	// NoIdentity: a credential was presented but cannot address the
	// resource at all -- it fails the <resource>:<resourceName>:read
	// identity gate.
	NoIdentity
	// InsufficientScope: a credential was presented and clears the
	// identity gate, but does not satisfy the declared requirement.
	InsufficientScope
)

func Classify

func Classify(
	ctx context.Context,
	c Checker,
	held entitlements.ParsedEntitlements,
	resource, name string,
	verbs ...string,
) Outcome

Classify decides which contract row a denial falls into. It runs ONLY after a gate has already denied, so the extra identity probe below is never on the happy path.

`held` is the caller's parsed entitlements, which every gate has ALREADY computed to make the decision that failed. Classify takes it rather than calling GetParsedEntitlements again: a second derivation costs another map+slice allocation, another claim re-parse, and another RLock on the per-host pattern cache that every concurrent request shares -- all to recompute a value the caller is holding.

Anonymity is tested against the auth context rather than by inspecting entitlements: anonymous entitlements live inside the AuthorizationChecker itself, not as a synthetic auth context, so an absent context really does mean "no credential presented".

A context with an EMPTY subject counts as anonymous too. That is the definition every other gate in host-manager already uses -- hasEvaluatedSubject (internal/host/feedback.go), apitokenRevokeHandler (internal/host/apitoken.go) and capabilityMintHandler (internal/host/capabilities.go) all require a non-empty sub -- and a credential that names nobody cannot clear an identity gate keyed on who the caller is, so NoIdentity would be a 403 the caller can never fix.

The identity/requirement split needs no library change. An EMPTY requirement set reduces VerifyResourceParsedEntitlements to exactly the identity check -- the same reduction /-/check relies on and documents (internal/host/check.go).

func (Outcome) String

func (o Outcome) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL