Danisz commited on
Commit
6d120ef
·
1 Parent(s): 3defe46

Add CSP logic

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -9,6 +9,7 @@ from fastapi import FastAPI, File, UploadFile, HTTPException, Form, Request
9
  from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
10
  from collections import OrderedDict
11
  import base64
 
12
 
13
  # Set up basic logging configuration
14
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -161,18 +162,16 @@ async def serve_main_page(request: Request):
161
  var iframe = document.getElementById('flet-app-frame');
162
  var currentProtocol = window.location.protocol;
163
  var currentHost = window.location.host;
164
- var src;
165
-
166
- if (currentHost.includes('Danisz')) {
167
- // Force HTTPS for Danisz's Hugging Face space
168
- src = 'https://' + currentHost + '/flet-app';
169
- } else {
170
- // Use the current protocol for other environments
171
- src = currentProtocol + '//' + currentHost + '/flet-app';
172
- }
173
 
174
  iframe.src = src;
175
  console.log('Iframe src set to:', src);
 
 
 
 
 
 
176
  };
177
  </script>
178
  </body>
 
9
  from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
10
  from collections import OrderedDict
11
  import base64
12
+ from starlette.responses import Response
13
 
14
  # Set up basic logging configuration
15
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
 
162
  var iframe = document.getElementById('flet-app-frame');
163
  var currentProtocol = window.location.protocol;
164
  var currentHost = window.location.host;
165
+ var src = currentProtocol + '//' + currentHost + '/flet-app/';
 
 
 
 
 
 
 
 
166
 
167
  iframe.src = src;
168
  console.log('Iframe src set to:', src);
169
+
170
+ // Set Content-Security-Policy based on the current protocol
171
+ var meta = document.createElement('meta');
172
+ meta.httpEquiv = "Content-Security-Policy";
173
+ meta.content = currentProtocol === 'https:' ? "upgrade-insecure-requests" : "";
174
+ document.head.appendChild(meta);
175
  };
176
  </script>
177
  </body>