Skip to content

Commit 58940a7

Browse files
fix(sandbox): report a missing Daytona snapshot permission as 403, not an invalid key (#477)
* fix(sandbox): report a missing Daytona snapshot permission as 403, not an invalid key Daytona scopes snapshot writes separately from sandbox writes, so an API key can be entirely valid — able to create sandboxes — and still be refused when TrueForge registers the release snapshot it clones every sandbox from. That refusal came back as 422 "Daytona rejected the API key — check the credentials", which points the user at the one thing that is not wrong. The documented fix is to grant Snapshots (write); the error told them to replace the key. The cause is that isDaytonaAuthError treated 401 and 403 as one condition. 401 means the credentials were not accepted and retrying the same key cannot succeed; 403 means Daytona authenticated the key and declined the operation. Collapsing them left the PUT handler with no way to tell "wrong key" from "key missing a permission", so both took the credentials branch. isDaytonaAuthError now matches 401 only, and a new isDaytonaPermissionError matches 403. The settings write maps the permission case to 403 and names write:snapshots in the message, so the response says which grant is missing rather than implying the key is bad. putSandboxProviderRoute declares the 403 so the OpenAPI document describes it. Behaviour change: the forbidden case now answers 403 where it previously answered 422. That is the correction asked for on the issue. It required updating an existing assertion that expected 422 for a DaytonaError(403); that test's real subject is that config is not persisted when the build call fails, and it still asserts the follow-up GET returns 404, so the coverage is preserved and only the status it waits on moved. Not addressed here: the report also suggests falling back to running the image directly when write:snapshots is absent. That decides whether snapshot registration stays mandatory, which is a product question about the provider's setup contract rather than an error-mapping bug, and it would change behaviour for keys that work today. Verified: tests/unit/apis/sandboxProviders.test.ts 13 passed. Full package unit suite 270 passed, 6 failed; those six are LocalSandboxProvider and Code Mode UDS on a Windows host (unix sockets and 0700 chmod) and fail identically on a clean checkout, which runs 269 passed and the same 6 failed. tsc and eslint clean. The five changed files are Prettier clean; the repo-wide format:check failure is a CRLF artifact of a Windows checkout and hits 779 files that this change does not touch. Claude-Session: https://claude.ai/code/session_01XCubdWKdbhQ2vVuDdYu68E * fix(sandbox): use the review's wording for the Daytona permission error The 403 body now reads exactly as asked on review: Daytona denied access: the API key is missing required permissions. Grant write:sandboxes, write:snapshots, and delete:snapshots on the key in the Daytona dashboard, then try again. It names every grant the setup needs and where to add them, instead of only write:snapshots. Nothing else moved: the 401/403 split, the status mapping, and the OpenAPI 403 are unchanged. The assertion in tests/unit/apis/sandboxProviders.test.ts and the changeset text follow the new string. Verified against a standalone server with the Daytona API pointed at a local stub that answers POST /snapshots with 403 "Access denied": PUT /api/v1/settings/sandbox-providers returns 403 with this body, and the Configure Daytona modal renders it as four wrapped lines above Save, in both light and dark themes, with no truncation or layout shift. Claude-Session: https://claude.ai/code/session_01Pdf58MEtVwuM2JRD1gxqPt * remove comment * fix(sandbox): update error handling for Daytona API key permissions * fix(sandbox): simplify Daytona API key error message --------- Co-authored-by: Sujai Kumar Gupta <120925871+thesujai@users.noreply.github.com> Co-authored-by: thesujai <sujayscience1234@gmail.com>
1 parent a3a1395 commit 58940a7

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@truefoundry/trueforge": patch
3+
---
4+
5+
Report a Daytona key that cannot register snapshots as missing key permissions (403) instead of an invalid API key (422), and name the grants to add in the Daytona dashboard.

‎packages/trueforge/src/apis/sandboxProviders.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getSandboxProviderRoute, putSandboxProviderRoute } from '../routes/sand
77
import {
88
checkSnapshotStatus,
99
isDaytonaAuthError,
10+
isDaytonaPermissionError,
1011
toDaytonaSandboxProvider,
1112
toSandboxStatus,
1213
} from '../sandbox/providerUtils';
@@ -106,6 +107,17 @@ export function createSandboxProvidersRouter<TTransaction>(deps: SandboxProvider
106107
if (isDaytonaAuthError(error)) {
107108
return c.json({ error: { message: 'Daytona rejected the API key — check the credentials' } }, 422);
108109
}
110+
if (isDaytonaPermissionError(error)) {
111+
return c.json(
112+
{
113+
error: {
114+
message:
115+
'Daytona denied access: the API key is missing required permissions. Grant write:sandboxes, write:snapshots, and delete:snapshots on the key in the Daytona dashboard, then try again.',
116+
},
117+
},
118+
422,
119+
);
120+
}
109121
throw error;
110122
}
111123
};

‎packages/trueforge/src/sandbox/providerUtils.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import {
1616
type SandboxStatus,
1717
} from '../schemas/sandboxProvider';
1818

19-
/** Daytona rejected the credentials (401 unauthorized / 403 forbidden); retrying the same key cannot succeed. */
19+
/** Daytona rejected the credentials (401 unauthorized); retrying the same key cannot succeed. */
2020
export function isDaytonaAuthError(error: unknown): boolean {
21-
return error instanceof DaytonaError && (error.statusCode === 401 || error.statusCode === 403);
21+
return error instanceof DaytonaError && error.statusCode === 401;
22+
}
23+
24+
export function isDaytonaPermissionError(error: unknown): boolean {
25+
return error instanceof DaytonaError && error.statusCode === 403;
2226
}
2327

2428
/**

‎packages/trueforge/tests/unit/apis/sandboxProviders.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Stub the Daytona-touching helpers so the router never talks to Daytona: the PUT path builds via
2-
// toDaytonaSandboxProvider, and the GET path refreshes via checkSnapshotStatus. isDaytonaAuthError
3-
// and toSandboxStatus stay real so the auth-error mapping and PUT wire shape are exercised.
2+
// toDaytonaSandboxProvider, and the GET path refreshes via checkSnapshotStatus. isDaytonaAuthError,
3+
// isDaytonaPermissionError and toSandboxStatus stay real so the error mapping and PUT wire shape are
4+
// exercised.
45
jest.mock('../../../src/sandbox/providerUtils', () => {
56
const actual = jest.requireActual('../../../src/sandbox/providerUtils');
67
return { ...actual, toDaytonaSandboxProvider: jest.fn(), checkSnapshotStatus: jest.fn() };
@@ -165,6 +166,20 @@ describe('sandboxProviders router', () => {
165166
expect(response.status).toBe(422);
166167
});
167168

169+
it('PUT returns 422 naming the permissions when the key cannot register snapshots', async () => {
170+
mockProviderFactory.mockReturnValue(
171+
stubProvider({ buildImage: jest.fn().mockRejectedValue(new DaytonaError('Access denied', 403)) }),
172+
);
173+
const response = await settingsRouter.request('/', putInit(putBody));
174+
expect(response.status).toBe(422);
175+
expect(await response.json()).toEqual({
176+
error: {
177+
message:
178+
'Daytona denied access: the API key is missing required permissions. Grant write:sandboxes, write:snapshots, and delete:snapshots on the key in the Daytona dashboard, then try again.',
179+
},
180+
});
181+
});
182+
168183
it('PUT does not persist config when the build call fails auth', async () => {
169184
const { settingsRouter: router } = await createRouters();
170185
mockProviderFactory.mockReturnValue(

0 commit comments

Comments
 (0)