Skip to content

Commit 705576f

Browse files
committed
Return 401 when an invalid password is used on an authenticated paste
Signed-off-by: Lilly Rose Berner <lilly@lostluma.net>
1 parent ce53f9c commit 705576f

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

echo/openapi.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,18 @@
128128
}
129129
}
130130
},
131+
"401": {
132+
"description": "You are not authorized to view the requested Paste.",
133+
"content": {
134+
"application/json": {
135+
"schema": {
136+
"$ref": "#/components/schemas/ErrorResponse"
137+
}
138+
}
139+
}
140+
},
131141
"404": {
132-
"description": "The requested Paste does not exist or you are not authorized to view it.",
142+
"description": "The requested Paste does not exist, has too many views, or has expired.",
133143
"content": {
134144
"application/json": {
135145
"schema": {

echo/src/database/pastes.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,28 @@ impl Paste {
2020
) -> Result<Paste> {
2121
let query = format!(
2222
"
23-
UPDATE
23+
WITH _ AS (
24+
UPDATE
25+
pastes
26+
SET
27+
views = views + 1
28+
WHERE
29+
id = $1 AND {}
30+
)
31+
SELECT
32+
created_at,
33+
expires_at,
34+
views + 1 AS views, -- account for in-progress update above
35+
max_views,
36+
CASE WHEN password IS NULL THEN true ELSE
37+
CASE WHEN $2 IS NULL THEN false ELSE password = CRYPT($2, password) END
38+
END AS authenticated
39+
FROM
2440
pastes
25-
SET
26-
views = views + 1
2741
WHERE
2842
id = $1 AND
2943
CASE WHEN max_views IS NULL THEN true ELSE views <= max_views END AND
30-
CASE WHEN expires_at IS NULL THEN true ELSE expires_at > CURRENT_TIMESTAMP END AND
31-
{}
32-
RETURNING
33-
created_at, expires_at, views, max_views
44+
CASE WHEN expires_at IS NULL THEN true ELSE expires_at > CURRENT_TIMESTAMP END
3445
",
3546
match password {
3647
None => "password IS NULL",
@@ -46,10 +57,21 @@ impl Paste {
4657

4758
match result {
4859
Ok(row) => {
49-
let files = File::fetch(conn, &id).await?;
50-
Ok(Paste::from_row(row, id, files, None))
60+
let authenticated = row.get("authenticated");
61+
62+
if authenticated {
63+
let files = File::fetch(conn, &id).await?;
64+
Ok(Paste::from_row(row, id, files, None))
65+
} else {
66+
Err(HTTPError::new(401, "Provided password is not valid."))
67+
}
68+
}
69+
Err(_) => {
70+
return Err(HTTPError::new(
71+
404,
72+
"Requested paste does not exist, has too many views, or has expired.",
73+
))
5174
}
52-
Err(_) => return Err(HTTPError::new(404, "Unknown paste or invalid password.")),
5375
}
5476
}
5577

0 commit comments

Comments
 (0)