Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@
## 2026-08-29 - [대용량 텍스트 스캔 시 정규표현식 대신 네이티브 메서드 활용]
**Learning:** `scripts/ci/opencode_review_normalize_output.py`의 라벨 스캐닝 루프에서 긴 LLM 리뷰 텍스트를 대상으로 `pattern.finditer()`를 호출하는 패턴이 있었습니다. 마이크로 벤치마크 결과, 단순 문자열 매칭에서는 네이티브 `str.find()`와 `while` 루프를 조합하는 것이 정규표현식 실행 오버헤드 없이 훨씬 빠르다는 것을 확인했습니다.
**Action:** 내부 탐색 루프에서 정확히 일치하는 리터럴 문자열(라벨 접두사 등)을 검색할 때는 `re.compile(re.escape(string)).finditer()` 대신 고도로 최적화된 Python 네이티브 `text.find(candidate, index)` 메서드를 사용하십시오. 단, 무한 루프를 방지하기 위해 루프의 모든 분기에서 인덱스가 올바르게 진행되도록 보장해야 합니다.
## 2024-05-24 - Secret Scrubbing Regex Optimization
**Learning:** Combining mutually exclusive `re.compile` patterns that are run sequentially on the same string via iteration into a single `|` alternated pattern using a dynamic replacement function based on `match.lastindex` reduces scanning overhead from O(M*N) to O(N).
**Action:** When seeing sequential `re.sub` calls over a fixed tuple of patterns, consolidate them into a single compiled pattern and a callback replacement function to dramatically improve string processing speed.
31 changes: 19 additions & 12 deletions scripts/ci/noema_review_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,31 @@

# ⚡ Bolt: Pre-compiled regex patterns to avoid recompilation on every scrub_sensitive_data call.
# Impact: Improves string processing performance in error reporting.
SENSITIVE_DATA_SCRUB_PATTERNS = (
(re.compile(r'(?i)(bearer\s+)[^\s"\'\\]+'), r'\1***'),
(re.compile(r'(?i)(token\s+)[^\s"\'\\]+'), r'\1***'),
(re.compile(r'(?i)\b(?:github_pat_[A-Za-z0-9_]+|gh[pousr]_[A-Za-z0-9_]+)\b'), '***'),
(re.compile(r'\b(sk-[A-Za-z0-9_-]+)'), '***'),
(re.compile(r'\b(xox[baprs]-[A-Za-z0-9-]+)'), '***'),
(re.compile(r'\b(AKIA[0-9A-Z]{16})'), '***'),
(re.compile(r'(?i)((?:api[_-]?key|access[_-]?token|refresh[_-]?token|id[_-]?token|client[_-]?secret|password|passwd|secret)\s*[:=]\s*)["\']?[^"\'\s]+["\']?'), r'\1***'),
(re.compile(r'(?i)((?:authorization|proxy-authorization)\s*:\s*(?:bearer|basic)\s+)[A-Za-z0-9._~+\/=-]+'), r'\1***'),
_SENSITIVE_DATA_SCRUB_RE = re.compile(
r'(?i)'
r'(bearer\s+)[^\s"\'\\]+|'
r'(token\s+)[^\s"\'\\]+|'
Comment thread
seonghobae marked this conversation as resolved.
r'\b(?:github_pat_[A-Za-z0-9_]+|gh[pousr]_[A-Za-z0-9_]+)\b|'
r'\b(sk-[A-Za-z0-9_-]+)|'
r'\b(xox[baprs]-[A-Za-z0-9-]+)|'
r'\b(AKIA[0-9A-Z]{16})|'
r'((?:api[_-]?key|access[_-]?token|refresh[_-]?token|id[_-]?token|client[_-]?secret|password|passwd|secret)\s*[:=]\s*)'
r'(?:"[^"\r\n]*"|\'[^\'\r\n]*\'|[^\r\n,;}\]]+)|'
Comment thread
seonghobae marked this conversation as resolved.
r'((?:authorization|proxy-authorization)\s*:\s*(?:bearer|basic)\s+)[A-Za-z0-9._~+\/=-]+'
)

def _sensitive_data_repl(match: re.Match[str]) -> str:
"""Return the masked string based on the matching group."""
idx = match.lastindex
if idx in (1, 2, 6, 7):
return match.group(idx) + "***"
return "***"
Comment thread
seonghobae marked this conversation as resolved.

def scrub_sensitive_data(text: str | None) -> str | None:
"""Mask sensitive tokens in text to prevent secret leakage."""
if not text:
return text
for pattern, repl in SENSITIVE_DATA_SCRUB_PATTERNS:
text = pattern.sub(repl, text)
return text
return _SENSITIVE_DATA_SCRUB_RE.sub(_sensitive_data_repl, text)


def run(args: Sequence[str], *, stdin: str | None = None) -> str:
Expand Down
37 changes: 19 additions & 18 deletions scripts/ci/pr_review_merge_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,32 @@ class Decision:
"""


SENSITIVE_DATA_SCRUB_PATTERNS = (
(re.compile(r'(?i)(bearer\s+)[^\s"\'\\]+'), r'\1***'),
(re.compile(r'(?i)(token\s+)[^\s"\'\\]+'), r'\1***'),
(re.compile(r'(?i)\b(?:github_pat_[A-Za-z0-9_]+|gh[pousr]_[A-Za-z0-9_]+)\b'), '***'),
(re.compile(r'\b(sk-[A-Za-z0-9_-]+)'), '***'),
(re.compile(r'\b(xox[baprs]-[A-Za-z0-9-]+)'), '***'),
(re.compile(r'\b(AKIA[0-9A-Z]{16})'), '***'),
(
re.compile(
r'(?i)((?:api[_-]?key|access[_-]?token|refresh[_-]?token|id[_-]?token|client[_-]?secret|password|passwd|secret)\s*[:=]\s*)'
r'(?:"[^"\r\n]*"|\'[^\'\r\n]*\'|[^\r\n,;}\]]+)'
),
r'\1***',
),
(re.compile(r'(?i)((?:authorization|proxy-authorization)\s*:\s*(?:bearer|basic)\s+)[A-Za-z0-9._~+\/=-]+'), r'\1***'),
_SENSITIVE_DATA_SCRUB_RE = re.compile(
r'(?i)'
r'(bearer\s+)[^\s"\'\\]+|'
r'(token\s+)[^\s"\'\\]+|'
r'\b(?:github_pat_[A-Za-z0-9_]+|gh[pousr]_[A-Za-z0-9_]+)\b|'
r'\b(sk-[A-Za-z0-9_-]+)|'
r'\b(xox[baprs]-[A-Za-z0-9-]+)|'
r'\b(AKIA[0-9A-Z]{16})|'
r'((?:api[_-]?key|access[_-]?token|refresh[_-]?token|id[_-]?token|client[_-]?secret|password|passwd|secret)\s*[:=]\s*)'
r'(?:"[^"\r\n]*"|\'[^\'\r\n]*\'|[^\r\n,;}\]]+)|'
r'((?:authorization|proxy-authorization)\s*:\s*(?:bearer|basic)\s+)[A-Za-z0-9._~+\/=-]+'
)

def _sensitive_data_repl(match: re.Match[str]) -> str:
"""Return the masked string based on the matching group."""
idx = match.lastindex
if idx in (1, 2, 6, 7):
return match.group(idx) + "***"
return "***"


def scrub_sensitive_data(text: str | None) -> str | None:
"""Mask sensitive tokens in text to prevent secret leakage."""
if not text:
return text
for pattern, repl in SENSITIVE_DATA_SCRUB_PATTERNS:
text = pattern.sub(repl, text)
return text
return _SENSITIVE_DATA_SCRUB_RE.sub(_sensitive_data_repl, text)


def mutation_token_source() -> str:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_noema_review_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def test_scrub_sensitive_data_authorization_headers():
assert noema.scrub_sensitive_data("authorization: bearer xyz") == "authorization: bearer ***"


def test_scrub_sensitive_data_masks_nested_credential_schemes():
"""Assigned credentials must not leak suffixes after Bearer/token prefixes."""
assert noema.scrub_sensitive_data("api_key=Bearer secret-value; keep this") == "api_key=***; keep this"
assert noema.scrub_sensitive_data("client_secret=token secret-value") == "client_secret=***"


def test_split_repo_and_graphql(monkeypatch):
with pytest.raises(ValueError):
noema.split_repo("owner")
Expand Down
2 changes: 2 additions & 0 deletions tests/test_pr_review_merge_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7933,6 +7933,8 @@ def test_scrub_sensitive_data_and_run_error():
assert sched.scrub_sensitive_data("password: my secret; keep this") == "password: ***; keep this"
assert sched.scrub_sensitive_data("api_key='my secret value'") == "api_key=***"
assert sched.scrub_sensitive_data("api_key : 'mysecret'") == "api_key : ***"
assert sched.scrub_sensitive_data("api_key=Bearer secret-value; keep this") == "api_key=***; keep this"
assert sched.scrub_sensitive_data("client_secret=token secret-value") == "client_secret=***"
assert sched.scrub_sensitive_data("No secrets here") == "No secrets here"
assert sched.scrub_sensitive_data("") == ""
assert sched.scrub_sensitive_data(None) is None
Expand Down
Loading