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

mozilla / blurts-server / #13275

pending completion
#13275

push

circleci

rhelmer
MNTOR-1452 - use UPDATE FOR to lock table and prevent race condition when adding email

282 of 1679 branches covered (16.8%)

Branch coverage included in aggregate %.

3 of 3 new or added lines in 1 file covered. (100.0%)

959 of 4548 relevant lines covered (21.09%)

1.76 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/mainLayout.js'
6
import { breaches } from '../views/partials/breaches.js'
7
import { setBreachResolution, updateBreachStats } from '../db/tables/subscribers.js'
8
import { appendBreachResolutionChecklist } from '../utils/breachResolution.js'
9
import { generateToken } from '../utils/csrf.js'
10
import { getAllEmailsAndBreaches } from '../utils/breaches.js'
11
import { getCountryCode } from '../utils/countryCode.js'
12

13
async function breachesPage (req, res) {
14
  // TODO: remove: to test out getBreaches call with JSON returns
15
  const breachesData = await getAllEmailsAndBreaches(req.user, req.app.locals.breaches)
×
16
  const emailVerifiedCount = breachesData.verifiedEmails?.length ?? 0
×
17
  const emailTotalCount = emailVerifiedCount + (breachesData.unverifiedEmails?.length ?? 0)
×
18
  appendBreachResolutionChecklist(breachesData, { countryCode: getCountryCode(req) })
×
19
  const cookies = req.cookies
×
20
  const selectedEmailIndex = typeof cookies['monitor.selected-email-index'] !== 'undefined'
×
21
    ? Number.parseInt(cookies['monitor.selected-email-index'], 10)
22
    : 0
23

24
  const data = {
×
25
    breachesData,
26
    breachLogos: req.app.locals.breachLogoMap,
27
    emailVerifiedCount,
28
    emailTotalCount,
29
    selectedEmailIndex,
30
    partial: breaches,
31
    csrfToken: generateToken(res),
32
    fxaProfile: req.user.fxa_profile_json
33
  }
34

35
  res.send(mainLayout(data))
×
36
}
37

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

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

72
  // check if current user's emails array contain affectedEmail
73
  if (!affectedEmailAsSubscriber && !affectedEmailInEmailAddresses) {
×
74
    return res.json('Error: affectedEmail is not valid for this subscriber')
×
75
  }
76

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

91
  // check if resolutionsChecked array is a subset of the breaches' datatypes
92
  const isSubset = resolutionsChecked.every(val => currentBreaches[0].DataClasses.includes(val))
×
93
  if (!isSubset) {
×
94
    return res.json(`Error: the resolutionChecked param contains more than allowed data types: ${resolutionsChecked}`)
×
95
  }
96

97
  /* new JsonB:
98
  {
99
    email_id: {
100
      recency_index: {
101
        resolutions: ['email', ...],
102
        isResolved: true
103
      }
104
    }
105
  }
106
  */
107

108
  const currentBreachDataTypes = currentBreaches[0].DataClasses // get this from existing breaches
×
109
  const currentBreachResolution = req.user.breach_resolution || {} // get this from existing breach resolution if available
×
110
  const isResolved = resolutionsChecked.length === currentBreachDataTypes.length
×
111
  currentBreachResolution[affectedEmail] = {
×
112
    ...(currentBreachResolution[affectedEmail] || {}),
×
113
    ...{
114
      [breachIdNumber]: {
115
        resolutionsChecked,
116
        isResolved
117
      }
118
    }
119
  }
120

121
  // set useBreachId to mark latest version of breach resolution
122
  // without this line, the get call might assume recency index
123
  currentBreachResolution.useBreachId = true
×
124

125
  const updatedSubscriber = await setBreachResolution(sessionUser, currentBreachResolution)
×
126

127
  req.session.user = updatedSubscriber
×
128

129
  const userBreachStats = breachStatsV1(verifiedEmails)
×
130

131
  await updateBreachStats(sessionUser.id, userBreachStats)
×
132

133
  res.json(updatedSubscriber.breach_resolution)
×
134
}
135

136
// PRIVATE
137

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

176
  // combine the breaches for each account, breach duplicates are ok
177
  // since the user may have multiple accounts with different emails
178
  verifiedEmails.forEach(email => {
×
179
    email.breaches.forEach(breach => {
×
180
      if (breach.IsResolved) {
×
181
        breachStats.numBreaches.numResolved++
×
182
      }
183

184
      const dataClasses = breach.DataClasses
×
185
      if (dataClasses.includes('passwords')) {
×
186
        breachStats.passwords.count++
×
187
        if (breach.IsResolved) {
×
188
          breachStats.passwords.numResolved++
×
189
        }
190
      }
191
    })
192
    foundBreaches = [...foundBreaches, ...email.breaches]
×
193
  })
194

195
  // total number of verified emails being monitored
196
  breachStats.monitoredEmails.count = verifiedEmails.length
×
197

198
  // total number of breaches across all emails
199
  breachStats.numBreaches.count = foundBreaches.length
×
200

201
  breachStats.numBreaches.numUnresolved = breachStats.numBreaches.count - breachStats.numBreaches.numResolved
×
202

203
  return breachStats
×
204
}
205

206
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