Skip to content

Commit

Permalink
fix(utils/tar): index access while match is null (#7555)
Browse files Browse the repository at this point in the history
  • Loading branch information
NormanPerrin committed May 25, 2024
1 parent 9122fb6 commit 153ec81
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/utils/tar.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const getContents = async (manifest, tarball) => {
totalEntries++
totalEntrySize += entry.size
const p = entry.path
if (p.startsWith('package/node_modules/')) {
if (p.startsWith('package/node_modules/') && p !== 'package/node_modules/') {
const name = p.match(/^package\/node_modules\/((?:@[^/]+\/)?[^/]+)/)[1]
bundled.add(name)
}
Expand All @@ -72,7 +72,7 @@ const getContents = async (manifest, tarball) => {
})
stream.end(tarball)

const integrity = await ssri.fromData(tarball, {
const integrity = ssri.fromData(tarball, {
algorithms: ['sha1', 'sha512'],
})

Expand Down
81 changes: 80 additions & 1 deletion test/lib/utils/tar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const t = require('tap')
const tar = require('tar')
const pack = require('libnpmpack')
const ssri = require('ssri')
const { readFile } = require('fs/promises')
const tmock = require('../../fixtures/tmock')
const { cleanZlib } = require('../../fixtures/clean-snapshot')

Expand Down Expand Up @@ -106,7 +108,7 @@ t.test('should log tarball contents with unicode', async (t) => {
t.end()
})

t.test('should getContents of a tarball', async (t) => {
t.test('should getContents of a tarball with only a package.json', async (t) => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
Expand Down Expand Up @@ -142,3 +144,80 @@ t.test('should getContents of a tarball', async (t) => {
}, 'contents are correct')
t.end()
})

t.test('should getContents of a tarball with a node_modules directory included', async (t) => {
const testDir = t.testdir({
package: {
'package.json': JSON.stringify({
name: 'my-cool-pkg',
version: '1.0.0',
}, null, 2),
node_modules: {
'bundle-dep': {
'package.json': JSON.stringify({
name: 'bundle-dep',
version: '1.0.0',
}, null, 2),
},
},
},
})

await tar.c({
gzip: true,
file: 'npm-example-v1.tgz',
C: testDir

Check failure on line 169 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Missing trailing comma
}, ['package'])

const tarball = await readFile(`npm-example-v1.tgz`)

const tarballContents = await getContents({
name: 'my-cool-pkg',
version: '1.0.0',
}, tarball)

const integrity = ssri.fromData(tarball, {
algorithms: ['sha1', 'sha512'],
})

// zlib is nondeterministic
t.match(tarballContents.shasum, /^[0-9a-f]{40}$/)
delete tarballContents.shasum
t.strictSame(tarballContents, {
id: 'my-cool-pkg@1.0.0',
name: 'my-cool-pkg',
version: '1.0.0',
size: tarball.length,
unpackedSize: 97,
integrity: ssri.parse(integrity.sha512[0]),
filename: 'my-cool-pkg-1.0.0.tgz',
files: [{
"path": "",

Check failure on line 195 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'path' found

Check failure on line 195 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

Check failure on line 195 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
"size": 0,

Check failure on line 196 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'size' found

Check failure on line 196 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
"mode": 493,

Check failure on line 197 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'mode' found

Check failure on line 197 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
},
{
"path": "node_modules/",

Check failure on line 200 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Unnecessarily quoted property 'path' found

Check failure on line 200 in test/lib/utils/tar.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
"size": 0,
"mode": 493,
},
{
"path": "node_modules/bundle-dep/",
"size": 0,
"mode": 493,
},
{
"path": "node_modules/bundle-dep/package.json",
"size": 48,
"mode": 420,
},
{
"path": "package.json",
"size": 49,
"mode": 420,
}],
entryCount: 5,
bundled: ['bundle-dep'],
}, 'contents are correct')
t.end()
})

0 comments on commit 153ec81

Please sign in to comment.