-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathexclude-paths.mts
More file actions
197 lines (180 loc) · 6.39 KB
/
Copy pathexclude-paths.mts
File metadata and controls
197 lines (180 loc) · 6.39 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
import path from 'node:path'
import { normalizePath } from '@socketsecurity/lib-stable/paths/normalize'
import { InputError } from '../../util/error/errors.mts'
import type { ReachabilityConfig } from './perform-reachability-analysis.mts'
import type { SocketYml } from '../../util/socket-yaml.mts'
export type ApplyFullExcludePathsResult = {
effectiveSocketConfig: SocketYml | undefined
mergedReachabilityOptions: ReachabilityConfig
}
/**
* Applies --exclude-paths consistently to SCA manifest discovery and Coana. SCA
* exclusion always applies when paths are provided. The reachability options
* are merged unconditionally; callers decide whether to actually run
* reachability and consume them.
*/
export function applyFullExcludePaths(
cwd: string,
reachabilityOptions: ReachabilityConfig,
socketConfig: SocketYml | undefined,
target: string,
): ApplyFullExcludePathsResult {
const { excludePaths } = reachabilityOptions
const scaExcludeGlobs = excludePaths.map(excludePathToProjectIgnorePath)
const coanaExcludeGlobs = projectIgnorePathsToReachExcludePaths(
scaExcludeGlobs,
{
cwd,
target,
},
)
const socketConfigReachExcludeGlobs = excludePaths.length
? projectIgnorePathsToReachExcludePaths(socketConfig?.projectIgnorePaths, {
cwd,
target,
})
: []
const effectiveSocketConfig = scaExcludeGlobs.length
? {
...socketConfig,
version: socketConfig?.version ?? 2,
issueRules: socketConfig?.issueRules ?? {},
githubApp: socketConfig?.githubApp ?? {},
projectIgnorePaths: [
...(socketConfig?.projectIgnorePaths ?? []),
...scaExcludeGlobs,
],
}
: socketConfig
const mergedReachabilityOptions = excludePaths.length
? {
...reachabilityOptions,
reachExcludePaths: [
...socketConfigReachExcludeGlobs,
...reachabilityOptions.reachExcludePaths,
...coanaExcludeGlobs,
],
}
: reachabilityOptions
return { effectiveSocketConfig, mergedReachabilityOptions }
}
/**
* Rejects gitignore-style negation patterns for --exclude-paths because the
* flag is a positive full-exclusion list, not a complete ignore language.
*/
export function assertNoNegationPatterns(paths: readonly string[]): void {
for (let i = 0, { length } = paths; i < length; i += 1) {
const excludePath = paths[i]!
if (excludePath.startsWith('!')) {
throw new InputError(
`--exclude-paths does not support negation patterns. Got: '${excludePath}'.`,
)
}
}
}
/**
* Converts a user-facing full-scan exclude path into the socket.yml
* projectIgnorePaths shape used by SCA manifest discovery.
*/
export function excludePathToProjectIgnorePath(excludePath: string): string {
const stripped = stripTrailingSlash(excludePath)
return stripped.endsWith('/**') ? stripped : `${stripped}/**`
}
export function expandReachExcludePath(reachExcludePath: string): string[] {
if (reachExcludePath === '**') {
return ['**']
}
const firstSlash = reachExcludePath.indexOf('/')
const prefix =
firstSlash === -1 || firstSlash === reachExcludePath.length - 1 ? '**/' : ''
const normalized = stripTrailingSlash(
normalizePath(reachExcludePath).startsWith('/')
? reachExcludePath.slice(1)
: reachExcludePath,
)
const pattern = `${prefix}${normalized}`
return pattern.endsWith('/*') || pattern.endsWith('/**')
? [pattern]
: [pattern, `${pattern}/**`]
}
export function normalizeProjectIgnorePath(ignorePath: string): string {
return stripTrailingSlash(
toPosixPath(
normalizePath(ignorePath).startsWith('/')
? ignorePath.slice(1)
: ignorePath,
),
)
}
export function pathRelativeToTarget(
ignorePath: string,
target: string,
): string | undefined {
const normalized = normalizeProjectIgnorePath(ignorePath)
if (target === '' || target === '.') {
return normalized
}
// Ignore paths outside the analysis target. They still affect SCA manifest
// discovery through projectIgnorePaths, but Coana cannot exclude directories
// outside the target it is analyzing.
if (normalized === target) {
return '**'
}
const targetPrefix = `${target}/`
if (normalized.startsWith(targetPrefix)) {
return normalized.slice(targetPrefix.length)
}
/* c8 ignore start - unreachable: recursiveTargetPrefix = `${targetPrefix}**\/` so any startsWith(recursiveTargetPrefix) match would have been caught by the startsWith(targetPrefix) check above. */
const recursiveTargetPrefix = `${targetPrefix}**/`
if (normalized.startsWith(recursiveTargetPrefix)) {
return normalized.slice(targetPrefix.length)
}
/* c8 ignore stop */
return undefined
}
/**
* Translates project-root projectIgnorePaths into Coana --exclude-dirs values,
* which are interpreted relative to the current reachability analysis target.
*/
export function projectIgnorePathsToReachExcludePaths(
paths: readonly string[] | undefined,
config: { cwd: string; target: string },
): string[] {
// GitHub App-style projectIgnorePaths support negation. Coana's
// --exclude-dirs does not, so keep the existing Coana behavior and let it
// infer config ignores itself when any negation is present.
const cfg = { __proto__: null, ...config } as typeof config
if (
!Array.isArray(paths) ||
paths.some(ignorePath => ignorePath.includes('!'))
) {
return []
}
// projectIgnorePaths are rooted at the project cwd. Coana receives excludes
// relative to its analysis target, so nested target scans need translation.
const targetPath = path.isAbsolute(cfg.target)
? path.relative(cfg.cwd, cfg.target)
: cfg.target
const targetPattern = toPosixPath(stripTrailingSlash(targetPath))
return paths.flatMap(ignorePath =>
projectIgnorePathToReachExcludePaths(ignorePath, targetPattern),
)
}
export function projectIgnorePathToReachExcludePaths(
ignorePath: string,
targetPattern: string,
): string[] {
const reachPath = pathRelativeToTarget(ignorePath, targetPattern)
if (!reachPath) {
return []
}
return expandReachExcludePath(reachPath)
}
export function stripTrailingSlash(value: string): string {
// normalizePath collapses separators AND drops any trailing slash, so the
// normalized form IS the stripped form (root '/' stays '/').
return value.length > 1 ? normalizePath(value) : value
}
export function toPosixPath(value: string): string {
return value.replaceAll('\\', '/')
}