Remove share parameters at canonical_path

This commit is contained in:
Matthew Esposito 2023-12-29 19:34:57 -05:00
parent 45d8f1bbc8
commit 90a800ff44
No known key found for this signature in database
2 changed files with 10 additions and 6 deletions

View File

@ -59,12 +59,18 @@ pub async fn canonical_path(path: String) -> Result<Option<String>, String> {
301 => match res.headers().get(header::LOCATION) { 301 => match res.headers().get(header::LOCATION) {
Some(val) => { Some(val) => {
let original = val.to_str().unwrap(); let original = val.to_str().unwrap();
// We need to strip the .json suffix from the original path.
// In addition, we want to remove share parameters.
// Cut it off here instead of letting it propagate all the way
// to main.rs
let stripped_uri = original.strip_suffix(".json").unwrap_or(original).split('?').next().unwrap_or_default();
// The reason why we now have to format_url, is because the new OAuth // The reason why we now have to format_url, is because the new OAuth
// endpoints seem to return full paths, instead of relative paths. // endpoints seem to return full paths, instead of relative paths.
// So we need to strip the .json suffix from the original path, and // So we need to strip the .json suffix from the original path, and
// also remove all Reddit domain parts with format_url. // also remove all Reddit domain parts with format_url.
// Otherwise, it will literally redirect to Reddit.com. // Otherwise, it will literally redirect to Reddit.com.
let uri = format_url(original.strip_suffix(".json").unwrap_or(original)); let uri = format_url(stripped_uri);
Ok(Some(uri)) Ok(Some(uri))
} }
None => Ok(None), None => Ok(None),
@ -357,7 +363,8 @@ async fn test_localization_popular() {
#[tokio::test(flavor = "multi_thread", worker_threads = 8)] #[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn test_obfuscated_share_link() { async fn test_obfuscated_share_link() {
let share_link = "/r/rust/s/kPgq8WNHRK".into(); let share_link = "/r/rust/s/kPgq8WNHRK".into();
let canonical_link = "/r/rust/comments/18t5968/why_use_tuple_struct_over_standard_struct/kfbqlbc?share_id=N0wD38nOLSUMMNnWpDRO3&utm_content=2&utm_medium=android_app&utm_name=androidcss&utm_source=share&utm_term=1".into(); // Correct link without share parameters
let canonical_link = "/r/rust/comments/18t5968/why_use_tuple_struct_over_standard_struct/kfbqlbc".into();
assert_eq!(canonical_path(share_link).await, Ok(Some(canonical_link))); assert_eq!(canonical_path(share_link).await, Ok(Some(canonical_link)));
} }

View File

@ -335,10 +335,7 @@ async fn main() {
match req.param("id").as_deref() { match req.param("id").as_deref() {
// Share link // Share link
Some(id) if (8..12).contains(&id.len()) => match canonical_path(format!("/r/{}/s/{}", sub, id)).await { Some(id) if (8..12).contains(&id.len()) => match canonical_path(format!("/r/{}/s/{}", sub, id)).await {
Ok(Some(path)) => { Ok(Some(path)) => Ok(redirect(path)),
// Remove share parameters here.
Ok(redirect(path.split('?').next().unwrap_or_default().to_string()))
}
Ok(None) => error(req, "Post ID is invalid. It may point to a post on a community that has been banned.").await, Ok(None) => error(req, "Post ID is invalid. It may point to a post on a community that has been banned.").await,
Err(e) => error(req, e).await, Err(e) => error(req, e).await,
}, },