Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ or removing a command fails until the table changes in the same commit.
| `remote.startForward` | Forward a local port to a port as the remote host sees it |
| `remote.stopForward` | Stop one port forward and close its local port |
| `remote.forwards` | List the running port forwards of one connection |
| `remote.parseForwardAddress` | Read a port, `host:port` or URL typed into the ports page into a forward target |
| `remote.connections` | Read the SSH connections the user saved |
| `remote.editConnections` | Save, remove or import saved SSH connections, or record a folder or a connection time |
| `remote.parseAddress` | Split a quick-connect address into connection fields |
Expand Down Expand Up @@ -6024,6 +6025,7 @@ owning process). Forwards belong to one connection:
| `remote.startForward` | `{ sessionId, targetHost, targetPort, localPort?, allowNonLoopbackTarget? }` | `{ forward, created }` |
| `remote.stopForward` | `{ sessionId, forwardId }` | `{ stopped }` |
| `remote.forwards` | `{ sessionId }` | `{ forwards }`, by `id` |
| `remote.parseForwardAddress` | `{ text }` | `{ targetHost, targetPort, scheme?, path? }` |

A forward is `{ id, localHost, localPort, requestedLocalPort?,
localPortChanged, targetHost, targetPort, loopbackTarget, connections }`.
Expand All @@ -6035,6 +6037,20 @@ localPortChanged, targetHost, targetPort, loopbackTarget, connections }`.
same family; `targetHost` in the result is the address sent to the host.
A malformed host or port `0` is `invalid_request` with `details.field`
(`targetHost` or `targetPort`).
- **Typed address.** `remote.parseForwardAddress` reads what the user typed
into the page, without a connection and without resolving anything: a
port alone (`3000`, on `localhost`), `host:port` with brackets around an
IPv6 address, or a URL `scheme://host[:port][/path][?query][#fragment]`.
A URL without a port uses its scheme's (80 for `http` and `ws`, 443 for
`https` and `wss`); any other scheme must name the port. `targetHost` has
no brackets and names are in lower case; `scheme` and `path` (what follows
the host and port, absent when empty or `/`) come only from a URL or an
address with a path, so the page opens the forwarded address the same
way. A refused address is `invalid_request` with `details` `{ field:
"text", reason }`, where `reason` is `empty`, `tooLong` (over 2048 bytes),
`malformed` (white space or a control character inside), `scheme`,
`credentials` (a user name or password, refused rather than kept),
`host` or `port`.
- **Confirmation.** A target outside the host's loopback interface reaches
another machine through the host, so it starts only with
`allowNonLoopbackTarget: true`, which the native client sets after the
Expand Down
4 changes: 4 additions & 0 deletions rust/crates/core/src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,9 @@ pub enum RemoteCommand {
StopForward,
/// Lists the running port forwards of one connection (`remote.forwards`).
Forwards,
/// Reads a port, `host:port` or URL typed into the ports page
/// (`remote.parseForwardAddress`).
ParseForwardAddress,
/// Reads the SSH connections the user saved (`remote.connections`).
Connections,
/// Saves, removes, imports or records a folder or a connection time on
Expand Down Expand Up @@ -1604,6 +1607,7 @@ impl CoreCommand {
"remote.startForward" => Some(Self::Remote(RemoteCommand::StartForward)),
"remote.stopForward" => Some(Self::Remote(RemoteCommand::StopForward)),
"remote.forwards" => Some(Self::Remote(RemoteCommand::Forwards)),
"remote.parseForwardAddress" => Some(Self::Remote(RemoteCommand::ParseForwardAddress)),
"remote.connections" => Some(Self::Remote(RemoteCommand::Connections)),
"remote.editConnections" => Some(Self::Remote(RemoteCommand::EditConnections)),
"remote.parseAddress" => Some(Self::Remote(RemoteCommand::ParseAddress)),
Expand Down
14 changes: 13 additions & 1 deletion rust/crates/core/src/runtime/dispatcher/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ pub(super) fn dispatch(command: RemoteCommand, id: Option<String>, payload: Valu
Err(error) => CoreResponse::failure(id, error),
}
}
RemoteCommand::ParseForwardAddress => {
match serde_json::from_value(payload)
.map_err(|error| invalid_forward_request(&error))
.and_then(lithe_workspace::forwards::parse_address)
{
Ok(address) => CoreResponse::success(
id,
serde_json::to_value(address).expect("port forward address should encode"),
),
Err(error) => CoreResponse::failure(id, error),
}
}
RemoteCommand::Connections => {
match serde_json::from_value(payload)
.map_err(|error| {
Expand Down Expand Up @@ -320,7 +332,7 @@ fn answer<R: serde::de::DeserializeOwned>(
}
}

/// A `remote.*Forward` payload that does not decode.
/// A `remote.*Forward*` payload that does not decode.
fn invalid_forward_request(error: &serde_json::Error) -> CoreError {
CoreError::new(ErrorCode::InvalidRequest, "Invalid port forward request")
.with_details(error.to_string())
Expand Down
32 changes: 32 additions & 0 deletions rust/crates/core/src/tests/remote_sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,39 @@ fn connection_window_helpers_answer_through_the_dispatcher() {
);
assert_eq!(motty["data"]["unavailable"], "missing", "{motty}");

let forward = call(
"remote.parseForwardAddress",
json!({"text": "HTTP://Localhost:3000/app?tab=1"}),
);
assert_eq!(
forward["data"],
json!({"targetHost": "localhost", "targetPort": 3000, "scheme": "http", "path": "/app?tab=1"}),
"{forward}"
);
let port = call("remote.parseForwardAddress", json!({"text": "8080"}));
assert_eq!(
port["data"],
json!({"targetHost": "localhost", "targetPort": 8080}),
"{port}"
);
let secret = call(
"remote.parseForwardAddress",
json!({"text": "https://user:secret@example.com"}),
);
assert_eq!(secret["error"]["code"], "invalid_request", "{secret}");
let details: Value =
serde_json::from_str(secret["error"]["details"].as_str().unwrap()).unwrap();
assert_eq!(details, json!({"field": "text", "reason": "credentials"}));
assert!(
!secret.to_string().contains("secret@"),
"a refused address is not echoed"
);

for (command, payload) in [
(
"remote.parseForwardAddress",
json!({"text": "3000", "extra": 1}),
),
(
"remote.effectiveSettings",
json!({"connection": {"host": "dev@box"}}),
Expand Down
Loading
Loading