Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(unfurl): sign fetches from our fediverse host with its key (#3780)

Hampus6e4070d4f7b3
diff --git a/fluxer_unfurl/src/activity_pub/signature.rs b/fluxer_unfurl/src/activity_pub/signature.rs
index 6f2808a8f..5a7f70371 100644
--- a/fluxer_unfurl/src/activity_pub/signature.rs
+++ b/fluxer_unfurl/src/activity_pub/signature.rs
@@ -26,15 +26,41 @@ pub fn public_key_base64(key: &RsaKeyPair) -> String {
BASE64_STANDARD.encode(key.public().as_ref())
}
+#[derive(Clone)]
+pub struct HostKey {
+ host: String,
+ key: Arc<RsaKeyPair>,
+ key_id: String,
+}
+
+impl HostKey {
+ pub fn new(key: RsaKeyPair, key_id: String) -> anyhow::Result<Self> {
+ let host = Url::parse(&key_id)?
+ .host_str()
+ .ok_or_else(|| anyhow::anyhow!("key id has no host"))?
+ .to_owned();
+ Ok(Self {
+ host,
+ key: Arc::new(key),
+ key_id,
+ })
+ }
+}
+
#[derive(Clone)]
pub struct RequestSigner {
key: Arc<RsaKeyPair>,
key_id: String,
+ host_key: Option<HostKey>,
}
impl RequestSigner {
- pub fn new(key: Arc<RsaKeyPair>, key_id: String) -> Self {
- Self { key, key_id }
+ pub fn new(key: Arc<RsaKeyPair>, key_id: String, host_key: Option<HostKey>) -> Self {
+ Self {
+ key,
+ key_id,
+ host_key,
+ }
}
pub fn headers(&self, url: &Url, at: SystemTime) -> anyhow::Result<HeaderMap> {
@@ -52,19 +78,22 @@ impl RequestSigner {
Some(query) => format!("{}?{query}", url.path()),
None => url.path().to_owned(),
};
+ let (key, key_id) = match &self.host_key {
+ Some(host_key) if host_key.host == host_name => (&host_key.key, &host_key.key_id),
+ _ => (&self.key, &self.key_id),
+ };
let signing_string = format!("(request-target): get {target}\nhost: {host}\ndate: {date}");
- let mut signature = vec![0; self.key.public().modulus_len()];
- self.key
- .sign(
- &RSA_PKCS1_SHA256,
- &SystemRandom::new(),
- signing_string.as_bytes(),
- &mut signature,
- )
- .map_err(|_| anyhow::anyhow!("failed to sign ActivityPub request"))?;
+ let mut signature = vec![0; key.public().modulus_len()];
+ key.sign(
+ &RSA_PKCS1_SHA256,
+ &SystemRandom::new(),
+ signing_string.as_bytes(),
+ &mut signature,
+ )
+ .map_err(|_| anyhow::anyhow!("failed to sign ActivityPub request"))?;
let signature_header = format!(
"keyId=\"{}\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date\",signature=\"{}\"",
- self.key_id,
+ key_id,
BASE64_STANDARD.encode(signature)
);
diff --git a/fluxer_unfurl/src/shard_impl.rs b/fluxer_unfurl/src/shard_impl.rs
index 37b7101e1..f8da92732 100644
--- a/fluxer_unfurl/src/shard_impl.rs
+++ b/fluxer_unfurl/src/shard_impl.rs
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
-use crate::activity_pub::signature::{self, RequestSigner};
+use crate::activity_pub::signature::{self, HostKey, RequestSigner};
use crate::cache_policy::{
UnfurlEntryExpiry, default_empty_entry_lifetime, provider_capability, unfurl_cache_key,
};
@@ -28,6 +28,7 @@ pub struct UnfurlShard {
youtube_api_key: Option<String>,
klipy_api_key: Option<String>,
activity_pub_key: Option<Arc<RsaKeyPair>>,
+ activity_pub_host_key: Option<HostKey>,
}
impl UnfurlShard {
@@ -82,6 +83,22 @@ impl UnfurlShard {
tracing::info!("ActivityPub request signing enabled");
Arc::new(key)
});
+ let activity_pub_host_key = match (
+ optional_env("FLUXER_ACTIVITYPUB_HOST_KEY_ID"),
+ optional_env("FLUXER_ACTIVITYPUB_HOST_SIGNING_KEY_BASE64"),
+ ) {
+ (Some(key_id), Some(encoded)) => {
+ let host_key = signature::parse_signing_key(&encoded)
+ .and_then(|key| HostKey::new(key, key_id))
+ .unwrap_or_else(|err| panic!("invalid ActivityPub host key: {err}"));
+ tracing::info!("ActivityPub host key enabled");
+ Some(host_key)
+ }
+ (None, None) => None,
+ _ => panic!(
+ "FLUXER_ACTIVITYPUB_HOST_KEY_ID and FLUXER_ACTIVITYPUB_HOST_SIGNING_KEY_BASE64 must be set together"
+ ),
+ };
Self {
cache: build_l2_cache(lifetime, default_empty_entry_lifetime()),
@@ -95,6 +112,7 @@ impl UnfurlShard {
klipy_api_key: optional_env("FLUXER_KLIPY_API_KEY")
.or_else(|| optional_env("KLIPY_API_KEY")),
activity_pub_key,
+ activity_pub_host_key,
}
}
@@ -127,11 +145,11 @@ impl UnfurlShard {
klipy_api_key: klipy_api_key
.map(str::to_owned)
.or_else(|| self.klipy_api_key.clone()),
- activity_pub_signer: self
- .activity_pub_key
- .clone()
- .zip(activity_pub_key_id)
- .map(|(key, key_id)| RequestSigner::new(key, key_id.to_owned())),
+ activity_pub_signer: self.activity_pub_key.clone().zip(activity_pub_key_id).map(
+ |(key, key_id)| {
+ RequestSigner::new(key, key_id.to_owned(), self.activity_pub_host_key.clone())
+ },
+ ),
};
if let Some(idx) = matched_resolver_idx {