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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

- <kbd>m</kbd> 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
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -168,7 +169,8 @@ When viewing a paste, you can use
* <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
Comment thread
theryecatcher marked this conversation as resolved.
Resolved
* <kbd>m</kbd> to toggle the rendered view on Markdown pastes or open HTML
pastes as a page and
* <kbd>?</kbd> to view the list of keybindings.

To paste some text you can also use the <kbd>ctrl</kbd>+<kbd>s</kbd> key
Expand All @@ -191,6 +193,30 @@ embedded via `![alt](https://…)`, 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
Expand All @@ -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` |
Expand Down
2 changes: 2 additions & 0 deletions crates/wastebin_core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions crates/wastebin_server/src/csp.rs
Original file line number Diff line number Diff line change
@@ -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<ResponseParts, Self::Error> {
res.extensions_mut().insert(self);
Ok(res)
}
}
16 changes: 15 additions & 1 deletion crates/wastebin_server/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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}")]
Expand Down Expand Up @@ -77,6 +79,18 @@ pub fn cache_size() -> Result<NonZeroUsize, Error> {
.map_err(Error::CacheSize)
}

pub fn render_html() -> Result<bool, Error> {
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<db::Open, Error> {
match std::env::var(vars::DATABASE_PATH) {
Ok(path) => Ok(db::Open::Path(PathBuf::from(path))),
Expand Down
Loading
Loading