-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: support allow and deny config of validateEmail rule #2661
Open
e-moran
wants to merge
3
commits into
main
Choose a base branch
from
eoin/email-validation-allow-deny
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file modified
BIN
+1.13 KB
(100%)
analyze-wasm/wasm/arcjet_analyze_js_req.component.core.wasm
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ import type { | |
DetectedSensitiveInfoEntity, | ||
SensitiveInfoEntity, | ||
BotConfig, | ||
EmailValidationConfig, | ||
} from "@arcjet/analyze"; | ||
import * as duration from "@arcjet/duration"; | ||
import ArcjetHeaders from "@arcjet/headers"; | ||
|
@@ -461,6 +462,8 @@ const validateEmailOptions = createValidator({ | |
validations: [ | ||
{ key: "mode", required: false, validate: validateMode }, | ||
{ key: "block", required: false, validate: validateEmailTypes }, | ||
{ key: "allow", required: false, validate: validateEmailTypes }, | ||
{ key: "deny", required: false, validate: validateEmailTypes }, | ||
{ | ||
key: "requireTopLevelDomain", | ||
required: false, | ||
|
@@ -521,13 +524,39 @@ type BotOptionsDeny = { | |
|
||
export type BotOptions = BotOptionsAllow | BotOptionsDeny; | ||
|
||
export type EmailOptions = { | ||
export type EmailOptionsAllow = { | ||
mode?: ArcjetMode; | ||
block?: ArcjetEmailType[]; | ||
allow: ArcjetEmailType[]; | ||
block?: never; | ||
deny?: never; | ||
requireTopLevelDomain?: boolean; | ||
allowDomainLiteral?: boolean; | ||
}; | ||
|
||
export type EmailOptionsDeny = { | ||
mode?: ArcjetMode; | ||
allow?: never; | ||
block?: never; | ||
deny: ArcjetEmailType[]; | ||
requireTopLevelDomain?: boolean; | ||
allowDomainLiteral?: boolean; | ||
}; | ||
|
||
type EmailOptionsBlock = { | ||
mode?: ArcjetMode; | ||
allow?: never; | ||
/** @deprecated use `deny` instead */ | ||
block: ArcjetEmailType[]; | ||
deny?: never; | ||
requireTopLevelDomain?: boolean; | ||
allowDomainLiteral?: boolean; | ||
}; | ||
|
||
export type EmailOptions = | ||
| EmailOptionsAllow | ||
| EmailOptionsDeny | ||
| EmailOptionsBlock; | ||
|
||
type DetectSensitiveInfoEntities<T> = ( | ||
tokens: string[], | ||
) => Array<ArcjetSensitiveInfoType | T | undefined>; | ||
|
@@ -944,24 +973,94 @@ export function validateEmail( | |
options: EmailOptions, | ||
): Primitive<{ email: string }> { | ||
validateEmailOptions(options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You didn't update this validator |
||
|
||
const mode = options.mode === "LIVE" ? "LIVE" : "DRY_RUN"; | ||
const block = options.block ?? []; | ||
if ( | ||
typeof options.allow !== "undefined" && | ||
typeof options.deny !== "undefined" | ||
) { | ||
throw new Error( | ||
"`validateEmail` options error: `allow` and `deny` cannot be provided together", | ||
); | ||
} | ||
if ( | ||
typeof options.allow !== "undefined" && | ||
typeof options.block !== "undefined" | ||
) { | ||
throw new Error( | ||
"`validateEmail` options error: `allow` and `block` cannot be provided together", | ||
); | ||
} | ||
if ( | ||
typeof options.deny !== "undefined" && | ||
typeof options.block !== "undefined" | ||
) { | ||
throw new Error( | ||
"`validateEmail` options error: `deny` and `block` cannot be provided together, `block` is now deprecated so `deny` should be preferred.", | ||
); | ||
} | ||
if ( | ||
typeof options.allow === "undefined" && | ||
typeof options.deny === "undefined" && | ||
typeof options.block === "undefined" | ||
) { | ||
throw new Error( | ||
"`validateEmail` options error: either `allow` or `deny` must be specified", | ||
); | ||
} | ||
const allow = options.allow ?? []; | ||
const deny = options.deny ?? options.block ?? []; | ||
const requireTopLevelDomain = options.requireTopLevelDomain ?? true; | ||
const allowDomainLiteral = options.allowDomainLiteral ?? false; | ||
|
||
const emailOpts = { | ||
requireTopLevelDomain, | ||
allowDomainLiteral, | ||
blockedEmails: block, | ||
let config: EmailValidationConfig = { | ||
tag: "deny-email-validation-config", | ||
val: { | ||
requireTopLevelDomain, | ||
allowDomainLiteral, | ||
deny: [], | ||
}, | ||
}; | ||
|
||
if (typeof options.allow !== "undefined") { | ||
config = { | ||
tag: "allow-email-validation-config", | ||
val: { | ||
requireTopLevelDomain, | ||
allowDomainLiteral, | ||
allow: options.allow, | ||
}, | ||
}; | ||
} | ||
|
||
if (typeof options.deny !== "undefined") { | ||
config = { | ||
tag: "deny-email-validation-config", | ||
val: { | ||
requireTopLevelDomain, | ||
allowDomainLiteral, | ||
deny: options.deny, | ||
}, | ||
}; | ||
} | ||
|
||
if (typeof options.block !== "undefined") { | ||
config = { | ||
tag: "deny-email-validation-config", | ||
val: { | ||
requireTopLevelDomain, | ||
allowDomainLiteral, | ||
deny: options.block, | ||
}, | ||
}; | ||
} | ||
|
||
return [ | ||
<ArcjetEmailRule<{ email: string }>>{ | ||
type: "EMAIL", | ||
priority: Priority.EmailValidation, | ||
mode, | ||
block, | ||
allow, | ||
deny, | ||
requireTopLevelDomain, | ||
allowDomainLiteral, | ||
|
||
|
@@ -979,7 +1078,7 @@ export function validateEmail( | |
context: ArcjetContext, | ||
{ email }: ArcjetRequestDetails & { email: string }, | ||
): Promise<ArcjetRuleResult> { | ||
const result = await analyze.isValidEmail(context, email, emailOpts); | ||
const result = await analyze.isValidEmail(context, email, config); | ||
if (result.validity === "valid") { | ||
return new ArcjetRuleResult({ | ||
ttl: 0, | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Block needs to be removed, right?