Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
ci(python-sdk): build in a job that cannot mint a PyPI credential
Yudhi Armyndharisa47fd0e5e1d4
diff --git a/.github/workflows/python-sdk-release.yml b/.github/workflows/python-sdk-release.ymlindex 968a7ffb0..fc8383ba0 100644--- a/.github/workflows/python-sdk-release.yml+++ b/.github/workflows/python-sdk-release.yml@@ -22,13 +22,14 @@ permissions:contents: readjobs:- publish:+ # Everything that installs third-party code (pip, the test dependencies, the build backend that+ # `python -m build` fetches) runs here, in a job that cannot mint a publish credential. Those+ # packages resolve to whatever is newest at tag time, so they must never share a job with+ # id-token: write. This is PyPA's documented layout for trusted publishing.+ build:runs-on: ubuntu-latestpermissions:contents: read- # Mints the OIDC token PyPI exchanges for its short-lived upload credential. Without this the- # publish has no credential at all — it is the whole mechanism.- id-token: writesteps:- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7@@ -89,6 +90,28 @@ jobs:python -m pip install --upgrade buildpython -m build+ - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7+ with:+ name: python-sdk-dist+ path: sdk/python/dist/+ if-no-files-found: error++ publish:+ # Runs only after the guards, the tests and the build above all passed.+ needs: build+ runs-on: ubuntu-latest+ permissions:+ contents: read+ # Mints the OIDC token PyPI exchanges for its short-lived upload credential. Without this the+ # publish has no credential at all. No checkout and no install: this job only uploads the+ # files the build job produced and tested.+ id-token: write+ steps:+ - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8+ with:+ name: python-sdk-dist+ path: sdk/python/dist/+- name: Publish to PyPI (trusted publishing)uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2with:diff --git a/src/common/ci-privilege-gate.spec.ts b/src/common/ci-privilege-gate.spec.tsindex 8bce14e2f..11a8ec8ab 100644--- a/src/common/ci-privilege-gate.spec.ts+++ b/src/common/ci-privilege-gate.spec.ts@@ -140,40 +140,50 @@ describe('a job that runs a repo script checks the repo out', () => {/*** A job granted `id-token: write` can mint a registry publish credential, so every tool it installs- * globally runs with that ability. A floating spec (`npm@latest`, a bare major) resolves to whatever- * was published most recently at tag time; pin the exact version the way the Dockerfile pins its npm.+ * runs with that ability. A floating spec (`npm@latest`, a bare major) resolves to whatever was+ * published most recently at tag time; pin the exact version the way the Dockerfile pins its npm.+ *+ * Python installs cannot be pinned that way: `pip install` resolves the ranges in pyproject.toml, and+ * `python -m build` fetches its build backend into an isolated environment no pin reaches. So an+ * id-token job runs neither; the install, test and build happen in a job without the grant.*/-describe('a job that can mint a publish credential installs only exact global versions', () => {+describe('a job that can mint a publish credential installs no floating third-party code', () => {type Permissions = Record<string, string> | string | null | undefined;type OidcJob = { permissions?: Permissions; steps?: Step[] };type OidcWorkflow = { permissions?: Permissions; jobs?: Record<string, OidcJob> };const workflows = fs.readdirSync(workflowDir).filter(f => f.endsWith('.yml') || f.endsWith('.yaml'));const GLOBAL_INSTALL = /\bnpm\s+(?:install|i|add)\s+(?:-g|--global)\s+([^\n;&|]+)/g;+ // `pip`, `pip3`, `python -m pip` and `uv pip` all contain `pip install`.+ const PYTHON_INSTALL = /\bpip[\d.]*\s+install\b|\bpython[\d.]*\s+-m\s+build\b/g;// A job without its own `permissions` inherits the workflow-level block; `write-all` grants id-token too.const grantsIdToken = (perms: Permissions): boolean =>perms === 'write-all' || (typeof perms === 'object' && perms !== null && perms['id-token'] === 'write');- const globalInstallsInOidcJobs = (source: string | OidcWorkflow): Array<{ job: string; spec: string }> => {+ const oidcJobRuns = (source: string | OidcWorkflow): Array<{ job: string; run: string }> => {const workflow =typeof source === 'string'? (yaml.load(fs.readFileSync(path.join(workflowDir, source), 'utf8')) as OidcWorkflow): source;return Object.entries(workflow.jobs ?? {}).filter(([, def]) => grantsIdToken(def.permissions !== undefined ? def.permissions : workflow.permissions))- .flatMap(([job, def]) =>- (def.steps ?? []).flatMap(step =>- [...executableLines(step.run ?? '').matchAll(GLOBAL_INSTALL)].flatMap(match =>- match[1]- .trim()- .split(/\s+/)- .filter(arg => !arg.startsWith('-'))- .map(spec => ({ job, spec })),- ),- ),- );+ .flatMap(([job, def]) => (def.steps ?? []).map(step => ({ job, run: executableLines(step.run ?? '') })));};+ const globalInstallsInOidcJobs = (source: string | OidcWorkflow): Array<{ job: string; spec: string }> =>+ oidcJobRuns(source).flatMap(({ job, run }) =>+ [...run.matchAll(GLOBAL_INSTALL)].flatMap(match =>+ match[1]+ .trim()+ .split(/\s+/)+ .filter(arg => !arg.startsWith('-'))+ .map(spec => ({ job, spec })),+ ),+ );++ const pythonInstallsInOidcJobs = (source: string | OidcWorkflow): string[] =>+ oidcJobRuns(source).flatMap(({ job, run }) => [...run.matchAll(PYTHON_INSTALL)].map(m => `${job}: ${m[0]}`));+// Non-vacuity: the JS SDK release job installs its own npm, so the finder must see it.it('finds the global npm install in the JS SDK publish job', () => {expect(globalInstallsInOidcJobs('js-sdk-release.yml').map(entry => entry.spec)).toEqual([@@ -196,4 +206,30 @@ describe('a job that can mint a publish credential installs only exact global veconst floating = globalInstallsInOidcJobs(file).filter(entry => !/@\d+\.\d+\.\d+$/.test(entry.spec));expect(floating).toEqual([]);});++ // Non-vacuity: the single-job shape the PyPI release used to have, and the Python install that+ // still exists in the release workflow, just outside the id-token job.+ it('finds pip installs and python -m build in an id-token job', () => {+ const singleJob: OidcWorkflow = {+ jobs: {+ publish: {+ permissions: { contents: 'read', 'id-token': 'write' },+ steps: [+ { run: "pip install -e '.[dev]'\npytest" },+ { run: 'python -m pip install --upgrade build\npython -m build' },+ ],+ },+ },+ };+ expect(pythonInstallsInOidcJobs(singleJob)).toEqual([+ 'publish: pip install',+ 'publish: pip install',+ 'publish: python -m build',+ ]);+ expect(runCommandsOf('python-sdk-release.yml').join('\n')).toMatch(PYTHON_INSTALL);+ });++ it.each(workflows)('%s: id-token jobs run no pip install or python -m build', file => {+ expect(pythonInstallsInOidcJobs(file)).toEqual([]);+ });});