Add CORS credentials, transactional endpoints, and container deployment
CORS
cors.go never set Access-Control-Allow-Credentials, so the
cookie-authenticated API was unreadable from any cross-origin frontend:
the server answered correctly and the browser blocked the page from
reading it. Set for allowlisted origins on both the preflight and the
actual response. Three tests added.
HTTP_COOKIE_SAMESITE (lax|none|strict, default lax) is new. CORS is only
half of what a cross-origin browser call needs; SameSite is judged on
registrable domain, so a frontend on an unrelated domain gets perfect CORS
headers and still no cookie. "none" is the only value that survives that,
and validate() refuses it without the Secure flag.
The "*" rejection now explains itself: browsers refuse Allow-Origin "*"
together with credentials, so it would break every authenticated call
rather than loosen anything.
Transactional endpoints (api-contract.md 12.1)
POST /api/v1/job-applications/{id}/hire
POST /api/v1/job-postings/{id}/assignments
Replaces two client-side loops that wrote several records with no
transaction and no rollback. Each is now one endpoint and one transaction,
built over repo.Repo so org scoping, derived columns, type casts and error
translation are not re-derived. Authorization reuses the existing policy
table rather than adding a parallel one: a workflow is exactly as
privileged as the writes it performs. 13 tests, including both rollback
paths.
Bug fix in the repository layer
repo.bindValue handled int64/int/float64/string but not int32, which is
what pgx returns for a PostgreSQL `int` column. Nothing previously read a
record and wrote one of its fields elsewhere, so it never surfaced; the
hire flow does exactly that and failed with "ai_score must be a number".
Both KindInt and KindFloat now accept the widths pgx actually produces.
Deployment
infrastructure/Dockerfile.api multi-stage, cross-compiling (BUILDPLATFORM
+ GOARCH) so linux/amd64 builds from arm64 are compiled rather than
emulated. Alpine runtime, non-root uid 10001, 22.1 MB. Ships api, seed,
setpassword and migrate, plus the migrations, so a Kubernetes
initContainer can apply the schema from the same image and tag as the
API. HEALTHCHECK keys on status code, not body, so a "degraded" instance
is not pulled from rotation during a migration window.
infrastructure/docker-compose.yml migrations run to completion before the
API starts. Assumes a managed PostgreSQL; the local-db overlay adds one
with TLS enabled so APP_ENV=production is met rather than dodged.
scripts/drop_public_tables.go the one-off used to clear an unrelated
schema from krowdb on 2026-08-24, kept for the record. Build-tagged
ignore and gated on CONFIRM_DROP=yes.
Verified against PostgreSQL: 16/16 new tests pass, and the image was built,
run and exercised end to end (login, CORS preflight, authenticated reads,
transaction rollback).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CmQiGq73Uyfq7J4yR8Vxxw
This commit is contained in:
@@ -47,6 +47,27 @@ type HTTPConfig struct {
|
||||
IdleTimeout time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
|
||||
// CookieSameSite is the SameSite attribute on the session cookie:
|
||||
// "lax" (default), "none" or "strict".
|
||||
//
|
||||
// This exists because CORS is only half of what a cross-origin browser call
|
||||
// needs, and the other half is easy to miss. SameSite is judged on SITE
|
||||
// (registrable domain), not origin:
|
||||
//
|
||||
// app.krow.com → api.krow.com SAME site. Lax sends the cookie. ✓
|
||||
// krow.vercel.app → api.krow.com CROSS site. Lax does NOT send it. ✗
|
||||
// localhost:5173 → 127.0.0.1:8080 CROSS site — different hosts. ✗
|
||||
//
|
||||
// So a deployment whose frontend is on an unrelated domain gets a perfect
|
||||
// set of CORS headers and still no session, because the browser never
|
||||
// attaches the cookie. "none" is the only value that survives that, and it
|
||||
// requires Secure, which means HTTPS.
|
||||
//
|
||||
// Default stays "lax": it is the safe value, it is correct for the
|
||||
// same-site and same-origin deployments this is normally run as, and it
|
||||
// gives CSRF protection that "none" gives up.
|
||||
CookieSameSite string
|
||||
|
||||
// CORSOrigins is the exact set of browser origins allowed to call the API.
|
||||
//
|
||||
// It exists for one reason: in local development the Vite dev server is an
|
||||
@@ -137,6 +158,7 @@ func Load() (*Config, error) {
|
||||
IdleTimeout: durationDefault("HTTP_IDLE_TIMEOUT", 60*time.Second),
|
||||
ShutdownTimeout: durationDefault("HTTP_SHUTDOWN_TIMEOUT", 10*time.Second),
|
||||
CORSOrigins: corsOrigins(withDefault("APP_ENV", "development")),
|
||||
CookieSameSite: strings.ToLower(withDefault("HTTP_COOKIE_SAMESITE", "lax")),
|
||||
},
|
||||
Seed: SeedConfig{
|
||||
FixturePath: withDefault("SEED_FIXTURE_PATH", "./seed/fixtures/seed.json"),
|
||||
@@ -194,12 +216,38 @@ func (c *Config) validate() error {
|
||||
if c.AppEnv == "production" && c.DB.SSLMode == "disable" {
|
||||
return fmt.Errorf("DATABASE_SSLMODE=disable is not allowed when APP_ENV=production")
|
||||
}
|
||||
switch c.HTTP.CookieSameSite {
|
||||
case "lax", "strict":
|
||||
case "none":
|
||||
// SameSite=None without Secure is ignored — and in current browsers,
|
||||
// rejected outright — so the cookie would simply never be stored. The
|
||||
// Secure flag is set for every APP_ENV except development, so this is
|
||||
// the one combination that produces a silently sessionless deployment.
|
||||
if c.AppEnv == "development" {
|
||||
return fmt.Errorf("HTTP_COOKIE_SAMESITE=none requires the Secure cookie flag, " +
|
||||
"which is not set when APP_ENV=development; SameSite=None over plain HTTP " +
|
||||
"is rejected by browsers")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("HTTP_COOKIE_SAMESITE must be lax, none or strict, got %q",
|
||||
c.HTTP.CookieSameSite)
|
||||
}
|
||||
for _, origin := range c.HTTP.CORSOrigins {
|
||||
// "*" is rejected rather than quietly honoured. The middleware echoes a
|
||||
// single matched origin, so a wildcard could only ever be a
|
||||
// misunderstanding of what this setting does.
|
||||
// "*" is not a stricter-than-necessary policy choice — it cannot work
|
||||
// here at all. Authentication is a cookie, so the API must answer
|
||||
// Access-Control-Allow-Credentials: true, and every browser REFUSES
|
||||
// the pairing of that header with Allow-Origin: "*". A deployment
|
||||
// configured this way would send correct-looking headers and have
|
||||
// every authenticated call blocked client-side.
|
||||
if origin == "*" {
|
||||
return fmt.Errorf("HTTP_CORS_ORIGINS must list explicit origins; \"*\" is not accepted")
|
||||
return fmt.Errorf(`HTTP_CORS_ORIGINS must list explicit origins; "*" cannot be used ` +
|
||||
`because this API authenticates with a cookie, and browsers reject ` +
|
||||
`Access-Control-Allow-Origin: "*" together with credentials. ` +
|
||||
`List each frontend origin, or serve the frontend from the API's own origin ` +
|
||||
`(then leave this unset and CORS is not involved at all)`)
|
||||
}
|
||||
if !strings.HasPrefix(origin, "http://") && !strings.HasPrefix(origin, "https://") {
|
||||
return fmt.Errorf("HTTP_CORS_ORIGINS entry %q must be a full origin including the scheme", origin)
|
||||
|
||||
Reference in New Issue
Block a user