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

Fixes iban checksum calculation #2591

Merged
merged 3 commits into from
Oct 31, 2022
Merged

Conversation

srcoley
Copy link
Contributor

@srcoley srcoley commented Oct 16, 2022

Summary

Fixes Issue #2588

IBAN check digit can be incorrectly calculated to fall outside the proper range of 2-98. This would cause some IBAN validator libraries(ex: https://github.com/gocardless/ibandit) to mark the IBANs as invalid.

This change calculates the check digit correctly and adds checksum asserts to each country's IBAN test.

Other Information

While this change resolves the IBAN check digit issue, there is another issue lurking in Faker's IBAN generation code. Some countries implement a national check digit in addition to the standard IBAN check digit. This can cause Faker generated IBANs to fail full validations where the national check digit is taken into account. This means that while an IBAN might be considered valid with a library such as ibandit, it could be flagged as invalid with more strict validators such as https://www.iban.com/iban-checker.

Valid according to ibandit:

require 'ibandit' #=> true
iban = Faker::Bank.iban(country_code: 'fr') #=> "FR8942372847507891Y7GYUJ162"
ibandit = Ibandit::IBAN.new(iban) #=> #<Ibandit::IBAN:0x000000010812ef90...
ibandit.valid? #=> true

Invalid according to https://www.iban.com:

Screen Shot 2022-10-16 at 3 09 58 PM

To complicate matters, the algorithm for calculating a national check digit varies country to country. France, for instance, make use of the standard ISO 7064 MOD-97-10, but has their own variation for encoding account letters to numbers. Some countries don't use the ISO 7064 MOD-97-10 standard at all, but instead opt for Luhn, "conversion + sum", or
"weighted sum" algorithms.

When I started working on this issue, I overlooked the IBAN check digit calculation bug that this pull request aims to solve, and instead thought the invalid IBANs were a result of Faker not calculating a valid national check digit. It took me a while to discover there were two separate issues, and I spent some time attempting to properly calculate national check digits focusing firstly on the ISO 7064 MOD-97-10 algorithm and France's variant.

You can see the progress I was making here: main...srcoley:faker:iban_national_check_digit_support

I'd be happy to continue adding national check digit support(at least for ISO 7064 MOD-97-10 and variants), but I figure the maintainers have some decisions to make first:

  1. Is this type of IBAN accuracy worth it?
  2. If it is worth it, are all of the edge cases(variants) worth it?
  3. Since ibandit(and no other lib I could find) properly validates national check digits, what should the testing strategy look like?

For more information on national check digits, see: https://en.wikipedia.org/wiki/International_Bank_Account_Number#National_check_digits

Copy link
Contributor

@fbuys fbuys left a comment

Choose a reason for hiding this comment

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

Left some comments, but I have not manually tested it. Will make some time to manually test it.

lib/faker/default/bank.rb Outdated Show resolved Hide resolved
test/faker/default/test_faker_bank.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@thdaraujo thdaraujo left a comment

Choose a reason for hiding this comment

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

thanks for working on this, @srcoley !

It looks good to me, but let's add an extra test for iban_checksum if possible.

private

# https://en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN
def valid_iban_checksum?(iban)
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

lib/faker/default/bank.rb Show resolved Hide resolved
@thdaraujo
Copy link
Contributor

thdaraujo commented Oct 28, 2022

To answer your question, I think your fixes should be merged so we can improve the current generator, even though it's currently limited.

Then, we can evaluate how much effort it would take to add support to national check digit support and make it more accurate.

@srcoley
Copy link
Contributor Author

srcoley commented Oct 29, 2022

Thanks for the review @thdaraujo! I've pushed up a new test for iban_checksum.

@srcoley srcoley requested a review from thdaraujo October 29, 2022 15:55
Copy link
Contributor

@thdaraujo thdaraujo left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

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

Successfully merging this pull request may close these issues.

3 participants