Skip to content

Commit 82222d7

Browse files
🔐 fix: Harden Extraction Network And Credential Boundaries (#359)
Co-authored-by: Lia <lia@librechat.ai>
1 parent 77bcd2e commit 82222d7

22 files changed

Lines changed: 762 additions & 200 deletions

‎.github/workflows/images.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ on:
3333
- javascript-packages.txt
3434
- launcher/**
3535
- packages/code/src/**
36+
- packages/extraction/**
3637
- service/**
3738
- shared/**
3839
pull_request:
@@ -70,6 +71,8 @@ jobs:
7071
run: |
7172
images=$(jq -c . <<'JSON'
7273
[
74+
{"image": "code-interpreter-extraction", "dockerfile": "packages/extraction/Dockerfile", "target": "runtime",
75+
"description": "Authenticated isolated PDF/DOCX extraction service", "timeout": 15},
7376
{"image": "code-interpreter-api", "dockerfile": "service/Dockerfile", "target": "api",
7477
"description": "Code API HTTP server (Compose service api)", "timeout": 30},
7578
{"image": "code-interpreter-worker", "dockerfile": "service/Dockerfile", "target": "worker",

‎packages/extraction/Dockerfile‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ COPY packages/extraction/tsconfig.json ./
66
COPY packages/extraction/src ./src
77
RUN npm run build
88
RUN apt-get update && apt-get install -y --no-install-recommends gcc libc6-dev linux-libc-dev && rm -rf /var/lib/apt/lists/*
9-
COPY packages/extraction/native/guard.c ./guard.c
10-
RUN gcc -O2 -Wall -Wextra -Werror -o guard guard.c
9+
COPY packages/extraction/native/guard.c packages/extraction/native/gateway.c ./
10+
RUN gcc -O2 -Wall -Wextra -Werror -o guard guard.c && gcc -O2 -Wall -Wextra -Werror -o gateway gateway.c
1111
COPY packages/extraction/tests/block-landlock.c ./block-landlock.c
1212
RUN gcc -O2 -Wall -Wextra -Werror -o block-landlock block-landlock.c
1313

@@ -17,14 +17,16 @@ COPY packages/extraction/worker/requirements.txt /opt/extraction/requirements.tx
1717
RUN pip install --no-cache-dir -r /opt/extraction/requirements.txt
1818
COPY --from=build /usr/local/bin/node /usr/local/bin/node
1919
COPY --from=build --chmod=0555 /build/guard /opt/extraction/guard
20+
COPY --from=build --chmod=0555 /build/gateway /opt/extraction/gateway
2021
COPY --from=build --chmod=0555 /build/dist /opt/extraction/dist
2122
COPY --chmod=0444 packages/extraction/package.json /opt/extraction/package.json
2223
COPY --chmod=0444 packages/extraction/worker/extract.py /opt/extraction/extract.py
2324
RUN groupadd -g 10001 extraction && useradd -u 10001 -g extraction -M extraction && \
24-
mkdir /socket /jobs && chown extraction:extraction /socket /jobs && chmod 0700 /jobs && chmod 0770 /socket
25+
mkdir -p /run/extraction /jobs && chown extraction:extraction /run/extraction /jobs && chmod 0700 /jobs && chmod 0755 /run/extraction
2526
USER 10001:10001
2627
WORKDIR /opt/extraction
27-
HEALTHCHECK --interval=10s --timeout=2s --start-period=10s --retries=3 CMD ["node", "dist/health.js"]
28+
HEALTHCHECK --interval=10s --timeout=2s --start-period=10s --retries=3 CMD ["/opt/extraction/gateway", "node", "dist/health.js"]
29+
ENTRYPOINT ["/opt/extraction/gateway"]
2830
CMD ["node", "dist/main.js"]
2931

3032
FROM runtime AS qualification

‎packages/extraction/compose.yaml‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
11
services:
2+
extraction-init:
3+
image: ${EXTRACTION_IMAGE:-librechat-extraction:local}
4+
build:
5+
context: ../..
6+
dockerfile: packages/extraction/Dockerfile
7+
target: runtime
8+
command: [node, dist/provision.js]
9+
user: '10001:10001'
10+
read_only: true
11+
cap_drop: [ALL]
12+
security_opt: [no-new-privileges:true]
13+
network_mode: none
14+
mem_limit: 128m
15+
cpus: 1
16+
pids_limit: 16
17+
healthcheck:
18+
disable: true
19+
volumes:
20+
- extraction-token:/run/extraction
221
extraction:
322
image: ${EXTRACTION_IMAGE:-librechat-extraction:local}
423
build:
524
context: ../..
625
dockerfile: packages/extraction/Dockerfile
726
target: runtime
27+
depends_on:
28+
extraction-init:
29+
condition: service_completed_successfully
830
init: true
931
user: '10001:10001'
1032
read_only: true
1133
cap_drop: [ALL]
1234
security_opt: [no-new-privileges:true]
13-
network_mode: none
35+
networks: [extraction]
1436
mem_limit: 768m
1537
cpus: 2
1638
pids_limit: 32
1739
tmpfs:
1840
- /jobs:rw,noexec,nosuid,nodev,size=96m,nr_inodes=1024,uid=10001,gid=10001,mode=0700
1941
volumes:
20-
- extraction-socket:/socket
42+
- extraction-token:/run/extraction:ro
43+
networks:
44+
extraction:
45+
internal: true
2146
volumes:
22-
extraction-socket:
47+
extraction-token:
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#define _GNU_SOURCE
2+
#include <errno.h>
3+
#include <fcntl.h>
4+
#include <linux/audit.h>
5+
#include <linux/filter.h>
6+
#include <linux/seccomp.h>
7+
#include <stddef.h>
8+
#include <stdio.h>
9+
#include <sys/prctl.h>
10+
#include <sys/socket.h>
11+
#include <sys/stat.h>
12+
#include <sys/syscall.h>
13+
#include <unistd.h>
14+
15+
#if defined(__x86_64__)
16+
#define ARCH AUDIT_ARCH_X86_64
17+
#elif defined(__aarch64__)
18+
#define ARCH AUDIT_ARCH_AARCH64
19+
#else
20+
#error Unsupported architecture
21+
#endif
22+
23+
#define DENY(nr) BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, nr, 0, 1), BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | EPERM)
24+
int main(int argc, char **argv) {
25+
if (argc < 2 || geteuid() == 0) goto fail;
26+
/* No inherited socket, including a caller-supplied socket on standard IO. */
27+
for (int fd = 0; fd < 3; fd++) {
28+
struct stat stat;
29+
if (fstat(fd, &stat)) goto fail;
30+
if (S_ISSOCK(stat.st_mode)) {
31+
int null = open("/dev/null", O_RDWR | O_CLOEXEC);
32+
if (null < 0 || dup2(null, fd) < 0) goto fail;
33+
close(null);
34+
}
35+
}
36+
if (syscall(SYS_close_range, 3, ~0U, 0)) goto fail;
37+
struct sock_filter filter[] = {
38+
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)),
39+
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ARCH, 1, 0),
40+
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
41+
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
42+
#ifdef __x86_64__
43+
BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 0x40000000, 0, 1),
44+
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
45+
#endif
46+
DENY(__NR_socketpair),
47+
DENY(__NR_sendmsg), DENY(__NR_recvmsg), DENY(__NR_sendmmsg), DENY(__NR_recvmmsg),
48+
/* Only IP sockets. Docker's default filter remains in force underneath. */
49+
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_socket, 0, 5),
50+
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, args[0])),
51+
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AF_INET, 2, 0),
52+
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AF_INET6, 1, 0),
53+
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | EPERM),
54+
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
55+
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
56+
};
57+
struct sock_fprog program = {.len = sizeof(filter) / sizeof(filter[0]), .filter = filter};
58+
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &program)) goto fail;
59+
execvp(argv[1], &argv[1]);
60+
fail:
61+
fputs("EXTRACTION_GATEWAY_UNAVAILABLE\n", stderr);
62+
return 125;
63+
}

‎packages/extraction/native/guard.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ static void restrict_syscalls(void) {
5959
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
6060
#endif
6161
DENY(__NR_socket), DENY(__NR_socketpair), DENY(__NR_connect),
62+
DENY(__NR_sendmsg), DENY(__NR_recvmsg), DENY(__NR_sendmmsg), DENY(__NR_recvmmsg),
63+
DENY(__NR_sendto), DENY(__NR_recvfrom), DENY(__NR_setsockopt), DENY(__NR_getsockopt),
64+
DENY(__NR_shutdown), DENY(__NR_getsockname), DENY(__NR_getpeername),
6265
DENY(__NR_bind), DENY(__NR_listen), DENY(__NR_accept), DENY(__NR_accept4),
6366
DENY(__NR_clone), DENY(__NR_clone3),
6467
#ifdef __NR_fork
@@ -119,6 +122,11 @@ static void restrict_syscalls(void) {
119122
int main(int argc, char **argv) {
120123
if (argc != 5 || geteuid() == 0) fail();
121124
/* Paths are supervisor-owned, never supplied in the HTTP protocol. */
125+
struct stat input, output, errors, null;
126+
if (fstat(0, &input) || fstat(1, &output) || fstat(2, &errors) || stat("/dev/null", &null)) fail();
127+
if (!S_ISCHR(input.st_mode) || input.st_rdev != null.st_rdev ||
128+
!S_ISCHR(errors.st_mode) || errors.st_rdev != null.st_rdev ||
129+
!S_ISREG(output.st_mode) || output.st_nlink != 1 || output.st_size != 0) fail();
122130
if (chdir(argv[1])) fail();
123131
pid_t parent = getppid();
124132
if (parent == 1 || prctl(PR_SET_PDEATHSIG, SIGKILL) || getppid() != parent) fail();

‎packages/extraction/src/backend.ts‎

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -46,67 +46,79 @@ export class RestrictedBackend implements Backend {
4646
signal: AbortSignal
4747
): Promise<string> {
4848
signal.throwIfAborted();
49-
const child = spawn(
50-
this.runtime.guard,
51-
[job, this.runtime.python, this.runtime.worker, mode],
52-
{
53-
detached: true,
54-
stdio: ['ignore', 'pipe', 'pipe'],
55-
env: {},
56-
}
49+
const status = await open(
50+
join(job, 'status'),
51+
constants.O_CREAT |
52+
constants.O_EXCL |
53+
constants.O_NOFOLLOW |
54+
constants.O_RDWR,
55+
0o600
5756
);
58-
let output = '';
59-
let count = 0;
60-
let overflow = false;
61-
const kill = () => {
62-
if (
63-
child.pid &&
64-
child.exitCode === null &&
65-
child.signalCode === null
66-
) {
67-
try {
68-
process.kill(-child.pid, 'SIGKILL');
69-
} catch (error) {
70-
if ((error as NodeJS.ErrnoException).code !== 'ESRCH')
71-
this.healthy = false;
57+
try {
58+
signal.throwIfAborted();
59+
const child = spawn(
60+
this.runtime.guard,
61+
[job, this.runtime.python, this.runtime.worker, mode],
62+
{
63+
detached: true,
64+
stdio: ['ignore', status.fd, 'ignore'],
65+
env: {},
7266
}
73-
}
74-
};
75-
signal.addEventListener('abort', kill, { once: true });
76-
if (signal.aborted) kill();
77-
let spawnError = false;
78-
child.on('error', () => {
79-
spawnError = true;
80-
});
81-
child.stdout.on('data', (chunk: Buffer) => {
82-
count += chunk.length;
83-
if (count > 1024) {
84-
overflow = true;
85-
kill();
86-
} else output += chunk.toString('ascii');
87-
});
88-
child.stderr.on('data', (chunk: Buffer) => {
89-
count += chunk.length;
90-
if (count > 2048) {
91-
overflow = true;
92-
kill();
93-
}
94-
});
95-
const code = await new Promise<number | null>(resolve =>
96-
child.once('close', resolve)
97-
);
98-
signal.removeEventListener('abort', kill);
99-
signal.throwIfAborted();
100-
if (spawnError || overflow) throw new ExtractionError('UNAVAILABLE');
101-
if (code !== 0) {
102-
const value = output.trim();
103-
if (parserErrors.has(value))
104-
throw new ExtractionError(value as 'INVALID_DOCUMENT');
105-
throw new ExtractionError(
106-
code === null ? 'RESOURCE_LIMIT' : 'UNAVAILABLE'
10767
);
68+
const kill = () => {
69+
if (
70+
child.pid &&
71+
child.exitCode === null &&
72+
child.signalCode === null
73+
) {
74+
try {
75+
process.kill(-child.pid, 'SIGKILL');
76+
} catch (error) {
77+
if ((error as NodeJS.ErrnoException).code !== 'ESRCH')
78+
this.healthy = false;
79+
}
80+
}
81+
};
82+
signal.addEventListener('abort', kill, { once: true });
83+
if (signal.aborted) kill();
84+
let spawnError = false;
85+
child.on('error', () => {
86+
spawnError = true;
87+
});
88+
const code = await new Promise<number | null>(resolve =>
89+
child.once('close', resolve)
90+
);
91+
signal.removeEventListener('abort', kill);
92+
signal.throwIfAborted();
93+
const stat = await status.stat();
94+
if (
95+
spawnError ||
96+
!stat.isFile() ||
97+
stat.nlink !== 1 ||
98+
stat.size > 1024
99+
)
100+
throw new ExtractionError('UNAVAILABLE');
101+
const buffer = Buffer.alloc(1025);
102+
const { bytesRead } = await status.read(
103+
buffer,
104+
0,
105+
buffer.length,
106+
0
107+
);
108+
if (bytesRead > 1024) throw new ExtractionError('UNAVAILABLE');
109+
const output = buffer.toString('ascii', 0, bytesRead);
110+
if (code !== 0) {
111+
const value = output.trim();
112+
if (parserErrors.has(value))
113+
throw new ExtractionError(value as 'INVALID_DOCUMENT');
114+
throw new ExtractionError(
115+
code === null ? 'RESOURCE_LIMIT' : 'UNAVAILABLE'
116+
);
117+
}
118+
return output;
119+
} finally {
120+
await status.close();
108121
}
109-
return output;
110122
}
111123
async probe(): Promise<void> {
112124
const job = await this.create();
@@ -131,7 +143,7 @@ export class RestrictedBackend implements Backend {
131143
await this.execute(job, format, signal);
132144
const file = await open(
133145
join(job, 'result'),
134-
constants.O_RDONLY | constants.O_NOFOLLOW
146+
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK
135147
);
136148
try {
137149
const stat = await file.stat();
@@ -142,7 +154,11 @@ export class RestrictedBackend implements Backend {
142154
) {
143155
throw new ExtractionError('OUTPUT_LIMIT');
144156
}
145-
const bytes = await file.readFile();
157+
const buffer = Buffer.alloc(stat.size + 1);
158+
const { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
159+
if (bytesRead > policy.resultBytes)
160+
throw new ExtractionError('OUTPUT_LIMIT');
161+
const bytes = buffer.subarray(0, bytesRead);
146162
signal.throwIfAborted();
147163
validateResult(bytes, format, digest);
148164
return bytes;

‎packages/extraction/src/config.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { isIP } from 'node:net';
2+
3+
export function endpoint() {
4+
const host = process.env.EXTRACTION_HOST ?? '0.0.0.0';
5+
const port = process.env.EXTRACTION_PORT ?? '8000';
6+
if (!isIP(host) || !/^[1-9]\d{0,4}$/.test(port) || Number(port) > 65535)
7+
throw new Error('INVALID_ENDPOINT');
8+
return { host, port: Number(port) };
9+
}

0 commit comments

Comments
 (0)