Skip to content

fix: support multi-hash SRI strings in parsePackageIntegrity - #1545

Open
TariQMah wants to merge 8 commits into
CycloneDX:mainfrom
TariQMah:fix/1544-multi-hash-integrity
Open

TariQMah wants to merge 8 commits into
CycloneDX:mainfrom
TariQMah:fix/1544-multi-hash-integrity

Conversation

@TariQMah

Copy link
Copy Markdown

Description

parsePackageIntegrity compared the whole integrity value against single-hash patterns anchored with ^...$, so a whitespace-separated multi-hash SRI string (for example sha512-… sha1-… from package-lock.json) threw RangeError: unparsable value. ExternalReferenceFactory.makeDist caught that error, leaving the distribution reference's hashes empty.

This PR splits the integrity value on whitespace and tests each token, returning the first supported hash in the pre-sorted list's order (SHA-512 first). A multi-hash string now yields the SHA-512 digest and makeDist records it. A string with no supported hash still throws RangeError. Added unit tests for multi-hash inputs and for a multi-hash string with no supported hash.

I could not run the build or tests locally. Please let CI run the checks; happy to address any feedback.

Resolves or fixes issue: #1544

AI Tool Disclosure

  • My contribution does not include any AI-generated content
  • My contribution includes AI-generated content, as disclosed below:
    • AI Tools: Muse (browser agent, acting for the contributor via the GitHub web editor)
    • LLMs and versions: Muse Spark
    • Prompts: Fix issue #1544: make parsePackageIntegrity handle whitespace-separated multi-hash SRI strings and add unit test coverage for it.

Affirmation

Split the integrity value on whitespace and test each SRI token, so a multi-hash string such as "sha512-… sha1-…" yields a supported hash (SHA-512 first, per the pre-sorted list) instead of throwing RangeError. This also fixes ExternalReferenceFactory.makeDist leaving hashes empty for such packages.

Fixes CycloneDX#1544

Signed-off-by: Tariq Mahmood  <t.ali1993@hotmail.com>
Add test cases for whitespace-separated multi-hash SRI strings: the SHA-512 digest is extracted whether it is the first or the second token, SHA-1 wins over SHA-256 per the pre-sorted list, and a string with no supported hash still throws RangeError.

Signed-off-by: Tariq Mahmood  <t.ali1993@hotmail.com>
The test file rewrite mistyped "my-own=registry" as "my-own-registry"; restore the original line so the diff only adds the new test cases.

Signed-off-by: Tariq Mahmood  <t.ali1993@hotmail.com>
@TariQMah
TariQMah requested a review from a team as a code owner November 25, 2026 09:58
@codacy-production

codacy-production Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@jkowalleck jkowalleck added the bug Something isn't working label Nov 25, 2026

@jkowalleck jkowalleck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for working on this.
While I like minimal non-breaking changes, I think this can be improved -see my comments.

@jkowalleck

jkowalleck commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Signed-off-by: Tariq Mahmood  <t.ali1993@hotmail.com>
Signed-off-by: Tariq Mahmood  <t.ali1993@hotmail.com>
Signed-off-by: Tariq Mahmood  <t.ali1993@hotmail.com>
@TariQMah
TariQMah requested a review from jkowalleck November 26, 2026 05:30
Signed-off-by: Tariq Mahmood  <t.ali1993@hotmail.com>
Signed-off-by: Tariq Mahmood  <t.ali1993@hotmail.com>
@TariQMah
TariQMah requested a review from jkowalleck November 27, 2026 04:53
@TariQMah

Copy link
Copy Markdown
Author

Thanks for the review, @jkowalleck — addressed in the two new commits:

  • parsePackageIntegrity now tracks a yielded flag and throws RangeError only if nothing was yielded, instead of re-running the match check.
  • Added break after the yield (a token can match at most one of the integrityRE patterns).
  • makeDist now iterates the generator with for...of instead of [...parsePackageIntegrity(integrity)].forEach(...).

I could not run the build or tests locally; please let CI verify. Ready for another look.

[[HashAlgorithm['SHA-384'],
'a2a56e01f5d129aa7b7dd81c098e6eca433af91f46a90f0afeec72f6bc7b1cd42519897590fcd0868d70c7827063cc02']]
],
['sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg== sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is a bit hard to read.
could you reformat this like

['sha512-/... sha1-2Klr13/...', [
  [HashAlgorithm['SHA-512'], '...'],
  [HashAlgorithm['SHA-1'], '...'],
]]

@jkowalleck

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fet: support multi-hash SRI strings in Contrib.FromNodePackageJson.Utils.parsePackageIntegrity

2 participants