Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Pyodide version check to prevent incorrect bundle release #3602

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/pyodide/internal/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { default as SetupEmscripten } from 'internal:setup-emscripten';
import { default as UnsafeEval } from 'internal:unsafe-eval';
import { simpleRunPython } from 'pyodide-internal:util';
import { loadPackages } from 'pyodide-internal:loadPackage';
import { default as MetadataReader } from 'pyodide-internal:runtime-generated/metadata';

/**
* After running `instantiateEmscriptenModule` but before calling into any C
Expand Down Expand Up @@ -79,6 +80,28 @@ function maybeAddVendorDirectoryToPath(pyodide: Pyodide) {
`);
}

/**
* Verifies that the Pyodide version in our compat flag matches our actual version. This is to
* prevent us accidentally releasing a Pyodide bundle built against a different version than one
* we expect.
*/
function validatePyodideVersion(Module: Module) {
const expectedPyodideVersion = MetadataReader.getPyodideVersion();
if (expectedPyodideVersion == 'dev') {
return;
}
try {
simpleRunPython(
Module,
`from pyodide import __version__ as v; assert v == \'${expectedPyodideVersion}\'; del v`
);
} catch {
throw new Error(
`Pyodide version mismatch, expected '${expectedPyodideVersion}'`
);
}
}

export async function loadPyodide(
isWorkerd: boolean,
lockfile: PackageLock,
Expand Down Expand Up @@ -118,6 +141,8 @@ export async function loadPyodide(

finishSnapshotSetup(pyodide);

validatePyodideVersion(Module);

// Need to set these here so that the logs go to the right context. If we don't they will go
// to SetupEmscripten's context and end up being KJ_LOG'd, which we do not want.
Module.API.initializeStreams(
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
$impliedByAfterDate(name = "nodeJsCompat", date = "2025-04-01");
# Automatically populate process.env from text bindings only
# when nodejs_compat is being used.

cacheApiRequestCfOverridesCacheRules @77 :Bool
$compatEnableFlag("cache_api_request_cf_overrides_cache_rules")
$experimental
Expand Down
Loading