Skip to content

Commit 00ef51e

Browse files
committed
improve base64 decoding in sandbox
1 parent dcc7616 commit 00ef51e

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

internal/worker/sandbox.ts

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Buffer } from "node:buffer";
2+
13
function cleanStack(str?: string) {
24
if (!str) return undefined;
35
return str
@@ -6,19 +8,19 @@ function cleanStack(str?: string) {
68
(line) =>
79
!line.includes(import.meta.url) &&
810
!line.includes("deno_http/00_serve.ts") &&
9-
!line.includes("core/01_core.js")
11+
!line.includes("core/01_core.js"),
1012
)
1113
.join("\n");
1214
}
1315

1416
function serializeError(e: Error) {
1517
return { name: e.name, message: e.message, stack: cleanStack(e.stack) };
16-
};
18+
}
1719

1820
function respondWithError(error: Error) {
1921
return Response.json(
2022
{ error: serializeError(error) },
21-
{ status: 500, headers: { 'Content-Type': 'application/json' } },
23+
{ status: 500, headers: { "Content-Type": "application/json" } },
2224
);
2325
}
2426

@@ -42,25 +44,37 @@ if (payload.command === "fetch") {
4244
try {
4345
const mod = await import(payload.entrypoint);
4446
if (!mod.default) {
45-
return new Response("The app does not provide a default export.", { status: 500 });
47+
return new Response(
48+
"The app does not provide a default export.",
49+
{ status: 500 },
50+
);
4651
}
4752

4853
if (typeof mod.default !== "object") {
49-
return new Response("The app default export must be an object.", { status: 500 });
54+
return new Response(
55+
"The app default export must be an object.",
56+
{ status: 500 },
57+
);
5058
}
5159
if (
5260
!("fetch" in mod.default) ||
5361
typeof mod.default.fetch !== "function"
5462
) {
55-
return new Response("The app default export does not have a fetch method.", { status: 500 });
63+
return new Response(
64+
"The app default export does not have a fetch method.",
65+
{ status: 500 },
66+
);
5667
}
5768

5869
const handler = mod.default.fetch;
5970
// Websocket requests are stateful and should be handled differently
6071
if (req.headers.get("upgrade") === "websocket") {
6172
const resp = await handler(req);
6273
if (!(resp instanceof Response)) {
63-
return new Response("Fetch handler must return a Response object.", { status: 500 });
74+
return new Response(
75+
"Fetch handler must return a Response object.",
76+
{ status: 500 },
77+
);
6478
}
6579

6680
return resp;
@@ -69,13 +83,20 @@ if (payload.command === "fetch") {
6983
const url = new URL(req.url);
7084
const proto = req.headers.get("x-forwarded-proto");
7185
const host = req.headers.get("x-forwarded-host");
72-
const resp = await handler(new Request(`${proto}://${host}${url.pathname}${url.search}`, {
73-
method: req.method,
74-
headers: req.headers,
75-
body: req.body,
76-
}));
86+
const resp = await handler(
87+
new Request(
88+
`${proto}://${host}${url.pathname}${url.search}`,
89+
{
90+
method: req.method,
91+
headers: req.headers,
92+
body: req.body,
93+
},
94+
),
95+
);
7796
if (!(resp instanceof Response)) {
78-
throw new Error("Fetch handler must return a Response object.");
97+
throw new Error(
98+
"Fetch handler must return a Response object.",
99+
);
79100
}
80101

81102
return resp;
@@ -100,7 +121,9 @@ if (payload.command === "fetch") {
100121
}
101122

102123
if (!(typeof handler.cron === "function")) {
103-
console.error("The mod default export cron property must be a function.");
124+
console.error(
125+
"The mod default export cron property must be a function.",
126+
);
104127
Deno.exit(1);
105128
}
106129

@@ -116,12 +139,14 @@ if (payload.command === "fetch") {
116139

117140
const handler = mod.default;
118141
if (!("email" in handler)) {
119-
console.error("The mod default export does not have an email function.");
142+
console.error(
143+
"The mod default export does not have an email function.",
144+
);
120145
Deno.exit(1);
121146
}
122147

123-
const data = btoa(payload.msg)
124-
const blob = new Blob([data]);
148+
const buf = Buffer.from(payload.email, "base64")
149+
const blob = new Blob([buf]);
125150
await handler.email(blob.stream());
126151
} else {
127152
console.error("Unknown command: ", payload.command);

0 commit comments

Comments
 (0)