Skip to content

SSH deploy keys: Ed25519 by default, private key never sent to the browser, key regeneration - #451

Open
gnomii wants to merge 6 commits into
php-censor:masterfrom
gnomii:fix/ssh-deploy-keys
Open

gnomii wants to merge 6 commits into
php-censor:masterfrom
gnomii:fix/ssh-deploy-keys

Conversation

@gnomii

@gnomii gnomii commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Contribution type

Bug fix, feature, UI

Description of change

Reworks how project SSH deploy keys are generated, stored and used: it fixes several issues and adds a way to regenerate a key from the UI.

Key generation (Helper\SshKey)

  • New php-censor.ssh.type option: ed25519 (new default) or rsa. RSA keys are 3072 bits by default (was 2048), and strength only applies to RSA.
  • Fix: the key pair is generated in a private temporary directory, removed even on failure. Before, both key files stayed in /tmp every time the "Add project" page was opened.
  • Fix: arguments are escaped and the type is checked against an allowlist.
  • Fix: failures are no longer silent. A key rejected by ssh-keygen is told apart from ssh-keygen not being able to run (not installed, proc_open() disabled, open_basedir, no writable temporary directory).
  • %PROJECT_TITLE% and %HOSTNAME% variables in php-censor.ssh.comment, so that the deploy keys of projects and instances can be told apart. The default comment is now %PROJECT_TITLE%@php-censor. Spaces in the variable values become -.

Project add and edit forms

  • Fix: the private key is never sent to the browser any more:
    • the add form no longer pre-fills a generated private key; the key pair is generated on save (the "SSH key" choice is set to "generate" by default, as before);
    • the edit form no longer shows the private key, and a blank field keeps the current key.
  • New "SSH key" choice on both forms: keep (or no key), generate a new key pair, or remove. A pasted private key wins over the choice.
  • Fix: the public key is derived from the private key on the server (ssh-keygen -y), instead of coming from a hidden form field. It can no longer differ from the private key, for example after pasting another private key. An invalid or passphrase-protected key is rejected with a clear message.
  • Generating or removing a key asks for a confirmation, because builds fail until the deploy key is updated on the hosting platform. On hosted projects (GitHub, GitLab, Bitbucket), adding a key also switches clones from HTTPS to SSH.
  • Fix: pasted keys with CRLF line endings or without a trailing newline are normalized. ssh rejected them ("invalid format"), at build time too.

Project page

  • SHA256 fingerprint of the public key (the format shown by GitHub, GitLab and Gitea) and a copy button.
  • Fix: the public key is HTML-escaped, and only displayed when the project has a private key.

Builds

  • Fix: the key file and the ssh wrapper are removed in a finally block, even when the clone throws.
  • Fix: the git/svn ssh wrapper forwards its arguments as they are ("$@" instead of $*) and quotes the key file path.
  • Mercurial keeps its own ssh command, and so its host key checking, with the key file path quoted.

Behaviour changes

  • New keys are Ed25519 by default. Existing keys are not changed. Hosting that only accepts RSA keys needs php-censor.ssh.type: rsa; this is described in docs/en/upgrades/UPGRADE_3.0.md.
  • The add form no longer shows the generated key before saving. The public key, its fingerprint and the copy button are on the project page right after saving.

Tests

  • New SshKeyTest, covering:
    • both key types, the RSA default size and the fingerprint (compared with ssh-keygen -l);
    • comment variables and escaping;
    • pasted keys: CRLF, encrypted, invalid;
    • failure classification, using a fake ssh-keygen;
    • open_basedir without /dev;
    • no files left on disk.
  • New SshCloneTest: temporary files are removed when the clone throws (git, hg and svn), the Mercurial command, wrapper argument forwarding, key normalization.
  • ProjectServiceTest: updating a project without key options keeps its keys.
  • ProjectTest: Project::hasSshPrivateKey().
  • Each commit passes the full test suite. The add and edit flows were also checked against a running instance (MariaDB): generate, keep, replace, invalid key, remove, project without a key, a public key left without its private key, and the confirmation dialog.

Maxime Patteri and others added 6 commits December 4, 2026 00:18
…rors

- New php-censor.ssh.type option (ed25519 by default, or rsa); RSA keys are 3072 bits by default.
- The key pair is generated in a private temporary directory, removed even on failure.
- Arguments are escaped, the type is checked against an allowlist.
- ssh-keygen failures throw: a key rejected by ssh-keygen is told apart from ssh-keygen not being able
  to run (not installed, proc_open() disabled, open_basedir, no writable temporary directory).
- Public key derived from a private key, SHA256 fingerprint, private key line endings normalized.
- %PROJECT_TITLE% and %HOSTNAME% variables in the key comment (default: %PROJECT_TITLE%@php-censor).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The key file and ssh wrapper are removed in a finally block, even when the clone throws.
- The git/svn ssh wrapper forwards its arguments as they are ("$@" instead of $*) and quotes the
  key file path (-i); Mercurial keeps its own ssh command (and its host key checking), with the key
  file path quoted.
- The key file is written with the line endings ssh expects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…or remove the key

- The add and edit forms have an "SSH key" choice: keep (or no key), generate a new key pair (on save,
  the default for a new project), or remove. A pasted private key wins over the choice.
- The private key is no longer pre-filled (add) nor sent back (edit): a blank field keeps the key.
- The public key is derived from the private key on the server; the hidden public key field is gone.
  An invalid or passphrase-protected key is rejected with a clear message.
- Generating or removing a key asks for a confirmation, as it breaks builds until the deploy key is
  updated.
- Key fields are read from the request body only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ic key

The public key is only shown when the project has a private key (a public key left alone by the
former form is no longer displayed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants