Skip to content

🛡️ Sentinel: Fix insecure SSH key creation permissions#29

Open
kidchenko wants to merge 1 commit intomainfrom
sentinel-fix-ssh-umask-15876976491062478903
Open

🛡️ Sentinel: Fix insecure SSH key creation permissions#29
kidchenko wants to merge 1 commit intomainfrom
sentinel-fix-ssh-umask-15876976491062478903

Conversation

@kidchenko
Copy link
Owner

@kidchenko kidchenko commented Feb 13, 2026

🛡️ Sentinel: [CRITICAL/HIGH] Fix Secure SSH Key Creation

🚨 Severity: HIGH
💡 Vulnerability: Race condition (TOCTOU) in SSH key creation. Private keys were created with default umask permissions (often 644/world-readable) before being restricted with chmod 600. This left a brief window where other users on the system could read the private key.
🎯 Impact: Potential exposure of private SSH keys to other local users on a shared machine.
🔧 Fix:

  • Wrapped directory creation mkdir -p in a subshell with umask 077.
  • Wrapped file writing op read ... > file in a subshell with umask 077.
  • Added rm -f for existing files to ensure new files are created with correct permissions rather than truncating existing files with potentially insecure permissions.
    ✅ Verification:
  • Verified with a test script that files created without umask are world-readable, while those created with (umask 077; ...) are restricted to owner-only (600).
  • Verified syntax of the modified script with bash -n.

PR created automatically by Jules for task 15876976491062478903 started by @kidchenko

Summary by CodeRabbit

  • Documentation

    • Added a new security guide documenting best practices for SSH key file permissions.
  • Bug Fixes

    • SSH key setup tool enhanced to ensure private keys are created with secure file permissions during the restoration process.

Prevent TOCTOU race condition where private keys were briefly world-readable
during creation. Use subshell with `umask 077` for atomic permissions.

- Wraps `mkdir` and file creation in `(umask 077; ...)`
- Removes existing files before writing to ensure new inode creation
- Adds Sentinel journal entry regarding secure file creation

Co-authored-by: kidchenko <5432753+kidchenko@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai
Copy link

coderabbitai bot commented Feb 13, 2026

📝 Walkthrough

Walkthrough

These changes address a security vulnerability in SSH key generation by implementing strict umask controls and atomic file creation practices. SSH directory creation now uses permission-restricted approaches, and existing key files are removed prior to restoration to ensure newly created files have correct restrictive permissions.

Changes

Cohort / File(s) Summary
Security Documentation
.jules/sentinel.md
New journal entry documenting an SSH private key security vulnerability, explaining umask inheritance in shell redirection and mkdir operations, and detailing atomic permission-safe practices for secure file and directory creation.
SSH Key Setup Implementation
tools/setup-ssh-keys.sh
Modified SSH directory creation with guarded, permission-restricted approach (umask 077, chmod 700). Existing key files are now removed before restoration to ensure correct permissions on newly created files. Private key retrieval now executes in a subshell with umask 077 for secure 600-permission file creation before explicit chmod.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~18 minutes

Poem

🐰 A hop, a skip, through shells so tight,
Keys now guarded with umask's might,
No more defaults in the night,
Permissions atomic, permissions right,
Sentinel watches—security's delight! 🔐

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main security fix: addressing insecure SSH key creation permissions via umask 077 implementation, which is the core change across both modified files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sentinel-fix-ssh-umask-15876976491062478903

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In @.jules/sentinel.md:
- Around line 3-6: Update the markdown to satisfy lint rules: change the heading
date from "2025-02-13" to "2026-02-13", add a blank line immediately after the
"## 2026-02-13 - Secure File Creation in Shell Scripts" heading to fix MD022,
and reflow the long lines in the three following bullet paragraphs so no line
exceeds 80 characters (split sentences or reorder phrases) to fix MD013 while
preserving the original content about umask, redirection, mkdir and chmod;
ensure the examples `(umask 077 && mkdir ...)` and `(umask 077; command > file)`
remain intact.
🧹 Nitpick comments (1)
tools/setup-ssh-keys.sh (1)

157-170: Consider: both key files are deleted before any 1Password call succeeds.

If op read for the private key (Line 164) fails, set -e will terminate the script — but both the private and public key files have already been removed on Line 158. The user ends up with no local keys at all. Since the keys live in 1Password this is recoverable, but you could mitigate by deferring the rm -f to just before each respective write, or deleting only the private key before the private-key write and the public key before the public-key write.

This is a minor resilience nit given set -e and the 1Password backup, so feel free to defer.

Comment on lines +3 to +6
## 2025-02-13 - Secure File Creation in Shell Scripts
**Vulnerability:** SSH private keys were created with default umask (often 022, resulting in 644/world-readable) before being restricted with `chmod 600`. This created a race condition where the key was briefly readable by other users.
**Learning:** Shell redirection (`>`) and `mkdir` adhere to the process's `umask` at the time of execution. `chmod` after creation leaves a window of vulnerability.
**Prevention:** Use `(umask 077 && mkdir ...)` for directories and `(umask 077; command > file)` for files to ensure atomic secure permissions. Remove existing files before writing if sensitive to ensure new inode creation with correct permissions.
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix markdown lint failures flagged by CI.

The documentation lint check is failing with:

  1. MD022 (Line 3): Missing blank line below the ## heading.
  2. MD013 (Lines 4–6): Lines exceed the 80-character limit.

Also, the date reads 2025-02-13 — should this be 2026-02-13 to match the PR date?

Proposed fix
-## 2025-02-13 - Secure File Creation in Shell Scripts
-**Vulnerability:** SSH private keys were created with default umask (often 022, resulting in 644/world-readable) before being restricted with `chmod 600`. This created a race condition where the key was briefly readable by other users.
-**Learning:** Shell redirection (`>`) and `mkdir` adhere to the process's `umask` at the time of execution. `chmod` after creation leaves a window of vulnerability.
-**Prevention:** Use `(umask 077 && mkdir ...)` for directories and `(umask 077; command > file)` for files to ensure atomic secure permissions. Remove existing files before writing if sensitive to ensure new inode creation with correct permissions.
+## 2026-02-13 - Secure File Creation in Shell Scripts
+
+**Vulnerability:** SSH private keys were created with default umask
+(often 022, resulting in 644/world-readable) before being restricted
+with `chmod 600`. This created a race condition where the key was
+briefly readable by other users.
+
+**Learning:** Shell redirection (`>`) and `mkdir` adhere to the
+process's `umask` at the time of execution. `chmod` after creation
+leaves a window of vulnerability.
+
+**Prevention:** Use `(umask 077 && mkdir ...)` for directories and
+`(umask 077; command > file)` for files to ensure atomic secure
+permissions. Remove existing files before writing if sensitive to
+ensure new inode creation with correct permissions.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
## 2025-02-13 - Secure File Creation in Shell Scripts
**Vulnerability:** SSH private keys were created with default umask (often 022, resulting in 644/world-readable) before being restricted with `chmod 600`. This created a race condition where the key was briefly readable by other users.
**Learning:** Shell redirection (`>`) and `mkdir` adhere to the process's `umask` at the time of execution. `chmod` after creation leaves a window of vulnerability.
**Prevention:** Use `(umask 077 && mkdir ...)` for directories and `(umask 077; command > file)` for files to ensure atomic secure permissions. Remove existing files before writing if sensitive to ensure new inode creation with correct permissions.
## 2026-02-13 - Secure File Creation in Shell Scripts
**Vulnerability:** SSH private keys were created with default umask
(often 022, resulting in 644/world-readable) before being restricted
with `chmod 600`. This created a race condition where the key was
briefly readable by other users.
**Learning:** Shell redirection (`>`) and `mkdir` adhere to the
process's `umask` at the time of execution. `chmod` after creation
leaves a window of vulnerability.
**Prevention:** Use `(umask 077 && mkdir ...)` for directories and
`(umask 077; command > file)` for files to ensure atomic secure
permissions. Remove existing files before writing if sensitive to
ensure new inode creation with correct permissions.
🧰 Tools
🪛 GitHub Check: Lint Documentation

[failure] 6-6: Line length
.jules/sentinel.md:6:81 MD013/line-length Line length [Expected: 80; Actual: 248] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md013.md


[failure] 5-5: Line length
.jules/sentinel.md:5:81 MD013/line-length Line length [Expected: 80; Actual: 164] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md013.md


[failure] 4-4: Line length
.jules/sentinel.md:4:81 MD013/line-length Line length [Expected: 80; Actual: 235] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md013.md


[failure] 3-3: Headings should be surrounded by blank lines
.jules/sentinel.md:3 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## 2025-02-13 - Secure File Creation in Shell Scripts"] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md022.md

🤖 Prompt for AI Agents
In @.jules/sentinel.md around lines 3 - 6, Update the markdown to satisfy lint
rules: change the heading date from "2025-02-13" to "2026-02-13", add a blank
line immediately after the "## 2026-02-13 - Secure File Creation in Shell
Scripts" heading to fix MD022, and reflow the long lines in the three following
bullet paragraphs so no line exceeds 80 characters (split sentences or reorder
phrases) to fix MD013 while preserving the original content about umask,
redirection, mkdir and chmod; ensure the examples `(umask 077 && mkdir ...)` and
`(umask 077; command > file)` remain intact.

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.

1 participant