Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(api/admin): serve admin routes to rexctl on a local socket

Hampusb8f03ec430d4
diff --git a/fluxer_api/Dockerfile b/fluxer_api/Dockerfile
index 505a130a5..b92d04522 100644
--- a/fluxer_api/Dockerfile
+++ b/fluxer_api/Dockerfile
@@ -15,7 +15,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
libssl-dev \
pkg-config \
openssl \
- libvips-dev \
python3 && \
rm -rf /var/lib/apt/lists/*
@@ -61,14 +60,16 @@ RUN npm install -g [email protected]
COPY --from=deploy /out ./
COPY --from=deploy /usr/src/app/fluxer_api/dist ./dist
+COPY --from=deploy /usr/src/app/fluxer_api/bin ./bin
COPY --from=deploy /usr/src/app/tsconfigs /usr/src/app/tsconfigs
RUN rm -rf pkgs && \
- chown -R 65532:65532 /usr/src/app
+ mkdir -p /run/fluxer && \
+ chmod 700 /run/fluxer && \
+ chown -R 65532:65532 /usr/src/app /run/fluxer
ENV HOME=/usr/src/app
ENV NODE_ENV=production
-ENV NODE_OPTIONS="--enable-source-maps"
ENV NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt
ENV BUILD_VERSION=${BUILD_VERSION}
diff --git a/fluxer_api/bin/operator-relay.mjs b/fluxer_api/bin/operator-relay.mjs
new file mode 100644
index 000000000..a2199520f
--- /dev/null
+++ b/fluxer_api/bin/operator-relay.mjs
@@ -0,0 +1,125 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {request} from 'node:http';
+
+const SOCKET_PATH = process.env.FLUXER_OPERATOR_SOCKET ?? '/run/fluxer/operator.sock';
+const HOST_USER_HEADER = 'X-Fluxer-Operator-Host-User';
+const SCHEMA_MAJOR = 1;
+const TIMEOUT_MS = 120_000;
+
+function respond(response) {
+ process.stdout.write(
+ `${JSON.stringify({api_version: process.env.BUILD_VERSION ?? '', schema_major: SCHEMA_MAJOR, ...response})}\n`,
+ );
+}
+
+function fail(message) {
+ respond({status: 0, headers: {}, body: null, relay_error: message});
+}
+
+async function readStdin() {
+ const chunks = [];
+ for await (const chunk of process.stdin) chunks.push(chunk);
+ return Buffer.concat(chunks).toString('utf8');
+}
+
+function buildPath(path, query) {
+ const params = new URLSearchParams();
+ for (const [key, value] of Object.entries(query ?? {})) params.append(key, String(value));
+ const search = params.toString();
+ return search ? `${path}?${search}` : path;
+}
+
+function buildBody(req, headers) {
+ if (req.body_base64) return Buffer.from(req.body_base64, 'base64');
+ if (req.body === undefined || req.body === null) return null;
+ if (!Object.keys(headers).some((key) => key.toLowerCase() === 'content-type')) {
+ headers['Content-Type'] = 'application/json';
+ }
+ return Buffer.from(JSON.stringify(req.body));
+}
+
+function parseBody(text, contentType) {
+ if (text === '') return null;
+ if (contentType.includes('json')) {
+ try {
+ return JSON.parse(text);
+ } catch {
+ return text;
+ }
+ }
+ return text;
+}
+
+function send(req) {
+ const target = req.target === 'loopback' ? 'loopback' : 'operator';
+ if (req.target !== target) return Promise.reject(new Error(`unknown target ${String(req.target)}`));
+ if (typeof req.path !== 'string' || !req.path.startsWith('/'))
+ return Promise.reject(new Error('path must start with /'));
+ if (target === 'operator' && !(req.path === '/admin' || req.path.startsWith('/admin/'))) {
+ return Promise.reject(new Error('the operator target serves /admin paths only'));
+ }
+ const headers = {...(req.headers ?? {})};
+ if (target === 'operator' && req.host_user) headers[HOST_USER_HEADER] = String(req.host_user);
+ if (target === 'loopback') {
+ const clientIpHeader = (process.env.FLUXER_CLIENT_IP_HEADER_NAME || 'x-forwarded-for').toLowerCase();
+ if (!Object.keys(headers).some((key) => key.toLowerCase() === clientIpHeader))
+ headers[clientIpHeader] = '127.0.0.1';
+ }
+ const body = buildBody(req, headers);
+ if (body !== null) headers['Content-Length'] = String(body.length);
+ const location =
+ target === 'operator'
+ ? {socketPath: SOCKET_PATH}
+ : {host: '127.0.0.1', port: Number(process.env.FLUXER_API_PORT ?? '8080')};
+ return new Promise((resolve, reject) => {
+ const outgoing = request(
+ {...location, method: req.method ?? 'GET', path: buildPath(req.path, req.query), headers, timeout: TIMEOUT_MS},
+ (incoming) => {
+ const chunks = [];
+ incoming.on('data', (chunk) => chunks.push(chunk));
+ incoming.on('error', reject);
+ incoming.on('end', () => {
+ const responseHeaders = {};
+ for (const [key, value] of Object.entries(incoming.headers)) {
+ if (value !== undefined) responseHeaders[key] = Array.isArray(value) ? value.join(', ') : value;
+ }
+ resolve({
+ status: incoming.statusCode ?? 0,
+ headers: responseHeaders,
+ body: parseBody(Buffer.concat(chunks).toString('utf8'), responseHeaders['content-type'] ?? ''),
+ api_version: responseHeaders['x-fluxer-version'] ?? process.env.BUILD_VERSION ?? '',
+ });
+ });
+ },
+ );
+ outgoing.on('timeout', () => outgoing.destroy(new Error('the request timed out')));
+ outgoing.on('error', reject);
+ outgoing.end(body ?? undefined);
+ });
+}
+
+async function main() {
+ let req;
+ try {
+ req = JSON.parse(await readStdin());
+ } catch {
+ fail('the request is not valid JSON');
+ return;
+ }
+ if (req === null || typeof req !== 'object') {
+ fail('the request is not a JSON object');
+ return;
+ }
+ try {
+ respond(await send(req));
+ } catch (error) {
+ fail(error instanceof Error ? error.message : String(error));
+ }
+}
+
+main().catch((error) => {
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
+ process.exit(1);
+});
diff --git a/fluxer_api/package.json b/fluxer_api/package.json
index 7411cdfbe..8b6577b06 100644
--- a/fluxer_api/package.json
+++ b/fluxer_api/package.json
@@ -8,16 +8,16 @@
"typecheck": "tsc --noEmit",
"dev": "tsx watch --clear-screen=false src/AppEntrypoint.ts",
"start": "tsx src/AppEntrypoint.ts",
- "start:worker": "tsx src/WorkerEntrypoint.ts"
+ "start:worker": "tsx src/WorkerEntrypoint.ts",
+ "operator-relay": "node bin/operator-relay.mjs"
},
"dependencies": {
- "@atproto/api": "catalog:",
"@atproto/jwk-jose": "catalog:",
"@atproto/oauth-client-node": "catalog:",
"@aws-sdk/client-s3": "catalog:",
"@aws-sdk/lib-storage": "catalog:",
"@aws-sdk/s3-request-presigner": "catalog:",
- "@bufbuild/protobuf": "^2.15.0",
+ "@bufbuild/protobuf": "^2.16.0",
"@elastic/elasticsearch": "catalog:",
"@fluxer/config": "workspace:*",
"@fluxer/constants": "workspace:*",
@@ -29,6 +29,7 @@
"@fluxer/ip_utils": "workspace:*",
"@fluxer/limits": "workspace:*",
"@fluxer/logger": "workspace:*",
+ "@fluxer/number_utils": "workspace:*",
"@fluxer/record_utils": "workspace:*",
"@fluxer/schema": "workspace:*",
"@fluxer/snowflake": "workspace:*",
@@ -71,6 +72,7 @@
"maxmind": "catalog:",
"mime": "catalog:",
"nodemailer": "catalog:",
+ "pdf-lib": "catalog:",
"pg": "catalog:",
"pino": "catalog:",
"sharp": "catalog:",
@@ -87,6 +89,7 @@
"devDependencies": {
"@types/archiver": "catalog:",
"esbuild": "catalog:",
+ "fflate": "^0.8.3",
"msw": "catalog:",
"typescript": "catalog:ts7",
"vitest": "catalog:"
diff --git a/fluxer_api/src/App.ts b/fluxer_api/src/App.ts
index c297bcf83..4d997fd5b 100644
--- a/fluxer_api/src/App.ts
+++ b/fluxer_api/src/App.ts
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {createAPIApp} from '@app/api/App';
+import {OPERATOR_SOCKET_PATH} from '@app/api/admin/operator/OperatorRequests';
+import {listenOperatorSocket} from '@app/api/admin/operator/OperatorSocket';
import {buildAPIServerOptions, initializeConfig} from '@app/api/Config';
import {initializeLogger} from '@app/api/Logger';
import {Config} from '@app/Config';
@@ -35,9 +37,13 @@ async function main(): Promise<void> {
});
const server = createServer(app, buildAPIServerOptions(Config));
Logger.info({port: Config.port}, `Starting Fluxer API on port ${Config.port}`);
+ const operatorServer = await listenOperatorSocket(app, OPERATOR_SOCKET_PATH, Logger);
setupGracefulShutdown(
async () => {
await closeHttpServer(server);
+ if (operatorServer) {
+ await closeHttpServer(operatorServer);
+ }
await shutdown();
},
{logger: Logger, timeoutMs: 30000},
diff --git a/fluxer_api/src/api/admin/AdminAuditAttribution.ts b/fluxer_api/src/api/admin/AdminAuditAttribution.ts
new file mode 100644
index 000000000..48c590940
--- /dev/null
+++ b/fluxer_api/src/api/admin/AdminAuditAttribution.ts
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {AsyncLocalStorage} from 'node:async_hooks';
+
+export type AdminAuditAttribution = Readonly<Record<string, string>>;
+
+const attributionContext = new AsyncLocalStorage<AdminAuditAttribution>();
+
+export function runWithAdminAuditAttribution<T>(attribution: AdminAuditAttribution | null, fn: () => T): T {
+ if (attribution === null) {
+ return fn();
+ }
+ return attributionContext.run(attribution, fn);
+}
+
+export function withAdminAuditAttribution(metadata: Map<string, string>): Map<string, string> {
+ const attribution = attributionContext.getStore();
+ if (!attribution) {
+ return metadata;
+ }
+ const merged = new Map(metadata);
+ for (const [key, value] of Object.entries(attribution)) {
+ merged.set(key, value);
+ }
+ return merged;
+}
diff --git a/fluxer_api/src/api/admin/AdminAuditRecorder.ts b/fluxer_api/src/api/admin/AdminAuditRecorder.ts
index c34ea949c..821c0fc81 100644
--- a/fluxer_api/src/api/admin/AdminAuditRecorder.ts
+++ b/fluxer_api/src/api/admin/AdminAuditRecorder.ts
@@ -30,8 +30,8 @@ function toAdminAuditMetadata(input: AdminAuditMetadataInput = {}): Map<string,
return metadata;
}
-async function recordAdminAuditEntry(ctx: Context<HonoEnv>, entry: AdminAuditEntryInput<string>): Promise<void> {
- await ctx.get('adminService').auditService.createAuditLog({
+async function recordAdminAuditEntry(ctx: Context<HonoEnv>, entry: AdminAuditEntryInput<string>): Promise<bigint> {
+ return await ctx.get('adminService').auditService.createAuditLog({
adminUserId: ctx.get('adminUserId'),
targetType: entry.targetType,
targetId: entry.targetId,
@@ -44,8 +44,8 @@ async function recordAdminAuditEntry(ctx: Context<HonoEnv>, entry: AdminAuditEnt
export async function recordAdminRead(
ctx: Context<HonoEnv>,
entry: AdminAuditEntryInput<AdminAuditReadAction>,
-): Promise<void> {
- await recordAdminAuditEntry(ctx, entry);
+): Promise<bigint> {
+ return await recordAdminAuditEntry(ctx, entry);
}
export {recordAdminAuditEntry as recordAdminWrite};
diff --git a/fluxer_api/src/api/admin/operator/OperatorRequests.ts b/fluxer_api/src/api/admin/operator/OperatorRequests.ts
new file mode 100644
index 000000000..db20157a7
--- /dev/null
+++ b/fluxer_api/src/api/admin/operator/OperatorRequests.ts
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {stripApiPrefix} from '@app/api/utils/RequestPathUtils';
+
+export const OPERATOR_SOCKET_PATH = '/run/fluxer/operator.sock';
+export const OPERATOR_HOST_USER_HEADER = 'X-Fluxer-Operator-Host-User';
+export const OPERATOR_PEER_ADDRESS = '127.0.0.1';
+
+const HOST_USER_MAX_LENGTH = 64;
+const HOST_USER_PATTERN = /^[\p{L}\p{N}._@\\-]+$/u;
+
+const operatorRequests = new WeakSet<Request>();
+
+export function markOperatorRequest(request: Request): void {
+ operatorRequests.add(request);
+}
+
+export function isOperatorRequest(request: Request): boolean {
+ return operatorRequests.has(request);
+}
+
+export function isOperatorPath(path: string): boolean {
+ const stripped = stripApiPrefix(path);
+ return stripped === '/admin' || stripped.startsWith('/admin/');
+}
+
+export function readOperatorHostUser(request: Request): string | null {
+ const value = request.headers.get(OPERATOR_HOST_USER_HEADER)?.trim();
+ if (!value || value.length > HOST_USER_MAX_LENGTH || !HOST_USER_PATTERN.test(value)) {
+ return null;
+ }
+ return value;
+}
diff --git a/fluxer_api/src/api/admin/operator/OperatorSocket.ts b/fluxer_api/src/api/admin/operator/OperatorSocket.ts
new file mode 100644
index 000000000..b4f719a3e
--- /dev/null
+++ b/fluxer_api/src/api/admin/operator/OperatorSocket.ts
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {chmod, mkdir, rm} from 'node:fs/promises';
+import {createServer, type Server} from 'node:http';
+import path from 'node:path';
+import {isOperatorPath, markOperatorRequest} from '@app/api/admin/operator/OperatorRequests';
+import type {HonoApp} from '@app/api/types/HonoEnv';
+import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
+import type {LoggerInterface} from '@fluxer/logger/src/LoggerInterface';
+import {getRequestListener, type Http2Bindings, type HttpBindings} from '@hono/node-server';
+
+function createOperatorFetch(app: HonoApp) {
+ return async (request: Request, env?: HttpBindings | Http2Bindings): Promise<Response> => {
+ if (!isOperatorPath(new URL(request.url).pathname)) {
+ return Response.json(
+ {code: APIErrorCodes.NOT_FOUND, message: 'Only admin routes are served here.'},
+ {status: 404},
+ );
+ }
+ markOperatorRequest(request);
+ return await app.fetch(request, env);
+ };
+}
+
+export function createOperatorServer(app: HonoApp): Server {
+ return createServer(getRequestListener(createOperatorFetch(app)));
+}
+
+export async function listenOperatorSocket(
+ app: HonoApp,
+ socketPath: string,
+ logger: LoggerInterface,
+): Promise<Server | null> {
+ try {
+ await mkdir(path.dirname(socketPath), {recursive: true, mode: 0o700});
+ await rm(socketPath, {force: true});
+ const server = createOperatorServer(app);
+ await new Promise<void>((resolve, reject) => {
+ server.once('error', reject);
+ server.listen(socketPath, () => {
+ server.off('error', reject);
+ resolve();
+ });
+ });
+ await chmod(socketPath, 0o600);
+ server.on('error', (error) => {
+ logger.error({error, socketPath}, 'Operator socket failed');
+ });
+ logger.info({socketPath}, 'Operator socket listening');
+ return server;
+ } catch (error) {
+ logger.warn({error, socketPath}, 'Operator socket is unavailable');
+ return null;
+ }
+}
diff --git a/fluxer_api/src/api/admin/services/AdminAuditService.ts b/fluxer_api/src/api/admin/services/AdminAuditService.ts
index 632a53677..e0d7198ef 100644
--- a/fluxer_api/src/api/admin/services/AdminAuditService.ts
+++ b/fluxer_api/src/api/admin/services/AdminAuditService.ts
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {ADMIN_AUDIT_READ_ACTIONS, getAdminAuditAccess} from '@app/api/admin/AdminAuditActions';
+import {withAdminAuditAttribution} from '@app/api/admin/AdminAuditAttribution';
import type {AdminAuditLog, AdminRepository} from '@app/api/admin/AdminRepository';
import type {UserID} from '@app/api/BrandedTypes';
import {createChannelID, createGuildID, createUserID} from '@app/api/BrandedTypes';
@@ -46,7 +47,7 @@ export class AdminAuditService {
action,
auditLogReason,
metadata = new Map(),
- }: CreateAdminAuditLogParams): Promise<void> {
+ }: CreateAdminAuditLogParams): Promise<bigint> {
const log = await this.adminRepository.createAuditLog({
log_id: await this.snowflakeService.generate(),
admin_user_id: adminUserId,
@@ -54,7 +55,7 @@ export class AdminAuditService {
target_id: targetId,
action,
audit_log_reason: auditLogReason ?? null,
- metadata,
+ metadata: withAdminAuditAttribution(metadata),
created_at: new Date(),
});
const auditLogSearchService = getAuditLogSearchService();
@@ -63,6 +64,7 @@ export class AdminAuditService {
Logger.error({error, logId: log.logId}, 'Failed to index audit log to search');
});
}
+ return log.logId;
}
async getAuditLog(logId: bigint): Promise<AdminAuditLogResponse | null> {
@@ -74,48 +76,6 @@ export class AdminAuditService {
return response;
}
- async listAuditLogs(data: {
- admin_user_id?: bigint;
- target_type?: string;
- target_id?: string;
- access?: AdminAuditAccess;
- limit?: number;
- offset?: number;
- }): Promise<AuditLogsListResponse> {
- const auditLogSearchService = getAuditLogSearchService();
- const targetIdBigInt = data.target_id ? BigInt(data.target_id) : undefined;
- if (!auditLogSearchService?.isAvailable()) {
- return this.listAuditLogsFromDatabase({
- adminUserId: data.admin_user_id,
- targetType: data.target_type,
- targetId: targetIdBigInt,
- access: data.access,
- limit: data.limit,
- offset: data.offset,
- });
- }
- const limit = data.limit || 50;
- const filters: AuditLogSearchFilters = {...accessFilters(data.access)};
- if (data.admin_user_id) {
- filters.adminUserId = data.admin_user_id.toString();
- }
- if (data.target_type) {
- filters.targetType = data.target_type;
- }
- if (data.target_id) {
- filters.targetId = data.target_id;
- }
- const {hits, total} = await auditLogSearchService.searchAuditLogs('', filters, {
- limit,
- offset: data.offset || 0,
- });
- const orderedLogs = await this.loadLogsInSearchOrder(hits.map((hit) => BigInt(hit.logId)));
- return {
- logs: await this.toResponses(orderedLogs),
- total,
- };
- }
-
async searchAuditLogs(data: {
query?: string;
admin_user_id?: bigint;
diff --git a/fluxer_api/src/api/admin/tests/OperatorChannel.test.ts b/fluxer_api/src/api/admin/tests/OperatorChannel.test.ts
new file mode 100644
index 000000000..cbc524e77
--- /dev/null
+++ b/fluxer_api/src/api/admin/tests/OperatorChannel.test.ts
@@ -0,0 +1,361 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {spawn} from 'node:child_process';
+import {existsSync} from 'node:fs';
+import {mkdtemp, readdir, readFile, rm} from 'node:fs/promises';
+import {createServer, request, type Server} from 'node:http';
+import type {AddressInfo} from 'node:net';
+import {tmpdir} from 'node:os';
+import path from 'node:path';
+import type {AdminAuditLog} from '@app/api/admin/AdminRepository';
+import {OPERATOR_HOST_USER_HEADER, OPERATOR_SOCKET_PATH} from '@app/api/admin/operator/OperatorRequests';
+import {createOperatorServer} from '@app/api/admin/operator/OperatorSocket';
+import {createAdminApiKey} from '@app/api/admin/tests/AdminTestUtils';
+import {createTestAccount, setUserACLs} from '@app/api/auth/tests/AuthTestUtils';
+import {getConfig} from '@app/api/Config';
+import {getAdminRepository, getInstanceConfigRepository} from '@app/api/middleware/ServiceSingletons';
+import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
+import {HTTP_STATUS} from '@app/api/test/TestConstants';
+import {createBuilder, createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';
+import {AccountIdentityModes} from '@fluxer/constants/src/AccountIdentityConstants';
+import {AdminACLs} from '@fluxer/constants/src/AdminACLs';
+import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
+import {afterAll, beforeAll, beforeEach, describe, expect, it} from 'vitest';
+
+const API_ROOT = path.resolve(import.meta.dirname, '../../../..');
+const REPO_ROOT = path.resolve(API_ROOT, '..');
+const RELAY_PATH = path.join(API_ROOT, 'bin/operator-relay.mjs');
+const NEW_PASSWORD = 'chosen-from-the-link-2026';
+
+interface SocketResponse {
+ status: number;
+ body: unknown;
+}
+
+interface CreatedUser {
+ user: {id: string; username: string; acls: Array<string>};
+ password_reset: {url: string; expires_at: string};
+}
+
+function socketRequest(
+ socketPath: string,
+ method: string,
+ requestPath: string,
+ options: {body?: unknown; headers?: Record<string, string>} = {},
+): Promise<SocketResponse> {
+ const payload = options.body === undefined ? null : Buffer.from(JSON.stringify(options.body));
+ return new Promise((resolve, reject) => {
+ const outgoing = request(
+ {
+ socketPath,
+ method,
+ path: requestPath,
+ headers: {
+ ...(payload ? {'Content-Type': 'application/json', 'Content-Length': String(payload.length)} : {}),
+ ...options.headers,
+ },
+ },
+ (incoming) => {
+ const chunks: Array<Buffer> = [];
+ incoming.on('data', (chunk: Buffer) => chunks.push(chunk));
+ incoming.on('end', () => {
+ const text = Buffer.concat(chunks).toString('utf8');
+ resolve({status: incoming.statusCode ?? 0, body: text ? JSON.parse(text) : null});
+ });
+ incoming.on('error', reject);
+ },
+ );
+ outgoing.on('error', reject);
+ outgoing.end(payload ?? undefined);
+ });
+}
+
+function runRelay(
+ socketPath: string,
+ input: string,
+ env: Record<string, string> = {},
+): Promise<{code: number | null; stdout: string}> {
+ return new Promise((resolve, reject) => {
+ const child = spawn(process.execPath, [RELAY_PATH], {
+ env: {...process.env, FLUXER_OPERATOR_SOCKET: socketPath, BUILD_VERSION: 'relay-test', ...env},
+ });
+ let stdout = '';
+ child.stdout.on('data', (chunk: Buffer) => {
+ stdout += chunk.toString('utf8');
+ });
+ child.on('error', reject);
+ child.on('close', (code) => resolve({code, stdout}));
+ child.stdin.end(input);
+ });
+}
+
+async function newAuditLogs(before: Set<string>): Promise<Array<AdminAuditLog>> {
+ return (await getAdminRepository().listAllAuditLogsPaginated(100000)).filter(
+ (log) => !before.has(log.logId.toString()),
+ );
+}
+
+async function auditLogIds(): Promise<Set<string>> {
+ return new Set((await getAdminRepository().listAllAuditLogsPaginated(100000)).map((log) => log.logId.toString()));
+}
+
+async function filesUnder(directory: string): Promise<Array<string>> {
+ if (!existsSync(directory)) return [];
+ const files: Array<string> = [];
+ for (const entry of await readdir(directory, {withFileTypes: true, recursive: true})) {
+ if (entry.isFile()) files.push(path.join(entry.parentPath, entry.name));
+ }
+ return files;
+}
+
+describe('operator channel', () => {
+ let harness: ApiTestHarness;
+ let server: Server;
+ let socketDir: string;
+ let socketPath: string;
+
+ beforeAll(async () => {
+ harness = await createApiTestHarness();
+ socketDir = await mkdtemp(path.join(tmpdir(), 'fluxer-op-'));
+ socketPath = path.join(socketDir, 'operator.sock');
+ server = createOperatorServer(harness.app);
+ await new Promise<void>((resolve) => server.listen(socketPath, resolve));
+ });
+
+ beforeEach(async () => {
+ await harness.reset();
+ });
+
+ afterAll(async () => {
+ await new Promise<void>((resolve) => server.close(() => resolve()));
+ await rm(socketDir, {recursive: true, force: true});
+ await harness.shutdown();
+ });
+
+ it('acts as the system user with every admin permission on the socket', async () => {
+ const response = await socketRequest(socketPath, 'GET', '/admin/users/@me');
+ expect(response.status).toBe(HTTP_STATUS.OK);
+ expect((response.body as {user: {id: string}}).user.id).toBe('0');
+ });
+
+ it('serves admin routes only', async () => {
+ const response = await socketRequest(socketPath, 'GET', '/users/@me');
+ expect(response.status).toBe(HTTP_STATUS.NOT_FOUND);
+ expect((response.body as {code: string}).code).toBe(APIErrorCodes.NOT_FOUND);
+ });
+
+ it('refuses operator access off the socket', async () => {
+ await createBuilderWithoutAuth(harness)
+ .get('/admin/users/@me')
+ .header(OPERATOR_HOST_USER_HEADER, 'alex')
+ .expect(HTTP_STATUS.UNAUTHORIZED)
+ .execute();
+ });
+
+ it('still refuses writes to system users', async () => {
+ const response = await socketRequest(socketPath, 'POST', '/admin/users/0/password-reset-link');
+ expect(response.status).not.toBe(HTTP_STATUS.OK);
+ expect((response.body as {code: string}).code).toBe(APIErrorCodes.UNKNOWN_USER);
+ });
+
+ it('records rexctl and the host user in audit metadata', async () => {
+ const target = await createTestAccount(harness);
+ const before = await auditLogIds();
+ const response = await socketRequest(socketPath, 'GET', `/admin/users/${target.userId}`, {
+ headers: {[OPERATOR_HOST_USER_HEADER]: 'alex'},
+ });
+ expect(response.status).toBe(HTTP_STATUS.OK);
+ const logs = await newAuditLogs(before);
+ expect(logs).toHaveLength(1);
+ expect(logs[0]!.adminUserId.toString()).toBe('0');
+ expect(Object.fromEntries(logs[0]!.metadata)).toMatchObject({via: 'rexctl', host_user: 'alex'});
+ });
+
+ it('records the key in audit metadata for admin API keys', async () => {
+ const admin = await setUserACLs(harness, await createTestAccount(harness), [AdminACLs.WILDCARD]);
+ const key = await createAdminApiKey(harness, admin, 'audit attribution', [AdminACLs.USER_LOOKUP], null);
+ const before = await auditLogIds();
+ await createBuilder(harness, key.token).get(`/admin/users/${admin.userId}`).expect(HTTP_STATUS.OK).execute();
+ const logs = await newAuditLogs(before);
+ expect(logs).toHaveLength(1);
+ expect(logs[0]!.metadata.get('via')).toBe(`api_key:${key.keyId}`);
+ });
+
+ it('creates an admin whose reset link works', async () => {
+ await getInstanceConfigRepository().setInstanceIntegrationsConfig({email: {delivery: 'off'}});
+ const email = `operator-created-${Date.now()}@example.com`;
+ const response = await socketRequest(socketPath, 'POST', '/admin/users', {
+ body: {username: 'firstadmin', email, admin: true},
+ });
+ expect(response.status).toBe(HTTP_STATUS.OK);
+ const created = response.body as CreatedUser;
+ expect(created.user.acls).toEqual([AdminACLs.WILDCARD]);
+ const token = created.password_reset.url.split('/reset#token=')[1]!;
+ const reset = await createBuilderWithoutAuth<{user_id: string}>(harness)
+ .post('/auth/reset')
+ .body({token, password: NEW_PASSWORD})
+ .execute();
+ expect(reset.user_id).toBe(created.user.id);
+ const login = await createBuilderWithoutAuth<{user_id: string}>(harness)
+ .post('/auth/login')
+ .body({email, password: NEW_PASSWORD})
+ .execute();
+ expect(login.user_id).toBe(created.user.id);
+ const admins = await socketRequest(socketPath, 'GET', `/admin/users?acl=${encodeURIComponent('*')}`);
+ expect((admins.body as {users: Array<{id: string}>}).users.map((user) => user.id)).toContain(created.user.id);
+ });
+
+ it('signs an admin in once with a link made on the socket', async () => {
+ const admin = await setUserACLs(harness, await createTestAccount(harness), [AdminACLs.WILDCARD]);
+ const member = await createTestAccount(harness);
+ await createBuilder(harness, admin.token)
+ .post(`/admin/users/${admin.userId}/sign-in-link`)
+ .expect(HTTP_STATUS.FORBIDDEN)
+ .execute();
+ const refused = await socketRequest(socketPath, 'POST', `/admin/users/${member.userId}/sign-in-link`);
+ expect((refused.body as {code: string}).code).toBe(APIErrorCodes.MISSING_ACL);
+ const before = await auditLogIds();
+ const created = await socketRequest(socketPath, 'POST', `/admin/users/${admin.userId}/sign-in-link`, {
+ headers: {[OPERATOR_HOST_USER_HEADER]: 'alex'},
+ });
+ expect(created.status).toBe(HTTP_STATUS.OK);
+ const token = (created.body as {url: string}).url.split('/login#sign_in_link=')[1]!;
+ const signedIn = await createBuilderWithoutAuth<{user_id: string; token: string}>(harness)
+ .post('/auth/sign-in-link')
+ .body({token})
+ .execute();
+ expect(signedIn.user_id).toBe(admin.userId);
+ await createBuilder(harness, signedIn.token).get('/users/@me').expect(HTTP_STATUS.OK).execute();
+ await createBuilderWithoutAuth(harness)
+ .post('/auth/sign-in-link')
+ .body({token})
+ .expect(HTTP_STATUS.BAD_REQUEST)
+ .execute();
+ const logs = (await newAuditLogs(before)).map((log) => ({
+ action: log.action,
+ admin: log.adminUserId.toString(),
+ target: log.targetId.toString(),
+ metadata: Object.fromEntries(log.metadata),
+ }));
+ expect(logs).toContainEqual({
+ action: 'create_sign_in_link',
+ admin: '0',
+ target: admin.userId,
+ metadata: {via: 'rexctl', host_user: 'alex'},
+ });
+ expect(logs).toContainEqual({
+ action: 'use_sign_in_link',
+ admin: admin.userId,
+ target: admin.userId,
+ metadata: {via: 'rexctl'},
+ });
+ });
+
+ it('creates accounts without email on a username instance', async () => {
+ const config = getConfig();
+ const selfHosted = config.instance.selfHosted;
+ config.instance.selfHosted = true;
+ try {
+ await getInstanceConfigRepository().setAccountIdentityMode(AccountIdentityModes.USERNAME, 'setup');
+ await expectUsernameInstanceCreation();
+ } finally {
+ config.instance.selfHosted = selfHosted;
+ }
+ });
+
+ async function expectUsernameInstanceCreation(): Promise<void> {
+ const refused = await socketRequest(socketPath, 'POST', '/admin/users', {
+ body: {username: 'withemail', email: '[email protected]'},
+ });
+ expect((refused.body as {code: string}).code).toBe(APIErrorCodes.USERNAME_SIGN_IN_ONLY);
+ const response = await socketRequest(socketPath, 'POST', '/admin/users', {body: {username: 'plainmember'}});
+ expect(response.status).toBe(HTTP_STATUS.OK);
+ const created = response.body as CreatedUser;
+ expect(created.user.acls).toEqual([]);
+ const admins = await socketRequest(socketPath, 'GET', '/admin/users?acl=*');
+ expect((admins.body as {users: Array<{id: string}>}).users.map((user) => user.id)).not.toContain(created.user.id);
+ }
+
+ it('refuses user creation to admins without full access', async () => {
+ const admin = await setUserACLs(harness, await createTestAccount(harness), [
+ AdminACLs.AUTHENTICATE,
+ AdminACLs.USER_LOOKUP,
+ AdminACLs.ACL_SET_USER,
+ ]);
+ await createBuilder(harness, admin.token)
+ .post('/admin/users')
+ .body({username: 'notallowed', email: '[email protected]'})
+ .expect(HTTP_STATUS.FORBIDDEN)
+ .execute();
+ });
+
+ it('relays one request and its response through the socket', async () => {
+ const result = await runRelay(
+ socketPath,
+ JSON.stringify({target: 'operator', method: 'GET', path: '/admin/users/@me', host_user: 'alex'}),
+ );
+ expect(result.code, result.stdout).toBe(0);
+ const response = JSON.parse(result.stdout) as {
+ status: number;
+ body: {user: {id: string}};
+ schema_major: number;
+ relay_error?: string;
+ };
+ expect(response.status, result.stdout).toBe(HTTP_STATUS.OK);
+ expect(response.body.user.id, result.stdout).toBe('0');
+ expect(response.schema_major).toBe(1);
+ expect(response.relay_error).toBeUndefined();
+ });
+
+ it('relays an error when the socket is missing', async () => {
+ const result = await runRelay(
+ path.join(socketDir, 'missing.sock'),
+ JSON.stringify({target: 'operator', method: 'GET', path: '/admin/users/@me'}),
+ );
+ expect(result.code).toBe(0);
+ const response = JSON.parse(result.stdout) as {status: number; relay_error: string};
+ expect(response.status).toBe(0);
+ expect(response.relay_error).not.toBe('');
+ });
+
+ it('gives loopback requests a client IP the api accepts', async () => {
+ const seen: Array<string | undefined> = [];
+ const server = createServer((req, res) => {
+ seen.push(req.headers['x-forwarded-for'] as string | undefined);
+ res.setHeader('Content-Type', 'application/json');
+ res.end('{}');
+ });
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
+ const port = String((server.address() as AddressInfo).port);
+ try {
+ const result = await runRelay(
+ socketPath,
+ JSON.stringify({target: 'loopback', method: 'GET', path: '/.well-known/fluxer'}),
+ {FLUXER_API_PORT: port, FLUXER_CLIENT_IP_HEADER_NAME: ''},
+ );
+ expect(result.code, result.stdout).toBe(0);
+ expect((JSON.parse(result.stdout) as {status: number}).status, result.stdout).toBe(HTTP_STATUS.OK);
+ expect(seen).toEqual(['127.0.0.1']);
+ } finally {
+ await new Promise<void>((resolve) => server.close(() => resolve()));
+ }
+ });
+
+ it('keeps the relay and the api on the same socket path', async () => {
+ expect(await readFile(RELAY_PATH, 'utf8')).toContain(`'${OPERATOR_SOCKET_PATH}'`);
+ });
+
+ it('gives the edge and the stack files no route to the socket', async () => {
+ const roots = [path.join(REPO_ROOT, 'deploy'), path.join(REPO_ROOT, 'fluxer_rexctl/internal/stack/files')];
+ const offenders: Array<string> = [];
+ for (const root of roots) {
+ for (const file of await filesUnder(root)) {
+ if (file.includes(`${path.sep}node_modules${path.sep}`) || file.includes(`${path.sep}target${path.sep}`))
+ continue;
+ const text = await readFile(file, 'utf8').catch(() => '');
+ if (text.includes(path.dirname(OPERATOR_SOCKET_PATH))) offenders.push(path.relative(REPO_ROOT, file));
+ }
+ }
+ expect(offenders).toEqual([]);
+ });
+});
diff --git a/fluxer_api/src/api/middleware/AdminMiddleware.ts b/fluxer_api/src/api/middleware/AdminMiddleware.ts
index 7a7ed9a32..80745e84d 100644
--- a/fluxer_api/src/api/middleware/AdminMiddleware.ts
+++ b/fluxer_api/src/api/middleware/AdminMiddleware.ts
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import {type AdminAuditAttribution, runWithAdminAuditAttribution} from '@app/api/admin/AdminAuditAttribution';
+import {isOperatorRequest, readOperatorHostUser} from '@app/api/admin/operator/OperatorRequests';
import {createApplicationID, createUserID} from '@app/api/BrandedTypes';
import {assertMutableUserId} from '@app/api/constants/Core';
import {Logger} from '@app/api/Logger';
@@ -16,7 +18,7 @@ import type {Context} from 'hono';
import {createMiddleware} from 'hono/factory';
const ADMIN_OAUTH2_APPLICATION_ID_BRANDED = createApplicationID(ADMIN_OAUTH2_APPLICATION_ID);
-type AdminAuthTokenType = 'bearer' | 'session' | 'admin_api_key';
+type AdminAuthTokenType = 'bearer' | 'session' | 'admin_api_key' | 'operator';
const ADMIN_TARGET_USER_PARAMS = ['user_id', 'target_user_id'] as const;
function ensureBearerIsBuiltInAdminApplication(ctx: Context<HonoEnv>): void {
@@ -30,10 +32,16 @@ function getAdminTokenType(ctx: Context<HonoEnv>): AdminAuthTokenType {
if (tokenType === 'bearer' || tokenType === 'session' || tokenType === 'admin_api_key') {
return tokenType;
}
+ if (tokenType === 'operator' && isOperatorRequest(ctx.req.raw)) {
+ return tokenType;
+ }
throw new UnauthorizedError();
}
-function ensureAdminCanAuthenticate(adminUser: User): void {
+function ensureAdminCanAuthenticate(adminUser: User, tokenType: AdminAuthTokenType): void {
+ if (tokenType === 'operator') {
+ return;
+ }
if (!adminUser.acls.has(AdminACLs.AUTHENTICATE) && !adminUser.acls.has(AdminACLs.WILDCARD)) {
throw new MissingPermissionsError();
}
@@ -68,9 +76,24 @@ function ensureAdminApiKeyOwnerCanUseACLs(
}
function getRequestAdminACLs(ctx: Context<HonoEnv>, adminUser: User, tokenType: AdminAuthTokenType): Set<string> {
+ if (tokenType === 'operator') {
+ return new Set([AdminACLs.WILDCARD]);
+ }
return tokenType === 'admin_api_key' ? (ctx.get('adminApiKeyAcls') ?? new Set()) : adminUser.acls;
}
+function getAdminAuditAttribution(ctx: Context<HonoEnv>, tokenType: AdminAuthTokenType): AdminAuditAttribution | null {
+ if (tokenType === 'operator') {
+ const hostUser = readOperatorHostUser(ctx.req.raw);
+ return hostUser === null ? {via: 'rexctl'} : {via: 'rexctl', host_user: hostUser};
+ }
+ if (tokenType === 'admin_api_key') {
+ const keyId = ctx.get('adminApiKeyId');
+ return keyId === null || keyId === undefined ? null : {via: `api_key:${keyId}`};
+ }
+ return null;
+}
+
function ensureAdminTargetIsMutable(ctx: Context<HonoEnv>): void {
if (ctx.req.method === 'GET') {
return;
@@ -106,7 +129,7 @@ function requireAdminAccess(requiredACLs: ReadonlyArray<string>) {
},
'Checking admin ACL requirements',
);
- ensureAdminCanAuthenticate(adminUser);
+ ensureAdminCanAuthenticate(adminUser, tokenType);
ensureAdminApiKeyOwnerCanUseACLs(adminUser, tokenType, requiredACLs);
if (!hasAnyAdminACL(requestAcls, requiredACLs)) {
throw new MissingACLError(requiredACLs[0] ?? AdminACLs.AUTHENTICATE);
@@ -114,7 +137,7 @@ function requireAdminAccess(requiredACLs: ReadonlyArray<string>) {
ensureAdminTargetIsMutable(ctx);
ctx.set('adminUserId', adminUser.id);
ctx.set('adminUserAcls', requestAcls);
- await next();
+ await runWithAdminAuditAttribution(getAdminAuditAttribution(ctx, tokenType), next);
});
}
diff --git a/fluxer_api/src/api/middleware/AuthMiddleware.ts b/fluxer_api/src/api/middleware/AuthMiddleware.ts
index 32e53d24b..0022fd298 100644
--- a/fluxer_api/src/api/middleware/AuthMiddleware.ts
+++ b/fluxer_api/src/api/middleware/AuthMiddleware.ts
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-import type {HonoEnv} from '@app/api/types/HonoEnv';
+import type {AuthTokenType, HonoEnv} from '@app/api/types/HonoEnv';
import {AccessDeniedError} from '@fluxer/errors/src/domains/core/AccessDeniedError';
import {UnauthorizedError} from '@fluxer/errors/src/domains/core/UnauthorizedError';
import {createMiddleware} from 'hono/factory';
function ensureOAuth2BearerRouteSupport(
- authTokenType: 'session' | 'bearer' | 'bot' | 'admin_api_key' | undefined,
+ authTokenType: AuthTokenType | undefined,
oauthBearerAllowed: boolean | undefined,
): void {
if (authTokenType === 'bearer' && !oauthBearerAllowed) {
diff --git a/fluxer_api/src/api/middleware/UserMiddleware.ts b/fluxer_api/src/api/middleware/UserMiddleware.ts
index eac5fc586..52b0a7ea8 100644
--- a/fluxer_api/src/api/middleware/UserMiddleware.ts
+++ b/fluxer_api/src/api/middleware/UserMiddleware.ts
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import {isOperatorPath, isOperatorRequest} from '@app/api/admin/operator/OperatorRequests';
import * as AuthSession from '@app/api/auth/AuthSession';
+import {SYSTEM_USER_ID} from '@app/api/constants/Core';
import {Logger} from '@app/api/Logger';
import {hashRequestToken, recordAuthFailure} from '@app/api/middleware/RequestErrorTelemetry';
import type {User} from '@app/api/models/User';
@@ -89,6 +91,18 @@ export const UserMiddleware = createMiddleware<HonoEnv>(async (ctx, next) => {
ctx.set('oauthBearerAllowed', false);
ctx.set('oauthBearerScopes', undefined);
ctx.set('authToken', undefined);
+ ctx.set('adminApiKeyId', null);
+ if (isOperatorRequest(ctx.req.raw)) {
+ if (isOperatorPath(path)) {
+ const operator = await ctx.get('apiContext').services.users.findUnique(SYSTEM_USER_ID);
+ if (operator) {
+ ctx.set('authTokenType', 'operator');
+ setUserInContext(ctx, operator, false);
+ }
+ }
+ await next();
+ return;
+ }
if (!parsed) {
if (rawAuthHeader) {
recordAuthFailure(resolvedClientIp, hashRequestToken(rawAuthHeader));
@@ -168,6 +182,7 @@ export const UserMiddleware = createMiddleware<HonoEnv>(async (ctx, next) => {
if (user) {
ctx.set('authTokenType', 'admin_api_key');
ctx.set('adminApiKeyAcls', apiKey.acls);
+ ctx.set('adminApiKeyId', apiKey.keyId);
setUserInContext(ctx, user, false);
}
} else {
diff --git a/fluxer_api/src/api/utils/RequestClientIp.ts b/fluxer_api/src/api/utils/RequestClientIp.ts
index afe97fce4..032dfe680 100644
--- a/fluxer_api/src/api/utils/RequestClientIp.ts
+++ b/fluxer_api/src/api/utils/RequestClientIp.ts
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import {isOperatorRequest, OPERATOR_PEER_ADDRESS} from '@app/api/admin/operator/OperatorRequests';
import {Config} from '@app/api/Config';
import type {APIConfig} from '@app/api/config/APIConfig';
import type {HonoEnv} from '@app/api/types/HonoEnv';
@@ -35,6 +36,9 @@ export const PeerAddressMiddleware = createMiddleware<HonoEnv>(async (ctx, next)
});
function withPeerAddress(request: Request, options: ClientIpResolutionOptions) {
+ if (isOperatorRequest(request)) {
+ return {...options, trustClientIpHeader: false, peerAddress: OPERATOR_PEER_ADDRESS};
+ }
return {...options, peerAddress: peerAddresses.get(request) ?? null};
}