Skip to content

Commit 33d421a

Browse files
fix: flatten path separators in pack output filename (#9468)
Backport of #9446 to `release/v11`. Co-authored-by: Dexter.k <164054284+rootvector2@users.noreply.github.com>
1 parent 1bb1b8c commit 33d421a

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

workspaces/libnpmpack/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async function pack (spec = 'file:.', opts = {}) {
3737
// check for explicit `false` so the default behavior is to skip writing to disk
3838
if (opts.dryRun === false) {
3939
const filename = `${manifest.name}-${manifest.version}.tgz`
40-
.replace(/^@/, '').replace(/\//, '-')
40+
.replace(/^@/, '').replace(/[/\\]/g, '-')
4141
const destination = path.resolve(opts.packDestination, filename)
4242
await writeFile(destination, tarball)
4343
}

workspaces/libnpmpack/test/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ t.test('packs from local directory', async t => {
3636
})
3737
})
3838

39+
t.test('flattens path separators in name so tarball stays in packDestination', async t => {
40+
const testDir = t.testdir({
41+
src: {
42+
'package.json': JSON.stringify({
43+
name: 'x/../../../../../../escaped',
44+
version: '1.0.0',
45+
}, null, 2),
46+
},
47+
dest: {},
48+
})
49+
50+
const dest = path.join(testDir, 'dest')
51+
await pack(`file:${path.join(testDir, 'src')}`, {
52+
dryRun: false,
53+
packDestination: dest,
54+
silent: true,
55+
})
56+
57+
const written = fs.readdirSync(dest)
58+
t.same(written, ['x-..-..-..-..-..-..-escaped-1.0.0.tgz'], 'separators flattened to a single filename')
59+
t.notOk(fs.existsSync(path.join(testDir, 'escaped-1.0.0.tgz')), 'nothing escaped the destination')
60+
})
61+
3962
t.test('writes tarball to file when dryRun === false', async t => {
4063
const testDir = t.testdir({
4164
'package.json': JSON.stringify({

0 commit comments

Comments
 (0)