Repository navigation
feat(security): harden HTTP server auth, CORS, input validation - #254
Conversation
…205 #132 #101) Security fixes: - Default bind address changed from 0.0.0.0 to 127.0.0.1 (#205) - Bearer token authentication for all endpoints except GET /health (#101 #204) - Config: uteke.toml [server].auth_token - CLI: --auth-token <TOKEN> - Env: UTEKE_AUTH_TOKEN - Precedence: CLI > env > config - Constant-time token comparison to resist timing attacks - Configurable CORS origins replacing wildcard (#204) - Config: uteke.toml [server].cors_origins = [...] - CLI: --cors-origin <URL> (repeatable) - Origin matching: request Origin header matched against allowlist - Preflight: validated header allowlist (no blind reflection) - When auth + wildcard CORS: Authorization header excluded from CORS - Input validation wired to /remember endpoint (#132) - Content length, tag count, tag length validated via validate_input() - Startup warnings for insecure configurations - Warns when auth disabled - Warns when CORS wildcard with auth enabled - Non-browser clients (curl, SDKs) unaffected by CORS restrictions Closes #204, Closes #205, Closes #132, Closes #101
|
Warning Review limit reached
More reviews will be available in 20 minutes and 47 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR adds bearer-token authentication and configurable per-request CORS handling to the server. A new ChangesBearer-Token Auth & CORS Support
Sequence DiagramsequenceDiagram
participant Request as HTTP Request
participant Router as Router
participant Auth as check_auth()
participant ReqCtx as ReqCtx
participant Handler as Endpoint Handler
participant Response as CORS Response
Request->>Router: incoming request (origin, auth header)
alt OPTIONS Preflight
Router->>Response: skip auth, return CORS headers
else GET /health
Router->>Handler: skip auth, process endpoint
else Other Routes
Router->>Auth: validate bearer token
alt Auth Success
Auth->>ReqCtx: token valid
ReqCtx->>Handler: pass context to handler
Handler->>ReqCtx: handler returns data
ReqCtx->>Response: apply CORS headers + JSON body
else Auth Failure
Auth->>Response: return 401 error with CORS headers
end
end
Response-->>Request: HTTP response
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🔍 Cora AI Code Review❌ Blocked — critical issues found. 🔴 Error (1)
Review powered by cora-cli · BYOK · MIT |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
crates/uteke-server/src/main.rs (1)
269-273: 💤 Low valueFallback behavior could be clarified.
When the browser requests headers that aren't in the allowlist (resulting in empty intersection), this falls back to returning the full allowlist. While not a security issue (browser will still enforce CORS on the actual request), it's inconsistent with the comment at line 259 stating "only echo back headers that are in our allowlist."
Consider returning an empty string or explicitly returning the full allowlist as the default rather than using it as a fallback for no-match scenarios.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/uteke-server/src/main.rs` around lines 269 - 273, The current logic sets allow_headers to allowed_headers_set.join(", ") when the computed intersection (allow_headers) is empty, which contradicts the earlier comment about echoing only headers in our allowlist; update the behavior in the code handling allow_headers so that when the intersection is empty it returns an empty string (or explicitly returns the full allowlist only if you intend that as the explicit default), and keep the change localized to the same block that assigns allow_headers and references allowed_headers_set so the result matches the comment "only echo back headers that are in our allowlist."
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/uteke-server/src/main.rs`:
- Around line 178-188: The current constant_time_eq returns immediately on
a.len() != b.len(), leaking token length to timing attacks; instead, change auth
checks in check_auth to compare fixed-size digests: hash both the incoming token
and the configured token with a fixed-size digest (e.g., SHA-256) and then
perform a constant-time comparison on the resulting fixed-size byte arrays (use
subtle::ConstantTimeEq or a purpose-built constant_time_eq that accepts
fixed-size [u8; N] and does not early-return). Update check_auth to compute the
digest for both values before calling the comparator and remove/replace the
length-based early-return in constant_time_eq so only fixed-size digests are
compared.
---
Nitpick comments:
In `@crates/uteke-server/src/main.rs`:
- Around line 269-273: The current logic sets allow_headers to
allowed_headers_set.join(", ") when the computed intersection (allow_headers) is
empty, which contradicts the earlier comment about echoing only headers in our
allowlist; update the behavior in the code handling allow_headers so that when
the intersection is empty it returns an empty string (or explicitly returns the
full allowlist only if you intend that as the explicit default), and keep the
change localized to the same block that assigns allow_headers and references
allowed_headers_set so the result matches the comment "only echo back headers
that are in our allowlist."
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1d28d42b-33cc-4be3-8da9-d919eb171fd6
📒 Files selected for processing (1)
crates/uteke-server/src/main.rs
…ve bearer, DRY 401 - Replace constant_time_eq with SHA-256 digest comparison to prevent token length leakage via timing attacks. Configured token hash is precomputed at startup; only incoming token is hashed per-request. - Parse Authorization header case-insensitively per RFC 7235. Split on whitespace, require exactly 2 parts (scheme + token). - Extract unauthorized_response() helper to eliminate 3x duplicated 401 response building code in check_auth. - Fix preflight header fallback: return empty string instead of full allowlist when no requested headers match (consistent with comments).
* feat(security): harden HTTP server auth, CORS, input validation (#204 #205 #132 #101) Security fixes: - Default bind address changed from 0.0.0.0 to 127.0.0.1 (#205) - Bearer token authentication for all endpoints except GET /health (#101 #204) - Config: uteke.toml [server].auth_token - CLI: --auth-token <TOKEN> - Env: UTEKE_AUTH_TOKEN - Precedence: CLI > env > config - Constant-time token comparison to resist timing attacks - Configurable CORS origins replacing wildcard (#204) - Config: uteke.toml [server].cors_origins = [...] - CLI: --cors-origin <URL> (repeatable) - Origin matching: request Origin header matched against allowlist - Preflight: validated header allowlist (no blind reflection) - When auth + wildcard CORS: Authorization header excluded from CORS - Input validation wired to /remember endpoint (#132) - Content length, tag count, tag length validated via validate_input() - Startup warnings for insecure configurations - Warns when auth disabled - Warns when CORS wildcard with auth enabled - Non-browser clients (curl, SDKs) unaffected by CORS restrictions Closes #204, Closes #205, Closes #132, Closes #101 * style: cargo fmt * fix(security): address review comments — SHA-256 auth, case-insensitive bearer, DRY 401 - Replace constant_time_eq with SHA-256 digest comparison to prevent token length leakage via timing attacks. Configured token hash is precomputed at startup; only incoming token is hashed per-request. - Parse Authorization header case-insensitively per RFC 7235. Split on whitespace, require exactly 2 parts (scheme + token). - Extract unauthorized_response() helper to eliminate 3x duplicated 401 response building code in check_auth. - Fix preflight header fallback: return empty string instead of full allowlist when no requested headers match (consistent with comments).
Security Hardening — Priority 1
Addresses issues #204, #205, #132, #101.
Changes
#205 — Default bind address
0.0.0.0→127.0.0.1#101/#204 — Bearer token authentication
Authorization: Bearer <token>required on all endpoints exceptGET /healthuteke.toml:[server].auth_token = "..."--auth-token <TOKEN>UTEKE_AUTH_TOKEN#204 — Configurable CORS origins
*with configurable allowlistuteke.toml:cors_origins = ["http://localhost:3000"]--cors-origin <URL>(repeatable)Originheader matched against allowlistAuthorizationexcluded from CORS allowed headers#132 — Input validation on /remember
validate_input()Startup warnings
Validation
cargo clippy --all-targets -- -D warnings— cleancargo test— 116/116 passSummary by CodeRabbit
New Features
Chores
--auth-tokenand repeatable--cors-originCLI flagsUTEKE_AUTH_TOKENenvironment variable