Time policy and /time endpoint
Browse files at this commitd34ceb3fd7bacecf7e5b5b55d2498287b7f430f7Parent: 877eead- docs
- time policy and /time endpoint
1 changed file +31 -0
- README.md +31 -0
README.mdmodified+31 -0
| @@ -58,6 +58,36 @@ the docs pages, `register` / `login` / `logout`) don't (`logout` is idempotent | |||
| 58 | it clears the session if one is present). Set `COOKIE_SECURE=true` when serving | 58 | it clears the session if one is present). Set `COOKIE_SECURE=true` when serving |
| 59 | behind TLS to add the cookie's `Secure` attribute. | 59 | behind TLS to add the cookie's `Secure` attribute. |
| 60 | 60 | ||
| 61 | +## Time policy | ||
| 62 | + | ||
| 63 | +Timezones and clock differences cannot corrupt data, by construction: | ||
| 64 | + | ||
| 65 | +- **Every instant is a UTC unix-millisecond `i64`** (`domain::timestamp::Timestamp`), | ||
| 66 | + stored as an `int`, sent as a plain number. Nothing stores or parses a | ||
| 67 | + timezone, so the server's `TZ`, the container's clock config, and the | ||
| 68 | + client's locale are all irrelevant — clients convert millis to local time | ||
| 69 | + for display only. | ||
| 70 | +- **One clock choke point.** `Timestamp::now()` is the only wall-clock read. | ||
| 71 | + `clippy.toml` bans every other source (`chrono::Local`/`Utc::now`, | ||
| 72 | + `SystemTime::now`, `time::OffsetDateTime::now_*`, absolute cookie | ||
| 73 | + `Expires`) and `[lints.clippy]` raises that to a hard `cargo clippy` error, | ||
| 74 | + so local-time bugs can't be reintroduced. | ||
| 75 | +- **Expiry is server-authoritative.** Sessions expire by comparing the stored | ||
| 76 | + millis against the server clock; the cookie carries a relative `Max-Age` | ||
| 77 | + (never an absolute `Expires`), so a wrong client clock changes nothing. | ||
| 78 | +- **Pacing uses the monotonic clock.** The rate limiter runs on `Instant`, | ||
| 79 | + immune to NTP steps and wall-clock jumps. | ||
| 80 | +- **Calendar dates get a timezone grace.** A birth date is a calendar date on | ||
| 81 | + the writer's wall, not an instant: validation accepts up to UTC-tomorrow, | ||
| 82 | + since a client ahead of UTC (up to UTC+14) legitimately writes a date the | ||
| 83 | + server's UTC calendar hasn't reached yet. | ||
| 84 | +- **Frontends can sync to the server clock.** `GET /time` (no auth) returns | ||
| 85 | + `{"now": <UTC unix-millis>}`. Fetch once, keep | ||
| 86 | + `offset = now - Date.now()`, and use `Date.now() + offset` for countdowns | ||
| 87 | + and past/future checks instead of trusting the device clock. | ||
| 88 | + | ||
| 89 | +Keep the single server's clock NTP-synced; that's the only clock that matters. | ||
| 90 | + | ||
| 61 | ## Rate limiting | 91 | ## Rate limiting |
| 62 | 92 | ||
| 63 | Requests are limited per client IP over a fixed 60-second window, in two tiers: | 93 | Requests are limited per client IP over a fixed 60-second window, in two tiers: |
| @@ -150,6 +180,7 @@ logged-in user. | |||
| 150 | |--------|----------------------------------|---------|---------------------------------| | 180 | |--------|----------------------------------|---------|---------------------------------| |
| 151 | | GET | `/health` | no | Liveness check | | 181 | | GET | `/health` | no | Liveness check | |
| 152 | | GET | `/` | no | Same as `/health` | | 182 | | GET | `/` | no | Same as `/health` | |
| 183 | +| GET | `/time` | no | Server clock: `{now}` UTC unix-millis (frontend sync) | | ||
| 153 | | GET | `/swagger` | no | Interactive API docs (Swagger UI) | | 184 | | GET | `/swagger` | no | Interactive API docs (Swagger UI) | |
| 154 | | GET | `/api-docs/openapi.json` | no | Raw OpenAPI 3 spec | | 185 | | GET | `/api-docs/openapi.json` | no | Raw OpenAPI 3 spec | |
| 155 | | POST | `/auth/register` | no | `{username, password}` (new users are `student`) | | 186 | | POST | `/auth/register` | no | `{username, password}` (new users are `student`) | |