Skip to content
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

fix: add sanitisation function for products #6880

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

KenLSM
Copy link
Contributor

@KenLSM KenLSM commented Nov 8, 2023

Problem

Users can maliciously submit a product from FE to manipulate the product through scripts. This causes the product item evaluated on our BE to be incorrectly referencing the data from FE, instead of from the form payment products.

Solution

Don't trust FE.

Treat the product data from FE as dirty. Extract only the relevant details (quantity + selected) and replace the product data from the one in BE.

Breaking Changes

  • No - this PR is backwards compatible

Tests

Regression

Payment by Product form submission should still work**
  • Make a payment on a form with payment by products
    • Ensure that payment had succeeded
    • Ensure that payment title on payment summary displays correctly
    • Ensure that payment title on payment successfully page displays correctly
    • Ensure that payment title on receipt displays correctly
Variable Payment form submission should still work
  • Make a payment on a form with Variable Payment
    • Ensure that payment had succeeded
    • Ensure that payment title on payment summary displays correctly
    • Ensure that payment title on payment successfully page displays correctly
    • Ensure that payment title on receipt displays correctly

@KenLSM KenLSM marked this pull request as ready for review November 9, 2023 05:08
@KenLSM KenLSM requested a review from justynoh November 16, 2023 15:11
@KenLSM KenLSM force-pushed the fix/payment-products-from-db branch from 9bf0320 to bccc3c2 Compare November 16, 2023 15:19
@KenLSM KenLSM force-pushed the fix/payment-products-from-db branch from 88af378 to e1c9b4f Compare January 8, 2024 06:57
Copy link
Contributor

@justynoh justynoh left a comment

Choose a reason for hiding this comment

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

lgtm

Comment on lines +398 to +201
/**
* Sanitizes the payment fields from the form and the incoming submission
* The payment products from incoming submission can be freely altered by the respondent
* which could result in undesirable data seeded into our database
* @param form
* @param dirtyPaymentProducts
*/
export const sanitisePaymentProducts = (
Copy link
Contributor

Choose a reason for hiding this comment

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

supernit: sanitize or sanitise? :)

if (!dirtyProduct) return null

return {
..._.pick(dirtyProduct, ['selected', 'quantity']), // only selected and quantity are allowed to be passed through
Copy link
Contributor

Choose a reason for hiding this comment

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

any particular reason to use lodash pick here as opposed to just { ...dirtyProduct, data: cleanProductData } or for even more safety, { selected: dirtyProduct.selected, quantity: dirtyProduct.quantity, data: cleanProductData } so that we can take advantage of the type system for typechecking? Since with pick, the keys are not typechecked.

Copy link
Contributor Author

@KenLSM KenLSM Feb 13, 2024

Choose a reason for hiding this comment

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

any particular reason to use lodash pick here as opposed to just { ...dirtyProduct, data: cleanProductData }

My intention was to operate as an explicit whitelist of properties that we will read from dirtyProduct, as opposed to accepting all the fields that are passed in. I felt that these properties are critical thus, it would be better to have them explicitly inherited rather than implicitly accepted.

Since with pick, the keys are not typechecked.

Lodash's pick does have typechecking, the selected property U must exist as a key in object T.

pick<T extends object, U extends keyof T>(object: T, ...props: Array<Many<U>>): Pick<T, U>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants