61 行
2.3 KiB
TypeScript
61 行
2.3 KiB
TypeScript
|
|
import { describe, expect, it } from "vitest";
|
||
|
|
|
||
|
|
import tauriConfigJson from "../../src-tauri/tauri.conf.json?raw";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The shipped Content-Security-Policy, asserted directly.
|
||
|
|
*
|
||
|
|
* Tauri serves this header from its asset resolver, which the Vite dev server
|
||
|
|
* bypasses — so a missing directive costs nothing in development and breaks the
|
||
|
|
* release build only. That divergence has already produced two defects here
|
||
|
|
* (F-04's missing `blob:` for images, and an inline script that would have been
|
||
|
|
* refused), so each directive the app depends on is pinned rather than assumed.
|
||
|
|
*/
|
||
|
|
describe("the shipped content security policy", () => {
|
||
|
|
const csp = (
|
||
|
|
JSON.parse(tauriConfigJson) as {
|
||
|
|
app: { security: { csp: string } };
|
||
|
|
}
|
||
|
|
).app.security.csp;
|
||
|
|
|
||
|
|
const directive = (name: string) => {
|
||
|
|
const found = csp
|
||
|
|
.split(";")
|
||
|
|
.map((part) => part.trim())
|
||
|
|
.find((part) => part === name || part.startsWith(`${name} `));
|
||
|
|
expect(
|
||
|
|
found,
|
||
|
|
`${name} is absent, so it falls back to default-src`,
|
||
|
|
).toBeDefined();
|
||
|
|
return found ?? "";
|
||
|
|
};
|
||
|
|
|
||
|
|
it("lets an image attachment render from a blob url", () => {
|
||
|
|
expect(directive("img-src")).toContain("blob:");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("lets a voice attachment play from a blob url", () => {
|
||
|
|
// Without its own directive, an <audio src="blob:"> falls back to
|
||
|
|
// default-src 'self', which has no blob: — so a voice reply downloads
|
||
|
|
// successfully and then refuses to play, in release builds only.
|
||
|
|
expect(directive("media-src")).toContain("blob:");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("still refuses inline script", () => {
|
||
|
|
// tauri-utils only nonces `script[src^='http']`, so inline script cannot be
|
||
|
|
// made to work here — the pet window uses a separate entry document instead.
|
||
|
|
// Relaxing this would silently reopen that hole.
|
||
|
|
expect(directive("script-src")).not.toContain("unsafe-inline");
|
||
|
|
expect(directive("script-src")).not.toContain("unsafe-eval");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("does not let the renderer reach arbitrary hosts", () => {
|
||
|
|
// An attachment is proxied through Rust and handed over as bytes precisely
|
||
|
|
// so the webview never talks to a host the server named.
|
||
|
|
expect(directive("default-src")).toBe("default-src 'self'");
|
||
|
|
for (const name of ["img-src", "media-src"]) {
|
||
|
|
expect(directive(name)).not.toMatch(/\shttps?:\/\/(?!asset\.localhost)/);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|