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
31 changes: 20 additions & 11 deletions pkgs/id/crates/id-worlds/src/world/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,17 +1648,26 @@ impl WorldActor {
"form.cancel" => self.execute_form_cancel(&request.args),
"loc.position" => self.execute_loc_position(&request.args),
"chat.say" => self.execute_chat(&request.args),
"chat.history" => Ok(serde_json::json!({
"lines": chat_history(
&self.core.events,
request
.args
.get("count")
.and_then(serde_json::Value::as_u64)
.unwrap_or(16)
.min(crate::world_caps::MAX_CHAT_HISTORY_LINES as u64) as usize,
),
})),
"chat.history" => {
// A supplied `count` must be an unsigned integer; only an
// omitted count falls back to the default. Treating a
// malformed count as omitted silently turns a client error
// into a successful read (#7892).
let count = match request.args.get("count") {
None => 16,
Some(value) => value
.as_u64()
.ok_or_else(CapError::bad_args)?
.min(crate::world_caps::MAX_CHAT_HISTORY_LINES as u64)
as usize,
};
Ok(serde_json::json!({
"lines": chat_history(
&self.core.events,
count.min(crate::world_caps::MAX_CHAT_HISTORY_LINES),
),
}))
}
"records.read" => self.execute_records_read(&request.args),
"schedule.at" => self.execute_schedule_at(&request.args),
"schedule.cancel" => self.execute_schedule_cancel(&request.args),
Expand Down
46 changes: 46 additions & 0 deletions pkgs/id/crates/id-worlds/src/world/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2886,6 +2886,36 @@ mod tests {
"chat.history",
serde_json::json!({"count": 100_000}),
),
(
"hist-u32-overflow",
"chat.history",
serde_json::json!({"count": 4_294_967_296_u64}),
),
(
"hist-string",
"chat.history",
serde_json::json!({"count": "2"}),
),
(
"hist-negative",
"chat.history",
serde_json::json!({"count": -1}),
),
(
"hist-fractional",
"chat.history",
serde_json::json!({"count": 1.5}),
),
(
"hist-null",
"chat.history",
serde_json::json!({"count": null}),
),
(
"hist-object",
"chat.history",
serde_json::json!({"count": {"n": 2}}),
),
]
.into_iter()
.filter(|(id, _, _)| !self.answered.contains(*id))
Expand Down Expand Up @@ -2948,6 +2978,22 @@ mod tests {
assert_eq!(lines.len(), crate::world_caps::MAX_CHAT_HISTORY_LINES);
assert_eq!(lines[0]["text"], "line-7");
assert_eq!(lines[63]["text"], "line-70");
let large = &results["hist-u32-overflow"];
assert_eq!(large["ok"], true, "{large}");
assert_eq!(large["value"]["lines"], clamped["value"]["lines"]);
// A supplied count of the wrong JSON kind is rejected, never
// treated as omitted (#7892).
for id in [
"hist-string",
"hist-negative",
"hist-fractional",
"hist-null",
"hist-object",
] {
let malformed = &results[id];
assert_eq!(malformed["ok"], false, "{id}: {malformed}");
assert_eq!(malformed["error"], "bad_args", "{id}: {malformed}");
}
// records.read whose first record cannot fit one page is BadArgs,
// never an empty page that looks complete.
let oversized_result = &results["records-oversized"];
Expand Down