Skip to content

Allow rendering HTML pastes as sandboxed pages - #236

Open
theryecatcher wants to merge 2 commits into
matze:masterfrom
theryecatcher:master
Open

theryecatcher wants to merge 2 commits into
matze:masterfrom
theryecatcher:master

Conversation

@theryecatcher

Copy link
Copy Markdown
Contributor

Closes #235

Lets .html and .htm pastes be opened as the page, at /html/{id}, so documents with inline CSS, SVG and web fonts can be shared directly instead of as highlighted source. It is off by default and turned on with WASTEBIN_RENDER_UNTRUSTED_HTML=true.

Security

  • The paste is served under a CSP sandbox with no allow flags. The page has an opaque origin with no access to wastebin cookies, and scripts, form submissions and top level navigation are blocked.
  • Burn confirmation and password prompts on /html/{id} are wastebin's own pages and keep the strict policy. To allow that, the security headers layer now keeps a CSP. Only /html/{id} sets it, so every other route should be unchanged.
  • When it's off /html/{id} returns wastebin's 404 page and no toggle is shown.
  • I also pulled the burn and password prompt flow, which the source, Markdown and HTML views each repeated, into one shared helper.

Testing

All tests (including clippy) pass. Some new tests cover the sandbox, burn and password pastes, missing pastes and the default off behaviour.

Let `.html` and `.htm` pastes be viewed as the page, so self contained
documents with inline CSS, SVG and web fonts can be shared directly
instead of as highlighted source. The feature is optin through
`WASTEBIN_RENDER_UNTRUSTED_HTML`.

- Serve the paste unmodified at `/html/{id}`, toggled from the source
and QR views like rendered Markdown. Burn confirmation and password
prompts post back to the same route, so both kinds of protected paste
work.
- Isolate the page with a CSP `sandbox` and no allow flags. It gets an
opaque origin with no access to wastebin cookies, and scripts, forms and
top level navigation are blocked.
- Let the security headers layer keep a CSP set by the handler, since
this route returns both wastebin pages and sandboxed pastes.
Collate the confirmation flows which returns the paste or the prompt
page to show first. Each view now only names the route its prompts post
back to. Caching and other pages shown are unchanged.
@theryecatcher
theryecatcher marked this pull request as draft November 23, 2026 04:55
@theryecatcher theryecatcher reopened this Nov 23, 2026
@theryecatcher
theryecatcher marked this pull request as ready for review November 23, 2026 04:58

@matze matze left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I will play further with it, thanks so far! Please also take a look at i18n.rs, paste.help.toggle_rendered is specific to Markdown.

tracing::debug!("enforcing a http timeout of {timeout:#?}");

if render_html {
tracing::warn!("serving html pastes as sandboxed pages at /html/{{id}}");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Should that be a server-side warning though?

Comment thread README.md
* <kbd>q</kbd> to display the current URL as a QR code,
* <kbd>p</kbd> to view the formatted paste,
* <kbd>w</kbd> to toggle line wrapping on and off (off by default),
* <kbd>m</kbd> to toggle the rendered view on Markdown pastes and

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please make that its own commit and CHANGELOG entry.

Comment thread README.md
| `WASTEBIN_ADDRESS_PORT` | Address and port to bind the server to. | `0.0.0.0:8088` |
| `WASTEBIN_BASE_URL` | Base URL for the QR code display. | |
| `WASTEBIN_CACHE_SIZE` | Number of rendered items to cache. | `128` |
| `WASTEBIN_RENDER_UNTRUSTED_HTML` | Set to `true` to show `html` and `htm` pastes as sandboxed pages at `/html/{id}`. See [HTML pages](#html-pages) before enabling. | `false` |

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This breaks order, please move it down.

Err(VarError::NotUnicode(_)) => Err(Error::RenderHtml),
Ok(value) => match value.to_ascii_lowercase().as_str() {
"true" | "1" => Ok(true),
"false" | "0" => Ok(false),

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

You only document true and false, so drop 1 and 0.

Comment thread README.md

When `WASTEBIN_RENDER_UNTRUSTED_HTML` is set to `true`, pastes
created with an `html` or `htm` extension can be viewed as a page of their own
at `/html/{id}`, reached through the same toggle as rendered Markdown. It is

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think the premise that it's based on file extension is a bit flawed because /html/{id} does not check extension.

// Pastes served as HTML documents carry their own sandbox policy.
if !headers.contains_key(CONTENT_SECURITY_POLICY) {
headers.insert(CONTENT_SECURITY_POLICY, csp);
}

@matze matze Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This looks a bit iffy to me. Can we make the policy explicit and typed and have the layer write exactly one CSP? So basically an enum Csp { Strict, Rendered, Sandbox } with const fn that returns the appropriate HeaderValue and something like this in this layer:

let mut response = next.run(req).await;
let csp = response.extensions_mut().remove::<Csp>().unwrap_or(Csp::Strict);
let headers = response.headers_mut();
headers.insert(CONTENT_SECURITY_POLICY, csp.header());
for (name, value) in defaults { headers.insert(name, value); }
response

@theryecatcher

Copy link
Copy Markdown
Contributor Author

Thanks for the comments !! I thought to take a look at the translation should have been a simple Google Translate will do that. The rest of the requested changes look straightforward enough will implement them too. The struct was definitely something I should have thought about too.

@matze

matze commented Nov 25, 2026

Copy link
Copy Markdown
Owner

I thought to take a look at the translation should have been a simple Google Translate will do that.

I can check the German translation 👍

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rendering HTML pastes as standalone pages

2 participants