Skip to content

Commit 923698b

Browse files
committed
fix: add error handling and validation for critical operations
- Add try-catch for JSON.parse in release and package scripts to prevent crashes - Add array validation in update-manifest to handle malformed entries - Fix indexOf edge case in git.mjs to prevent package name truncation Resolves quality scan critical issues #1, #2, #5, #7, #8, #9 (JSON.parse crashes, array access, indexOf bug).
1 parent 7b122e5 commit 923698b

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

scripts/npm/release-npm-packages.mjs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ async function getLocalPackageFileHashes(packagePath) {
7676
// Read package.json to get files field.
7777
const pkgJsonPath = path.join(packagePath, PACKAGE_JSON)
7878
const pkgJsonContent = await readFileUtf8(pkgJsonPath)
79-
const pkgJson = JSON.parse(pkgJsonContent)
79+
let pkgJson
80+
try {
81+
pkgJson = JSON.parse(pkgJsonContent)
82+
} catch (e) {
83+
throw new Error(`Failed to parse package.json at ${pkgJsonPath}`, {
84+
cause: e,
85+
})
86+
}
8087
const filesPatterns = pkgJson.files || []
8188

8289
// Always include package.json.
@@ -182,7 +189,14 @@ async function getRemotePackageFileHashes(spec) {
182189

183190
if (entry.name === PACKAGE_JSON) {
184191
// For package.json, hash only relevant fields (not version).
185-
const pkgJson = JSON.parse(content)
192+
let pkgJson
193+
try {
194+
pkgJson = JSON.parse(content)
195+
} catch (e) {
196+
throw new Error(`Failed to parse package.json at ${fullPath}`, {
197+
cause: e,
198+
})
199+
}
186200
const exportsValue = pkgJson.exports
187201
const relevantData = {
188202
dependencies: toSortedObject(pkgJson.dependencies ?? {}),

scripts/npm/update-manifest.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ async function addNpmManifestData(manifest, options) {
190190

191191
const latestIndexes = []
192192
for (let i = 0, { length } = manifestData; i < length; i += 1) {
193-
if (manifestData[i][0].endsWith(AT_LATEST)) {
193+
const entry = manifestData[i]
194+
if (Array.isArray(entry) && entry[0]?.endsWith?.(AT_LATEST)) {
194195
latestIndexes.push(i)
195196
}
196197
}

scripts/utils/git.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ function innerGetPackages(eco, files, options) {
5151
if (eco === NPM && filepath.startsWith(REL_REGISTRY_PKG_PATH)) {
5252
sockRegPkgName = '../../registry/dist/index.js'
5353
} else {
54+
const slashIndex = filepath.indexOf('/', sliceStart)
5455
sockRegPkgName = filepath.slice(
5556
sliceStart,
56-
filepath.indexOf('/', sliceStart),
57+
slashIndex === -1 ? undefined : slashIndex,
5758
)
5859
}
5960
packageNames.add(sockRegPkgName)

scripts/utils/package.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,14 @@ async function installPackageForTesting(sourcePath, packageName, options = {}) {
396396

397397
// Merge back the test scripts and devDependencies if they existed.
398398
const pkgJsonPath = path.join(installedPath, 'package.json')
399-
const pkgJson = JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'))
399+
let pkgJson
400+
try {
401+
pkgJson = JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'))
402+
} catch (e) {
403+
throw new Error(`Failed to parse package.json at ${pkgJsonPath}`, {
404+
cause: e,
405+
})
406+
}
400407

401408
// Preserve devDependencies from original (only when we installed from npm).
402409
if (versionSpec && originalDevDependencies) {

0 commit comments

Comments
 (0)