Return false instead of throwing in validate() for a bad npm namespace#97
Open
arpitjain099 wants to merge 1 commit into
Open
Conversation
The npm type validator follows the (purl, throws) => boolean contract used across purl-type.js: on a violation it throws only when throws is true and otherwise returns false. Fifteen of the sixteen throw sites guard the throw with if (throws). The npm namespace check that requires a leading @ was the one exception, so PurlType.npm.validate(purl, false) threw a PurlError for a namespace not starting with @ instead of returning false, breaking the documented non-throwing boolean path. Wrap that throw in the same if (throws) guard as its siblings and return false when throws is false. Added tests covering the throws=false and throws=true paths for a bad namespace plus a scoped namespace. Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The validators in
src/purl-type.jsshare the(purl, throws) => booleancontract: on a violation they throw only whenthrowsis true, and otherwise return false. Of the sixteen throw sites in the file, fifteen wrap the throw inif (throws). The npm namespace check that requires a leading@was the odd one out and threw unconditionally.Because of that,
PurlType.npm.validate(purl, false)throws aPurlErrorfor an npm purl whose namespace does not start with@, when the non-throwing path is supposed to return false. The constructor always calls withthrows=true, so this only shows up when calling the boolean validator directly.The fix wraps that one throw in the same
if (throws)guard the sibling checks use and returns false whenthrowsis false. Same shape as the golang parameter fix in #88, just a different site.I added a few tests: a bad namespace with
throws=falsenow returns false, a bad namespace withthrows=truestill throws, and a scoped namespace withthrows=falsereturns true. Ran the full suite with mocha and it passes; the new false-return test fails on master before the change.Signed-off-by per DCO.