Skip to content

Commit 46278d6

Browse files
committed
feat(remote): read a port, host:port or URL typed into the ports page (Refs 1lck/Lithe-IDEA#2231)
remote.parseForwardAddress turns what the user types into a forward target without a connection: a port alone on localhost, host:port, or a URL whose scheme and path the page keeps to open the forwarded address the same way. User names and passwords are refused, not kept; each refusal names one reason in details.
1 parent 822f871 commit 46278d6

8 files changed

Lines changed: 452 additions & 2 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ or removing a command fails until the table changes in the same commit.
820820
| `remote.startForward` | Forward a local port to a port as the remote host sees it |
821821
| `remote.stopForward` | Stop one port forward and close its local port |
822822
| `remote.forwards` | List the running port forwards of one connection |
823+
| `remote.parseForwardAddress` | Read a port, `host:port` or URL typed into the ports page into a forward target |
823824
| `remote.connections` | Read the SSH connections the user saved |
824825
| `remote.editConnections` | Save, remove or import saved SSH connections, or record a folder or a connection time |
825826
| `remote.parseAddress` | Split a quick-connect address into connection fields |
@@ -6024,6 +6025,7 @@ owning process). Forwards belong to one connection:
60246025
| `remote.startForward` | `{ sessionId, targetHost, targetPort, localPort?, allowNonLoopbackTarget? }` | `{ forward, created }` |
60256026
| `remote.stopForward` | `{ sessionId, forwardId }` | `{ stopped }` |
60266027
| `remote.forwards` | `{ sessionId }` | `{ forwards }`, by `id` |
6028+
| `remote.parseForwardAddress` | `{ text }` | `{ targetHost, targetPort, scheme?, path? }` |
60276029

60286030
A forward is `{ id, localHost, localPort, requestedLocalPort?,
60296031
localPortChanged, targetHost, targetPort, loopbackTarget, connections }`.
@@ -6035,6 +6037,20 @@ localPortChanged, targetHost, targetPort, loopbackTarget, connections }`.
60356037
same family; `targetHost` in the result is the address sent to the host.
60366038
A malformed host or port `0` is `invalid_request` with `details.field`
60376039
(`targetHost` or `targetPort`).
6040+
- **Typed address.** `remote.parseForwardAddress` reads what the user typed
6041+
into the page, without a connection and without resolving anything: a
6042+
port alone (`3000`, on `localhost`), `host:port` with brackets around an
6043+
IPv6 address, or a URL `scheme://host[:port][/path][?query][#fragment]`.
6044+
A URL without a port uses its scheme's (80 for `http` and `ws`, 443 for
6045+
`https` and `wss`); any other scheme must name the port. `targetHost` has
6046+
no brackets and names are in lower case; `scheme` and `path` (what follows
6047+
the host and port, absent when empty or `/`) come only from a URL or an
6048+
address with a path, so the page opens the forwarded address the same
6049+
way. A refused address is `invalid_request` with `details` `{ field:
6050+
"text", reason }`, where `reason` is `empty`, `tooLong` (over 2048 bytes),
6051+
`malformed` (white space or a control character inside), `scheme`,
6052+
`credentials` (a user name or password, refused rather than kept),
6053+
`host` or `port`.
60386054
- **Confirmation.** A target outside the host's loopback interface reaches
60396055
another machine through the host, so it starts only with
60406056
`allowNonLoopbackTarget: true`, which the native client sets after the

‎rust/crates/core/src/protocol/command.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,9 @@ pub enum RemoteCommand {
10181018
StopForward,
10191019
/// Lists the running port forwards of one connection (`remote.forwards`).
10201020
Forwards,
1021+
/// Reads a port, `host:port` or URL typed into the ports page
1022+
/// (`remote.parseForwardAddress`).
1023+
ParseForwardAddress,
10211024
/// Reads the SSH connections the user saved (`remote.connections`).
10221025
Connections,
10231026
/// Saves, removes, imports or records a folder or a connection time on
@@ -1604,6 +1607,7 @@ impl CoreCommand {
16041607
"remote.startForward" => Some(Self::Remote(RemoteCommand::StartForward)),
16051608
"remote.stopForward" => Some(Self::Remote(RemoteCommand::StopForward)),
16061609
"remote.forwards" => Some(Self::Remote(RemoteCommand::Forwards)),
1610+
"remote.parseForwardAddress" => Some(Self::Remote(RemoteCommand::ParseForwardAddress)),
16071611
"remote.connections" => Some(Self::Remote(RemoteCommand::Connections)),
16081612
"remote.editConnections" => Some(Self::Remote(RemoteCommand::EditConnections)),
16091613
"remote.parseAddress" => Some(Self::Remote(RemoteCommand::ParseAddress)),

‎rust/crates/core/src/runtime/dispatcher/remote.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ pub(super) fn dispatch(command: RemoteCommand, id: Option<String>, payload: Valu
203203
Err(error) => CoreResponse::failure(id, error),
204204
}
205205
}
206+
RemoteCommand::ParseForwardAddress => {
207+
match serde_json::from_value(payload)
208+
.map_err(|error| invalid_forward_request(&error))
209+
.and_then(lithe_workspace::forwards::parse_address)
210+
{
211+
Ok(address) => CoreResponse::success(
212+
id,
213+
serde_json::to_value(address).expect("port forward address should encode"),
214+
),
215+
Err(error) => CoreResponse::failure(id, error),
216+
}
217+
}
206218
RemoteCommand::Connections => {
207219
match serde_json::from_value(payload)
208220
.map_err(|error| {
@@ -320,7 +332,7 @@ fn answer<R: serde::de::DeserializeOwned>(
320332
}
321333
}
322334

323-
/// A `remote.*Forward` payload that does not decode.
335+
/// A `remote.*Forward*` payload that does not decode.
324336
fn invalid_forward_request(error: &serde_json::Error) -> CoreError {
325337
CoreError::new(ErrorCode::InvalidRequest, "Invalid port forward request")
326338
.with_details(error.to_string())

‎rust/crates/core/src/tests/remote_sessions.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,39 @@ fn connection_window_helpers_answer_through_the_dispatcher() {
12781278
);
12791279
assert_eq!(motty["data"]["unavailable"], "missing", "{motty}");
12801280

1281+
let forward = call(
1282+
"remote.parseForwardAddress",
1283+
json!({"text": "HTTP://Localhost:3000/app?tab=1"}),
1284+
);
1285+
assert_eq!(
1286+
forward["data"],
1287+
json!({"targetHost": "localhost", "targetPort": 3000, "scheme": "http", "path": "/app?tab=1"}),
1288+
"{forward}"
1289+
);
1290+
let port = call("remote.parseForwardAddress", json!({"text": "8080"}));
1291+
assert_eq!(
1292+
port["data"],
1293+
json!({"targetHost": "localhost", "targetPort": 8080}),
1294+
"{port}"
1295+
);
1296+
let secret = call(
1297+
"remote.parseForwardAddress",
1298+
json!({"text": "https://user:secret@example.com"}),
1299+
);
1300+
assert_eq!(secret["error"]["code"], "invalid_request", "{secret}");
1301+
let details: Value =
1302+
serde_json::from_str(secret["error"]["details"].as_str().unwrap()).unwrap();
1303+
assert_eq!(details, json!({"field": "text", "reason": "credentials"}));
1304+
assert!(
1305+
!secret.to_string().contains("secret@"),
1306+
"a refused address is not echoed"
1307+
);
1308+
12811309
for (command, payload) in [
1310+
(
1311+
"remote.parseForwardAddress",
1312+
json!({"text": "3000", "extra": 1}),
1313+
),
12821314
(
12831315
"remote.effectiveSettings",
12841316
json!({"connection": {"host": "dev@box"}}),

0 commit comments

Comments
 (0)