-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathrules.ts
More file actions
262 lines (245 loc) · 7.77 KB
/
rules.ts
File metadata and controls
262 lines (245 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { isObject } from '@socketsecurity/registry/lib/objects'
import { findSocketYmlSync, getConfigValue } from '../config'
import { isErrnoException } from '../errors'
import { getPublicToken, setupSdk } from '../sdk'
import type { SocketSdkResultType } from '@socketsecurity/sdk'
type AlertUxLookup = ReturnType<typeof createAlertUXLookup>
type AlertUxLookupSettings = Parameters<AlertUxLookup>[0]
type AlertUxLookupResult = ReturnType<AlertUxLookup>
type NonNormalizedRule =
| NonNullable<
NonNullable<
NonNullable<
(SocketSdkResultType<'postSettings'> & {
success: true
})['data']['entries'][number]['settings'][string]
>['issueRules']
>
>[string]
| boolean
type NonNormalizedResolvedRule =
| (NonNullable<
NonNullable<
(SocketSdkResultType<'postSettings'> & {
success: true
})['data']['defaults']['issueRules']
>[string]
> & { action: string })
| boolean
type RuleActionUX = { block: boolean; display: boolean }
const ERROR_UX: RuleActionUX = {
block: true,
display: true
}
const IGNORE_UX: RuleActionUX = {
block: false,
display: false
}
const WARN_UX: RuleActionUX = {
block: false,
display: true
}
// Iterates over all entries with ordered issue rule for deferral. Iterates over
// all issue rules and finds the first defined value that does not defer otherwise
// uses the defaultValue. Takes the value and converts into a UX workflow.
function resolveAlertRuleUX(
orderedRulesCollection: Iterable<Iterable<NonNormalizedRule>>,
defaultValue: NonNormalizedResolvedRule
): RuleActionUX {
if (
defaultValue === true ||
defaultValue === null ||
defaultValue === undefined
) {
defaultValue = { action: 'error' }
} else if (defaultValue === false) {
defaultValue = { action: 'ignore' }
}
let block = false
let display = false
let needDefault = true
iterate_entries: for (const rules of orderedRulesCollection) {
for (const rule of rules) {
if (ruleValueDoesNotDefer(rule)) {
needDefault = false
const narrowingFilter = uxForDefinedNonDeferValue(rule)
block = block || narrowingFilter.block
display = display || narrowingFilter.display
continue iterate_entries
}
}
const narrowingFilter = uxForDefinedNonDeferValue(defaultValue)
block = block || narrowingFilter.block
display = display || narrowingFilter.display
}
if (needDefault) {
const narrowingFilter = uxForDefinedNonDeferValue(defaultValue)
block = block || narrowingFilter.block
display = display || narrowingFilter.display
}
return { block, display }
}
// Negative form because it is narrowing the type.
function ruleValueDoesNotDefer(
rule: NonNormalizedRule
): rule is NonNormalizedResolvedRule {
if (rule === undefined) {
return false
}
if (isObject(rule)) {
const { action } = rule
if (action === undefined || action === 'defer') {
return false
}
}
return true
}
// Handles booleans for backwards compatibility.
function uxForDefinedNonDeferValue(
ruleValue: NonNormalizedResolvedRule
): RuleActionUX {
if (typeof ruleValue === 'boolean') {
return ruleValue ? ERROR_UX : IGNORE_UX
}
const { action } = ruleValue
if (action === 'warn') {
return WARN_UX
} else if (action === 'ignore') {
return IGNORE_UX
}
return ERROR_UX
}
type SettingsType = (SocketSdkResultType<'postSettings'> & {
success: true
})['data']
export function createAlertUXLookup(settings: SettingsType): (context: {
package: { name: string; version: string }
alert: { type: string }
}) => RuleActionUX {
const cachedUX: Map<keyof typeof settings.defaults.issueRules, RuleActionUX> =
new Map()
return context => {
const { type } = context.alert
let ux = cachedUX.get(type)
if (ux) {
return ux
}
const orderedRulesCollection: NonNormalizedRule[][] = []
for (const settingsEntry of settings.entries) {
const orderedRules: NonNormalizedRule[] = []
let target = settingsEntry.start
while (target !== null) {
const resolvedTarget = settingsEntry.settings[target]
if (!resolvedTarget) {
break
}
const issueRuleValue = resolvedTarget.issueRules?.[type]
if (typeof issueRuleValue !== 'undefined') {
orderedRules.push(issueRuleValue)
}
target = resolvedTarget.deferTo ?? null
}
orderedRulesCollection.push(orderedRules)
}
const defaultValue = settings.defaults.issueRules[type] as
| { action: 'error' | 'ignore' | 'warn' }
| boolean
| undefined
let resolvedDefaultValue: NonNormalizedResolvedRule = {
action: 'error'
}
if (defaultValue === false) {
resolvedDefaultValue = { action: 'ignore' }
} else if (defaultValue && defaultValue !== true) {
resolvedDefaultValue = { action: defaultValue.action ?? 'error' }
}
ux = resolveAlertRuleUX(orderedRulesCollection, resolvedDefaultValue)
cachedUX.set(type, ux)
return ux
}
}
let _uxLookup: AlertUxLookup | undefined
export async function uxLookup(
settings: AlertUxLookupSettings
): Promise<AlertUxLookupResult> {
if (_uxLookup === undefined) {
const { orgs, settings } = await (async () => {
try {
const sockSdk = await setupSdk(getPublicToken())
const orgResult = await sockSdk.getOrganizations()
if (!orgResult.success) {
if (orgResult.status === 429) {
throw new Error(`API token quota exceeded: ${orgResult.error}`)
}
throw new Error(
`Failed to fetch Socket organization info: ${orgResult.error}`
)
}
const { organizations } = orgResult.data
const orgs: Array<Exclude<(typeof organizations)[string], undefined>> =
[]
for (const org of Object.values(organizations)) {
if (org) {
orgs.push(org)
}
}
const settingsResult = await sockSdk.postSettings(
orgs.map(org => ({ organization: org.id }))
)
if (!settingsResult.success) {
throw new Error(
`Failed to fetch API key settings: ${settingsResult.error}`
)
}
return {
orgs,
settings: settingsResult.data
}
} catch (e) {
const cause = isObject(e) && 'cause' in e ? e['cause'] : undefined
if (
isErrnoException(cause) &&
(cause.code === 'ENOTFOUND' || cause.code === 'ECONNREFUSED')
) {
throw new Error(
'Unable to connect to socket.dev, ensure internet connectivity before retrying',
{
cause: e
}
)
}
throw e
}
})()
// Remove any organizations not being enforced.
const enforcedOrgs = getConfigValue('enforcedOrgs') ?? []
for (const { 0: i, 1: org } of orgs.entries()) {
if (!enforcedOrgs.includes(org.id)) {
settings.entries.splice(i, 1)
}
}
const socketYml = findSocketYmlSync()
if (socketYml) {
settings.entries.push({
start: socketYml.path,
settings: {
[socketYml.path]: {
deferTo: null,
// TODO: TypeScript complains about the type not matching. We should
// figure out why are providing
// issueRules: { [issueName: string]: boolean }
// but expecting
// issueRules: { [issueName: string]: { action: 'defer' | 'error' | 'ignore' | 'monitor' | 'warn' } }
issueRules: socketYml.parsed.issueRules as unknown as {
[key: string]: {
action: 'defer' | 'error' | 'ignore' | 'monitor' | 'warn'
}
}
}
}
})
}
_uxLookup = createAlertUXLookup(settings)
}
return _uxLookup(settings)
}