Percent-encode URI segments in ap_url

This commit is contained in:
Kitaiti Makoto 2020-05-03 06:05:58 +09:00
parent 73aa301d4a
commit 8253aa79bd
3 changed files with 21 additions and 7 deletions

View File

@ -20,6 +20,7 @@ use plume_common::activity_pub::{
inbox::{AsActor, FromId}, inbox::{AsActor, FromId},
sign, ActivityStream, ApSignature, Id, IntoId, PublicKey, Source, sign, ActivityStream, ApSignature, Id, IntoId, PublicKey, Source,
}; };
use rocket::http::uri::Uri;
use serde_json; use serde_json;
use url::Url; use url::Url;
use webfinger::*; use webfinger::*;
@ -481,13 +482,18 @@ impl NewBlog {
instance_id: i32, instance_id: i32,
) -> Result<NewBlog> { ) -> Result<NewBlog> {
let (pub_key, priv_key) = sign::gen_keypair(); let (pub_key, priv_key) = sign::gen_keypair();
let instance = Instance::get_local()?;
let encoded_actor_id = Uri::percent_encode(&actor_id);
Ok(NewBlog { Ok(NewBlog {
actor_id, actor_id: actor_id.clone(),
title, title,
summary, summary,
instance_id, instance_id,
public_key: String::from_utf8(pub_key).or(Err(Error::Signature))?, public_key: String::from_utf8(pub_key).or(Err(Error::Signature))?,
private_key: Some(String::from_utf8(priv_key).or(Err(Error::Signature))?), private_key: Some(String::from_utf8(priv_key).or(Err(Error::Signature))?),
outbox_url: instance.compute_box(BLOG_PREFIX, &encoded_actor_id, "outbox"),
inbox_url: instance.compute_box(BLOG_PREFIX, &encoded_actor_id, "inbox"),
ap_url: instance.compute_box(BLOG_PREFIX, &encoded_actor_id, ""),
..NewBlog::default() ..NewBlog::default()
}) })
} }

View File

@ -19,6 +19,7 @@ use plume_common::{
}, },
utils::md_to_html, utils::md_to_html,
}; };
use rocket::http::uri::Uri;
use serde_json; use serde_json;
use std::collections::HashSet; use std::collections::HashSet;
@ -69,11 +70,12 @@ impl Post {
.execute(conn)?; .execute(conn)?;
let mut post = Self::last(conn)?; let mut post = Self::last(conn)?;
if post.ap_url.is_empty() { if post.ap_url.is_empty() {
// Should use uri!(plume::routes::posts::detauls) if possible
post.ap_url = ap_url(&format!( post.ap_url = ap_url(&format!(
"{}/~/{}/{}/", "{}/~/{}/{}/",
CONFIG.base_url, CONFIG.base_url,
post.get_blog(conn)?.fqn, Uri::percent_encode(&post.get_blog(conn)?.fqn),
post.slug Uri::percent_encode(&post.slug)
)); ));
let _: Post = post.save_changes(conn)?; let _: Post = post.save_changes(conn)?;
} }

View File

@ -34,6 +34,7 @@ use reqwest::{
ClientBuilder, ClientBuilder,
}; };
use rocket::{ use rocket::{
http::uri::Uri,
outcome::IntoOutcome, outcome::IntoOutcome,
request::{self, FromRequest, Request}, request::{self, FromRequest, Request},
}; };
@ -992,6 +993,7 @@ impl NewUser {
return Err(Error::Blocklisted(x.notify_user, x.notification_text)); return Err(Error::Blocklisted(x.notify_user, x.notification_text));
} }
let encoded_username = Uri::percent_encode(&username);
let res = User::insert( let res = User::insert(
conn, conn,
NewUser { NewUser {
@ -1005,11 +1007,15 @@ impl NewUser {
instance_id: instance.id, instance_id: instance.id,
public_key: String::from_utf8(pub_key).or(Err(Error::Signature))?, public_key: String::from_utf8(pub_key).or(Err(Error::Signature))?,
private_key: Some(String::from_utf8(priv_key).or(Err(Error::Signature))?), private_key: Some(String::from_utf8(priv_key).or(Err(Error::Signature))?),
outbox_url: instance.compute_box(USER_PREFIX, &username, "outbox"), outbox_url: instance.compute_box(USER_PREFIX, &encoded_username, "outbox"),
inbox_url: instance.compute_box(USER_PREFIX, &username, "inbox"), inbox_url: instance.compute_box(USER_PREFIX, &encoded_username, "inbox"),
ap_url: instance.compute_box(USER_PREFIX, &username, ""), ap_url: instance.compute_box(USER_PREFIX, &encoded_username, ""),
shared_inbox_url: Some(ap_url(&format!("{}/inbox", &instance.public_domain))), shared_inbox_url: Some(ap_url(&format!("{}/inbox", &instance.public_domain))),
followers_endpoint: instance.compute_box(USER_PREFIX, &username, "followers"), followers_endpoint: instance.compute_box(
USER_PREFIX,
&encoded_username,
"followers",
),
fqn: username, fqn: username,
avatar_id: None, avatar_id: None,
}, },