1 # Remediation Strategies
2
3 Detailed tracing recipe, semver analysis, per-tier commands, and breaking-change checks. Package manager here is Yarn 3.x (berry) with a node-modules linker; `yarn.lock` uses `@npm:` resolution keys.
4
5 ## Tracing the dependency chain
6
7 Goal: get from the vulnerable package up to the workspace package(s) under `packages/` that own it.
8
9 ```bash
10 # 1. Which versions are installed, and which match the vulnerable range?
11 grep -n "<pkg>@npm" yarn.lock
12
13 # 2. Who depends on that version? Walk up one level at a time.
14 # Search for the package as a dependency line, then find the parent stanza.
15 grep -n "<pkg>" yarn.lock # find the "<pkg>: <range>" dependency lines
16 # For each parent range key (e.g. "@some/parent@npm:^1.2.3"), repeat until
17 # you reach a name that appears in a packages/*/package.json.
18
19 # 3. Which workspace package declares it (directly or via nearest ancestor)?
20 grep -rn "<ancestor-or-pkg>" packages/*/package.json packages/*/*/package.json package.json
21 ```
22
23 For the owning workspace package, classify it:
24
25 ```bash
26 node -e "const p=require('./packages/<path>/package.json'); \
27 console.log('name', p.name, '| private', !!p.private); \
28 console.log('dep', (p.dependencies||{})['<ancestor>']); \
29 console.log('devDep', (p.devDependencies||{})['<ancestor>'])"
30
31 # Is the ancestor actually imported at runtime (ships + reachable) or build/test only?
32 grep -rn "<ancestor>" packages/<path>/src | head
33 ```
34
35 Interpretation:
36 - In `dependencies` of a `private: false` package, imported in `src/` → **ships to consumers and runs**. Highest priority to fix properly (Tier 1b enforces it).
37 - In `devDependencies` or a `private: true` package → does not ship; a lockfile-only fix (Tier 1a) or even root override is acceptable.
38 - Purely transitive → the affected package is the nearest ancestor it declares; fix targets that ancestor's range.
39
40 ## Semver: reachable vs. pinnable
41
42 A fixed version can be **reachable** (the highest version resolvable within the currently declared ranges is patched) without being **pinnable** (every version the ranges allow is patched).
43
44 Work out both by reading the declared ranges up the chain:
45
46 ```bash
47 # What range does the parent declare for the child, across versions?
48 npm view <parent>@<version> dependencies.<child>
49
50 # What does each candidate child version pull in turn?
51 npm view <child>@<candidateVersion> version dependencies.<grandchild>
52 ```
53
54 - **Reachable only** (float): the range spans both vulnerable and patched versions (e.g. `^7.0.0` where `7.0.x` is vulnerable but `7.1.x` is patched). A fresh install lands on the patched high end, but an existing lockfile, a dedupe constraint, or `npm ci` can legitimately resolve the vulnerable low end. Fixing the repo lockfile works; downstream consumers are only *probabilistically* safe.
55 - **Pinnable** (guaranteed): the lowest version the ranges allow is already patched (e.g. after a bump, the range becomes `^7.1.1` and `7.1.1` is the first version pulling the patched grandchild). Enforced for everyone, including downstream.
56
57 Always state which one applies. Prefer moving to a pinnable state when a published package is affected.
58
59 ## Tier 1a — In-range transitive refresh (lockfile only)
60
61 Use when a patched version is **reachable** within the existing declared ranges and introduces no breaking changes. No `package.json` change.
62
63 ```bash
64 # Recursively re-resolve every occurrence of the transitive dep to the highest
65 # version allowed by existing ranges (pulls patched grandchildren in cascade).
66 yarn up -R <transitive-pkg>
67
68 # Or lockfile-only (no linking):
69 yarn up -R <transitive-pkg> --mode update-lockfile
70 ```
71
72 Then verify (see below). This reflects what a fresh downstream install already resolves, so it aligns the repo with real-world resolution — but it does not tighten ranges, so it's a float, not a guarantee.
73
74 ## Tier 1b — Bump the direct dependency in the affected package
75
76 Use when the fix is only **pinnable** by moving the declared range, or when 1a would introduce breaking changes at the transitive level. Edit the affected `packages/*/package.json` dependency to the LOWEST version whose transitive ranges guarantee the patched dependency (found via the semver analysis above), then `yarn install`.
77
78 Breaking-change checks BEFORE bumping:
79
80 ```bash
81 # Major-version jump? Compare current resolved vs candidate.
82 npm view <dep> versions --json | tail
83 # Peer dependency compatibility with what the workspace already uses:
84 npm view <dep>@<candidate> peerDependencies
85 # Read the changelog / migration notes for the candidate major:
86 npm view <dep>@<candidate> homepage repository
87 ```
88
89 - Use Context7 MCP (`resolve-library-id` → `query-docs`) for the library's migration guide when crossing a major version.
90 - Verify the affected package's ACTUAL usage still compiles and behaves: the API it calls, config shape, and output. A bump that typechecks can still change runtime output.
91 - If a sibling dep must move in lockstep (e.g. a codegen `core` + plugin pair sharing a major), bump them together.
92 - Prefer the smallest bump that reaches a pinnable-patched state; avoid gratuitous majors.
93
94 ## Tier 2 — Root resolutions / overrides (LAST RESORT)
95
96 Only when no in-package option exists. The repo already uses this pattern for some security bumps (`resolutions` in the root `package.json`).
97
98 ```jsonc
99 // root package.json
100 "resolutions": {
101 "<pkg>": "^<patched>", // force everywhere
102 "<parent>/<pkg>": "^<patched>" // or scope to one parent
103 }
104 ```
105
106 Critical caveats to always state:
107 - **Does not ship.** `resolutions`/`overrides` are honored only at THIS repo's install root. They are not part of a published package's metadata, so a downstream `npm install @medusajs/<pkg>` re-resolves the transitive freshly and can get the vulnerable version again.
108 - Therefore this fixes only the repo's own alert, not consumers. If the vulnerable dep reaches a published package, note that Tier 2 is incomplete and the real fix is Tier 1b (or an upstream release).
109 - Forcing a version outside a parent's declared range (e.g. `~3.7.6`) works mechanically but can break that parent — confirm API compatibility.
110
111 ## Verification
112
113 ```bash
114 git diff --stat # only yarn.lock (+ package.json if 1b)
115 grep -n "<pkg>@npm" yarn.lock # confirm no version in the vulnerable range remains
116 git diff yarn.lock | grep -E '^[-+]"' | \
117 grep -oE '"[^"]+@npm:' | sort -u # confirm churn is scoped to the dep neighborhood
118 ```
119
120 If a direct dep was bumped (1b): build/typecheck the affected package and exercise the code path that uses the dependency.
121
122 ## Doing it in a worktree + PR
123
124 When asked to open a PR, isolate the work:
125
126 ```bash
127 git worktree add -b fix/<slug> <scratchpad>/wt-<slug> develop
128 # ...run the refresh/bump inside the worktree, verify, commit...
129 git worktree remove <scratchpad>/wt-<slug> # branch persists after removal
130 ```
131
132 Commit as `chore(deps): ...` referencing the GHSA id and alert number. For a lockfile-only refresh, note that no changeset is needed (no published `package.json`/source change).