@arcjet/analyze 1.0.0-alpha.21 → 1.0.0-alpha.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ export namespace ArcjetJsReqSensitiveInformationIdentifier {
2
+ export function detect(tokens: Array<string>): Array<SensitiveInfoEntity | undefined>;
3
+ }
4
+ export type SensitiveInfoEntity = SensitiveInfoEntityEmail | SensitiveInfoEntityPhoneNumber | SensitiveInfoEntityIpAddress | SensitiveInfoEntityCreditCardNumber | SensitiveInfoEntityCustom;
5
+ export interface SensitiveInfoEntityEmail {
6
+ tag: 'email',
7
+ }
8
+ export interface SensitiveInfoEntityPhoneNumber {
9
+ tag: 'phone-number',
10
+ }
11
+ export interface SensitiveInfoEntityIpAddress {
12
+ tag: 'ip-address',
13
+ }
14
+ export interface SensitiveInfoEntityCreditCardNumber {
15
+ tag: 'credit-card-number',
16
+ }
17
+ export interface SensitiveInfoEntityCustom {
18
+ tag: 'custom',
19
+ val: string,
20
+ }
package/workerd.d.ts CHANGED
@@ -1,9 +1,22 @@
1
- import type { ArcjetLogger, ArcjetRequestDetails } from "@arcjet/protocol";
2
- import type { EmailValidationConfig, BotDetectionResult, BotType, EmailValidationResult } from "./wasm/arcjet_analyze_js_req.component.js";
1
+ import type { ArcjetLogger } from "@arcjet/protocol";
2
+ import type { EmailValidationConfig, BotDetectionResult, BotType, EmailValidationResult, DetectedSensitiveInfoEntity, SensitiveInfoEntities, SensitiveInfoEntity, SensitiveInfoResult } from "./wasm/arcjet_analyze_js_req.component.js";
3
+ import type { ArcjetJsReqSensitiveInformationIdentifier } from "./wasm/interfaces/arcjet-js-req-sensitive-information-identifier.js";
4
+ type AnalyzeRequest = {
5
+ ip?: string;
6
+ method?: string;
7
+ protocol?: string;
8
+ host?: string;
9
+ path?: string;
10
+ headers?: Record<string, string>;
11
+ cookies?: string;
12
+ query?: string;
13
+ extra?: Record<string, string>;
14
+ };
3
15
  interface AnalyzeContext {
4
16
  log: ArcjetLogger;
5
17
  characteristics: string[];
6
18
  }
19
+ type DetectSensitiveInfoFunction = typeof ArcjetJsReqSensitiveInformationIdentifier.detect;
7
20
  export { type EmailValidationConfig, type BotType,
8
21
  /**
9
22
  * Represents the result of the bot detection.
@@ -17,7 +30,7 @@ export { type EmailValidationConfig, type BotType,
17
30
  * confidence level. `BotType.LikelyNotABot` with a score of 99 means we are
18
31
  * almost certain this request was not a bot.
19
32
  */
20
- type BotDetectionResult, };
33
+ type BotDetectionResult, type DetectedSensitiveInfoEntity, type SensitiveInfoEntity, type DetectSensitiveInfoFunction, };
21
34
  /**
22
35
  * Generate a fingerprint for the client. This is used to identify the client
23
36
  * across multiple requests.
@@ -25,6 +38,7 @@ type BotDetectionResult, };
25
38
  * @param request - The request to fingerprint.
26
39
  * @returns A SHA-256 string fingerprint.
27
40
  */
28
- export declare function generateFingerprint(context: AnalyzeContext, request: Partial<ArcjetRequestDetails>): Promise<string>;
41
+ export declare function generateFingerprint(context: AnalyzeContext, request: AnalyzeRequest): Promise<string>;
29
42
  export declare function isValidEmail(context: AnalyzeContext, candidate: string, options?: EmailValidationConfig): Promise<EmailValidationResult>;
30
43
  export declare function detectBot(context: AnalyzeContext, headers: string, patterns_add: string, patterns_remove: string): Promise<BotDetectionResult>;
44
+ export declare function detectSensitiveInfo(context: AnalyzeContext, candidate: string, entities: SensitiveInfoEntities, contextWindowSize: number, detect?: DetectSensitiveInfoFunction): Promise<SensitiveInfoResult>;
package/workerd.js CHANGED
@@ -22,8 +22,14 @@ async function moduleFromPath(path) {
22
22
  }
23
23
  throw new Error(`Unknown path: ${path}`);
24
24
  }
25
- async function init(context) {
25
+ function noOpDetect() {
26
+ return [];
27
+ }
28
+ async function init(context, detectSensitiveInfo) {
26
29
  const { log } = context;
30
+ if (typeof detectSensitiveInfo !== "function") {
31
+ detectSensitiveInfo = noOpDetect;
32
+ }
27
33
  const coreImports = {
28
34
  "arcjet:js-req/logger": {
29
35
  debug(msg) {
@@ -50,9 +56,13 @@ async function init(context) {
50
56
  return "unknown";
51
57
  },
52
58
  },
59
+ "arcjet:js-req/sensitive-information-identifier": {
60
+ detect: detectSensitiveInfo,
61
+ },
53
62
  };
54
63
  try {
55
- return instantiate(moduleFromPath, coreImports);
64
+ // Await the instantiation to catch the failure
65
+ return await instantiate(moduleFromPath, coreImports);
56
66
  }
57
67
  catch {
58
68
  log.debug("WebAssembly is not supported in this runtime");
@@ -104,5 +114,19 @@ async function detectBot(context, headers, patterns_add, patterns_remove) {
104
114
  };
105
115
  }
106
116
  }
117
+ async function detectSensitiveInfo(context, candidate, entities, contextWindowSize, detect) {
118
+ const analyze = await init(context, detect);
119
+ if (typeof analyze !== "undefined") {
120
+ const skipCustomDetect = typeof detect !== "function";
121
+ return analyze.detectSensitiveInfo(candidate, {
122
+ entities,
123
+ contextWindowSize,
124
+ skipCustomDetect,
125
+ });
126
+ }
127
+ else {
128
+ throw new Error("SENSITIVE_INFO rule failed to run because Wasm is not supported in this environment.");
129
+ }
130
+ }
107
131
 
108
- export { detectBot, generateFingerprint, isValidEmail };
132
+ export { detectBot, detectSensitiveInfo, generateFingerprint, isValidEmail };
package/workerd.ts CHANGED
@@ -1,18 +1,35 @@
1
- import type { ArcjetLogger, ArcjetRequestDetails } from "@arcjet/protocol";
1
+ import type { ArcjetLogger } from "@arcjet/protocol";
2
2
 
3
- import * as core from "./wasm/arcjet_analyze_js_req.component.js";
3
+ import { instantiate } from "./wasm/arcjet_analyze_js_req.component.js";
4
4
  import type {
5
5
  ImportObject,
6
6
  EmailValidationConfig,
7
7
  BotDetectionResult,
8
8
  BotType,
9
9
  EmailValidationResult,
10
+ DetectedSensitiveInfoEntity,
11
+ SensitiveInfoEntities,
12
+ SensitiveInfoEntity,
13
+ SensitiveInfoResult,
10
14
  } from "./wasm/arcjet_analyze_js_req.component.js";
15
+ import type { ArcjetJsReqSensitiveInformationIdentifier } from "./wasm/interfaces/arcjet-js-req-sensitive-information-identifier.js";
11
16
 
12
17
  import componentCoreWasm from "./wasm/arcjet_analyze_js_req.component.core.wasm";
13
18
  import componentCore2Wasm from "./wasm/arcjet_analyze_js_req.component.core2.wasm";
14
19
  import componentCore3Wasm from "./wasm/arcjet_analyze_js_req.component.core3.wasm";
15
20
 
21
+ type AnalyzeRequest = {
22
+ ip?: string;
23
+ method?: string;
24
+ protocol?: string;
25
+ host?: string;
26
+ path?: string;
27
+ headers?: Record<string, string>;
28
+ cookies?: string;
29
+ query?: string;
30
+ extra?: Record<string, string>;
31
+ };
32
+
16
33
  const FREE_EMAIL_PROVIDERS = [
17
34
  "gmail.com",
18
35
  "yahoo.com",
@@ -26,6 +43,9 @@ interface AnalyzeContext {
26
43
  characteristics: string[];
27
44
  }
28
45
 
46
+ type DetectSensitiveInfoFunction =
47
+ typeof ArcjetJsReqSensitiveInformationIdentifier.detect;
48
+
29
49
  async function moduleFromPath(path: string): Promise<WebAssembly.Module> {
30
50
  if (path === "arcjet_analyze_js_req.component.core.wasm") {
31
51
  return componentCoreWasm;
@@ -40,9 +60,20 @@ async function moduleFromPath(path: string): Promise<WebAssembly.Module> {
40
60
  throw new Error(`Unknown path: ${path}`);
41
61
  }
42
62
 
43
- async function init(context: AnalyzeContext) {
63
+ function noOpDetect(): SensitiveInfoEntity[] {
64
+ return [];
65
+ }
66
+
67
+ async function init(
68
+ context: AnalyzeContext,
69
+ detectSensitiveInfo?: DetectSensitiveInfoFunction,
70
+ ) {
44
71
  const { log } = context;
45
72
 
73
+ if (typeof detectSensitiveInfo !== "function") {
74
+ detectSensitiveInfo = noOpDetect;
75
+ }
76
+
46
77
  const coreImports: ImportObject = {
47
78
  "arcjet:js-req/logger": {
48
79
  debug(msg) {
@@ -69,10 +100,14 @@ async function init(context: AnalyzeContext) {
69
100
  return "unknown";
70
101
  },
71
102
  },
103
+ "arcjet:js-req/sensitive-information-identifier": {
104
+ detect: detectSensitiveInfo,
105
+ },
72
106
  };
73
107
 
74
108
  try {
75
- return core.instantiate(moduleFromPath, coreImports);
109
+ // Await the instantiation to catch the failure
110
+ return await instantiate(moduleFromPath, coreImports);
76
111
  } catch {
77
112
  log.debug("WebAssembly is not supported in this runtime");
78
113
  }
@@ -94,6 +129,9 @@ export {
94
129
  * almost certain this request was not a bot.
95
130
  */
96
131
  type BotDetectionResult,
132
+ type DetectedSensitiveInfoEntity,
133
+ type SensitiveInfoEntity,
134
+ type DetectSensitiveInfoFunction,
97
135
  };
98
136
 
99
137
  /**
@@ -105,7 +143,7 @@ export {
105
143
  */
106
144
  export async function generateFingerprint(
107
145
  context: AnalyzeContext,
108
- request: Partial<ArcjetRequestDetails>,
146
+ request: AnalyzeRequest,
109
147
  ): Promise<string> {
110
148
  const analyze = await init(context);
111
149
 
@@ -161,3 +199,26 @@ export async function detectBot(
161
199
  };
162
200
  }
163
201
  }
202
+
203
+ export async function detectSensitiveInfo(
204
+ context: AnalyzeContext,
205
+ candidate: string,
206
+ entities: SensitiveInfoEntities,
207
+ contextWindowSize: number,
208
+ detect?: DetectSensitiveInfoFunction,
209
+ ): Promise<SensitiveInfoResult> {
210
+ const analyze = await init(context, detect);
211
+
212
+ if (typeof analyze !== "undefined") {
213
+ const skipCustomDetect = typeof detect !== "function";
214
+ return analyze.detectSensitiveInfo(candidate, {
215
+ entities,
216
+ contextWindowSize,
217
+ skipCustomDetect,
218
+ });
219
+ } else {
220
+ throw new Error(
221
+ "SENSITIVE_INFO rule failed to run because Wasm is not supported in this environment.",
222
+ );
223
+ }
224
+ }