-
Notifications
You must be signed in to change notification settings - Fork 31
/
access-control.ts
329 lines (293 loc) · 10.2 KB
/
access-control.ts
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
326
327
328
329
import signingKeyApp from "./signing-key";
import { validatePost } from "../middleware";
import { Request, Router } from "express";
import _ from "lodash";
import { db } from "../store";
import sql from "sql-template-strings";
import {
NotFoundError,
ForbiddenError,
BadRequestError,
BadGatewayError,
InternalServerError,
} from "../store/errors";
import tracking from "../middleware/tracking";
import { DBWebhook } from "../store/webhook-table";
import { Asset, PlaybackPolicy, User } from "../schema/types";
import { signatureHeaders, storeTriggerStatus } from "../webhooks/cannon";
import { Response } from "node-fetch";
import { fetchWithTimeoutAndRedirects } from "../util";
import fetch from "node-fetch";
import { WithID } from "../store/types";
import { DBStream } from "../store/stream-table";
import { HACKER_DISABLE_CUTOFF_DATE } from "./utils/notification";
import { isFreeTierUser } from "./helpers";
import { cache } from "../store/cache";
const WEBHOOK_TIMEOUT = 30 * 1000;
const MAX_ALLOWED_VIEWERS_FOR_FREE_TIER = 5;
const app = Router();
interface HitRecord {
timestamp: number;
}
const playbackHits: Record<string, HitRecord[]> = {};
async function fireGateWebhook(
webhook: DBWebhook,
plabackPolicy: PlaybackPolicy,
accessKey: string
) {
let timestamp = Date.now();
let params = {
method: "POST",
headers: {
"content-type": "application/json",
"user-agent": "livepeer.studio",
},
timeout: WEBHOOK_TIMEOUT,
body: JSON.stringify({
context: plabackPolicy.webhookContext,
accessKey: accessKey,
timestamp: timestamp,
}),
};
const sigHeaders = signatureHeaders(
params.body,
webhook.sharedSecret,
timestamp
);
params.headers = { ...params.headers, ...sigHeaders };
const startTime = process.hrtime();
let resp: Response;
let errorMessage: string;
let statusCode: number;
try {
resp = await fetchWithTimeoutAndRedirects(webhook.url, params);
statusCode = resp.status;
if (resp.status < 200 || resp.status >= 300) {
console.error(
`access-control: gate: webhook=${webhook.id} got response status != 2XX statusCode=${resp.status}`
);
errorMessage = `webhook=${webhook.id} got response status != 2XX statusCode=${resp.status}`;
}
} catch (e) {
errorMessage = e.message;
statusCode = 0;
} finally {
await storeTriggerStatus(
webhook,
timestamp,
statusCode,
errorMessage,
undefined
);
if (resp) {
// Dispose of the response body
await resp.text();
}
}
return statusCode;
}
app.use("/signing-key", signingKeyApp);
app.post(
"/gate",
validatePost("access-control-gate-payload"),
async (req, res) => {
const playbackId = req.body.stream.replace(/^\w+\+/, "");
let content = await cache.getOrSet(
`acl-content-${playbackId}`,
async () => {
return (
(await db.stream.getByPlaybackId(playbackId)) ||
(await db.asset.getByPlaybackId(playbackId))
);
}
);
res.set("Cache-Control", "max-age=120,stale-while-revalidate=600");
if (!content || content.deleted) {
const contentLog = JSON.stringify(JSON.stringify(content));
console.log(`
access-control: gate: content not found for playbackId=${playbackId}, disallowing playback, content=${contentLog}
`);
throw new NotFoundError("Content not found");
}
let user = await db.user.get(content.userId, { useCache: true });
if (user.suspended || ("suspended" in content && content.suspended)) {
const contentLog = JSON.stringify(JSON.stringify(content));
console.log(`
access-control: gate: disallowing access for contentId=${content.id} playbackId=${playbackId}, user=${user.id} is suspended, content=${contentLog}
`);
throw new NotFoundError("Content not found");
}
const playbackPolicyType = content.playbackPolicy?.type ?? "public";
if (user.createdAt < HACKER_DISABLE_CUTOFF_DATE) {
let limitReached = freeTierLimitReached(content, user, req);
if (limitReached) {
throw new ForbiddenError("Free tier user reached viewership limit");
}
}
if (
playbackPolicyType !== "public" &&
req.body.pub === req.config.accessControlAdminPubkey &&
req.body.pub !== "" &&
req.body.pub
) {
res.status(204);
return res.end();
}
switch (playbackPolicyType) {
case "public":
res.status(204);
return res.end();
case "jwt":
if (!req.body.pub) {
console.log(`
access-control: gate: no pub provided for playbackId=${playbackId}, disallowing playback
`);
throw new ForbiddenError(
"Content is gated and requires a public key"
);
}
const [signingKeyOutput] = await cache.getOrSet(
`acl-signing-key-pub-${req.body.pub}`,
() => {
const query = [
sql`signing_key.data->>'publicKey' = ${req.body.pub}`,
];
return db.signingKey.find(query, { limit: 2 });
}
);
if (signingKeyOutput.length == 0) {
console.log(`
access-control: gate: content with playbackId=${playbackId} is gated but corresponding public key not found for key=${req.body.pub}, disallowing playback
`);
throw new ForbiddenError(
"Content is gated and corresponding public key not found"
);
}
if (signingKeyOutput.length > 1) {
let collisionKeys = JSON.stringify(signingKeyOutput);
console.log(`
access-control: gate: content contentId=${content.id} with playbackId=${playbackId} is gated but multiple (${signingKeyOutput.length}) public keys found for key=${req.body.pub}, disallowing playback, colliding keys=${collisionKeys}
`);
throw new BadRequestError(
"Multiple signing keys found for the same public key."
);
}
const signingKey = signingKeyOutput[0];
if (signingKey.userId !== content.userId) {
console.log(`
access-control: gate: disallowing playback for contentId=${content.id} with playbackId=${playbackId} the content and the public key pub=${req.body.pub} do not share the same owner, signingKeyUserId=${signingKey.userId}, contentUserId=${content.userId}
`);
throw new NotFoundError("Content not found");
}
if (signingKey.disabled || signingKey.deleted) {
const signingKeyLog = JSON.stringify(JSON.stringify(signingKey));
console.log(`
access-control: gate: disallowing playback for contentId=${content.id} with playbackId=${playbackId} the public key pub=${signingKey.id} is disabled or deleted, signingKey=${signingKeyLog}
`);
throw new ForbiddenError("The public key is disabled or deleted");
}
tracking.recordSigningKeyValidation(signingKey.id);
res.status(204);
return res.end();
case "webhook":
if (!req.body.accessKey || req.body.type !== "accessKey") {
throw new ForbiddenError(
"Content is gated and requires an access key"
);
}
const webhook = await db.webhook.get(content.playbackPolicy.webhookId, {
useCache: true,
});
if (!webhook) {
console.log(`
access-control: gate: content with playbackId=${playbackId} is gated but corresponding webhook not found for webhookId=${content.playbackPolicy.webhookId}, disallowing playback
`);
throw new InternalServerError(
"Content is gated and corresponding webhook not found"
);
}
const statusCode = await fireGateWebhook(
webhook,
content.playbackPolicy,
req.body.accessKey
);
if (statusCode >= 200 && statusCode < 300) {
res.status(204);
return res.end();
} else if (statusCode === 0) {
console.log(`
access-control: gate: content with playbackId=${playbackId} is gated but webhook=${webhook.id} failed, disallowing playback
`);
throw new BadGatewayError(
"Content is gated and corresponding webhook failed"
);
} else {
console.log(`
access-control: gate: content with playbackId=${playbackId} is gated but webhook=${webhook.id} returned status code ${statusCode}, disallowing playback
`);
throw new BadGatewayError(
"Content is gated and corresponding webhook failed"
);
}
default:
throw new BadRequestError(
`unknown playbackPolicy type: ${playbackPolicyType}`
);
}
}
);
app.get("/public-key", async (req, res) => {
const { catalystBaseUrl } = req.config;
let url = `${catalystBaseUrl}/api/pubkey`;
let options = {
method: "GET",
headers: {
"Content-Type": "application/json",
},
};
await fetch(url, options)
.then((response) => {
if (response.status === 200) {
return response.json();
} else {
throw new Error(
`access-control: error retrieving public key from catalyst statusCode=${response.status} catalystUrl=${url}`
);
}
})
.then((responseJson) => {
res.status(200).json(responseJson);
})
.catch((error) => {
console.log(error);
res.status(500).json({ error: "Unable to retrieve public key" });
});
});
function freeTierLimitReached(
content: DBStream | WithID<Asset>,
user: User,
req: Request
): boolean {
if (!isFreeTierUser(user)) {
return false;
}
// Register a hit for the given playbackId
const now = Date.now();
const playbackId = content.playbackId;
if (!playbackHits[playbackId]) {
playbackHits[playbackId] = [];
}
// Remove hits that are older than three minutes
playbackHits[playbackId] = playbackHits[playbackId].filter(
(hit) => now - hit.timestamp < 60 * 3 * 1000
);
// Add a new hit record
playbackHits[playbackId].push({ timestamp: now });
// Check if the number of hits in the last minute exceeds the threshold
if (playbackHits[playbackId].length > MAX_ALLOWED_VIEWERS_FOR_FREE_TIER) {
return true;
} else {
return false;
}
}
export default app;