-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
honeypot.ts
174 lines (147 loc) · 4.76 KB
/
honeypot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import CryptoJS from "crypto-js";
export interface HoneypotInputProps {
/**
* The name expected to be used by the honeypot input field.
*/
nameFieldName: string;
/**
* The name expected to be used by the honeypot valid from input field.
*/
validFromFieldName: string | null;
/**
* The encrypted value of the current timestamp.
*/
encryptedValidFrom: string;
}
export interface HoneypotConfig {
/**
* Enable randomization of the name field name, this way the honeypot field
* name will be different for each request.
*/
randomizeNameFieldName?: boolean;
/**
* The name of the field that will be used for the honeypot input.
*/
nameFieldName?: string;
/**
* The name of the field that will be used for the honeypot valid from input.
*/
validFromFieldName?: string | null;
/**
* The seed used for the encryption of the valid from timestamp.
*/
encryptionSeed?: string;
}
/**
* The error thrown when the Honeypot fails, meaning some automated bot filled
* the form and the request is probably spam.
*/
export class SpamError extends Error {
readonly name = "SpamError";
}
const DEFAULT_NAME_FIELD_NAME = "name__confirm";
const DEFAULT_VALID_FROM_FIELD_NAME = "from__confirm";
/**
* Module used to implement a Honeypot.
* A Honeypot is a visually hidden input that is used to detect spam bots. This
* field is expected to be left empty by users because they don't see it, but
* bots will fill it falling in the honeypot trap.
*/
export class Honeypot {
private generatedEncryptionSeed = this.randomValue();
protected config: HoneypotConfig;
constructor(config: HoneypotConfig = {}) {
this.config = config;
}
/**
* Get the HoneypotInputProps to be used in your forms.
* @param {Object} options The options for the input props.
* @param {number} options.validFromTimestamp Since when the timestamp is valid.
* @returns {HoneypotInputProps} The props to be used in the form.
*/
public getInputProps({
validFromTimestamp = Date.now(),
} = {}): HoneypotInputProps {
return {
nameFieldName: this.nameFieldName,
validFromFieldName: this.validFromFieldName,
encryptedValidFrom: this.encrypt(validFromTimestamp.toString()),
};
}
public check(formData: FormData) {
let nameFieldName = this.config.nameFieldName ?? DEFAULT_NAME_FIELD_NAME;
if (this.config.randomizeNameFieldName) {
let actualName = this.getRandomizedNameFieldName(nameFieldName, formData);
if (actualName) nameFieldName = actualName;
}
if (!this.shouldCheckHoneypot(formData, nameFieldName)) return;
if (!formData.has(nameFieldName)) {
throw new SpamError("Missing honeypot input");
}
let honeypotValue = formData.get(nameFieldName);
if (honeypotValue !== "") throw new SpamError("Honeypot input not empty");
if (!this.validFromFieldName) return;
let validFrom = formData.get(this.validFromFieldName);
if (!validFrom) throw new SpamError("Missing honeypot valid from input");
let time = this.decrypt(validFrom as string);
if (!time) throw new SpamError("Invalid honeypot valid from input");
if (!this.isValidTimeStamp(Number(time))) {
throw new SpamError("Invalid honeypot valid from input");
}
if (this.isFuture(Number(time))) {
throw new SpamError("Honeypot valid from is in future");
}
}
protected get nameFieldName() {
let fieldName = this.config.nameFieldName ?? DEFAULT_NAME_FIELD_NAME;
if (!this.config.randomizeNameFieldName) return fieldName;
return `${fieldName}_${this.randomValue()}`;
}
protected get validFromFieldName() {
if (this.config.validFromFieldName === undefined) {
return DEFAULT_VALID_FROM_FIELD_NAME;
}
return this.config.validFromFieldName;
}
protected get encryptionSeed() {
return this.config.encryptionSeed ?? this.generatedEncryptionSeed;
}
protected getRandomizedNameFieldName(
nameFieldName: string,
formData: FormData,
): string | undefined {
for (let key of formData.keys()) {
if (!key.startsWith(nameFieldName)) continue;
return key;
}
}
protected shouldCheckHoneypot(
formData: FormData,
nameFieldName: string,
): boolean {
return (
formData.has(nameFieldName) ||
Boolean(this.validFromFieldName && formData.has(this.validFromFieldName))
);
}
protected randomValue() {
return CryptoJS.lib.WordArray.random(128 / 8).toString();
}
protected encrypt(value: string) {
return CryptoJS.AES.encrypt(value, this.encryptionSeed).toString();
}
protected decrypt(value: string) {
return CryptoJS.AES.decrypt(value, this.encryptionSeed).toString(
CryptoJS.enc.Utf8,
);
}
protected isFuture(timestamp: number) {
return timestamp > Date.now();
}
protected isValidTimeStamp(timestampp: number) {
if (Number.isNaN(timestampp)) return false;
if (timestampp <= 0) return false;
if (timestampp >= Number.MAX_SAFE_INTEGER) return false;
return true;
}
}