Skip to content
File

Blob: fuzzilli/worker-full.js

javascript194 lines
1// Enhanced REPRL worker script with all API bindings exposed
2import { default as Stdin } from 'workerd:stdin';
3import crypto from 'crypto';
4import * as fs from 'node:fs';
5import { Buffer } from 'node:buffer';
6import {
7 ok,
8 match,
9 rejects,
10 strictEqual,
11 throws,
12 deepStrictEqual,
13 notStrictEqual,
14} from 'node:assert';
15import { mock } from 'node:test';
16import { Writable, Readable, Transform } from 'node:stream';
17import { text } from 'node:stream/consumers';
18import { pipeline } from 'node:stream/promises';
19import { StringDecoder } from 'node:string_decoder';
20import { EventEmitter } from 'node:events';
21import { env } from 'node:process';
22import zlib from 'node:zlib';
23import { AsyncLocalStorage } from 'node:async_hooks';
24import {
25 channel,
26 hasSubscribers,
27 subscribe,
28 unsubscribe,
29 tracingChannel,
30} from 'node:diagnostics_channel';
31import dns from 'node:dns';
32import path from 'node:path';
33 
34// Expose all APIs to the global scope for fuzzing
35export default {
36 async fetch(request, env, ctx) {
37 return new Response('REPRL mode active');
38 },
39 
40 async test(request, env, ctx) {
41 // fuzzilli is already globally available when compiled with WORKERD_FUZZILLI
42 // Expose bindings
43 //globalThis.CONSUMER_FETCH = env.CONSUMER.fetch;
44 
45 globalThis.env = env;
46 process = undefined;
47 globalThis.process = undefined;
48 
49 // used for scheduler.await
50 globalThis.scheduler = scheduler;
51 
52 // zlib
53 globalThis.zlib = zlib;
54 
55 // Expose Web Crypto API
56 globalThis.crypto = crypto;
57 
58 globalThis.AsyncLocalStorage = AsyncLocalStorage;
59 
60 //path
61 globalThis.path = path;
62 
63 // dns
64 globalThis.dns = dns;
65 
66 // diagnostics
67 globalThis.channel = channel;
68 globalThis.hasSubscribers = hasSubscribers;
69 globalThis.subscribe = subscribe;
70 globalThis.unsubscribe = unsubscribe;
71 globalThis.tracingChannel = tracingChannel;
72 
73 // Expose Web Standards
74 globalThis.Response = Response;
75 globalThis.Request = Request;
76 globalThis.Headers = Headers;
77 globalThis.URL = URL;
78 globalThis.URLPattern = URLPattern;
79 globalThis.URLSearchParams = URLSearchParams;
80 globalThis.TextEncoder = TextEncoder;
81 globalThis.TextDecoder = TextDecoder;
82 
83 // Expose streams
84 globalThis.ReadableStream = ReadableStream;
85 globalThis.WritableStream = WritableStream;
86 globalThis.TransformStream = TransformStream;
87 globalThis.ByteLengthQueuingStrategy = ByteLengthQueuingStrategy;
88 globalThis.CountQueuingStrategy = CountQueuingStrategy;
89 
90 // Expose compression/decompression streams
91 globalThis.CompressionStream = CompressionStream;
92 globalThis.DecompressionStream = DecompressionStream;
93 
94 // Expose text encoding/decoding streams
95 globalThis.TextEncoderStream = TextEncoderStream;
96 globalThis.TextDecoderStream = TextDecoderStream;
97 
98 // Expose identity/fixed-length streams
99 globalThis.IdentityTransformStream = IdentityTransformStream;
100 globalThis.FixedLengthStream = FixedLengthStream;
101 
102 // Expose fetch API
103 globalThis.fetch = fetch;
104 
105 // Expose WebSocket and EventSource APIs
106 globalThis.WebSocket = WebSocket;
107 globalThis.EventSource = EventSource;
108 
109 // Expose Encoding APIs
110 globalThis.atob = atob;
111 globalThis.btoa = btoa;
112 
113 // Expose Blob and FormData APIs
114 globalThis.Blob = Blob;
115 globalThis.File = File;
116 globalThis.FormData = FormData;
117 
118 // Expose HTML Rewriter
119 globalThis.HTMLRewriter = HTMLRewriter;
120 
121 // Expose MessageChannel APIs
122 globalThis.MessageChannel = MessageChannel;
123 globalThis.MessagePort = MessagePort;
124 
125 // Expose AbortController for fetch
126 globalThis.AbortController = AbortController;
127 globalThis.AbortSignal = AbortSignal;
128 
129 // Expose Node.js APIs
130 globalThis.fs = fs;
131 globalThis.Buffer = Buffer;
132 globalThis.Writable = Writable;
133 globalThis.Readable = Readable;
134 globalThis.Transform = Transform;
135 globalThis.EventEmitter = EventEmitter;
136 globalThis.StringDecoder = StringDecoder;
137 
138 // Expose testing APIs
139 globalThis.ok = ok;
140 globalThis.match = match;
141 globalThis.rejects = rejects;
142 globalThis.throws = throws;
143 globalThis.strictEqual = strictEqual;
144 globalThis.deepStrictEqual = deepStrictEqual;
145 globalThis.notStrictEqual = notStrictEqual;
146 globalThis.mock = mock;
147 
148 // Mock Cloudflare APIs for fuzzing
149 globalThis.MOCK_KV = {
150 get: (key, options) =>
151 Promise.resolve(
152 options?.type === 'json' ? { test: 'value' } : 'test-value'
153 ),
154 put: (key, value, options) => Promise.resolve(),
155 delete: (key) => Promise.resolve(),
156 list: (options) =>
157 Promise.resolve({ keys: [{ name: 'key1' }], list_complete: true }),
158 };
159 
160 globalThis.MOCK_D1 = {
161 prepare: (query) => ({
162 bind: (...params) => ({
163 first: () => Promise.resolve({ id: 1, name: 'test' }),
164 all: () => Promise.resolve({ results: [{ id: 1 }], meta: {} }),
165 run: () => Promise.resolve({ success: true, meta: { changes: 1 } }),
166 }),
167 }),
168 batch: (stmts) => Promise.resolve(stmts.map(() => ({ success: true }))),
169 };
170 
171 globalThis.MOCK_R2 = {
172 get: (key) =>
173 Promise.resolve({
174 body: new ReadableStream(),
175 text: () => Promise.resolve('content'),
176 json: () => Promise.resolve({ data: 'test' }),
177 arrayBuffer: () => Promise.resolve(new ArrayBuffer(8)),
178 }),
179 put: (key, value, options) => Promise.resolve({ etag: 'test-etag' }),
180 delete: (keys) =>
181 Promise.resolve({
182 deleted: Array.isArray(keys)
183 ? keys.map((k) => ({ key: k }))
184 : [{ key: keys }],
185 }),
186 list: (options) =>
187 Promise.resolve({ objects: [{ key: 'test.txt', size: 100 }] }),
188 };
189 
190 // Enter the REPRL loop
191 Stdin.reprl();
192 },
193};