2018-09-19 08:48:39 +00:00
|
|
|
use ammonia::{Builder, UrlRelative};
|
2018-11-24 11:44:17 +00:00
|
|
|
use diesel::{
|
|
|
|
self,
|
|
|
|
deserialize::Queryable,
|
|
|
|
serialize::{self, Output},
|
2018-06-11 09:43:27 +00:00
|
|
|
sql_types::Text,
|
2018-11-24 11:44:17 +00:00
|
|
|
types::ToSql,
|
|
|
|
};
|
|
|
|
use serde::{self, de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
use std::{
|
|
|
|
borrow::{Borrow, Cow},
|
|
|
|
fmt::{self, Display},
|
|
|
|
io::Write,
|
|
|
|
ops::Deref,
|
|
|
|
};
|
2018-06-11 09:43:27 +00:00
|
|
|
|
2018-09-19 08:48:39 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref CLEAN: Builder<'static> = {
|
|
|
|
let mut b = Builder::new();
|
2021-01-01 23:13:54 +00:00
|
|
|
b.add_generic_attributes(&["id", "dir"])
|
2019-04-06 17:20:33 +00:00
|
|
|
.add_tags(&["iframe", "video", "audio", "label", "input"])
|
2018-09-19 08:48:39 +00:00
|
|
|
.id_prefix(Some("postcontent-"))
|
|
|
|
.url_relative(UrlRelative::Custom(Box::new(url_add_prefix)))
|
2018-11-24 11:44:17 +00:00
|
|
|
.add_tag_attributes(
|
|
|
|
"iframe",
|
2019-03-20 16:56:17 +00:00
|
|
|
["width", "height", "src", "frameborder"].iter().cloned(),
|
2019-03-06 13:11:36 +00:00
|
|
|
)
|
2019-03-20 16:56:17 +00:00
|
|
|
.add_tag_attributes("video", ["src", "title", "controls"].iter())
|
2019-04-06 17:20:33 +00:00
|
|
|
.add_tag_attributes("audio", ["src", "title", "controls"].iter())
|
|
|
|
.add_tag_attributes("label", ["for"].iter())
|
|
|
|
.add_tag_attributes("input", ["type", "checked"].iter())
|
|
|
|
.add_allowed_classes("input", ["cw-checkbox"].iter())
|
2019-12-30 13:35:27 +00:00
|
|
|
.add_allowed_classes(
|
|
|
|
"span",
|
|
|
|
[
|
|
|
|
"cw-container",
|
|
|
|
"cw-text",
|
|
|
|
//Scope classes for the syntax highlighting.
|
|
|
|
"attribute-name",
|
|
|
|
"comment",
|
|
|
|
"constant",
|
|
|
|
"control",
|
|
|
|
"declaration",
|
|
|
|
"entity",
|
|
|
|
"function",
|
|
|
|
"invalid",
|
|
|
|
"keyword",
|
|
|
|
"language",
|
|
|
|
"modifier",
|
|
|
|
"name",
|
|
|
|
"numeric",
|
|
|
|
"operator",
|
|
|
|
"parameter",
|
|
|
|
"punctuation",
|
|
|
|
"source",
|
|
|
|
"storage",
|
|
|
|
"string",
|
|
|
|
"support",
|
|
|
|
"tag",
|
|
|
|
"type",
|
|
|
|
"variable",
|
|
|
|
]
|
|
|
|
.iter(),
|
|
|
|
)
|
2019-11-20 15:16:38 +00:00
|
|
|
// Related to https://github.com/Plume-org/Plume/issues/637
|
|
|
|
.add_allowed_classes("sup", ["footnote-reference", "footnote-definition-label"].iter())
|
|
|
|
.add_allowed_classes("div", ["footnote-definition"].iter())
|
2019-04-06 17:20:33 +00:00
|
|
|
.attribute_filter(|elem, att, val| match (elem, att) {
|
|
|
|
("input", "type") => Some("checkbox".into()),
|
|
|
|
("input", "checked") => Some("checked".into()),
|
|
|
|
("label", "for") => {
|
|
|
|
if val.starts_with("postcontent-cw-") {
|
|
|
|
Some(val.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Some(val.into()),
|
|
|
|
});
|
2018-09-19 08:48:39 +00:00
|
|
|
b
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-15 16:05:42 +00:00
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
2020-01-21 06:02:03 +00:00
|
|
|
fn url_add_prefix(url: &str) -> Option<Cow<'_, str>> {
|
2018-11-24 11:44:17 +00:00
|
|
|
if url.starts_with('#') && !url.starts_with("#postcontent-") {
|
|
|
|
//if start with an #
|
|
|
|
let mut new_url = "#postcontent-".to_owned(); //change to valid id
|
2018-09-19 08:48:39 +00:00
|
|
|
new_url.push_str(&url[1..]);
|
|
|
|
Some(Cow::Owned(new_url))
|
|
|
|
} else {
|
|
|
|
Some(Cow::Borrowed(url))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 11:44:17 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, AsExpression, FromSqlRow, Default)]
|
2018-06-11 09:43:27 +00:00
|
|
|
#[sql_type = "Text"]
|
2018-11-24 11:44:17 +00:00
|
|
|
pub struct SafeString {
|
2018-06-11 09:43:27 +00:00
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
2018-11-24 11:44:17 +00:00
|
|
|
impl SafeString {
|
|
|
|
pub fn new(value: &str) -> Self {
|
|
|
|
SafeString {
|
2021-11-27 22:53:13 +00:00
|
|
|
value: CLEAN.clean(value).to_string(),
|
2018-06-11 09:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 13:11:36 +00:00
|
|
|
|
|
|
|
/// Creates a new `SafeString`, but without escaping the given value.
|
|
|
|
///
|
|
|
|
/// Only use when you are sure you can trust the input (when the HTML
|
|
|
|
/// is entirely generated by Plume, not depending on user-inputed data).
|
|
|
|
/// Prefer `SafeString::new` as much as possible.
|
|
|
|
pub fn trusted(value: impl AsRef<str>) -> Self {
|
|
|
|
SafeString {
|
2019-03-20 16:56:17 +00:00
|
|
|
value: value.as_ref().to_string(),
|
2019-03-06 13:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 09:43:27 +00:00
|
|
|
pub fn set(&mut self, value: &str) {
|
2018-09-19 08:48:39 +00:00
|
|
|
self.value = CLEAN.clean(value).to_string();
|
2018-06-11 09:43:27 +00:00
|
|
|
}
|
|
|
|
pub fn get(&self) -> &String {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for SafeString {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2018-11-24 11:44:17 +00:00
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2018-06-11 09:43:27 +00:00
|
|
|
serializer.serialize_str(&self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SafeStringVisitor;
|
|
|
|
|
|
|
|
impl<'de> Visitor<'de> for SafeStringVisitor {
|
|
|
|
type Value = SafeString;
|
|
|
|
|
2020-01-21 06:02:03 +00:00
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-06-11 09:43:27 +00:00
|
|
|
formatter.write_str("a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<SafeString, E>
|
2018-11-24 11:44:17 +00:00
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2018-06-11 09:43:27 +00:00
|
|
|
Ok(SafeString::new(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for SafeString {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2018-11-24 11:44:17 +00:00
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
2021-03-27 18:46:37 +00:00
|
|
|
deserializer.deserialize_string(SafeStringVisitor)
|
2018-06-11 09:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 17:58:15 +00:00
|
|
|
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
|
2018-06-11 09:43:27 +00:00
|
|
|
impl Queryable<Text, diesel::pg::Pg> for SafeString {
|
|
|
|
type Row = String;
|
|
|
|
fn build(value: Self::Row) -> Self {
|
|
|
|
SafeString::new(&value)
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 21:06:40 +00:00
|
|
|
|
2018-10-08 17:58:15 +00:00
|
|
|
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
|
2018-09-27 21:06:40 +00:00
|
|
|
impl Queryable<Text, diesel::sqlite::Sqlite> for SafeString {
|
|
|
|
type Row = String;
|
|
|
|
fn build(value: Self::Row) -> Self {
|
|
|
|
SafeString::new(&value)
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 17:58:15 +00:00
|
|
|
|
2018-06-11 09:43:27 +00:00
|
|
|
impl<DB> ToSql<diesel::sql_types::Text, DB> for SafeString
|
|
|
|
where
|
|
|
|
DB: diesel::backend::Backend,
|
2018-11-24 11:44:17 +00:00
|
|
|
str: ToSql<diesel::sql_types::Text, DB>,
|
|
|
|
{
|
2020-01-21 06:02:03 +00:00
|
|
|
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> serialize::Result {
|
2018-06-11 09:43:27 +00:00
|
|
|
str::to_sql(&self.value, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Borrow<str> for SafeString {
|
|
|
|
fn borrow(&self) -> &str {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for SafeString {
|
2020-01-21 06:02:03 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-06-11 09:43:27 +00:00
|
|
|
write!(f, "{}", self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SafeString {
|
|
|
|
type Target = str;
|
|
|
|
fn deref(&self) -> &str {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for SafeString {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 16:26:42 +00:00
|
|
|
|
|
|
|
use rocket::http::RawStr;
|
2018-11-24 11:44:17 +00:00
|
|
|
use rocket::request::FromFormValue;
|
2018-09-14 16:26:42 +00:00
|
|
|
|
|
|
|
impl<'v> FromFormValue<'v> for SafeString {
|
|
|
|
type Error = &'v RawStr;
|
|
|
|
|
|
|
|
fn from_form_value(form_value: &'v RawStr) -> Result<SafeString, &'v RawStr> {
|
|
|
|
let val = String::from_form_value(form_value)?;
|
2018-09-14 17:50:59 +00:00
|
|
|
Ok(SafeString::new(&val))
|
2018-09-14 16:26:42 +00:00
|
|
|
}
|
|
|
|
}
|