Skip to content

Commit 4663417

Browse files
committed
fix(server): guest message line breaks count once against the length limit
Multipart form encoding sends each line break as CRLF, so a guest message the textarea allowed could exceed the 4,000-character limit on the server and fail with 400. Normalize CRLF to LF before validation. Fixes #26534
1 parent 5211acf commit 4663417

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

‎apps/server/src/guest/http.test.ts‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, it } from "@effect/vitest";
3+
import { GUEST_MESSAGE_MAX_LENGTH } from "@t3tools/contracts";
34
import * as Effect from "effect/Effect";
45
import { HttpServerRequest } from "effect/http";
56

67
import { readSubmission } from "./http.ts";
78

9+
const submit = (form: FormData) =>
10+
readSubmission.pipe(
11+
Effect.provideService(
12+
HttpServerRequest.HttpServerRequest,
13+
HttpServerRequest.fromWeb(
14+
new Request("http://localhost/api/guest/messages?share=share-1", {
15+
method: "POST",
16+
body: form,
17+
}),
18+
),
19+
),
20+
);
21+
822
it.effect("keeps a JSON file a guest attaches as a file, not a form field", () =>
923
Effect.gen(function* () {
1024
const form = new FormData();
1125
form.append("shareId", "share-1");
1226
form.append("text", "");
1327
form.append("file", new File(['{"a":1}'], "data.json", { type: "application/json" }));
14-
const request = HttpServerRequest.fromWeb(
15-
new Request("http://localhost/api/guest/messages?share=share-1", {
16-
method: "POST",
17-
body: form,
18-
}),
19-
);
20-
const submission = yield* readSubmission.pipe(
21-
Effect.provideService(HttpServerRequest.HttpServerRequest, request),
22-
);
28+
const submission = yield* submit(form);
2329
assert.deepEqual(
2430
submission?.uploads.map((upload) => [upload.name, upload.contentType]),
2531
[["data.json", "application/json"]],
2632
);
2733
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
2834
);
35+
36+
it.effect("counts a line break in a multipart message as one character", () =>
37+
Effect.gen(function* () {
38+
const text = "a\n".repeat(GUEST_MESSAGE_MAX_LENGTH / 2 - 1) + "ab";
39+
const form = new FormData();
40+
form.append("shareId", "share-1");
41+
form.append("text", text);
42+
const submission = yield* submit(form);
43+
assert.strictEqual(submission?.fields.text, text);
44+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
45+
);

‎apps/server/src/guest/http.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,12 @@ export const readSubmission = Effect.gen(function* () {
189189
}),
190190
),
191191
);
192-
const fields = yield* decodeSubmit({ shareId: parts.shareId, text: parts.text ?? "" }).pipe(
193-
Effect.option,
194-
);
192+
const text = parts.text ?? "";
193+
const fields = yield* decodeSubmit({
194+
shareId: parts.shareId,
195+
// Form encoding sends each line break as CRLF, which would count twice against the limit.
196+
text: typeof text === "string" ? text.replaceAll("\r\n", "\n") : text,
197+
}).pipe(Effect.option);
195198
if (fields._tag === "None") return null;
196199
const files = parts.file;
197200
const uploads: ReadonlyArray<GuestUpload> = Array.isArray(files)

0 commit comments

Comments
 (0)