BolyosCsaba commited on
Commit
469018d
ยท
1 Parent(s): 2fa6c59

fix: use file-served iframe src instead of srcdoc

Browse files

srcdoc iframes inherit parent CSP which blocks CDN scripts on HF Spaces.
Now writes studio HTML to sandbox_cache/studio_<hash>.html and loads it
via iframe src=/gradio_api/file=... (same origin, no CSP issues).

Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +7 -5
.gitignore CHANGED
@@ -3,3 +3,4 @@ __pycache__/
3
  result/
4
  *.pyc
5
  .DS_Store
 
 
3
  result/
4
  *.pyc
5
  .DS_Store
6
+ sandbox_cache/studio_*.html
app.py CHANGED
@@ -87,13 +87,15 @@ BRIDGE_JS = """
87
 
88
 
89
  def _wrap_in_iframe(studio_html: str) -> str:
90
- """Wrap raw studio HTML in a sandboxed iframe via srcdoc."""
91
- import html as html_mod
92
- escaped = html_mod.escape(studio_html)
 
 
93
  return (
94
- f'<iframe id="ivds-studio-iframe" srcdoc="{escaped}" '
95
  f'style="width:100%;height:600px;border:none;border-radius:8px;background:#1a1a2e" '
96
- f'allow="autoplay" sandbox="allow-scripts allow-same-origin"></iframe>'
97
  )
98
 
99
  # โ”€โ”€ Background temp-dir cleanup daemon โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
87
 
88
 
89
  def _wrap_in_iframe(studio_html: str) -> str:
90
+ """Write studio HTML to a file and return an iframe loading it via src."""
91
+ import hashlib
92
+ key = hashlib.md5(studio_html[:200].encode()).hexdigest()[:8]
93
+ filename = f"sandbox_cache/studio_{key}.html"
94
+ Path(filename).write_text(studio_html)
95
  return (
96
+ f'<iframe id="ivds-studio-iframe" src="/gradio_api/file={filename}" '
97
  f'style="width:100%;height:600px;border:none;border-radius:8px;background:#1a1a2e" '
98
+ f'allow="autoplay"></iframe>'
99
  )
100
 
101
  # โ”€โ”€ Background temp-dir cleanup daemon โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€