Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
70 changes: 70 additions & 0 deletions docs/rules/require-absolute-workdir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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
# 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,
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 (`$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:

```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
1 change: 1 addition & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("plugin", () => {
"no-sudo",
"prefer-copy-over-add",
"prefer-exec-form",
"require-absolute-workdir",
]);
});

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +28,7 @@ const rules = {
"no-sudo": noSudo,
"prefer-copy-over-add": preferCopyOverAdd,
"prefer-exec-form": preferExecForm,
"require-absolute-workdir": requireAbsoluteWorkdir,
};

const plugin = {
Expand Down Expand Up @@ -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,
},
},
Expand Down
148 changes: 148 additions & 0 deletions src/rules/require-absolute-workdir.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* @fileoverview Tests for the require-absolute-workdir rule.
*/

import { RuleTester } from "eslint";
import { DockerFileLanguage } from "../languages/docker-language.js";
import rule from "./require-absolute-workdir.js";

const ruleTester = new RuleTester({
plugins: {
docker: {
languages: { dockerfile: new DockerFileLanguage() },
},
// 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"',
// 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.
"WORKDIR $HOME",
'WORKDIR "$HOME/app"',
"WORKDIR ${APP_DIR}",
"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',
// 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" }],
},
{
// 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",
errors: [{ messageId: "expectedAbsolute" }],
},
{
// 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: [
{
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" }],
},
{
// 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" }],
},
],
});
Loading
Loading