Skip to content

Commit de6e3fa

Browse files
author
Peter Steinberger
committed
fix(gateway): honor HTTP If-Modified-Since media validators
1 parent e857d8e commit de6e3fa

6 files changed

Lines changed: 469 additions & 46 deletions

‎src/gateway/control-ui.http.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ describe("handleControlUiHttpRequest", () => {
250250
basePath?: string;
251251
auth?: ResolvedGatewayAuth;
252252
headers?: IncomingMessage["headers"];
253+
distinctHeaders?: IncomingMessage["headersDistinct"];
253254
trustedProxies?: string[];
254255
remoteAddress?: string;
255256
}) {
@@ -259,6 +260,14 @@ describe("handleControlUiHttpRequest", () => {
259260
url: params.url,
260261
method: params.method,
261262
headers: params.headers ?? {},
263+
headersDistinct:
264+
params.distinctHeaders ??
265+
Object.fromEntries(
266+
Object.entries(params.headers ?? {}).map(([name, value]) => [
267+
name,
268+
Array.isArray(value) ? value : [String(value)],
269+
]),
270+
),
262271
socket: { remoteAddress: params.remoteAddress ?? "127.0.0.1" },
263272
} as IncomingMessage,
264273
res,
@@ -605,6 +614,74 @@ describe("handleControlUiHttpRequest", () => {
605614
},
606615
);
607616

617+
it.each(["GET", "HEAD"] as const)(
618+
"revalidates assistant media with If-Modified-Since before ranges for %s",
619+
async (method) => {
620+
await withAllowedAssistantMediaRoot({
621+
prefix: "ui-media-modified-since-",
622+
fn: async (tmpRoot) => {
623+
const filePath = path.join(tmpRoot, "photo.png");
624+
await fs.writeFile(filePath, Buffer.from("assistant-media-bytes"));
625+
const url = `/__openclaw__/assistant-media?source=${encodeURIComponent(filePath)}&token=test-token`;
626+
const auth = { mode: "token", token: "test-token", allowTailscale: false } as const;
627+
const initial = await runAssistantMediaRequest({ url, method: "HEAD", auth });
628+
const lastModified = initial.setHeader.mock.calls.find(
629+
([name]) => name === "Last-Modified",
630+
)?.[1];
631+
expect(lastModified).toEqual(expect.any(String));
632+
633+
const unchanged = await runAssistantMediaRequest({
634+
url,
635+
method,
636+
auth,
637+
headers: {
638+
"if-modified-since": String(lastModified),
639+
range: "bytes=0-3",
640+
"if-range": '"stale"',
641+
},
642+
});
643+
644+
expect(unchanged.res.statusCode).toBe(304);
645+
expect(unchanged.setHeader).toHaveBeenCalledWith("Last-Modified", lastModified);
646+
expect(unchanged.setHeader).not.toHaveBeenCalledWith("Content-Length", expect.anything());
647+
expect(unchanged.end).toHaveBeenCalledWith();
648+
},
649+
});
650+
},
651+
);
652+
653+
it.each(["GET", "HEAD"] as const)(
654+
"ignores duplicate assistant-media dates discarded by normalized Node headers for %s",
655+
async (method) => {
656+
await withAllowedAssistantMediaRoot({
657+
prefix: "ui-media-duplicate-modified-since-",
658+
fn: async (tmpRoot) => {
659+
const filePath = path.join(tmpRoot, "photo.png");
660+
await fs.writeFile(filePath, Buffer.from("assistant-media-bytes"));
661+
const url = `/__openclaw__/assistant-media?source=${encodeURIComponent(filePath)}&token=test-token`;
662+
const auth = { mode: "token", token: "test-token", allowTailscale: false } as const;
663+
const initial = await runAssistantMediaRequest({ url, method: "HEAD", auth });
664+
const lastModified = String(
665+
initial.setHeader.mock.calls.find(([name]) => name === "Last-Modified")?.[1],
666+
);
667+
668+
const duplicate = await runAssistantMediaRequest({
669+
url,
670+
method,
671+
auth,
672+
headers: { "if-modified-since": lastModified },
673+
distinctHeaders: {
674+
"if-modified-since": [lastModified, "not-an-http-date"],
675+
},
676+
});
677+
678+
expect(duplicate.res.statusCode).toBe(200);
679+
expect(duplicate.setHeader).toHaveBeenCalledWith("Last-Modified", lastModified);
680+
},
681+
});
682+
},
683+
);
684+
608685
it("resumes assistant media only for an exact If-Range HTTP-date", async () => {
609686
await withAllowedAssistantMediaRoot({
610687
prefix: "ui-media-if-range-date-",

‎src/gateway/control-ui.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,7 @@ export async function handleControlUiAssistantMediaRequest(
731731
const byteResponse = resolveByteResponse({
732732
file: opened.stat,
733733
method: req.method,
734-
rangeHeader: req.headers.range,
735-
ifRangeHeader: req.headers["if-range"],
736-
ifNoneMatchHeader: req.headers["if-none-match"],
734+
request: req,
737735
});
738736
writeByteHeaders(res, byteResponse);
739737
await byteStream.pipe(byteResponse, req.method);

0 commit comments

Comments
 (0)