File size: 10,135 Bytes
34810d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Core Canvas document storage and URL contract coverage.
import { link, mkdir, readFile, rename, symlink, writeFile } from "node:fs/promises";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
import {
  createCanvasDocument,
  readCanvasDocumentHtmlSource,
  resolveCanvasDocumentsDir,
  resolveCanvasHttpPathToLocalPath,
} from "./documents.js";

const tempDirs = useAutoCleanupTempDirTracker(afterEach);

function resolveCanvasDocumentDir(stateDir: string, documentId: string): string {
  return path.join(resolveCanvasDocumentsDir(stateDir), documentId);
}

describe("canvas documents", () => {
  it.skipIf(process.platform === "win32").each(["document", "manifest.json", "index.html"])(
    "rejects a symlinked %s when reading widget HTML",
    async (target) => {
      const stateDir = tempDirs.make("openclaw-canvas-links-");
      const outsideDir = tempDirs.make("openclaw-canvas-outside-");
      const document = await createCanvasDocument(
        { kind: "html_bundle", entrypoint: { type: "html", value: "<p>outside</p>" } },
        { stateDir },
      );
      const documentDir = resolveCanvasDocumentDir(stateDir, document.id);
      const original = target === "document" ? documentDir : path.join(documentDir, target);
      const outside = path.join(outsideDir, target);
      await rename(original, outside);
      await symlink(outside, original, target === "document" ? "dir" : "file");

      await expect(readCanvasDocumentHtmlSource(document.id, { stateDir })).rejects.toMatchObject({
        code: target === "document" ? "outside-workspace" : "symlink",
      });
    },
  );

  it.each(["manifest.json", "index.html"])(
    "rejects a hardlinked %s when reading widget HTML",
    async (target) => {
      const stateDir = tempDirs.make("openclaw-canvas-hardlinks-");
      const document = await createCanvasDocument(
        { kind: "html_bundle", entrypoint: { type: "html", value: "<p>aliased</p>" } },
        { stateDir },
      );
      await link(
        path.join(resolveCanvasDocumentDir(stateDir, document.id), target),
        path.join(stateDir, `alias-${target}`),
      );
      await expect(readCanvasDocumentHtmlSource(document.id, { stateDir })).rejects.toMatchObject({
        code: "hardlink",
      });
    },
  );

  it("bounds HTML reads by bytes while independently allowing the manifest", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-bounded-");
    const document = await createCanvasDocument(
      { kind: "html_bundle", entrypoint: { type: "html", value: "éééé" } },
      { stateDir },
    );
    await expect(
      readCanvasDocumentHtmlSource(document.id, { stateDir, maxBytes: 7 }),
    ).rejects.toMatchObject({ code: "too-large" });
    await expect(
      readCanvasDocumentHtmlSource(document.id, { stateDir, maxBytes: 8 }),
    ).resolves.toEqual({ html: "éééé" });
  });

  it("rejects oversized manifests before parsing them", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-manifest-");
    const document = await createCanvasDocument(
      { kind: "html_bundle", entrypoint: { type: "html", value: "<p>small</p>" } },
      { stateDir },
    );
    const manifestPath = path.join(
      resolveCanvasDocumentDir(stateDir, document.id),
      "manifest.json",
    );
    await writeFile(
      manifestPath,
      JSON.stringify({ ...document, title: "x".repeat(2 * 1024 * 1024) }),
    );
    await expect(readCanvasDocumentHtmlSource(document.id, { stateDir })).rejects.toMatchObject({
      code: "too-large",
    });
  });

  it("builds entry urls for materialized path documents under managed storage", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-documents-");
    const workspaceDir = tempDirs.make("openclaw-canvas-documents-workspace-");
    await mkdir(path.join(workspaceDir, "player"), { recursive: true });
    await writeFile(path.join(workspaceDir, "player/index.html"), "<div>ok</div>", "utf8");

    const document = await createCanvasDocument(
      {
        kind: "html_bundle",
        entrypoint: { type: "path", value: "player/index.html" },
      },
      { stateDir, workspaceDir },
    );

    expect(document.entryUrl).toContain("/__openclaw__/canvas/documents/");
    expect(document.localEntrypoint).toBe("index.html");
    expect(resolveCanvasDocumentDir(stateDir, document.id)).toContain(stateDir);
  });

  it("materializes inline html bundles as index documents", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-documents-");
    const document = await createCanvasDocument(
      {
        kind: "html_bundle",
        title: "Preview",
        entrypoint: {
          type: "html",
          value:
            "<!doctype html><html><head><style>.demo{color:red}</style></head><body><div class='demo'>Front</div></body></html>",
        },
      },
      { stateDir },
    );

    const indexHtml = await readFile(
      path.join(resolveCanvasDocumentDir(stateDir, document.id), "index.html"),
      "utf8",
    );
    expect(indexHtml).toContain("<div class='demo'>Front</div>");
    expect(indexHtml).toContain("<style>.demo{color:red}</style>");
    expect(document.title).toBe("Preview");
    expect(document.entryUrl).toBe(`/__openclaw__/canvas/documents/${document.id}/index.html`);
    await expect(readCanvasDocumentHtmlSource(document.id, { stateDir })).resolves.toEqual({
      html: indexHtml,
    });
  });

  it("reports the document sandbox policy alongside board source bytes", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-documents-");
    const document = await createCanvasDocument(
      {
        kind: "html_bundle",
        entrypoint: { type: "html", value: "<script>ready()</script>" },
        cspSandbox: "scripts",
      },
      { stateDir },
    );

    await expect(readCanvasDocumentHtmlSource(document.id, { stateDir })).resolves.toEqual({
      html: "<script>ready()</script>",
      cspSandbox: "scripts",
    });
  });

  it("reuses a supplied stable id by replacing the prior materialized view", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-documents-");
    const first = await createCanvasDocument(
      {
        id: "status-card",
        kind: "html_bundle",
        entrypoint: { type: "html", value: "<div>first</div>" },
      },
      { stateDir },
    );
    const second = await createCanvasDocument(
      {
        id: "status-card",
        kind: "html_bundle",
        entrypoint: { type: "html", value: "<div>second</div>" },
      },
      { stateDir },
    );

    expect(first.id).toBe("status-card");
    expect(second.id).toBe("status-card");
    const indexHtml = await readFile(
      path.join(resolveCanvasDocumentDir(stateDir, second.id), "index.html"),
      "utf8",
    );
    expect(indexHtml).toContain("second");
    expect(indexHtml).not.toContain("first");
  });

  it("copies declared assets into managed storage", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-documents-");
    const workspaceDir = tempDirs.make("openclaw-canvas-documents-workspace-");
    await mkdir(path.join(workspaceDir, "collection.media"), { recursive: true });
    await writeFile(path.join(workspaceDir, "collection.media/audio.mp3"), "audio", "utf8");

    const document = await createCanvasDocument(
      {
        kind: "html_bundle",
        entrypoint: { type: "html", value: "<audio></audio>" },
        assets: [
          {
            logicalPath: "collection.media/audio.mp3",
            sourcePath: "collection.media/audio.mp3",
            contentType: "audio/mpeg",
          },
        ],
      },
      { stateDir, workspaceDir },
    );

    expect(document.assets).toEqual([
      { logicalPath: "collection.media/audio.mp3", contentType: "audio/mpeg" },
    ]);
    await expect(
      readFile(
        path.join(resolveCanvasDocumentDir(stateDir, document.id), "collection.media/audio.mp3"),
        "utf8",
      ),
    ).resolves.toBe("audio");
  });

  it("wraps local and remote PDF documents in index viewer pages", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-documents-");
    const workspaceDir = tempDirs.make("openclaw-canvas-documents-workspace-");
    await writeFile(path.join(workspaceDir, "demo.pdf"), "%PDF-1.4", "utf8");
    const localDocument = await createCanvasDocument(
      { kind: "document", entrypoint: { type: "path", value: "demo.pdf" } },
      { stateDir, workspaceDir },
    );
    const remoteDocument = await createCanvasDocument(
      {
        kind: "document",
        entrypoint: { type: "url", value: "https://example.com/demo.pdf" },
      },
      { stateDir },
    );

    const localHtml = await readFile(
      path.join(resolveCanvasDocumentDir(stateDir, localDocument.id), "index.html"),
      "utf8",
    );
    const remoteHtml = await readFile(
      path.join(resolveCanvasDocumentDir(stateDir, remoteDocument.id), "index.html"),
      "utf8",
    );
    expect(localHtml).toContain('data="demo.pdf"');
    expect(remoteHtml).toContain('data="https://example.com/demo.pdf"');
  });

  it("rejects traversal and malformed encoded hosted paths", async () => {
    const stateDir = tempDirs.make("openclaw-canvas-documents-");
    expect(
      resolveCanvasHttpPathToLocalPath(
        "/__openclaw__/canvas/documents/../collection.media/index.html",
        { stateDir },
      ),
    ).toBeNull();

    const documentDir = resolveCanvasDocumentDir(stateDir, "cv_malformed");
    await mkdir(documentDir, { recursive: true });
    await writeFile(path.join(documentDir, "%E0%A4%A.html"), "literal-percent-name", "utf8");
    expect(
      resolveCanvasHttpPathToLocalPath(
        "/__openclaw__/canvas/documents/cv_malformed/%E0%A4%A.html",
        { stateDir },
      ),
    ).toBeNull();
    expect(
      resolveCanvasHttpPathToLocalPath(
        "/__openclaw__/canvas/documents/cv_malformed/%25E0%25A4%25A.html",
        { stateDir },
      ),
    ).toBe(path.join(documentDir, "%E0%A4%A.html"));
  });
});