Adjacent Code Neighborhood Sweep
After a fix, sweep the same pattern through neighboring code, because the copy-paste origin of this defect almost always generated siblings
Use when: the fixed defect is a classic pattern class, meaning other implementations of the same shape exist and probably predate this one
Avoid when: the defect is demonstrably unique to one function that nothing else copies or mirrors
Procedure
- Abstract the fixed defect into a searchable pattern: the check shape, data flow, or common call sequence it uses.
- Search the codebase for that pattern, including tests, scripts, configuration templates and vendored code.
- For each candidate surface found, apply the reproduction logic and observe where the defect shape recurs.
- Treat confirmed siblings as defects in their own right, recording them in the same taxonomy rather than absorbing them into this finding.
- Decide per sibling whether it ships in the same fix or a scheduled follow-up, but never leave it unrecorded.
- Add a lint or test check where possible, so the pattern is mechanically excluded going forward.
Signals
- The same error shape exists in a file one directory over, produced by copy-paste from the vulnerable code path.
- A variant surface computes an equivalent decision with slightly different conditions, meriting the same defect inquiry.
- The pattern appears in generated or vendored code, and an upstream maintainer had probably fixed it first.
- Conservative sweep misses the pattern, so a refined search string finds candidates the first pass overlooked.
Patterns
1. Pattern expr
pattern: if(!query.param).doSomething(); -> grep token: 'if(!.*\.param' -> candidates 4
2. Sibling row
candidate: reports.py:212 same shape -> confirmed -> F-0118 (separate defect)
3. Mechanical
lint rule: forbid raw user-supplied id in ownerless query context -> blocks future copies
Pitfalls
- Searching only the exact code shape without abstracting the pattern, which finds identical lines but misses logical siblings.
- Recording siblings as one defect, which hides the count the fix pattern actually earned.
- Skipping the mechanical check, so re-implementing the pattern remains untouched for tomorrow's reviewer.
Done when
- The pattern is abstracted, searched across the codebase, and every candidate has an explicit verdict recorded.
- Mechanical exclusion, where feasible, is added to tests or lint so recurrence is checked automatically rather than promoted by memory.
References
1---2name: adjacent-code-neighborhood-sweep3description: "After a fix, sweep the same pattern through neighboring code, because the copy-paste origin of this defect almost always generated siblings. Use when the fixed defect is a classic pattern class, meaning other implementations of the same shape exist and probably predate this one"4pack: patchwise5version: 1.0.06category: defensive-security7tags: [sweep, pattern, hardening]8metadata:9 source: https://skillmd.com/skills/wsh7ash/adjacent-code-neighborhood-sweep10---1112# Adjacent Code Neighborhood Sweep1314> After a fix, sweep the same pattern through neighboring code, because the copy-paste origin of this defect almost always generated siblings1516**Use when:** the fixed defect is a classic pattern class, meaning other implementations of the same shape exist and probably predate this one1718**Avoid when:** the defect is demonstrably unique to one function that nothing else copies or mirrors1920## Procedure21221. Abstract the fixed defect into a searchable pattern: the check shape, data flow, or common call sequence it uses.232. Search the codebase for that pattern, including tests, scripts, configuration templates and vendored code.243. For each candidate surface found, apply the reproduction logic and observe where the defect shape recurs.254. Treat confirmed siblings as defects in their own right, recording them in the same taxonomy rather than absorbing them into this finding.265. Decide per sibling whether it ships in the same fix or a scheduled follow-up, but never leave it unrecorded.276. Add a lint or test check where possible, so the pattern is mechanically excluded going forward.2829## Signals3031- The same error shape exists in a file one directory over, produced by copy-paste from the vulnerable code path.32- A variant surface computes an equivalent decision with slightly different conditions, meriting the same defect inquiry.33- The pattern appears in generated or vendored code, and an upstream maintainer had probably fixed it first.34- Conservative sweep misses the pattern, so a refined search string finds candidates the first pass overlooked.3536## Patterns3738**1. Pattern expr**3940```text41pattern: if(!query.param).doSomething(); -> grep token: 'if(!.*\.param' -> candidates 442```4344**2. Sibling row**4546```text47candidate: reports.py:212 same shape -> confirmed -> F-0118 (separate defect)48```4950**3. Mechanical**5152```text53lint rule: forbid raw user-supplied id in ownerless query context -> blocks future copies54```5556## Pitfalls5758- Searching only the exact code shape without abstracting the pattern, which finds identical lines but misses logical siblings.59- Recording siblings as one defect, which hides the count the fix pattern actually earned.60- Skipping the mechanical check, so re-implementing the pattern remains untouched for tomorrow's reviewer.6162## Done when6364- The pattern is abstracted, searched across the codebase, and every candidate has an explicit verdict recorded.65- Mechanical exclusion, where feasible, is added to tests or lint so recurrence is checked automatically rather than promoted by memory.6667## References6869- [CWE - pattern catalog for searching](https://cwe.mitre.org/)70- [OWASP - secure coding practices checklist](https://owasp.org/www-project-secure-coding-practices-quick-reference-guide/)