• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / blurts-server / #12632

pending completion
#12632

push

circleci

web-flow
Merge pull request #2854 from mozilla/MNTOR-741

MNTOR-741

282 of 1416 branches covered (19.92%)

Branch coverage included in aggregate %.

107 of 107 new or added lines in 9 files covered. (100.0%)

959 of 3912 relevant lines covered (24.51%)

2.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/controllers/breaches.js
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4

5
import { mainLayout } from '../views/main.js'
6
import { breaches } from '../views/partials/breaches.js'
7
import { setBreachResolution, updateBreachStats } from '../db/tables/subscribers.js'
8
import { appendBreachResolutionChecklist } from '../utils/breach-resolution.js'
9
import { generateToken } from '../utils/csrf.js'
10
import { getAllEmailsAndBreaches } from '../utils/breaches.js'
11

12
async function breachesPage (req, res) {
13
  // TODO: remove: to test out getBreaches call with JSON returns
14
  const breachesData = await getAllEmailsAndBreaches(req.user, req.app.locals.breaches)
×
15
  const emailVerifiedCount = breachesData.verifiedEmails?.length ?? 0
×
16
  const emailTotalCount = emailVerifiedCount + (breachesData.unverifiedEmails?.length ?? 0)
×
17
  appendBreachResolutionChecklist(breachesData)
×
18

19
  const data = {
×
20
    breachesData,
21
    emailVerifiedCount,
22
    emailTotalCount,
23
    partial: breaches,
24
    csrfToken: generateToken(res),
25
    fxaProfile: req.user.fxa_profile_json,
26
    nonce: res.locals.nonce
27
  }
28

29
  res.send(mainLayout(data))
×
30
}
31

32
/**
33
 * Get breaches from the database and return a JSON object
34
 * TODO: Takes in additional query parameters:
35
 *
36
 * status: enum (resolved, unresolved)
37
 * email: string
38
 *
39
 * @param {object} req
40
 * @param {object} res
41
 */
42
async function getBreaches (req, res) {
43
  const allBreaches = req.app.locals.breaches
×
44
  const sessionUser = req.user
×
45
  const resp = await getAllEmailsAndBreaches(sessionUser, allBreaches)
×
46
  return res.json(resp)
×
47
}
48

49
/**
50
 * Modify breach resolution for a user
51
 *
52
 * @param {object} req containing {user, body: {affectedEmail, breachId, resolutionsChecked}}
53
 *
54
 * breachId: id of the breach in the `breaches` table
55
 *
56
 * resolutionsChecked: has the following structure [DataTypes]
57
 * @param {object} res JSON object containing the updated breach resolution
58
 */
59
async function putBreachResolution (req, res) {
60
  const sessionUser = req.user
×
61
  const { affectedEmail, breachId, resolutionsChecked } = req.body
×
62
  const breachIdNumber = Number(breachId)
×
63
  const affectedEmailAsSubscriber = sessionUser.primary_email === affectedEmail ? sessionUser.primary_email : false
×
64
  const affectedEmailInEmailAddresses = sessionUser.email_addresses.find(ea => ea.email === affectedEmail)?.email || false
×
65

66
  // check if current user's emails array contain affectedEmail
67
  if (!affectedEmailAsSubscriber && !affectedEmailInEmailAddresses) {
×
68
    return res.json('Error: affectedEmail is not valid for this subscriber')
×
69
  }
70

71
  // check if breach id is a part of affectEmail's breaches
72
  const allBreaches = req.app.locals.breaches
×
73
  const { verifiedEmails } = await getAllEmailsAndBreaches(req.session.user, allBreaches)
×
74
  let currentEmail
75
  if (affectedEmailAsSubscriber) {
×
76
    currentEmail = verifiedEmails.find(ve => ve.email === affectedEmailAsSubscriber)
×
77
  } else {
78
    currentEmail = verifiedEmails.find(ve => ve.email === affectedEmailInEmailAddresses)
×
79
  }
80
  const currentBreaches = currentEmail?.breaches?.filter(b => b.Id === breachIdNumber)
×
81
  if (!currentBreaches) {
×
82
    return res.json('Error: breachId provided does not exist')
×
83
  }
84

85
  // check if resolutionsChecked array is a subset of the breaches' datatypes
86
  const isSubset = resolutionsChecked.every(val => currentBreaches[0].DataClasses.includes(val))
×
87
  if (!isSubset) {
×
88
    return res.json(`Error: the resolutionChecked param contains more than allowed data types: ${resolutionsChecked}`)
×
89
  }
90

91
  /* new JsonB:
92
  {
93
    email_id: {
94
      recency_index: {
95
        resolutions: ['email', ...],
96
        isResolved: true
97
      }
98
    }
99
  }
100
  */
101

102
  const currentBreachDataTypes = currentBreaches[0].DataClasses // get this from existing breaches
×
103
  const currentBreachResolution = req.user.breach_resolution || {} // get this from existing breach resolution if available
×
104
  const isResolved = resolutionsChecked.length === currentBreachDataTypes.length
×
105
  currentBreachResolution[affectedEmail] = {
×
106
    ...(currentBreachResolution[affectedEmail] || {}),
×
107
    ...{
108
      [breachIdNumber]: {
109
        resolutionsChecked,
110
        isResolved
111
      }
112
    }
113
  }
114

115
  // set useBreachId to mark latest version of breach resolution
116
  // without this line, the get call might assume recency index
117
  currentBreachResolution.useBreachId = true
×
118

119
  const updatedSubscriber = await setBreachResolution(sessionUser, currentBreachResolution)
×
120

121
  req.session.user = updatedSubscriber
×
122

123
  const userBreachStats = breachStatsV1(verifiedEmails)
×
124

125
  await updateBreachStats(sessionUser.id, userBreachStats)
×
126

127
  res.json(updatedSubscriber.breach_resolution)
×
128
}
129

130
// PRIVATE
131

132
/**
133
 * TODO: DEPRECATE
134
 * This utiliy function is maintained to keep backwards compatibility with V1.
135
 * After v2 is launched, we will deprecate this function
136
 *
137
 * @param {object} verifiedEmails [{breaches: [isResolved: true/false, dataClasses: []]}]
138
 * @returns {object} breachStats
139
 * {
140
 *    monitoredEmails: {
141
      count: 0
142
    },
143
    numBreaches: {
144
      count: 0,
145
      numResolved: 0
146
      numUnresolved: 0
147
    },
148
    passwords: {
149
      count: 0,
150
      numResolved: 0
151
    }
152
  }
153
 */
154
function breachStatsV1 (verifiedEmails) {
155
  const breachStats = {
×
156
    monitoredEmails: {
157
      count: 0
158
    },
159
    numBreaches: {
160
      count: 0,
161
      numResolved: 0
162
    },
163
    passwords: {
164
      count: 0,
165
      numResolved: 0
166
    }
167
  }
168
  let foundBreaches = []
×
169

170
  // combine the breaches for each account, breach duplicates are ok
171
  // since the user may have multiple accounts with different emails
172
  verifiedEmails.forEach(email => {
×
173
    email.breaches.forEach(breach => {
×
174
      if (breach.IsResolved) {
×
175
        breachStats.numBreaches.numResolved++
×
176
      }
177

178
      const dataClasses = breach.DataClasses
×
179
      if (dataClasses.includes('passwords')) {
×
180
        breachStats.passwords.count++
×
181
        if (breach.IsResolved) {
×
182
          breachStats.passwords.numResolved++
×
183
        }
184
      }
185
    })
186
    foundBreaches = [...foundBreaches, ...email.breaches]
×
187
  })
188

189
  // total number of verified emails being monitored
190
  breachStats.monitoredEmails.count = verifiedEmails.length
×
191

192
  // total number of breaches across all emails
193
  breachStats.numBreaches.count = foundBreaches.length
×
194

195
  breachStats.numBreaches.numUnresolved = breachStats.numBreaches.count - breachStats.numBreaches.numResolved
×
196

197
  return breachStats
×
198
}
199

200
export { breachesPage, putBreachResolution, getBreaches }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc