From a015864a966d807adff733e353ce7a5f3812a701 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Tue, 10 Nov 2026 17:32:56 -0500 Subject: [PATCH 1/8] feat: add require-absolute-workdir rule Co-Authored-By: Claude Fable 5.2 --- README.md | 1 + docs/rules/require-absolute-workdir.md | 63 ++++++++++++++++++ src/index.spec.ts | 1 + src/index.ts | 3 + src/rules/require-absolute-workdir.spec.ts | 58 +++++++++++++++++ src/rules/require-absolute-workdir.ts | 75 ++++++++++++++++++++++ 6 files changed, 201 insertions(+) create mode 100644 docs/rules/require-absolute-workdir.md create mode 100644 src/rules/require-absolute-workdir.spec.ts create mode 100644 src/rules/require-absolute-workdir.ts diff --git a/README.md b/README.md index 2d0ee61..af45bb4 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ rule offers an editor suggestion instead. | [`no-sudo`](./docs/rules/no-sudo.md) | DL3004 | Don't use `sudo` in `RUN`; switch users with `USER` | | error | | [`prefer-copy-over-add`](./docs/rules/prefer-copy-over-add.md) | DL3020 | Use `COPY` unless you need `ADD`'s URL or archive handling | 💡 | error | | [`prefer-exec-form`](./docs/rules/prefer-exec-form.md) | DL3025 | Use the exec (JSON array) form for `CMD` and `ENTRYPOINT` | ✅ | warn | +| [`require-absolute-workdir`](./docs/rules/require-absolute-workdir.md) | DL3000 | `WORKDIR` paths must be absolute | | error | Fixable rules only autofix when the rewrite is unambiguous — for example, `prefer-exec-form` leaves commands that rely on shell features alone. diff --git a/docs/rules/require-absolute-workdir.md b/docs/rules/require-absolute-workdir.md new file mode 100644 index 0000000..a31cbe4 --- /dev/null +++ b/docs/rules/require-absolute-workdir.md @@ -0,0 +1,63 @@ +# require-absolute-workdir + +Require WORKDIR paths to be absolute. + +## Background + +A relative WORKDIR is resolved against the previous WORKDIR, so its +meaning depends on everything that came before it — including WORKDIR +instructions inherited from the base image: + +```dockerfile +FROM some-base # whose WORKDIR might be anything +WORKDIR app # …/app, wherever that is +``` + +If the base image changes its working directory, the build breaks or, +worse, silently writes files to the wrong place. An absolute path always +means the same thing. + +## Rule Details + +This rule warns when a WORKDIR path is not absolute. Unix paths starting +with `/`, Windows paths starting with a drive letter (`C:/`), and paths +starting with a variable reference (`$`, `${...}`, or `%...%`) are +accepted; variable values cannot be checked statically. + +Examples of **incorrect** code for this rule: + +```dockerfile +# eslint docker/require-absolute-workdir: "error" + +FROM node:20 +WORKDIR app +WORKDIR ./app +WORKDIR usr/src/app +``` + +Examples of **correct** code for this rule: + +```dockerfile +# eslint docker/require-absolute-workdir: "error" + +FROM node:20 +WORKDIR /usr/src/app +WORKDIR $APP_HOME +WORKDIR C:/app +``` + +## Options + +This rule has no options. + +## When Not to Use It + +If you deliberately use relative WORKDIR instructions to build paths in +steps (`WORKDIR /app` followed by `WORKDIR src`), you can disable this +rule, though writing the full path in each WORKDIR is clearer. + +## Prior Art + +- [hadolint DL3000](https://github.com/hadolint/hadolint/wiki/DL3000) +- [`WorkdirRelativePath`](https://docs.docker.com/reference/build-checks/workdir-relative-path/) + build check diff --git a/src/index.spec.ts b/src/index.spec.ts index 5ab43f2..ac80fc8 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -38,6 +38,7 @@ describe("plugin", () => { "no-sudo", "prefer-copy-over-add", "prefer-exec-form", + "require-absolute-workdir", ]); }); diff --git a/src/index.ts b/src/index.ts index 43ba2e5..9b725db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ import noMaintainer from "./rules/no-maintainer.js"; import noSudo from "./rules/no-sudo.js"; import preferCopyOverAdd from "./rules/prefer-copy-over-add.js"; import preferExecForm from "./rules/prefer-exec-form.js"; +import requireAbsoluteWorkdir from "./rules/require-absolute-workdir.js"; const rules = { "consistent-instruction-casing": consistentInstructionCasing, @@ -27,6 +28,7 @@ const rules = { "no-sudo": noSudo, "prefer-copy-over-add": preferCopyOverAdd, "prefer-exec-form": preferExecForm, + "require-absolute-workdir": requireAbsoluteWorkdir, }; const plugin = { @@ -59,6 +61,7 @@ const plugin = { "docker/no-sudo": "error", "docker/prefer-copy-over-add": "error", "docker/prefer-exec-form": "warn", + "docker/require-absolute-workdir": "error", } as const, }, }, diff --git a/src/rules/require-absolute-workdir.spec.ts b/src/rules/require-absolute-workdir.spec.ts new file mode 100644 index 0000000..2331c46 --- /dev/null +++ b/src/rules/require-absolute-workdir.spec.ts @@ -0,0 +1,58 @@ +/** + * @fileoverview Tests for the require-absolute-workdir rule. + */ + +import { RuleTester } from "eslint"; +import { DockerLanguage } from "../languages/docker-language.js"; +import rule from "./require-absolute-workdir.js"; + +const ruleTester = new RuleTester({ + plugins: { + docker: { + languages: { dockerfile: new DockerLanguage() }, + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- plugin shape is validated by ESLint at runtime. + } as any, + language: "docker/dockerfile", +}); + +ruleTester.run("require-absolute-workdir", rule as never, { + valid: [ + "WORKDIR /app", + "WORKDIR /usr/src/app", + 'WORKDIR "/quoted path"', + // Windows-style absolute paths. + "WORKDIR C:/app", + // Variables cannot be checked. + "WORKDIR $HOME", + "WORKDIR ${APP_DIR}", + "WORKDIR %APP_HOME%", + "WORKDIR", + ], + invalid: [ + { + code: "WORKDIR app", + errors: [ + { + messageId: "expectedAbsolute", + data: { path: "app" }, + line: 1, + column: 9, + endColumn: 12, + }, + ], + }, + { + code: "WORKDIR ./app", + errors: [{ messageId: "expectedAbsolute" }], + }, + { + code: "WORKDIR usr/src/app", + errors: [{ messageId: "expectedAbsolute" }], + }, + { + code: 'WORKDIR "relative dir"', + errors: [{ messageId: "expectedAbsolute" }], + }, + ], +}); diff --git a/src/rules/require-absolute-workdir.ts b/src/rules/require-absolute-workdir.ts new file mode 100644 index 0000000..cb7fffd --- /dev/null +++ b/src/rules/require-absolute-workdir.ts @@ -0,0 +1,75 @@ +/** + * @fileoverview Rule to require WORKDIR paths to be absolute. Relative + * paths depend on the previous WORKDIR and break silently when earlier + * instructions change. + */ + +//------------------------------------------------------------------------------ +// Imports +//------------------------------------------------------------------------------ + +import type { DockerRuleDefinition } from "../types.js"; + +//------------------------------------------------------------------------------ +// Helpers +//------------------------------------------------------------------------------ + +const WINDOWS_ABSOLUTE = /^[A-Za-z]:[\\/]/u; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +const rule: DockerRuleDefinition<{ MessageIds: "expectedAbsolute" }> = { + meta: { + type: "problem", + docs: { + description: "Require WORKDIR paths to be absolute", + recommended: true, + url: "https://github.com/eslint/docker/blob/main/docs/rules/require-absolute-workdir.md", + }, + schema: [], + messages: { + expectedAbsolute: + "Use an absolute path for WORKDIR instead of '{{path}}'.", + }, + }, + + create(context) { + return { + WorkdirInstruction(node) { + if (!node.path) { + return; + } + + // Remove surrounding quotes, if any. A quoted path with + // spaces is split across tokens, so the quotes are stripped + // independently. + const path = node.path.value + .replace(/^["']/u, "") + .replace(/["']$/u, ""); + + if (path === "") { + return; + } + + const isAbsolute = + path.startsWith("/") || + WINDOWS_ABSOLUTE.test(path) || + // Variable references cannot be checked. + path.startsWith("$") || + path.startsWith("%"); + + if (!isAbsolute) { + context.report({ + node: node.path, + messageId: "expectedAbsolute", + data: { path }, + }); + } + }, + }; + }, +}; + +export default rule; From a18d9b92010ccd89e7961ca50a2796b9858df1c6 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 11 Nov 2026 09:23:52 -0500 Subject: [PATCH 2/8] fix: make WORKDIR absolute-path checks quote-aware - Check the whole WORKDIR operand, so leading whitespace inside quotes is no longer dropped. - Treat single-quoted variables as literal relative paths. - Only exempt complete %NAME% Windows variable references. Co-Authored-By: Claude Opus 5.5 --- src/rules/require-absolute-workdir.spec.ts | 21 +++++++++++++++++++++ src/rules/require-absolute-workdir.ts | 19 +++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/rules/require-absolute-workdir.spec.ts b/src/rules/require-absolute-workdir.spec.ts index 2331c46..2b5a470 100644 --- a/src/rules/require-absolute-workdir.spec.ts +++ b/src/rules/require-absolute-workdir.spec.ts @@ -25,6 +25,7 @@ ruleTester.run("require-absolute-workdir", rule as never, { "WORKDIR C:/app", // Variables cannot be checked. "WORKDIR $HOME", + 'WORKDIR "$HOME/app"', "WORKDIR ${APP_DIR}", "WORKDIR %APP_HOME%", "WORKDIR", @@ -54,5 +55,25 @@ ruleTester.run("require-absolute-workdir", rule as never, { code: 'WORKDIR "relative dir"', errors: [{ messageId: "expectedAbsolute" }], }, + { + // Leading whitespace inside quotes is part of the path. + code: 'WORKDIR " relative dir"', + errors: [ + { + messageId: "expectedAbsolute", + data: { path: " relative dir" }, + }, + ], + }, + { + // Single quotes suppress variable substitution. + code: "WORKDIR '$APP_HOME'", + errors: [{ messageId: "expectedAbsolute" }], + }, + { + // A lone percent sign is not a variable reference. + code: "WORKDIR %cache", + errors: [{ messageId: "expectedAbsolute" }], + }, ], }); diff --git a/src/rules/require-absolute-workdir.ts b/src/rules/require-absolute-workdir.ts index cb7fffd..f37ba43 100644 --- a/src/rules/require-absolute-workdir.ts +++ b/src/rules/require-absolute-workdir.ts @@ -16,6 +16,9 @@ import type { DockerRuleDefinition } from "../types.js"; const WINDOWS_ABSOLUTE = /^[A-Za-z]:[\\/]/u; +/** A leading `%NAME%` Windows variable reference. */ +const WINDOWS_VARIABLE = /^%[^%\s]+%/u; + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -42,12 +45,12 @@ const rule: DockerRuleDefinition<{ MessageIds: "expectedAbsolute" }> = { return; } - // Remove surrounding quotes, if any. A quoted path with - // spaces is split across tokens, so the quotes are stripped - // independently. - const path = node.path.value - .replace(/^["']/u, "") - .replace(/["']$/u, ""); + // Remove surrounding quotes, if any. Single quotes suppress + // variable substitution, so their contents are literal. + const raw = node.path.value; + const quote = /^(["'])[\s\S]*\1$/u.exec(raw)?.[1]; + const path = quote ? raw.slice(1, -1) : raw; + const expands = quote !== "'"; if (path === "") { return; @@ -57,8 +60,8 @@ const rule: DockerRuleDefinition<{ MessageIds: "expectedAbsolute" }> = { path.startsWith("/") || WINDOWS_ABSOLUTE.test(path) || // Variable references cannot be checked. - path.startsWith("$") || - path.startsWith("%"); + (expands && + (path.startsWith("$") || WINDOWS_VARIABLE.test(path))); if (!isAbsolute) { context.report({ From f3b94185464ec2f8c0c17628de99a385612aca59 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 11 Nov 2026 09:25:16 -0500 Subject: [PATCH 3/8] docs: move explanatory comments off instruction lines Dockerfile instructions do not support trailing comments. Co-Authored-By: Claude Opus 5.5 --- docs/rules/require-absolute-workdir.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/rules/require-absolute-workdir.md b/docs/rules/require-absolute-workdir.md index a31cbe4..c5a402f 100644 --- a/docs/rules/require-absolute-workdir.md +++ b/docs/rules/require-absolute-workdir.md @@ -9,8 +9,10 @@ meaning depends on everything that came before it — including WORKDIR instructions inherited from the base image: ```dockerfile -FROM some-base # whose WORKDIR might be anything -WORKDIR app # …/app, wherever that is +# a base image whose WORKDIR might be anything +FROM some-base +# …/app, wherever that is +WORKDIR app ``` If the base image changes its working directory, the build breaks or, From ece2063b4b6777a5ccab710b217ce56a239e1253 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 11 Nov 2026 11:14:19 -0500 Subject: [PATCH 4/8] docs: format Dockerfile instructions as code Co-Authored-By: Claude Opus 5.5 --- docs/rules/require-absolute-workdir.md | 12 ++++++------ src/rules/require-absolute-workdir.ts | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/rules/require-absolute-workdir.md b/docs/rules/require-absolute-workdir.md index c5a402f..d6b940e 100644 --- a/docs/rules/require-absolute-workdir.md +++ b/docs/rules/require-absolute-workdir.md @@ -1,11 +1,11 @@ # require-absolute-workdir -Require WORKDIR paths to be absolute. +Require `WORKDIR` paths to be absolute. ## Background -A relative WORKDIR is resolved against the previous WORKDIR, so its -meaning depends on everything that came before it — including WORKDIR +A relative `WORKDIR` is resolved against the previous `WORKDIR`, so its +meaning depends on everything that came before it — including `WORKDIR` instructions inherited from the base image: ```dockerfile @@ -21,7 +21,7 @@ means the same thing. ## Rule Details -This rule warns when a WORKDIR path is not absolute. Unix paths starting +This rule warns when a `WORKDIR` path is not absolute. Unix paths starting with `/`, Windows paths starting with a drive letter (`C:/`), and paths starting with a variable reference (`$`, `${...}`, or `%...%`) are accepted; variable values cannot be checked statically. @@ -54,9 +54,9 @@ This rule has no options. ## When Not to Use It -If you deliberately use relative WORKDIR instructions to build paths in +If you deliberately use relative `WORKDIR` instructions to build paths in steps (`WORKDIR /app` followed by `WORKDIR src`), you can disable this -rule, though writing the full path in each WORKDIR is clearer. +rule, though writing the full path in each `WORKDIR` is clearer. ## Prior Art diff --git a/src/rules/require-absolute-workdir.ts b/src/rules/require-absolute-workdir.ts index f37ba43..afc4269 100644 --- a/src/rules/require-absolute-workdir.ts +++ b/src/rules/require-absolute-workdir.ts @@ -27,7 +27,7 @@ const rule: DockerRuleDefinition<{ MessageIds: "expectedAbsolute" }> = { meta: { type: "problem", docs: { - description: "Require WORKDIR paths to be absolute", + description: "Require `WORKDIR` paths to be absolute", recommended: true, url: "https://github.com/eslint/docker/blob/main/docs/rules/require-absolute-workdir.md", }, From 74cea214c2d71eea2743d77881fe4ab7d7779866 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Tue, 17 Nov 2026 10:32:39 -0500 Subject: [PATCH 5/8] refactor: use DockerFileLanguage in rule tests Co-Authored-By: Claude Opus 5.5 --- src/rules/require-absolute-workdir.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rules/require-absolute-workdir.spec.ts b/src/rules/require-absolute-workdir.spec.ts index 2b5a470..b2a4552 100644 --- a/src/rules/require-absolute-workdir.spec.ts +++ b/src/rules/require-absolute-workdir.spec.ts @@ -3,13 +3,13 @@ */ import { RuleTester } from "eslint"; -import { DockerLanguage } from "../languages/docker-language.js"; +import { DockerFileLanguage } from "../languages/docker-language.js"; import rule from "./require-absolute-workdir.js"; const ruleTester = new RuleTester({ plugins: { docker: { - languages: { dockerfile: new DockerLanguage() }, + languages: { dockerfile: new DockerFileLanguage() }, }, // eslint-disable-next-line @typescript-eslint/no-explicit-any -- plugin shape is validated by ESLint at runtime. } as any, From 51bf2486ddf4351762bcb84bdbd164d6ef1fa5fb Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 18 Nov 2026 10:40:32 -0500 Subject: [PATCH 6/8] fix: remove quotes across the WORKDIR operand before checking it Co-Authored-By: Claude Opus 5.5 --- src/rules/require-absolute-workdir.spec.ts | 17 ++++++++++ src/rules/require-absolute-workdir.ts | 38 ++++++++++++++++++---- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/rules/require-absolute-workdir.spec.ts b/src/rules/require-absolute-workdir.spec.ts index b2a4552..7eb686a 100644 --- a/src/rules/require-absolute-workdir.spec.ts +++ b/src/rules/require-absolute-workdir.spec.ts @@ -21,6 +21,10 @@ ruleTester.run("require-absolute-workdir", rule as never, { "WORKDIR /app", "WORKDIR /usr/src/app", 'WORKDIR "/quoted path"', + // Quoted prefixes combine with the rest of the operand. + 'WORKDIR "/opt/my app"/src', + "WORKDIR '/opt'/src", + 'WORKDIR ""/app', // Windows-style absolute paths. "WORKDIR C:/app", // Variables cannot be checked. @@ -31,6 +35,19 @@ ruleTester.run("require-absolute-workdir", rule as never, { "WORKDIR", ], invalid: [ + { + // Single-quoted variable references are literal. + code: "WORKDIR '$HOME'/app", + errors: [ + { messageId: "expectedAbsolute", data: { path: "$HOME/app" } }, + ], + }, + { + code: 'WORKDIR "my app"/src', + errors: [ + { messageId: "expectedAbsolute", data: { path: "my app/src" } }, + ], + }, { code: "WORKDIR app", errors: [ diff --git a/src/rules/require-absolute-workdir.ts b/src/rules/require-absolute-workdir.ts index afc4269..f94d87d 100644 --- a/src/rules/require-absolute-workdir.ts +++ b/src/rules/require-absolute-workdir.ts @@ -19,6 +19,34 @@ const WINDOWS_ABSOLUTE = /^[A-Za-z]:[\\/]/u; /** A leading `%NAME%` Windows variable reference. */ const WINDOWS_VARIABLE = /^%[^%\s]+%/u; +/** + * Removes shell-style quotes anywhere in a WORKDIR operand, the way Docker + * combines `"/opt/my app"/src` into `/opt/my app/src`. Returns the + * resulting path and whether its first character came from inside single + * quotes, where variable references are literal. + */ +function unquote(raw: string): { path: string; literalStart: boolean } { + let path = ""; + let quote: string | null = null; + let literalStart = false; + + for (const character of raw) { + if (quote === null && (character === '"' || character === "'")) { + quote = character; + } else if (character === quote) { + quote = null; + } else { + if (path === "") { + literalStart = quote === "'"; + } + + path += character; + } + } + + return { path, literalStart }; +} + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -45,12 +73,10 @@ const rule: DockerRuleDefinition<{ MessageIds: "expectedAbsolute" }> = { return; } - // Remove surrounding quotes, if any. Single quotes suppress - // variable substitution, so their contents are literal. - const raw = node.path.value; - const quote = /^(["'])[\s\S]*\1$/u.exec(raw)?.[1]; - const path = quote ? raw.slice(1, -1) : raw; - const expands = quote !== "'"; + // Remove quotes. Single quotes suppress variable + // substitution, so their contents are literal. + const { path, literalStart } = unquote(node.path.value); + const expands = !literalStart; if (path === "") { return; From ec9ae90c49ea716ba58d76966dc7cfd02dc329b3 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Fri, 20 Nov 2026 10:45:47 -0500 Subject: [PATCH 7/8] fix: resolve more WORKDIR operands like Docker does - Remove Dockerfile escapes before checking the path - Report %NAME% paths, which BuildKit does not expand - Report an explicit empty path - Treat only $ followed by a variable name as a reference - Reject drive letters in stages that explicitly target Linux - Cover comment lines inside continued operands Co-Authored-By: Claude Opus 5.5 --- docs/rules/require-absolute-workdir.md | 9 ++- src/rules/require-absolute-workdir.spec.ts | 39 ++++++++- src/rules/require-absolute-workdir.ts | 93 ++++++++++++++++------ 3 files changed, 113 insertions(+), 28 deletions(-) diff --git a/docs/rules/require-absolute-workdir.md b/docs/rules/require-absolute-workdir.md index d6b940e..9b50c43 100644 --- a/docs/rules/require-absolute-workdir.md +++ b/docs/rules/require-absolute-workdir.md @@ -23,8 +23,13 @@ means the same thing. This rule warns when a `WORKDIR` path is not absolute. Unix paths starting with `/`, Windows paths starting with a drive letter (`C:/`), and paths -starting with a variable reference (`$`, `${...}`, or `%...%`) are -accepted; variable values cannot be checked statically. +starting with a variable reference (`$NAME` or `${...}`) are accepted; +variable values cannot be checked statically. Quotes and Dockerfile escapes +are removed before the check. Docker does not expand `%NAME%` in `WORKDIR`, +so such paths are reported, as is an empty path (`WORKDIR ""`), which +keeps the previous working directory. A drive letter is not accepted in a +stage that explicitly targets Linux (`FROM --platform=linux/...`), where it +is an ordinary relative path component. Examples of **incorrect** code for this rule: diff --git a/src/rules/require-absolute-workdir.spec.ts b/src/rules/require-absolute-workdir.spec.ts index 7eb686a..bb113bc 100644 --- a/src/rules/require-absolute-workdir.spec.ts +++ b/src/rules/require-absolute-workdir.spec.ts @@ -31,10 +31,47 @@ ruleTester.run("require-absolute-workdir", rule as never, { "WORKDIR $HOME", 'WORKDIR "$HOME/app"', "WORKDIR ${APP_DIR}", - "WORKDIR %APP_HOME%", "WORKDIR", + // Dockerfile escapes are removed before the check. + "WORKDIR \\/app", + "# escape=`\nFROM scratch\nWORKDIR `/app", + // Comment lines inside a continuation are not part of the path. + 'WORKDIR ""\\\n# comment\n/app', + // Drive letters are absolute when the stage may target Windows. + "FROM --platform=windows/amd64 example/app\nWORKDIR C:/app", + "WORKDIR C:\\app", ], invalid: [ + { + // BuildKit does not expand %NAME% in WORKDIR. + code: "WORKDIR %APP_HOME%", + errors: [{ messageId: "expectedAbsolute" }], + }, + { + // An empty operand keeps the previous working directory. + code: 'WORKDIR ""', + errors: [{ messageId: "expectedAbsolute", data: { path: "" } }], + }, + { + // A drive letter is a relative path component on Linux. + code: "FROM --platform=linux/amd64 alpine\nWORKDIR C:/app", + errors: [{ messageId: "expectedAbsolute" }], + }, + { + // A $ without a variable name is kept literally. + code: "WORKDIR $/cache", + errors: [{ messageId: "expectedAbsolute" }], + }, + { + // $'...' is not expanded by Docker. + code: "WORKDIR $'HOME'/cache", + errors: [{ messageId: "expectedAbsolute" }], + }, + { + // An escaped $ is literal. + code: "WORKDIR \\$HOME/app", + errors: [{ messageId: "expectedAbsolute" }], + }, { // Single-quoted variable references are literal. code: "WORKDIR '$HOME'/app", diff --git a/src/rules/require-absolute-workdir.ts b/src/rules/require-absolute-workdir.ts index f94d87d..1ca1f1b 100644 --- a/src/rules/require-absolute-workdir.ts +++ b/src/rules/require-absolute-workdir.ts @@ -16,35 +16,54 @@ import type { DockerRuleDefinition } from "../types.js"; const WINDOWS_ABSOLUTE = /^[A-Za-z]:[\\/]/u; -/** A leading `%NAME%` Windows variable reference. */ -const WINDOWS_VARIABLE = /^%[^%\s]+%/u; +/** A character that can start a variable name after `$`. */ +const VARIABLE_START = /^[A-Za-z_{]/u; /** - * Removes shell-style quotes anywhere in a WORKDIR operand, the way Docker - * combines `"/opt/my app"/src` into `/opt/my app/src`. Returns the - * resulting path and whether its first character came from inside single - * quotes, where variable references are literal. + * Removes quotes and Dockerfile escapes anywhere in a WORKDIR operand, the + * way Docker combines `"/opt/my app"/src` into `/opt/my app/src` and + * `\/app` into `/app`. Returns the resulting path and whether it starts + * with a variable reference that Docker expands: an unescaped `$` outside + * single quotes, directly followed by a variable name or `{`. */ -function unquote(raw: string): { path: string; literalStart: boolean } { +function unquote( + raw: string, + escapeCharacter: string, +): { path: string; startsWithVariable: boolean } { let path = ""; let quote: string | null = null; - let literalStart = false; - - for (const character of raw) { - if (quote === null && (character === '"' || character === "'")) { + let startsWithVariable = false; + + for (let i = 0; i < raw.length; i++) { + const character = raw[i] as string; + + if ( + character === escapeCharacter && + quote !== "'" && + i + 1 < raw.length + ) { + // The escaped character is literal, including `$`. + i++; + path += raw[i]; + } else if (quote === null && (character === '"' || character === "'")) { quote = character; } else if (character === quote) { quote = null; } else { - if (path === "") { - literalStart = quote === "'"; + if ( + path === "" && + character === "$" && + quote !== "'" && + VARIABLE_START.test(raw.slice(i + 1)) + ) { + startsWithVariable = true; } path += character; } } - return { path, literalStart }; + return { path, startsWithVariable }; } //------------------------------------------------------------------------------ @@ -67,27 +86,51 @@ const rule: DockerRuleDefinition<{ MessageIds: "expectedAbsolute" }> = { }, create(context) { + const { sourceCode } = context; + const escapeCharacter = + sourceCode.ast.directives.find( + directive => directive.name.toLowerCase() === "escape", + )?.value ?? "\\"; + + // Whether the current stage explicitly targets Linux, where a + // drive letter is an ordinary relative path component. + let linuxStage = false; + return { + FromInstruction(node) { + const platform = node.flags.find( + flag => flag.name === "platform", + ); + + linuxStage = /^linux(?:\/|$)/iu.test(platform?.value ?? ""); + }, + WorkdirInstruction(node) { if (!node.path) { return; } - // Remove quotes. Single quotes suppress variable - // substitution, so their contents are literal. - const { path, literalStart } = unquote(node.path.value); - const expands = !literalStart; - - if (path === "") { - return; - } + // Remove quotes and escapes. Single quotes suppress + // variable substitution, so their contents are literal. + const { path, startsWithVariable } = unquote( + node.path.value, + escapeCharacter, + ); + // An empty operand (`WORKDIR ""`) keeps the previous working + // directory, which is as fragile as a relative path. const isAbsolute = path.startsWith("/") || - WINDOWS_ABSOLUTE.test(path) || + // Windows paths such as `C:\app` are often written with + // the default escape character, so also check the + // operand without escape processing. + (!linuxStage && + (WINDOWS_ABSOLUTE.test(path) || + WINDOWS_ABSOLUTE.test( + unquote(node.path.value, "").path, + ))) || // Variable references cannot be checked. - (expands && - (path.startsWith("$") || WINDOWS_VARIABLE.test(path))); + startsWithVariable; if (!isAbsolute) { context.report({ From 3a9fa5e620f5f609373abf03b55db1c9ab47e34d Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Mon, 23 Nov 2026 14:05:03 -0500 Subject: [PATCH 8/8] fix: keep backslashes before ordinary characters in quoted WORKDIR paths Co-Authored-By: Claude Opus 5.5 --- src/rules/require-absolute-workdir.spec.ts | 15 +++++++++++++++ src/rules/require-absolute-workdir.ts | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/rules/require-absolute-workdir.spec.ts b/src/rules/require-absolute-workdir.spec.ts index bb113bc..ef81052 100644 --- a/src/rules/require-absolute-workdir.spec.ts +++ b/src/rules/require-absolute-workdir.spec.ts @@ -34,6 +34,7 @@ ruleTester.run("require-absolute-workdir", rule as never, { "WORKDIR", // Dockerfile escapes are removed before the check. "WORKDIR \\/app", + 'WORKDIR "C:\\app"', "# escape=`\nFROM scratch\nWORKDIR `/app", // Comment lines inside a continuation are not part of the path. 'WORKDIR ""\\\n# comment\n/app', @@ -67,6 +68,20 @@ ruleTester.run("require-absolute-workdir", rule as never, { code: "WORKDIR $'HOME'/cache", errors: [{ messageId: "expectedAbsolute" }], }, + { + // Inside double quotes, the backslash before `/` is kept. + code: 'WORKDIR "\\/app"', + errors: [ + { messageId: "expectedAbsolute", data: { path: "\\/app" } }, + ], + }, + { + // Inside double quotes, an escaped $ is still literal. + code: 'WORKDIR "\\$HOME/app"', + errors: [ + { messageId: "expectedAbsolute", data: { path: "$HOME/app" } }, + ], + }, { // An escaped $ is literal. code: "WORKDIR \\$HOME/app", diff --git a/src/rules/require-absolute-workdir.ts b/src/rules/require-absolute-workdir.ts index 1ca1f1b..f58d54d 100644 --- a/src/rules/require-absolute-workdir.ts +++ b/src/rules/require-absolute-workdir.ts @@ -37,10 +37,16 @@ function unquote( for (let i = 0; i < raw.length; i++) { const character = raw[i] as string; + const next = raw[i + 1]; + if ( character === escapeCharacter && - quote !== "'" && - i + 1 < raw.length + next !== undefined && + (quote === null || + // Inside double quotes, the escape character is kept + // unless it escapes `"`, `$`, or itself. + (quote === '"' && + (next === '"' || next === "$" || next === escapeCharacter))) ) { // The escaped character is literal, including `$`. i++;