generated from digicatapult/openapi-service-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parameter escaping and binary renaming to ensure we don't get rac… (
#14) * Add parameter escaping and binary renaming to ensure we don't get race conditions if there are multiple inflight requests. Further tests on the controller to come * Linting fix * Version bump * Add in other examples to openapi type * Additional unit tests * Fix linting * Debug CI failure * Fix test to incorporate env
- Loading branch information
1 parent
7af1f1f
commit e557fd7
Showing
7 changed files
with
138 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const { expect } = require('chai') | ||
const { escapeParam, getValidHeredocEOF, getRandomProcessName } = require('../params') | ||
|
||
describe('getRandomProcessName', () => { | ||
it('should append a length 10 string of lowercase alpha chars', () => { | ||
expect(getRandomProcessName('test')).to.match(/^test_[a-z]{10}$/) | ||
}) | ||
|
||
it('should always return a different string', () => { | ||
const exampleSet = new Set( | ||
Array(1000) | ||
.fill(null) | ||
.map(() => getRandomProcessName('test')) | ||
) | ||
expect(exampleSet.size).to.equal(1000) | ||
}) | ||
}) | ||
|
||
describe('getValidHeredocEOF', () => { | ||
it('should by default returns EOF', () => { | ||
expect(getValidHeredocEOF('test', [])).to.equal('EOF') | ||
}) | ||
|
||
it('should append _ if bin contains EOF', () => { | ||
expect(getValidHeredocEOF('test_EOF', [])).to.equal('EOF_') | ||
}) | ||
|
||
it('should append _ if parameter contains EOF', () => { | ||
expect(getValidHeredocEOF('test', ['param_EOF'])).to.equal('EOF_') | ||
}) | ||
|
||
it('should append __ if bin contains EOF_', () => { | ||
expect(getValidHeredocEOF('test_EOF_', [])).to.equal('EOF__') | ||
}) | ||
|
||
it('should append __ if parameter contains EOF_', () => { | ||
expect(getValidHeredocEOF('test_EOF', ['param_EOF_'])).to.equal('EOF__') | ||
}) | ||
|
||
it(`should append _____ if bin contains EOF____`, () => { | ||
expect(getValidHeredocEOF('test_EOF____test', [])).to.equal('EOF_____') | ||
}) | ||
}) | ||
|
||
describe('escapeParams', () => { | ||
it('returns input wrapped in quotes', () => { | ||
expect(escapeParam('arg1')).to.equal(`'arg1'`) | ||
}) | ||
|
||
it('replaces single quotes with an escaped quote', () => { | ||
expect(escapeParam("arg1'test")).to.equal(`'arg1'\\''test'`) | ||
}) | ||
|
||
it('replaces all single quotes with an escaped quote', () => { | ||
expect(escapeParam("arg1'test'test")).to.equal(`'arg1'\\''test'\\''test'`) | ||
}) | ||
|
||
it('replaces initial quote correctly', () => { | ||
expect(escapeParam("'arg1")).to.equal(`''\\''arg1'`) | ||
}) | ||
|
||
it('replaces trailing quote correctly', () => { | ||
expect(escapeParam("arg1'")).to.equal(`'arg1'\\'''`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const processChars: string = 'abcdefghijklmnopqrstuvwxyz' | ||
export const getRandomProcessName = (bin: string): string => { | ||
return `${bin}_${Array(10).fill(null).map(_ => processChars[Math.floor(Math.random() * processChars.length)]).join('')}` | ||
} | ||
|
||
export const getValidHeredocEOF = (bin: string, params: string[]): string => { | ||
const fullCmd: string = `/tmp/${bin} ${params.join(' ')}` | ||
let eof: string = 'EOF' | ||
while (fullCmd.includes(eof)) { | ||
eof = `${eof}_` | ||
} | ||
return eof | ||
} | ||
|
||
export const escapeParam = (param: string): string => { | ||
return `'${param.replace(/'/g, "'\\''")}'` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters