File size: 1,034 Bytes
c72f0a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { bridgeScript } from "./bridge";

export const CODE_APP_SANDBOX = "allow-scripts";
export const CODE_APP_MAX_BYTES = 512 * 1024;

const POLICY = "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'none'; img-src 'none'; font-src data:; media-src 'none'; object-src 'none'; frame-src 'none'; worker-src 'none'; base-uri 'none'; form-action 'none'";
const POLICY_META = `<meta http-equiv="Content-Security-Policy" content="${POLICY}">`;

export function checkCodeAppSize(html: string): void {
  if (new TextEncoder().encode(html).byteLength > CODE_APP_MAX_BYTES) {
    throw new RangeError("This App is too large to open.");
  }
}

export function appDocument(html: string, css: string): string {
  checkCodeAppSize(html);
  // Escape stylesheet markup so the kit cannot close its host-owned style element.
  const stylesheet = css.replace(/</g, "\\3c ");
  return `<!doctype html>${POLICY_META}<meta charset="utf-8"><style>${stylesheet}</style><script>${bridgeScript()}</script>${html}`;
}