Skip to content

Commit

Permalink
Fix #114 Add more checks to ensure a heredoc is well-formed
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Nov 10, 2024
1 parent 4710283 commit 365d060
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) === "<") {
Expand Down
52 changes: 38 additions & 14 deletions test/heredoc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<<EOT
* <EOT
*/
it(`${keyword} <<<EOT\\n<EOT`, () => {
const instruction = DockerfileParser.parse(`${keyword} <<<EOT\n<EOT`).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);
});

/**
* 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`, () => {
Expand Down Expand Up @@ -417,16 +451,6 @@ describe("Heredoc", () => {
assert.strictEqual(heredocs[0].getDelimiterRange(), null);
});

/**
* RUN <<<EOT
* <EOT
*/
it(`${keyword} <<<EOT\\n<EOT`, () => {
const instruction = DockerfileParser.parse(`${keyword} <<<EOT\n<EOT`).getInstructions()[0];
const heredocs = heredocsExtractor(instruction);
assert.strictEqual(heredocs.length, 0);
});

/**
* RUN <<\
* #EOT
Expand Down
6 changes: 6 additions & 0 deletions test/imageTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ describe("ImageTemplate", () => {

dockerfile = DockerfileParser.parse(onbuildPrefix + "RUN <<-\"<EOT\"\n<EOT");
assert.strictEqual(dockerfile.getInstructions().length, 2);

dockerfile = DockerfileParser.parse(onbuildPrefix + "RUN <<'EOT\nEOT\n");
assert.strictEqual(dockerfile.getInstructions().length, 2);

dockerfile = DockerfileParser.parse(onbuildPrefix + "RUN <<\"EOT\nEOT\n");
assert.strictEqual(dockerfile.getInstructions().length, 2);
});
}

Expand Down

0 comments on commit 365d060

Please sign in to comment.