Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(build): read the JSON form of COPY and ADD in the gate

Yudhi Armyndhariscbbafd93cddf
diff --git a/scripts/check-dockerignore.mjs b/scripts/check-dockerignore.mjs
index 162211171..b538701b4 100644
--- a/scripts/check-dockerignore.mjs
+++ b/scripts/check-dockerignore.mjs
@@ -195,9 +195,24 @@ function copyInputs(source) {
.filter(entry => name.test(entry))
.map(entry => dir + entry);
}
+// The words of an instruction's arguments. The exec form, COPY ["src", "dest"], is a JSON array after any
+// flags and the only form that can name a path with a blank; text that does not parse as JSON is the shell
+// form, as it is in BuildKit.
+function argumentWords(args) {
+ const text = args.trim();
+ const json = /^((?:--\S+\s+)*)(\[.*\])$/.exec(text);
+ if (json) {
+ try {
+ return [...json[1].split(/\s+/).filter(Boolean), ...JSON.parse(json[2]).map(String)];
+ } catch {
+ // not JSON
+ }
+ }
+ return text.split(/\s+/);
+}
const copied = [];
for (const [, args] of dockerfile.matchAll(/^[ \t]*(?:COPY|ADD)[ \t]+(.+)$/gim)) {
- const words = args.trim().split(/\s+/);
+ const words = argumentWords(args);
if (words.some(word => word.startsWith('--from='))) continue;
const sources = words.filter(word => !word.startsWith('--')).slice(0, -1);
copied.push(...sources.flatMap(copyInputs));
diff --git a/scripts/check-dockerignore.spec.mjs b/scripts/check-dockerignore.spec.mjs
index d90e306b0..e10aee694 100644
--- a/scripts/check-dockerignore.spec.mjs
+++ b/scripts/check-dockerignore.spec.mjs
@@ -126,6 +126,30 @@ test('a comment ends with its line, whatever it ends in, and is not part of a co
}
});
+test('the JSON array form of COPY and ADD names its sources', () => {
+ // Real BuildKit builds COPY ["package.json", "./"], and it is the only form that can name a path with a blank.
+ const forms = [
+ ['COPY ["new.txt", "./"]', 'new.txt'],
+ ['COPY ["scripts/new.js", "./scripts/"]', 'scripts/new.js'],
+ ['COPY --chown=openwa:openwa ["new.txt", "./"]', 'new.txt'],
+ ['ADD ["new.txt", "./"]', 'new.txt'],
+ ['COPY ["dir with blank/new.txt", "./"]', 'dir with blank/new.txt'],
+ ];
+ for (const [line, source] of forms) {
+ const run = runGate({
+ Dockerfile: `COPY control.txt ./\n${line}\n`,
+ '.dockerignore': `${REAL_DOCKERIGNORE}\n${source}\n`,
+ });
+ assert.equal(run.status, 1, `${line} is dropped by a rule and the gate passed: ${run.out}`);
+ assert.match(run.out, new RegExp(`rejects '${source.replaceAll('.', '\\.')}'`), `${line}: ${run.out}`);
+ }
+});
+
+test('text that looks like the JSON form but is not JSON is read as the shell form, not as a crash', () => {
+ const run = runGate({ Dockerfile: 'COPY control.txt ./\nCOPY [not json]\n', '.dockerignore': REAL_DOCKERIGNORE });
+ assert.equal(run.status, 0, run.out);
+});
+
test('an indented or lower-case instruction is read like any other', () => {
for (const line of [
' COPY ./scripts/new.js ./scripts/',
@@ -177,6 +201,7 @@ test('only the context files a COPY or ADD reads are build inputs', () => {
[
'COPY --chown=openwa:openwa src-a.txt dest-1/',
'COPY --from=builder /stage-src /stage-dest',
+ 'COPY --from=builder ["/stage-json", "/stage-json-dest"]',
'COPY ./src-b.txt dest-2',
'ADD https://example.com/tool.tgz /opt/',
'ADD [email protected]:org/repo.git /opt/repo',