Skip to content

Commit c3a7d8b

Browse files
committed
Use isReportSupportedFile instead of SUPPORTED_FILE_PATTERNS regexp
1 parent 920b38c commit c3a7d8b

File tree

3 files changed

+41
-56
lines changed

3 files changed

+41
-56
lines changed

src/commands/scan/create-scan-from-github.mts

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,13 @@ import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
77
import { logger } from '@socketsecurity/registry/lib/logger'
88
import { confirm, select } from '@socketsecurity/registry/lib/prompts'
99

10+
import { fetchSupportedScanFileNames } from './fetch-supported-scan-file-names.mts'
1011
import { handleCreateNewScan } from './handle-create-new-scan.mts'
12+
import { isReportSupportedFile } from '../../utils/glob.mts'
1113
import { fetchListAllRepos } from '../repository/fetch-list-all-repos.mts'
1214

1315
import type { CResult, OutputKind } from '../../types.mts'
1416

15-
// Supported manifest file name patterns
16-
// Keep in mind that we have to request these files through the GitHub API; that cost is much heavier than local disk searches
17-
// TODO: get this list from API instead? Is that too much? Has to fetch through gh api...
18-
const SUPPORTED_FILE_PATTERNS = [
19-
/.*[-.]spdx\.json/,
20-
/bom\.json/,
21-
/.*[-.]cyclonedx\.json/,
22-
/.*[-.]cyclonedx\.xml/,
23-
/package\.json/,
24-
/package-lock\.json/,
25-
/npm-shrinkwrap\.json/,
26-
/yarn\.lock/,
27-
/pnpm-lock\.yaml/,
28-
/pnpm-lock\.yml/,
29-
/pnpm-workspace\.yaml/,
30-
/pnpm-workspace\.yml/,
31-
/pipfile/,
32-
/pyproject\.toml/,
33-
/poetry\.lock/,
34-
/requirements[\\/].*\.txt/,
35-
/requirements-.*\.txt/,
36-
/requirements_.*\.txt/,
37-
/requirements\.frozen/,
38-
/setup\.py/,
39-
/pipfile\.lock/,
40-
/go\.mod/,
41-
/go\.sum/,
42-
/pom\.xml/,
43-
/.*\..*proj/,
44-
/.*\.props/,
45-
/.*\.targets/,
46-
/.*\.nuspec/,
47-
/nuget\.config/,
48-
/packages\.config/,
49-
/packages\.lock\.json/,
50-
]
51-
5217
export async function createScanFromGithub({
5318
all,
5419
githubApiUrl,
@@ -359,7 +324,12 @@ async function testAndDownloadManifestFile({
359324
}): Promise<CResult<{ isManifest: boolean }>> {
360325
debugFn('notice', 'testing: file', file)
361326

362-
if (!SUPPORTED_FILE_PATTERNS.some(regex => regex.test(file))) {
327+
const supportedFilesCResult = await fetchSupportedScanFileNames()
328+
const supportedFiles = supportedFilesCResult.ok
329+
? supportedFilesCResult.data
330+
: undefined
331+
332+
if (!supportedFiles || !isReportSupportedFile(file, supportedFiles)) {
363333
debugFn('notice', ' - skip: not a known pattern')
364334
// Not an error.
365335
return { ok: true, data: { isManifest: false } }

src/utils/glob.mts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { safeReadFile } from './fs.mts'
1212

1313
import type { Agent } from './package-environment.mts'
1414
import type { SocketYml } from '@socketsecurity/config'
15-
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
15+
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
1616
import type { GlobOptions } from 'tinyglobby'
1717

1818
const ignoredDirs = [
@@ -157,18 +157,25 @@ function workspacePatternToGlobPattern(workspace: string): string {
157157
return `${workspace}/package.json`
158158
}
159159

160-
export async function filterGlobResultToSupportedFiles(
161-
entries: string[] | readonly string[],
162-
supportedFiles: SocketSdkReturnType<'getReportSupportedFiles'>['data'],
163-
): Promise<string[]> {
160+
export function filterReportSupportedFiles(
161+
filepaths: string[] | readonly string[],
162+
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
163+
): string[] {
164+
const patterns = getSupportedFilePatterns(supportedFiles)
165+
return filepaths.filter(p => micromatch.some(p, patterns))
166+
}
167+
168+
export function getSupportedFilePatterns(
169+
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
170+
): string[] {
164171
const patterns: string[] = []
165172
for (const key of Object.keys(supportedFiles)) {
166173
const supported = supportedFiles[key]
167174
if (supported) {
168175
patterns.push(...Object.values(supported).map(p => `**/${p.pattern}`))
169176
}
170177
}
171-
return entries.filter(p => micromatch.some(p, patterns))
178+
return patterns
172179
}
173180

174181
type GlobWithGitIgnoreOptions = GlobOptions & {
@@ -257,6 +264,14 @@ export async function globWorkspace(
257264
: []
258265
}
259266

267+
export function isReportSupportedFile(
268+
filepath: string,
269+
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
270+
) {
271+
const patterns = getSupportedFilePatterns(supportedFiles)
272+
return micromatch.some(filepath, patterns)
273+
}
274+
260275
export function pathsToGlobPatterns(
261276
paths: string[] | readonly string[],
262277
): string[] {

src/utils/path-resolve.mts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import { resolveBinPathSync } from '@socketsecurity/registry/lib/npm'
88
import constants from '../constants.mts'
99
import { safeStatsSync } from './fs.mts'
1010
import {
11-
filterGlobResultToSupportedFiles,
11+
filterReportSupportedFiles,
1212
globWithGitIgnore,
1313
pathsToGlobPatterns,
1414
} from './glob.mts'
1515

1616
import type { SocketYml } from '@socketsecurity/config'
17-
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
18-
19-
const { NODE_MODULES, NPM, shadowBinPath } = constants
17+
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
2018

2119
export function findBinPathDetailsSync(binName: string): {
2220
name: string
@@ -28,6 +26,8 @@ export function findBinPathDetailsSync(binName: string): {
2826
all: true,
2927
nothrow: true,
3028
}) ?? []
29+
// Lazily access constants.shadowBinPath.
30+
const { shadowBinPath } = constants
3131
let shadowIndex = -1
3232
let theBinPath: string | undefined
3333
for (let i = 0, { length } = binPaths; i < length; i += 1) {
@@ -48,7 +48,7 @@ export function findNpmPathSync(npmBinPath: string): string | undefined {
4848
const { WIN32 } = constants
4949
let thePath = npmBinPath
5050
while (true) {
51-
const libNmNpmPath = path.join(thePath, 'lib', NODE_MODULES, NPM)
51+
const libNmNpmPath = path.join(thePath, 'lib/node_modules/npm')
5252
// mise puts its npm bin in a path like:
5353
// /Users/SomeUsername/.local/share/mise/installs/node/vX.X.X/bin/npm.
5454
// HOWEVER, the location of the npm install is:
@@ -60,9 +60,9 @@ export function findNpmPathSync(npmBinPath: string): string | undefined {
6060
existsSync(libNmNpmPath) &&
6161
safeStatsSync(libNmNpmPath)?.isDirectory()
6262
) {
63-
thePath = path.join(libNmNpmPath, NPM)
63+
thePath = path.join(libNmNpmPath, 'npm')
6464
}
65-
const nmPath = path.join(thePath, NODE_MODULES)
65+
const nmPath = path.join(thePath, 'node_modules')
6666
if (
6767
// npm bin paths may look like:
6868
// /usr/local/share/npm/bin/npm
@@ -77,9 +77,9 @@ export function findNpmPathSync(npmBinPath: string): string | undefined {
7777
existsSync(nmPath) &&
7878
safeStatsSync(nmPath)?.isDirectory() &&
7979
// Optimistically look for the default location.
80-
(path.basename(thePath) === NPM ||
80+
(path.basename(thePath) === 'npm' ||
8181
// Chocolatey installs npm bins in the same directory as node bins.
82-
(WIN32 && existsSync(path.join(thePath, `${NPM}.cmd`))))
82+
(WIN32 && existsSync(path.join(thePath, 'npm.cmd'))))
8383
) {
8484
return thePath
8585
}
@@ -98,16 +98,16 @@ export type PackageFilesForScanOptions = {
9898

9999
export async function getPackageFilesForScan(
100100
inputPaths: string[],
101-
supportedFiles: SocketSdkReturnType<'getReportSupportedFiles'>['data'],
101+
supportedFiles: SocketSdkSuccessResult<'getReportSupportedFiles'>['data'],
102102
options?: PackageFilesForScanOptions | undefined,
103103
): Promise<string[]> {
104104
const { config: socketConfig, cwd = process.cwd() } = {
105105
__proto__: null,
106106
...options,
107107
} as PackageFilesForScanOptions
108-
const entries = await globWithGitIgnore(pathsToGlobPatterns(inputPaths), {
108+
const filepaths = await globWithGitIgnore(pathsToGlobPatterns(inputPaths), {
109109
cwd,
110110
socketConfig,
111111
})
112-
return await filterGlobResultToSupportedFiles(entries, supportedFiles)
112+
return filterReportSupportedFiles(filepaths, supportedFiles)
113113
}

0 commit comments

Comments
 (0)