Skip to content

cmd: use os.Root for data directories in compact, receive, and sidecar#8726

Closed
Retr0-XD wants to merge 1 commit into
thanos-io:mainfrom
Retr0-XD:feat/use-os-root-for-data-dirs-8103
Closed

cmd: use os.Root for data directories in compact, receive, and sidecar#8726
Retr0-XD wants to merge 1 commit into
thanos-io:mainfrom
Retr0-XD:feat/use-os-root-for-data-dirs-8103

Conversation

@Retr0-XD

Copy link
Copy Markdown

What this PR does / Why we need it

Closes #8103.

Uses os.Root (introduced in Go 1.24) to open restricted file-system roots for the configured data directories in the Compactor, Receiver, and Sidecar components. File operations performed through an *os.Root are bounded to the named directory and cannot escape via path traversal (e.g. ../). This prevents any future code from accidentally introducing a path-traversal vulnerability by accepting malicious or malformed directory input.

Changes

cmd/thanos/compact.go

  • Ensure conf.dataDir exists (os.MkdirAll) before opening the root (no-op when the directory already exists).
  • Open *os.Root for conf.dataDir and defer its close.
  • Replace the two os.MkdirAll calls for the compact and downsample subdirs with dataRoot.Mkdir, so those operations are also root-restricted.

cmd/thanos/receive.go

  • Ensure conf.dataDir exists with os.MkdirAll at startup (no-op when it already exists).
  • Open *os.Root for conf.dataDir and defer its close.

cmd/thanos/sidecar.go

  • Add "os" to imports.
  • Open *os.Root for conf.tsdb.path immediately after the existing IsWALDirAccessible check (which confirms the directory is accessible), and defer its close.

Which issue(s) this PR fixes

Fixes #8103


AI disclosure: GitHub Copilot was used during implementation. I fully understand all changes made in this PR.

@Retr0-XD

Copy link
Copy Markdown
Author

Hi @bwplotka @GiedriusS — would appreciate a review when you get a chance!

@Retr0-XD
Retr0-XD force-pushed the feat/use-os-root-for-data-dirs-8103 branch from 8f05536 to f7e12d3 Compare March 24, 2026 18:34
Use os.Root (introduced in Go 1.24) to open restricted file-system
roots for the data directories in the Compactor, Receiver, and Sidecar
components. File operations performed through os.Root are bounded to
the named directory and cannot escape via path traversal (e.g. '../').

Compactor (compact.go):
  - Ensure conf.dataDir exists before opening the root.
  - Replace os.MkdirAll for the 'compact' and 'downsample' subdirs with
    dataRoot.Mkdir so those operations are also root-restricted.

Receiver (receive.go):
  - Ensure conf.dataDir exists with os.MkdirAll before opening the
    root; this is a no-op when the directory already exists.

Sidecar (sidecar.go):
  - Open a restricted root for conf.tsdb.path immediately after the
    existing IsWALDirAccessible validation confirms the path is present.

Fixes thanos-io#8103

Signed-off-by: Retr0-XD <[email protected]>
@Retr0-XD
Retr0-XD force-pushed the feat/use-os-root-for-data-dirs-8103 branch from f7e12d3 to db95c01 Compare March 24, 2026 18:34
@Retr0-XD

Retr0-XD commented Mar 25, 2026

Copy link
Copy Markdown
Author

@GiedriusS @bwplotka can you review this PR :)

@GiedriusS GiedriusS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good first issues are for humans, not llms. I don't think you understood the changes that the bot made for you.

@GiedriusS GiedriusS closed this Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use the new os.Root type

2 participants