Files
PaperClipAI/packages/shared/src/account-handle.ts
T
Nicky LeachandPaperclip 9064cfd09e feat(codex-local): give each Codex account its own home and path secret (#12709)
## Thinking Path

> - Paperclip is the control plane for companies that use AI agents for
work
> - Local adapters connect Paperclip agents to provider command line
tools
> - The Codex adapter stores login data in a shared company home
> - A shared home cannot keep credentials for more than one Codex
account
> - This pull request gives each account a safe home and a matching
company secret
> - The benefit is that one company can use multiple Codex accounts at
the same time

## Linked Issues or Issue Description

**Problem or motivation**

A company can hold only one Codex subscription credential because device
login uses one shared home. A second account cannot log in without
replacing or conflicting with the first credential.

**Proposed solution**

This change validates the vendor account identifier, stores each
credential in its own home, and creates a company secret that points to
that home. Repeat login calls return success when the matching secret
already exists.

**Roadmap alignment**

The change supports the roadmap goal for centrally managed secrets with
scoped access and audited resolution.

**Additional context**

The security review returned approve with no blocking finding. The
branch adds shared account-handle validation and tests for device login
and the Codex local adapter.

## What Changed

- Add strict allowlist validation for Codex account handles.
- Store each Codex account credential in a separate home under the Codex
cache root.
- Verify that the resolved account home stays inside the cache root.
- Create the `CODEX_HOME_<handle>` company secret for each account.
- Keep repeat and concurrent login calls safe and idempotent.
- Add shared helper and route, adapter, and validation tests.

## Verification

- `pnpm --filter @paperclipai/adapter-codex-local test` passes with 343
tests.
- `pnpm --filter @paperclipai/server test
src/__tests__/agent-device-login-routes.test.ts` passes with 25 tests.
- The adapter suite passes with 23 tests.
- The shared package and Codex adapter typechecks pass.
- Continuous integration must pass on every check before merge.

## Risks

The account handle becomes part of a directory path and secret name. The
strict allowlist and root containment check reduce path traversal risk.
Existing single-account homes remain unchanged unless a new device login
creates an account-specific home.

## Model Used

OpenAI GPT-5 (exact runtime model ID: gpt-5), with tool use and code
execution. The runtime context window is not exposed in this run.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-09-02 14:46:53 -07:00

45 lines
2.1 KiB
TypeScript

// A canonical account handle names one login account inside one company. Two
// downstream sinks each guard the raw account identifier with a different
// validator: a directory-segment denylist, and the secret-key allowlist
// (`packages/shared/src/validators/secret.ts`). `toAccountHandle` accepts a
// value only when it satisfies both, so one handle can safely name both a
// directory and a secret.
/** The longest accepted account handle. The secret key limit is 120
* characters, and the longest name prefix a caller adds is `CODEX_HOME_` at
* 11 characters, so 100 leaves headroom. */
export const ACCOUNT_HANDLE_MAX_LENGTH = 100;
// The same character class the secret key schema uses.
const ACCOUNT_HANDLE_PATTERN = /^[a-zA-Z0-9_.-]+$/;
/**
* Converts a raw account identifier into a safe account handle. Returns
* `null` when the value cannot become one instead of throwing, so a caller
* decides how to fail the operation.
*
* The identifier is the account's identity key: it names one directory and
* one secret, so two distinct identifiers must never resolve to the same
* handle. A caller must never normalize the value before this check (for
* example, by trimming it) — normalizing first can alias a distinct
* identifier onto an already-claimed handle and let a login accept a
* different account's credential home as its own.
*
* An input becomes a handle only when every rule below is true:
*
* - It carries no leading or trailing whitespace.
* - It matches `/^[a-zA-Z0-9_.-]+$/`.
* - It is {@link ACCOUNT_HANDLE_MAX_LENGTH} characters or shorter.
* - It is neither `.` nor `..`.
* - It does not start with `-` (a leading `-` reads as a command-line option).
*/
export function toAccountHandle(rawAccountId: string): string | null {
if (typeof rawAccountId !== "string" || rawAccountId.length === 0) return null;
if (rawAccountId.trim() !== rawAccountId) return null;
if (!ACCOUNT_HANDLE_PATTERN.test(rawAccountId)) return null;
if (rawAccountId.length > ACCOUNT_HANDLE_MAX_LENGTH) return null;
if (rawAccountId === "." || rawAccountId === "..") return null;
if (rawAccountId.startsWith("-")) return null;
return rawAccountId;
}