I had been hunting for a way to prevent users from entering credit card numbers in submissions, but couldn't find anything robust.

I wrote a custom validation using this great module (and included clientside validation support) that removes all non-numbers, and then Luhn checks every 15 and 16 digit sequence in a number, textfield, or textarea component and rejects any component containing a sequence that passes.

My code is here: https://www.drupal.org/sandbox/jannis/2486167

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Liam Morland’s picture

Thanks. Can you write this as a patch to webform_validation?

jannis’s picture

I've done a lot of work on the 'no_ccs' module, and one important feature is that is uses clientside validation to do a javascript check which completely prevents transmission of potential credit card numbers.

Knowing that my module is best used with clientside validation and requires js, should I just add a patch that includes the entire module and js as a sub-module? Or would you want just the standard server-side validation code ?

Liam Morland’s picture

This is perhaps a less-common use case, so perhaps it is best for it to be a sub-module.

jannis’s picture

jannis’s picture

Wrong patch there here's the right one

jannis’s picture

Having all kinds of trouble getting the patch to upload. Here it is one more time.

Liam Morland’s picture

Status: Active » Needs review

The last submitted patch, 4: webform_entity-search_api_support-2650410-2.patch, failed testing. View results

The last submitted patch, 5: webform_validation-no_ccs-2486411-4.patch, failed testing. View results

Liam Morland’s picture

Status: Needs review » Needs work

The patch contains a patch file.

dhruveshdtripathi’s picture

Status: Needs work » Needs review
FileSize
12.34 KB

Status: Needs review » Needs work

The last submitted patch, 11: prevent_credit_card-2486411-11.patch.patch, failed testing. View results

dhruveshdtripathi’s picture

Status: Needs work » Needs review
FileSize
12.34 KB

Status: Needs review » Needs work

The last submitted patch, 13: prevent_credit_card-2486411-13.patch, failed testing. View results

dhruveshdtripathi’s picture

Status: Needs work » Needs review
FileSize
5.96 KB
Liam Morland’s picture

Status: Needs review » Needs work

Thanks. Please run a coding standards check on your patch. Every file should end in a newline. There should not be more than two newlines in a row. Please provide full doxygen comments for each function. Please indent with two spaces. Please ensure every function name starts with _?webform_validation_.

mvc’s picture

I believe the regex in the previous patch will strip non-numeric characters before testing, meaning that the string "my favourite numbers are 41111111 and 11111111" will be flagged as a false positive. This also assumes all credit cards are either 15 or 16 digits, which is usually but not always the case in the US and is not true globally. I also swapped out the Luhn algorithm check with one from Symfony (the license permits this with attribution).

I've rewritten the PHP test but we don't use clientside validation with JS here so I am leaving that as an exercise for someone else.

@Liam Morland: I installed phpcs just to check this patch before submitting it, as per your request :)

karolinam’s picture

I made a few changes to patch from comment #17 (prevent_credit_card-2486411-17.patch), more precisely to _webform_validation_no_ccs_check_for_valid_luhn() function:

  • - added word boundaries in regex and min and max optional parameters to be able to check for numbers of specific length (e.g. check for credit card number with only 15-16 digits )
  • - return detected number for better reporting (e.g. highlight the number in block of text)
Liam Morland’s picture

Status: Needs work » Needs review
Liam Morland’s picture

Status: Needs review » Needs work

This patch adds coding standards issues. Please fix.