openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

feat: make CORS origin configurable for cloud deployments

Open hobostay opened this issue 6 months ago • 4 comments

Summary

  • Adds OPENHUMAN_CORS_ORIGIN environment variable to configure the JSON-RPC server's Access-Control-Allow-Origin header
  • Default (unset) preserves current behavior: wildcard * for local/Tauri desktop
  • When set to a specific origin (e.g. https://app.example.com), CORS is restricted to that origin only
  • Falls back to wildcard on invalid input so the server never crashes over a typo

Context

The JSON-RPC server in src/core/jsonrpc.rs hardcodes Access-Control-Allow-Origin: *. This is correct for the desktop case where the sidecar binds to 127.0.0.1, but becomes a security concern for Docker/cloud deployments where OPENHUMAN_CORE_HOST=0.0.0.0 — any website could make cross-origin requests to the API.

The .env.example already documents cloud deployment mode (OPENHUMAN_CORE_HOST=0.0.0.0) and gitbooks/features/cloud-deploy.md covers the setup. This change gives cloud deployers the ability to lock down CORS without modifying source code.

Test plan

  • [ ] Verify local desktop still works with OPENHUMAN_CORS_ORIGIN unset (wildcard)
  • [ ] Set OPENHUMAN_CORS_ORIGIN=https://example.com and verify CORS header reflects the value
  • [ ] Set OPENHUMAN_CORS_ORIGIN to an invalid string and verify fallback to wildcard

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • New Features
    • Added configurable CORS origin support, enabling operators to selectively restrict cross-origin requests to specific trusted origins instead of accepting all requests. Configuration is managed via an optional environment variable with a permissive default to maintain backward compatibility. This enhances security for production deployments.

Review Change Stack

hobostay avatar May 20 '26 12:05 hobostay

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 10c03f9a-ba1a-48be-8471-0fb319c1ce14

📥 Commits

Reviewing files that changed from the base of the PR and between c441d2ea31f1f0a63239572ece496d38812f9962 and c6530c923a6736e1ad13aa51f38900af0981d980.

📒 Files selected for processing (2)
  • .env.example
  • src/core/jsonrpc.rs
💤 Files with no reviewable changes (1)
  • src/core/jsonrpc.rs
✅ Files skipped from review due to trivial changes (1)
  • .env.example

📝 Walkthrough

Walkthrough

Adds a commented OPENHUMAN_CORE_ALLOWED_ORIGINS entry to .env.example and a doc comment above with_cors_headers in src/core/jsonrpc.rs advising using that env var (comma-separated) for non-loopback Docker/cloud deployments instead of wildcarding CORS.

Changes

CORS docs and comments

Layer / File(s) Summary
Env example and doc comment
.env.example, src/core/jsonrpc.rs
Adds a commented OPENHUMAN_CORE_ALLOWED_ORIGINS example to .env.example and a doc comment above with_cors_headers advising the env var be used (comma-separated origins) when the server binds to 0.0.0.0; no runtime logic changes.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Possibly related PRs

Suggested labels

rust-core, working

Suggested reviewers

  • M3gA-Mind
  • sanil-23

Poem

🐰 I nibbled docs beneath the moonlit code,
A hint for origins, in a safe, small node.
Commented lines to guide the cloud-bound host,
No logic changed, just a helpful post.
Hooray — deployment's safer, says this rabbit most.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change—making CORS origins configurable—which directly corresponds to the PR objectives and file modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 20 '26 12:05 coderabbitai[bot]

@hobostay CI is failing on changes in this PR — please fix before review.

M3gA-Mind avatar May 20 '26 13:05 M3gA-Mind

@hobostay CI is failing on changes in this PR — please fix before review.

M3gA-Mind avatar May 20 '26 13:05 M3gA-Mind

@hobostay CI is failing on changes in this PR — please fix before review.

M3gA-Mind avatar May 20 '26 14:05 M3gA-Mind

Per pr-manager-lite finishing pass:

Merge with main: Resolved the src/core/jsonrpc.rs CORS conflict. Main now ships a proper Origin-echoing allowlist (is_origin_allowed keyed off OPENHUMAN_CORE_ALLOWED_ORIGINS), so the original OPENHUMAN_CORS_ORIGIN wildcard mechanism this PR introduced is superseded. Took main's with_cors_headers implementation, kept this PR's contribution by:

  • documenting OPENHUMAN_CORE_ALLOWED_ORIGINS in .env.example for the Docker / cloud deployment use case
  • adding a rustdoc note above with_cors_headers pointing operators at the env var instead of wildcard

CodeRabbit comment (jsonrpc.rs:635 — log invalid OPENHUMAN_CORS_ORIGIN fallback): stale. The env var no longer exists; the invalid-value-falls-back-to-wildcard path it asked to log is gone. The spirit of the request (observability on the security-relevant path) is already satisfied by tracing::warn!("[cors] rejected disallowed origin: ...") introduced on main.

CI failures (Type Check / Rust Quality fmt+clippy / PR Submission Checklist): those failures were against the pre-merge code. cargo fmt --check and cargo check are clean on the merged tree; the CORS unit tests (8/8) pass.

Commits pushed to hobostay:fix/configurable-cors-origins:

  • abd87a5 Merge branch 'main' into pr/2344
  • c6530c9 chore(pr-fix): drop stray blank line in with_cors_headers

senamakel avatar May 22 '26 23:05 senamakel