@@ -122,7 +122,7 @@ async function populateR2IncrementalCache(
122
122
123
123
runWrangler (
124
124
options ,
125
- [ "r2 object put" , JSON . stringify ( path . join ( bucket , cacheKey ) ) , `--file ${ JSON . stringify ( fullPath ) } ` ] ,
125
+ [ "r2 object put" , quoteShellMeta ( path . join ( bucket , cacheKey ) ) , `--file ${ quoteShellMeta ( fullPath ) } ` ] ,
126
126
// NOTE: R2 does not support the environment flag and results in the following error:
127
127
// Incorrect type for the 'cacheExpiry' field on 'HttpMetadata': the provided value is not of type 'date'.
128
128
{ target : populateCacheOptions . target , excludeRemoteFlag : true , logging : "error" }
@@ -168,11 +168,10 @@ async function populateKVIncrementalCache(
168
168
169
169
writeFileSync ( chunkPath , JSON . stringify ( kvMapping ) ) ;
170
170
171
- runWrangler (
172
- options ,
173
- [ "kv bulk put" , JSON . stringify ( chunkPath ) , `--binding ${ JSON . stringify ( KV_CACHE_BINDING_NAME ) } ` ] ,
174
- { ...populateCacheOptions , logging : "error" }
175
- ) ;
171
+ runWrangler ( options , [ "kv bulk put" , quoteShellMeta ( chunkPath ) , `--binding ${ KV_CACHE_BINDING_NAME } ` ] , {
172
+ ...populateCacheOptions ,
173
+ logging : "error" ,
174
+ } ) ;
176
175
177
176
rmSync ( chunkPath ) ;
178
177
}
@@ -197,7 +196,7 @@ function populateD1TagCache(
197
196
options ,
198
197
[
199
198
"d1 execute" ,
200
- JSON . stringify ( D1_TAG_BINDING_NAME ) ,
199
+ D1_TAG_BINDING_NAME ,
201
200
`--command "CREATE TABLE IF NOT EXISTS revalidations (tag TEXT NOT NULL, revalidatedAt INTEGER NOT NULL, UNIQUE(tag) ON CONFLICT REPLACE);"` ,
202
201
] ,
203
202
{ ...populateCacheOptions , logging : "error" }
@@ -258,3 +257,23 @@ export async function populateCache(
258
257
}
259
258
}
260
259
}
260
+
261
+ /**
262
+ * Escape shell metacharacters.
263
+ *
264
+ * When `spawnSync` is invoked with `shell: true`, metacharacters need to be escaped.
265
+ *
266
+ * Based on https://github.com/ljharb/shell-quote/blob/main/quote.js
267
+ *
268
+ * @param arg
269
+ * @returns escaped arg
270
+ */
271
+ function quoteShellMeta ( arg : string ) {
272
+ if ( / [ " \s ] / . test ( arg ) && ! / ' / . test ( arg ) ) {
273
+ return `'${ arg . replace ( / ( [ ' \\ ] ) / g, "\\$1" ) } '` ;
274
+ }
275
+ if ( / [ " ' \s ] / . test ( arg ) ) {
276
+ return `"${ arg . replace ( / ( [ " \\ $ ` ! ] ) / g, "\\$1" ) } "` ;
277
+ }
278
+ return arg . replace ( / ( [ A - Z a - z ] : ) ? ( [ # ! " $ & ' ( ) * , : ; < = > ? @ [ \\ \] ^ ` { | } ] ) / g, "$1\\$2" ) ;
279
+ }
0 commit comments