返回提交历史
Modified
etc/unittest/mcp.py
+3
-0
Modified
g4f/api/__init__.py
+26
-2
XFEstudio/gpt4free
Tighten CSP (connect-src/object-src none), add Permissions-Policy, document localStorage same-origin behavior
Agent-Logs-Url: https://github.com/xtekky/gpt4free/sessions/bf9b023a-6921-41be-b964-7b245db2fb4f Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>
63e6ddfd
代码差异
2 个文件
+29
-2
@@ -758,3 +758,6 @@ class TestWorkspaceFileServing(unittest.TestCase):
758
758
self.assertIn("X-Content-Type-Options", src)
759
759
self.assertIn("X-Frame-Options", src)
760
760
self.assertIn("Content-Security-Policy", src)
761
self.assertIn("Permissions-Policy", src)
762
self.assertIn("connect-src 'none'", src, "CSP should block outbound connections")
763
self.assertIn("object-src 'none'", src, "CSP should block object/embed elements")
@@ -900,6 +900,20 @@ class Api:
900
900
HTML files may freely reference co-located CSS and JS files; the
901
901
browser will fetch those via additional ``GET /pa/files/…`` calls
902
902
which are also subject to the same security checks.
903
904
.. note:: **localStorage / sessionStorage / cookies**
905
906
Files served here share the browser origin with the g4f server
907
(e.g. ``http://localhost:8080``), so JavaScript inside them
908
**can** read ``localStorage`` and ``sessionStorage`` stored by
909
the main g4f web UI (e.g. saved API keys). The HTTP
910
``Permissions-Policy`` header cannot restrict storage access.
911
The protection boundary is therefore at the *workspace* level:
912
only files that the operator or sandboxed PA code has
913
explicitly placed in ``~/.g4f/workspace`` are ever served.
914
Operators who expose this server to untrusted users should
915
keep their API key out of ``localStorage`` or serve the g4f
916
API on a separate origin/port.
903
917
"""
904
918
from g4f.mcp.pa_provider import get_workspace_dir
905
919
workspace = get_workspace_dir()
@@ -933,13 +947,23 @@ class Api:
933
947
"X-Frame-Options": "SAMEORIGIN",
934
948
# Basic XSS filter (belt-and-suspenders; CSP is more important)
935
949
"X-XSS-Protection": "1; mode=block",
936
# Restrict what the page itself can load/execute
950
# Restrict what the page itself can load/execute.
951
# Note: localStorage / sessionStorage are NOT controllable via
952
# CSP or Permissions-Policy; isolation requires a distinct origin.
937
953
"Content-Security-Policy": (
938
954
"default-src 'self'; "
939
955
"script-src 'self' 'unsafe-inline'; "
940
956
"style-src 'self' 'unsafe-inline'; "
941
957
"img-src 'self' data:; "
942
"font-src 'self' data:;"
958
"font-src 'self' data:; "
959
"connect-src 'none'; "
960
"object-src 'none'; "
961
"base-uri 'none';"
962
),
963
# Restrict powerful browser APIs that workspace pages don't need
964
"Permissions-Policy": (
965
"geolocation=(), camera=(), microphone=(), "
966
"payment=(), usb=(), fullscreen=self"
943
967
),
944
968
"Cache-Control": "no-store",
945
969
}