[MEDIUM] inject-hashed-filenames.py: tag-aware HTML rewrite instead of substring replace #3
Loading…
Add table
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
From audit #1.
Problem
bin/inject-hashed-filenames.py:50-52does a plainhtml.replace(old_src, new_src). Ifold_srcever appears outside a<script src="…">attribute (e.g. inside a comment or a JSON literal embedded in the HTML), the rewrite is wrong.Remediation
Tag-aware rewrite: regex limited to
<script src="…">(or a tiny HTML parser).Acceptance
old_srcinside HTML comments / non-script-src contexts are NOT rewritten.Effort: small.
Agent Evaluation
Feasibility: Trivial. The substring replace is exactly one line (
bin/inject-hashed-filenames.py:50); a regex anchored to<script ... src="OLD"(and<link ... href="OLD"once stylesheets land) plus a small unit test covers it.Impact: Low–Medium today. The placeholder filenames (
/static/dist/app.js,/guest/dist/guest-app.js) are unique enough that a stray match in a comment or JSON literal is unlikely, but the failure mode is silent corruption — production HTML emits a broken script src and the SPA breaks at runtime. Worth fixing because the build pipeline is exactly the place where silent-substring bugs are most expensive.Effort: Small.
Recommendation: Accept.
Implementation plan
html.replace(old_src, new_src)with a regex that matches onlysrc/hrefattribute values (single or double quoted) on<script>/<link>tags. Pattern:re.compile(r'(<(?:script|link)[^>]*\b(?:src|href)\s*=\s*["\'])' + re.escape(old_src) + r'(["\']\s*/?>)'), replacementr'\g<1>' + new_src + r'\g<2>'.print("skip")into an explicit warning so a typo inold_srcis loud).tests/test_inject_hashed_filenames.py(pytest):<script src="/static/dist/app.js">→ rewritten.<!-- /static/dist/app.js -->and<pre>{ "src": "/static/dist/app.js" }</pre>→ NOT rewritten.pyteststep topackage.jsonscripts.testor to the parent project'spreparehook).No decision checkboxes — the change is mechanical and the regex+test pair is the only sensible shape.
Note on stylesheets
The scaffold doesn't currently rewrite hashed CSS, but it's adjacent and easy to fold in. Recommend doing both
<script src>and<link href>in the same regex so we don't have to revisit when CSS hashing lands.Evaluated webapp-scaffold#3 — Small/Medium, recommend accept; tag-aware regex + pytest test, no decision needed.
Implemented #3 → commit
b3b2903(tag-aware regex on