-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathsyncBreaches.ts
153 lines (135 loc) · 4.4 KB
/
syncBreaches.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Cron: Daily
* Fetches the list of breaches from HIBP, sync database with the latest breaches list
*
* Usage:
* node src/scripts/syncBreaches.js
*/
import { readdir } from "node:fs/promises";
import os from "node:os";
import Sentry from "@sentry/nextjs";
import { fetchHibpBreaches, HibpGetBreachesResponse } from "../../utils/hibp";
import {
getAllBreaches,
upsertBreaches,
updateBreachFaviconUrl,
} from "../../db/tables/breaches";
import { redisClient, REDIS_ALL_BREACHES_KEY } from "../../db/redis/client.js";
import { uploadToS3 } from "../../utils/s3.js";
const SENTRY_SLUG = "cron-sync-breaches";
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
});
const checkInId = Sentry.captureCheckIn({
monitorSlug: SENTRY_SLUG,
status: "in_progress",
});
export async function getBreachIcons(breaches: HibpGetBreachesResponse) {
// make logofolder if it doesn't exist
const logoFolder = os.tmpdir();
console.log(`Logo folder: ${logoFolder}`);
// read existing logos
const existingLogos = await readdir(logoFolder);
await Promise.allSettled(
breaches.map(async ({ Domain: breachDomain, Name: breachName }) => {
if (!breachDomain || breachDomain.length == 0) {
console.log("empty domain: ", breachName);
await updateBreachFaviconUrl(breachName, null);
return;
}
const logoFilename = breachDomain.toLowerCase() + ".ico";
if (existingLogos.includes(logoFilename)) {
console.log("skipping ", logoFilename);
await updateBreachFaviconUrl(
breachName,
`https://s3.amazonaws.com/${process.env.S3_BUCKET}/${logoFilename}`,
);
return;
}
console.log(`fetching: ${logoFilename}`);
const res = await fetch(
`https://icons.duckduckgo.com/ip3/${breachDomain}.ico`,
);
if (res.status !== 200) {
// update logo path with null
console.log(`Logo does not exist for: ${breachName} ${breachDomain}`);
await updateBreachFaviconUrl(breachName, null);
return;
}
try {
await uploadToS3(logoFilename, Buffer.from(await res.arrayBuffer()));
await updateBreachFaviconUrl(
breachName,
`https://s3.amazonaws.com/${process.env.S3_BUCKET}/${logoFilename}`,
);
} catch (e) {
console.error(e);
return;
}
}),
);
}
// Get breaches and upserts to DB
const breachesResponse = await fetchHibpBreaches();
const seen = new Set();
breachesResponse.forEach((breach) => {
seen.add(breach.Name + breach.BreachDate);
// sanity check: corrupt data structure
if (!isValidBreach(breach)) {
throw new Error(
"Breach data structure is not valid: " + JSON.stringify(breach),
);
}
});
console.log("Breaches found: ", breachesResponse.length);
console.log("Unique breaches based on Name + BreachDate", seen.size);
// sanity check: no duplicate breaches with Name + BreachDate
if (seen.size !== breachesResponse.length) {
throw new Error("Breaches contain duplicates. Stopping script...");
} else {
await upsertBreaches(breachesResponse);
// get
const result = await getAllBreaches();
console.log(
"Number of breaches in the database after upsert:",
result.length,
);
// try to refresh Redis cache of all breaches
try {
const rClient = redisClient();
await rClient.set(REDIS_ALL_BREACHES_KEY, JSON.stringify(result));
await rClient.expire(REDIS_ALL_BREACHES_KEY, 3600 * 12); // 12 hour expiration
} catch (e) {
Sentry.captureMessage(
`Update Redis failed for syncBreaches.ts: ${e as string}`,
);
console.error(
`Update Redis failed for syncBreaches.ts: ${(e as Error).stack}`,
);
}
}
await getBreachIcons(breachesResponse);
Sentry.captureCheckIn({
checkInId,
monitorSlug: SENTRY_SLUG,
status: "ok",
});
setTimeout(process.exit, 1000);
/**
* Null check for some required field
*
* @param breach breach object from HIBP
* @returns Boolean is it a valid breach
*/
function isValidBreach(breach: HibpGetBreachesResponse[number]) {
return (
breach.Name !== undefined &&
breach.BreachDate !== undefined &&
breach.Title !== undefined &&
breach.Domain !== undefined &&
breach.DataClasses !== undefined
);
}