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,
)
| 852 | /// at the trust boundary fails loudly before the string is assembled and |
| 853 | /// catches gateway bugs or tampering early. |
| 854 | pub 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 | |
| 893 | fn validate_field( |
| 894 | name: &'static str, |
no test coverage detected