forked from Betta-Pay/BettaPay-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.ts
More file actions
223 lines (195 loc) · 8.37 KB
/
Copy pathplugins.ts
File metadata and controls
223 lines (195 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { FastifyInstance, FastifyError, FastifyBaseLogger, FastifyRequest, FastifyReply } from 'fastify';
import { z } from 'zod';
import crypto from 'crypto';
import { createErrorResponse, ErrorCodes } from './index.js';
// Makes `fastify.serviceAuth` available as a typed decorator/preValidation hook.
declare module 'fastify' {
interface FastifyInstance {
serviceAuth: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
}
}
export type ErrorClass = 'fatal' | 'transient' | 'validation' | 'security' | 'business';
/**
* Classify an error into an alerting-friendly category.
*
* - `fatal` — infrastructure outage (DB unreachable, OOM, unhandled crash)
* - `transient` — retryable (rate limit, timeout, temporary upstream failure)
* - `validation` — bad input (Zod, Prisma unique constraint, invalid request)
* - `security` — auth/authz failure (401, 403, invalid token)
* - `business` — domain rule violation (insufficient funds, duplicate settlement)
*/
export function classifyError(error: unknown, statusCode?: number): ErrorClass {
// Prisma error codes
const prismaCode = (error as { code?: string }).code;
if (prismaCode === 'P1001' || prismaCode === 'P1002' || prismaCode === 'P1017') {
return 'fatal'; // DB unreachable / connection refused
}
if (prismaCode === 'P2002' || prismaCode === 'P2025') {
return 'validation'; // unique constraint / record not found
}
// Zod validation errors
if (error instanceof z.ZodError) {
return 'validation';
}
// Fastify rate-limit
if ((error as { statusCode?: number }).statusCode === 429) {
return 'transient';
}
// HTTP status-based classification
if (statusCode === 401 || statusCode === 403) {
return 'security';
}
if (statusCode === 400 || statusCode === 422) {
return 'validation';
}
if (statusCode === 503 || statusCode === 504) {
return 'transient';
}
// Network / timeout errors
const msg = String((error as Error).message ?? '').toLowerCase();
if (msg.includes('timeout') || msg.includes('econnrefused') || msg.includes('enotfound')) {
return 'transient';
}
return 'fatal';
}
export const PII_FIELD_PATTERNS = [/email/i, /address/i, /secret/i, /key/i, /token/i];
export function isPiiField(path: (string | number)[]): boolean {
return path.some((segment) =>
typeof segment === 'string' && PII_FIELD_PATTERNS.some((re) => re.test(segment))
);
}
export function redactPiiFromDetails(details: unknown): unknown {
if (!Array.isArray(details)) return details;
return details.map((item: Record<string, unknown>) => {
const path: (string | number)[] = Array.isArray(item.path)
? (item.path as (string | number)[])
: typeof item.instancePath === 'string'
? item.instancePath.split('/').filter(Boolean)
: [];
if (isPiiField(path)) {
return { ...item, message: '[REDACTED]', received: undefined, data: undefined, value: undefined, params: undefined };
}
return item;
});
}
export function registerErrorHandler(fastify: FastifyInstance, customLogger?: FastifyBaseLogger) {
fastify.setErrorHandler((error, request, reply) => {
const logger = customLogger || request.log || fastify.log;
if (error instanceof z.ZodError) {
const response = createErrorResponse(ErrorCodes.VALIDATION_ERROR, 'Invalid request data', redactPiiFromDetails(error.errors));
return reply.code(400).send(response);
}
if ((error as FastifyError).statusCode) {
const fastifyErr = error as FastifyError & { validation?: unknown };
const code = fastifyErr.code || ErrorCodes.INVALID_REQUEST;
const details = fastifyErr.validation ? redactPiiFromDetails(fastifyErr.validation) : undefined;
const response = createErrorResponse(code, fastifyErr.message, details);
return reply.code(fastifyErr.statusCode!).send(response);
}
// Generic fallback for unhandled errors — assign a referenceId so the
// client can quote it when reporting the problem, and the server log entry
// ties back to exactly that request.
const referenceId = crypto.randomUUID();
const errorClass = classifyError(error);
const errObj = error instanceof Error ? error : new Error(String(error));
logger.error(
{ err: errObj, reqId: request.id, referenceId, errorClass, stack: errObj.stack },
'Unhandled internal error',
);
return reply.code(500).send({
error: 'Internal Server Error',
statusCode: 500,
referenceId,
});
});
// Belt-and-suspenders: Fastify's onError lifecycle hook fires after the
// error handler has run. We use it to catch any error that reaches this
// stage without already having been converted to a response (e.g. errors
// thrown inside reply serialization or other hooks). If the reply has
// already been sent this is a no-op.
fastify.addHook('onError', async (request, reply, error) => {
if (reply.sent) return;
const logger = customLogger || request.log || fastify.log;
const referenceId = crypto.randomUUID();
const errorClass = classifyError(error, reply.statusCode);
const errObj = error instanceof Error ? error : new Error(String(error));
logger.error(
{ err: errObj, reqId: request.id, referenceId, errorClass, stack: errObj.stack },
'Panic recovery: unhandled error reached onError hook',
);
return reply.code(500).send({
error: 'Internal Server Error',
statusCode: 500,
referenceId,
});
});
}
/**
* Constant-time string equality. Avoids leaking secret length/contents through
* early-exit timing. Returns false for length mismatches (after a same-length
* compare to keep timing uniform).
*/
export function timingSafeStrEqual(a: string, b: string): boolean {
const ab = Buffer.from(a, 'utf8');
const bb = Buffer.from(b, 'utf8');
if (ab.length !== bb.length) {
crypto.timingSafeEqual(ab, ab);
return false;
}
return crypto.timingSafeEqual(ab, bb);
}
/**
* Build a Fastify preValidation handler that authenticates inter-service calls.
*
* Internal (service-to-service) endpoints should present the shared
* `INTER_SERVICE_SECRET` in the `x-service-token` header. Requests without a
* valid token are rejected with 401 before the route handler runs.
*
* @param secret the shared INTER_SERVICE_SECRET
*/
export function createServiceAuth(
secretOrSecrets: string | string[],
): (request: FastifyRequest, reply: FastifyReply) => Promise<void> {
// Normalize to an array of trimmed, non-empty secrets. Accepts either a
// single string (possibly comma-separated) or a string[].
const secrets: string[] = Array.isArray(secretOrSecrets)
? secretOrSecrets.map((s) => String(s).trim()).filter(Boolean)
: String(secretOrSecrets)
.split(',')
.map((s) => s.trim())
.filter(Boolean);
if (!secrets.length) {
throw new Error('createServiceAuth: a non-empty INTER_SERVICE_SECRET is required');
}
return async function serviceAuth(request: FastifyRequest, reply: FastifyReply): Promise<void> {
const header = request.headers['x-service-token'];
const token = Array.isArray(header) ? header[0] : header;
// If token is missing or doesn't match any accepted secret, reject.
if (!token) {
request.log?.warn({ reqId: request.id }, 'serviceAuth: missing service token');
return reply
.code(401)
.send(createErrorResponse(ErrorCodes.UNAUTHORIZED, 'Invalid or missing service token'));
}
// Compare against all configured secrets using timing-safe equality.
let matched = false;
for (const s of secrets) {
if (timingSafeStrEqual(String(token), s)) {
matched = true;
}
}
if (!matched) {
request.log?.warn({ reqId: request.id }, 'serviceAuth: invalid service token');
return reply
.code(401)
.send(createErrorResponse(ErrorCodes.UNAUTHORIZED, 'Invalid or missing service token'));
}
};
}
/**
* Decorate a Fastify instance with `serviceAuth` so routes can use
* `preValidation: [fastify.serviceAuth]` to require a valid service token.
*/
export function registerServiceAuth(fastify: FastifyInstance, secretOrSecrets: string | string[]): void {
fastify.decorate('serviceAuth', createServiceAuth(secretOrSecrets));
}