Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixed

- Ignore `password` and `confirm_burn` in the query string, so a link can no
longer confirm a burn on the reader's behalf or carry a paste's password.
- Reject off-origin and scheme-relative `Referer` values when redirecting back
after a theme change, closing an open-redirect bypass via `//`-prefixed paths.
- Unlink Unix domain socket after shutdown.
Expand Down
7 changes: 7 additions & 0 deletions crates/wastebin_server/src/handlers/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ where
return Ok(password);
}

// `Form` reads the query string on GET and HEAD, which would put the password in the URL
// and from there into browser history and every proxy log on the way. Only accept it from
// a request body; the header above is the way to send one with a GET.
if matches!(*req.method(), http::Method::GET | http::Method::HEAD) {
return Ok(None);
}

Ok(Form::<Data>::from_request(req, state)
.await
.ok()
Expand Down
98 changes: 93 additions & 5 deletions crates/wastebin_server/src/handlers/html/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub async fn get<E>(
uids: Option<Uids>,
theme: Option<Theme>,
lang: Lang,
method: http::Method,
form: Result<Form<PasteForm>, E>,
) -> Result<Response, ErrorResponse> {
if let Some(token) = handoff.owner.as_deref()
Expand All @@ -90,7 +91,14 @@ pub async fn get<E>(
}

async {
let form = form.ok().map(|Form(form)| form);
// `Form` reads the query string on GET and HEAD, so `?confirm_burn=1` burnt the paste for
// anything that merely follows a link (an `<img>`, a prefetch, a link unfurler), and
// `?password=` put the password into browser history, the `Referer` and proxy logs. The
// interstitial and the password prompt both post, so only a request body counts.
let form = form
.ok()
.filter(|_| method == http::Method::POST)
.map(|Form(form)| form);
let password = form
.as_ref()
.and_then(|form| form.password.as_ref())
Expand Down Expand Up @@ -189,8 +197,9 @@ pub async fn get<E>(

#[cfg(test)]
mod tests {
use crate::handlers::insert::form::Entry;
use crate::test_helpers::{Client, StoreCookies};
use reqwest::StatusCode;
use reqwest::{StatusCode, header};

#[tokio::test]
async fn unknown_paste() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -205,7 +214,7 @@ mod tests {
#[tokio::test]
async fn password_prompt_keeps_extension() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(StoreCookies(false)).await;
let data = crate::handlers::insert::form::Entry {
let data = Entry {
text: String::from("fn main() {}"),
extension: Some(String::from("rs")),
password: String::from("hunter2"),
Expand All @@ -231,7 +240,7 @@ mod tests {
async fn encrypted_paste_is_not_stored() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(StoreCookies(false)).await;
let password = "hunter2";
let data = crate::handlers::insert::form::Entry {
let data = Entry {
text: String::from("secret-body-xyz"),
password: password.to_string(),
..Default::default()
Expand All @@ -253,7 +262,7 @@ mod tests {
let res = client
.post(&location)
.form(&[("password", password)])
.header(reqwest::header::ACCEPT, "text/html; charset=utf-8")
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?;
assert_eq!(res.status(), StatusCode::OK);
Expand All @@ -264,4 +273,83 @@ mod tests {

Ok(())
}

/// `Form` reads the query string on GET, so a link carrying `?confirm_burn=1` skipped the
/// interstitial and burnt the paste for anything that merely follows a URL: an `<img>`, a
/// prefetch, a link unfurler.
#[tokio::test]
async fn burn_is_not_confirmed_from_the_query_string() -> Result<(), Box<dyn std::error::Error>>
{
let client = Client::new(StoreCookies(false)).await;
let data = Entry {
text: String::from("secret-body-xyz"),
burn_after_reading: Some(String::from("on")),
..Default::default()
};

let res = client.post_form().form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);
let location = res
.headers()
.get("location")
.unwrap()
.to_str()?
.replace("burn/", "");

for _ in 0..2 {
let res = client
.get(&location)
.query(&[("confirm_burn", "1")])
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?;
assert_eq!(res.status(), StatusCode::OK);
let body = res.text().await?;
assert!(!body.contains("secret-body-xyz"), "body: {body}");
assert!(body.contains(">reveal<"), "body: {body}");
}

Ok(())
}

/// `Form` reads the query string on GET, so `?password=` unlocked the paste, putting the
/// password into browser history, bookmarks, the `Referer` and every proxy log on the way.
/// The prompt posts, so only a request body may carry one.
#[tokio::test]
async fn password_is_not_taken_from_the_query_string() -> Result<(), Box<dyn std::error::Error>>
{
let client = Client::new(StoreCookies(false)).await;
let data = Entry {
text: String::from("secret-body-xyz"),
password: String::from("hunter2"),
..Default::default()
};

let res = client.post_form().form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);
let location = res.headers().get("location").unwrap().to_str()?;

let body = client
.get(location)
.query(&[("password", "hunter2")])
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?
.text()
.await?;
assert!(!body.contains("secret-body-xyz"), "body: {body}");

// The prompt's own POST is unaffected.
let body = client
.post(location)
.form(&[("password", "hunter2")])
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?
.text()
.await?;
assert!(body.contains("secret-body-xyz"), "body: {body}");

Ok(())
}
}
90 changes: 89 additions & 1 deletion crates/wastebin_server/src/handlers/html/rendered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ pub async fn get<E>(
uids: Option<Uids>,
theme: Option<Theme>,
lang: Lang,
method: http::Method,
form: Result<Form<PasteForm>, E>,
) -> Result<Response, ErrorResponse> {
async {
let form = form.ok().map(|Form(form)| form);
// Only a posted form counts; see `paste::get`.
let form = form
.ok()
.filter(|_| method == http::Method::POST)
.map(|Form(form)| form);
let password = form
.as_ref()
.and_then(|form| form.password.as_ref())
Expand Down Expand Up @@ -327,4 +332,87 @@ mod tests {

Ok(())
}

/// `Form` reads the query string on GET, so a link carrying `?confirm_burn=1` skipped the
/// interstitial and burnt the paste for anything that merely follows a URL: an `<img>`, a
/// prefetch, a link unfurler.
#[tokio::test]
async fn burn_is_not_confirmed_from_the_query_string() -> Result<(), Box<dyn std::error::Error>>
{
let client = Client::new(StoreCookies(false)).await;
let data = Entry {
text: String::from("secret-body-xyz"),
extension: Some(String::from("md")),
burn_after_reading: Some(String::from("on")),
..Default::default()
};

let res = client.post_form().form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);
let location = res
.headers()
.get("location")
.unwrap()
.to_str()?
.replace("burn/", "");
let rendered = format!("/md{location}");

for _ in 0..2 {
let res = client
.get(&rendered)
.query(&[("confirm_burn", "1")])
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?;
assert_eq!(res.status(), StatusCode::OK);
let body = res.text().await?;
assert!(!body.contains("secret-body-xyz"), "body: {body}");
assert!(body.contains(">reveal<"), "body: {body}");
}

Ok(())
}

/// `Form` reads the query string on GET, so `?password=` unlocked the paste, putting the
/// password into browser history, bookmarks, the `Referer` and every proxy log on the way.
/// The prompt posts, so only a request body may carry one.
#[tokio::test]
async fn password_is_not_taken_from_the_query_string() -> Result<(), Box<dyn std::error::Error>>
{
let client = Client::new(StoreCookies(false)).await;
let data = Entry {
text: String::from("secret-body-xyz"),
extension: Some(String::from("md")),
password: String::from("hunter2"),
..Default::default()
};

let res = client.post_form().form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);
let location = res.headers().get("location").unwrap().to_str()?;
let rendered = format!("/md{location}");

let body = client
.get(&rendered)
.query(&[("password", "hunter2")])
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?
.text()
.await?;
assert!(!body.contains("secret-body-xyz"), "body: {body}");

// The prompt's own POST is unaffected.
let body = client
.post(&rendered)
.form(&[("password", "hunter2")])
.header(header::ACCEPT, "text/html; charset=utf-8")
.send()
.await?
.text()
.await?;
assert!(body.contains("secret-body-xyz"), "body: {body}");

Ok(())
}
}
41 changes: 41 additions & 0 deletions crates/wastebin_server/src/handlers/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,45 @@ mod tests {

Ok(())
}

/// `Form` reads the query string on GET, so `?password=` unlocked the raw view and the
/// download, putting the password into browser history and proxy logs.
#[tokio::test]
async fn password_is_not_taken_from_the_query_string() -> Result<(), Box<dyn std::error::Error>>
{
let client = Client::new(StoreCookies(false)).await;
let password = "hunter2";
let data = Entry {
text: String::from("secret-body-xyz"),
password: password.to_string(),
..Default::default()
};

let res = client.post_form().form(&data).send().await?;
assert_eq!(res.status(), StatusCode::SEE_OTHER);
let location = res.headers().get("location").unwrap().to_str()?.to_owned();

for route in ["raw", "dl"] {
let body = client
.get(&format!("/{route}{location}"))
.query(&[("password", password)])
.send()
.await?
.text()
.await?;
assert!(!body.contains("secret-body-xyz"), "/{route}: {body}");

// The documented way to send one with a GET still works.
let body = client
.get(&format!("/{route}{location}"))
.header(PASSWORD_HEADER_NAME, password)
.send()
.await?
.text()
.await?;
assert_eq!(body, "secret-body-xyz", "/{route}");
}

Ok(())
}
}
Loading