Check whether python blocks in Pyodide can reach the network before the next release #249

open cmc opened this on 2026-11-27 06:03 UTC · milestone 8 Automation and extension

Discussion

cmc 2026-11-27 06:03 UTC

PythonBlocks (MR 516) runs Pyodide in a web worker of a hidden WKWebView with no Content-Security-Policy or content rule list, so a python block in the App Store builds may be able to make network requests (pyodide.http, js.fetch, synchronous XHR in the worker).

Before the next release that ships Pyodide:

  • Test what a block can reach: fetch/XHR from the worker, urllib through Pyodide, and loading packages from a CDN by micropip or loadPackage.
  • Compare with the download build's python3, which has network access, and with JavaScript blocks, whose shim refuses fetch.
  • Decide whether to block it (CSP on the page and worker, a WKContentRuleList) or document it.

Owner asked to keep this as a watch item to research and test before releasing.

Ref #90

cmc 2026-11-27 07:38 UTC

Tested while building the command host for #244 (Mac, swift test, a local TCP listener on 127.0.0.1 counting connections; nothing outside the machine was contacted).

Python blocks in Pyodide reach the network today. From a block, each of these opened a connection to the listener: js.fetch(url), synchronous js.XMLHttpRequest, pyodide.http.open_url, urllib.request.urlopen (Pyodide routes it through the browser), pyodide_js.loadPackage(url) and js.WebSocket. micropip isn't bundled, so that route is closed.

The #244 approach works for PythonBlocks. As an experiment (not committed) I added to PythonBlocks.Files:

  • on the worker and Pyodide's files: Content-Security-Policy: default-src 'none'; script-src orgstar-python: 'wasm-unsafe-eval'; connect-src orgstar-python: (Pyodide fetches its own python_stdlib.zip and lock file, so connect-src must allow the scheme; WebAssembly needs 'wasm-unsafe-eval');
  • on index.html the same without connect-src, plus 'unsafe-inline' because the page's script is inline (moving it to a served file, as the command host does, drops that);
  • a WKContentRuleList blocking every URL but ^orgstar-python://pyodide/.

With that, every probe above made no connection (WebSocket throws SecurityError, the others NetworkError or URLError), and all PythonBlocksTests pass, first result 1.1 s as before.

Both layers are needed. In the command host, CSP alone did not stop a fetch run in an isolated content world (.defaultClient); the rule list did. The rule list alone covers loads by the page and its workers.

Not checked: iOS, and whether a block could still reach anything through postMessage to the page (the page only passes results back to the app). Suggested fix for this issue: the three changes above, plus a test with a local listener as CommandHostTests.reachesNoNetwork does.

Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form