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

mozilla / blurts-server / 1ed11915-f823-4661-a126-c43f39568341

pending completion
1ed11915-f823-4661-a126-c43f39568341

push

circleci

GitHub
Merge pull request #2768 from mozilla/MNTOR-978-2

282 of 1227 branches covered (22.98%)

Branch coverage included in aggregate %.

10 of 10 new or added lines in 2 files covered. (100.0%)

959 of 3295 relevant lines covered (29.1%)

4.72 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
  const emailCount = 1 + (req.user.email_addresses?.length || 0) // +1 because user.email_addresses does not include primary
×
14
  // TODO: remove: to test out getBreaches call with JSON returns
15
  const breachesData = await getAllEmailsAndBreaches(req.user, req.app.locals.breaches)
×
16
  appendBreachResolutionChecklist(breachesData)
×
17

18
  const data = {
×
19
    breachesData,
20
    emailCount,
21
    partial: breaches,
22
    csrfToken: generateToken(res),
23
    fxaProfile: req.user.fxa_profile_json
24
  }
25

26
  res.send(mainLayout(data))
×
27
}
28

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

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

62
  // check if current user's emails array contain affectedEmail
63
  if (!affectedEmailIsSubscriberRecord && !affectedEmailInEmailAddresses) {
×
64
    return res.json('Error: affectedEmail is not valid for this subscriber')
×
65
  }
66

67
  // check if recency index is a part of affectEmail's breaches
68
  const allBreaches = req.app.locals.breaches
×
69
  const { verifiedEmails } = await getAllEmailsAndBreaches(req.session.user, allBreaches)
×
70
  const currentEmail = verifiedEmails.find(ve => ve.email === affectedEmailInEmailAddresses[0].email)
×
71
  const currentBreaches = currentEmail.breaches?.filter(b => b.Id === breachIdNumber)
×
72
  if (!currentBreaches) {
×
73
    return res.json('Error: breachId provided does not exist')
×
74
  }
75

76
  // check if resolutionsChecked array is a subset of the breaches' datatypes
77
  const isSubset = resolutionsChecked.every(val => currentBreaches[0].DataClasses.includes(val))
×
78
  if (!isSubset) {
×
79
    return res.json(`Error: the resolutionChecked param contains more than allowed data types: ${resolutionsChecked}`)
×
80
  }
81

82
  /* new JsonB:
83
  {
84
    email_id: {
85
      recency_index: {
86
        resolutions: ['email', ...],
87
        isResolved: true
88
      }
89
    }
90
  }
91
  */
92

93
  const currentBreachDataTypes = currentBreaches[0].DataClasses // get this from existing breaches
×
94
  const currentBreachResolution = req.user.breach_resolution || {} // get this from existing breach resolution if available
×
95
  const isResolved = resolutionsChecked.length === currentBreachDataTypes.length
×
96
  currentBreachResolution[affectedEmail] = {
×
97
    ...(currentBreachResolution[affectedEmail] || {}),
×
98
    ...{
99
      [breachIdNumber]: {
100
        resolutionsChecked,
101
        isResolved
102
      }
103
    }
104
  }
105

106
  const updatedSubscriber = await setBreachResolution(sessionUser, currentBreachResolution)
×
107

108
  req.session.user = updatedSubscriber
×
109

110
  const userBreachStats = breachStatsV1(verifiedEmails)
×
111

112
  await updateBreachStats(sessionUser.id, userBreachStats)
×
113

114
  res.json(updatedSubscriber.breach_resolution)
×
115
}
116

117
// PRIVATE
118

119
/**
120
 * TODO: DEPRECATE
121
 * This utiliy function is maintained to keep backwards compatibility with V1.
122
 * After v2 is launched, we will deprecate this function
123
 * @param {object} verifiedEmails [{breaches: [isResolved: true/false, dataClasses: []]}]
124
 * @returns {object} breachStats
125
 * {
126
 *    monitoredEmails: {
127
      count: 0
128
    },
129
    numBreaches: {
130
      count: 0,
131
      numResolved: 0
132
      numUnresolved: 0
133
    },
134
    passwords: {
135
      count: 0,
136
      numResolved: 0
137
    }
138
  }
139
 */
140
function breachStatsV1 (verifiedEmails) {
141
  const breachStats = {
×
142
    monitoredEmails: {
143
      count: 0
144
    },
145
    numBreaches: {
146
      count: 0,
147
      numResolved: 0
148
    },
149
    passwords: {
150
      count: 0,
151
      numResolved: 0
152
    }
153
  }
154
  let foundBreaches = []
×
155

156
  // combine the breaches for each account, breach duplicates are ok
157
  // since the user may have multiple accounts with different emails
158
  verifiedEmails.forEach(email => {
×
159
    email.breaches.forEach(breach => {
×
160
      if (breach.IsResolved) {
×
161
        breachStats.numBreaches.numResolved++
×
162
      }
163

164
      const dataClasses = breach.DataClasses
×
165
      if (dataClasses.includes('passwords')) {
×
166
        breachStats.passwords.count++
×
167
        if (breach.IsResolved) {
×
168
          breachStats.passwords.numResolved++
×
169
        }
170
      }
171
    })
172
    foundBreaches = [...foundBreaches, ...email.breaches]
×
173
  })
174

175
  // total number of verified emails being monitored
176
  breachStats.monitoredEmails.count = verifiedEmails.length
×
177

178
  // total number of breaches across all emails
179
  breachStats.numBreaches.count = foundBreaches.length
×
180

181
  breachStats.numBreaches.numUnresolved = breachStats.numBreaches.count - breachStats.numBreaches.numResolved
×
182

183
  return breachStats
×
184
}
185

186
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