Skip to content

Commit 3703b05

Browse files
authored
Merge pull request #145 from PhysShell/claude/ownts-parser-hardening
fix(ownts): literal-proof useEffect-парсер + строгий матчинг release для addEventListener
2 parents 11db18a + 943c3fb commit 3703b05

4 files changed

Lines changed: 273 additions & 50 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,18 @@ jobs:
10761076
|| { echo "FAIL: expected exactly one OWN001 (the uncleared timer)"; exit 1; }
10771077
echo "$edges" | grep -q "pollB" \
10781078
|| { echo "FAIL: the leak must be the second (uncleared) interval"; exit 1; }
1079+
- name: Parser hardening — string literals, nested dep brackets, listener options
1080+
run: |
1081+
python frontend/ownts/ownts.py frontend/ownts/examples/EffectHardening.tsx \
1082+
-o "$RUNNER_TEMP/hard.facts.json"
1083+
hard=$(python -m ownlang ownir "$RUNNER_TEMP/hard.facts.json" || true)
1084+
echo "$hard"
1085+
# a string with commas/braces does not truncate the body (the timer is cleared);
1086+
# the leak is the options-dropped listener; the object dep fires EFF001 once
1087+
[ "$(echo "$hard" | grep -cE '\[(OWN|EFF|DI)[0-9]{3}\]')" -eq 2 ] \
1088+
|| { echo "FAIL: expected one OWN001 (listener) + one EFF001 (object dep)"; exit 1; }
1089+
echo "$hard" | grep -q "scroll" \
1090+
|| { echo "FAIL: the leak must be the options-dropped scroll listener"; exit 1; }
10791091
- name: The clean fixture (cleanups + useMemo'd dep) is silent
10801092
run: |
10811093
python frontend/ownts/ownts.py frontend/ownts/examples/DashboardClean.tsx \
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Parser-hardening cases (CodeRabbit): string literals must not truncate bodies or
2+
// split deps, deps brackets nest, and a listener is released only by a matching
3+
// target+handler+options removeEventListener. Run:
4+
// python frontend/ownts/ownts.py frontend/ownts/examples/EffectHardening.tsx --check
5+
// Expect exactly: OWN001 (the options-dropped listener) + EFF001 (the object dep).
6+
import { useEffect } from "react";
7+
8+
export function Hardening({ id }: { id: string }) {
9+
// A string with commas and braces must not truncate the body or split the deps;
10+
// the timer IS cleared, so this effect leaks nothing.
11+
useEffect(() => {
12+
const t = setInterval(() => fetch("/a,b,{c}"), 1000);
13+
return () => clearInterval(t);
14+
}, [id]);
15+
16+
// Listener added WITH capture, but cleanup drops the option -> different listener,
17+
// still leaks (one OWN001).
18+
useEffect(() => {
19+
window.addEventListener("scroll", onScroll, true);
20+
return () => window.removeEventListener("scroll", onScroll);
21+
}, []);
22+
23+
// Nested-bracket dep parses (`items[0]` stays one entry); the object literal dep
24+
// is unstable + IO -> exactly one EFF001 (items[0] is not identifier-like: silent).
25+
const filters = { id };
26+
useEffect(() => {
27+
fetch("/x");
28+
}, [filters, items[0]]);
29+
30+
return <div>hardening</div>;
31+
}

‎frontend/ownts/ownts.py‎

Lines changed: 190 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,45 @@ def _lhs_token(setup: str, pos: int) -> str | None:
7575
return m.group(1) if m else None
7676

7777

78+
def _capture_flag(opts: str) -> str:
79+
"""The capture flag an addEventListener/removeEventListener options arg implies —
80+
the only field that identifies a listener for removal. `'true'`/`'false'`, or
81+
`'unknown'` for a non-literal we cannot read. An omitted arg, or an options
82+
object without a `capture` key, defaults to capture **false** (the DOM default);
83+
`passive`/`once`/`signal` do not affect removal identity."""
84+
o = opts.strip()
85+
if o in ("", "false"):
86+
return "false"
87+
if o == "true":
88+
return "true"
89+
if o.startswith("{"):
90+
m = re.search(r"\bcapture\s*:\s*(true|false)\b", o)
91+
return m.group(1) if m else "false"
92+
return "unknown" # a variable/call — compare verbatim (equal only to itself)
93+
94+
95+
_LISTENER = re.compile(
96+
r"\.\s*(?:add|remove)EventListener\s*\(\s*([^,]+?)\s*,\s*([A-Za-z_$][\w$.]*)\s*"
97+
r"(?:,\s*([^)]+?))?\s*\)")
98+
99+
100+
def _listener_call(s: str) -> tuple[str, str, str] | None:
101+
"""Parse a `.addEventListener`/`.removeEventListener` head into the
102+
(event, handler, capture) triple that identifies the listener for removal, or
103+
None when it doesn't parse. `s` starts at the `.`."""
104+
m = _LISTENER.match(s.lstrip())
105+
if not m:
106+
return None
107+
return (m.group(1).strip(), m.group(2), _capture_flag(m.group(3) or ""))
108+
109+
110+
def _receiver(text: str, end: int) -> str:
111+
"""The receiver expression ending at `end` (just before `.addEventListener`),
112+
allowing a member/index chain like `this.ref` or `nodes[i]`."""
113+
m = re.search(r"([A-Za-z_$][\w$.\[\]]*)$", text[:end])
114+
return m.group(1) if m else ""
115+
116+
78117
def _is_released(acq: Acquire, setup: str, pos: int, cleanup: str) -> bool:
79118
"""Whether THIS acquire (at `pos` in `setup`) is released by the effect's cleanup
80119
— matched to its own handle, so two `setInterval`s with one `clearInterval` leave
@@ -88,12 +127,26 @@ def _is_released(acq: Acquire, setup: str, pos: int, cleanup: str) -> bool:
88127
tok = _lhs_token(setup, pos)
89128
return bool(tok and re.search(
90129
rf"\b{re.escape(tok)}\s*\.\s*(?:unsubscribe|remove)\s*\(", cleanup))
91-
if acq.resource == "subscription": # addEventListener(event, handler)
92-
hm = re.search(r"addEventListener\s*\(\s*[^,]+,\s*([A-Za-z_$][\w$.]*)",
93-
setup[pos:])
94-
handler = hm.group(1) if hm else None
95-
return bool(handler and re.search(
96-
rf"removeEventListener\s*\(\s*[^,]+,\s*{re.escape(handler)}\b", cleanup))
130+
if acq.resource == "subscription": # target.addEventListener(event, handler[, opts])
131+
# The full listener key is (receiver, event, handler, capture). A listener is
132+
# released ONLY by a removeEventListener whose whole key matches — a different
133+
# event name, target, or capture flag is a different listener that still
134+
# leaks. Fail closed: if the acquire's own target can't be identified, or no
135+
# cleanup call matches every field, treat it as a leak.
136+
a = _listener_call(setup[pos:])
137+
if a is None:
138+
return False
139+
a_recv = _receiver(setup, pos)
140+
if not a_recv:
141+
return False
142+
for rm in re.finditer(r"([A-Za-z_$][\w$.\[\]]*)\s*\.\s*removeEventListener\s*\(",
143+
cleanup):
144+
if rm.group(1) != a_recv:
145+
continue
146+
b = _listener_call(cleanup[rm.start() + len(rm.group(1)):])
147+
if b == a:
148+
return True
149+
return False
97150
return False
98151

99152
# A React component is a function whose name is Capitalized (the JSX convention).
@@ -206,6 +259,68 @@ def _match_block(text: str, open_idx: int) -> int:
206259
return len(text)
207260

208261

262+
def _mask_strings(text: str) -> str:
263+
"""Blank the CONTENT of string/template literals — keeping the delimiters, the
264+
length, and every newline — so the structural scanners (brace/paren/bracket
265+
depth, top-level commas, the deps array) are never fooled by punctuation inside
266+
a literal: `fetch("/a,b")`, `const s = "{"`, a `;` inside a string. Comments are
267+
already gone (`_strip_comments`). Positions are preserved, so a match found on
268+
the masked copy slices identically out of the original text."""
269+
out = list(text)
270+
i, n = 0, len(text)
271+
while i < n:
272+
c = text[i]
273+
if c in "\"'`":
274+
i += 1
275+
while i < n and text[i] != c:
276+
if text[i] == "\\" and i + 1 < n:
277+
for k in (i, i + 1):
278+
if text[k] != "\n":
279+
out[k] = " "
280+
i += 2
281+
continue
282+
if text[i] != "\n":
283+
out[i] = " "
284+
i += 1
285+
i += 1 # past the closing delimiter (kept)
286+
else:
287+
i += 1
288+
return "".join(out)
289+
290+
291+
def _match_pair(text: str, i: int, open_c: str, close_c: str) -> int:
292+
"""Index just past the `close_c` matching the `open_c` at/after `i`. Assumes
293+
string contents are already masked (so no quote-skipping is needed here)."""
294+
i = text.index(open_c, i)
295+
depth = 0
296+
while i < len(text):
297+
if text[i] == open_c:
298+
depth += 1
299+
elif text[i] == close_c:
300+
depth -= 1
301+
if depth == 0:
302+
return i + 1
303+
i += 1
304+
return len(text)
305+
306+
307+
def _split_top_commas(s: str) -> list[str]:
308+
"""Split on commas at bracket/paren/brace depth 0, so a dependency like
309+
`items[i]` or `f(a, b)` stays one entry instead of being torn at its inner comma."""
310+
parts: list[str] = []
311+
depth, start = 0, 0
312+
for j, c in enumerate(s):
313+
if c in "([{":
314+
depth += 1
315+
elif c in ")]}":
316+
depth -= 1
317+
elif c == "," and depth == 0:
318+
parts.append(s[start:j])
319+
start = j + 1
320+
parts.append(s[start:])
321+
return [p.strip() for p in parts if p.strip()]
322+
323+
209324
def _component_at(text: str, idx: int) -> str:
210325
"""Name of the React component enclosing position `idx` — the nearest preceding
211326
Capitalized function declaration. Falls back to a synthetic name."""
@@ -233,61 +348,85 @@ def _expr_end(text: str, i: int) -> int:
233348
return i
234349

235350

236-
def _effect_callback(text: str, after_open: int) -> tuple[str, int, list[str] | None, int]:
237-
"""Parse `useEffect(<cb>, [deps])` from just past the `(`. Returns
351+
def _effect_callback(masked: str, after_open: int) -> tuple[str, int, list[str] | None, int]:
352+
"""Parse `useEffect(<cb>, [deps])` from just past the `(`, over the STRING-MASKED
353+
source (so literals never truncate a body or split a dep). Returns
238354
(body, body_start, deps, end). Handles BOTH a block `() => { ... }` and an
239355
expression `() => fetch(url)` callback — calling `_match_block` blindly on the
240356
latter would jump to an unrelated `{` or run off the end. `deps` is None when no
241-
dependency array is present; `body` includes the braces for a block callback."""
242-
arrow = text.find("=>", after_open)
357+
dependency array is present; the array is matched by BALANCED brackets so a dep
358+
like `items[i]` survives. `body` includes the braces for a block callback."""
359+
arrow = masked.find("=>", after_open)
243360
i = (arrow + 2) if arrow != -1 else after_open
244-
while i < len(text) and text[i] in " \t\r\n":
361+
while i < len(masked) and masked[i] in " \t\r\n":
245362
i += 1
246-
if i < len(text) and text[i] == "{":
247-
end = _match_block(text, i)
363+
if i < len(masked) and masked[i] == "{":
364+
end = _match_block(masked, i)
248365
else:
249-
end = _expr_end(text, i)
250-
body = text[i:end]
251-
deps_m = re.search(r"\s*,\s*\[([^\]]*)\]", text[end:end + 200])
252-
deps = ([d.strip() for d in deps_m.group(1).split(",") if d.strip()]
253-
if deps_m else None)
366+
end = _expr_end(masked, i)
367+
body = masked[i:end]
368+
# the dependency array is the balanced `[ ... ]` after an optional `, `
369+
deps: list[str] | None = None
370+
j = end
371+
while j < len(masked) and masked[j] in " \t\r\n":
372+
j += 1
373+
if j < len(masked) and masked[j] == ",":
374+
j += 1
375+
while j < len(masked) and masked[j] in " \t\r\n":
376+
j += 1
377+
if j < len(masked) and masked[j] == "[":
378+
close = _match_pair(masked, j, "[", "]")
379+
deps = _split_top_commas(masked[j + 1:close - 1])
254380
return body, i, deps, end
255381

256382

257-
def _split_cleanup(body: str) -> tuple[str, str]:
258-
"""Split an effect block body into (setup, cleanup). Cleanup is the block of the
259-
effect's OWN top-level `return () => { ... }` — a `return ... =>` nested inside a
260-
callback (brace-depth > 1) is NOT the effect cleanup and must not suppress a
261-
leak. For an expression-bodied effect there is no cleanup."""
262-
for m in re.finditer(r"return\s*(?:\(\s*\)|\w+)\s*=>", body):
263-
prefix = body[:m.start()]
383+
def _cleanup_span(mbody: str) -> tuple[int, int] | None:
384+
"""The (start, end) span of the effect's OWN top-level cleanup within `mbody`
385+
(the masked body), or None. A `return ... =>` nested inside a callback
386+
(brace-depth > 1) is NOT the effect cleanup and must not suppress a leak. The
387+
span is returned (not slices) so the caller can cut it out of BOTH the masked
388+
body (for acquire scanning) and the original body (for event/string comparison)."""
389+
for m in re.finditer(r"return\s*(?:\(\s*\)|\w+)\s*=>", mbody):
390+
prefix = mbody[:m.start()]
264391
if prefix.count("{") - prefix.count("}") != 1: # 1 == the effect body's own brace
265392
continue
266-
brace = body.find("{", m.end())
393+
brace = mbody.find("{", m.end())
267394
if brace == -1:
268395
# `return () => clearInterval(id)` — single-expression cleanup, no block.
269-
nl = body.find("\n", m.end())
270-
tail = body[m.end(): nl if nl != -1 else len(body)]
271-
return body[: m.start()], tail
272-
end = _match_block(body, brace)
273-
return body[: m.start()] + body[end:], body[brace:end]
274-
return body, ""
396+
nl = mbody.find("\n", m.end())
397+
return (m.start(), nl if nl != -1 else len(mbody))
398+
return (m.start(), _match_block(mbody, brace))
399+
return None
275400

276401

277402
def extract(path: str) -> list[Component]:
278403
text = _strip_comments(open(path, encoding="utf-8").read())
404+
masked = _mask_strings(text) # scan structure on this; show snippets from `text`
279405
comps: dict[str, Component] = {}
280-
for eff in _USE_EFFECT.finditer(text):
281-
body, body_start, _deps, _end = _effect_callback(text, eff.end())
282-
setup, cleanup = _split_cleanup(body)
283-
cname = _component_at(text, eff.start())
406+
for eff in _USE_EFFECT.finditer(masked):
407+
body_m, body_start, _deps, end = _effect_callback(masked, eff.end())
408+
body_o = text[body_start:end] # original (unmasked) body — same positions
409+
span = _cleanup_span(body_m)
410+
if span:
411+
cs, ce = span
412+
setup_m = body_m[:cs] + body_m[ce:]
413+
setup_o = body_o[:cs] + body_o[ce:]
414+
cleanup_o = body_o[cs:ce]
415+
else:
416+
setup_m, setup_o, cleanup_o = body_m, body_o, ""
417+
cname = _component_at(masked, eff.start())
284418
comp = comps.setdefault(cname, Component(cname, path))
285419
for acq in ACQUIRES:
286-
for hit in acq.pattern.finditer(setup):
287-
line = text.count("\n", 0, body_start + hit.start()) + 1
288-
released = _is_released(acq, setup, hit.start(), cleanup)
289-
# the acquire expression, trimmed to the call head for a readable tag
290-
snippet = setup[hit.start():].splitlines()[0].strip().rstrip("{").strip()
420+
# find the acquire on the MASKED setup (a keyword inside a string is gone);
421+
# decide release on the ORIGINAL setup/cleanup (so event-name strings and
422+
# handles are intact for the full-key comparison).
423+
for hit in acq.pattern.finditer(setup_m):
424+
abs_pos = body_start + hit.start()
425+
line = masked.count("\n", 0, abs_pos) + 1
426+
released = _is_released(acq, setup_o, hit.start(), cleanup_o)
427+
# the acquire expression for the tag — from the ORIGINAL text, so the
428+
# message shows the real call (string args intact), trimmed to one line.
429+
snippet = text[abs_pos:].splitlines()[0].strip().rstrip("{").strip()
291430
comp.resources.append(
292431
Resource(snippet or acq.name, line, released, acq.resource, acq.eff))
293432
return [c for c in comps.values() if c.resources]
@@ -361,8 +500,10 @@ def _classify_rhs(rhs: str) -> tuple[str, list[str]]:
361500

362501

363502
def _render_bindings(text: str) -> dict[str, list[dict]]:
364-
"""The render-scope binding table per component: only bindings declared DIRECTLY
365-
in the component body (brace-depth 1). A `const filters = {...}` inside a
503+
"""The render-scope binding table per component, scanned over STRING-MASKED
504+
source so a brace/quote inside a literal cannot shift the depth. Only bindings
505+
declared DIRECTLY in the component body (brace-depth 1) count. A `const filters
506+
= {...}` inside a
366507
`useEffect` callback, an event handler, or any nested block is NOT render scope —
367508
it must not shadow the real outer dependency of the same name and mint a false
368509
EFF001. Excluding a render-level `if`/`try` binding only costs a missed finding
@@ -393,16 +534,17 @@ def extract_effects(path: str) -> list[dict]:
393534
whether its body does network IO, and the render-scope binding table of the
394535
component it lives in. The core's effects analysis turns these into a verdict."""
395536
text = _strip_comments(open(path, encoding="utf-8").read())
396-
binds_by_comp = _render_bindings(text)
537+
masked = _mask_strings(text)
538+
binds_by_comp = _render_bindings(masked)
397539
effects: list[dict] = []
398-
for eff in _USE_EFFECT.finditer(text):
399-
body, _start, deps, _end = _effect_callback(text, eff.end())
540+
for eff in _USE_EFFECT.finditer(masked):
541+
body, _start, deps, _end = _effect_callback(masked, eff.end())
400542
if deps is None: # no dep array -> not an EFF001 candidate (by-design re-run cadence)
401543
continue
402-
cname = _component_at(text, eff.start())
544+
cname = _component_at(masked, eff.start())
403545
effects.append({
404546
"component": cname, "file": path,
405-
"line": text.count("\n", 0, eff.start()) + 1,
547+
"line": masked.count("\n", 0, eff.start()) + 1,
406548
"io": bool(_IO.search(body)),
407549
"deps": deps,
408550
"bindings": binds_by_comp.get(cname, []),

0 commit comments

Comments
 (0)