@fluid-app/fluid-cli-member-site 0.1.24 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1022,6 +1022,26 @@ function rewritePreviewHtml(html, target, previewToken) {
1022
1022
  function rewritePreviewAsset(source, target, previewToken) {
1023
1023
  return source.split(`/accounts/_frame/${target.siteId}/assets/`).join(`${ASSET_ROUTE_PREFIX}${previewToken}/`);
1024
1024
  }
1025
+ const FETCH_DIRECTIVES = new Set([
1026
+ "script-src",
1027
+ "style-src",
1028
+ "img-src",
1029
+ "font-src",
1030
+ "media-src"
1031
+ ]);
1032
+ /**
1033
+ * Adds the preview's own origin to the sources of code, styles, images, fonts
1034
+ * and media in the server's policy. The server renders a draft for no origin
1035
+ * in particular and names only Fluid's site SDK, so a page shown from here
1036
+ * would otherwise have its assets, which this proxy serves, blocked. Every
1037
+ * other directive, the closed network included, is kept as the server sent it.
1038
+ */
1039
+ function allowPreviewOrigin(policy, origin) {
1040
+ return policy.split(";").map((directive) => {
1041
+ const name = directive.trim().split(/\s+/)[0] ?? "";
1042
+ return FETCH_DIRECTIVES.has(name) ? `${directive.trimEnd()} ${origin}` : directive;
1043
+ }).join(";");
1044
+ }
1025
1045
  /**
1026
1046
  * Serves a draft preview by asking the preview route of the member or company
1027
1047
  * API with the CLI's bearer, from this process only. The browser sends no
@@ -1106,7 +1126,8 @@ var PreviewProxy = class {
1106
1126
  return text(502, "The draft preview could not be rendered.");
1107
1127
  }
1108
1128
  const type = upstream.headers.get("content-type") ?? "text/plain; charset=utf-8";
1109
- const policy = upstream.headers.get("content-security-policy") ?? "frame-ancestors 'self'; sandbox allow-scripts allow-forms";
1129
+ const upstreamPolicy = upstream.headers.get("content-security-policy");
1130
+ const policy = upstreamPolicy ? allowPreviewOrigin(upstreamPolicy, url.origin) : PREVIEW_SANDBOX_POLICY;
1110
1131
  const raw = Buffer.from(await upstream.arrayBuffer());
1111
1132
  const rewrite = type.startsWith("text/html") ? rewritePreviewHtml : /^(text\/css|(text|application)\/(x-)?javascript)\b/.test(type) ? rewritePreviewAsset : void 0;
1112
1133
  send(200, type, rewrite ? Buffer.from(rewrite(raw.toString("utf8"), target, this.previewToken)) : raw, policy, isSandboxedAssetRequest(req, this.previewToken) ? {