Skip to content

web search done right

Fixed XSS by not properly escaping search query

Mikkel Denker647044f27d89
diff --git a/Cargo.lock b/Cargo.lock
index 42fb854b..1ac839f4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -218,6 +218,18 @@ dependencies = [
"tower-service",
]
+[[package]]
+name = "axum-macros"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6293dae2ec708e679da6736e857cf8532886ef258e92930f38279c12641628b8"
+dependencies = [
+ "heck",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "base64"
version = "0.13.0"
@@ -690,6 +702,7 @@ dependencies = [
"async-trait",
"axum",
"axum-extra",
+ "axum-macros",
"base64",
"bincode",
"boomphf",
diff --git a/Cargo.toml b/Cargo.toml
index 8cb74bd4..673e0e3b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -59,6 +59,7 @@ min-max-heap = "1.3.0"
base64 = "0.13.0"
async-trait = "0.1.57"
boomphf = { version = "0.5.9", features = ["serde"] }
+axum-macros = "0.2.3"
[dev-dependencies]
criterion = "0.3.6"
diff --git a/frontend/src/components/Entity.astro b/frontend/src/components/Entity.astro
index 3aa87a58..182e854b 100644
--- a/frontend/src/components/Entity.astro
+++ b/frontend/src/components/Entity.astro
@@ -29,7 +29,7 @@ export interface Props {
</a>
</div>
<div class="text-sm">
- {askama`entity.small_abstract $ {{lorem.lines}}`}
+ {askama`entity.small_abstract|safe $ {{lorem.lines}}`}
<span class="italic">
source: <a
class="text-blue-600"
@@ -44,8 +44,8 @@ export interface Props {
<div class="grid grid-cols-[auto_1fr] gap-x-4 gap-y-2">
{askama.for_("(key, value) in entity.info", () => (
<>
- <div class="text-gray-500">{askama`key $ {{word.adjective}}`}</div>
- <div>{askama`value $ {{random.words}}`}</div>
+ <div class="text-gray-500">{askama`key|safe $ {{word.adjective}}`}</div>
+ <div>{askama`value|safe $ {{random.words}}`}</div>
</>
))}
</div>
diff --git a/frontend/src/pages/search.astro b/frontend/src/pages/search.astro
index a03eb0e5..07e46301 100644
--- a/frontend/src/pages/search.astro
+++ b/frontend/src/pages/search.astro
@@ -70,7 +70,7 @@ import { askama } from "../askama";
class="title-link font-medium"
href="/search?q={{ correction.raw|urlencode }}"
>
- {askama`correction.highlighted $ {{lorem.sentence}}`}
+ {askama`correction.highlighted|safe $ {{lorem.sentence}}`}
</a>
</div>
))
diff --git a/src/frontend/search.rs b/src/frontend/search.rs
index cebf5e35..558738ac 100644
--- a/src/frontend/search.rs
+++ b/src/frontend/search.rs
@@ -17,6 +17,7 @@
use std::{collections::HashMap, sync::Arc};
use axum::Extension;
+use axum_macros::debug_handler;
use crate::{
ranking::site_rankings::SiteRankings,
@@ -38,7 +39,7 @@ use axum::{
};
#[derive(Template)]
-#[template(path = "search/index.html", escape = "none")]
+#[template(path = "search/index.html")]
struct SearchTemplate {
search_result: Vec<DisplayedWebpage>,
query: String,
@@ -61,6 +62,7 @@ enum RegionSelection {
#[allow(clippy::unused_async)]
#[allow(clippy::match_wild_err_arm)]
+#[debug_handler]
pub async fn route(
extract::Query(params): extract::Query<HashMap<String, String>>,
Extension(state): Extension<Arc<State>>,