somratpro commited on
Commit
40855ff
Β·
1 Parent(s): dd7ada8

feat: enable iframe embedding by injecting a preload script to strip security headers and update CSP policies

Browse files
Files changed (3) hide show
  1. health-server.js +1 -12
  2. iframe-fix.cjs +45 -0
  3. start.sh +4 -0
health-server.js CHANGED
@@ -6,7 +6,6 @@ const PORT = process.env.HEALTH_PORT || 7861;
6
  const startTime = Date.now();
7
  const LLM_MODEL = process.env.LLM_MODEL || "Not Set";
8
  const GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || "huggingclaw";
9
- const SPACE_HOST = process.env.SPACE_HOST || "localhost:7860";
10
  const TELEGRAM_ENABLED = !!process.env.TELEGRAM_BOT_TOKEN;
11
 
12
  const server = http.createServer((req, res) => {
@@ -50,16 +49,6 @@ const server = http.createServer((req, res) => {
50
  return;
51
  }
52
 
53
- // Auto-login redirect to OpenClaw Control UI
54
- if (req.url === "/login") {
55
- const protocol = req.headers["x-forwarded-proto"] || "http";
56
- const host = req.headers["host"] || SPACE_HOST;
57
- const target = `${protocol}://${host}/?token=${GATEWAY_TOKEN}`;
58
- res.writeHead(302, { Location: target });
59
- res.end();
60
- return;
61
- }
62
-
63
  if (req.url === "/") {
64
  res.writeHead(200, { "Content-Type": "text/html" });
65
  res.end(`
@@ -262,7 +251,7 @@ const server = http.createServer((req, res) => {
262
  <span class="stat-label">Telegram</span>
263
  <span id="tg-status">Loading...</span>
264
  </div>
265
- <a href="/login" class="stat-btn">πŸš€ Open Control UI</a>
266
  </div>
267
 
268
  <div class="stat-card" style="width: 100%;">
 
6
  const startTime = Date.now();
7
  const LLM_MODEL = process.env.LLM_MODEL || "Not Set";
8
  const GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || "huggingclaw";
 
9
  const TELEGRAM_ENABLED = !!process.env.TELEGRAM_BOT_TOKEN;
10
 
11
  const server = http.createServer((req, res) => {
 
49
  return;
50
  }
51
 
 
 
 
 
 
 
 
 
 
 
52
  if (req.url === "/") {
53
  res.writeHead(200, { "Content-Type": "text/html" });
54
  res.end(`
 
251
  <span class="stat-label">Telegram</span>
252
  <span id="tg-status">Loading...</span>
253
  </div>
254
+ <a href="/" class="stat-btn">πŸš€ Open Control UI</a>
255
  </div>
256
 
257
  <div class="stat-card" style="width: 100%;">
iframe-fix.cjs ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * iframe-fix.cjs β€” Node.js preload script
3
+ *
4
+ * Intercepts OpenClaw's HTTP server to:
5
+ * 1. Allow iframe embedding (strip X-Frame-Options, fix CSP)
6
+ */
7
+ 'use strict';
8
+
9
+ const http = require('http');
10
+
11
+ console.log('[iframe-fix] Initialized: Allowing iframe embedding for *.hf.space and huggingface.co');
12
+
13
+ const origEmit = http.Server.prototype.emit;
14
+
15
+ http.Server.prototype.emit = function (event, ...args) {
16
+ if (event === 'request') {
17
+ const [, res] = args;
18
+
19
+ // Only intercept on the main OpenClaw server (port 7860)
20
+ const serverPort = this.address && this.address() && this.address().port;
21
+ if (serverPort && serverPort !== 7860) {
22
+ return origEmit.apply(this, [event, ...args]);
23
+ }
24
+
25
+ // Fix iframe embedding β€” must be applied BEFORE any early returns
26
+ const origWriteHead = res.writeHead;
27
+ res.writeHead = function (statusCode, ...whArgs) {
28
+ if (res.getHeader) {
29
+ // Strip X-Frame-Options so it can load in a Hugging Face Space iframe
30
+ res.removeHeader('x-frame-options');
31
+
32
+ // Update Content-Security-Policy if it contains frame-ancestors 'none'
33
+ const csp = res.getHeader('content-security-policy');
34
+ if (csp && typeof csp === 'string') {
35
+ res.setHeader('content-security-policy',
36
+ csp.replace(/frame-ancestors\s+'none'/i,
37
+ "frame-ancestors 'self' https://huggingface.co https://*.hf.space"));
38
+ }
39
+ }
40
+ return origWriteHead.apply(this, [statusCode, ...whArgs]);
41
+ };
42
+ }
43
+
44
+ return origEmit.apply(this, [event, ...args]);
45
+ };
start.sh CHANGED
@@ -242,6 +242,10 @@ CONFIG_JSON=$(echo "$CONFIG_JSON" | jq '.channels.whatsapp = {"dmPolicy": "pairi
242
  echo "$CONFIG_JSON" > "/home/node/.openclaw/openclaw.json"
243
  chmod 600 /home/node/.openclaw/openclaw.json
244
 
 
 
 
 
245
  # ── Startup Summary ──
246
  echo ""
247
  echo " β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”"
 
242
  echo "$CONFIG_JSON" > "/home/node/.openclaw/openclaw.json"
243
  chmod 600 /home/node/.openclaw/openclaw.json
244
 
245
+ # ── Enable Iframe Fix (Security: No Token Redirect) ──
246
+ # This Node.js preload script strips X-Frame-Options to allow HF Space embedding
247
+ export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--require /home/node/app/iframe-fix.cjs"
248
+
249
  # ── Startup Summary ──
250
  echo ""
251
  echo " β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”"