chore: centralize blacklist check
This commit is contained in:
parent
f572a38622
commit
6482483791
4 changed files with 17 additions and 7 deletions
|
@ -136,3 +136,16 @@ impl TypeName for String {
|
|||
"String".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn strip_proto(url: &str) -> &str {
|
||||
url
|
||||
.strip_prefix("https://")
|
||||
.unwrap_or(url)
|
||||
.strip_prefix("http://")
|
||||
.unwrap_or(url)
|
||||
}
|
||||
|
||||
pub fn is_blacklisted(id: &str, blacklist: &[String]) -> bool {
|
||||
let stripped = strip_proto(id);
|
||||
blacklist.iter().any(|x| stripped.starts_with(x))
|
||||
}
|
||||
|
|
|
@ -198,9 +198,8 @@ async fn expand_addressing(targets: Vec<String>, audience: Option<String>, tx: &
|
|||
}
|
||||
|
||||
async fn expand_addressing_with_blacklist(id: &str, blacklist: &[String], mut targets: Vec<String>, audience: Option<String>, tx: &impl ConnectionTrait) -> Result<Vec<String>, DbErr> {
|
||||
let trimmed = id.replace("https://", "").replace("http://", "");
|
||||
if blacklist.iter().any(|x| trimmed.starts_with(x)) {
|
||||
targets.retain(|x| x != apb::target::PUBLIC && x != apb::target::PUBLIC_COMPACT);
|
||||
if crate::ext::is_blacklisted(id, blacklist) {
|
||||
targets.retain(|x| !apb::target::is_public(x));
|
||||
}
|
||||
expand_addressing(targets, audience, tx).await
|
||||
}
|
||||
|
|
|
@ -139,8 +139,7 @@ pub async fn cloak_proxy(
|
|||
let uri = ctx.uncloak(&hmac, &uri)
|
||||
.ok_or_else(ApiError::unauthorized)?;
|
||||
|
||||
let stripped = uri.replace("https://", "").replace("http://", "");
|
||||
if ctx.cfg().reject.media.iter().any(|x| stripped.starts_with(x)) {
|
||||
if upub::ext::is_blacklisted(&uri, &ctx.cfg().reject.media) {
|
||||
return Err(ApiError::Status(axum::http::StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS));
|
||||
}
|
||||
|
||||
|
|
|
@ -188,8 +188,7 @@ pub async fn process(ctx: Context, job: &model::job::Model) -> crate::JobResult<
|
|||
|
||||
targets
|
||||
.retain(|target| {
|
||||
let stripped = target.replace("https://", "").replace("http://", "");
|
||||
if ctx.cfg().reject.delivery.iter().any(|x| stripped.starts_with(x)) {
|
||||
if upub::ext::is_blacklisted(target, &ctx.cfg().reject.delivery) {
|
||||
tracing::warn!("rejecting delivery of {} to {target}", job.activity);
|
||||
false
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue