Skip to content

fix: make custom commands executable without dropping group-write or ACLs, fixes #9031 - #9032

Merged
rfay merged 3 commits into
ddev:mainfrom
steffenmaechtel:20261029_steffenmaechtel_fix_custom_commands_chmod
Oct 31, 2026
Merged

rfay merged 3 commits into
ddev:mainfrom
steffenmaechtel:20261029_steffenmaechtel_fix_custom_commands_chmod

Conversation

@steffenmaechtel

@steffenmaechtel steffenmaechtel commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Short Summary (TL;DR)

Every ddev invocation rewrote custom command files to 0755, stripping group-write and capping POSIX ACLs. The chmod now runs only when the file has no owner-executable bit, so files that are already executable keep their mode. ddev now adds an exec bit wherever the file has a read bit, so commands become executable without losing group-write or the ACL mask.

The Issue

addCustomCommandsFromDir() chmod'ed every custom command file to 0755 on every ddev invocation, including ddev --version and ddev describe. That stripped group-write from plain modes and capped the POSIX ACL mask to r-x, so in shared-group setups teammates lost write access to .ddev/commands/ until someone re-applied chmod g+w, and the next invocation broke it again.

How This PR Solves The Issue

The chmod now runs only when the file has no owner-executable bit. Files that already have owner-exec keep their mode and ACLs; files without any exec bit still become 0755 as before, which keeps the guarantee from #1861 that custom commands are executable. This implements variant 1 from this comment.

Two limits of that approach:

- A command file that arrives without an exec bit, for example 0664 from a git pull under umask 002, is still rewritten to 0755 on its first ddev invocation and loses group-write and the ACL mask once. After that it has owner-exec and is left alone.

- Project commands run in a container are bind-mounted with their host mode and executed by the container user. On Linux, a file with owner-exec but no other-exec (0700, 0750) used with a non-root x-ddev.container-user was made 0755 before and is now left as-is, so that user can no longer execute it. With the default root container user, and for host commands, nothing changes.

A new makeExecutable() helper derives the new mode from the file's current permissions: it adds an exec bit wherever the file has a read bit, and the owner always gets read, since DDEV parses the file's directives. A 0644 command becomes 0755, a 0664 command becomes 0775, and files that are already executable (0775, 0700) are left untouched, so group-write and the POSIX ACL mask survive. The same helper now covers the .ddev/commands/*/autocomplete/ scripts, which were previously reset to 0755 on every run.

On Windows os.Stat reports no executable bits and os.Chmod only toggles the read-only flag, so the condition always holds there and the behavior is the same as before.

Manual Testing Instructions

  1. In a project, create .ddev/commands/host/foo, chmod 0775 it, and run any ddev command from the project directory:
    ddev --version
    stat -c '%a' .ddev/commands/host/foo   # 775 with this PR, 755 without
  2. Repeat the invocation; the mode stays 0775.
  3. With POSIX ACLs, run setfacl -m g:somegroup:rwX .ddev/commands/host/foo and invoke ddev again; getfacl -p keeps mask::rwx.
  4. Create a command without exec bits (chmod 0644) and run ddev; it still becomes 0755.
  5. On Linux, for a container command, set x-ddev.container-user to a non-root user: with 0755 the command runs, with 0700 it fails, as described above.
  6. A 0664 command becomes 0775, and a 0775 script in .ddev/commands/host/autocomplete/<command> stays 0775.

Automated Testing Overview

TestCustomCommandModePreserved in cmd/ddev/cmd/commands_test.go creates a project with two host commands, one at 0775 and one at 0644, runs ddev version inside the project, and asserts the first keeps 0775 while the second becomes 0755 host commands at 0775, 0664, 0644, 0700 and 0000, plus an autocomplete script at 0775, runs ddev version inside the project, and asserts the resulting modes (0775, 0775, 0755, 0700, 0500, 0775). It fails without the change and passes with it. The test is skipped on Windows, which has no exec bits. It does not use SkipUnlessDefaultEnvironment, so it also runs on rootless Docker and Mutagen setups where TestCustomCommands skips.

Release/Deployment Notes

Custom commands that are already executable are no longer rewritten to 0755 on every invocation. Two things to know:

  • Command files without an exec bit are still made 0755 once, which can strip group-write on that first invocation.
  • Files without exec bits gain them based on their read bits: 0644 becomes 0755, 0664 becomes 0775, 0000 becomes 0500.
  • On Linux, container commands executed by a non-root x-ddev.container-user need the file to be executable by that user; such files were previously normalized to 0755 automatically.
  • Web and db containers run as $DDEV_UID, the owner of the file, so container commands with private modes like 0700 keep working.

@steffenmaechtel
steffenmaechtel requested a review from a team as a code owner October 29, 2026 22:43
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Download this PR's build for your OS and architecture with:

ddev utility download-ddev --pr 9032

Or download the artifact for your OS and architecture directly:

See Testing a PR.

Open in GitHub Codespaces

@rfay

rfay commented Oct 30, 2026

Copy link
Copy Markdown
Member

The first thing to do... is see if we even have needed to chmod. I suspect we haven't needed to chmod at all for years.

@rfay

rfay commented Oct 30, 2026

Copy link
Copy Markdown
Member

I do find that the chmod is currently required. I'm not entirely sure what your situation is where you're apparently commiting files without the x mode?

I don't object to this as it is, but I'm pretty sure we could change to using bash to execute the commands on the host like we do in the container (where it doesn't have to have the x).

@stasadev

stasadev commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

I'm not entirely sure what your situation is where you're apparently commiting files without the x mode?

It's committed as 775, but DDEV overrides it to 755 in v1.25.5

we could change to using bash to execute the commands on the host

We have RawCmd: true, which needs +x bit, and not all commands are Bash commands, it could be a PHP or Python script.

@stasadev stasadev 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.

This fixes the reported case. Three changes before merge:

  1. Add x wherever r is set instead of skipping files that already have owner-x, so a 664 command becomes 775 instead of 755. See the inline suggestion, then add this helper before isCustomCommandInArgs:

    // makeExecutable adds an exec bit wherever the file has a read bit, so a
    // custom command runs without losing group-write or the POSIX ACL mask.
    // The owner always gets read, since DDEV parses the file's directives.
    func makeExecutable(path string) {
    	if si, err := os.Stat(path); err == nil {
    		m := si.Mode().Perm() | 0400
    		_ = util.Chmod(path, m|(m&0444)>>2)
    	}
    }
  2. Replace both _ = util.Chmod(autocompletePathOnHost, 0755) with makeExecutable(autocompletePathOnHost). Right now .ddev/commands/*/autocomplete/* is still reset to 755 on every run.

  3. Apply the inline test suggestion, which also covers 664 and an autocomplete script. Then update the PR body: the first limit is gone, and the second one doesn't apply to web and db, which run as $DDEV_UID, the owner of the file, so 700 and 750 still work there.

🤖 Reviewed with assistance from Claude Code

Comment thread cmd/ddev/cmd/commands.go Resolved
Comment thread cmd/ddev/cmd/commands_test.go Resolved
@steffenmaechtel steffenmaechtel changed the title fix: skip custom-command chmod when the file is already executable, fixes #9031 fix: make custom commands executable without dropping group-write or ACLs, fixes #9031 Oct 30, 2026
@steffenmaechtel

steffenmaechtel commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

I do find that the chmod is currently required. I'm not entirely sure what your situation is where you're apparently commiting files without the x mode?

It's committed as 775, but DDEV overrides it to 755 in v1.25.5

Both happen:

  1. Command files without the x mode in the git repository (chmod 644),
    e.g. committed with a Windows GUI tool, while the files live on a WSL
    Ubuntu system accessed via //wsl.localhost/... . Friendly enough, ddev
    changes them to 755 => working.

  2. Files with 775 in git that get overridden by ddev to 755 => this removes
    the group-write permission.
    (On systems where multiple users share the same group, they rely on
    group-write permissions.)

Updated the PR title and body to reflect the changes.

@rfay rfay 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.

Works great. Simple and elegant solution to a very obscure (and probably unsupported) problem.

@rfay
rfay force-pushed the 20261029_steffenmaechtel_fix_custom_commands_chmod branch from 7b71937 to 5bdbdb7 Compare October 31, 2026 03:12
@rfay

rfay commented Oct 31, 2026

Copy link
Copy Markdown
Member

Rebased

@rfay
rfay merged commit e907890 into ddev:main Oct 31, 2026
43 checks passed
@steffenmaechtel
steffenmaechtel deleted the 20261029_steffenmaechtel_fix_custom_commands_chmod branch October 31, 2026 21:02
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in the v1.25.6 release.

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.

Every ddev invocation chmods project custom commands to 0755 — strips group-write and caps POSIX ACL mask

3 participants