From 365d06066a6b10e8fa913e09956c91ad85f69604 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Sun, 10 Nov 2024 06:43:16 -0500 Subject: [PATCH] Fix #114 Add more checks to ensure a heredoc is well-formed Signed-off-by: Remy Suen --- CHANGELOG.md | 1 + src/util.ts | 11 +++++++- test/heredoc.test.ts | 52 ++++++++++++++++++++++++++++---------- test/imageTemplate.test.ts | 6 +++++ 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f8c3a9..2af18c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. ### Fixed - improve heredoc parser so that invalid delimiters are now skipped over when parsing ([#117](https://github.com/rcjsuen/dockerfile-ast/issues/117)) +- stop considering a heredoc delimiter without a closing quote as a heredoc ([#114](https://github.com/rcjsuen/dockerfile-ast/issues/114)) ## [0.6.1] - 2023-09-10 ### Fixed diff --git a/src/util.ts b/src/util.ts index 56195c0..d710cec 100644 --- a/src/util.ts +++ b/src/util.ts @@ -77,7 +77,16 @@ export class Util { if (value.charAt(0) === '-') { value = value.substring(1); } - if (value.charAt(0) === '"' || value.charAt(0) === '\'') { + if (value.charAt(0) === '"') { + if (value.charAt(value.length - 1) !== '"') { + return null; + } + value = value.substring(1, value.length - 1); + } + if (value.charAt(0) === '\'') { + if (value.charAt(value.length - 1) !== '\'') { + return null; + } value = value.substring(1, value.length - 1); } if (value.charAt(0) === "<") { diff --git a/test/heredoc.test.ts b/test/heredoc.test.ts index 0226f06..ef32951 100644 --- a/test/heredoc.test.ts +++ b/test/heredoc.test.ts @@ -12,10 +12,44 @@ describe("Heredoc", () => { function testHeredoc(keyword: string, heredocsExtractor: (instruction: Instruction) => Heredoc[]): void { const keywordLength = keyword.length; describe(keyword, () => { - it("no heredocs", () => { - const instruction = DockerfileParser.parse(keyword).getInstructions()[0]; - const heredocs = heredocsExtractor(instruction); - assert.strictEqual(heredocs.length, 0); + describe("no heredocs", () => { + it("instruction only", () => { + const instruction = DockerfileParser.parse(keyword).getInstructions()[0]; + const heredocs = heredocsExtractor(instruction); + assert.strictEqual(heredocs.length, 0); + }); + + /** + * INSTRUCTION << { + const instruction = DockerfileParser.parse(`${keyword} << { + const instruction = DockerfileParser.parse(`${keyword} <<'EOT\nEOT\n`).getInstructions()[0]; + const heredocs = heredocsExtractor(instruction); + assert.strictEqual(heredocs.length, 0); + }); + + /** + * INSTRUCTION <<"EOT + * EOT + * + */ + it(`${keyword} <<"EOT\\nEOT\\n`, () => { + const instruction = DockerfileParser.parse(`${keyword} <<"EOT\nEOT\n`).getInstructions()[0]; + const heredocs = heredocsExtractor(instruction); + assert.strictEqual(heredocs.length, 0); + }); }); it(`${keyword} <<--EOT\\n-EOT`, () => { @@ -417,16 +451,6 @@ describe("Heredoc", () => { assert.strictEqual(heredocs[0].getDelimiterRange(), null); }); - /** - * RUN << { - const instruction = DockerfileParser.parse(`${keyword} << { dockerfile = DockerfileParser.parse(onbuildPrefix + "RUN <<-\"