Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Option for providing system user during upload/download url creation (#784)

Jiri Svecenycb3a009ac7d1
diff --git a/.changeset/nervous-hornets-flow.md b/.changeset/nervous-hornets-flow.md
new file mode 100644
index 000000000..f354b7420
--- /dev/null
+++ b/.changeset/nervous-hornets-flow.md
@@ -0,0 +1,5 @@
+---
+'e2b': minor
+---
+
+File upload url with optional user parameter
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index 097891bba..0dc714757 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -3,7 +3,7 @@ import { createConnectTransport } from '@connectrpc/connect-web'
import {
ConnectionConfig,
ConnectionOpts,
- defaultUsername,
+ defaultUsername, Username,
} from '../connectionConfig'
import { EnvdApiClient, handleEnvdApiError } from '../envd/api'
import { createRpcLogger } from '../logs'
@@ -66,6 +66,11 @@ export interface SandboxUrlOpts {
* Optional parameter to set the expiration time for the signature.
*/
useSignatureExpiration?: number
+
+ /**
+ * User that will be used to access the file.
+ */
+ user?: Username
}
/**
@@ -401,13 +406,14 @@ export class Sandbox extends SandboxApi {
throw new Error('Signature expiration can be used only when signature is set to true.')
}
+ const username = opts.user ?? defaultUsername
const filePath = path ?? ''
- const fileUrl = this.fileUrl(filePath, defaultUsername)
+ const fileUrl = this.fileUrl(filePath, username)
if (opts.useSignature) {
const url = new URL(fileUrl)
const sig = getSignature(
- { path: filePath, operation: 'write', user: defaultUsername, expirationInSeconds: opts.useSignatureExpiration, envdAccessToken: this.envdAccessToken}
+ { path: filePath, operation: 'write', user: username, expirationInSeconds: opts.useSignatureExpiration, envdAccessToken: this.envdAccessToken}
)
url.searchParams.set('signature', sig.signature)
@@ -441,12 +447,13 @@ export class Sandbox extends SandboxApi {
throw new Error('Signature expiration can be used only when signature is set to true.')
}
- const fileUrl = this.fileUrl(path, defaultUsername)
+ const username = opts.user ?? defaultUsername
+ const fileUrl = this.fileUrl(path, username)
if (opts.useSignature) {
const url = new URL(fileUrl)
const sig = getSignature(
- { path, operation: 'read', user: defaultUsername, expirationInSeconds: opts.useSignatureExpiration, envdAccessToken: this.envdAccessToken}
+ { path, operation: 'read', user: username, expirationInSeconds: opts.useSignatureExpiration, envdAccessToken: this.envdAccessToken}
)
url.searchParams.set('signature', sig.signature)
diff --git a/packages/js-sdk/tests/sandbox/files/signing.test.ts b/packages/js-sdk/tests/sandbox/files/signing.test.ts
index 7fbdd910d..8831caf45 100644
--- a/packages/js-sdk/tests/sandbox/files/signing.test.ts
+++ b/packages/js-sdk/tests/sandbox/files/signing.test.ts
@@ -52,6 +52,29 @@ test.skipIf(isDebug)('test access file with valid signing', async () => {
await sbx.kill()
})
+test.skipIf(isDebug)('test access file with valid signing as root', async () => {
+ const sbx = await Sandbox.create(template, {
+ timeoutMs: timeout,
+ secure: true,
+ })
+ await sbx.files.write('hello.txt', 'hello world', { user: 'root' })
+
+ const fileUrlWithSigning = sbx.downloadUrl('hello.txt', {
+ user: 'root',
+ useSignature: true,
+ useSignatureExpiration: 10_000,
+ })
+
+ const res = await fetch(fileUrlWithSigning)
+ const resBody = await res.text()
+ const resStatus = res.status
+
+ assert.equal(resStatus, 200)
+ assert.equal(resBody, 'hello world')
+
+ await sbx.kill()
+})
+
test.skipIf(isDebug)('test upload file with valid signing', async () => {
const sbx = await Sandbox.create(template, {
timeoutMs: timeout,
@@ -77,6 +100,33 @@ test.skipIf(isDebug)('test upload file with valid signing', async () => {
await sbx.kill()
})
+test.skipIf(isDebug)('test upload file with valid signing as root user', async () => {
+ const sbx = await Sandbox.create(template, {
+ timeoutMs: timeout,
+ secure: true,
+ })
+
+ const fileUrlWithSigning = sbx.uploadUrl('hello.txt', {
+ user: 'root',
+ useSignature: true,
+ useSignatureExpiration: 10_000,
+ })
+
+ const form = new FormData()
+ form.append('file', 'file content')
+
+ const res = await fetch(fileUrlWithSigning, { method: 'POST', body: form })
+ const resBody = await res.text()
+ const resStatus = res.status
+
+ assert.equal(resStatus, 200)
+ assert.deepEqual(JSON.parse(resBody), [
+ { name: 'hello.txt', path: '/root/hello.txt', type: 'file' },
+ ])
+
+ await sbx.kill()
+})
+
test.skipIf(isDebug)('test upload file with invalid signing', async () => {
const sbx = await Sandbox.create(template, {
timeoutMs: timeout,