Skip to content

security: fix CORS misconfiguration and unclosed file handle#3

Draft
ghost wants to merge 1 commit into
mainfrom
security/fix-cors-and-resource-leak-v2
Draft

security: fix CORS misconfiguration and unclosed file handle#3
ghost wants to merge 1 commit into
mainfrom
security/fix-cors-and-resource-leak-v2

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Apr 26, 2026

Opened by @roomote-v0 on behalf of J oz

Summary

Addresses the review feedback from PR #2 and implements the security fixes with the corrected wildcard origin check.

Changes

1. CORS Misconfiguration Fix (HIGH)

The server was configured with allow_origins=["*"] combined with allow_credentials=True, which is a security anti-pattern per the CORS spec.

What changed:

  • Added a configurable cors_allow_origins setting to ServerSettings (defaults to ["*"])
  • allow_credentials is now set to "*" not in server_settings.cors_allow_origins

Key improvement over PR #2: The previous PR used server_settings.cors_allow_origins != ["*"] which only caught the exact single-wildcard list. This fix uses "*" not in server_settings.cors_allow_origins to correctly detect wildcards even in mixed lists like ["*", "https://example.com"].

2. Unclosed File Handle Fix (LOW)

In model.py, json.load(open(...)) was used without a context manager, creating a potential resource leak.

Fix: Wrapped in a proper with statement.


View task on Roo Code Cloud

- Add configurable cors_allow_origins setting to ServerSettings
- Disable allow_credentials when any wildcard origin is present
  (uses "in" check instead of equality to catch mixed lists like
  ["*", "https://example.com"])
- Fix unclosed file handle in model.py with proper context manager
@ghost
Copy link
Copy Markdown
Author

ghost commented Apr 26, 2026

Rooviewer Clock   See task

I have reviewed the changes and left a comment regarding file encoding for the tokenizer config.

  • Explicitly specify encoding="utf-8" when reading the tokenizer config

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

Comment thread llama_cpp/server/model.py
chat_handler = llama_cpp.llama_chat_format.hf_tokenizer_config_to_chat_completion_handler(
json.load(open(settings.hf_tokenizer_config_path))
)
with open(settings.hf_tokenizer_config_path) as f:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When opening JSON files, it's highly recommended to explicitly specify encoding="utf-8". Without it, open() uses the platform's default encoding (e.g., cp1252 on Windows), which will cause UnicodeDecodeError or corrupted data when reading tokenizer configurations that contain special or non-ASCII characters.

            with open(settings.hf_tokenizer_config_path, encoding="utf-8") as f:

Fix it with Roo Code or mention @roomote and request a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant