openai/codex-action

repository·main·Indexed 22 days ago

https://github.com/openai/codex-action

A GitHub Action that runs Codex (an AI agent) within workflows. It manages the installation of the Codex CLI, provides a secure proxy to the Responses API, and allows developers to control filesystem and network permissions via permission profiles and safety strategies. It supports both OpenAI and Azure OpenAI backends and can be used for tasks such as automated pull request reviews.

Tokens
5.4K
Snippets
14
Records
26
Agent score
76%

What's inside openai-codex-action

  1. Defend against prompt injection from untrusted input

    main

    Codex uses various sources of context to perform tasks, but these can be used for prompt injection attacks. Treat the following sources as untrusted input:

    • Pull requests: Specifically pull request bodies, which can hide malicious instructions in HTML comments (<!-- -->).
    • Commit messages: Individual commit messages within a PR can be read by Codex.
    • Repository instruction files: Files like AGENTS.md, AGENTS.override.md, or fallback project docs.
    • Screenshots/Media: Images can be used as vehicles for injection.
  2. How Codex permission profiles work

    main

    Permission profiles independently describe filesystem and network access. They are the modern way to control Codex privileges, replacing the legacy sandbox input.

    Key usage rules:

    • To edit the repository: Use permission-profile: ":workspace". This is preferred over the legacy workspace-write fallback.
    • For read-only workflows: Use permission-profile: ":read-only".
    • Custom profiles: You can select a named profile defined in a config.toml under codex-home.
    • Compatibility: Permission profiles require Codex CLI 0.138.0 or later. Do not use them if pinning an older codex-version.
    • Exclusions:
      • permission-profile is mutually exclusive with sandbox and output-schema-file (if using output-schema).
      • safety-strategy: read-only forces the legacy sandbox and cannot be combined with a permission profile.
      • Do not set sandbox_mode in codex-args or config.toml when using a profile, as this opts out of the profile system.
    - name: Run Codex with a permission profile
      uses: openai/codex-action@v1
      with:
        openai-api-key: ${{ secrets.OPENAI_API_KEY }}
        permission-profile: ":workspace"
        prompt: Review the public change.
  3. Configure Azure OpenAI as the backend

    main

    To use Azure OpenAI instead of the default OpenAI endpoint, you must configure the responses-api-endpoint and openai-api-key inputs.

    Requirements

    • responses-api-endpoint: Must be the full URL including the v1/responses suffix (e.g., https://YOUR_RESOURCE.openai.azure.com/openai/v1/responses).
    • openai-api-key: Must be a valid key compatible with the Authorization: Bearer <KEY> header for your Azure endpoint.

    Example Configuration

    - name: Start Codex proxy
      uses: openai/codex-action@v1
      with:
        openai-api-key: ${{ secrets.AZURE_OPENAI_API_KEY }}
        responses-api-endpoint: "https://bolinfest-7804-resource.cognitiveservices.azure.com/openai/v1/responses"
        prompt: "Debug all the things."
  4. Avoid shell injection in workflow steps

    main

    To prevent shell injection, do not splice untrusted values (like branch names, issue titles, or PR comments) directly into a run: script using ${{ ... }} expressions. Instead, pass these values through the env: block and use quoted shell variables to consume them.

    - name: Safe shell usage
      env:
        PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
      run: |
        git fetch origin "$PR_BASE_REF"
  5. Best practices for using Codex GitHub Action

    main

    Follow these tips for optimal integration:

    • Checkout first: Always run this action after actions/checkout@v5 so Codex can access your repository files.
    • Custom Endpoints: To use non-default endpoints (like Azure), set responses-api-endpoint. The proxy handles the Authorization: Bearer header automatically.
    • Privileged Functionality: If Codex needs specific privileged access, consider running a local MCP (Model Context Protocol) server and configuring Codex to use it.
    • CLI Customization: Use codex-args to pass flags directly to the CLI, or create a config.toml in codex-home for persistent configuration.
    • Permission Profiles: Prefer using permission profiles (e.g., :workspace for workspace editing) over legacy sandbox flags.
    • Subsequent Calls: Once the action has been run once with openai-api-key in a job, you can call the codex CLI directly in later scripts without re-providing the prompt or prompt-file.
  6. Limit who can run your Codex workflow

    main

    By default, openai/codex-action only runs for users with write access to your repository. You can expand this list using specific inputs, but do so with caution:

    • allow-users: A list of specific users allowed to run the workflow.
    • allow-bots: Allows trusted GitHub bot actors (e.g., github-actions[bot]).
    • allow-bot-users: A list of custom trusted bots. Note: This does not support the wildcard *; you must list each bot explicitly.
  7. Run openai/codex-action as the last step in a job

    main

    It is recommended to run openai/codex-action as the final step in a job, especially when using loose permissions. Because Codex can spawn background processes, overwrite source code of other actions, or modify configuration files (like .git/hooks), the state of the host is not guaranteed after Codex exits.

    To maintain a clean workflow, consider taking the output of the action and passing it to a subsequent, fresh job.

  8. Install and use the Codex GitHub Action

    main

    The openai/codex-action allows you to run Codex from a GitHub Actions workflow with controlled privileges. It handles the installation of the Codex CLI and configures a secure proxy to the Responses API.

    To use the action, you must provide an API key (e.g., OPENAI_API_KEY or AZURE_OPENAI_API_KEY) as a GitHub Actions secret.

    - name: Run Codex
      uses: openai/codex-action@v1
      with:
        openai-api-key: ${{ secrets.OPENAI_API_KEY }}
        prompt: "Your prompt here"
  9. Protect your OPENAI_API_KEY

    main

    To ensure your OPENAI_API_KEY remains secret, you must use either drop-sudo or unprivileged-user as your safety-strategy.

    Even if you use a read-only filesystem profile, the presence of passwordless sudo (the default on GitHub-hosted runners) can allow a process to access sensitive information via procfs. Using drop-sudo or unprivileged-user mitigates this risk.

  10. Configure the `safety-strategy` input

    main

    The safety-strategy input determines the level of access Codex has on the runner. This is critical for security when sensitive secrets like openai-api-key are present.

    Available Strategies

    • drop-sudo (default): On Linux and macOS, the action revokes the default user's sudo membership. Codex runs without superuser privileges. Note: This change persists for the rest of the job, so subsequent steps cannot use sudo.
    • unprivileged-user: Runs Codex as the user specified via the codex-user input. Use this on self-managed runners with a pre-created unprivileged account.
    • read-only: Executes Codex in a read-only sandbox. Codex can view files but cannot mutate the filesystem or access the network directly.
    • unsafe: No privilege reduction. Codex runs as the default runner user (typically with sudo).

    OS Support and Constraints

    • Windows: Only unsafe is supported. The action will fail if any other strategy is selected.
    • Linux/macOS: All options are supported.
    • GitHub-hosted Linux: The action automatically handles unprivileged user namespaces and AppArmor settings to prevent common permission errors.
  11. Define Prompt Sources

    main

    You can provide the prompt to Codex in two ways using the PromptSource type:

    • inline: Provide the prompt text directly via the content field.
    • file: Provide a path to a file via the path field; the action will read the file contents.
    // Inline prompt
    const prompt = { type: 'inline', content: 'Write a hello world script' };
    
    // File-based prompt
    const prompt = { type: 'file', path: './prompts/task.txt' };
    export type PromptSource =
      | { type: "inline"; content: string; }
      | { type: "file"; path: string; };
  12. Configure Safety Strategies and Sandbox Modes

    main

    The Codex action uses SafetyStrategy and SandboxMode to control the level of access and risk during execution.

    Safety Strategies

    • drop-sudo: Default behavior.
    • read-only: Forces the legacy read-only sandbox mode.
    • unprivileged-user: Runs the command as a specific user via sudo -u <codexUser>. This is not supported on Windows and requires codexUser to be specified.
    • unsafe: No restrictions.

    Sandbox Modes

    When using the sandbox permission type, you can choose from:

    • read-only: Restricted access.
    • workspace-write: Allows writing to the workspace.
    • danger-full-access: Unrestricted access.

    Note: permissionProfile and sandbox are mutually exclusive. You cannot combine a permission profile with legacy sandbox settings.