Skip to content

Commit fb54217

Browse files
committed
Make regex more specific
1 parent 2057972 commit fb54217

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/__tests__/unit/checks/pii.test.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,67 @@ describe('pii guardrail', () => {
4343
await expect(pii({}, '', config)).rejects.toThrow('Text cannot be empty or null');
4444
});
4545

46-
it('detects Korean Resident Registration Number (KR_RRN)', async () => {
46+
it('detects valid Korean Resident Registration Number (KR_RRN)', async () => {
4747
const config = PIIConfig.parse({
4848
entities: [PIIEntity.KR_RRN],
4949
block: false,
5050
});
51-
const text = 'Korean RRN: 123456-1234567';
51+
// Valid format: YYMMDD-GNNNNNN (900101 = Jan 1, 1990, gender digit 1)
52+
const text = 'Korean RRN: 900101-1234567';
5253

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

5556
expect(result.tripwireTriggered).toBe(false);
56-
expect((result.info?.detected_entities as Record<string, string[]>)?.KR_RRN).toEqual(['123456-1234567']);
57+
expect((result.info?.detected_entities as Record<string, string[]>)?.KR_RRN).toEqual(['900101-1234567']);
5758
expect(result.info?.checked_text).toBe('Korean RRN: <KR_RRN>');
5859
});
5960

61+
it('detects multiple valid KR_RRN formats', async () => {
62+
const config = PIIConfig.parse({
63+
entities: [PIIEntity.KR_RRN],
64+
block: false,
65+
});
66+
// Testing different valid date ranges and gender digits (1-4)
67+
const text = 'RRNs: 850315-2345678, 001231-3456789, 750628-4123456';
68+
69+
const result = await pii({}, text, config);
70+
71+
expect(result.tripwireTriggered).toBe(false);
72+
expect((result.info?.detected_entities as Record<string, string[]>)?.KR_RRN).toHaveLength(3);
73+
expect((result.info?.detected_entities as Record<string, string[]>)?.KR_RRN).toContain('850315-2345678');
74+
expect((result.info?.detected_entities as Record<string, string[]>)?.KR_RRN).toContain('001231-3456789');
75+
expect((result.info?.detected_entities as Record<string, string[]>)?.KR_RRN).toContain('750628-4123456');
76+
});
77+
78+
it('does not detect invalid KR_RRN patterns (false positives)', async () => {
79+
const config = PIIConfig.parse({
80+
entities: [PIIEntity.KR_RRN],
81+
block: false,
82+
});
83+
// Invalid patterns that should NOT be detected:
84+
// - Invalid month (13)
85+
// - Invalid day (00, 32)
86+
// - Invalid gender digit (0, 5, 9)
87+
// - Random tracking numbers
88+
const text = 'Invalid: 901301-1234567, 900100-1234567, 900132-1234567, 900101-0234567, 900101-5234567, 123456-7890123';
89+
90+
const result = await pii({}, text, config);
91+
92+
expect(result.tripwireTriggered).toBe(false);
93+
expect(result.info?.detected_entities).toEqual({});
94+
expect(result.info?.checked_text).toBe(text); // No masking should occur
95+
});
96+
6097
it('triggers tripwire for KR_RRN when block=true', async () => {
6198
const config = PIIConfig.parse({
6299
entities: [PIIEntity.KR_RRN],
63100
block: true,
64101
});
65-
const text = 'Korean RRN: 123456-1234567';
102+
const text = 'Korean RRN: 900101-1234567';
66103

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

69106
expect(result.tripwireTriggered).toBe(true);
70-
expect((result.info?.detected_entities as Record<string, string[]>)?.KR_RRN).toEqual(['123456-1234567']);
107+
expect((result.info?.detected_entities as Record<string, string[]>)?.KR_RRN).toEqual(['900101-1234567']);
71108
});
72109
});

src/checks/pii.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ const DEFAULT_PII_PATTERNS: Record<PIIEntity, RegExp> = {
241241
[PIIEntity.FI_PERSONAL_IDENTITY_CODE]: /\b\d{6}[+-A]\d{3}[A-Z0-9]\b/g,
242242

243243
// Korea
244-
[PIIEntity.KR_RRN]: /\b\d{6}-\d{7}\b/g,
244+
// Format: YYMMDD-GNNNNNN where YY=year, MM=month(01-12), DD=day(01-31), G=gender/century(1-4)
245+
[PIIEntity.KR_RRN]: /\b\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])-[1-4]\d{6}\b/g,
245246
};
246247

247248
/**

0 commit comments

Comments
 (0)