7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
130 lines
4.4 KiB
Python
130 lines
4.4 KiB
Python
"""
|
|
Tests for the check-env-vars pre-commit hook.
|
|
|
|
Covers regressions for the substring-based filename exemption:
|
|
- `"test_" in filepath` matched `protest_handler.py` (contains 'test_')
|
|
- `"settings/" in filepath` matched `foo_settings_override.py`
|
|
- Segment / basename / prefix / suffix matching now used instead.
|
|
"""
|
|
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
|
|
HOOK_SCRIPT = (
|
|
Path(__file__).parent.parent.parent
|
|
/ ".pre-commit-hooks"
|
|
/ "check-env-vars.py"
|
|
)
|
|
|
|
|
|
def _run_hook(
|
|
content: str, filename: str = "service.py"
|
|
) -> subprocess.CompletedProcess:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
path = Path(d) / filename
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(content)
|
|
return subprocess.run(
|
|
[sys.executable, str(HOOK_SCRIPT), str(path)],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
|
|
|
|
VIOLATION = 'import os\nx = os.environ.get("LDR_FOO")\n'
|
|
|
|
|
|
class TestSubstringExemptionBugFixed:
|
|
"""Production files whose paths merely contain an allowlist substring
|
|
must no longer be silently exempted."""
|
|
|
|
def test_protest_handler_scanned(self):
|
|
"""'protest_handler.py' contains substring 'test_'."""
|
|
result = _run_hook(VIOLATION, "protest_handler.py")
|
|
assert result.returncode == 1
|
|
assert "LDR_FOO" in result.stdout
|
|
|
|
def test_foo_settings_override_scanned(self):
|
|
"""'foo_settings_override.py' contains substring 'settings/' is
|
|
false, but 'foo_settings.py' would have matched 'settings' as a
|
|
bare string if we weren't segment-anchored. Use a path where
|
|
'settings' is part of the basename, not a directory segment."""
|
|
result = _run_hook(VIOLATION, "foo_settings.py")
|
|
assert result.returncode == 1
|
|
|
|
def test_scriptlike_basename_scanned(self):
|
|
"""A basename containing 'scripts' is not a scripts/ directory."""
|
|
result = _run_hook(VIOLATION, "typescripts_loader.py")
|
|
assert result.returncode == 1
|
|
|
|
|
|
class TestRealExemptionsStillApply:
|
|
"""All the genuine exemption cases must still work."""
|
|
|
|
def test_settings_directory_exempt(self):
|
|
result = _run_hook(VIOLATION, "src/settings/foo.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_tests_directory_exempt(self):
|
|
result = _run_hook(VIOLATION, "tests/helpers.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_test_prefix_exempt(self):
|
|
result = _run_hook(VIOLATION, "test_foo.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_underscore_test_suffix_exempt(self):
|
|
"""Go-style *_test.py — previously this was matched as a bare
|
|
substring which happened to work, but now is handled explicitly."""
|
|
result = _run_hook(VIOLATION, "foo_test.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_migrations_directory_exempt(self):
|
|
result = _run_hook(VIOLATION, "migrations/0001_init.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_scripts_directory_exempt(self):
|
|
result = _run_hook(VIOLATION, "scripts/bootstrap.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_examples_directory_exempt(self):
|
|
result = _run_hook(VIOLATION, "examples/demo.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_log_utils_exempt(self):
|
|
result = _run_hook(VIOLATION, "log_utils.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_sqlcipher_utils_exempt(self):
|
|
result = _run_hook(VIOLATION, "sqlcipher_utils.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_server_config_exempt(self):
|
|
result = _run_hook(VIOLATION, "server_config.py")
|
|
assert result.returncode == 0
|
|
|
|
def test_security_rate_limiter_exempt(self):
|
|
"""Path-anchored entry: 'security/rate_limiter.py'."""
|
|
result = _run_hook(VIOLATION, "security/rate_limiter.py")
|
|
assert result.returncode == 0
|
|
|
|
|
|
class TestBareRateLimiterStillFlagged:
|
|
"""A file literally named rate_limiter.py at a different path must
|
|
NOT be exempt (only the security/ one is)."""
|
|
|
|
def test_rate_limiter_outside_security_flagged(self):
|
|
result = _run_hook(VIOLATION, "other/rate_limiter.py")
|
|
assert result.returncode == 1
|
|
|
|
|
|
class TestRealViolation:
|
|
"""Sanity check — normal production files are still flagged."""
|
|
|
|
def test_production_module_flagged(self):
|
|
result = _run_hook(VIOLATION, "service.py")
|
|
assert result.returncode == 1
|