Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-warehouse): accept openssh tunnel keys pasted with extra whitespace (#110866)

Tom Owers40f40238662e
diff --git a/products/warehouse_sources/backend/models/ssh_tunnel.py b/products/warehouse_sources/backend/models/ssh_tunnel.py
index 85f2d52c10b3..b54e0fe2b683 100644
--- a/products/warehouse_sources/backend/models/ssh_tunnel.py
+++ b/products/warehouse_sources/backend/models/ssh_tunnel.py
@@ -106,7 +106,6 @@ def from_private_key(file_obj: IO[str], passphrase: str | None = None) -> PKey:
file_bytes,
password=password,
)
- file_obj.seek(0)
except ValueError as ssh_error:
# A wrong passphrase on an OpenSSH key surfaces as a checksum/decrypt failure. Falling
# through to the PEM loader masks it with a misleading "no BEGIN/END" error, so re-raise
@@ -117,18 +116,21 @@ def from_private_key(file_obj: IO[str], passphrase: str | None = None) -> PKey:
file_bytes,
password=password if passphrase is not None else None,
)
- encryption_algorithm: crypto_serialization.KeySerializationEncryption
- if passphrase:
- encryption_algorithm = crypto_serialization.BestAvailableEncryption(password)
- else:
- encryption_algorithm = crypto_serialization.NoEncryption()
- file_obj = StringIO(
- key.private_bytes(
- crypto_serialization.Encoding.PEM,
- crypto_serialization.PrivateFormat.OpenSSH,
- encryption_algorithm,
- ).decode("utf-8")
- )
+ # paramiko's parser is stricter than cryptography's. An OpenSSH key pasted with leading
+ # whitespace, indented lines, or its line breaks joined loads above but fails in paramiko,
+ # so give paramiko a re-serialized copy of the key rather than the pasted text.
+ encryption_algorithm: crypto_serialization.KeySerializationEncryption
+ if passphrase:
+ encryption_algorithm = crypto_serialization.BestAvailableEncryption(password)
+ else:
+ encryption_algorithm = crypto_serialization.NoEncryption()
+ file_obj = StringIO(
+ key.private_bytes(
+ crypto_serialization.Encoding.PEM,
+ crypto_serialization.PrivateFormat.OpenSSH,
+ encryption_algorithm,
+ ).decode("utf-8")
+ )
if isinstance(key, rsa.RSAPrivateKey):
private_key = RSAKey.from_private_key(file_obj, passphrase)
elif isinstance(key, ed25519.Ed25519PrivateKey):
diff --git a/products/warehouse_sources/backend/tests/test_ssh_tunnel.py b/products/warehouse_sources/backend/tests/test_ssh_tunnel.py
index f86f833b6c9f..008dc53611c4 100644
--- a/products/warehouse_sources/backend/tests/test_ssh_tunnel.py
+++ b/products/warehouse_sources/backend/tests/test_ssh_tunnel.py
@@ -1,7 +1,7 @@
import pytest
from cryptography.hazmat.primitives import serialization
-from cryptography.hazmat.primitives.asymmetric import ed25519
+from cryptography.hazmat.primitives.asymmetric import ed25519, rsa
from paramiko import RSAKey
from products.warehouse_sources.backend.models.ssh_tunnel import SSHTunnel, SSHTunnelConfig
@@ -107,6 +107,40 @@ def test_is_auth_valid_key_pair(private_key, passphrase, expected):
assert res is expected
+def _openssh_private_key(key: ed25519.Ed25519PrivateKey | rsa.RSAPrivateKey, passphrase: str | None) -> str:
+ return key.private_bytes(
+ encoding=serialization.Encoding.PEM,
+ format=serialization.PrivateFormat.OpenSSH,
+ encryption_algorithm=serialization.BestAvailableEncryption(passphrase.encode())
+ if passphrase
+ else serialization.NoEncryption(),
+ ).decode()
+
+
+ "key_type,passphrase,paste",
+ [
+ (key_type, passphrase, paste)
+ for key_type in ("ed25519", "rsa")
+ for passphrase in (None, "correct-passphrase")
+ for paste in ("as_generated", "leading_whitespace", "indented", "line_breaks_joined")
+ ],
+)
+def test_is_auth_valid_accepts_openssh_key_as_pasted(key_type, passphrase, paste):
+ key = ed25519.Ed25519PrivateKey.generate() if key_type == "ed25519" else rsa.generate_private_key(65537, 2048)
+ pem = _openssh_private_key(key, passphrase)
+ pasted = {
+ "as_generated": pem,
+ "leading_whitespace": " " + pem,
+ "indented": "\n".join(" " + line for line in pem.splitlines()),
+ "line_breaks_joined": pem.replace("\n", " "),
+ }[paste]
+
+ res, error = _keypair_tunnel(private_key=pasted, passphrase=passphrase).is_auth_valid()
+
+ assert (res, error) == (True, "")
+
+
def test_is_auth_valid_unparseable_key_suggests_format():
res, error = _keypair_tunnel(private_key="not a private key", passphrase=None).is_auth_valid()