Skip to content

Preview actions: share change-detection script + harden regex/script-injection (#35) - #46

Merged
mmcky merged 2 commits into
mainfrom
fix/preview-dedup-harden
Jun 16, 2026
Merged

mmcky merged 2 commits into
mainfrom
fix/preview-dedup-harden

Conversation

@mmcky

@mmcky mmcky commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Closes #35 (M9, H7, N2) — the pragmatic de-dup we agreed on: extract the genuinely-shared change-detection into one script (fixing H7 once), harden N2 in both comment steps, and leave the bits that legitimately differ (netlify CLI vs wrangler, marker emoji).

M9 — shared change-detection

The ~60 lines of identical change-detection are now scripts/detect-changed-lectures.sh, called by both actions:

env:
  LECTURES_DIR: ${{ inputs.lectures-dir }}
  BASE_SHA: ${{ github.event.pull_request.base.sha }}
  HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: bash "$GITHUB_ACTION_PATH/../scripts/detect-changed-lectures.sh"

Each action drops ~57 lines. The deploy + PR-comment steps stay per-action.

H7 — lectures-dir treated as a literal path

The old check interpolated lectures-dir into a bash regex (^${lectures_dir}/.*\.md$). The shared script uses quoted-glob matching, so a dir name with regex metacharacters behaves correctly. Unit-tested:

  • lectures/aiyagari.md, lectures/sub/nested.md → included
  • lectures/_static/x.md, lectures/intro.md, lectures/notes.txt, other/foo.md → skipped
  • with lectures-dir: "lec.tures", lecXtures/foo.md → skipped (the old regex would have wrongly matched the .)

N2 (security) — no untrusted data in the script body

The github-script comment step interpolated PR-controlled values (changed-files = file paths, deploy-url) directly into the JS template, a script-injection vector. They're now passed via env and read from process.env:

const changedFiles = process.env.CHANGED_FILES || '';
const deployUrl = process.env.DEPLOY_URL || '';

(It was already gated by the trusted-actor check, so exploitation needed repo push access — this removes the anti-pattern regardless.)

Notes / verification

  • Behavior preserved — the script is the same logic (no set -e/pipefail, so a grep no-match still yields 0 like before), plus the H7 fix.
  • Both action YAMLs validated; bash -n clean; no ${{ … }} interpolation remains inside either script: body.
  • Deliberately did not share the PR-comment github-script (it differs by marker/title, and sharing JS via github.action_path/require adds the kind of path indirection we just got bitten by in build-jupyter-cache/publish-gh-pages: relative sibling paths + artifact/release guards #38). That was the "full consolidation" option we set aside.

Closes #35

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings June 16, 2026 04:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR consolidates shared “changed lecture detection” logic between the preview-netlify and preview-cloudflare composite actions into a single script, and hardens both PR-comment github-script steps by removing direct interpolation of PR-controlled values into the JS script body.

Changes:

  • Extracted shared change-detection into scripts/detect-changed-lectures.sh and wired both preview actions to call it.
  • Updated both PR-comment steps to pass PR-controlled values via env and read them from process.env.
  • Documented the behavior/security changes in CHANGELOG.md.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
scripts/detect-changed-lectures.sh New shared bash script for detecting changed lecture Markdown files and _toc.yml location.
preview-netlify/action.yml Replaces inline change-detection with shared script; hardens PR-comment step by using env vars.
preview-cloudflare/action.yml Same as Netlify: shared script + env-based values in PR-comment step.
CHANGELOG.md Records the de-duplication and security hardening under Unreleased.

Comment thread scripts/detect-changed-lectures.sh Resolved
mmcky added a commit that referenced this pull request Jun 16, 2026
The content-change check filtered diff lines with grep -vE '^[+-]{3}' to
drop ---/+++ headers, but that also dropped real content lines starting
with +/- combos (e.g. a markdown '---' front-matter delimiter shows up as
'+---'), so a file changing only such lines was wrongly excluded. Use
git diff --quiet to test for any diff instead. Pre-existing bug carried
over from the original inline logic.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
mmcky and others added 2 commits June 16, 2026 15:43
Pragmatic de-dup of the two near-identical preview actions:

- M9: extract the change-detection logic into a shared
  scripts/detect-changed-lectures.sh, called by both preview-netlify and
  preview-cloudflare via "$GITHUB_ACTION_PATH/../scripts/...". The deploy and
  PR-comment steps stay per-action (netlify CLI vs wrangler, marker emoji).
- H7: the shared script matches lectures-dir as a literal path (quoted-glob)
  instead of injecting it into a regex, so a dir name containing regex
  metacharacters (., +, ...) no longer misbehaves.
- N2 (security): the github-script PR-comment step now receives PR-controlled
  values (changed file paths, deploy URL) via env and reads them from
  process.env, instead of interpolating them into the JS body — closing a
  script-injection vector. (Was gated by the trusted-actor check, but this
  removes the anti-pattern.)

Closes #35

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The content-change check filtered diff lines with grep -vE '^[+-]{3}' to
drop ---/+++ headers, but that also dropped real content lines starting
with +/- combos (e.g. a markdown '---' front-matter delimiter shows up as
'+---'), so a file changing only such lines was wrongly excluded. Use
git diff --quiet to test for any diff instead. Pre-existing bug carried
over from the original inline logic.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preview actions: de-duplicate and harden (regex + github-script injection)

2 participants