fix(isISO6346): anchor regex alternation and remove stray comma#2771
Open
DucMinhNe wants to merge 1 commit into
Open
fix(isISO6346): anchor regex alternation and remove stray comma#2771DucMinhNe wants to merge 1 commit into
DucMinhNe wants to merge 1 commit into
Conversation
The isISO6346 pattern had an un-grouped alternation:
/^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/
Because | has the lowest precedence and the two branches were not
wrapped in a group, the regex parsed as:
(^[A-Z]{3}(U[0-9]{7})) OR (([J,Z][0-9]{6,7})$)
The first branch was anchored only at the start and the second only at
the end, so strings with trailing garbage after a valid prefix, or
leading garbage before a valid suffix, passed validation. The character
class [J,Z] also matched a literal comma, which is never valid in an
ISO 6346 category identifier.
Wrap the alternation in the existing group and drop the comma so the
whole string is anchored:
/^[A-Z]{3}(U[0-9]{7}|[JZ][0-9]{6,7})$/
Add regression cases for the previous false positives.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2771 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2587 2587
Branches 656 656
=========================================
Hits 2587 2587 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
isISO6346validator accepts strings it should reject because of an un-grouped regex alternation.The bug
|has the lowest precedence, and the two branches are not wrapped in a group, so the pattern actually parses as:The first branch is anchored only at the start and the second only at the end. As a result:
Additionally, the character class
[J,Z]matches a literal comma, which is never a valid ISO 6346 category identifier.Reproduction
These all return
truetoday. Because the checksum block only runs whenstr.length === 11, the un-anchored false positives bypass validation entirely.The fix
Wrap the alternation in the existing group and drop the stray comma, so the whole string is anchored:
This is the same class of fix already applied to other anchored-alternation validators in this project.
Tests
ABCU1234567GARBAGE,GARBAGEZ123456,XYZ,123456) to theisISO6346/isFreightContainerIDinvalid lists.J/Zshort forms likeQJRZ123456) still pass.eslint src testis clean.