Skip to content

Commit

Permalink
fix: properly quote parameters for vip wp
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Jan 8, 2025
1 parent 0a6d195 commit 5f5c64f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
14 changes: 7 additions & 7 deletions __tests__/lib/cli/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ describe( 'utils/cli/format', () => {
},
{
input: [ 'textnospaces' ],
expected: [ 'textnospaces' ],
expected: [ '"textnospaces"' ],
},
{
input: [ '{"json":"json with spaces"}' ],
expected: [ '{"json":"json with spaces"}' ],
expected: [ '"{\\"json\\":\\"json with spaces\\"}"' ],
},
{
input: [ '{ "json" : "json with spaces outside strings" }' ],
expected: [ '{ "json" : "json with spaces outside strings" }' ],
expected: [ '"{ \\"json\\" : \\"json with spaces outside strings\\" }"' ],
},
{
input: [
' { "json" : "json with spaces outside strings and outside the object" } ',
],
expected: [
' { "json" : "json with spaces outside strings and outside the object" } ',
'" { \\"json\\" : \\"json with spaces outside strings and outside the object\\" } "',
],
},
{
input: [ '{ "json" : "spaces-outside-strings-only" }' ],
expected: [ '{ "json" : "spaces-outside-strings-only" }' ],
expected: [ '"{ \\"json\\" : \\"spaces-outside-strings-only\\" }"' ],
},
{
input: [ '{"json":broken json with spaces}' ],
expected: [ '"{\\"json\\":broken json with spaces}"' ],
},
{
input: [ '--foo=bar1 "bar2" "bar3"' ],
expected: [ '--foo="bar1 \\"bar2\\" \\"bar3\\""' ],
expected: [ '"--foo=bar1 \\"bar2\\" \\"bar3\\""' ],
},
{
input: [ '--foo', 'bar1 "bar2" "bar3"' ],
expected: [ '--foo', '"bar1 \\"bar2\\" \\"bar3\\""' ],
expected: [ '"--foo"', '"bar1 \\"bar2\\" \\"bar3\\""' ],
},
] )( 'should requote args when needed - %o', ( { input, expected } ) => {
const result = requoteArgs( input );
Expand Down
25 changes: 1 addition & 24 deletions src/lib/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,7 @@ export function keyValue( values: Tuple[] ): string {
}

export function requoteArgs( args: string[] ): string[] {
return args.map( arg => {
if ( arg.includes( '--' ) && arg.includes( '=' ) && arg.includes( ' ' ) ) {
return arg.replace( /"/g, '\\"' ).replace( /^--([^=]*)=(.*)$/, '--$1="$2"' );
}

if ( arg.includes( ' ' ) && ! isJsonObject( arg ) ) {
return `"${ arg.replace( /"/g, '\\"' ) }"`;
}

return arg;
} );
}

export function isJsonObject( str: unknown ): boolean {
return typeof str === 'string' && str.trim().startsWith( '{' ) && isJson( str );
}

export function isJson( str: string ): boolean {
try {
JSON.parse( str );
return true;
} catch ( error ) {
return false;
}
return args.map( arg => `"${ arg.replace( /"/g, '\\"' ) }"` );

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.
}

export function capitalize( str: unknown ): string {
Expand Down

0 comments on commit 5f5c64f

Please sign in to comment.