wp-advads 1.0.37 → 1.0.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "wp-advads",
4
- "version": "1.0.37",
4
+ "version": "1.0.38",
5
5
  "description": "Create an Advanced Ads WordPress plugin ecosystem.",
6
6
  "author": {
7
7
  "name": "Shakeeb Ahmed",
@@ -3,6 +3,48 @@
3
3
  */
4
4
  import { spawn } from 'node:child_process'
5
5
 
6
+ // cmd.exe metacharacters. See https://github.com/moxystudio/node-cross-spawn/blob/v7.0.6/lib/util/escape.js
7
+ const WIN_META = /([()\][%!^"`<>&|;, *?])/g
8
+
9
+ function escapeWinCommand( arg ) {
10
+ return String( arg ).replace( WIN_META, '^$1' )
11
+ }
12
+
13
+ function escapeWinArgument( arg ) {
14
+ let value = String( arg )
15
+ value = value.replace( /(?=(\\+?)?)\1"/g, '$1$1\\"' )
16
+ value = value.replace( /(?=(\\+?)?)\1$/, '$1$1' )
17
+ value = `"${value}"`
18
+ return value.replace( WIN_META, '^$1' )
19
+ }
20
+
21
+ /**
22
+ * Windows argv spawn must go through cmd.exe so PATHEXT (.cmd/.bat) resolves.
23
+ * Direct spawn('wp') looks for wp.exe only and throws ENOENT.
24
+ *
25
+ * @param {string} file Command name
26
+ * @param {string[]} args Arguments
27
+ * @param {string} [comspec] cmd.exe path
28
+ * @returns {{ file: string, args: string[], options: Object }}
29
+ */
30
+ export function winArgvSpawnSpec( file, args, comspec = process.env.ComSpec || process.env.comspec || 'cmd.exe' ) {
31
+ const shellCommand = [ escapeWinCommand( file ), ...args.map( escapeWinArgument ) ].join( ' ' )
32
+ return {
33
+ file: comspec,
34
+ args: [ '/d', '/s', '/c', `"${shellCommand}"` ],
35
+ options: { shell: false, windowsVerbatimArguments: true },
36
+ }
37
+ }
38
+
39
+ function spawnArgv( file, args, spawnOpts ) {
40
+ if ( process.platform !== 'win32' ) {
41
+ return spawn( file, args, { ...spawnOpts, shell: false } )
42
+ }
43
+
44
+ const spec = winArgvSpawnSpec( file, args )
45
+ return spawn( spec.file, spec.args, { ...spawnOpts, ...spec.options } )
46
+ }
47
+
6
48
  /**
7
49
  * Run a command in the terminal.
8
50
  *
@@ -38,7 +80,7 @@ export function execCommand( command, callback, options = {} ) {
38
80
  }
39
81
 
40
82
  const child = Array.isArray( command )
41
- ? spawn( command[0], command.slice( 1 ), { ...spawnOpts, shell: false } )
83
+ ? spawnArgv( command[0], command.slice( 1 ), spawnOpts )
42
84
  : spawn( command, { ...spawnOpts, shell: true } )
43
85
 
44
86
  let stdout = ''
@@ -7,7 +7,7 @@ import { existsSync, rmSync } from 'node:fs'
7
7
  import { tmpdir } from 'node:os'
8
8
  import { join } from 'node:path'
9
9
  import { test } from 'node:test'
10
- import { execCommand, runCommand } from '../src/utilities/shell.js'
10
+ import { execCommand, runCommand, winArgvSpawnSpec } from '../src/utilities/shell.js'
11
11
 
12
12
  function execOnce( command ) {
13
13
  return new Promise( ( resolve ) => {
@@ -130,3 +130,24 @@ test( 'execCommand does not ENOBUFS on stdout larger than 1MB', async () => {
130
130
  console.log = originalLog
131
131
  }
132
132
  } )
133
+
134
+ test( 'Windows argv spawn uses cmd.exe so PATHEXT commands like wp.cmd resolve', () => {
135
+ const spec = winArgvSpawnSpec( 'wp', [ 'i18n', 'make-pot' ], 'C:\\Windows\\system32\\cmd.exe' )
136
+ assert.equal( spec.file, 'C:\\Windows\\system32\\cmd.exe' )
137
+ assert.deepEqual( spec.args.slice( 0, 3 ), [ '/d', '/s', '/c' ] )
138
+ assert.match( spec.args[3], /wp/ )
139
+ assert.equal( spec.options.windowsVerbatimArguments, true )
140
+ assert.equal( spec.options.shell, false )
141
+ } )
142
+
143
+ test( 'Windows argv spawn escapes cmd metacharacters so headers and injection stay literal', () => {
144
+ const spec = winArgvSpawnSpec(
145
+ 'wp',
146
+ [ '--headers={"Last-Translator":"A <b@c.com>"}', '& echo pwned' ],
147
+ 'cmd.exe'
148
+ )
149
+ const line = spec.args[3]
150
+ assert.match( line, /\^</ )
151
+ assert.match( line, /\^>/ )
152
+ assert.match( line, /\^&/ )
153
+ } )