Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: block encoded word emails #10

Merged
merged 2 commits into from
Aug 16, 2024
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
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
Loading