Skip to content

Fix KidnummerValidator incorrectly accepting invalid KID numbers#76

Open
leifthorbjornsen wants to merge 1 commit intobekkopen:masterfrom
leifthorbjornsen:fix/kidnummer-validator-mod11-dash-checksum
Open

Fix KidnummerValidator incorrectly accepting invalid KID numbers#76
leifthorbjornsen wants to merge 1 commit intobekkopen:masterfrom
leifthorbjornsen:fix/kidnummer-validator-mod11-dash-checksum

Conversation

@leifthorbjornsen
Copy link

Fixes a bug in KidnummerValidator.isValid() where some invalid KID numbers were accepted.

Examples:

  • 100030
  • 100031
  • 100033

The issue is in the mod11 check:

if ("-".equals(kMod11) || Integer.parseInt(kMod11) == k.getChecksumDigit()) {
    return; // valid
}

When the mod11 remainder is 1, there is no valid single digit that can serve as a checksum digit, so it returns - instead. The bug was that the validator treated this as an automatic pass, without checking that the KID number actually ended with -. As a result, any KID that failed mod10 and happened to produce a mod11 remainder of 1 would be incorrectly accepted.

The fix is to compare the calculated mod11 result directly with the last character of the KID:

String lastChar = kidnummer.substring(kidnummer.length() - 1);
if (kMod11.equals(lastChar)) {
    return; // valid
}

This works for both digits and - without special handling.
1000005- still passes because mod11 returns - and the last character is -, while 100030 now correctly fails because mod11 returns - but the last character is 0.

Also added a test for the bug case:

private static final String KIDNUMMER_INVALID_CHECKSUM_MOD10_AND_MOD11 = "100030";

@Test
public void testInvalidKidnummerFailingBothMod10AndMod11() {
    assertFalse(KidnummerValidator.isValid(KIDNUMMER_INVALID_CHECKSUM_MOD10_AND_MOD11));
}

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.

1 participant