phacility/phabricator · error · Exception

Provided public key is not properly formatted.

Error message

Provided public key is not properly formatted.

What it means

newFromRawKey() splits the key on whitespace into at most three parts (type, body, comment). If the split yields exactly one part, there is no type/body separation at all, so the key cannot have a valid 'type base64body' shape and the generic format exception is thrown.

Source

Thrown at src/applications/auth/sshkey/PhabricatorAuthSSHPublicKey.php:45

    if (!strlen($entire_key)) {
      throw new Exception(pht('No public key was provided.'));
    }

    $parts = str_replace("\n", '', $entire_key);

    // The third field (the comment) can have spaces in it, so split this
    // into a maximum of three parts.
    $parts = preg_split('/\s+/', $parts, 3);

    if (preg_match('/private\s*key/i', $entire_key)) {
      // Try to give the user a better error message if it looks like
      // they uploaded a private key.
      throw new Exception(pht('Provide a public key, not a private key!'));
    }

    switch (count($parts)) {
      case 1:
        throw new Exception(
          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;

    $recognized_keys = array(
      'ssh-dsa',
      'ssh-dss',
      'ssh-rsa',
      'ssh-ed25519',
      'ecdsa-sha2-nistp256',

View on GitHub (pinned to 5720a38cfe)

Solutions

  1. Paste the complete one-line key starting with its type, e.g. 'ssh-ed25519 AAAAC3Nza... user@host'
  2. Re-copy the line from the .pub file rather than reconstructing it by hand
  3. Verify the line has at least two whitespace-separated fields before submitting
Defensive patterns

Strategy: validation

Validate before calling

// Cheap shape check before calling newFromRawKey():
$parts = preg_split('/\s+/', str_replace("\n", '', trim($raw)), 3);
if (count($parts) < 2) {
  return pht('Key must be "<type> <base64 body> [comment]", e.g. ssh-ed25519 AAAA...');
}

Try / catch

try {
  $public_key = PhabricatorAuthSSHPublicKey::newFromRawKey($raw);
} catch (Exception $ex) {
  // Show 'Provided public key is not properly formatted.' on the field.
  $e_key = $ex->getMessage();
  $errors[] = $ex->getMessage();
}

Prevention

When it happens

Trigger: Submitting a single token with no whitespace: just the base64 body without the 'ssh-rsa'/'ssh-ed25519' prefix, or a bare word/garbage string.

Common situations: Users copying only the base64 portion of the key and losing the algorithm prefix; terminal copy that grabbed a fragment; keys whose type prefix was edited away.

Related errors


AI-assisted analysis of phacility/phabricator@5720a38cfe (2026-08-21). Data as JSON: /api/errors/e008609d907c257a. Report an issue: GitHub.