Skip to content
Merged
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
131 changes: 130 additions & 1 deletion pkgs/id/src/world_ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ impl Handler for SshClient {
commands: VecDeque::new(),
records_prefix: None,
records_drained: 0,
records_after: None,
errored: false,
previous_screen: None,
};
Expand Down Expand Up @@ -740,6 +741,10 @@ struct SshIo {
/// Cumulative bytes already rendered for the current `/records` drain, so a
/// large world's records are bounded across pages, not just per page.
records_drained: usize,
/// Cursor that produced the in-flight records page (`None` before the
/// first page, whose request sends `after: null`); the page's
/// `next_after` must strictly advance past it (#6615).
records_after: Option<String>,
errored: bool,
/// Last screen successfully written to this shell's terminal. A missing
/// value forces the next screen view to repaint the whole terminal.
Expand Down Expand Up @@ -841,6 +846,7 @@ impl SessionIo for SshIo {
if let WorldCommand::Records { prefix } = &command {
self.records_prefix = Some(prefix.clone());
self.records_drained = 0;
self.records_after = None;
}
return Inbound::Text(frame);
}
Expand Down Expand Up @@ -1004,14 +1010,39 @@ impl SessionIo for SshIo {
Some("error") => {
self.errored = true;
self.records_prefix = None;
self.records_after = None;
let message = value["message"].as_str().unwrap_or("error");
self.last_error = Some(format!("\r\n{}\r\n", terminal_text(message)));
self.last_error.clone().unwrap_or_default()
}
Some("records") => {
let records = value["records"].as_object();
let sequence = value["sequence"].as_u64().unwrap_or_default();
let after = value["next_after"].as_str().map(str::to_owned);
// Validate the cursor before trusting it (#6615): a
// present-but-non-string `next_after` is a malformed page,
// not a clean end-of-pages, and a cursor that does not
// strictly advance past the one that produced this page
// would re-request the same page until the byte cap stops
// the drain (~18,700 re-requests in the filing's probe).
// The advance rule is also why no page-count bound is
// needed beside the byte bound below: every continuation
// moves the cursor forward through the host's key space,
// so pages cannot repeat, and the rendered-byte total
// still caps the drain's size. Oracle: the `world_net`
// records cursor contract (#6505/#6578).
let produced_by = self.records_after.clone().unwrap_or_default();
let (after, notice) = match records_cursor(value.get("next_after"), &produced_by) {
RecordsCursor::Advance(next) => (Some(next), None),
RecordsCursor::End => (None, None),
RecordsCursor::Malformed => (
None,
Some("[records truncated: host sent a malformed next-page cursor]\n"),
),
RecordsCursor::Stalled => (
None,
Some("[records truncated: host cursor did not advance]\n"),
),
};
let text = render_records_value(records, sequence, after.as_deref());
// Bound the whole drain, not just each page: a world with many
// records would otherwise keep fetching pages without limit.
Expand All @@ -1026,17 +1057,25 @@ impl SessionIo for SshIo {
};
let truncated = after.is_some() && self.records_drained >= MAX_RECORDS_TOTAL_BYTES;
if let Some((prefix, after)) = next {
self.records_after = Some(after.clone());
self.commands
.push_back(WorldCommand::RecordsContinue { prefix, after });
Comment on lines +1060 to 1062

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Track the cursor when its records request is dispatched

When two identical /records commands are pasted in one input frame, recv() queues both initial requests before their continuations, so the drains interleave. After the first drain's second page advances records_after to k2, the second drain's queued continuation still sends after: k1, but its valid next_after: k2 is compared against the shared k2 and falsely rejected as stalled. This clears the drain state and, for listings spanning four or more pages, leaves later pages unfetched. Set records_after from the dequeued RecordsContinue.after when dispatching the request, rather than from a continuation being enqueued, or serialize complete drains.

Useful? React with 👍 / 👎.

} else {
self.records_prefix = None;
self.records_after = None;
}
if truncated {
format!(
"\r\n{}\r\n{}",
terminal_text(&text),
terminal_text("[records truncated: too much output]\n")
)
} else if let Some(notice) = notice {
format!(
"\r\n{}\r\n{}",
terminal_text(&text),
terminal_text(notice)
)
} else {
format!("\r\n{}", terminal_text(&text))
}
Expand Down Expand Up @@ -1455,6 +1494,37 @@ fn render_records_value(
output
}

/// What a records page's `next_after` means for the drain, judged
/// against `produced_by` — the cursor that produced this page (the
/// empty string before the first page, whose request sends
/// `after: null`). Factored out of the `records` frame arm so the
/// contract can be unit-tested without a live session (#6615).
#[derive(Debug, PartialEq, Eq)]
enum RecordsCursor {
/// No cursor (absent or null): the listing is complete.
End,
/// A cursor that strictly advances: fetch the next page after it.
Advance(String),
/// A present, non-string cursor: a malformed page, never a clean
/// end-of-pages.
Malformed,
/// A cursor that does not advance past `produced_by` (a repeat,
/// a step backwards, or the empty cursor at the start): following
/// it would re-request the page just rendered.
Stalled,
}

fn records_cursor(next_after: Option<&serde_json::Value>, produced_by: &str) -> RecordsCursor {
match next_after {
None | Some(serde_json::Value::Null) => RecordsCursor::End,
Some(serde_json::Value::String(cursor)) if cursor.as_str() > produced_by => {
RecordsCursor::Advance(cursor.clone())
}
Some(serde_json::Value::String(_)) => RecordsCursor::Stalled,
Some(_) => RecordsCursor::Malformed,
}
}

#[cfg(test)]
fn render_records(
records: &crate::world::Records,
Expand Down Expand Up @@ -3219,6 +3289,65 @@ mod tests {
};
}

#[test]
fn records_cursor_must_be_a_string_that_advances() {
// Issue #6615: the SSH /records drain trusted `next_after`
// unchecked — a non-string cursor silently read as a complete
// listing, and a repeated/empty cursor re-requested the same
// page ~18,700 times until the rendered-byte cap stopped it.
// Oracle: the world_net records cursor contract (#6505/#6578)
// — the cursor must be a string and must strictly advance.
let cursor = |value: serde_json::Value, produced_by: &str| {
records_cursor(Some(&value), produced_by)
};

// Absent and null both mean the host has no more pages.
assert_eq!(records_cursor(None, ""), RecordsCursor::End);
assert_eq!(
cursor(serde_json::Value::Null, "k1"),
RecordsCursor::End
);

// A normal multi-page drain: each cursor advances past the
// one that produced the page, then the listing ends.
assert_eq!(
cursor(serde_json::json!("k1"), ""),
RecordsCursor::Advance("k1".to_owned())
);
assert_eq!(
cursor(serde_json::json!("k2"), "k1"),
RecordsCursor::Advance("k2".to_owned())
);
assert_eq!(records_cursor(None, "k2"), RecordsCursor::End);

// Present but not a string: a malformed page, never End.
for bad in [
serde_json::json!(123),
serde_json::json!(true),
serde_json::json!({"key": "k1"}),
serde_json::json!(["k1"]),
] {
assert_eq!(
cursor(bad, ""),
RecordsCursor::Malformed,
"non-string cursor must be malformed"
);
}

// A cursor that does not advance stops the drain: the empty
// cursor on the first page, a repeat of the producing
// cursor, and a step backwards.
assert_eq!(cursor(serde_json::json!(""), ""), RecordsCursor::Stalled);
assert_eq!(
cursor(serde_json::json!("k1"), "k1"),
RecordsCursor::Stalled
);
assert_eq!(
cursor(serde_json::json!("k0"), "k1"),
RecordsCursor::Stalled
);
}

#[cfg(test)]
fn records_of(count: usize) -> crate::world::Records {
(0..count)
Expand Down
Loading