Skip to content

Commit 4dbd137

Browse files
fix(ssh): reject control characters in ssh_add_host_key inputs
ssh_add_host_key writes the host/key_type/key into the line-oriented known_hosts file. Add a field validator on host/key/key_type that rejects ASCII control characters so a value with an embedded newline cannot inject additional known_hosts entries.
1 parent 0fe026b commit 4dbd137

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

  • python/coinbase-agentkit/coinbase_agentkit/action_providers/ssh

python/coinbase-agentkit/coinbase_agentkit/action_providers/ssh/schemas.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@module ssh/schemas
77
"""
88

9-
from pydantic import BaseModel, Field
9+
from pydantic import BaseModel, Field, field_validator
1010

1111
from .connection import SSHConnectionParams
1212

@@ -87,3 +87,17 @@ class AddHostKeySchema(BaseModel):
8787
default="~/.ssh/known_hosts",
8888
description="Path to the known_hosts file",
8989
)
90+
91+
@field_validator("host", "key", "key_type")
92+
@classmethod
93+
def _reject_control_characters(cls, value: str) -> str:
94+
r"""Reject ASCII control characters (newline, CR, NUL, etc.).
95+
96+
``ssh_add_host_key`` writes ``f"{host} {key_type} {key}\n"`` to the
97+
line-oriented ``known_hosts`` file, so an embedded newline in any of
98+
these fields would inject additional entries. Keeping them free of
99+
control characters ensures each call writes a single, well-formed line.
100+
"""
101+
if any(ord(ch) < 0x20 or ord(ch) == 0x7F for ch in value):
102+
raise ValueError("must not contain control characters (e.g. newlines)")
103+
return value

0 commit comments

Comments
 (0)