Normalize runtime secret transport whitespace
This commit is contained in:
parent
5e08b8129c
commit
0ef2ae515e
2 changed files with 27 additions and 1 deletions
|
|
@ -69,7 +69,13 @@ def main() -> None:
|
||||||
|
|
||||||
|
|
||||||
def _required(name: str) -> str:
|
def _required(name: str) -> str:
|
||||||
value = os.environ.get(name)
|
# Values sourced from files or `kubectl create secret --from-file` commonly
|
||||||
|
# retain one trailing newline. Such a value is unusable in HTTP headers
|
||||||
|
# (notably USER_ENGINE_PROXY_SECRET and provisioning bearer tokens), and
|
||||||
|
# comparing it byte-for-byte makes the trusted boundary impossible to
|
||||||
|
# exercise. Normalize transport whitespace at the runtime boundary; the
|
||||||
|
# domain and adapters still receive an opaque non-empty value.
|
||||||
|
value = os.environ.get(name, "").strip()
|
||||||
if not value:
|
if not value:
|
||||||
raise RuntimeError(f"{name} is required")
|
raise RuntimeError(f"{name} is required")
|
||||||
return value
|
return value
|
||||||
|
|
|
||||||
20
tests/test_runtime_config.py
Normal file
20
tests/test_runtime_config.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from user_engine.runtime import _required
|
||||||
|
|
||||||
|
|
||||||
|
class RequiredRuntimeValueTests(unittest.TestCase):
|
||||||
|
def test_normalizes_secret_file_newline(self):
|
||||||
|
with patch.dict(os.environ, {"USER_ENGINE_TEST_SECRET": "secret-value\n"}):
|
||||||
|
self.assertEqual(_required("USER_ENGINE_TEST_SECRET"), "secret-value")
|
||||||
|
|
||||||
|
def test_rejects_whitespace_only_value(self):
|
||||||
|
with patch.dict(os.environ, {"USER_ENGINE_TEST_SECRET": " \n\t"}):
|
||||||
|
with self.assertRaisesRegex(RuntimeError, "USER_ENGINE_TEST_SECRET is required"):
|
||||||
|
_required("USER_ENGINE_TEST_SECRET")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue