Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(export): a web build's CSP lists every inline script, so the orientation lock runs

esenginec1cb39154392
diff --git a/desktop/electron/exportGame.ts b/desktop/electron/exportGame.ts
index 2c3c118d6..fac1011c7 100644
--- a/desktop/electron/exportGame.ts
+++ b/desktop/electron/exportGame.ts
@@ -35,7 +35,7 @@ import type { MiniGameExportProfile } from './miniGameExportProfile';
import { exportPlayable } from './exportPlayable';
import type { OnExportProgress } from './exportProgress';
import { ESENGINE_EXTERNAL } from './esengineResolve';
-import { orientationCss, orientationOverlayHtml, orientationLockScript, type ScreenOrientation } from './orientationHtml';
+import { orientationCss, orientationOverlayHtml, orientationLockScript, orientationLockCspHash, type ScreenOrientation } from './orientationHtml';
import { emitIosXcodeProject, type IosProjectSources } from './iosProject';
import { isNativePlatform, type ExportPlatform } from '../src/project/platforms';
@@ -160,13 +160,17 @@ export interface ExportGameResult {
* to-fit overlay + best-effort lock) — set for the mobile-facing web target, omitted
* for desktop (the Electron shell sizes its own window). */
function indexHtml(title: string, orientation?: ScreenOrientation): string {
+ // Every inline script on this page needs its hash listed, or the browser blocks it.
+ const inlineScripts = [IMPORT_MAP_CSP_HASH, ...(orientation ? [orientationLockCspHash(orientation)] : [])]
+ .map((h) => `'${h}'`)
+ .join(' ');
return `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
http-equiv="Content-Security-Policy"
- content="default-src 'self'; script-src 'self' 'unsafe-eval' blob: '${IMPORT_MAP_CSP_HASH}'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; connect-src 'self' data: blob:; worker-src 'self' blob:;"
+ content="default-src 'self'; script-src 'self' 'unsafe-eval' blob: ${inlineScripts}; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; connect-src 'self' data: blob:; worker-src 'self' blob:;"
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>${title}</title>
diff --git a/desktop/electron/orientationHtml.ts b/desktop/electron/orientationHtml.ts
index 3f93f202f..f4d182f33 100644
--- a/desktop/electron/orientationHtml.ts
+++ b/desktop/electron/orientationHtml.ts
@@ -11,6 +11,8 @@
* via game.json deviceOrientation, and desktop sizes its window instead — this
* is only the browser surfaces.
*/
+import { createHash } from 'node:crypto';
+
export type ScreenOrientation = 'portrait' | 'landscape';
/** CSS pinning the canvas to `orientation`: the `#rotate-hint` overlay replaces the
@@ -32,8 +34,18 @@ export function orientationOverlayHtml(orientation: ScreenOrientation): string {
return `<div id="rotate-hint" role="alert"><div class="rotate-glyph">📱</div><p>Rotate your device to ${orientation}</p></div>`;
}
+function orientationLockJs(orientation: ScreenOrientation): string {
+ return `try{var o=screen.orientation;o&&o.lock&&o.lock(${JSON.stringify(orientation)}).catch(function(){});}catch(e){}`;
+}
+
/** Best-effort orientation lock. Silently ignored where unsupported (the CSS overlay
* covers those cases); a raw <script> so both the web and inlined-playable pages use it. */
export function orientationLockScript(orientation: ScreenOrientation): string {
- return `<script>try{var o=screen.orientation;o&&o.lock&&o.lock(${JSON.stringify(orientation)}).catch(function(){});}catch(e){}</script>`;
+ return `<script>${orientationLockJs(orientation)}</script>`;
+}
+
+/** The lock script's CSP hash. A page that pins `script-src` MUST list it, or the
+ * browser blocks the inline script and the lock silently never runs. */
+export function orientationLockCspHash(orientation: ScreenOrientation): string {
+ return `sha256-${createHash('sha256').update(orientationLockJs(orientation)).digest('base64')}`;
}
diff --git a/desktop/tests/export-game.test.ts b/desktop/tests/export-game.test.ts
index 368d2330d..86a63a71a 100644
--- a/desktop/tests/export-game.test.ts
+++ b/desktop/tests/export-game.test.ts
@@ -9,6 +9,7 @@
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync, copyFileSync } from 'node:fs';
+import { createHash } from 'node:crypto';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -113,6 +114,28 @@ describe('exportGame', () => {
expect(JSON.parse(readFileSync(path.join(out, 'game.config.json'), 'utf8')).entryScene).toBe('scenes/main.esscene');
}, 60_000);
+ it('lists every inline script in the page CSP, for either orientation', async () => {
+ for (const orientation of ['landscape', 'portrait'] as const) {
+ const cspOut = path.join(root, `dist-game-csp-${orientation}`);
+ const res = await exportGame({
+ root, entryScene: 'scenes/main.esscene', gameHostEntry: GAME_HOST, scriptsEntry: 'src/main.ts',
+ sdkDistDir: path.join(root, '_sdk'), wasmDir: path.join(root, '_wasm'), outDir: cspOut, orientation,
+ });
+ expect(res.ok).toBe(true);
+
+ const html = readFileSync(path.join(cspOut, 'index.html'), 'utf8');
+ const csp = /http-equiv="Content-Security-Policy"\s*\n?\s*content="([^"]+)"/.exec(html)?.[1];
+ expect(csp).toBeTruthy();
+ // A hash the policy omits means the browser blocks that script — which is how
+ // the orientation lock came to be dead in a shipped build.
+ const inline = [...html.matchAll(/<script(?![^>]*\bsrc=)[^>]*>([\s\S]*?)<\/script>/g)].map((m) => m[1]);
+ expect(inline.length).toBe(2); // the import map + the orientation lock
+ for (const body of inline) {
+ expect(csp).toContain(`sha256-${createHash('sha256').update(body).digest('base64')}`);
+ }
+ }
+ }, 60_000);
+
it('writes the project camera fit into game.config.json (only when opted in)', async () => {
const fitOut = path.join(root, 'dist-game-fit');
const res = await exportGame({