Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/__tests__/unit/checks/pii.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,54 @@ describe('pii guardrail', () => {
expect(result.info?.checked_text).toBe('cvv=<CVV>');
});

it('detects BIC/SWIFT codes', async () => {
it('detects BIC/SWIFT codes with explicit prefixes', async () => {
const config = PIIConfig.parse({
entities: [PIIEntity.BIC_SWIFT],
block: false,
});
const text = 'Transfer to BIC DEXXDEXX tomorrow.';
const text = 'Transfer to BIC DEUTDEFF500 tomorrow.';

const result = await pii({}, text, config);

expect((result.info?.detected_entities as Record<string, string[]>)?.BIC_SWIFT).toEqual(['DEXXDEXX']);
expect((result.info?.detected_entities as Record<string, string[]>)?.BIC_SWIFT).toEqual([
'DEUTDEFF500',
]);
expect(result.info?.checked_text).toBe('Transfer to BIC <BIC_SWIFT> tomorrow.');
});

it('detects BIC/SWIFT codes from known bank prefixes', async () => {
const config = PIIConfig.parse({
entities: [PIIEntity.BIC_SWIFT],
block: false,
});
const text = 'Send funds to CHASUS33 by Friday.';

const result = await pii({}, text, config);

expect((result.info?.detected_entities as Record<string, string[]>)?.BIC_SWIFT).toEqual(['CHASUS33']);
expect(result.info?.checked_text).toBe('Send funds to <BIC_SWIFT> by Friday.');
});

it('does not flag common words as BIC/SWIFT codes', async () => {
const config = PIIConfig.parse({
entities: [PIIEntity.BIC_SWIFT],
block: false,
});
const texts = [
'The CUSTOMER ordered a product.',
'We will REGISTER your account.',
'Please CONSIDER this option.',
'The DOCUMENT is ready.',
'This is ABSTRACT art.',
];

for (const text of texts) {
const result = await pii({}, text, config);
expect((result.info?.detected_entities as Record<string, string[]>)?.BIC_SWIFT).toBeUndefined();
expect(result.info?.pii_detected).toBe(false);
}
});

it('detects precise street addresses as location', async () => {
const config = PIIConfig.parse({
entities: [PIIEntity.LOCATION],
Expand Down
87 changes: 86 additions & 1 deletion src/checks/pii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,88 @@ interface PiiDetectionResult {
spans: ReplacementSpan[];
}

const BIC_CONTEXT_PREFIX_PATTERN = [
'(?:[sS][wW][iI][fF][tT])',
'(?:[bB][iI][cC])',
'(?:[bB][aA][nN][kK][\\s-]?[cC][oO][dD][eE])',
'(?:[sS][wW][iI][fF][tT][\\s-]?[cC][oO][dD][eE])',
'(?:[bB][iI][cC][\\s-]?[cC][oO][dD][eE])',
].join('|');

const BIC_WITH_CONTEXT_REGEX = new RegExp(
`(?:${BIC_CONTEXT_PREFIX_PATTERN})[:\\s=]+([A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?)\\b`,
'g'
);

const KNOWN_BIC_PREFIXES = [
'DEUT',
'CHAS',
'BARC',
'HSBC',
'BNPA',
'CITI',
'WELL',
'BOFA',
'JPMC',
'GSCC',
'MSNY',
'COBA',
'DRSD',
'BYLA',
'MALA',
'HYVE',
'WFBI',
'USBC',
'LOYD',
'MIDL',
'NWBK',
'RBOS',
'CRLY',
'SOGE',
'AGRI',
'UBSW',
'CRES',
'SANB',
'BBVA',
'UNCR',
'BCIT',
'INGB',
'ABNA',
'RABO',
'ROYA',
'TDOM',
'BNSC',
'ANZB',
'NATA',
'WPAC',
'CTBA',
'BKCH',
'MHCB',
'BOTK',
'ICBK',
'ABOC',
'PCBC',
'HSBC',
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

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

The prefix 'HSBC' appears twice in the KNOWN_BIC_PREFIXES array (lines 225 and 269). Remove the duplicate entry to avoid redundancy.

Suggested change
'HSBC',

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[nit]

'SCBL',
'DBSS',
'OCBC',
'UOVB',
'CZNB',
'SHBK',
'KOEX',
'HVBK',
'NACF',
'IBKO',
'KODB',
'HNBN',
'CITI',
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

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

The prefix 'CITI' appears twice in the KNOWN_BIC_PREFIXES array (lines 227 and 282). Remove the duplicate entry to avoid redundancy.

Suggested change
'CITI',

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[nit]

];

const KNOWN_BIC_REGEX = new RegExp(
`\\b(?:${KNOWN_BIC_PREFIXES.join('|')})[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?\\b`,
'g'
);

/**
* Default regex patterns for PII entity types.
*/
Expand Down Expand Up @@ -245,7 +327,10 @@ const DEFAULT_PII_PATTERNS: Record<PIIEntity, PatternDefinition[]> = {
group: 1,
},
],
[PIIEntity.BIC_SWIFT]: [{ regex: /\b[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?\b/g }],
[PIIEntity.BIC_SWIFT]: [
{ regex: BIC_WITH_CONTEXT_REGEX, group: 1 },
{ regex: KNOWN_BIC_REGEX },
],

// USA
[PIIEntity.US_BANK_NUMBER]: [{ regex: /\b\d{8,17}\b/g }],
Expand Down
Loading