aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2023-01-15 13:21:34 +0200
committerGravatar GitHub <noreply@github.com> 2023-01-15 03:21:34 -0800
commit9eb4a4ff3a8a4b0babf3d7e1bb2188c7ef1bbac1 (patch)
treeb375a1a8b1fdc8382fe6b05a597800755d511dcc
parent7932a558703d7f7f2c655383f25d2432bb2e6263 (diff)
downloadbun-9eb4a4ff3a8a4b0babf3d7e1bb2188c7ef1bbac1.tar.gz
bun-9eb4a4ff3a8a4b0babf3d7e1bb2188c7ef1bbac1.tar.zst
bun-9eb4a4ff3a8a4b0babf3d7e1bb2188c7ef1bbac1.zip
[Response] reject HTTP status code correctly (#1800)
-rw-r--r--src/bun.js/webcore/body.zig3
-rw-r--r--test/bun.js/serve.test.ts49
2 files changed, 41 insertions, 11 deletions
diff --git a/src/bun.js/webcore/body.zig b/src/bun.js/webcore/body.zig
index fe8643062..05616c7df 100644
--- a/src/bun.js/webcore/body.zig
+++ b/src/bun.js/webcore/body.zig
@@ -171,7 +171,7 @@ pub const Body = struct {
if (response_init.fastGet(ctx, .status)) |status_value| {
const number = status_value.to(i32);
- if (number > 0)
+ if (100 <= number and number < 1000)
result.status_code = @truncate(u16, @intCast(u32, number));
}
@@ -183,7 +183,6 @@ pub const Body = struct {
}
}
- if (result.headers == null and result.status_code < 200) return null;
return result;
}
};
diff --git a/test/bun.js/serve.test.ts b/test/bun.js/serve.test.ts
index d6e7b7322..1961f58ad 100644
--- a/test/bun.js/serve.test.ts
+++ b/test/bun.js/serve.test.ts
@@ -1,13 +1,13 @@
import { file, gc, serve } from "bun";
-import { afterEach, describe, it, expect, beforeAll, afterAll } from "bun:test";
-import { readFile, readFileSync, writeFileSync } from "fs";
+import { afterEach, describe, it, expect, afterAll } from "bun:test";
+import { readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
-afterEach(() => Bun.gc(true));
+afterEach(() => gc(true));
-var port = 10000;
-var count = 200;
-var server;
+const count = 200;
+let port = 10000;
+let server;
async function runTest(serverOptions, test) {
if (server) {
@@ -39,6 +39,40 @@ afterAll(() => {
}
});
+[ 100, 101, 418, 999 ].forEach((statusCode) => {
+ it(`should response with HTTP status code (${statusCode})`, async () => {
+ await runTest(
+ {
+ fetch() {
+ return new Response("Foo Bar", { status: statusCode });
+ },
+ },
+ async (server) => {
+ const response = await fetch(`http://${server.hostname}:${server.port}`);
+ expect(response.status).toBe(statusCode);
+ expect(await response.text()).toBe("Foo Bar");
+ },
+ );
+ });
+});
+
+[ -200, 42, 12345, Math.PI ].forEach((statusCode) => {
+ it(`should ignore invalid HTTP status code (${statusCode})`, async () => {
+ await runTest(
+ {
+ fetch() {
+ return new Response("Foo Bar", { status: statusCode });
+ },
+ },
+ async (server) => {
+ const response = await fetch(`http://${server.hostname}:${server.port}`);
+ expect(response.status).toBe(200);
+ expect(await response.text()).toBe("Foo Bar");
+ }
+ );
+ });
+});
+
it("should display a welcome message when the response value type is incorrect", async () => {
await runTest(
{
@@ -234,7 +268,6 @@ describe("streaming", () => {
it("Error handler is called when a throwing stream hasn't written anything", async () => {
await runTest(
{
- port: port++,
error(e) {
return new Response("Test Passed", { status: 200 });
},
@@ -266,7 +299,6 @@ describe("streaming", () => {
it("text from JS throws on start with no error handler", async () => {
await runTest(
{
- port: port++,
error: undefined,
fetch(req) {
@@ -832,7 +864,6 @@ it("should support multiple Set-Cookie headers", async () => {
});
describe("should support Content-Range with Bun.file()", () => {
- var server;
// this must be a big file so we can test potentially multiple chunks
// more than 65 KB
const full = (function () {