Skip to content
Merged
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
24 changes: 18 additions & 6 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,15 @@ def rules(text: str) -> list:
class Validator(unittest.TestCase):
LIMIT = 2 # seconds; a search of 4,000 characters takes well under a millisecond unless it backtracks

def backtracks(self, pattern: str) -> bool:
"""Whether `pattern` runs past the limit on a long value of the shapes that make a pattern backtrack, each after
the literal text the pattern opens with, so that it reaches a repeat behind that text. It does not reach a repeat
behind a class: a guard on patterns the repo writes, not a proof."""
def backtracks(self, pattern, opens: str = None) -> bool:
"""Whether `pattern`, a text or a compiled pattern with its flags, runs past the limit on a long value of the
shapes that make a pattern backtrack, each after the literal text the pattern opens with, or `opens` where that
text sits behind a class, so that it reaches a repeat behind that text: a guard on patterns the repo writes, not
a proof."""
compiled = re.compile(pattern)
opens = re.match(r"\^?((?:\\[^A-Za-z0-9]|[^\\\[\](){}.*+?|^$])*)", pattern).group(1)
opens = re.sub(r"\\(.)", r"\1", opens)
if opens is None:
opens = re.match(r"\^?((?:\\[^A-Za-z0-9]|[^\\\[\](){}.*+?|^$])*)", compiled.pattern).group(1)
opens = re.sub(r"\\(.)", r"\1", opens)
def over(_signum, _frame):
raise TimeoutError
before = signal.signal(signal.SIGALRM, over)
Expand Down Expand Up @@ -10355,6 +10357,16 @@ def setUp(self):
for name in self.WANTED]}
self.policies = {name: (200, {"total_count": 1, "branch_policies": [{"name": "main", "type": "branch"}]}) for name in self.WANTED}

def test_the_pattern_that_reads_a_workflows_environment_does_not_backtrack(self): # issue 361's audit, S8786
# SonarCloud reads the blanks before the value as overlapping it. An attempt starts at a line's start alone, and
# once past `environment:` it ends in a repeat that matches to the line's end: this holds that, on the value's
# line and on the mapping's `name:` line, with the check that holds the schemas' patterns. That it notices one
# is the two mutants' to show, since a pattern that backtracks written here is CodeQL's alert.
guard = Validator()
for opens in ("environment:", " environment: \t", "environment:\n", " environment:\n name: \t"):
with self.subTest(opens=opens):
self.assertFalse(guard.backtracks(environments_drift.WORKFLOW, opens))

def compare(self, wanted=None) -> list:
return environments_drift.compare(self.listed, self.policies, set(self.WANTED if wanted is None else wanted))

Expand Down
7 changes: 5 additions & 2 deletions tools/environments_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
ROOT = pathlib.Path(__file__).resolve().parent.parent
PAGE = 100 # the script reads one page of this many; a full page may not be all
PLAIN = re.compile(r"[A-Za-z0-9_-]+")
# A job's environment: its value, or, in the mapping's form, the `name:` on the next line.
WORKFLOW = re.compile(r"^[ \t]*environment:[ \t]*(?:#[^\n]*)?(?:\n[ \t]*name:[ \t]*)?([^\n]*)$", re.M)
# A job's environment: its value, or, in the mapping's form, the `name:` on the next line. Linear, though SonarCloud reads
# the blanks before the value as overlapping it: an attempt starts only at a line's start, and once past `environment:`
# it ends in `([^\n]*)$`, which matches to the line's end whatever came before, so nothing after the colon fails and
# backtracks. Its input is this repo's own workflow files, read at the Maintainer's terminal.
WORKFLOW = re.compile(r"^[ \t]*environment:[ \t]*(?:#[^\n]*)?(?:\n[ \t]*name:[ \t]*)?([^\n]*)$", re.M) # NOSONAR
MAIN_ALONE = [("main", "branch")]
OWN_LIST = {"custom_branch_policies": True, "protected_branches": False}

Expand Down
3 changes: 3 additions & 0 deletions tools/mutants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7203,6 +7203,9 @@ def moved_before(rel: str, block: str, anchor: str) -> tuple:
'told(call), clock)', 'call, clock)', ("tests.test_evals.EvalJob.test_the_canary_is_red_unless_the_cli_answered",)),
('the canary says all the CLI said', 'harness/eval/canary.py',
'said[-TAIL:]', 'said', ("tests.test_evals.EvalJob.test_the_canary_is_red_unless_the_cli_answered",)),
# SonarCloud's S8786 on a workflow's environment: the pattern stays linear.
("the workflow's environment pattern backtracks on its name line", 'tools/environments_drift.py', '(?:\\n[ \\t]*name:[ \\t]*)?', '(?:\\n(?:[ \\t]*)*name:[ \\t]*)?', ("tests.test_tools.EnvironmentsDrift.test_the_pattern_that_reads_a_workflows_environment_does_not_backtrack",)),
("the workflow's environment pattern backtracks", 'tools/environments_drift.py', '(?:\\n[ \\t]*name:[ \\t]*)?([^\\n]*)$", re.M)', '(?:\\n[ \\t]*name:[ \\t]*)?([ \\t]*[^\\n]*)*!$", re.M)', ("tests.test_tools.EnvironmentsDrift.test_the_pattern_that_reads_a_workflows_environment_does_not_backtrack",)),
# Issue 440: the fake GitHub does what P-36 recorded.
("the fake's upload changes nothing", 'sim/world.py', ' self.checks[SCANNING] = "neutral"\n', '', ("tests.test_simulator.Simulation.test_the_seeds_upload_under_codeqls_tool",)),
("a job stays unstarted past its check", 'sim/world.py', ' self.not_started.discard(lens)\n', '', ("tests.test_simulator.Simulation.test_the_seeds_create_a_check_under_a_lens_name",)),
Expand Down
Loading