MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / validate_ssh_session_response

Function validate_ssh_session_response

crates/openshell-core/src/forward.rs:854–891  ·  view source on GitHub ↗

Validate a `CreateSshSessionResponse` before any of its fields are used to build a shell command or config file. This is a belt-and-suspenders pair to [`build_proxy_command`]: escaping alone is sufficient to prevent injection, but rejecting malformed fields at the trust boundary fails loudly before the string is assembled and catches gateway bugs or tampering early.

(
    resp: &crate::proto::CreateSshSessionResponse,
)

Source from the content-addressed store, hash-verified

852/// at the trust boundary fails loudly before the string is assembled and
853/// catches gateway bugs or tampering early.
854pub fn validate_ssh_session_response(
855 resp: &crate::proto::CreateSshSessionResponse,
856) -> std::result::Result<(), SshSessionResponseError> {
857 validate_field(
858 "sandbox_id",
859 &resp.sandbox_id,
860 MAX_SANDBOX_ID_LEN,
861 is_sandbox_id_byte,
862 )?;
863 validate_field("token", &resp.token, MAX_TOKEN_LEN, is_token_byte)?;
864 validate_field(
865 "gateway_host",
866 &resp.gateway_host,
867 MAX_GATEWAY_HOST_LEN,
868 is_gateway_host_byte,
869 )?;
870 match resp.gateway_scheme.as_str() {
871 "http" | "https" => {}
872 _ => return Err(SshSessionResponseError::InvalidScheme),
873 }
874 if resp.gateway_port == 0 || resp.gateway_port > u32::from(u16::MAX) {
875 return Err(SshSessionResponseError::InvalidPort);
876 }
877 if !resp.host_key_fingerprint.is_empty() {
878 if resp.host_key_fingerprint.len() > MAX_FINGERPRINT_LEN {
879 return Err(SshSessionResponseError::TooLong {
880 field: "host_key_fingerprint",
881 max: MAX_FINGERPRINT_LEN,
882 });
883 }
884 if !resp.host_key_fingerprint.bytes().all(is_fingerprint_byte) {
885 return Err(SshSessionResponseError::InvalidChars {
886 field: "host_key_fingerprint",
887 });
888 }
889 }
890 Ok(())
891}
892
893fn validate_field(
894 name: &'static str,

Callers 4

handle_shell_connectFunction · 0.85
handle_exec_commandFunction · 0.85
start_port_forwardsFunction · 0.85
ssh_session_configFunction · 0.85

Calls 4

validate_fieldFunction · 0.85
lenMethod · 0.80
as_strMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected