Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(e2e): r6-m2 reject reset navigation path escapes

Josué26f25b16a17d
diff --git a/tools/e2e_static_audit.py b/tools/e2e_static_audit.py
index ae0ff22a2..12d567edf 100755
--- a/tools/e2e_static_audit.py
+++ b/tools/e2e_static_audit.py
@@ -22,6 +22,9 @@ RULES: tuple[tuple[str, re.Pattern[str]], ...] = (
("core-route success mock", re.compile(r"(?:route|fulfill)\s*\([^\n]*(?:uploads|gfs|complete|parts)[^\n]*(?:status\s*:\s*2|status\s*:\s*20)")),
)
+EMAIL_RESET_ENTRY_PATH = re.compile(r"/invitations/[A-Za-z0-9_-]+")
+STATIC_GOTO_ARGUMENT = re.compile(r"\s*(['\"])([^'\"\\\r\n]*)\1\s*(?=[,)])")
+
def audit_file(path: Path) -> list[str]:
text = path.read_text(encoding="utf-8")
@@ -56,10 +59,17 @@ def audit_text(text: str, path: Path = Path("<source>")) -> list[str]:
# Navigation exceptions must be attached to the specific call. Searching a
# broad look-behind for "guard" also matches "GUARDIAN" and lets one marker
# bless later calls.
- for match in re.finditer(r"\bpage\.goto\s*\(", text):
+ navigation_matches = list(re.finditer(r"\bpage\.goto\s*\(", text))
+ navigation_counts_by_line: dict[int, int] = {}
+ for match in navigation_matches:
+ line = text.count("\n", 0, match.start()) + 1
+ navigation_counts_by_line[line] = navigation_counts_by_line.get(line, 0) + 1
+
+ for match in navigation_matches:
adjacent = _adjacent_line(text, match.start())
after = text[match.end() : match.end() + 160]
- if re.match(r"\s*['\"]/?['\"]", after):
+ root_argument = STATIC_GOTO_ARGUMENT.match(after)
+ if root_argument and root_argument.group(2) in {"", "/"}:
continue
if re.fullmatch(
r"\s*//\s*Negative (?:protected-route guard|route-guard|deep-link guard|"
@@ -68,20 +78,22 @@ def audit_text(text: str, path: Path = Path("<source>")) -> list[str]:
re.IGNORECASE,
):
continue
- if (
- re.fullmatch(
- r"\s*//\s*E2E_GUARDIAN_EMAIL_RESET_ENTRY_POINT(?::[^\n]*)?",
- adjacent,
- )
- and re.match(r"\s*['\"]\/invitations\/[^'\"]+['\"]", after)
- ):
+ root_entry_marker = re.fullmatch(
+ r"\s*//\s*E2E_GUARDIAN_ENTRY_POINT(?::[^\n]*)?", adjacent
+ )
+ if root_entry_marker and root_argument and root_argument.group(2) in {"", "/"}:
continue
+ email_reset_marker = re.fullmatch(
+ r"\s*//\s*E2E_GUARDIAN_EMAIL_RESET_ENTRY_POINT(?::[^\n]*)?", adjacent
+ )
+ line = text.count("\n", 0, match.start()) + 1
if (
- re.fullmatch(r"\s*//\s*E2E_GUARDIAN_ENTRY_POINT(?::[^\n]*)?", adjacent)
- and re.match(r"\s*['\"]/?['\"]", after)
+ email_reset_marker
+ and navigation_counts_by_line.get(line) == 1
+ and root_argument
+ and EMAIL_RESET_ENTRY_PATH.fullmatch(root_argument.group(2))
):
continue
- line = text.count("\n", 0, match.start()) + 1
findings.append(f"{path}:{line}: direct page.goto outside an explicit negative guard")
return findings
diff --git a/tools/test_e2e_static_audit_scope.py b/tools/test_e2e_static_audit_scope.py
index 7d292aaa8..0eb1d5abc 100644
--- a/tools/test_e2e_static_audit_scope.py
+++ b/tools/test_e2e_static_audit_scope.py
@@ -1,18 +1,169 @@
from __future__ import annotations
+import json
+import subprocess
import unittest
from pathlib import Path
from tools.e2e_static_audit import audit_text
+def normalized_browser_url(url: str) -> tuple[str, str]:
+ script = (
+ "const url = new URL(process.argv[1], 'http://127.0.0.1:31873');"
+ "process.stdout.write(JSON.stringify([url.origin, url.pathname]));"
+ )
+ result = subprocess.run(
+ ["node", "-e", script, url],
+ check=True,
+ capture_output=True,
+ text=True,
+ )
+ origin, pathname = json.loads(result.stdout)
+ return origin, pathname
+
+
+def normalized_browser_concatenation(left: str, right: str) -> tuple[str, str]:
+ script = (
+ "const destination = process.argv[1] + process.argv[2];"
+ "const url = new URL(destination, 'http://127.0.0.1:31873');"
+ "process.stdout.write(JSON.stringify([url.origin, url.pathname]));"
+ )
+ result = subprocess.run(
+ ["node", "-e", script, left, right],
+ check=True,
+ capture_output=True,
+ text=True,
+ )
+ origin, pathname = json.loads(result.stdout)
+ return origin, pathname
+
+
class E2EStaticAuditMarkerScopeTests(unittest.TestCase):
+ def audit_email_reset_entry(self, argument: str) -> list[str]:
+ source = (
+ "const response = page.waitForResponse(() => true)\n"
+ "// E2E_GUARDIAN_EMAIL_RESET_ENTRY_POINT: synthetic emailed reset entry\n"
+ f"page.goto({argument})"
+ )
+ return audit_text(source, Path("email-reset.spec.ts"))
+
+ def test_static_same_origin_invitation_token_is_allowed(self) -> None:
+ argument = "'/invitations/synthetic-reset-proof'"
+ self.assertEqual(
+ normalized_browser_url("/invitations/synthetic-reset-proof"),
+ ("http://127.0.0.1:31873", "/invitations/synthetic-reset-proof"),
+ )
+ self.assertFalse(
+ any("direct page.goto" in item for item in self.audit_email_reset_entry(argument))
+ )
+
+ def test_browser_normalization_rejects_literal_and_encoded_traversal(self) -> None:
+ cases = [
+ ("'/invitations/../unexpected-terminal'", "/invitations/../unexpected-terminal"),
+ (
+ "'/invitations/%2e%2e/unexpected-terminal'",
+ "/invitations/%2e%2e/unexpected-terminal",
+ ),
+ ]
+ for argument, browser_url in cases:
+ with self.subTest(argument=argument):
+ _, normalized_path = normalized_browser_url(browser_url)
+ self.assertEqual(normalized_path, "/unexpected-terminal")
+ self.assertTrue(
+ any(
+ "direct page.goto" in item
+ for item in self.audit_email_reset_entry(argument)
+ )
+ )
+
+ def test_dynamic_concatenation_that_normalizes_outside_route_is_rejected(self) -> None:
+ _, normalized_path = normalized_browser_concatenation(
+ "/invitations/reset-token", "/../../unexpected-terminal"
+ )
+ self.assertEqual(normalized_path, "/unexpected-terminal")
+ findings = self.audit_email_reset_entry(
+ "'/invitations/reset-token' + '/../../unexpected-terminal'"
+ )
+ self.assertTrue(any("direct page.goto" in item for item in findings))
+
+ def test_template_expression_is_rejected(self) -> None:
+ self.assertTrue(
+ any(
+ "direct page.goto" in item
+ for item in self.audit_email_reset_entry("`/invitations/${token}`")
+ )
+ )
+
+ def test_external_origin_is_rejected_even_with_invitation_path(self) -> None:
+ origin, pathname = normalized_browser_url("https://outside.invalid/invitations/token")
+ self.assertEqual((origin, pathname), ("https://outside.invalid", "/invitations/token"))
+ self.assertTrue(
+ any(
+ "direct page.goto" in item
+ for item in self.audit_email_reset_entry(
+ "'https://outside.invalid/invitations/token'"
+ )
+ )
+ )
+
+ def test_additional_path_query_fragment_and_encoded_path_are_rejected(self) -> None:
+ arguments = [
+ "'/admin/secret'",
+ "'/invitations/token/extra'",
+ "'/invitations/token?next=/home'",
+ "'/invitations/token#fragment'",
+ "'/invitations/%74oken'",
+ "'/invitations\\\\token'",
+ ]
+ for argument in arguments:
+ with self.subTest(argument=argument):
+ self.assertTrue(
+ any(
+ "direct page.goto" in item
+ for item in self.audit_email_reset_entry(argument)
+ )
+ )
+
+ def test_marker_does_not_allow_unrelated_guardian_text(self) -> None:
+ source = (
+ "const response = page.waitForResponse(() => true)\n"
+ "// GUARDIAN email reset entry point\n"
+ "page.goto('/admin/secret')"
+ )
+ self.assertTrue(
+ any("direct page.goto" in item for item in audit_text(source, Path("scope.spec.ts")))
+ )
+
def test_plain_application_root_entry_remains_allowed(self) -> None:
source = "const response = page.waitForResponse(() => true)\npage.goto('/')"
self.assertFalse(
any("direct page.goto" in finding for finding in audit_text(source, Path("root.spec.ts")))
)
+ def test_explicit_root_marker_remains_scoped_to_static_root(self) -> None:
+ source = (
+ "const response = page.waitForResponse(() => true)\n"
+ "// E2E_GUARDIAN_ENTRY_POINT\n"
+ "page.goto('/')"
+ )
+ self.assertFalse(
+ any("direct page.goto" in item for item in audit_text(source, Path("root.spec.ts")))
+ )
+
+ def test_explicit_negative_route_guard_remains_allowed(self) -> None:
+ source = (
+ "const response = page.waitForResponse(() => true)\n"
+ "// Negative route-guard: verifies authorization rejection.\n"
+ "page.goto('/admin/secret')"
+ )
+ self.assertFalse(
+ any(
+ "direct page.goto" in item
+ for item in audit_text(source, Path("negative.spec.ts"))
+ )
+ )
+
def test_marker_must_be_adjacent_to_its_navigation(self) -> None:
source = "// E2E_GUARDIAN_EMAIL_RESET_ENTRY_POINT\n\npage.goto('/invitations/reset-token')"
self.assertTrue(
@@ -30,6 +181,17 @@ class E2EStaticAuditMarkerScopeTests(unittest.TestCase):
self.assertEqual(len(navigation_findings), 1)
self.assertTrue(navigation_findings[0].endswith(":3: direct page.goto outside an explicit negative guard"))
+ def test_one_marker_does_not_allow_two_calls_on_the_same_line(self) -> None:
+ source = (
+ "const response = page.waitForResponse(() => true)\n"
+ "// E2E_GUARDIAN_EMAIL_RESET_ENTRY_POINT\n"
+ "page.goto('/invitations/reset-token'); page.goto('/admin/secret')"
+ )
+ navigation_findings = [
+ item for item in audit_text(source, Path("scope.spec.ts")) if "direct page.goto" in item
+ ]
+ self.assertEqual(len(navigation_findings), 2)
+
def test_removing_the_real_spec_marker_is_detected(self) -> None:
spec = Path(
"tests/e2e/playwright/profile-ui-recovery/reset-session-hydration.spec.ts"