remix-utils
Advanced tools
Comparing version 7.0.0-pre.1 to 7.0.0-pre.2
@@ -1,2 +0,16 @@ | ||
/// <reference types="react" /> | ||
export declare function HoneypotInputs(): JSX.Element; | ||
import * as React from "react"; | ||
import type { HoneypotInputProps } from "../server/honeypot.js"; | ||
type HoneypotContextType = Partial<HoneypotInputProps>; | ||
export declare function HoneypotInputs({ | ||
label, | ||
}: { | ||
label?: string; | ||
}): JSX.Element; | ||
export type HoneypotProviderProps = HoneypotContextType & { | ||
children: React.ReactNode; | ||
}; | ||
export declare function HoneypotProvider({ | ||
children, | ||
...context | ||
}: HoneypotProviderProps): JSX.Element; | ||
export {}; |
@@ -0,42 +1,50 @@ | ||
/* eslint-disable jsx-a11y/autocomplete-valid */ | ||
import * as React from "react"; | ||
import { useRouteLoaderData } from "@remix-run/react"; | ||
export function HoneypotInputs() { | ||
let rootLoaderData = useRouteLoaderData("root"); | ||
if (!rootLoaderData) throw new Error("Missing loader data from root"); | ||
if (!rootLoaderData.nameFieldName) { | ||
throw new Error("Missing Honeypot's nameFieldName on root loader data"); | ||
} | ||
if (!rootLoaderData.validFromFieldName) { | ||
throw new Error( | ||
"Missing Honeypot's validFromFieldName on root loader data" | ||
); | ||
} | ||
if (!rootLoaderData.encryptedValidFrom) { | ||
throw new Error( | ||
"Missing Honeypot's encryptedValidFrom on root loader data" | ||
); | ||
} | ||
const HoneypotContext = React.createContext({}); | ||
export function HoneypotInputs({ label = "Please leave this field blank" }) { | ||
let context = React.useContext(HoneypotContext); | ||
let { | ||
nameFieldName = "name__confirm", | ||
validFromFieldName = "from__confirm", | ||
encryptedValidFrom, | ||
} = context; | ||
return React.createElement( | ||
"div", | ||
{ | ||
id: `${rootLoaderData.nameFieldName}_wrap`, | ||
id: `${nameFieldName}_wrap`, | ||
style: { display: "none" }, | ||
"aria-hidden": "true", | ||
}, | ||
React.createElement("label", { htmlFor: nameFieldName }, label), | ||
React.createElement("input", { | ||
id: rootLoaderData.nameFieldName, | ||
name: rootLoaderData.nameFieldName, | ||
id: nameFieldName, | ||
name: nameFieldName, | ||
type: "text", | ||
defaultValue: "", | ||
autoComplete: "off", | ||
autoComplete: "nope", | ||
tabIndex: -1, | ||
}), | ||
React.createElement("input", { | ||
name: rootLoaderData.validFromFieldName, | ||
type: "text", | ||
value: rootLoaderData.encryptedValidFrom, | ||
autoComplete: "off", | ||
tabIndex: -1, | ||
}) | ||
validFromFieldName && encryptedValidFrom | ||
? React.createElement( | ||
React.Fragment, | ||
null, | ||
React.createElement("label", { htmlFor: validFromFieldName }, label), | ||
React.createElement("input", { | ||
name: validFromFieldName, | ||
type: "text", | ||
value: encryptedValidFrom, | ||
readOnly: true, | ||
autoComplete: "off", | ||
tabIndex: -1, | ||
}) | ||
) | ||
: null | ||
); | ||
} | ||
export function HoneypotProvider({ children, ...context }) { | ||
return React.createElement( | ||
HoneypotContext.Provider, | ||
{ value: context }, | ||
children | ||
); | ||
} |
@@ -10,3 +10,2 @@ export interface HoneypotInputProps { | ||
validFromFieldName?: string; | ||
validFromTimestamp?: number; | ||
encryptionSeed?: string; | ||
@@ -19,7 +18,10 @@ } | ||
constructor(config?: HonetpotConfig); | ||
getInputProps(): HoneypotInputProps; | ||
getInputProps({ | ||
validFromTimestamp, | ||
}?: { | ||
validFromTimestamp?: number | undefined; | ||
}): HoneypotInputProps; | ||
check(formData: FormData): void; | ||
protected get nameFieldName(): string; | ||
protected get validFromFieldName(): string; | ||
protected get validFromTimestamp(): number; | ||
protected get encryptionSeed(): string; | ||
@@ -26,0 +28,0 @@ protected getRandomizedNameFieldName( |
import CryptoJS from "crypto-js"; | ||
export class SpamError extends Error {} | ||
const DEFAULT_NAME_FIELD_NAME = "name__confirm"; | ||
const DEFAULT_VALID_FROM_FIELD_NAME = "from__confirm"; | ||
export class Honeypot { | ||
@@ -9,11 +11,11 @@ config; | ||
} | ||
getInputProps() { | ||
getInputProps({ validFromTimestamp = Date.now() } = {}) { | ||
return { | ||
nameFieldName: this.nameFieldName, | ||
validFromFieldName: this.validFromFieldName, | ||
encryptedValidFrom: this.encrypt(this.validFromTimestamp.toString()), | ||
encryptedValidFrom: this.encrypt(validFromTimestamp.toString()), | ||
}; | ||
} | ||
check(formData) { | ||
let nameFieldName = this.config.nameFieldName ?? "honeypot"; | ||
let nameFieldName = this.config.nameFieldName ?? DEFAULT_NAME_FIELD_NAME; | ||
if (this.config.randomizeNameFieldName) { | ||
@@ -29,3 +31,3 @@ let actualName = this.getRandomizedNameFieldName(nameFieldName, formData); | ||
if (honeypotValue !== "") throw new SpamError("Honeypot input not empty"); | ||
if (!this.config.validFromTimestamp) return; | ||
if (!this.validFromFieldName) return; | ||
let validFrom = formData.get(this.validFromFieldName); | ||
@@ -43,3 +45,3 @@ if (!validFrom) throw new SpamError("Missing honeypot valid from input"); | ||
get nameFieldName() { | ||
let fieldName = this.config.nameFieldName ?? "honeypot"; | ||
let fieldName = this.config.nameFieldName ?? DEFAULT_NAME_FIELD_NAME; | ||
if (!this.config.randomizeNameFieldName) return fieldName; | ||
@@ -49,7 +51,4 @@ return `${fieldName}_${this.randomValue()}`; | ||
get validFromFieldName() { | ||
return this.config.validFromFieldName ?? "honeypot_from"; | ||
return this.config.validFromFieldName ?? DEFAULT_VALID_FROM_FIELD_NAME; | ||
} | ||
get validFromTimestamp() { | ||
return this.config.validFromTimestamp ?? Date.now(); | ||
} | ||
get encryptionSeed() { | ||
@@ -56,0 +55,0 @@ return this.config.encryptionSeed ?? this.generatedEncryptionSeed; |
{ | ||
"name": "remix-utils", | ||
"version": "7.0.0-pre.1", | ||
"version": "7.0.0-pre.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
166964
82
3409