Skip to content

πŸ›‘οΈ Sentinel: [MEDIUM] hmac.compare_digestμ—μ„œμ˜ μ²˜λ¦¬λ˜μ§€ μ•Šμ€ μ˜ˆμ™Έλ‘œ μΈν•œ DoS 취약점 μˆ˜μ • - #386

Closed
seonghobae wants to merge 1 commit into
mainfrom
sentinel-hmac-fix-2895693522345377856
Closed

πŸ›‘οΈ Sentinel: [MEDIUM] hmac.compare_digestμ—μ„œμ˜ μ²˜λ¦¬λ˜μ§€ μ•Šμ€ μ˜ˆμ™Έλ‘œ μΈν•œ DoS 취약점 μˆ˜μ •#386
seonghobae wants to merge 1 commit into
mainfrom
sentinel-hmac-fix-2895693522345377856

Conversation

@seonghobae

@seonghobae seonghobae commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

🚨 Severity: MEDIUM
πŸ’‘ Vulnerability: hmac.compare_digestλŠ” ASCII μ „μš© λ¬Έμžμ—΄μ΄λ‚˜ λ°”μ΄νŠΈλ§Œ μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ•…μ˜μ μΈ μ‚¬μš©μžκ°€ x-api-key HTTP 헀더에 ASCIIκ°€ μ•„λ‹Œ 문자λ₯Ό ν¬ν•¨μ‹œν‚€λ©΄ μ²˜λ¦¬λ˜μ§€ μ•Šμ€ TypeErrorκ°€ λ°œμƒν•˜μ—¬ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μΆ©λŒν•˜κ³  500 Internal Server Errorλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
🎯 Impact: μ μ ˆν•œ 401 μƒνƒœ μ½”λ“œ λ°˜ν™˜μ„ μš°νšŒν•˜μ—¬ μŠ€νƒ 트레이슀 λ…ΈμΆœ λ˜λŠ” μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ DoS 곡격을 μœ λ°œν•  수 μžˆμŠ΅λ‹ˆλ‹€.
πŸ”§ Fix: λΉ„κ΅ν•˜κΈ° 전에 두 λ¬Έμžμ—΄μ„ λͺ¨λ‘ λ°”μ΄νŠΈ(UTF-8)둜 μ•ˆμ „ν•˜κ²Œ μΈμ½”λ”©ν•˜μ—¬ λͺ¨λ“  문자 μž…λ ₯을 였λ₯˜ 없이 μ²˜λ¦¬ν•˜λ„λ‘ μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€.
βœ… Verification: ν…ŒμŠ€νŠΈλ₯Ό μ‹€ν–‰ν•˜μ—¬ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ ASCIIκ°€ μ•„λ‹Œ λ¬Έμžμ— λŒ€ν•΄ 더 이상 μΆ©λŒν•˜μ§€ μ•Šκ³  401 응닡을 μ˜¬λ°”λ₯΄κ²Œ λ°˜ν™˜ν•˜λŠ”μ§€ ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€.


PR created automatically by Jules for task 2895693522345377856 started by @seonghobae

Summary by CodeRabbit

  • 버그 μˆ˜μ •

    • λΉ„ASCII λ¬Έμžκ°€ ν¬ν•¨λœ API ν‚€ 헀더도 μ²˜λ¦¬λ˜μ§€ μ•Šμ€ μ˜ˆμ™Έ 없이 μ•ˆμ „ν•˜κ²Œ κ±°λΆ€λ˜λ„λ‘ κ°œμ„ ν–ˆμŠ΅λ‹ˆλ‹€.
    • 잘λͺ»λœ API ν‚€ μš”μ²­μ΄ λ‚΄λΆ€ 였λ₯˜(500) λŒ€μ‹  ν‘œμ€€ 인증 μ‹€νŒ¨(401) 응닡을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • ν…ŒμŠ€νŠΈ

    • λ‹€μ–‘ν•œ 문자 인코딩과 μ›μ‹œ 헀더λ₯Ό ν¬ν•¨ν•œ API ν‚€ 인증 νšŒκ·€ ν…ŒμŠ€νŠΈλ₯Ό μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€.
  • λ¬Έμ„œ

    • 이번 API ν‚€ 인증 였λ₯˜ μˆ˜μ • 사항을 λ³€κ²½ λ‘œκ·Έμ— λ°˜μ˜ν–ˆμŠ΅λ‹ˆλ‹€.

@google-labs-jules

Copy link
Copy Markdown

πŸ‘‹ Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a πŸ‘€ emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

πŸ“ Walkthrough

Walkthrough

API ν‚€ κ²€μ¦μ—μ„œ hmac.compare_digest에 UTF-8 λ°”μ΄νŠΈλ₯Ό μ „λ‹¬ν•˜λ„λ‘ λ³€κ²½ν–ˆμŠ΅λ‹ˆλ‹€. λΉ„ASCII x-api-key ν—€λ”λŠ” 500 였λ₯˜ λŒ€μ‹  401 응닡을 λ°˜ν™˜ν•˜λ„λ‘ νšŒκ·€ ν…ŒμŠ€νŠΈμ™€ μž¬ν˜„ μ½”λ“œλ₯Ό μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€.

Changes

API ν‚€ 검증

Layer / File(s) Summary
API ν‚€ 비ꡐ 및 νšŒκ·€ 검증
saas_web.py, tests/test_saas_web.py, CHANGELOG.md, .jules/sentinel.md
API ν‚€ 비ꡐ 인자λ₯Ό UTF-8 λ°”μ΄νŠΈλ‘œ λ³€κ²½ν–ˆμŠ΅λ‹ˆλ‹€. λΉ„ASCII ν—€λ”μ˜ 401 응닡을 κ²€μ¦ν•˜λŠ” ν…ŒμŠ€νŠΈμ™€ λ³€κ²½ 기둝을 μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€.
HMAC λ°”μ΄νŠΈ λ™μž‘ 확인
test_encode.py, test_hmac_encode.py, test_hmac_exception.py
λ¬Έμžμ—΄κ³Ό UTF-8 λ°”μ΄νŠΈμ— λŒ€ν•œ hmac.compare_digest λ™μž‘ 확인 μ½”λ“œλ₯Ό μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€.
HTTP 미듀웨어 μž¬ν˜„ 흐름
test_hmac_header.py, test_hmac_latin1.py, test_hmac_direct.py, test_hmac_direct2.py, test_direct.py
FastAPI API ν‚€ 미듀웨어와 ν…ŒμŠ€νŠΈ μš”μ²­μ„ μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€. λΉ„ASCII 헀더에 λŒ€ν•œ 인증 μ‹€νŒ¨ 및 μ˜ˆμ™Έ 응닡을 ν™•μΈν•©λ‹ˆλ‹€.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • ContextualWisdomLab/codec-carver#361: API ν‚€λ₯Ό UTF-8 λ°”μ΄νŠΈλ‘œ λ³€ν™˜ν•΄ hmac.compare_digest의 λΉ„ASCII μž…λ ₯ 였λ₯˜λ₯Ό μ²˜λ¦¬ν•©λ‹ˆλ‹€.
  • ContextualWisdomLab/codec-carver#376: λ™μΌν•œ saas_web.py μˆ˜μ •κ³Ό λΉ„ASCII API ν‚€ νšŒκ·€ ν…ŒμŠ€νŠΈλ₯Ό ν¬ν•¨ν•©λ‹ˆλ‹€.
πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed 제λͺ©μ€ λΉ„ASCII x-api-key 처리 쀑 λ°œμƒν•˜λŠ” hmac.compare_digest μ˜ˆμ™Έμ™€ DoS 취약점 μˆ˜μ •μ΄λΌλŠ” μ£Όμš” 변경을 λͺ…ν™•ν•˜κ²Œ μ„€λͺ…ν•©λ‹ˆλ‹€.
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.
✨ Finishing Touches πŸ’‘ 1
πŸ“ Generate docstrings πŸ’‘
  • Create stacked PR
  • Commit on current branch
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sentinel-hmac-fix-2895693522345377856

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

πŸ€– 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 `@saas_web.py`:
- Line 117: Update saas_web.py lines 117-117 in get_configured_api_keys() to
load runtime API keys from the credential registry/KV instead of directly
reading CODEC_CARVER_API_KEYS from the environment, while preserving the
existing key comparison behavior. Update tests/test_saas_web.py lines 642-652 to
configure the test API key in the test registry/KV rather than patching the
environment variable.

In `@test_hmac_direct2.py`:
- Around line 1-44: Remove the import-time reproduction scripts from
test_hmac_direct2.py (1-44), test_encode.py (1-10), test_hmac_encode.py (1-6),
test_hmac_exception.py (1-7), test_hmac_header.py (1-27), test_hmac_latin1.py
(1-27), test_hmac_direct.py (1-33), and test_direct.py (1-11); delete each
file’s executable comparison, FastAPI/Uvicorn server, raw socket, or localhost
request code so these files cannot run during test collection, relying on
tests/test_saas_web.py for regression coverage.
πŸͺ„ Autofix

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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9edf0d4c-c99a-4651-bb83-d5ece31cf3d5

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between b2bc83a and dba0a1f.

πŸ“’ Files selected for processing (12)
  • .jules/sentinel.md
  • CHANGELOG.md
  • saas_web.py
  • test_direct.py
  • test_encode.py
  • test_hmac_direct.py
  • test_hmac_direct2.py
  • test_hmac_encode.py
  • test_hmac_exception.py
  • test_hmac_header.py
  • test_hmac_latin1.py
  • tests/test_saas_web.py

Comment thread saas_web.py
provided_key = request.headers.get("x-api-key", "")
if not any(
hmac.compare_digest(provided_key, key) for key in configured_keys
hmac.compare_digest(provided_key.encode("utf-8"), key.encode("utf-8")) for key in configured_keys

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

πŸ”’ Security & Privacy | 🟠 Major | πŸ—οΈ Heavy lift

λŸ°νƒ€μž„ API ν‚€ μ†ŒμŠ€λ₯Ό credential registry/KV둜 μ΄μ „ν•˜μ„Έμš”.

saas_web.py의 get_configured_api_keys()λŠ” Line 97μ—μ„œ CODEC_CARVER_API_KEYSλ₯Ό 직접 μ½μŠ΅λ‹ˆλ‹€. μƒˆ 인증 흐름도 이 값을 μ‚¬μš©ν•©λ‹ˆλ‹€. ν™˜κ²½ λ³€μˆ˜μ—μ„œ 직접 μ½λŠ” 방식은 ν‚€ νšŒμ „, μ ‘κ·Ό μ œμ–΄, 감사 경둜λ₯Ό μš°νšŒν•©λ‹ˆλ‹€.

  • saas_web.py#L117-L117: get_configured_api_keys()κ°€ credential registry/KVμ—μ„œ λŸ°νƒ€μž„ ν‚€λ₯Ό 읽도둝 λ³€κ²½ν•˜μ„Έμš”.
  • tests/test_saas_web.py#L642-L652: ν™˜κ²½ λ³€μˆ˜λ₯Ό νŒ¨μΉ˜ν•˜μ§€ 말고, ν…ŒμŠ€νŠΈμš© registry/KV에 ν‚€λ₯Ό μ„€μ •ν•˜μ„Έμš”.

As per coding guidelines, β€œsaas_web.py must source runtime API keys, database credentials, endpoints, and other secrets from the credential registry/KV rather than directly from environment variables.”

πŸ“ Affects 2 files
  • saas_web.py#L117-L117 (this comment)
  • tests/test_saas_web.py#L642-L652
πŸ€– 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 `@saas_web.py` at line 117, Update saas_web.py lines 117-117 in
get_configured_api_keys() to load runtime API keys from the credential
registry/KV instead of directly reading CODEC_CARVER_API_KEYS from the
environment, while preserving the existing key comparison behavior. Update
tests/test_saas_web.py lines 642-652 to configure the test API key in the test
registry/KV rather than patching the environment variable.

Source: Coding guidelines

Comment thread test_hmac_direct2.py
Comment on lines +1 to +44
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import hmac
import os
import uvicorn
import threading
import time
import socket

app = FastAPI()

@app.middleware("http")
async def require_api_key(request: Request, call_next):
configured_keys = ["secret-key"]
provided_key = request.headers.get("x-api-key", "")
try:
if not any(
hmac.compare_digest(provided_key, key) for key in configured_keys
):
return JSONResponse(status_code=401, content={"error": "Invalid"})
except Exception as e:
print(f"Server caught exception: {repr(e)}")
return JSONResponse(status_code=500, content={"error": repr(e)})
return await call_next(request)

@app.get("/")
def read_root():
return {"Hello": "World"}

def run_server():
uvicorn.run(app, host="127.0.0.1", port=8000, log_level="error")

t = threading.Thread(target=run_server, daemon=True)
t.start()
time.sleep(1)

# Manually send a raw HTTP request with non-ASCII header
req = b"GET / HTTP/1.1\r\nHost: localhost:8000\r\nx-api-key: \xff\r\n\r\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 8000))
s.sendall(req)
resp = s.recv(4096)
print(resp.decode("latin-1"))
s.close()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚑ Quick win

μ‹€ν—˜ μž¬ν˜„ νŒŒμΌμ„ ν…ŒμŠ€νŠΈ μˆ˜μ§‘ κ²½λ‘œμ—μ„œ μ œκ±°ν•˜μ„Έμš”.

이 νŒŒμΌλ“€μ€ test_*.py 이름을 μ‚¬μš©ν•˜κ³  λͺ¨λ“ˆ import μ‹œ μ¦‰μ‹œ μ‹€ν–‰λ©λ‹ˆλ‹€. ν…ŒμŠ€νŠΈ μˆ˜μ§‘ 쀑에 μ˜ˆμ™Έ, λ„€νŠΈμ›Œν¬ λŒ€κΈ°, 포트 좩돌, Uvicorn λ°±κ·ΈλΌμš΄λ“œ μ„œλ²„κ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. 검증은 이미 tests/test_saas_web.py의 νšŒκ·€ ν…ŒμŠ€νŠΈλ‘œ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

  • test_hmac_direct2.py#L1-L44: Uvicorn μ„œλ²„μ™€ raw socket μž¬ν˜„ μ½”λ“œλ₯Ό μ‚­μ œν•˜μ„Έμš”.
  • test_encode.py#L1-L10: import μ‹œ μ‹€ν–‰λ˜λŠ” 비ꡐ μ½”λ“œλ₯Ό μ‚­μ œν•˜μ„Έμš”.
  • test_hmac_encode.py#L1-L6: import μ‹œ μ‹€ν–‰λ˜λŠ” 비ꡐ μ½”λ“œλ₯Ό μ‚­μ œν•˜μ„Έμš”.
  • test_hmac_exception.py#L1-L7: import μ‹œ μ‹€ν–‰λ˜λŠ” μ·¨μ•½ 비ꡐ μ½”λ“œλ₯Ό μ‚­μ œν•˜μ„Έμš”.
  • test_hmac_header.py#L1-L27: import μ‹œ μ‹€ν–‰λ˜λŠ” FastAPI μž¬ν˜„ μ½”λ“œλ₯Ό μ‚­μ œν•˜μ„Έμš”.
  • test_hmac_latin1.py#L1-L27: import μ‹œ μ‹€ν–‰λ˜λŠ” FastAPI μž¬ν˜„ μ½”λ“œλ₯Ό μ‚­μ œν•˜μ„Έμš”.
  • test_hmac_direct.py#L1-L33: import μ‹œ μ‹€ν–‰λ˜λŠ” localhost μš”μ²­ μ½”λ“œλ₯Ό μ‚­μ œν•˜μ„Έμš”.
  • test_direct.py#L1-L11: import μ‹œ μ‹€ν–‰λ˜λŠ” μš”μ²­ μ½”λ“œλ₯Ό μ‚­μ œν•˜μ„Έμš”.
🧰 Tools
πŸͺ› Ruff (0.16.1)

[warning] 21-21: Do not catch blind exception: Exception

(BLE001)


[warning] 22-22: Use explicit conversion flag

Replace with conversion flag

(RUF010)

πŸ“ Affects 8 files
  • test_hmac_direct2.py#L1-L44 (this comment)
  • test_encode.py#L1-L10
  • test_hmac_encode.py#L1-L6
  • test_hmac_exception.py#L1-L7
  • test_hmac_header.py#L1-L27
  • test_hmac_latin1.py#L1-L27
  • test_hmac_direct.py#L1-L33
  • test_direct.py#L1-L11
πŸ€– 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 `@test_hmac_direct2.py` around lines 1 - 44, Remove the import-time
reproduction scripts from test_hmac_direct2.py (1-44), test_encode.py (1-10),
test_hmac_encode.py (1-6), test_hmac_exception.py (1-7), test_hmac_header.py
(1-27), test_hmac_latin1.py (1-27), test_hmac_direct.py (1-33), and
test_direct.py (1-11); delete each file’s executable comparison, FastAPI/Uvicorn
server, raw socket, or localhost request code so these files cannot run during
test collection, relying on tests/test_saas_web.py for regression coverage.

Copy link
Copy Markdown
Contributor Author

Closing as superseded by the narrower canonical fix in #390 for the same hmac.compare_digest non-ASCII exception cause. #390 preserves the one-line production repair and focused in-suite regression without this branch's eight import-time reproduction scripts, which CodeRabbit correctly identified as test-collection/network/port-conflict hazards. The unique CHANGELOG intent from this branch remains visible here and should be carried into #390 before integration. No check, review, or approval evidence transfers between heads.

@seonghobae seonghobae closed this Aug 11, 2026
@google-labs-jules

Copy link
Copy Markdown

Closing as superseded by the narrower canonical fix in #390 for the same hmac.compare_digest non-ASCII exception cause. #390 preserves the one-line production repair and focused in-suite regression without this branch's eight import-time reproduction scripts, which CodeRabbit correctly identified as test-collection/network/port-conflict hazards. The unique CHANGELOG intent from this branch remains visible here and should be carried into #390 before integration. No check, review, or approval evidence transfers between heads.

Understood. Acknowledging that this work is superseded by #390 and stopping work on this task.

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