Codex Automation Permissions: Safe Practices for codex exec and Playwright MCP

A clear look at permission boundaries in Codex automation: how to set least-privilege access for codex exec, Playwright MCP, network access, and CI, and how to reduce execution risk with rollback-friendly verification steps.

10 分钟阅读
Codex Automation Permissions: Safe Practices for codex exec and Playwright MCP

AI Code With · Codex Automation, Permissions, and Browser Workflows

Once Codex moves into real automation, the question quickly shifts from "can it run?" to "what exactly can it do, who can make sense of a failure, and how do we stop it when something goes wrong?"

Many automation incidents aren't caused by Codex failing to execute a task. They happen because the boundaries never made it into the workflow: a permission failure leads straight to full access; codex exec works once and gets dropped into CI; Playwright MCP can open a browser, so it gets to log into the admin panel, fill in forms, submit, and publish, all in one pass.

These three things look like separate topics, namely permissions, the command line, and the browser, but they form one chain. A Permission Profile decides where Codex can reach. codex exec decides how a task runs non-interactively and what evidence it leaves behind. Playwright MCP extends automation from the local workspace to real web pages, cookies, and account actions.

Automation doesn't mean removing the human. It means taking the boundaries a person used to watch over and writing them into permissions, inputs and outputs, confirmation points, and rollback procedures.

1. Before Automation Goes Live, Answer Five Questions

QuestionWhy It MattersTypical Controls
What can it read?Reading can expose sensitive information tooread-only, workspace roots, browser domain scope
What can it write?Editing files, filling forms, and publishing content carry different risksworkspace permissions, staged operations, manual confirmation
Where can it reach on the network?External content may carry prompt injection, and secrets may leakdomain allowlist, minimal network permissions
Who gets the key?Letting the whole job read secrets widens the exposure surfaceinject only in the steps that need it
How does it stop on failure?Automatic retries may overwrite the scene or repeat high-risk actionsexit status, JSONL, logs, rollback points

2. When Permissions Fall Short, Look at What It's Trying to Reach Before Reaching for the Highest Level

When Codex says it lacks permission, the highest permission level may well be the fastest way to keep the task moving, but it's also the easiest way to leave long-term risk behind.

Permission Profiles are still in Beta. This draft follows the current official documentation and covers three commonly used built-in profiles:

ProfileSuited ForRecommendation
:read-onlyReading, explaining, and reviewing, with no file writes neededStart here by default
:workspaceModifying files within the current workspace rootsMost coding tasks
:danger-full-accessBroad local access is explicitly requiredUse only when truly necessary and the risk has been acknowledged

If you only need to explain an error, read-only is enough. If you're only changing the current project, use workspace. Consider a higher permission level only when you explicitly need to reach files outside the workspace or system-level resources.

Don't stack Permission Profiles on top of the legacy sandbox config

The current permission system works on different logic than the legacy sandbox_mode / sandbox_workspace_write. When you migrate, don't leave two overlapping security models active in the same config.

When permission behavior doesn't match what you expect, first figure out which set of rules is actually in effect, rather than piling on more config.

A minimal-permission experiment beats staring at ten tables

• :read-only: reading files should succeed, and writes should be blocked.

• :workspace: create a harmless test file inside the project.

• Try a harmless write outside the project and confirm it's denied or requires extra permission.

• Delete the test file and restore the original config.

Next time a permission fails, look at which path, domain, or command got blocked, then ask whether that action is really required for the current task.

3. AI Code With: which layer of your automation does it fit best?

If your automation already calls different models through AI Code With, it fits best at the "model API, key management, and call attribution" layer, not as a replacement for Codex permissions or browser security.

For example, a long-running CI job can get its own Key with a clearly defined purpose. If the task only needs to reach the AI Code With Codex API, grant minimal network access around the API domain you've verified in the current docs, then use Usage Records to check that calls came from the expected Key and model.

The boundary matters here: AI Code With can tell you whether a given model call went through it, which Key was used, and how much usage it generated. It won't decide for Codex which local directories are writable, and it won't decide for Playwright whether a "Publish" button should be clicked automatically.

4. codex exec: great for scripts and CI, but don't treat it like a plain shell

codex exec is Codex's non-interactive entry point, suited to scripts, scheduled tasks, and CI. The simplest call is straightforward:

codex exec "summarize the repository structure and list the top 5 risky areas"

What really matters is the input/output contract. Along the way, the agent may call tools, run commands, hit permission denials, or fail midway. If you only look at the final line of natural-language text, your automation can easily turn into a black box.

Keep stdout, stderr, and structured output separate

Non-interactive mode currently separates progress information from the final result, so you can hand the final output to downstream steps while keeping the process logs.

codex exec "generate release notes for the last 10 commits" > release-notes.md

If downstream steps also need to know what happened during the task, read structured events instead of parsing large blocks of natural-language logs.

--json: open a window into the automation black box

codex exec --json "summarize the repo structure" | jq

The JSONL event stream lets your program handle thread, turn, item, and error events by type, instead of assuming that "a certain line is always the result."

When parsing, branch on type, let unknown events pass through, and handle completion and failure states explicitly. If downstream needs fixed fields, constrain the final result with an output schema.

A reviewable exec pipeline should leave behind at least this evidence

• The task prompt and allowed scope.

• The stderr process log.

• JSONL events, or at least failure events.

• The exit status.

• The project's existing test / lint / build results.

• The Git diff, if any files were modified.

• The final artifacts and acceptance fields.

That way, when something fails, you can tell whether the problem was authentication, permissions, a tool command, tests, or the structure of the model output.

5. Keys in automation shouldn't belong to the whole job

If you set the model Key as an environment variable readable by the entire CI job, then dependency installation after checkout, tests, build hooks, and every other step can potentially share the same Secret exposure surface.

A safer rule: the Key comes from a secret management system, goes only to the one step or process that actually calls Codex, and never ends up in YAML, the repo, prompts, or logs.

CODEX_API_KEY=<from-secret> codex exec --json "triage open bug reports"

If your team uses AI Code With, you can create a dedicated low-exposure Key for CI and use the backend call records for attribution. But secret injection on the CI platform, trust in PR events, and repo permissions are still CI's responsibility.

6. Playwright MCP: A Browser That Opens Only Proves the Connection Works

Playwright MCP makes it easy to fall for an illusion: if the browser opens, then publishing from an admin panel, submitting forms, and even payment flows must be safe to automate.

In reality, a browser isn't just public web pages. It also holds login sessions, cookies, orders, admin buttons, outgoing messages, deletions, and permission changes. Being able to navigate to a page only proves that the call chain between the Server, the browser, and Codex is working.

The way Microsoft's current official README adds it to Codex is:

codex mcp add playwright npx "@playwright/mcp@latest"

You can also configure it in ~/.codex/config.toml:

[mcp_servers.playwright]

command = "npx"

args = ["@playwright/mcp@latest"]

Don't start with a logged-in admin panel. Pick a public page and have Codex read only the page title and current URL. That lets you verify three things separately: Server startup, browser navigation, and result return.

Playwright MCP's docs say explicitly: it is not a security boundary

The current Playwright MCP README states clearly that options such as allowed-origins / blocked-origins are only restrictions and guardrails. They are not a substitute for real security isolation, and they can't fully handle redirect risks.

By default, file access is limited to the workspace roots or the current directory. Enabling unrestricted file access widens the file scope. The docs make the same point about Secrets substitution: it's a convenience mechanism, not a security boundary.

In the end, browser automation still depends on client permissions, minimal scope, isolation, and explicit confirmation points.

7. Split Browser Tasks into Read → Prepare → Submit

StageTypical actionsRecommended policy
ReadOpen pages, read titles, scrape public informationRead-only first; good for the initial verification
PrepareFill in forms, upload files awaiting confirmation, generate draftsAllow edits, but don't trigger the final action
SubmitPublish, pay, send messages, delete, change permissionsConfirm separately; have a human do it when necessary

"Just run the whole process for me" sounds convenient, but it hides every risk inside the word "whole." A better prompt spells out the allowed domains, the page scope, whether data can be entered, which buttons must not be clicked, and what evidence must be returned at the end.

A Login Session Is Not Free Context

If Playwright MCP uses an existing login session, the Agent may see account names, internal pages, orders, cookies, or other sensitive data. Redact logs and screenshots, and keep the browser profile directory and tokens out of the repo.

Treat page text, issues, comments, and third-party links as untrusted input. A web page itself should never decide which automated actions are allowed next.

8. A Real-World Case: Have Codex Fill in a CMS, but Stop at "Save Draft"

Say you need to enter approved Markdown articles into a CMS every day. A low-quality automation usually starts with a single line like:

"Open the admin panel and publish today's article."

The problem with this line isn't a lack of detail. It's that there are no safety boundaries at all. A more controllable flow looks like this:

1. codex exec reads the article files marked "approved"

2. Output structured JSON: title, summary, body path, category

3. Playwright MCP opens the specified CMS domain

4. Fill in the title, body, and category

5. Save as a draft

6. Return the draft URL + key page evidence

7. Stop

8. Publish only after a human confirms

If step 1 fails, it's a file / exec / permissions problem. If step 3 fails, it's an MCP / browser chain problem. A 401 on a model request is a Provider / Key problem. The actual "publish" action is left as a separate human confirmation point.

That's the value of high-quality automation: it doesn't just run, it also knows who owns each stage, where the failure evidence lives, and how to roll back.

9. When Automation Stops in an Unknown State, Halt. Don't Keep Clicking

The most dangerous state in a browser task is not knowing whether the previous step actually succeeded. For example, the submit button was clicked but the page didn't navigate; the network timed out, but the backend may have received the request; a delete operation returned no confirmation.

The right move here is not to have the Agent "try again." First save the current page, the last tool call, the Server logs, and the visible state, then decide whether to continue. Repeated clicks can turn a recoverable problem into a duplicate submission, a duplicate payment, or multiple deletions.

10. A 12-Item Checklist Before Going Live

□ Tasks start from :read-only or minimal permissions by default.

□ When writing files is required, only the current workspace is opened up.

□ New Permission Profiles and legacy sandbox settings are not mixed.

□ The network allows only the domains the task actually needs.

□ The API Key goes only to the step that actually calls the model.

□ codex exec saves stderr, the exit status, and the necessary JSONL events.

□ After code changes, the project's existing test / lint / build run.

□ When files are modified, the Git diff is kept.

□ The first Playwright MCP verification uses only public pages and read-only actions.

□ Browser login sessions, cookies, and screenshots never enter the repo.

□ High-risk actions such as publishing, payments, deletion, and permission changes have their own confirmation points.

□ When an interruption leaves an unknown state, evidence is saved first and nothing is automatically re-run.

11. Finally: Mature Automation Isn't "One Click to the End"

A Permission Profile decides where Codex can reach; codex exec decides whether non-interactive tasks can be audited; Playwright MCP decides how far you can operate inside the browser.

Once these three layers are connected, the most important thing isn't making every step unattended. It's letting low-risk actions run automatically as much as possible, giving high-risk actions explicit confirmation points, and making sure every failure leaves logs, state, and a way to roll back.

Automation doesn't take people out of the loop. It keeps people where judgment is needed.

AI Code With fits into this chain best by handling the model API, Keys, and call records, so that model calls themselves are manageable and attributable. Codex's local permissions, CI security, and browser action boundaries remain the responsibility of their respective execution layers. That keeps the product fit natural and avoids blurring the boundaries of what each layer can do.

Sources and Verification Notes

This article is a merged rewrite of three original drafts on Codex permissions, codex exec, and Playwright MCP, with key commands and security boundaries checked against current public documentation. Permission Profiles may still change, and Playwright MCP's configuration options and browser capabilities may also change between versions. For a real rollout, rely on the official docs of the day and the current CLI help.

OpenAI Codex Permissionshttps://developers.openai.com/codex/permissions

OpenAI Codex Non-interactive mode

Microsoft Playwright MCP

AI Code With Codex docs:https://docs.aicodewith.ai/zh/docs/codex-cli