forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemas.js
More file actions
325 lines (293 loc) · 9.19 KB
/
Copy pathschemas.js
File metadata and controls
325 lines (293 loc) · 9.19 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"use strict";
const { z } = require("zod");
const webhookEvents = require("../services/webhookEvents");
const config = require("../config");
const stellarPublicKeySchema = z
.string()
.regex(/^G[A-Z0-9]{55}$/, "Must be a valid Stellar public key");
const { toStroops, sumStroops, stroopsEqual } = require("../utils/stroops");
const assetCodeSchema = z
.string()
.trim()
.min(1, "Asset code is required")
.max(12, "Asset code must be 12 characters or fewer")
.regex(/^[A-Za-z0-9]+$/, "Asset code must be alphanumeric")
.transform((value) => value.toUpperCase());
const optionalIssuerSchema = z.preprocess(
(value) => (value === "" ? undefined : value),
stellarPublicKeySchema.optional(),
);
const paginationQuerySchema = z.object({
page: z.coerce.number().int().min(1).default(1),
limit: z.coerce.number().int().min(1).max(100).default(20),
});
const routeIdParamsSchema = z.object({
id: z
.string()
.trim()
.min(1)
.max(128)
.regex(
/^[A-Za-z0-9_-]+$/,
"ID can contain only letters, numbers, underscores, and hyphens",
),
});
// Ranges that must never be reachable from an operator-supplied URL — hitting
// them lets an attacker use this server as a relay into the internal network.
const PRIVATE_HOSTNAME_RE = /^(localhost|.*\.local)(:\d+)?$/i;
const PRIVATE_IP_RE = new RegExp(
"^(" +
"127\\." + // loopback
"|10\\." + // RFC-1918 /8
"|172\\.(1[6-9]|2\\d|3[01])\\." + // RFC-1918 /12
"|192\\.168\\." + // RFC-1918 /16
"|169\\.254\\." + // link-local
"|0\\.0\\.0\\.0" + // unspecified
"|::1" + // IPv6 loopback
"|fc[0-9a-f]{2}:" + // IPv6 ULA
")",
);
function isPrivateTarget(hostname) {
return PRIVATE_HOSTNAME_RE.test(hostname) || PRIVATE_IP_RE.test(hostname);
}
const CONTROL_CHAR_RE = /[\x00-\x08\x0E-\x1F\x7F]/;
const httpUrlSchema = z
.string()
.trim()
.refine((value) => !CONTROL_CHAR_RE.test(value), {
message: "URL must not contain control characters",
})
.refine(
(value) => {
try {
const url = new URL(value);
return ["http:", "https:"].includes(url.protocol);
} catch {
return false;
}
},
{
message: "Must be an http(s) URL",
},
)
.refine(
(value) => {
try {
const { hostname } = new URL(value);
return !isPrivateTarget(hostname);
} catch {
return false;
}
},
{
message: "URL must not target a private or internal network address",
},
);
const priceParamsSchema = z.object({
asset_code: assetCodeSchema,
});
const priceQuerySchema = z.object({
issuer: optionalIssuerSchema,
});
const keyCreateBodySchema = z.object({
label: z.string().trim().min(1).max(80),
scopes: z.array(z.string().trim().min(1)).nonempty().optional(),
// Sizes the key's own rate limit bucket (issue #251). Enumerated from
// configuration so adding a tier does not require touching validation.
tier: z.enum(Object.keys(config.apiKeyRateLimit.tiers)).optional(),
});
const keyRotateBodySchema = z.object({
// Preserves label and scopes from the old key, but allows overriding tier
tier: z.enum(Object.keys(config.apiKeyRateLimit.tiers)).optional(),
});
const alertCreateBodySchema = z.object({
asset: assetCodeSchema.refine(
(code) =>
config.watchedAssets.length === 0 || config.watchedAssets.includes(code),
{ message: "Asset code is not in the list of watched Stellar assets" },
),
type: z.enum(["above", "below", "change_pct"]),
threshold_usd: z.number().positive(),
webhook_url: httpUrlSchema,
webhook_secret: z.string().min(8),
repeat: z.boolean().optional(),
});
const webhookSubscriptionSchema = z
.array(z.string().trim())
.nonempty()
.refine((value) => webhookEvents.isValidSubscription(value))
.superRefine((events, ctx) => {
if (events.length === 1 && events[0] === webhookEvents.WILDCARD) {
return;
}
if (events.includes(webhookEvents.WILDCARD)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Wildcard "${webhookEvents.WILDCARD}" cannot be mixed with explicit event types`,
});
return;
}
if (events.length > webhookEvents.MAX_EXPLICIT_SUBSCRIPTIONS) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Too many explicit subscriptions (max ${webhookEvents.MAX_EXPLICIT_SUBSCRIPTIONS})`,
});
return;
}
const invalidEvents = events.filter((e) => !webhookEvents.isKnownEvent(e));
if (invalidEvents.length > 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Unknown event type(s): ${invalidEvents.join(", ")}. Valid events are: ${webhookEvents.ALL_EVENTS.join(", ")}`,
});
}
});
const webhookFiltersSchema = z
.object({
asset: z
.string()
.trim()
.min(1)
.max(12)
.regex(/^[A-Za-z0-9]+$/, "Asset filter must be alphanumeric")
.transform((value) => value.toUpperCase())
.optional(),
pool_id: z.string().trim().min(1).max(128).optional(),
})
.strict()
.refine((value) => value.asset !== undefined || value.pool_id !== undefined, {
message: "At least one filter must be provided",
});
const webhookCreateBodySchema = z.object({
url: httpUrlSchema,
events: webhookSubscriptionSchema,
secret: z.string().min(16).optional(),
description: z.string().optional(),
filters: webhookFiltersSchema.optional(),
});
const webhookPatchBodySchema = z.object({
url: httpUrlSchema.optional(),
events: webhookSubscriptionSchema.optional(),
secret: z.string().min(16).optional(),
active: z.boolean().optional(),
description: z.string().optional(),
filters: webhookFiltersSchema.optional(),
});
const webhookDeliveriesQuerySchema = z.object({
limit: z.coerce.number().int().min(1).max(100).default(50),
status: z.enum(["pending", "success", "failed"]).optional(),
});
// Stellar Int64 max in stroops, divided by 10_000_000 to get max in whole units
// that can be safely represented as a JS number
const MAX_AMOUNT_UNITS = Number(9223372036854775807n / 10000000n);
const recipientSchema = z.object({
address: stellarPublicKeySchema,
amount: z
.number()
.positive()
.max(MAX_AMOUNT_UNITS, "amount exceeds Stellar Int64 ceiling")
.refine((v) => Number.isFinite(v), "amount must be a finite number")
.refine((v) => {
const str = String(v);
const dotIndex = str.indexOf(".");
return dotIndex === -1 || str.length - dotIndex - 1 <= 7;
}, "amount must have at most 7 decimal places"),
});
const recipientsSchema = z
.array(recipientSchema)
.max(10000, "recipients cannot exceed 10,000")
.superRefine((recipients, ctx) => {
const seen = new Set();
recipients.forEach((recipient, index) => {
if (seen.has(recipient.address)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: [index, "address"],
message: `recipient ${index}: duplicate address ${recipient.address}`,
});
}
seen.add(recipient.address);
});
});
function expiryLedgerSchema(currentLedger) {
return z
.number()
.int()
.gt(
currentLedger,
`expiry_ledger must be greater than current ledger (${currentLedger})`,
);
}
function airdropCreateBodySchema(currentLedger) {
return z
.object({
name: z.string().trim().min(1),
description: z.string().optional(),
asset: assetCodeSchema,
asset_issuer: stellarPublicKeySchema,
total_amount: z
.number()
.positive()
.max(MAX_AMOUNT_UNITS, `total_amount exceeds Stellar Int64 ceiling`),
expiry_ledger: expiryLedgerSchema(currentLedger),
recipients: recipientsSchema.optional().default([]),
})
.superRefine((body, ctx) => {
if (body.recipients.length === 0) return;
try {
const totalStroops = sumStroops(body.recipients);
const expectedStroops = toStroops(body.total_amount);
if (totalStroops !== expectedStroops) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["recipients"],
message: `sum of recipient amounts must equal total_amount (${body.total_amount})`,
});
}
} catch (err) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["recipients"],
message: err.message,
});
}
});
}
function airdropUpdateBodySchema(currentLedger) {
return z.object({
name: z.string().trim().min(1).optional(),
description: z.string().optional(),
expiry_ledger: expiryLedgerSchema(currentLedger).optional(),
});
}
const airdropRecipientsBodySchema = z.object({
recipients: z.preprocess((value) => {
if (typeof value !== "string") return value;
try {
return JSON.parse(value);
} catch {
return value;
}
}, recipientsSchema.optional()),
});
module.exports = {
airdropCreateBodySchema,
airdropRecipientsBodySchema,
airdropUpdateBodySchema,
alertCreateBodySchema,
assetCodeSchema,
httpUrlSchema,
keyCreateBodySchema,
keyRotateBodySchema,
optionalIssuerSchema,
paginationQuerySchema,
priceParamsSchema,
priceQuerySchema,
recipientsSchema,
routeIdParamsSchema,
stellarPublicKeySchema,
webhookCreateBodySchema,
webhookDeliveriesQuerySchema,
webhookPatchBodySchema,
webhookSubscriptionSchema,
};