@cloudflare/sandbox 0.0.0-46eb4e6 → 0.0.0-485cf61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -6
- package/Dockerfile +82 -18
- package/README.md +89 -824
- package/dist/chunk-3NEP4CNV.js +99 -0
- package/dist/chunk-3NEP4CNV.js.map +1 -0
- package/dist/chunk-6IYG2RIN.js +117 -0
- package/dist/chunk-6IYG2RIN.js.map +1 -0
- package/dist/chunk-HB44YO2A.js +2331 -0
- package/dist/chunk-HB44YO2A.js.map +1 -0
- package/dist/chunk-KPVMMMIP.js +105 -0
- package/dist/chunk-KPVMMMIP.js.map +1 -0
- package/dist/chunk-NNGBXDMY.js +89 -0
- package/dist/chunk-NNGBXDMY.js.map +1 -0
- package/dist/file-stream.d.ts +43 -0
- package/dist/file-stream.js +9 -0
- package/dist/file-stream.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/interpreter.d.ts +33 -0
- package/dist/interpreter.js +8 -0
- package/dist/interpreter.js.map +1 -0
- package/dist/request-handler.d.ts +18 -0
- package/dist/request-handler.js +12 -0
- package/dist/request-handler.js.map +1 -0
- package/dist/sandbox-CtlKjZwf.d.ts +583 -0
- package/dist/sandbox.d.ts +4 -0
- package/dist/sandbox.js +12 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/security.d.ts +35 -0
- package/dist/security.js +15 -0
- package/dist/security.js.map +1 -0
- package/dist/sse-parser.d.ts +28 -0
- package/dist/sse-parser.js +11 -0
- package/dist/sse-parser.js.map +1 -0
- package/package.json +11 -5
- package/src/clients/base-client.ts +297 -0
- package/src/clients/command-client.ts +118 -0
- package/src/clients/file-client.ts +272 -0
- package/src/clients/git-client.ts +95 -0
- package/src/clients/index.ts +63 -0
- package/src/{interpreter-client.ts → clients/interpreter-client.ts} +151 -171
- package/src/clients/port-client.ts +108 -0
- package/src/clients/process-client.ts +180 -0
- package/src/clients/sandbox-client.ts +41 -0
- package/src/clients/types.ts +81 -0
- package/src/clients/utility-client.ts +97 -0
- package/src/errors/adapter.ts +180 -0
- package/src/errors/classes.ts +469 -0
- package/src/errors/index.ts +105 -0
- package/src/file-stream.ts +119 -117
- package/src/index.ts +81 -69
- package/src/interpreter.ts +17 -8
- package/src/request-handler.ts +61 -7
- package/src/sandbox.ts +698 -495
- package/src/security.ts +20 -0
- package/startup.sh +7 -0
- package/tests/base-client.test.ts +328 -0
- package/tests/command-client.test.ts +407 -0
- package/tests/file-client.test.ts +643 -0
- package/tests/file-stream.test.ts +306 -0
- package/tests/git-client.test.ts +328 -0
- package/tests/port-client.test.ts +301 -0
- package/tests/process-client.test.ts +658 -0
- package/tests/sandbox.test.ts +465 -0
- package/tests/sse-parser.test.ts +291 -0
- package/tests/utility-client.test.ts +266 -0
- package/tests/wrangler.jsonc +35 -0
- package/tsconfig.json +9 -1
- package/vitest.config.ts +31 -0
- package/container_src/bun.lock +0 -76
- package/container_src/circuit-breaker.ts +0 -121
- package/container_src/control-process.ts +0 -784
- package/container_src/handler/exec.ts +0 -185
- package/container_src/handler/file.ts +0 -457
- package/container_src/handler/git.ts +0 -130
- package/container_src/handler/ports.ts +0 -314
- package/container_src/handler/process.ts +0 -568
- package/container_src/handler/session.ts +0 -92
- package/container_src/index.ts +0 -600
- package/container_src/interpreter-service.ts +0 -276
- package/container_src/isolation.ts +0 -1213
- package/container_src/mime-processor.ts +0 -255
- package/container_src/package.json +0 -18
- package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
- package/container_src/runtime/executors/python/ipython_executor.py +0 -338
- package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
- package/container_src/runtime/process-pool.ts +0 -464
- package/container_src/shell-escape.ts +0 -42
- package/container_src/startup.sh +0 -11
- package/container_src/types.ts +0 -131
- package/src/client.ts +0 -1048
- package/src/errors.ts +0 -219
- package/src/interpreter-types.ts +0 -390
- package/src/types.ts +0 -571
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// src/security.ts
|
|
2
|
+
var SecurityError = class extends Error {
|
|
3
|
+
constructor(message, code) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.name = "SecurityError";
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function validatePort(port) {
|
|
10
|
+
if (!Number.isInteger(port)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (port < 1024 || port > 65535) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const reservedPorts = [
|
|
17
|
+
3e3,
|
|
18
|
+
// Control plane port
|
|
19
|
+
8787
|
|
20
|
+
// Common wrangler dev port
|
|
21
|
+
];
|
|
22
|
+
if (reservedPorts.includes(port)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
function sanitizeSandboxId(id) {
|
|
28
|
+
if (!id || id.length > 63) {
|
|
29
|
+
throw new SecurityError(
|
|
30
|
+
"Sandbox ID must be 1-63 characters long.",
|
|
31
|
+
"INVALID_SANDBOX_ID_LENGTH"
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
if (id.startsWith("-") || id.endsWith("-")) {
|
|
35
|
+
throw new SecurityError(
|
|
36
|
+
"Sandbox ID cannot start or end with hyphens (DNS requirement).",
|
|
37
|
+
"INVALID_SANDBOX_ID_HYPHENS"
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const reservedNames = [
|
|
41
|
+
"www",
|
|
42
|
+
"api",
|
|
43
|
+
"admin",
|
|
44
|
+
"root",
|
|
45
|
+
"system",
|
|
46
|
+
"cloudflare",
|
|
47
|
+
"workers"
|
|
48
|
+
];
|
|
49
|
+
const lowerCaseId = id.toLowerCase();
|
|
50
|
+
if (reservedNames.includes(lowerCaseId)) {
|
|
51
|
+
throw new SecurityError(
|
|
52
|
+
`Reserved sandbox ID '${id}' is not allowed.`,
|
|
53
|
+
"RESERVED_SANDBOX_ID"
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return id;
|
|
57
|
+
}
|
|
58
|
+
function validateLanguage(language) {
|
|
59
|
+
if (!language) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const supportedLanguages = ["python", "python3", "javascript", "js", "node", "typescript", "ts"];
|
|
63
|
+
const normalized = language.toLowerCase();
|
|
64
|
+
if (!supportedLanguages.includes(normalized)) {
|
|
65
|
+
throw new SecurityError(
|
|
66
|
+
`Unsupported language '${language}'. Supported languages: python, javascript, typescript`,
|
|
67
|
+
"INVALID_LANGUAGE"
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function logSecurityEvent(event, details, severity = "medium") {
|
|
72
|
+
const logEntry = {
|
|
73
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
74
|
+
event,
|
|
75
|
+
severity,
|
|
76
|
+
...details
|
|
77
|
+
};
|
|
78
|
+
switch (severity) {
|
|
79
|
+
case "critical":
|
|
80
|
+
case "high":
|
|
81
|
+
console.error(`[SECURITY:${severity.toUpperCase()}] ${event}:`, JSON.stringify(logEntry));
|
|
82
|
+
break;
|
|
83
|
+
case "medium":
|
|
84
|
+
console.warn(`[SECURITY:${severity.toUpperCase()}] ${event}:`, JSON.stringify(logEntry));
|
|
85
|
+
break;
|
|
86
|
+
case "low":
|
|
87
|
+
console.info(`[SECURITY:${severity.toUpperCase()}] ${event}:`, JSON.stringify(logEntry));
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export {
|
|
93
|
+
SecurityError,
|
|
94
|
+
validatePort,
|
|
95
|
+
sanitizeSandboxId,
|
|
96
|
+
validateLanguage,
|
|
97
|
+
logSecurityEvent
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=chunk-3NEP4CNV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/security.ts"],"sourcesContent":["/**\n * Security utilities for URL construction and input validation\n *\n * This module contains critical security functions to prevent:\n * - URL injection attacks\n * - SSRF (Server-Side Request Forgery) attacks\n * - DNS rebinding attacks\n * - Host header injection\n * - Open redirect vulnerabilities\n */\n\nexport class SecurityError extends Error {\n constructor(message: string, public readonly code?: string) {\n super(message);\n this.name = 'SecurityError';\n }\n}\n\n/**\n * Validates port numbers for sandbox services\n * Only allows non-system ports to prevent conflicts and security issues\n */\nexport function validatePort(port: number): boolean {\n // Must be a valid integer\n if (!Number.isInteger(port)) {\n return false;\n }\n\n // Only allow non-system ports (1024-65535)\n if (port < 1024 || port > 65535) {\n return false;\n }\n\n // Exclude ports reserved by our system\n const reservedPorts = [\n 3000, // Control plane port\n 8787, // Common wrangler dev port\n ];\n\n if (reservedPorts.includes(port)) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Sanitizes and validates sandbox IDs for DNS compliance and security\n * Only enforces critical requirements - allows maximum developer flexibility\n */\nexport function sanitizeSandboxId(id: string): string {\n // Basic validation: not empty, reasonable length limit (DNS subdomain limit is 63 chars)\n if (!id || id.length > 63) {\n throw new SecurityError(\n 'Sandbox ID must be 1-63 characters long.',\n 'INVALID_SANDBOX_ID_LENGTH'\n );\n }\n\n // DNS compliance: cannot start or end with hyphens (RFC requirement)\n if (id.startsWith('-') || id.endsWith('-')) {\n throw new SecurityError(\n 'Sandbox ID cannot start or end with hyphens (DNS requirement).',\n 'INVALID_SANDBOX_ID_HYPHENS'\n );\n }\n\n // Prevent reserved names that cause technical conflicts\n const reservedNames = [\n 'www', 'api', 'admin', 'root', 'system',\n 'cloudflare', 'workers'\n ];\n\n const lowerCaseId = id.toLowerCase();\n if (reservedNames.includes(lowerCaseId)) {\n throw new SecurityError(\n `Reserved sandbox ID '${id}' is not allowed.`,\n 'RESERVED_SANDBOX_ID'\n );\n }\n\n return id;\n}\n\n\n/**\n * Validates language for code interpreter\n * Only allows supported languages\n */\nexport function validateLanguage(language: string | undefined): void {\n if (!language) {\n return; // undefined is valid, will default to python\n }\n\n const supportedLanguages = ['python', 'python3', 'javascript', 'js', 'node', 'typescript', 'ts'];\n const normalized = language.toLowerCase();\n\n if (!supportedLanguages.includes(normalized)) {\n throw new SecurityError(\n `Unsupported language '${language}'. Supported languages: python, javascript, typescript`,\n 'INVALID_LANGUAGE'\n );\n }\n}\n\n/**\n * Logs security events for monitoring\n */\nexport function logSecurityEvent(\n event: string,\n details: Record<string, any>,\n severity: 'low' | 'medium' | 'high' | 'critical' = 'medium'\n): void {\n const logEntry = {\n timestamp: new Date().toISOString(),\n event,\n severity,\n ...details\n };\n\n switch (severity) {\n case 'critical':\n case 'high':\n console.error(`[SECURITY:${severity.toUpperCase()}] ${event}:`, JSON.stringify(logEntry));\n break;\n case 'medium':\n console.warn(`[SECURITY:${severity.toUpperCase()}] ${event}:`, JSON.stringify(logEntry));\n break;\n case 'low':\n console.info(`[SECURITY:${severity.toUpperCase()}] ${event}:`, JSON.stringify(logEntry));\n break;\n }\n}\n"],"mappings":";AAWO,IAAM,gBAAN,cAA4B,MAAM;AAAA,EACvC,YAAY,SAAiC,MAAe;AAC1D,UAAM,OAAO;AAD8B;AAE3C,SAAK,OAAO;AAAA,EACd;AACF;AAMO,SAAS,aAAa,MAAuB;AAElD,MAAI,CAAC,OAAO,UAAU,IAAI,GAAG;AAC3B,WAAO;AAAA,EACT;AAGA,MAAI,OAAO,QAAQ,OAAO,OAAO;AAC/B,WAAO;AAAA,EACT;AAGA,QAAM,gBAAgB;AAAA,IACpB;AAAA;AAAA,IACA;AAAA;AAAA,EACF;AAEA,MAAI,cAAc,SAAS,IAAI,GAAG;AAChC,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAMO,SAAS,kBAAkB,IAAoB;AAEpD,MAAI,CAAC,MAAM,GAAG,SAAS,IAAI;AACzB,UAAM,IAAI;AAAA,MACR;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAGA,MAAI,GAAG,WAAW,GAAG,KAAK,GAAG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|
+
import {
|
|
2
|
+
validateLanguage
|
|
3
|
+
} from "./chunk-3NEP4CNV.js";
|
|
4
|
+
|
|
5
|
+
// src/interpreter.ts
|
|
6
|
+
import {
|
|
7
|
+
Execution,
|
|
8
|
+
ResultImpl
|
|
9
|
+
} from "@repo/shared";
|
|
10
|
+
var CodeInterpreter = class {
|
|
11
|
+
interpreterClient;
|
|
12
|
+
contexts = /* @__PURE__ */ new Map();
|
|
13
|
+
constructor(sandbox) {
|
|
14
|
+
this.interpreterClient = sandbox.client.interpreter;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a new code execution context
|
|
18
|
+
*/
|
|
19
|
+
async createCodeContext(options = {}) {
|
|
20
|
+
validateLanguage(options.language);
|
|
21
|
+
const context = await this.interpreterClient.createCodeContext(options);
|
|
22
|
+
this.contexts.set(context.id, context);
|
|
23
|
+
return context;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Run code with optional context
|
|
27
|
+
*/
|
|
28
|
+
async runCode(code, options = {}) {
|
|
29
|
+
let context = options.context;
|
|
30
|
+
if (!context) {
|
|
31
|
+
const language = options.language || "python";
|
|
32
|
+
context = await this.getOrCreateDefaultContext(language);
|
|
33
|
+
}
|
|
34
|
+
const execution = new Execution(code, context);
|
|
35
|
+
await this.interpreterClient.runCodeStream(context.id, code, options.language, {
|
|
36
|
+
onStdout: (output) => {
|
|
37
|
+
execution.logs.stdout.push(output.text);
|
|
38
|
+
if (options.onStdout) return options.onStdout(output);
|
|
39
|
+
},
|
|
40
|
+
onStderr: (output) => {
|
|
41
|
+
execution.logs.stderr.push(output.text);
|
|
42
|
+
if (options.onStderr) return options.onStderr(output);
|
|
43
|
+
},
|
|
44
|
+
onResult: async (result) => {
|
|
45
|
+
execution.results.push(new ResultImpl(result));
|
|
46
|
+
if (options.onResult) return options.onResult(result);
|
|
47
|
+
},
|
|
48
|
+
onError: (error) => {
|
|
49
|
+
execution.error = error;
|
|
50
|
+
if (options.onError) return options.onError(error);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return execution;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Run code and return a streaming response
|
|
57
|
+
*/
|
|
58
|
+
async runCodeStream(code, options = {}) {
|
|
59
|
+
let context = options.context;
|
|
60
|
+
if (!context) {
|
|
61
|
+
const language = options.language || "python";
|
|
62
|
+
context = await this.getOrCreateDefaultContext(language);
|
|
63
|
+
}
|
|
64
|
+
const response = await this.interpreterClient.doFetch("/api/execute/code", {
|
|
65
|
+
method: "POST",
|
|
66
|
+
headers: {
|
|
67
|
+
"Content-Type": "application/json",
|
|
68
|
+
Accept: "text/event-stream"
|
|
69
|
+
},
|
|
70
|
+
body: JSON.stringify({
|
|
71
|
+
context_id: context.id,
|
|
72
|
+
code,
|
|
73
|
+
language: options.language
|
|
74
|
+
})
|
|
75
|
+
});
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
|
|
78
|
+
throw new Error(
|
|
79
|
+
errorData.error || `Failed to execute code: ${response.status}`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (!response.body) {
|
|
83
|
+
throw new Error("No response body for streaming execution");
|
|
84
|
+
}
|
|
85
|
+
return response.body;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* List all code contexts
|
|
89
|
+
*/
|
|
90
|
+
async listCodeContexts() {
|
|
91
|
+
const contexts = await this.interpreterClient.listCodeContexts();
|
|
92
|
+
for (const context of contexts) {
|
|
93
|
+
this.contexts.set(context.id, context);
|
|
94
|
+
}
|
|
95
|
+
return contexts;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Delete a code context
|
|
99
|
+
*/
|
|
100
|
+
async deleteCodeContext(contextId) {
|
|
101
|
+
await this.interpreterClient.deleteCodeContext(contextId);
|
|
102
|
+
this.contexts.delete(contextId);
|
|
103
|
+
}
|
|
104
|
+
async getOrCreateDefaultContext(language) {
|
|
105
|
+
for (const context of this.contexts.values()) {
|
|
106
|
+
if (context.language === language) {
|
|
107
|
+
return context;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return this.createCodeContext({ language });
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export {
|
|
115
|
+
CodeInterpreter
|
|
116
|
+
};
|
|
117
|
+
//# sourceMappingURL=chunk-6IYG2RIN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/interpreter.ts"],"sourcesContent":["import {\n type CodeContext,\n type CreateContextOptions,\n Execution,\n type ExecutionError,\n type OutputMessage,\n type Result,\n ResultImpl,\n type RunCodeOptions,\n} from \"@repo/shared\";\nimport type { InterpreterClient } from \"./clients/interpreter-client.js\";\nimport type { Sandbox } from \"./sandbox.js\";\nimport { validateLanguage } from \"./security.js\";\n\nexport class CodeInterpreter {\n private interpreterClient: InterpreterClient;\n private contexts = new Map<string, CodeContext>();\n\n constructor(sandbox: Sandbox) {\n // In init-testing architecture, client is a SandboxClient with an interpreter property\n this.interpreterClient = (sandbox.client as any).interpreter as InterpreterClient;\n }\n\n /**\n * Create a new code execution context\n */\n async createCodeContext(\n options: CreateContextOptions = {}\n ): Promise<CodeContext> {\n // Validate language before sending to container\n validateLanguage(options.language);\n\n const context = await this.interpreterClient.createCodeContext(options);\n this.contexts.set(context.id, context);\n return context;\n }\n\n /**\n * Run code with optional context\n */\n async runCode(\n code: string,\n options: RunCodeOptions = {}\n ): Promise<Execution> {\n // Get or create context\n let context = options.context;\n if (!context) {\n // Try to find or create a default context for the language\n const language = options.language || \"python\";\n context = await this.getOrCreateDefaultContext(language);\n }\n\n // Create execution object to collect results\n const execution = new Execution(code, context);\n\n // Stream execution\n await this.interpreterClient.runCodeStream(context.id, code, options.language, {\n onStdout: (output: OutputMessage) => {\n execution.logs.stdout.push(output.text);\n if (options.onStdout) return options.onStdout(output);\n },\n onStderr: (output: OutputMessage) => {\n execution.logs.stderr.push(output.text);\n if (options.onStderr) return options.onStderr(output);\n },\n onResult: async (result: Result) => {\n execution.results.push(new ResultImpl(result) as any);\n if (options.onResult) return options.onResult(result);\n },\n onError: (error: ExecutionError) => {\n execution.error = error;\n if (options.onError) return options.onError(error);\n },\n });\n\n return execution;\n }\n\n /**\n * Run code and return a streaming response\n */\n async runCodeStream(\n code: string,\n options: RunCodeOptions = {}\n ): Promise<ReadableStream> {\n // Get or create context\n let context = options.context;\n if (!context) {\n const language = options.language || \"python\";\n context = await this.getOrCreateDefaultContext(language);\n }\n\n // Create streaming response\n // Note: doFetch is protected but we need direct access for raw stream response\n const response = await (this.interpreterClient as any).doFetch(\"/api/execute/code\", {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Accept: \"text/event-stream\",\n },\n body: JSON.stringify({\n context_id: context.id,\n code,\n language: options.language,\n }),\n });\n\n if (!response.ok) {\n const errorData = (await response\n .json()\n .catch(() => ({ error: \"Unknown error\" }))) as { error?: string };\n throw new Error(\n errorData.error || `Failed to execute code: ${response.status}`\n );\n }\n\n if (!response.body) {\n throw new Error(\"No response body for streaming execution\");\n }\n\n return response.body;\n }\n\n /**\n * List all code contexts\n */\n async listCodeContexts(): Promise<CodeContext[]> {\n const contexts = await this.interpreterClient.listCodeContexts();\n\n // Update local cache\n for (const context of contexts) {\n this.contexts.set(context.id, context);\n }\n\n return contexts;\n }\n\n /**\n * Delete a code context\n */\n async deleteCodeContext(contextId: string): Promise<void> {\n await this.interpreterClient.deleteCodeContext(contextId);\n this.contexts.delete(contextId);\n }\n\n private async getOrCreateDefaultContext(\n language: \"python\" | \"javascript\" | \"typescript\"\n ): Promise<CodeContext> {\n // Check if we have a cached context for this language\n for (const context of this.contexts.values()) {\n if (context.language === language) {\n return context;\n }\n }\n\n // Create new default context\n return this.createCodeContext({ language });\n }\n}\n"],"mappings":";;;;;AAAA;AAAA,EAGE;AAAA,EAIA;AAAA,OAEK;AAKA,IAAM,kBAAN,MAAsB;AAAA,EACnB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