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

cameri / nostream / 45253673870

23 Nov 2026 07:53PM UTC coverage: 79.82% (+0.04%) from 79.779%
45253673870

Pull #1234

github

web-flow
Merge 41d155f77 into dcaba6971
Pull Request #1234: fix(admin): record a logout in the audit history only when the request has a valid session

5950 of 8070 branches covered (73.73%)

Branch coverage included in aggregate %.

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

1 existing line in 1 file now uncovered.

12229 of 14705 relevant lines covered (83.16%)

656.1 hits per line

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

98.82
/src/controllers/admin/patch-settings-controller.ts
1
import { Request, Response } from 'express'
2

3
import { Settings } from '../../@types/settings'
4
import { IController } from '../../@types/controllers'
5
import { adminSettingsPatchBodySchema } from '../../schemas/admin-settings-schema'
16 ✔
6
import {
16 ✔
7
  appendSettingsAuditLog,
8
  getByPath,
9
  loadMergedSettings,
10
  loadUserSettings,
11
  saveSettings,
12
  setByPath,
13
  validatePathAgainstDefaults,
14
  validateSettings,
15
  withSettingsWriteLock,
16
} from '../../utils/settings-config'
17
import {
16 ✔
18
  changedWriteProtectedSettingsPaths,
19
  isWriteProtectedSettingsPath,
20
  redactSettingsValue,
21
} from '../../utils/settings-redaction'
22
import { validateSchema } from '../../utils/validation'
16 ✔
23
import { INotificationOutboxRepository } from '../../@types/repositories'
24
import { OperatorNotificationEventType } from '../../@types/operator-notifications'
16 ✔
25
import { redactSettingsSecrets } from '../../utils/settings-redaction'
16 ✔
26
import { createLogger } from '../../factories/logger-factory'
16 ✔
27
import { recordAdminAuditFromRequest } from '../../utils/record-admin-audit'
16 ✔
28

29
const logger = createLogger('patch-admin-settings-controller')
16 ✔
30

31
export class PatchAdminSettingsController implements IController {
16 ✔
32
  public constructor(private readonly notificationOutboxRepository: INotificationOutboxRepository) {}
18 ✔
33

34
  public async handleRequest(request: Request, response: Response): Promise<void> {
35
    const validation = validateSchema(adminSettingsPatchBodySchema)(request.body)
18 ✔
36
    if (validation.error) {
18 ✔
37
      response.status(400).setHeader('content-type', 'application/json').send({ error: 'Invalid request' })
1 ✔
38
      return
1 ✔
39
    }
40

41
    const changes = 'changes' in validation.value ? validation.value.changes : [validation.value]
17 ✔
42
    const issues = changes.flatMap(({ path }) => {
17 ✔
43
      if (isWriteProtectedSettingsPath(path)) {
19 ✔
44
        return [{ path, message: 'Path is write-protected' }]
2 ✔
45
      }
46

47
      return validatePathAgainstDefaults(path)
17 ✔
48
    })
49

50
    if (issues.length > 0) {
17 ✔
51
      response.status(400).setHeader('content-type', 'application/json').send({ error: 'Validation failed', issues })
3 ✔
52
      return
3 ✔
53
    }
54

55
    // Every writer of settings.yaml takes this lock. Reading inside it is what
56
    // stops an ordinary settings save from writing a document read before a
57
    // moderation ban was applied, which would silently undo the ban.
58
    const outcome = await withSettingsWriteLock<
14 ✔
59
      | { ok: true; nextUserSettings: Record<string, unknown> }
60
      | { ok: false; issues: Array<{ path: string; message: string }> }
61
    >(() => {
62
      const userSettings = loadUserSettings() as unknown as Record<string, unknown>
14 ✔
63
      // Taken before the changes are applied, in case applying them edits this object in place.
64
      const protectedBefore = JSON.parse(JSON.stringify(userSettings)) as Record<string, unknown>
14 ✔
65
      const nextUserSettings = changes.reduce(
14 ✔
66
        (settings, change) => setByPath(settings, change.path, change.value),
16 ✔
67
        userSettings,
68
      )
69

70
      // The paths of the request were checked above, but a change to a path that holds a protected value, such as
71
      // replacing `admin`, writes that value too. Compare what would be saved with what is stored.
72
      const changedProtected = changedWriteProtectedSettingsPaths(protectedBefore, nextUserSettings)
14 ✔
73
      if (changedProtected.length > 0) {
14 ✔
74
        return {
4 ✔
75
          ok: false as const,
76
          issues: changedProtected.map((path) => ({ path, message: 'Path is write-protected' })),
4 ✔
77
        }
78
      }
79

80
      const merged = loadMergedSettings() as unknown as Record<string, unknown>
10 ✔
81
      const mergedNext = changes.reduce(
10 ✔
82
        (settings, change) => setByPath(settings, change.path, getByPath(nextUserSettings, change.path)),
11 ✔
83
        merged,
84
      )
85
      const validationIssues = validateSettings(mergedNext as unknown as Settings)
10 ✔
86

87
      if (validationIssues.length > 0) {
10 ✔
88
        return { ok: false as const, issues: validationIssues }
1 ✔
89
      }
90

91
      saveSettings(nextUserSettings as unknown as Settings)
9 ✔
92

93
      return { ok: true as const, nextUserSettings }
9 ✔
94
    })
95

96
    if ('issues' in outcome) {
14 ✔
97
      response
5 ✔
98
        .status(400)
99
        .setHeader('content-type', 'application/json')
100
        .send({ error: 'Validation failed', issues: outcome.issues })
101
      return
5 ✔
102
    }
103

104
    const nextUserSettings = outcome.nextUserSettings
9 ✔
105
    const updatedChanges = changes.map(({ path }) => ({
10 ✔
106
      path,
107
      value: redactSettingsValue(path, getByPath(nextUserSettings, path)),
108
      reload: getSettingsReloadBehavior(path),
109
    }))
110
    appendSettingsAuditLog({
9 ✔
111
      action: 'settings.updated',
112
      changes: updatedChanges.map(({ path, reload }) => ({ path, reload })),
10 ✔
113
      remoteAddress: request.ip,
114
    })
115
    await recordAdminAuditFromRequest('settings.updated', request, {
9 ✔
116
      changes: updatedChanges.map(({ path, reload }) => ({ path, reload })),
10 ✔
117
    })
118

119
    try {
9 ✔
120
      await this.notificationOutboxRepository.enqueue(OperatorNotificationEventType.SETTINGS_CHANGED, {
9 ✔
121
        changes: redactSettingsSecrets(
122
          updatedChanges.map(({ path, value, reload }) => ({ path, value, reload })),
10 ✔
123
        ),
124
        remoteAddress: request.ip,
125
      })
126
    } catch (error) {
UNCOV
127
      logger.error('Unable to enqueue settings notification outbox event', error)
×
128
    }
129

130
    if (changes.length === 1 && !('changes' in validation.value)) {
9 ✔
131
      const [change] = updatedChanges
8 ✔
132
      response.status(200).setHeader('content-type', 'application/json').send({ ok: true, ...change })
8 ✔
133
      return
8 ✔
134
    }
135

136
    response.status(200).setHeader('content-type', 'application/json').send({ ok: true, changes: updatedChanges })
1 ✔
137
  }
138
}
139

140
const getSettingsReloadBehavior = (path: string): 'hot-reload' | 'restart-required' => {
16 ✔
141
  if (path.startsWith('workers.') || path.startsWith('payments') || path.startsWith('network.')) {
10 ✔
142
    return 'restart-required'
6 ✔
143
  }
144

145
  return 'hot-reload'
4 ✔
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc