Skip to content

Commit

Permalink
fix: block encoded word emails
Browse files Browse the repository at this point in the history
  • Loading branch information
zeyu2001 authored Aug 16, 2024
1 parent 8cf3cf8 commit b51f011
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/validators/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opengovsg/starter-kitty-validators",
"version": "1.1.0",
"version": "1.1.1",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
Expand Down
14 changes: 14 additions & 0 deletions packages/validators/src/__tests__/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ describe('EmailValidator with default options', () => {
expect(() => schema.parse('postmaster@[123.123.123.123]')).toThrowError(
ZodError,
)

// Encoded-word
// https://portswigger.net/research/splitting-the-email-atom
expect(() =>
schema.parse('[email protected]'),
).toThrowError(ZodError)

expect(() =>
schema.parse('[email protected]'),
).toThrowError(ZodError)

expect(() =>
schema.parse('=?x?q?collab=40invalid.com=3e=00?=open.gov.sg'),
).toThrowError(ZodError)
})

it('should clean up unnecessary whitespace', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/validators/src/email/consts.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const MAX_LOCAL_LENGTH = 64
export const MAX_DOMAIN_LENGTH = 255
export const ENCODED_WORD_REGEX = /=[?].+[?]=/
11 changes: 10 additions & 1 deletion packages/validators/src/email/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ParsedMailbox } from 'email-addresses'

import { MAX_DOMAIN_LENGTH, MAX_LOCAL_LENGTH } from './consts'
import {
ENCODED_WORD_REGEX,
MAX_DOMAIN_LENGTH,
MAX_LOCAL_LENGTH,
} from './consts'
import { ParsedEmailValidatorOptions } from './options'

export const isValidEmail = (
Expand All @@ -17,6 +21,11 @@ export const isValidEmail = (
if (whitelisted.length === 0) {
return true
}

if (ENCODED_WORD_REGEX.test(email.address)) {
return false
}

return isWhitelistedDomain(domain, whitelisted)
}

Expand Down

0 comments on commit b51f011

Please sign in to comment.