workers-start 0.10.0 → 0.13.0

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,158 @@
1
+ /**
2
+ * Rate limiting on the Workers rate limiting binding, with one answer for
3
+ * every app: the client is keyed by a salted hash of its IP (never the raw
4
+ * address) or by an API key the app names, a missing binding fails open and
5
+ * says so once per isolate, and a limited request gets a 429 problem
6
+ * (RFC 9457) with `Retry-After`, the IETF `RateLimit-Policy` and `RateLimit`
7
+ * headers, and `cache-control: no-store`.
8
+ *
9
+ * The binding counts per Cloudflare location over 10 or 60 seconds. Quotas
10
+ * over hours or days (a plan's daily allowance) are product rules, not abuse
11
+ * limits; keep those in the app's database.
12
+ */
13
+
14
+ /** The `ratelimits` binding, structurally. */
15
+ export interface RateLimitBinding {
16
+ limit(options: { key: string }): Promise<{ success: boolean }>;
17
+ }
18
+
19
+ export interface RateLimitPolicy {
20
+ /** Names the limit in the headers, such as `api` or `join`. */
21
+ name: string;
22
+ /** Requests allowed per period. Must match the binding's wrangler config. */
23
+ limit: number;
24
+ /** The binding's period: 10 or 60 seconds. */
25
+ periodSeconds: 10 | 60;
26
+ }
27
+
28
+ const encoder = new TextEncoder();
29
+
30
+ /**
31
+ * A stable, anonymous key for the client: the SHA-256 of `salt` and the
32
+ * client IP (`cf-connecting-ip`), hex, 32 characters. Without an IP (a
33
+ * request from a test or a service binding) the key is `anon`.
34
+ */
35
+ export async function clientKey(
36
+ request: Request,
37
+ { salt = "" }: { salt?: string } = {},
38
+ ): Promise<string> {
39
+ const ip = request.headers.get("cf-connecting-ip")?.trim();
40
+ if (!ip) return "anon";
41
+ const digest = await crypto.subtle.digest(
42
+ "SHA-256",
43
+ encoder.encode(`${salt}\n${ip}`),
44
+ );
45
+ return [...new Uint8Array(digest).slice(0, 16)]
46
+ .map((b) => b.toString(16).padStart(2, "0"))
47
+ .join("");
48
+ }
49
+
50
+ const reported = new WeakSet<object>();
51
+ let reportedUnbound = false;
52
+
53
+ /**
54
+ * Whether the request may go ahead. A missing binding allows it (failing
55
+ * closed would take the site down over a config slip) and calls `onUnbound`
56
+ * once per isolate so the slip is seen. So does a binding that throws.
57
+ */
58
+ export async function checkRateLimit(
59
+ binding: RateLimitBinding | undefined | null,
60
+ key: string,
61
+ { onUnbound }: { onUnbound?: (error?: unknown) => void } = {},
62
+ ): Promise<boolean> {
63
+ if (!binding) {
64
+ if (!reportedUnbound) {
65
+ reportedUnbound = true;
66
+ onUnbound?.();
67
+ }
68
+ return true;
69
+ }
70
+ try {
71
+ return (await binding.limit({ key })).success;
72
+ } catch (error) {
73
+ if (!reported.has(binding)) {
74
+ reported.add(binding);
75
+ onUnbound?.(error);
76
+ }
77
+ return true;
78
+ }
79
+ }
80
+
81
+ /** The IETF rate limit headers for a limited response, with `Retry-After`. */
82
+ export function rateLimitHeaders(
83
+ policy: RateLimitPolicy,
84
+ ): Record<string, string> {
85
+ const name = JSON.stringify(policy.name);
86
+ return {
87
+ "retry-after": String(policy.periodSeconds),
88
+ "ratelimit-policy": `${name};q=${policy.limit};w=${policy.periodSeconds}`,
89
+ ratelimit: `${name};r=0;t=${policy.periodSeconds}`,
90
+ };
91
+ }
92
+
93
+ /** The 429 problem for `policy`. `detail` replaces the default sentence. */
94
+ export function tooManyRequests(
95
+ policy: RateLimitPolicy,
96
+ { detail }: { detail?: string } = {},
97
+ ): Response {
98
+ const body = {
99
+ type: "about:blank",
100
+ title: "Too Many Requests",
101
+ status: 429,
102
+ code: "rate_limited",
103
+ detail:
104
+ detail ??
105
+ `Too many requests. Try again in ${policy.periodSeconds} seconds.`,
106
+ };
107
+ return new Response(JSON.stringify(body), {
108
+ status: 429,
109
+ headers: {
110
+ "content-type": "application/problem+json",
111
+ "cache-control": "no-store",
112
+ ...rateLimitHeaders(policy),
113
+ },
114
+ });
115
+ }
116
+
117
+ export interface RateLimitOptions {
118
+ /** The key to count by. Defaults to `clientKey(request, { salt })`. */
119
+ key?: string;
120
+ /** Salt for the default IP key, such as a secret from the env. */
121
+ salt?: string;
122
+ /** Let verified bots (`request.cf.verifiedBotCategory`) through uncounted. */
123
+ allowVerifiedBots?: boolean;
124
+ /** Called once per isolate when the binding is missing or fails. */
125
+ onUnbound?: (error?: unknown) => void;
126
+ /** Detail for the 429. */
127
+ detail?: string;
128
+ }
129
+
130
+ /**
131
+ * Counts the request against `policy` and returns the 429 to send, or null
132
+ * to go ahead.
133
+ *
134
+ * const limited = await rateLimit(request, env.API_LIMIT, API_POLICY);
135
+ * if (limited) return limited;
136
+ */
137
+ export async function rateLimit(
138
+ request: Request,
139
+ binding: RateLimitBinding | undefined | null,
140
+ policy: RateLimitPolicy,
141
+ options: RateLimitOptions = {},
142
+ ): Promise<Response | null> {
143
+ if (options.allowVerifiedBots) {
144
+ const cf = (request as { cf?: { verifiedBotCategory?: string } }).cf;
145
+ if (cf?.verifiedBotCategory) return null;
146
+ }
147
+ const key =
148
+ options.key ??
149
+ (await clientKey(request, options.salt ? { salt: options.salt } : {}));
150
+ const allowed = await checkRateLimit(
151
+ binding,
152
+ `${policy.name}:${key}`,
153
+ options.onUnbound ? { onUnbound: options.onUnbound } : {},
154
+ );
155
+ return allowed
156
+ ? null
157
+ : tooManyRequests(policy, options.detail ? { detail: options.detail } : {});
158
+ }
package/src/sentry.ts CHANGED
@@ -19,6 +19,8 @@
19
19
  /** The part of a Sentry event the filters read. */
20
20
  export interface SentryEventLike {
21
21
  request?: { url?: string };
22
+ message?: string;
23
+ logentry?: { message?: string };
22
24
  exception?: { values?: { value?: string; type?: string }[] };
23
25
  }
24
26
 
@@ -46,10 +48,10 @@ export function dropLocalEvents<T extends SentryEventLike>(event: T): T | null {
46
48
  }
47
49
 
48
50
  /** Browser wording for a request that never left the device: Chrome, Safari, Firefox. */
49
- export const NETWORK_ERROR = /failed to fetch|load failed|networkerror/i;
51
+ export const NETWORK_ERROR = /failed to fetch|load failed|network ?error/i;
50
52
  /** Errors thrown by extension content scripts and injected wallets. */
51
53
  export const EXTENSION_ERROR =
52
- /func \w+ not found|tronlink|trap returned falsish|origin not allowed|object captured as promise rejection with keys: code, (data, )?message/i;
54
+ /func \w+ not found|tronlink|trap returned falsish|origin not allowed|object captured as promise rejection with keys: (?=[^\n]*\bcode\b)(?=[^\n]*\bmessage\b)/i;
53
55
  /** A person declining a wallet prompt. */
54
56
  export const USER_REJECTION = /user rejected|user denied|rejected the request/i;
55
57
 
@@ -62,17 +64,26 @@ export const BROWSER_DENY_URLS: readonly RegExp[] = [
62
64
  /chrome-extension:\/\//i,
63
65
  /moz-extension:\/\//i,
64
66
  /safari-(web-)?extension:\/\//i,
67
+ /ms-browser-extension:\/\//i,
68
+ /webkit-masked-url:/i,
65
69
  ];
66
70
 
67
- /** Whether a browser event is noise that is not the app's to fix. `extra` adds patterns matched against the message. */
71
+ /** The texts the noise check reads: the exception value, the message and the log entry. */
72
+ function messagesOf(event: SentryEventLike): string[] {
73
+ return [
74
+ event.exception?.values?.[0]?.value,
75
+ event.message,
76
+ event.logentry?.message,
77
+ ].filter((m): m is string => typeof m === "string" && m !== "");
78
+ }
79
+
80
+ /** Whether a browser event is noise that is not the app's to fix. `extra` adds patterns matched against its messages. */
68
81
  export function isBrowserNoise(
69
82
  event: SentryEventLike,
70
83
  extra: readonly RegExp[] = [],
71
84
  ): boolean {
72
- const message = event.exception?.values?.[0]?.value ?? "";
73
- return [NETWORK_ERROR, EXTENSION_ERROR, USER_REJECTION, ...extra].some((re) =>
74
- re.test(message),
75
- );
85
+ const patterns = [NETWORK_ERROR, EXTENSION_ERROR, USER_REJECTION, ...extra];
86
+ return messagesOf(event).some((m) => patterns.some((re) => re.test(m)));
76
87
  }
77
88
 
78
89
  /** A DSN worth using: an `https://` URL. Anything else (empty, `off`, a placeholder) turns Sentry off. */
@@ -95,7 +106,7 @@ function chain(...steps: (BeforeSend | undefined)[]): BeforeSend {
95
106
  interface CommonOptions {
96
107
  /** Overrides the DSN read from the env. */
97
108
  dsn?: string | undefined;
98
- /** Default 0: no tracing. */
109
+ /** Left unset by default: an unset rate means no tracing at all, where 0 still creates unsampled spans. */
99
110
  tracesSampleRate?: number;
100
111
  /** Runs after the built-in filters. */
101
112
  beforeSend?: BeforeSend;
@@ -108,7 +119,7 @@ interface CommonOptions {
108
119
  export interface WorkerSentryOptions {
109
120
  dsn: string | undefined;
110
121
  enabled: boolean;
111
- tracesSampleRate: number;
122
+ tracesSampleRate?: number;
112
123
  beforeSend: BeforeSend;
113
124
  beforeSendTransaction: BeforeSend;
114
125
  initialScope?: { tags: Record<string, string> };
@@ -129,10 +140,11 @@ export function workerSentryOptions(
129
140
  const out: WorkerSentryOptions = {
130
141
  dsn,
131
142
  enabled: dsn !== undefined,
132
- tracesSampleRate: options.tracesSampleRate ?? 0,
133
143
  beforeSend: chain(dropLocalEvents, options.beforeSend),
134
144
  beforeSendTransaction: dropLocalEvents,
135
145
  };
146
+ if (options.tracesSampleRate !== undefined)
147
+ out.tracesSampleRate = options.tracesSampleRate;
136
148
  if (options.tags) out.initialScope = { tags: options.tags };
137
149
  if (options.release) out.release = options.release;
138
150
  if (options.environment) out.environment = options.environment;
@@ -155,6 +167,8 @@ export function browserSentryOptions(
155
167
  denyUrls?: readonly RegExp[];
156
168
  /** More messages that are noise. */
157
169
  noise?: readonly RegExp[];
170
+ /** Events to keep even when they read like noise, such as handled captures from a wallet flow. */
171
+ keep?: (event: AnyEvent) => boolean;
158
172
  } = {},
159
173
  ): BrowserSentryOptions {
160
174
  const dsn = validDsn(options.dsn);
@@ -162,15 +176,16 @@ export function browserSentryOptions(
162
176
  const out: BrowserSentryOptions = {
163
177
  dsn,
164
178
  enabled: dsn !== undefined && options.enabled !== false,
165
- tracesSampleRate: options.tracesSampleRate ?? 0,
166
179
  denyUrls: [...BROWSER_DENY_URLS, ...(options.denyUrls ?? [])],
167
- beforeSend: chain(
168
- (event) =>
169
- isLocalEvent(event) || isBrowserNoise(event, noise) ? null : event,
170
- options.beforeSend,
171
- ),
180
+ beforeSend: chain((event) => {
181
+ if (isLocalEvent(event)) return null;
182
+ if (options.keep?.(event)) return event;
183
+ return isBrowserNoise(event, noise) ? null : event;
184
+ }, options.beforeSend),
172
185
  beforeSendTransaction: dropLocalEvents,
173
186
  };
187
+ if (options.tracesSampleRate !== undefined)
188
+ out.tracesSampleRate = options.tracesSampleRate;
174
189
  if (options.tags) out.initialScope = { tags: options.tags };
175
190
  if (options.release) out.release = options.release;
176
191
  if (options.environment) out.environment = options.environment;