Skip to content

Commit 703f847

Browse files
authored
fix(cloud-agent-next): rotate shared sandbox IDs (#3879)
1 parent 7ddf7b3 commit 703f847

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

services/cloud-agent-next/src/sandbox-id.test.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,45 @@ describe('generateSandboxId', () => {
5353
const id2 = await generateSandboxId(undefined, 'org-id', 'user-id', 'session-b');
5454
expect(id1).toBe(id2);
5555
});
56+
57+
it.each([
58+
[
59+
'org',
60+
'org-id',
61+
undefined,
62+
'org-7d891a9e4905bb0d5ff8dffcb99ba76973039c70340665b0',
63+
'org-aa6ba1f356e062c430f121b97b5fd9cfd64c51487e5f28c5',
64+
],
65+
[
66+
'usr',
67+
undefined,
68+
undefined,
69+
'usr-e4da69a737a38f1fc3283e8159b965e9d88f13d84c23cab1',
70+
'usr-3c060fe2d53dd0b6e7a7e03084b290b64c8e0f67a8988161',
71+
],
72+
[
73+
'bot',
74+
'org-id',
75+
'reviewer',
76+
'bot-b7b5ae452e738ff4c3e88238a0bd903edb1039b22314e3dc',
77+
'bot-4415e0ae1dcbda7236e3bf04b66f13344682f349eac4500d',
78+
],
79+
[
80+
'ubt',
81+
undefined,
82+
'reviewer',
83+
'ubt-5714320d8e828e8d428046c7f8601c126755f3e04d55b0d6',
84+
'ubt-fb0f08bd868516e812f84fc34ddc327364046281c8e4c978',
85+
],
86+
])(
87+
'should use the second shared sandbox ID generation for %s IDs',
88+
async (_prefix, orgId, botId, expectedId, legacyId) => {
89+
const id = await generateSandboxId(undefined, orgId, 'user-id', 'session', botId);
90+
91+
expect(id).toBe(expectedId);
92+
expect(id).not.toBe(legacyId);
93+
}
94+
);
5695
});
5796

5897
describe('prefix correctness', () => {
@@ -134,9 +173,9 @@ describe('generateSandboxId', () => {
134173
});
135174

136175
describe('per-session sandbox', () => {
137-
it('should produce a ses- prefixed ID for a per-session org', async () => {
176+
it('should preserve the existing per-session ID generation', async () => {
138177
const id = await generateSandboxId('my-org', 'my-org', 'user-id', 'agent_abc123');
139-
expect(id).toMatch(/^ses-[0-9a-f]{48}$/);
178+
expect(id).toBe('ses-51256c9fcd04ef0144d0afcdfb9ffb2abc280ff2e0bae370');
140179
});
141180

142181
it('should be exactly 52 characters', async () => {
@@ -199,7 +238,7 @@ describe('generateSandboxId', () => {
199238
});
200239

201240
describe('devcontainer sandbox', () => {
202-
it('should produce a dind- prefixed ID when devcontainer is true', async () => {
241+
it('should preserve the existing devcontainer ID generation', async () => {
203242
const id = await generateSandboxId(
204243
undefined,
205244
'org-id',
@@ -208,7 +247,7 @@ describe('generateSandboxId', () => {
208247
undefined,
209248
true
210249
);
211-
expect(id).toMatch(/^dind-[0-9a-f]{48}$/);
250+
expect(id).toBe('dind-51256c9fcd04ef0144d0afcdfb9ffb2abc280ff2e0bae370');
212251
});
213252

214253
it('should be exactly 53 characters', async () => {

services/cloud-agent-next/src/sandbox-id.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { SandboxId, Env } from './types.js';
22
import type { Sandbox } from '@cloudflare/sandbox';
33

4+
const SHARED_SANDBOX_ID_VERSION = 'shared-v2';
5+
46
/**
57
* Parses a comma-separated org ID list into a set.
68
* Returns an empty set when the value is falsy or blank.
@@ -79,5 +81,5 @@ export async function generateSandboxId(
7981
prefix = orgId ? 'org' : 'usr';
8082
}
8183

82-
return hashToSandboxId(originalFormat, prefix);
84+
return hashToSandboxId(`${SHARED_SANDBOX_ID_VERSION}:${originalFormat}`, prefix);
8385
}

0 commit comments

Comments
 (0)