Skip to content

Commit

Permalink
fix: further harden add (#41)
Browse files Browse the repository at this point in the history
* fix: don't allow setting shell to true

* fix: harden add

* test: fix
  • Loading branch information
sparten11740 authored Mar 13, 2024
1 parent adf651a commit 5bc3d09
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 13 deletions.
6 changes: 5 additions & 1 deletion dist/publish/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/publish/index.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions dist/version/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/version/index.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/utils/git.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { add, commit, resetLastCommit } from './git'

describe('add', () => {
it('should allow valid paths', () => {
expect(() => add(['./some-path', 'package.json'])).not.toThrow('Options are not allowed')
expect(() => add(['/some-absolute-path'])).not.toThrow('Options are not allowed')
})

it('should throw when trying to use flags', () => {
expect(() => add(['--force', '.yarnrc.yml'])).toThrow('Options are not allowed')
expect(() => add(['--force', '.yarnrc.yml'])).toThrow('Options are not allowed')
})

it('should throw trying to hide flags', () => {
expect(() => add(['``--force', '.yarnrc.yml'])).toThrow('Options are not allowed')
expect(() => add(['${}--force', '.yarnrc.yml'])).toThrow('Options are not allowed')
})
})

describe('commit', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { spawnSync } from './process'
import { flagsAsArguments } from './objects'
import * as assert from 'node:assert'

const PATH_CHARACTERS = /^[\w./-]+$/

export function add(pathSpecs: string[]) {
assert(
pathSpecs.every((it) => !it.startsWith('-')),
pathSpecs.every((it) => !it.startsWith('-') && PATH_CHARACTERS.test(it)),
'Options are not allowed. Please supply paths to the files you want to add only.'
)

Expand Down
8 changes: 6 additions & 2 deletions src/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { spawnSync as nodeSpawnSync, SpawnSyncOptionsWithStringEncoding } from '
export const spawnSync = (
command: string,
args: string[],
options: Partial<SpawnSyncOptionsWithStringEncoding> = {}
options: Omit<Partial<SpawnSyncOptionsWithStringEncoding>, 'shell'> = {}
) => {
const { stdout, stderr, status } = nodeSpawnSync(command, args, { encoding: 'utf8', ...options })
const { stdout, stderr, status } = nodeSpawnSync(command, args, {
encoding: 'utf8',
...options,
shell: false,
})

if (status !== 0) {
throw new Error(stderr)
Expand Down
4 changes: 2 additions & 2 deletions src/version/get-tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function setup(tags: string[]) {
const commit = '26d0f601ef58b14de321cad15b059fe2962b37f5'

when(spawnSync)
.calledWith('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' })
.calledWith('git', ['rev-parse', 'HEAD'], { encoding: 'utf8', shell: false })
.mockReturnValue({ stdout: commit, stderr: '', status: 0 } as never)
.calledWith('git', ['tag', '--contains', commit], { encoding: 'utf8' })
.calledWith('git', ['tag', '--contains', commit], { encoding: 'utf8', shell: false })
.mockReturnValue({ stdout: tags.join('\n'), stderr: '', status: 0 } as never)
}
6 changes: 3 additions & 3 deletions src/version/version-packages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('versionPackages', () => {
'--no-private',
'--force-publish',
],
{ encoding: 'utf8' }
{ encoding: 'utf8', shell: false }
)
})

Expand All @@ -47,7 +47,7 @@ describe('versionPackages', () => {
'--no-private',
'--force-publish',
],
{ encoding: 'utf8' }
{ encoding: 'utf8', shell: false }
)
})

Expand All @@ -69,7 +69,7 @@ describe('versionPackages', () => {
'--force-publish',
'--let-bruce-wayne-decide',
],
{ encoding: 'utf8' }
{ encoding: 'utf8', shell: false }
)
})
})

0 comments on commit 5bc3d09

Please sign in to comment.