Skip to content

Commit

Permalink
Reject codes with one char after the +.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Nov 25, 2024
1 parent 4b95673 commit ff3dedf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions nr-app/src/__tests__/nr-common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ describe("nr-common/utils", () => {
});
});

describe("rejects plus codes with a single character after the plus", () => {
const invalidCodes = ["7FG49QCJ+2"];
invalidCodes.forEach((code) => {
it(`rejects ${code}`, () => {
expect(isPlusCode(code)).toEqual(false);
});
});
});

describe("rejects codes with leading zeroes", () => {
const invalidCodes = [
"00G49Q00+2",
Expand Down
13 changes: 11 additions & 2 deletions nr-common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ export function isHexKey(key: string): boolean {
}

export function isPlusCode(code: string) {
// Test against a regex that does a reasonable job of finding bad values
const re =
/(^|\s)([23456789C][23456789CFGHJMPQRV][023456789CFGHJMPQRVWX]{6}\+[23456789CFGHJMPQRVWX]*)(\s|$)/i;
const simpleTestResult = re.test(code);
if (simpleTestResult === false) {
return false;
}
if (code[0] === "0" || code[1] === "0") {
return false;

// Don't allow just 1 trailing character after the plus
if (code.length > 9) {
const [, trailing] = code.split("+");
if (trailing.length === 1) {
return false;
}
}

// Check if any characters follow a zero like `AA00AA00+` is invalid
const { failed } = code.split("").reduce(
({ failed, zeroSeen }, letter) => {
if (failed || letter === "+") {
Expand All @@ -61,6 +69,7 @@ export function isPlusCode(code: string) {
if (failed) {
return false;
}

return true;
}

Expand Down

0 comments on commit ff3dedf

Please sign in to comment.