|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Cron: Daily |
| 5 | + * From all the HIBP breaches, we parse out the new breaches that are not already present |
| 6 | + * in firefox remote settings, and update the data source accordingly |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * |
| 11 | + * |
| 12 | + * |
| 13 | + * |
| 14 | + *********************************** Warning *********************************** |
| 15 | + * |
| 16 | + * This script was in the repository unused, and referenced a module |
| 17 | + * `remote-settings.js` that no longer existed at the time of writing (it was |
| 18 | + * deleted in commit c727bfe968937e51b0cd42efefd010c7c401aeae). |
| 19 | + * I dug it up from the Git history, inlined it in this file, and made sure |
| 20 | + * it compiled, but I didn't get to actually run it. |
| 21 | + * |
| 22 | + * Thus, it could be used as a starting point when re-enabling the remote |
| 23 | + * settings upload, but shouldn't be expected to work without modifications. |
| 24 | + * |
| 25 | + *********************************** Warning *********************************** |
| 26 | + * |
| 27 | + * |
| 28 | + * |
| 29 | + * |
| 30 | + * |
| 31 | + * |
| 32 | + * |
| 33 | + * |
| 34 | + */ |
| 35 | + |
| 36 | +import AppConstants from "../../appConstants"; |
| 37 | +import * as HIBP from "../../utils/hibp"; |
| 38 | +import type { Breach } from "../../app/functions/universal/breach"; |
| 39 | + |
| 40 | +type RemoteSettingsBreach = Pick< |
| 41 | + Breach, |
| 42 | + "Name" | "Domain" | "BreachDate" | "PwnCount" | "AddedDate" | "DataClasses" |
| 43 | +>; |
| 44 | + |
| 45 | +const BREACHES_COLLECTION = "fxmonitor-breaches"; |
| 46 | +const FX_RS_COLLECTION = `${AppConstants.FX_REMOTE_SETTINGS_WRITER_SERVER}/buckets/main-workspace/collections/${BREACHES_COLLECTION}`; |
| 47 | +const FX_RS_RECORDS = `${FX_RS_COLLECTION}/records`; |
| 48 | +const FX_RS_WRITER_USER = AppConstants.FX_REMOTE_SETTINGS_WRITER_USER; |
| 49 | +const FX_RS_WRITER_PASS = AppConstants.FX_REMOTE_SETTINGS_WRITER_PASS; |
| 50 | + |
| 51 | +async function whichBreachesAreNotInRemoteSettingsYet(breaches: Breach[]) { |
| 52 | + const response = await fetch(FX_RS_RECORDS, { |
| 53 | + headers: { |
| 54 | + Authorization: `Basic ${Buffer.from(FX_RS_WRITER_USER + ":" + FX_RS_WRITER_PASS).toString("base64")}`, |
| 55 | + }, |
| 56 | + }); |
| 57 | + const fxRSRecords = await response.json(); |
| 58 | + const remoteSettingsBreachesSet = new Set( |
| 59 | + fxRSRecords.body.data.map((b: Breach) => b.Name), |
| 60 | + ); |
| 61 | + |
| 62 | + return breaches.filter(({ Name }) => !remoteSettingsBreachesSet.has(Name)); |
| 63 | +} |
| 64 | + |
| 65 | +async function postNewBreachToBreachesCollection(data: RemoteSettingsBreach) { |
| 66 | + // Create the record |
| 67 | + const response = await fetch(FX_RS_RECORDS, { |
| 68 | + method: "POST", |
| 69 | + body: JSON.stringify(data), |
| 70 | + headers: { |
| 71 | + "Content-Type": "application/json", |
| 72 | + Authorization: `Basic ${Buffer.from(FX_RS_WRITER_USER + ":" + FX_RS_WRITER_PASS).toString("base64")}`, |
| 73 | + }, |
| 74 | + }); |
| 75 | + return response.json(); |
| 76 | +} |
| 77 | + |
| 78 | +async function requestReviewOnBreachesCollection() { |
| 79 | + const response = await fetch(FX_RS_COLLECTION, { |
| 80 | + method: "PATCH", |
| 81 | + body: JSON.stringify({ data: { status: "to-review" } }), |
| 82 | + headers: { |
| 83 | + "Content-Type": "application/json", |
| 84 | + Authorization: `Basic ${Buffer.from(FX_RS_WRITER_USER + ":" + FX_RS_WRITER_PASS).toString("base64")}`, |
| 85 | + }, |
| 86 | + }); |
| 87 | + return response.json(); |
| 88 | +} |
| 89 | + |
| 90 | +if ( |
| 91 | + !AppConstants.FX_REMOTE_SETTINGS_WRITER_USER || |
| 92 | + !AppConstants.FX_REMOTE_SETTINGS_WRITER_PASS || |
| 93 | + !AppConstants.FX_REMOTE_SETTINGS_WRITER_SERVER |
| 94 | +) { |
| 95 | + console.error( |
| 96 | + "updatebreaches requires FX_REMOTE_SETTINGS_WRITER_SERVER, FX_REMOTE_SETTINGS_WRITER_USER, FX_REMOTE_SETTINGS_WRITER_PASS.", |
| 97 | + ); |
| 98 | + process.exit(1); |
| 99 | +} |
| 100 | + |
| 101 | +(async () => { |
| 102 | + const allHibpBreaches = (await HIBP.req("/breaches")) as { body: Breach[] }; |
| 103 | + const verifiedSiteBreaches = allHibpBreaches.body.filter((breach) => { |
| 104 | + return ( |
| 105 | + !breach.IsRetired && |
| 106 | + !breach.IsSpamList && |
| 107 | + !breach.IsFabricated && |
| 108 | + breach.IsVerified && |
| 109 | + breach.Domain !== "" |
| 110 | + ); |
| 111 | + }); |
| 112 | + const verifiedSiteBreachesWithPWs = verifiedSiteBreaches.filter((breach) => |
| 113 | + breach.DataClasses.includes("Passwords"), |
| 114 | + ); |
| 115 | + |
| 116 | + const newBreaches = await whichBreachesAreNotInRemoteSettingsYet( |
| 117 | + verifiedSiteBreachesWithPWs, |
| 118 | + ); |
| 119 | + |
| 120 | + if (newBreaches.length <= 0) { |
| 121 | + console.log("No new breaches detected."); |
| 122 | + process.exit(0); |
| 123 | + } |
| 124 | + |
| 125 | + console.log(`${newBreaches.length} new breach(es) found.`); |
| 126 | + |
| 127 | + for (const breach of newBreaches) { |
| 128 | + const data: RemoteSettingsBreach = { |
| 129 | + Name: breach.Name, |
| 130 | + Domain: breach.Domain, |
| 131 | + BreachDate: breach.BreachDate, |
| 132 | + PwnCount: breach.PwnCount, |
| 133 | + AddedDate: breach.AddedDate, |
| 134 | + DataClasses: breach.DataClasses, |
| 135 | + }; |
| 136 | + |
| 137 | + console.log("New breach detected: \n", data); |
| 138 | + |
| 139 | + try { |
| 140 | + await postNewBreachToBreachesCollection(data); |
| 141 | + } catch (e) { |
| 142 | + console.error(e); |
| 143 | + process.exit(1); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + console.log("Requesting review on breaches collection"); |
| 148 | + await requestReviewOnBreachesCollection(); |
| 149 | +})(); |
0 commit comments