- Location
- lib/collections.ts:178
- Finding
- xAI Bearer Credentials Are Exposed Through Child-Process Arguments<![CDATA[
## Vulnerability Details
**File Location**: `lib/collections.ts:178-262`
**Vulnerability Type**: Sensitive credential exposure through process arguments
**Risk Level**: High
### Vulnerable Code
The management API credential is included directly in the curl argument vector:
```typescript
const proc = Bun.spawn([
"curl", "-s",
"-X", "POST",
`${MGMT_BASE}/collections/${collectionId}/documents`,
"-H", `Authorization: Bearer ${key}`,
"-F", `document_id=${documentId}`,
], { stdout: "pipe", stderr: "pipe" });
```
The same pattern is used when uploading a document:
```typescript
const proc = Bun.spawn([
"curl", "-s",
"-X", "POST",
`${MGMT_BASE}/collections/${collectionId}/documents`,
"-H", `Authorization: Bearer ${key}`,
"-F", `file=@${filePath}`,
"-F", `data=@${filePath}`,
"-F", `name=${name}`,
"-F", `content_type=${contentType}`,
], { stdout: "pipe", stderr: "pipe" });
```
It is also used for the xAI Files API credential:
```typescript
const proc = Bun.spawn([
"curl", "-s",
"-X", "POST",
`${API_BASE}/files`,
"-H", `Authorization: Bearer ${key}`,
"-F", `file=@${filePath};filename=${filename}`,
"-F", `purpose=${purpose}`,
], { stdout: "pipe", stderr: "pipe" });
```
### Technical Analysis
Although `Bun.spawn` is invoked with an argument array and therefore avoids ordinary shell metacharacter expansion, the bearer token becomes part of curl's operating-system process argument vector.
Depending on operating-system configuration, process arguments can be visible through process inspection tools, `/proc` interfaces, endpoint monitoring, debugging utilities, telemetry agents, audit logs, or crash diagnostics. A local account or monitoring service able to inspect the curl process may capture the complete authorization header.
The management credential is particularly sensitive because it is used for collection administration and document attachment. Exposure is avoidable because Bun provides native `fetch` and `FormD
...[truncated 1330 chars]
- Remediation
- <![CDATA[
## Remediation Suggestions
1. Replace curl subprocesses with native `fetch` and `FormData`:
```typescript
const form = new FormData();
form.append("document_id", documentId);
const response = await fetch(endpoint, {
method: "POST",
headers: {
Authorization: `Bearer ${key}`,
},
body: form,
});
```
2. For file uploads, create a `Blob` from the file content and append it to `FormData`.
3. Keep credentials in process memory and HTTP headers rather than command-line arguments.
4. Ensure errors never include request headers or bearer credentials.
5. Rotate existing xAI and management keys if these operations have been used on multi-user or heavily monitored systems.
6. Apply the narrowest available xAI-side permissions to management and upload keys.
7. Add automated tests that assert secrets are never passed to `Bun.spawn` or included in logged command representations.
]]>