diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9d352978..b50288d3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,19 @@
## [Unreleased]
+### Added
+
+- Show pastes as standalone pages at `/html/{id}`, with a toggle from the paste
+ view on `html` and `htm` pastes like rendered Markdown. The page is served
+ unmodified under a CSP `sandbox` that blocks scripts, forms and access to
+ wastebin cookies. It is off by default; set
+ `WASTEBIN_RENDER_UNTRUSTED_HTML=true` to turn it on.
+
+### Changed
+
+- m opens HTML pastes as a page, in addition to toggling the rendered
+ view on Markdown pastes.
+
### Fixed
- Reject off-origin and scheme-relative `Referer` values when redirecting back
diff --git a/README.md b/README.md
index 4a2d058e..0fca3185 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,7 @@ to released versions here:
[syntect](https://github.com/trishume/syntect)
* renders Markdown pastes to HTML, including GitHub-flavored tables, task lists
and admonitions
+* optionally shows HTML pastes as sandboxed pages
* comes with [nine color themes](https://matze.github.io/wastebin/) in light and
dark mode
* encrypts entries using ChaCha20Poly1305 and argon2 hashed passwords
@@ -168,7 +169,8 @@ When viewing a paste, you can use
* q to display the current URL as a QR code,
* p to view the formatted paste,
* w to toggle line wrapping on and off (off by default),
-* m to toggle the rendered view on Markdown pastes and
+* m to toggle the rendered view on Markdown pastes or open HTML
+ pastes as a page and
* ? to view the list of keybindings.
To paste some text you can also use the ctrl+s key
@@ -191,6 +193,30 @@ embedded via ``, the Content Security Policy is relaxed to
`img-src *` for `/md/*` responses only; all other routes keep the strict
default.
+### HTML pages
+
+When `WASTEBIN_RENDER_UNTRUSTED_HTML` is set to `true`, any paste can be viewed
+as a page of its own at `/html/{id}`. Pastes opened with an `html` or `htm`
+extension get a toggle to that page, like rendered Markdown. The extension is
+part of the URL rather than the paste, so it only decides whether the toggle is
+shown. It is off by default, and `/html/{id}` then returns 404. The paste is
+served unmodified, without the wastebin interface, so inline styles, SVG and web
+fonts render the way it was written. Burn after reading and password protected
+pastes show the usual confirmation and password pages first.
+
+The page is served with a Content Security Policy `sandbox`, which gives it an
+opaque origin, so it cannot read wastebin cookies or act on behalf of the
+viewer. Scripts, form submissions, pop-ups and automatic redirects are blocked;
+links still work when clicked. Stylesheets may load from Google Fonts and images
+from anywhere.
+
+The sandbox keeps wastebin and its viewers' sessions safe, but it cannot vouch
+for what a page says. Anyone who can paste can publish a full screen page
+under your domain, such as a convincing fake login form that tells visitors to
+send their credentials elsewhere, and loading remote images lets the author
+see who opened it. Turn this on only when you trust the people who can paste,
+or when a reverse proxy in front of wastebin limits who can reach `/html/`.
+
### Configuration
The following environment variables can be set to configure the server and
@@ -206,6 +232,7 @@ run-time behavior:
| `WASTEBIN_MAX_BODY_SIZE` | Number of bytes to accept for POST requests. | `1048576`, i.e. 1 MB |
| `WASTEBIN_PASSWORD_SALT` | Salt used to hash user passwords used for encrypting pastes. | `somesalt` |
| `WASTEBIN_PASTE_EXPIRATIONS` | Possible paste expirations as a comma-separated list of seconds or values with duration magnitudes (`s`, `m`, `h`, `d`, `M`, `y` for seconds, minutes, hours, days, months and years respectively). Appending `=d` to one of the value makes it the default selection. | see [here](https://github.com/matze/wastebin/blob/eb61c78506a165605f145e8374ed64822405eda0/crates/wastebin_server/src/env.rs#L166) |
+| `WASTEBIN_RENDER_UNTRUSTED_HTML` | Set to `true` to show pastes as sandboxed pages at `/html/{id}`. See [HTML pages](#html-pages) before enabling. | `false` |
| `WASTEBIN_SIGNING_KEY` | Key to sign cookies. Must be at least 64 bytes long. | Random key generated at startup, i.e. cookies will become invalid after restarts and paste creators will not be able to delete their pastes. |
| `WASTEBIN_THEME` | Theme colors, one of `ayu`, `base16ocean`, `catppuccin`, `coldark`, `gruvbox`, `monokai`, `onehalf`, `rosepine`, `solarized`. See [this page](https://matze.github.io/wastebin/) for a preview. | `ayu` |
| `WASTEBIN_TITLE` | HTML page title. Pastes with their own title use that title instead. | `wastebin` |
diff --git a/crates/wastebin_core/src/env.rs b/crates/wastebin_core/src/env.rs
index 87c71a9f..95f313f8 100644
--- a/crates/wastebin_core/src/env.rs
+++ b/crates/wastebin_core/src/env.rs
@@ -16,6 +16,8 @@ pub mod vars {
pub const PASSWORD_SALT: &str = "WASTEBIN_PASSWORD_SALT";
/// Expirations list.
pub const PASTE_EXPIRATIONS: &str = "WASTEBIN_PASTE_EXPIRATIONS";
+ /// Serve pastes as sandboxed pages at `/html/{id}`.
+ pub const RENDER_HTML: &str = "WASTEBIN_RENDER_UNTRUSTED_HTML";
/// Signing key for signed cookie store.
pub const SIGNING_KEY: &str = "WASTEBIN_SIGNING_KEY";
/// Theme to use.
diff --git a/crates/wastebin_server/src/csp.rs b/crates/wastebin_server/src/csp.rs
new file mode 100644
index 00000000..3d6f56a4
--- /dev/null
+++ b/crates/wastebin_server/src/csp.rs
@@ -0,0 +1,42 @@
+use std::convert::Infallible;
+
+use axum::http::HeaderValue;
+use axum::response::{IntoResponseParts, ResponseParts};
+
+/// Content Security Policy of a response. Handlers return it as a response part and the security
+/// headers layer writes it as the only `Content-Security-Policy` header, defaulting to
+/// [`Csp::Strict`].
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum Csp {
+ /// Wastebin's own pages.
+ Strict,
+ /// Rendered Markdown, which may embed remote images via ``.
+ Rendered,
+ /// HTML pastes served as their own page.
+ Sandbox,
+}
+
+impl Csp {
+ pub(crate) const fn header(self) -> HeaderValue {
+ match self {
+ Self::Strict => HeaderValue::from_static(
+ "default-src 'none'; script-src 'self'; img-src 'self' data: ; style-src 'self' data: ; font-src 'self' data: ; object-src 'none' ; base-uri 'none' ; frame-ancestors 'none' ; form-action 'self' ;",
+ ),
+ Self::Rendered => HeaderValue::from_static(
+ "default-src 'none'; script-src 'self'; img-src * data: ; style-src 'self' data: ; font-src 'self' data: ; object-src 'none' ; base-uri 'none' ; frame-ancestors 'none' ; form-action 'self' ;",
+ ),
+ Self::Sandbox => HeaderValue::from_static(
+ "sandbox; default-src 'none'; style-src 'unsafe-inline' https://fonts.googleapis.com ; font-src https://fonts.gstatic.com data: ; img-src * data: ; base-uri 'none' ; form-action 'none' ; frame-ancestors 'none' ;",
+ ),
+ }
+ }
+}
+
+impl IntoResponseParts for Csp {
+ type Error = Infallible;
+
+ fn into_response_parts(self, mut res: ResponseParts) -> Result {
+ res.extensions_mut().insert(self);
+ Ok(res)
+ }
+}
diff --git a/crates/wastebin_server/src/env.rs b/crates/wastebin_server/src/env.rs
index 9bc4ada1..7ff74342 100644
--- a/crates/wastebin_server/src/env.rs
+++ b/crates/wastebin_server/src/env.rs
@@ -9,7 +9,7 @@ use axum_extra::extract::cookie::Key;
use wastebin_core::env::vars::{
self, ADDRESS_PORT, BASE_URL, CACHE_SIZE, DATABASE_PATH, HTTP_TIMEOUT, MAX_BODY_SIZE,
- PASTE_EXPIRATIONS, SIGNING_KEY,
+ PASTE_EXPIRATIONS, RENDER_HTML, SIGNING_KEY,
};
use wastebin_core::{db, expiration};
use wastebin_highlight::{Theme, theme::ParseThemeNameError};
@@ -22,6 +22,8 @@ pub(crate) enum Error {
CacheConstruction(#[from] cached::BuildError),
#[error("failed to parse {CACHE_SIZE}, expected number of elements: {0}")]
CacheSize(ParseIntError),
+ #[error("failed to parse {RENDER_HTML}, expected `true` or `false`")]
+ RenderHtml,
#[error("failed to parse {DATABASE_PATH}, contains non-Unicode data")]
DatabasePath,
#[error("failed to parse {MAX_BODY_SIZE}, expected number of bytes: {0}")]
@@ -77,6 +79,18 @@ pub fn cache_size() -> Result {
.map_err(Error::CacheSize)
}
+pub fn render_html() -> Result {
+ match std::env::var(vars::RENDER_HTML) {
+ Err(VarError::NotPresent) => Ok(false),
+ Err(VarError::NotUnicode(_)) => Err(Error::RenderHtml),
+ Ok(value) => match value.to_ascii_lowercase().as_str() {
+ "true" => Ok(true),
+ "false" => Ok(false),
+ _ => Err(Error::RenderHtml),
+ },
+ }
+}
+
pub fn database_method() -> Result {
match std::env::var(vars::DATABASE_PATH) {
Ok(path) => Ok(db::Open::Path(PathBuf::from(path))),
diff --git a/crates/wastebin_server/src/handlers/html/document.rs b/crates/wastebin_server/src/handlers/html/document.rs
new file mode 100644
index 00000000..4d0a9ea5
--- /dev/null
+++ b/crates/wastebin_server/src/handlers/html/document.rs
@@ -0,0 +1,248 @@
+use axum::extract::{Form, Path, State};
+use axum::http::{HeaderValue, header};
+use axum::response::{IntoResponse, Response};
+
+use crate::cache::Key;
+use crate::csp::Csp;
+use crate::handlers::NoStore;
+use crate::handlers::extract::Theme;
+use crate::handlers::html::paste::PasteForm;
+use crate::handlers::html::{ErrorResponse, FormAction, Unlock, Unlocked, make_error, unlock};
+use crate::i18n::Lang;
+use crate::{Database, Page};
+use wastebin_core::db;
+
+pub async fn get(
+ State(page): State,
+ State(db): State,
+ Path(id): Path,
+ theme: Option,
+ lang: Lang,
+ form: Result