mirror of
https://we.phorge.it/source/phorge.git
synced 2025-02-06 20:08:29 +01:00
Fix validation of SSH keys with spaces in the comment field
Summary: Fixes T5449. Keys are in the form `<type> <key> <comments>`, where comments are optional and can have spaces. Test Plan: Tried these invalid keys: - Empty. - One part. - Invalid type. Tried these valid keys: - No comment. - Normal comment. - Comment with spaces. Reviewers: btrahan, chad Reviewed By: chad Subscribers: epriestley Maniphest Tasks: T5449 Differential Revision: https://secure.phabricator.com/D9701
This commit is contained in:
parent
dd91732df3
commit
5dffd88737
1 changed files with 20 additions and 13 deletions
|
@ -379,22 +379,29 @@ final class PhabricatorSettingsPanelSSHKeys
|
||||||
|
|
||||||
private static function parsePublicKey($entire_key) {
|
private static function parsePublicKey($entire_key) {
|
||||||
$parts = str_replace("\n", '', trim($entire_key));
|
$parts = str_replace("\n", '', trim($entire_key));
|
||||||
$parts = preg_split('/\s+/', $parts);
|
|
||||||
|
|
||||||
if (count($parts) == 2) {
|
// The third field (the comment) can have spaces in it, so split this
|
||||||
$parts[] = ''; // Add an empty comment part.
|
// into a maximum of three parts.
|
||||||
} else if (count($parts) == 3) {
|
$parts = preg_split('/\s+/', $parts, 3);
|
||||||
// This is the expected case.
|
|
||||||
} else {
|
|
||||||
if (preg_match('/private\s*key/i', $entire_key)) {
|
if (preg_match('/private\s*key/i', $entire_key)) {
|
||||||
// Try to give the user a better error message if it looks like
|
// Try to give the user a better error message if it looks like
|
||||||
// they uploaded a private key.
|
// they uploaded a private key.
|
||||||
throw new Exception(
|
throw new Exception(
|
||||||
pht('Provide your public key, not your private key!'));
|
pht('Provide your public key, not your private key!'));
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
switch (count($parts)) {
|
||||||
|
case 1:
|
||||||
throw new Exception(
|
throw new Exception(
|
||||||
pht('Provided public key is not properly formatted.'));
|
pht('Provided public key is not properly formatted.'));
|
||||||
}
|
case 2:
|
||||||
|
// Add an empty comment part.
|
||||||
|
$parts[] = '';
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
// This is the expected case.
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
list($type, $body, $comment) = $parts;
|
list($type, $body, $comment) = $parts;
|
||||||
|
|
Loading…
Add table
Reference in a new issue