test: pin semver build-metadata precedence and exercise the real versionMatchesRange path - #1094
test: pin semver build-metadata precedence and exercise the real versionMatchesRange path#1094Retsumdk wants to merge 1 commit into
Conversation
…sRange path Coverage for issue OWASP#1087: 1. Build metadata carries no precedence in semver (10): a version with a '+build' suffix is equal to the bare release, for both string and pre-release-carrying inputs. Nothing previously asserted this. 2. The existing hand-copied test only proved compareVersions, not the scanner path it is named after. Add an end-to-end test that seeds a lodash advisory fixed at 4.17.21 and asserts a pre-release below that fix (4.17.21-beta.1) is flagged while the fixed release is not, exercising the real versionMatchesRange in src/advisory/local-db.ts. Test-only change; no behavior in src altered.
sonukapoor
left a comment
There was a problem hiding this comment.
Really strong first contribution, and I want to be clear that the substance is right before I ask for anything.
I verified both gaps by mutation. Deleting (?:\+.*)? from VERSION_SHAPE produces exactly one failure across 1,684 tests, and it is your build-metadata test. Making versionMatchesRange pre-release-blind produces exactly one failure, and it is your scanner test. Each of your two tests is now the sole guard in the repo for the thing it covers. That is exactly what the issue asked for, and the fact that you ran the mutations yourself and put the output in the PR body is not something I see often.
One thing to fix, and I am fairly sure it is a paste that got away from you rather than anything you meant. tests/helpers.test.ts:354:
it("ranks a pre-release below its associated release (semver 11.3)", () => { expect(compareVersions("1.2.3", "1.2.3")).toBe(0);
expect(compareVersions("1.2.3-beta", "1.2.3-alpha")).toBeGreaterThan(0);
});It duplicates the name of the real test ten lines below, its body is a copy of the assertions from the test above it, and the name does not describe what it asserts. Deleting those three lines is the whole fix. We have no linter yet, so nothing in CI catches a duplicated test name.
One optional question. The old test at tests/helpers.test.ts:389 still hand-copies versionMatchesRange into the test body, which is what your new test now does properly. Happy either way, but if you want to rename it to something that describes what it actually tests, or drop it as superseded, that would be a tidy finish.
Closes #1087
Adds the two test gaps called out in the issue.
1. Semver build-metadata coverage (
tests/helpers.test.ts)Semver §10 says build metadata is ignored for precedence, so
1.2.3+buildand
1.2.3are equal, and likewise for variants carrying a pre-release(e.g.
1.2.3-beta+avs1.2.3-beta+b). Nothing asserted this before, so aregression of the
(?:\+.*)?group inVERSION_SHAPEcould go unnoticed.I verified the new assertions kill that mutation: deleting
(?:\+.*)?$from
VERSION_SHAPEmakes the test fail (Expected: 0, Received: 1).2. Exercise the real
versionMatchesRangepath (tests/local-advisory-source.test.ts)The existing
#1077test hand-copies the body ofversionMatchesRangeinto the test, so it proves
compareVersionsbut not the scanner path it isnamed after. This adds an end-to-end test that seeds a lodash advisory fixed
at
4.17.21and asserts:lodash@4.17.21-beta.1(below the fix) is flagged vulnerablelodash@4.17.21(at the fix) is not flaggedIt goes through
LocalAdvisorySource.queryBatch→ the realversionMatchesRangeinsrc/advisory/local-db.ts. I verified it catchesthe boundary mutation (
>= 0→> 0): that flips the at-fix case tovulnerable and fails the test.
Verification
npm testpasses for both files (--no-cache); full suite unchangedfrom baseline (no new failures).
npm run buildand test-hygiene lint pass.src/modified.