Skip to content

sudo: install validated drop-ins instead of editing /etc/sudoers - #27

Open
presto8 wants to merge 1 commit into
mainfrom
fix/sudoers-validation
Open

presto8 wants to merge 1 commit into
mainfrom
fix/sudoers-validation

Conversation

@presto8

@presto8 presto8 commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Two functions edited /etc/sudoers in place with no validation:
enable_sudo_without_password ran 'sed -i.bak' against the %sudo line,
and pass_http_thru_sudo appended an env_keep Defaults line via
'sudo tee -a'. A syntax error introduced by either -- a sed pattern
that matches unexpectedly on a future Ubuntu, a partially written
append -- makes sudo refuse to run at all, and the .bak is then only
recoverable from a root shell or recovery boot.

Write both rules to /etc/sudoers.d/ instead, via a helper that:

  • confirms /etc/sudoers actually has an includedir directive
  • runs 'visudo -cf' on the candidate file and aborts if it fails
  • installs with 'install -m 0440 -o root -g root', which is atomic

This is also idempotent by construction, replacing the grep-based
guards. Verified both generated snippets parse with visudo -cf.

Machines provisioned by an earlier version keep the inline edits in
/etc/sudoers; the drop-ins are equivalent, so those lines can be
removed by hand with visudo.


🤖 Generated with Claude Code

Two functions edited /etc/sudoers in place with no validation:
enable_sudo_without_password ran 'sed -i.bak' against the %sudo line,
and pass_http_thru_sudo appended an env_keep Defaults line via
'sudo tee -a'. A syntax error introduced by either -- a sed pattern
that matches unexpectedly on a future Ubuntu, a partially written
append -- makes sudo refuse to run at all, and the .bak is then only
recoverable from a root shell or recovery boot.

Write both rules to /etc/sudoers.d/ instead, via a helper that:
  - confirms /etc/sudoers actually has an includedir directive
  - runs 'visudo -cf' on the candidate file and aborts if it fails
  - installs with 'install -m 0440 -o root -g root', which is atomic

This is also idempotent by construction, replacing the grep-based
guards. Verified both generated snippets parse with visudo -cf.

Machines provisioned by an earlier version keep the inline edits in
/etc/sudoers; the drop-ins are equivalent, so those lines can be
removed by hand with visudo.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new helper’s includedir detection regex is incorrect/too strict, and the drop-in installation path should avoid non-atomic overwrites to prevent partial sudoers files if interrupted.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR hardens sudo configuration changes in setup by moving from in-place edits of /etc/sudoers to validated drop-in files under /etc/sudoers.d, reducing the risk of locking sudo out due to syntax errors or partial writes.

Changes:

  • Added _install_sudoers_snippet helper to validate generated sudoers content with visudo -cf before installing.
  • Updated enable_sudo_without_password to install a %sudo ... NOPASSWD:SETENV rule via a managed drop-in.
  • Updated pass_http_thru_sudo to install an env_keep Defaults rule via a managed drop-in.
File summaries
File Description
setup Replaces direct /etc/sudoers edits with validated, managed sudoers drop-ins.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread setup
Comment on lines +185 to +186
if ! sudo grep -qE '^[@#]includedir[[:space:]]+/etc/sudoers.d' /etc/sudoers; then
die "/etc/sudoers does not include /etc/sudoers.d; refusing to edit sudoers"
Comment thread setup
Comment on lines +199 to +200
sudo install -o root -g root -m 0440 "$tmp" "/etc/sudoers.d/$name"
rm -f "$tmp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants