forge
Explore
Log in

Don't you dare go Hollow

Live update streams are capped at 64 per client address and 4096 server-wide (429 past that

Browse files at this commit

a7608d152fce44514c15949e71784f71098ab175Ahmet Kaan Gümüş and ClaudeParent: 2e6d8a0
Some checks failed
Show checks
  • harden
    • live update streams are capped at 64 per client address and 4096 server-wide (429 past that
    • the page keeps working without live updates)
  • style
    • the PR page lists approvals given before the latest commits apart, as needing a new approval
Show full message
🔒 harden: live update streams are capped at 64 per client address and 4096 server-wide (429 past that; the page keeps working without live updates); 💄 style: the PR page lists approvals given before the latest commits apart, as needing a new approval
crates/forge-web/src/i18n/pulls_review.rsmodified+8 -0
@@ -399,6 +399,14 @@ pub fn approvals_of(lang: Lang, have: impl Display, need: impl Display) -> Strin
399 }399 }
400 400
401 /// "Approved by Ada, Lin".401 /// "Approved by Ada, Lin".
402+/// Approvals given before the latest push: shown, but no longer counted for the merge rules.
403+pub fn approved_before_push(lang: Lang, names: &str) -> String {
404+ match lang {
405+ Lang::En => format!("Approved before the latest commits by {names} (needs a new approval)"),
406+ Lang::Tr => format!("{names} son commitlerden önce onaylamıştı (yeniden onay gerekiyor)"),
407+ }
408+}
409+
402 pub fn approved_by(lang: Lang, names: &str) -> String {410 pub fn approved_by(lang: Lang, names: &str) -> String {
403 match lang {411 match lang {
404 Lang::En => format!("Approved by {names}"),412 Lang::En => format!("Approved by {names}"),
crates/forge-web/src/pages/ci_runs.rsmodified+69 -0
@@ -2029,6 +2029,64 @@ fn repo_changes(wake: watch::Receiver<u64>) -> impl Stream<Item = Result<Event>>
2029 })2029 })
2030 }2030 }
2031 2031
2032+/// Open event streams one client address (IPv6 by /64) may hold. A page holds one or two, so this
2033+/// is many tabs; past it a script that opens streams in a loop is refused.
2034+const MAX_STREAMS_PER_CLIENT: usize = 64;
2035+/// Open event streams server-wide. Each is a task and a socket that stay until the client leaves.
2036+const MAX_STREAMS: usize = 4096;
2037+
2038+/// Open streams per client address, and their sum.
2039+static OPEN_STREAMS: std::sync::Mutex<(usize, Option<HashMap<std::net::IpAddr, usize>>)> =
2040+ std::sync::Mutex::new((0, None));
2041+
2042+/// One open event stream, counted against its client's and the server's cap until dropped.
2043+/// `ip` is `None` for an uncounted stream.
2044+struct StreamSlot {
2045+ ip: Option<std::net::IpAddr>,
2046+}
2047+
2048+impl StreamSlot {
2049+ /// A slot for this request's client, or `None` when a cap is reached. Without the app state
2050+ /// (unit tests of a bare router) nothing is counted.
2051+ fn take(cx: &Cx) -> Option<StreamSlot> {
2052+ if topcoat::context::try_app_context::<AppState>(cx).is_none() {
2053+ return Some(StreamSlot { ip: None });
2054+ }
2055+ let ip = crate::client_ip::client_ip(cx);
2056+ let mut open = OPEN_STREAMS.lock().unwrap_or_else(|e| e.into_inner());
2057+ let (total, per_client) = &mut *open;
2058+ let count = per_client
2059+ .get_or_insert_with(HashMap::new)
2060+ .entry(ip)
2061+ .or_insert(0);
2062+ if *total >= MAX_STREAMS || *count >= MAX_STREAMS_PER_CLIENT {
2063+ return None;
2064+ }
2065+ *count += 1;
2066+ *total += 1;
2067+ Some(StreamSlot { ip: Some(ip) })
2068+ }
2069+}
2070+
2071+impl Drop for StreamSlot {
2072+ fn drop(&mut self) {
2073+ let Some(ip) = self.ip else {
2074+ return;
2075+ };
2076+ let mut open = OPEN_STREAMS.lock().unwrap_or_else(|e| e.into_inner());
2077+ let (total, per_client) = &mut *open;
2078+ *total = total.saturating_sub(1);
2079+ if let Some(map) = per_client.as_mut()
2080+ && let Some(count) = map.get_mut(&ip)
2081+ {
2082+ *count = count.saturating_sub(1);
2083+ if *count == 0 {
2084+ map.remove(&ip);
2085+ }
2086+ }
2087+ }
2088+}
2089+
2032 /// How long the browser waits before it reconnects a dropped event stream. The browser's default is2090 /// How long the browser waits before it reconnects a dropped event stream. The browser's default is
2033 /// a few seconds; one second makes a page come back as soon as a server restart is done.2091 /// a few seconds; one second makes a page come back as soon as a server restart is done.
2034 pub(crate) const STREAM_RETRY: Duration = Duration::from_secs(1);2092 pub(crate) const STREAM_RETRY: Duration = Duration::from_secs(1);
@@ -2039,6 +2097,17 @@ pub(crate) fn events_response(
2039 cx: &Cx,2097 cx: &Cx,
2040 events: impl Stream<Item = Result<Event>> + Send + 'static,2098 events: impl Stream<Item = Result<Event>> + Send + 'static,
2041 ) -> Result<Response> {2099 ) -> Result<Response> {
2100+ // Past the cap the stream is refused: the page keeps working, only without live updates.
2101+ let Some(slot) = StreamSlot::take(cx) else {
2102+ let mut res = Response::new(topcoat::router::Body::from("too many open live streams"));
2103+ *res.status_mut() = http::StatusCode::TOO_MANY_REQUESTS;
2104+ return Ok(res);
2105+ };
2106+ // The slot lives as long as the stream: it is given back when the client goes away.
2107+ let events = events.map(move |event| {
2108+ let _held = &slot;
2109+ event
2110+ });
2042 let retry = stream::once(std::future::ready(Ok(Event::new().retry(STREAM_RETRY))));2111 let retry = stream::once(std::future::ready(Ok(Event::new().retry(STREAM_RETRY))));
2043 let events = Sse::new(retry.chain(events)).keep_alive(2112 let events = Sse::new(retry.chain(events)).keep_alive(
2044 KeepAlive::new()2113 KeepAlive::new()
crates/forge-web/src/pages/pull_view.rsmodified+26 -4
@@ -494,6 +494,7 @@ struct DismissRow {
494 #[component]494 #[component]
495 async fn review_box(495 async fn review_box(
496 approved: Option<String>,496 approved: Option<String>,
497+ approved_before: Option<String>,
497 changes: Option<String>,498 changes: Option<String>,
498 dismiss: Vec<DismissRow>,499 dismiss: Vec<DismissRow>,
499 csrf: String,500 csrf: String,
@@ -505,6 +506,9 @@ async fn review_box(
505 if let Some(text) = approved {506 if let Some(text) = approved {
506 <p class="review-summary">(text.as_str())</p>507 <p class="review-summary">(text.as_str())</p>
507 }508 }
509+ if let Some(text) = approved_before {
510+ <p class="review-summary muted">(text.as_str())</p>
511+ }
508 if let Some(text) = changes {512 if let Some(text) = changes {
509 <p class="review-summary">(text.as_str())</p>513 <p class="review-summary">(text.as_str())</p>
510 }514 }
@@ -840,11 +844,29 @@ async fn pull_conversation(
840 844
841 let review_error = review_error_from(lang, cx);845 let review_error = review_error_from(lang, cx);
842 let summary = state.store.review_summary(&pr.issue.id).await?;846 let summary = state.store.review_summary(&pr.issue.id).await?;
843- let approved = (!summary.approved_by.is_empty())847+ // Approvals of an earlier head no longer count for the merge rules (see `merge_gate`), so
844- .then(|| g::approved_by(lang, &summary.approved_by.join(", ")));848+ // they are listed apart from the current ones.
849+ let mut outdated: Vec<String> = state
850+ .store
851+ .latest_review_states(&pr.issue.id)
852+ .await?
853+ .into_iter()
854+ .filter(|(_, _, review, commit)| *review == ReviewState::Approved && *commit != pr.head_oid)
855+ .map(|(_, name, _, _)| name)
856+ .collect();
857+ outdated.sort_unstable();
858+ let current: Vec<&str> = summary
859+ .approved_by
860+ .iter()
861+ .filter(|name| !outdated.contains(name))
862+ .map(String::as_str)
863+ .collect();
864+ let approved = (!current.is_empty()).then(|| g::approved_by(lang, &current.join(", ")));
865+ let approved_before =
866+ (!outdated.is_empty()).then(|| g::approved_before_push(lang, &outdated.join(", ")));
845 let changes = (!summary.changes_requested_by.is_empty())867 let changes = (!summary.changes_requested_by.is_empty())
846 .then(|| g::changes_requested_by(lang, &summary.changes_requested_by.join(", ")));868 .then(|| g::changes_requested_by(lang, &summary.changes_requested_by.join(", ")));
847- let review_box_shown = approved.is_some() || changes.is_some();869+ let review_box_shown = approved.is_some() || approved_before.is_some() || changes.is_some();
848 // Dismiss forms are for Write holders on an open pull request.870 // Dismiss forms are for Write holders on an open pull request.
849 let dismiss: Vec<DismissRow> = if open && can_write {871 let dismiss: Vec<DismissRow> = if open && can_write {
850 counted_reviews(state, &pr)872 counted_reviews(state, &pr)
@@ -881,7 +903,7 @@ async fn pull_conversation(
881 <div class="issue-column">903 <div class="issue-column">
882 discussion_timeline(lang: lang, discussion: discussion)904 discussion_timeline(lang: lang, discussion: discussion)
883 if review_box_shown {905 if review_box_shown {
884- review_box(approved: approved, changes: changes, dismiss: dismiss,906+ review_box(approved: approved, approved_before: approved_before, changes: changes, dismiss: dismiss,
885 csrf: csrf.clone(), lang: lang)907 csrf: csrf.clone(), lang: lang)
886 }908 }
887 if open {909 if open {