about summary refs log tree commit diff
path: root/docs
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-08 14:57:35 +0000
committerPjotr <wrk@bosa.lan>2026-10-08 14:57:35 +0000
commit69aac61b0c5b2d8a4f909de6a6d6af8d6ee293a3 (patch)
tree8c3734e6a3ca0baca7872d6e87cbe3167fb128b6 /docs
parent679136801effc34ff4b50e54abfc40592ee1d64d (diff)
downloadgenenetwork4-69aac61b0c5b2d8a4f909de6a6d6af8d6ee293a3.tar.gz
* src/routes/dev.rs: POST /get-test-token answers local callers only, not tied to dev mode.
The endpoint no longer depends on GENENETWORK4_DEV_MODE or on the bind
address. It decides per request whether to answer.

* src/routes/dev.rs (get_test_token): answers a caller only when the TCP
peer (axum ConnectInfo) is in 127.0.x.x (or ::1, or an IPv4-mapped
127.x) and the request carries no X-Forwarded-For, Forwarded or
X-Real-IP header (a reverse proxy on the same host would otherwise make
every client look local). Anything else, including an unknown peer, is a
403. Local is never established from a header.
* src/main.rs: serve with into_make_service_with_connect_info, which
provides the peer address.
* src/config.rs: remove dev_mode, GENENETWORK4_DEV_MODE and
dev_token_enabled(). Add GENENETWORK4_TEST_TOKEN (on by default); when it
is not a clearly affirmative value (true/1/yes/on) the route is not
registered: a 404, absent from /openapi.json. It fails closed, so a
typo ("flase") turns the endpoint off.
* src/lib.rs: register the route unless that switch is off.

* tests: tests/get_test_token.rs rewritten (remote, unknown and proxied
peers refused; 127.x and ::1 answered; always documented by default);
tests/get_test_token_off.rs for the switch (own process: it sets an
environment variable); common::post_from supplies a peer address to the
in-process requests. Python: test_get_test_token.py rewritten, TOKEN_ENV
is now empty (no setting needed).
* README.md, docs/gn-auth.md, docs/SKILL.md, RELEASE_NOTES.md: document
the check, the switch, and "Behind nginx or in a container" (a bridge
container sees the gateway as the peer; a host-network container behind
nginx sees 127.0.0.1, so block the path in nginx, set X-Forwarded-For,
or switch the endpoint off).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'docs')
-rw-r--r--docs/SKILL.md57
-rw-r--r--docs/gn-auth.md25
2 files changed, 48 insertions, 34 deletions
diff --git a/docs/SKILL.md b/docs/SKILL.md
index 243c064..ce936d7 100644
--- a/docs/SKILL.md
+++ b/docs/SKILL.md
@@ -215,19 +215,30 @@ Use `utoipa` + `utoipa-axum`, not a hand-maintained spec file:
   collision silently returning the wrong thing -- resolving `container`
   first instead means only a much smaller set of names (containers) is
   ever disambiguated across categories, not every item name ever.
-- A dev-only endpoint that hands out a credential (e.g. proxies an
-  auth service's own token grant for a well-known test user) needs at
-  least two independent config conditions to enable it (e.g. "dev mode
-  on" AND "bound to loopback only"), not one flag -- and the condition
-  should be a plain function with its own unit tests
-  (`Config::dev_token_enabled()`), not inline `if` logic duplicated at
-  each call site. Register the route conditionally at startup rather
-  than registering it unconditionally and rejecting inside the
-  handler: an unregistered route is a 404 and genuinely absent from the
-  generated OpenAPI spec, so a production deployment doesn't even
-  document that a token-issuing endpoint could exist. Default any
-  credentials this endpoint needs to the target service's own published
-  "dev fixtures" (a well-known test client/user every checkout of that
+- An endpoint that hands out a credential (e.g. proxies an auth
+  service's own token grant for a well-known test user) should decide
+  per request, from the connection, whether to answer -- not from a mode
+  flag or a bind address, which are easy to leave set wrongly in a
+  deployment. Answer only a caller whose TCP peer address is loopback
+  (`axum::extract::ConnectInfo`; serve with
+  `into_make_service_with_connect_info`), and fail closed when there is
+  no peer address. Never establish "local" from a header, but do refuse a
+  request that carries `X-Forwarded-For`/`Forwarded`/`X-Real-IP`: a
+  reverse proxy on the same host connects from loopback for every client,
+  so without that refusal the whole internet looks local. Keep the
+  decision in small plain functions with their own unit tests, test the
+  handler with injected peer addresses (a local one, several remote
+  ones, none at all, and a local one with a proxy header), and list the
+  403 in the OpenAPI responses. Default any credentials the endpoint
+  needs to the target service's own published "dev fixtures" (a
+  well-known test client/user every checkout of that service already
+  has) so it works with zero extra configuration.
+  Also give deployments a way to leave the route out entirely (a config
+  switch checked when the router is built, so the route is a 404 and
+  absent from the OpenAPI spec). Make that switch fail closed: parse it
+  strictly so only a clearly affirmative value keeps the endpoint on and
+  a typo ("flase") turns it off, since the safe state of a
+  credential-issuing endpoint is "not running".
   service already has) so it works with zero extra configuration.
 
 ## Testing
@@ -262,19 +273,19 @@ Two parallel suites exercising the same endpoints:
   once per test *file*, not just once overall -- that's expected, not
   a sign the fix didn't work; what matters is that repeated calls
   inside one file (or one real server process) stay fast after it.
-- If endpoints need a bearer token and the service has a dev-only
+- If endpoints need a bearer token and the service has a local-only
   token-issuing endpoint (see "Authentication/authorization" above),
   have the test suite fetch its own token from it rather than requiring
   a human to mint and export one by hand first. Try that first, fall
-  back to an externally-provided token (an env var) for a setup that
-  doesn't want dev mode on even for tests -- so the suite still works
-  unmodified either way. For the in-process Rust suite this means
-  calling the router's own dev endpoint via the same `oneshot` plumbing
-  used for every other request; for a suite that spawns the real
-  binary as a subprocess (Python, here), it means starting that
-  specific test's subprocess with the dev-mode env vars set so it can
-  answer its own token request over the same HTTP connection the test
-  already uses.
+  back to an externally-provided token (an env var) when there is none
+  -- so the suite still works unmodified either way. For the in-process
+  Rust suite this means calling the router's own endpoint via the same
+  `oneshot` plumbing used for every other request, with a local peer
+  address injected into the request (there is no real connection);
+  for a suite that spawns the real binary as a subprocess (Python,
+  here), the test client is already local, so the subprocess answers
+  its own token request over the same HTTP connection the test already
+  uses.
 - Mutating process-wide state (env vars, global config) from a test is
   only safe when nothing else in the same process can read or write
   that same state concurrently. A single compiled test binary (one
diff --git a/docs/gn-auth.md b/docs/gn-auth.md
index 1c89c63..8e39773 100644
--- a/docs/gn-auth.md
+++ b/docs/gn-auth.md
@@ -345,24 +345,27 @@ password grant for gn-auth's well-known development user
 (`test@development.user`, from `flask init-dev-clients`/
 `gn_auth/wsgi.py`) and returns the result as the `data` of the usual
 success envelope (README, "Success responses"). See the README's
-"Development mode" section for how to enable it
-(`GENENETWORK4_DEV_MODE=true` and `GENENETWORK4_BIND=127.0.0.1`,
-required together) and gn-auth's own `AUTHLIB_INSECURE_TRANSPORT`
-requirement for accepting the grant over plain HTTP at all.
+"Development" section for who it answers (only a caller on this
+machine; nothing to enable) and gn-auth's own
+`AUTHLIB_INSECURE_TRANSPORT` requirement for accepting the grant over
+plain HTTP at all.
 
 This doesn't change anything about how genenetwork4 verifies or
 authorizes tokens (src/auth.rs) -- it is purely a convenience for
-obtaining one of the tokens that mechanism already accepts, scoped to
-local development by two independent config checks rather than one.
+obtaining one of the tokens that mechanism already accepts, limited to
+callers on the same machine by a per-request check of the connection's
+peer address (127.0.x.x or ::1, and no proxy header). That check sees a
+reverse proxy or a container's network gateway as the peer, so a
+deployed instance needs more than this -- ideally
+`GENENETWORK4_TEST_TOKEN=false`, which leaves the route out entirely;
+see the README's "Behind nginx or in a container".
 
 The test suite uses it automatically: `tests/common::auth_token()`
 (Rust) and `test/python/auth_helpers.auth_token()` (Python) both try
 `POST /get-test-token` first, falling back to a `GN4_TEST_AUTH_TOKEN`
-env var only if that fails. Set `GENENETWORK4_DEV_MODE=true` and
-`GENENETWORK4_BIND=127.0.0.1` for the test run (plus a reachable
-gn-auth with `AUTHLIB_INSECURE_TRANSPORT` set) and the whole suite --
-every endpoint now needing a token -- runs with no token manually
-provisioned at all.
+env var only if that fails. With a reachable gn-auth
+(`AUTHLIB_INSECURE_TRANSPORT` set) the whole suite -- every endpoint now
+needing a token -- runs with no token manually provisioned at all.
 
 ### Found wrong in gn-auth while building this