forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 2
bug: validate wallet top-up amount and card before crediting balance #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
1
commit into
develop
Choose a base branch
from
devin/1789000466-wallet-topup-validation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,24 @@ import { type Request, type Response, type NextFunction } from 'express' | |
| import { WalletModel } from '../models/wallet' | ||
| import { CardModel } from '../models/card' | ||
|
|
||
| const MIN_TOP_UP_AMOUNT = 10 | ||
| const MAX_TOP_UP_AMOUNT = 1000 | ||
|
|
||
| function parseTopUpAmount (value: unknown): number | null { | ||
| const amount = typeof value === 'number' ? value : (typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN) | ||
| if (!Number.isInteger(amount) || amount < MIN_TOP_UP_AMOUNT || amount > MAX_TOP_UP_AMOUNT) { | ||
| return null | ||
| } | ||
| return amount | ||
| } | ||
|
|
||
| function isCardExpired (card: CardModel): boolean { | ||
| const now = new Date() | ||
| const year = now.getFullYear() | ||
| const month = now.getMonth() + 1 | ||
| return card.expYear < year || (card.expYear === year && card.expMonth < month) | ||
| } | ||
|
|
||
| export function getWalletBalance () { | ||
| return async (req: Request, res: Response, next: NextFunction) => { | ||
| const wallet = await WalletModel.findOne({ where: { UserId: req.body.UserId } }) | ||
|
|
@@ -20,12 +38,17 @@ export function getWalletBalance () { | |
|
|
||
| export function addWalletBalance () { | ||
| return async (req: Request, res: Response, next: NextFunction) => { | ||
| const cardId = req.body.paymentId | ||
| const card = cardId ? await CardModel.findOne({ where: { id: cardId, UserId: req.body.UserId } }) : null | ||
| if (card != null) { | ||
| const amount = parseTopUpAmount(req.body.balance) | ||
| if (amount === null) { | ||
| res.status(400).json({ status: 'error', message: `Top-up amount must be a whole number between ${MIN_TOP_UP_AMOUNT} and ${MAX_TOP_UP_AMOUNT}.` }) | ||
| return | ||
| } | ||
| const cardId = Number(req.body.paymentId) | ||
| const card = Number.isInteger(cardId) && cardId > 0 ? await CardModel.findOne({ where: { id: cardId, UserId: req.body.UserId } }) : null | ||
| if (card != null && !isCardExpired(card)) { | ||
| try { | ||
| await WalletModel.increment({ balance: req.body.balance }, { where: { UserId: req.body.UserId } }) | ||
| res.status(200).json({ status: 'success', data: req.body.balance }) | ||
| await WalletModel.increment({ balance: amount }, { where: { UserId: req.body.UserId } }) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| res.status(200).json({ status: 'success', data: amount }) | ||
| } catch { | ||
| res.status(404).json({ status: 'error' }) | ||
| } | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Fractional wallet deposits fail after card selection
When
balanceControlaccepts a fractional amount,parseTopUpAmountrejects it after card selection. Users traverse the payment flow before learning that the accepted deposit cannot complete.Learn more
The wallet form defines only required, minimum, and maximum validators. Angular therefore treats values such as
10.5as valid and enables the continue button. The payment page converts the stored value withparseFloatand submits it throughchoosePayment. The new API integer check then returns 400, despite both client screens allowing the user to proceed.Example: A user enters
10.5. The deposit button enables, card selection succeeds, and the final request returns “Top-up amount must be a whole number” instead of adding the displayed amount.Recommended fix: Add an integer validator to
balanceControland display its validation error before navigation. Keep the frontend and API bounds and integer contract synchronized.Was this helpful? React with 👍 or 👎 to provide feedback.