fix(other): validate current two-digit year when it matches century prefix - #169
Open
stevechen256-source wants to merge 1 commit into
Open
Conversation
…refix A two-digit expiration year that equals the current century prefix (e.g. "20" during 2020) was short-circuited to "potentially valid" by the century-prefix check, so every MM/YY expiration whose year was "20" failed to validate for the entire year 2020. Reorder the checks so a complete two-digit year that is the current year (or within the valid window) is accepted first, and the century prefix match only serves as a potentially-valid fallback for partially typed 4-digit years. Fixes braintree#127
|
This PR has had no activity for 14 days. It will be closed in 7 days if no activity is registered. |
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.
Summary of changes
Fixes #127 — a card expiration such as
07/20was reported as invalid for the entire year 2020.What & Why
In
expirationYear, the two-digit-year branch first checked whether the input matched the century prefix of the current year (String(currentYear).substr(0, 2)):During 2020 the century prefix is
"20", which is also a complete two-digit year. So"20"was always short-circuited to "potentially valid" and never evaluated as the current year. EveryMM/YYexpiration withYY = 20therefore failed validation in 2020 (e.g.expirationDate("07/20")on 2020-01-01 →{ isValid: false }), even though July 2020 was in the future. The same bug mis-classified future years whose two-digit form matched the century prefix (e.g."20"entered in 2018/2019).Changes
src/expiration-year.ts: compute the actual validity (isCurrentYear/ valid window) before the century-prefix check, so a complete two-digit year that is the current year or within the valid window is accepted. The century-prefix match now only serves as a "potentially valid" fallback for partially typed 4-digit years (e.g."20"→"2021").src/__tests__/expiration-year.ts: regression tests with the system clock set to 2020-01-01.src/__tests__/expiration-date.ts: regression tests asserting07/20is valid (and06/19is invalid) in 2020.Test plan
npm test(jest + eslint):Before the fix, the new regression cases failed:
After the fix both return
isValid: true. Full suite passes with coverage thresholds met.Checklist
Authors
stevechen256-source