Compare commits

...

5 Commits

7 changed files with 1337 additions and 1 deletions

4
.gitignore vendored
View File

@ -62,3 +62,7 @@ i18n.cache
# Generated by apps/frontend/scripts/fetch-gtm.mjs on install
apps/frontend/public/g.js
# Codex
.codex
AGENTS.md

View File

@ -1,3 +1,4 @@
import { PublicPrivacyController } from './routes/v1/public.privacy.controller';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AuthService } from '@gitroom/backend/services/auth/auth.service';
import { StripeService } from '@gitroom/nestjs-libraries/services/stripe.service';
@ -12,7 +13,7 @@ import { PublicIntegrationsController } from '@gitroom/backend/public-api/routes
import { PublicAuthMiddleware } from '@gitroom/backend/services/auth/public.auth.middleware';
import { SuperAdminGuard } from '@gitroom/backend/services/auth/super.admin.guard';
const authenticatedController = [PublicIntegrationsController];
const authenticatedController = [PublicIntegrationsController, PublicPrivacyController];
@Module({
imports: [UploadModule],
controllers: process.env.MCP_ONLY ? [] : [...authenticatedController],

View File

@ -0,0 +1,108 @@
import {
Body,
Controller,
Logger,
Post,
UseGuards,
Res,
ValidationPipe,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { Organization } from '@prisma/client';
import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request';
import { SuperAdminGuard } from '@gitroom/backend/services/auth/super.admin.guard';
import { PrivacyExportDto } from '@gitroom/nestjs-libraries/dtos/users/privacy.export.dto';
import { PrivacyExportService } from '@gitroom/nestjs-libraries/database/prisma/privacy/privacy.export.service';
class ExportReaderDisconnected extends Error {
constructor() {
super('Export reader disconnected');
}
}
@ApiTags('Privacy exports')
@Controller('/public/v1/privacy')
// Person exports are platform-wide and require the platform SuperAdminGuard.
@UseGuards(SuperAdminGuard)
export class PublicPrivacyController {
private readonly _logger = new Logger(PublicPrivacyController.name);
constructor(private _exports: PrivacyExportService) {}
@Post('/export')
async export(
@GetOrgFromRequest() org: Organization,
@Body(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
})
)
body: PrivacyExportDto,
@Res({ passthrough: true }) response: Response
) {
response.setHeader('Cache-Control', 'no-store');
return this._exports.export(org.id, body);
}
@Post('/export/stream')
async stream(
@GetOrgFromRequest() org: Organization,
@Body(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
})
)
body: PrivacyExportDto,
@Res() response: Response
) {
response.setHeader('Cache-Control', 'no-store');
response.setHeader('Content-Type', 'application/x-ndjson');
response.setHeader('X-Accel-Buffering', 'no');
try {
await this._exports.stream(org.id, body, async (line) => {
if (response.destroyed) throw new ExportReaderDisconnected();
if (response.write(line)) return;
await new Promise<void>((resolve, reject) => {
const cleanup = () => {
response.off('drain', drain);
response.off('close', close);
response.off('error', fail);
};
const drain = () => {
cleanup();
resolve();
};
const close = () => {
cleanup();
reject(new ExportReaderDisconnected());
};
const fail = (error: Error) => {
cleanup();
reject(error);
};
response.once('drain', drain);
response.once('close', close);
response.once('error', fail);
});
});
response.end();
} catch (error) {
if (response.headersSent) {
if (!(error instanceof ExportReaderDisconnected)) {
this._logger.error(
`Privacy export stream failed for ${body.target} ${body.targetId}`,
error
);
}
response.destroy();
return;
}
throw error;
}
}
}

View File

@ -1,3 +1,5 @@
import { PrivacyExportRepository } from './privacy/privacy.export.repository';
import { PrivacyExportService } from './privacy/privacy.export.service';
import { Global, Module } from '@nestjs/common';
import { PrismaRepository, PrismaService, PrismaTransaction } from './prisma.service';
import { OrganizationRepository } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.repository';
@ -56,6 +58,8 @@ import { AdminStatsService } from '@gitroom/nestjs-libraries/database/prisma/adm
imports: [],
controllers: [],
providers: [
PrivacyExportRepository,
PrivacyExportService,
PrismaService,
PrismaRepository,
PrismaTransaction,

View File

@ -0,0 +1,144 @@
import { createHash } from 'node:crypto';
import {
Injectable,
NotFoundException,
ForbiddenException,
PayloadTooLargeException,
} from '@nestjs/common';
import { PrivacyExportDto } from '@gitroom/nestjs-libraries/dtos/users/privacy.export.dto';
import { PrivacyExportRepository } from './privacy.export.repository';
@Injectable()
export class PrivacyExportService {
constructor(private _exports: PrivacyExportRepository) {}
async export(authorizedWorkspaceId: string, input: PrivacyExportDto) {
// The caller must enforce platform super-admin access for person exports.
// Person records span workspaces; only workspace exports are org-scoped.
if (
input.target === 'workspace' &&
input.targetId !== authorizedWorkspaceId
) {
throw new ForbiddenException(
'The export target must be the authorized workspace.'
);
}
const data = await this._exports.snapshot(input.target, input.targetId);
if (!data) throw new NotFoundException('Export target not found.');
this.checkSize(data, input.target === 'workspace' ? 'workspace' : '');
const result = {
format: 'postiz-privacy-export-v1',
generatedAt: new Date().toISOString(),
requestReference: input.requestReference,
target: { type: input.target, id: input.targetId },
reviewRequired: true,
data,
manifest: this.manifest(),
};
const serialized = JSON.stringify(result, (_key, value) =>
typeof value === 'bigint' ? value.toString() : value
);
if (Buffer.byteLength(serialized, 'utf8') > 25_000_000) {
throw new PayloadTooLargeException(
'This export exceeds 25 MB. Request a manual controller export.'
);
}
// Return the same JSON-safe representation that was checked for size.
return JSON.parse(serialized);
}
async stream(
authorizedWorkspaceId: string,
input: PrivacyExportDto,
write: (line: string) => Promise<void>
) {
if (
input.target === 'workspace' &&
input.targetId !== authorizedWorkspaceId
)
throw new ForbiddenException(
'The export target must be the authorized workspace.'
);
const hash = createHash('sha256');
let records = 0;
const line = async (value: unknown) => {
const encoded =
JSON.stringify(value, (_key, item) =>
typeof item === 'bigint' ? item.toString() : item
) + '\n';
hash.update(encoded);
await write(encoded);
};
const found = await this._exports.stream(
input.target,
input.targetId,
() =>
line({
kind: 'header',
format: 'postiz-privacy-export-stream-v1',
generatedAt: new Date().toISOString(),
requestReference: input.requestReference,
target: { type: input.target, id: input.targetId },
reviewRequired: true,
batchSize: 100,
manifest: this.manifest(),
}),
async (category, value) => {
await line({ kind: 'record', category, value });
records++;
}
);
if (!found) throw new NotFoundException('Export target not found.');
// Written only after the snapshot transaction succeeds. A truncated stream is incomplete.
await write(
JSON.stringify({
kind: 'complete',
records,
sha256: hash.digest('hex'),
}) + '\n'
);
}
private manifest() {
return {
credentialsExcluded: true,
media:
'Stored references only. Media bytes require authorised storage retrieval.',
personAttribution:
'Workspace posts do not store an author user ID. Workspace content is not attributed to a person merely because they are a member.',
controllerInputRequired: [
'Social network and OAuth provider records outside the Postiz database',
'Payment provider, email delivery, support and operational monitoring records',
'Backups and media storage, including availability of soft-deleted files',
'AutoPost feed URLs and configurations require controller review because URLs may embed credentials',
'Workspace marketplace orders and buyer correspondence require controller retrieval and third-party privacy review',
'Person marketplace messages, message groups and orders contain the counterparty identity and words and require controller third-party privacy review before release',
'Clipping source URLs, error diagnostics and thumbnails on source records require controller review for embedded credentials',
'AI conversations and traces require controller attribution review; opaque runtime payloads can contain credentials',
'Opaque integration settings, plug configuration, error bodies and webhook URLs are withheld because they can contain credentials; controller must review any attributable non-secret data',
],
withheldFields: [
'Post.settings and Post.error: opaque provider settings and error bodies that can contain credentials',
'Post.submittedForOrderId, Post.submittedForOrganizationId, Post.approvedSubmitForOrder and Post.lastMessageId: marketplace links, covered by the marketplace review above',
'Messages.special: opaque marketplace message payload that can contain credentials or third-party data',
'Orders.captureId, Organization.paymentId and Subscription.identifier: payment provider references, retrieved from the payment provider',
'User.account, User.connectedAccount and User.inviteId: payment provider account reference and state, and invitation reference',
'UsedCodes: redeemed purchase and promotional code rows of a workspace',
],
};
}
private checkSize(value: unknown, path: string): void {
if (Array.isArray(value)) {
if (value.length > 1000)
throw new PayloadTooLargeException(
`Data category ${path} exceeds 1,000 records. Request a complete manual controller export.`
);
value.forEach((item, index) => this.checkSize(item, `${path}[${index}]`));
} else if (value && typeof value === 'object') {
for (const [key, item] of Object.entries(value)) {
this.checkSize(item, path ? `${path}.${key}` : key);
}
}
}
}

View File

@ -0,0 +1,14 @@
import { IsIn, IsString, IsUUID, Length } from 'class-validator';
export class PrivacyExportDto {
@IsString()
@IsIn(['person', 'workspace'])
target: 'person' | 'workspace';
@IsUUID()
targetId: string;
@IsString()
@Length(1, 100)
requestReference: string;
}