Skip to content

Commit 4e80607

Browse files
Make email and permissive URL validation run in linear time (#11417)
Co-authored-by: Starbird <64431622+StarbirdTech@users.noreply.github.com>
1 parent 680a090 commit 4e80607

5 files changed

Lines changed: 25 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Make `Validation.Rule.email()` and `Validation.Rule.url({ requireProtocol: false })` reject long malformed values in linear time while preserving the accepted inputs.

‎packages/effect/src/cli/Flag.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ export const orElseResult: {
13001300
* import { Schema } from "effect"
13011301
* import { Flag } from "effect/cli"
13021302
*
1303-
* const isEmail = Schema.isPattern(/^[^\s@]+@[^\s@]+\.[^\s@]+$/, {
1303+
* const isEmail = Schema.isPattern(/^[^\s@]+@[^\s@][^\s@.]*\.[^\s@]+$/, {
13041304
* message: "Must be a valid email address"
13051305
* })
13061306
*

‎packages/effect/src/cli/Param.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,7 @@ export const withoutNegation = <A>(self: Param<typeof flagKind, A>): Param<typeo
20722072
* ```ts import.meta.vitest
20732073
* import { Schema } from "effect"
20742074
* import { Param } from "effect/cli"
2075-
* const isEmail = Schema.isPattern(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)
2075+
* const isEmail = Schema.isPattern(/^[^\s@]+@[^\s@][^\s@.]*\.[^\s@]+$/)
20762076
*
20772077
* const Email = Schema.String.pipe(
20782078
* Schema.check(isEmail)

‎packages/effect/src/ui/Validation.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ const pattern = (
106106
}, message]
107107
}
108108

109-
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
109+
// EMAIL and PERMISSIVE_URL exclude dots from the repeated prefix so rejection
110+
// cannot retry each dot as the separator and take quadratic time.
111+
const EMAIL = /^[^\s@]+@[^\s@][^\s@.]*\.[^\s@]+$/
110112
const STRICT_URL = /^https?:\/\/.+/
111-
const PERMISSIVE_URL = /^(https?:\/\/)?\S+\.\S+$/
113+
const PERMISSIVE_URL = /^\S[^\s.]*\.\S+$/
112114

113115
/**
114116
* Foldkit's rules, with its patterns and default messages.

‎packages/effect/test/ui/Validation.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ describe("Validation", () => {
192192
)
193193
})
194194

195+
it("checks email and bare domain shapes in linear time", () => {
196+
const [email] = Rule.email()
197+
const [url] = Rule.url({ requireProtocol: false })
198+
for (const value of ["a@example.com", "a@.b..c"]) assert.isTrue(email(value, {}), value)
199+
for (const value of ["a@b.", "a@b@c.d"]) assert.isFalse(email(value, {}), value)
200+
for (const value of ["example.com", "..a"]) assert.isTrue(url(value, {}), value)
201+
for (const value of ["example", ".a", "a b.c"]) assert.isFalse(url(value, {}), value)
202+
const dots = ".".repeat(40_000)
203+
const start = performance.now()
204+
assert.isFalse(email("a@" + dots + "@", {}))
205+
assert.isFalse(url(dots + " ", {}))
206+
assert.isTrue(performance.now() - start < 500)
207+
})
208+
195209
it("shows a field by its mode, and every field after a submit", () => {
196210
const shown = (mode: Validation.Mode) => {
197211
const Form = Validation.make({

0 commit comments

Comments
 (0)