mirror of
https://github.com/fluxerapp/fluxer.git
synced 2026-11-20 01:01:18 -08:00
feat(media): serve attachments through the api when enabled (#4068)
This commit is contained in:
1 parent
deb395fcc3
commit
847d66d888
107 files changed
+4102
-256
No files matched your search
Generated
+7
-2
@@ -18366,9 +18366,10 @@
|
||||
"effective"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"proxy_attachments_through_api": {"type": "boolean"}
|
||||
},
|
||||
"required": ["attachment_decay"],
|
||||
"required": ["attachment_decay", "proxy_attachments_through_api"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"billing": {"$ref": "#/components/schemas/InstanceBillingResponse"},
|
||||
@@ -18887,6 +18888,10 @@
|
||||
"maximum": 9007199254740991
|
||||
}
|
||||
}
|
||||
},
|
||||
"proxy_attachments_through_api": {
|
||||
"description": "Whether attachment links point at the API, which checks that the viewer can read the message before it hands over the file",
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -358,6 +358,8 @@ pub struct InstanceBlueskyIntegrationResponse {
|
||||
pub struct InstanceMediaResponse {
|
||||
#[serde(default)]
|
||||
pub attachment_decay: InstanceAttachmentDecayResponse,
|
||||
#[serde(default)]
|
||||
pub proxy_attachments_through_api: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||
@@ -1204,6 +1206,8 @@ pub struct InstanceBlueskyKeyIntegrationUpdateRequest {
|
||||
pub struct InstanceMediaUpdateRequest {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub attachment_decay: Option<InstanceAttachmentDecayUpdateRequest>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub proxy_attachments_through_api: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
|
||||
@@ -218,6 +218,7 @@ pub async fn instance_config_post(
|
||||
"update_app_legal" => Ok(app_public::build_app_legal_update(&form)),
|
||||
"update_policy" => Ok(policy::build_policy_update(&form)),
|
||||
"update_media" => Ok(build_media_update(&form)),
|
||||
"update_media_access" => Ok(build_media_access_update(&form)),
|
||||
"update_billing" => {
|
||||
super::billing_actions::build_billing_update(&form).map_err(FlashData::error)
|
||||
}
|
||||
@@ -551,6 +552,19 @@ fn build_media_update(form: &MultiValueForm) -> InstanceConfigUpdateRequest {
|
||||
"media_attachment_decay_renew_window_days",
|
||||
),
|
||||
}),
|
||||
proxy_attachments_through_api: None,
|
||||
}),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn build_media_access_update(form: &MultiValueForm) -> InstanceConfigUpdateRequest {
|
||||
InstanceConfigUpdateRequest {
|
||||
media: Some(InstanceMediaUpdateRequest {
|
||||
attachment_decay: None,
|
||||
proxy_attachments_through_api: form
|
||||
.first("media_proxy_attachments_through_api")
|
||||
.map(|value| value == "true"),
|
||||
}),
|
||||
..Default::default()
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ use self::{
|
||||
connections::connection_providers_section,
|
||||
experiments::{domain_migration_section, voice_p2p_section},
|
||||
integrations::integrations_config_section,
|
||||
media::{HostingMode, media_config_section},
|
||||
media::{HostingMode, media_access_section, media_config_section},
|
||||
policy::policy_config_section,
|
||||
sso::sso_config_section,
|
||||
};
|
||||
@@ -273,8 +273,9 @@ pub fn instance_config_page(
|
||||
))
|
||||
(config_group(
|
||||
"Media & retention",
|
||||
"Attachment expiry rules that can be changed without editing environment variables.",
|
||||
media_config_section(
|
||||
"Attachment expiry and access rules that can be changed without editing environment variables.",
|
||||
html! {
|
||||
(media_config_section(
|
||||
base,
|
||||
config_form,
|
||||
&instance_config.media,
|
||||
@@ -283,7 +284,9 @@ pub fn instance_config_page(
|
||||
} else {
|
||||
HostingMode::Hosted
|
||||
},
|
||||
),
|
||||
))
|
||||
(media_access_section(base, config_form, &instance_config.media))
|
||||
},
|
||||
))
|
||||
(config_group(
|
||||
"Infrastructure & limits",
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::{
|
||||
api::types::InstanceMediaResponse,
|
||||
templates::components::{
|
||||
badge::{BadgeVariant, badge},
|
||||
form::{NumberField, form_actions, submit_button},
|
||||
form::{NumberField, form_actions, select_input, submit_button},
|
||||
section_card::section_card_with_description,
|
||||
},
|
||||
};
|
||||
@@ -167,3 +167,34 @@ pub(super) fn media_config_section(
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn media_access_section(
|
||||
base: &str,
|
||||
config_form: ConfigForm<'_>,
|
||||
media: &InstanceMediaResponse,
|
||||
) -> Markup {
|
||||
let options: &[(&str, &str)] = &[("true", "Enabled"), ("false", "Disabled")];
|
||||
section_card_with_description(
|
||||
"Attachment Access",
|
||||
"Choose where attachment links point. When this is on, every link points at the API, which checks that the viewer can read the message before it hands over the file. Data exports keep their own links.",
|
||||
html! {
|
||||
form method="post" action={(base) "/instance-config?action=update_media_access"} {
|
||||
(config_form.fields())
|
||||
div class="space-y-4" {
|
||||
(select_input(
|
||||
"media_proxy_attachments_through_api",
|
||||
"Serve attachments through the API",
|
||||
options,
|
||||
if media.proxy_attachments_through_api { "true" } else { "false" },
|
||||
))
|
||||
p class="text-xs text-neutral-500" {
|
||||
"Links handed out before the change keep working until they expire. Set the media proxy to enforce signatures, or anyone who guesses a file link can fetch it without signing in. Turning this off again breaks links handed out while it was on, such as favorite GIFs saved from attachments."
|
||||
}
|
||||
(form_actions(html! {
|
||||
(submit_button("Save attachment access"))
|
||||
}))
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -903,6 +903,9 @@ const INSTANCE_CONFIG_SECTION_APPLIERS = {
|
||||
renew_window_days: readOptionalField(media.attachment_decay, 'renew_window_days'),
|
||||
})
|
||||
: undefined,
|
||||
...omitUndefinedFields({
|
||||
proxy_attachments_through_api: readOptionalField(media, 'proxy_attachments_through_api'),
|
||||
}),
|
||||
});
|
||||
const attachmentDecay = await instanceConfigRepository.getEffectiveAttachmentDecayConfig();
|
||||
if (attachmentDecay.enabled && attachmentExpiryRulesChanged(previousAttachmentDecay, attachmentDecay)) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import {ActivityPubController} from '@app/api/activity_pub/ActivityPubController
|
||||
import {AdminDownloadController} from '@app/api/admin/AdminDownloadController';
|
||||
import {registerAdminControllers} from '@app/api/admin/controllers/index';
|
||||
import {AttachmentController} from '@app/api/attachment/AttachmentController';
|
||||
import {AttachmentMediaController} from '@app/api/attachment/AttachmentMediaController';
|
||||
import {AccountEntryController} from '@app/api/auth/AccountEntryController';
|
||||
import {AuthController} from '@app/api/auth/AuthController';
|
||||
import {OriginHandoffController} from '@app/api/auth/OriginHandoffController';
|
||||
@@ -94,4 +95,5 @@ export function registerControllers(routes: HonoApp, config: APIConfig): void {
|
||||
BillingDocumentController(routes);
|
||||
}
|
||||
StripeController(routes);
|
||||
AttachmentMediaController(routes);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {publicAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {LoginRequired} from '@app/api/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';
|
||||
import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';
|
||||
@@ -26,12 +26,12 @@ export function AttachmentController(app: HonoApp) {
|
||||
security: ['botToken', 'sessionToken'],
|
||||
tags: ['Messages'],
|
||||
description:
|
||||
'Reissues the expiring signature on attachment URLs. Returns one entry per requested URL, in the order they were requested, each pairing the URL exactly as it was sent with a freshly signed copy. A URL that is not an attachment URL of this instance is returned unchanged. No membership or existence check is performed.',
|
||||
'Reissues the expiring signature on attachment URLs. Returns one entry per requested URL, in the order they were requested, each pairing the URL exactly as it was sent with a freshly signed copy. A URL that is not an attachment URL of this instance is returned unchanged. No membership or existence check is performed. When the instance serves attachments through the API, the refreshed URL points at the API and has no signature.',
|
||||
}),
|
||||
async (ctx) => {
|
||||
const urls = ctx.req.valid('json').attachment_urls;
|
||||
return ctx.json({
|
||||
refreshed_urls: urls.map((original) => ({original, refreshed: signAttachmentUrl(original)})),
|
||||
refreshed_urls: urls.map((original) => ({original, refreshed: publicAttachmentUrl(original)})),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {encodeStorageKey} from '@app/api/attachment/AttachmentMediaUpstream';
|
||||
import {
|
||||
type ApiAttachmentLocation,
|
||||
ownAttachmentLocation,
|
||||
parseApiAttachmentUrl,
|
||||
} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {type MessageID, userIdToChannelId} from '@app/api/BrandedTypes';
|
||||
import {Config} from '@app/api/Config';
|
||||
import type {ChannelRepository} from '@app/api/channel/ChannelRepository';
|
||||
import {
|
||||
attachmentStorageChannelId,
|
||||
isOwnedEmbedAttachment,
|
||||
makeAttachmentCdnKey,
|
||||
} from '@app/api/channel/services/message/MessageHelpers';
|
||||
import type {MessageRequestService} from '@app/api/channel/services/message/MessageRequestService';
|
||||
import {type ThreadViewer, viewerFromCtx} from '@app/api/experiment/ChannelThreadsGate';
|
||||
import type {FavoriteMemeRepository} from '@app/api/favorite_meme/FavoriteMemeRepository';
|
||||
import {isApiProxiedAttachments} from '@app/api/instance/AttachmentAccessMode';
|
||||
import type {RequestCache} from '@app/api/middleware/RequestCacheMiddleware';
|
||||
import {getFavoriteMemeRepository} from '@app/api/middleware/ServiceSingletons';
|
||||
import type {Embed} from '@app/api/models/Embed';
|
||||
import type {Message} from '@app/api/models/Message';
|
||||
import type {User} from '@app/api/models/User';
|
||||
import type {HonoEnv} from '@app/api/types/HonoEnv';
|
||||
import {FluxerError} from '@fluxer/errors/src/FluxerError';
|
||||
import type {MessageEmbedChildResponse} from '@fluxer/schema/src/domains/message/EmbedSchemas';
|
||||
import type {
|
||||
MessageAttachmentResponse,
|
||||
MessageResponse,
|
||||
} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';
|
||||
import {attachmentStorageKeyFromUrl} from '@pkgs/media_proxy_utils/src/AttachmentUrlSignature';
|
||||
import type {Context} from 'hono';
|
||||
|
||||
const DECISION_TTL_MS = 15_000;
|
||||
const DECISION_CACHE_MAX_ENTRIES = 10_000;
|
||||
|
||||
interface CachedDecision {
|
||||
storageKey: string;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
const decisionCache = new Map<string, CachedDecision>();
|
||||
|
||||
interface AttachmentMediaAccessRequest {
|
||||
user: User;
|
||||
viewer: ThreadViewer;
|
||||
credentialKey: string;
|
||||
location: ApiAttachmentLocation;
|
||||
requestCache: RequestCache;
|
||||
}
|
||||
|
||||
interface AttachmentMediaAccessDeps {
|
||||
messageRequestService: MessageRequestService;
|
||||
channelRepository: ChannelRepository;
|
||||
favoriteMemeRepository: FavoriteMemeRepository;
|
||||
}
|
||||
|
||||
function decisionKey(request: AttachmentMediaAccessRequest): string {
|
||||
const {location} = request;
|
||||
return [
|
||||
request.user.id,
|
||||
request.credentialKey,
|
||||
request.viewer.kind === 'user' && request.viewer.capable ? 'capable' : 'basic',
|
||||
location.channelId,
|
||||
location.messageId ?? '',
|
||||
location.attachmentId,
|
||||
location.filename,
|
||||
].join('\u0000');
|
||||
}
|
||||
|
||||
function readCachedDecision(key: string): string | null {
|
||||
const cached = decisionCache.get(key);
|
||||
if (!cached) return null;
|
||||
if (cached.expiresAt <= Date.now()) {
|
||||
decisionCache.delete(key);
|
||||
return null;
|
||||
}
|
||||
return cached.storageKey;
|
||||
}
|
||||
|
||||
function rememberDecision(key: string, storageKey: string): void {
|
||||
decisionCache.delete(key);
|
||||
decisionCache.set(key, {storageKey, expiresAt: Date.now() + DECISION_TTL_MS});
|
||||
if (decisionCache.size > DECISION_CACHE_MAX_ENTRIES) {
|
||||
const oldest = decisionCache.keys().next().value;
|
||||
if (oldest !== undefined) decisionCache.delete(oldest);
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAttachmentMediaDecisionsForTesting(): void {
|
||||
decisionCache.clear();
|
||||
}
|
||||
|
||||
function isServedAttachment(
|
||||
attachments: ReadonlyArray<MessageAttachmentResponse> | null | undefined,
|
||||
location: ApiAttachmentLocation,
|
||||
): boolean {
|
||||
return (attachments ?? []).some(
|
||||
(attachment) =>
|
||||
attachment.id === location.attachmentId.toString() &&
|
||||
attachment.filename === location.filename &&
|
||||
attachment.url != null &&
|
||||
attachment.expired !== true,
|
||||
);
|
||||
}
|
||||
|
||||
function embedResponseUrls(
|
||||
embed: MessageEmbedChildResponse & {children?: Array<MessageEmbedChildResponse> | null},
|
||||
): Array<string> {
|
||||
const media = [embed.image, embed.thumbnail, embed.video, embed.audio];
|
||||
return [
|
||||
embed.url,
|
||||
embed.author?.icon_url,
|
||||
embed.author?.proxy_icon_url,
|
||||
embed.footer?.icon_url,
|
||||
embed.footer?.proxy_icon_url,
|
||||
...media.flatMap((entry) => [entry?.url, entry?.proxy_url]),
|
||||
...(embed.children ?? []).flatMap((child) => embedResponseUrls(child)),
|
||||
].filter((url): url is string => typeof url === 'string');
|
||||
}
|
||||
|
||||
function isServedEmbedUrl(
|
||||
embeds: ReadonlyArray<MessageEmbedChildResponse> | null | undefined,
|
||||
location: ApiAttachmentLocation,
|
||||
messageId: MessageID,
|
||||
): boolean {
|
||||
return (embeds ?? [])
|
||||
.flatMap((embed) => embedResponseUrls(embed))
|
||||
.map((url) => parseApiAttachmentUrl(url))
|
||||
.some(
|
||||
(served) =>
|
||||
served !== null &&
|
||||
served.channelId === location.channelId &&
|
||||
served.attachmentId === location.attachmentId &&
|
||||
served.filename === location.filename &&
|
||||
served.messageId === messageId,
|
||||
);
|
||||
}
|
||||
|
||||
function ownedEmbedMediaUrls(embed: Embed): Array<string> {
|
||||
return [
|
||||
...[embed.image, embed.thumbnail, embed.video, embed.audio]
|
||||
.filter((media) => media !== null && isOwnedEmbedAttachment(media))
|
||||
.map((media) => media?.url),
|
||||
...embed.children.flatMap((child) => ownedEmbedMediaUrls(child)),
|
||||
].filter((url): url is string => typeof url === 'string');
|
||||
}
|
||||
|
||||
function hasOwnedEmbedKey(embeds: ReadonlyArray<Embed>, storageKey: string): boolean {
|
||||
return embeds
|
||||
.flatMap((embed) => ownedEmbedMediaUrls(embed))
|
||||
.some((url) => attachmentStorageKeyFromUrl(url, Config.endpoints.media) === storageKey);
|
||||
}
|
||||
|
||||
function storedAttachmentKey(
|
||||
message: Message,
|
||||
response: MessageResponse,
|
||||
location: ApiAttachmentLocation,
|
||||
messageId: MessageID,
|
||||
): string | null {
|
||||
const {attachmentId, filename} = location;
|
||||
const matches = (attachment: {id: bigint; filename: string}) =>
|
||||
attachment.id === attachmentId && attachment.filename === filename;
|
||||
const storageChannelId = attachmentStorageChannelId(message);
|
||||
if (message.attachments.some(matches) && isServedAttachment(response.attachments, location)) {
|
||||
return makeAttachmentCdnKey(storageChannelId, attachmentId, filename);
|
||||
}
|
||||
const snapshots = response.message_snapshots ?? [];
|
||||
if (
|
||||
message.messageSnapshots.some((snapshot) => snapshot.attachments.some(matches)) &&
|
||||
snapshots.some((snapshot) => isServedAttachment(snapshot.attachments, location))
|
||||
) {
|
||||
return makeAttachmentCdnKey(message.channelId, attachmentId, filename);
|
||||
}
|
||||
if (location.messageId === null) return null;
|
||||
const ownedKey = makeAttachmentCdnKey(storageChannelId, attachmentId, filename);
|
||||
if (hasOwnedEmbedKey(message.embeds, ownedKey) && isServedEmbedUrl(response.embeds, location, messageId)) {
|
||||
return ownedKey;
|
||||
}
|
||||
const snapshotKey = makeAttachmentCdnKey(message.channelId, attachmentId, filename);
|
||||
if (
|
||||
message.messageSnapshots.some((snapshot) => hasOwnedEmbedKey(snapshot.embeds, snapshotKey)) &&
|
||||
snapshots.some((snapshot) => isServedEmbedUrl(snapshot.embeds, location, messageId))
|
||||
) {
|
||||
return snapshotKey;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function isHiddenFromViewer(error: unknown): boolean {
|
||||
return error instanceof FluxerError && (error.status === 403 || error.status === 404);
|
||||
}
|
||||
|
||||
async function resolveFavoriteMeme(
|
||||
deps: AttachmentMediaAccessDeps,
|
||||
{user, location}: AttachmentMediaAccessRequest,
|
||||
): Promise<string | null> {
|
||||
const memes = await deps.favoriteMemeRepository.findByUserId(user.id);
|
||||
const owned = memes.some(
|
||||
(meme) => meme.attachmentId === location.attachmentId && meme.filename === location.filename,
|
||||
);
|
||||
return owned ? makeAttachmentCdnKey(userIdToChannelId(user.id), location.attachmentId, location.filename) : null;
|
||||
}
|
||||
|
||||
async function resolveUncached(
|
||||
deps: AttachmentMediaAccessDeps,
|
||||
request: AttachmentMediaAccessRequest,
|
||||
): Promise<string | null> {
|
||||
const {location, user} = request;
|
||||
if (location.messageId === null && location.channelId.toString() === user.id.toString()) {
|
||||
return resolveFavoriteMeme(deps, request);
|
||||
}
|
||||
const messageId =
|
||||
location.messageId ??
|
||||
(await deps.channelRepository.messages.lookupAttachmentByChannelAndFilename(
|
||||
location.channelId,
|
||||
location.attachmentId,
|
||||
location.filename,
|
||||
));
|
||||
if (messageId === null) return null;
|
||||
let response: MessageResponse;
|
||||
try {
|
||||
response = await deps.messageRequestService.getMessage({
|
||||
userId: user.id,
|
||||
viewer: request.viewer,
|
||||
channelId: location.channelId,
|
||||
messageId,
|
||||
requestCache: request.requestCache,
|
||||
});
|
||||
} catch (error) {
|
||||
if (isHiddenFromViewer(error)) return null;
|
||||
throw error;
|
||||
}
|
||||
const message = await deps.channelRepository.messages.getMessage(location.channelId, messageId);
|
||||
if (!message) return null;
|
||||
return storedAttachmentKey(message, response, location, messageId);
|
||||
}
|
||||
|
||||
function credentialKey(ctx: Context<HonoEnv>): string {
|
||||
const session = ctx.get('authSession');
|
||||
if (ctx.get('authTokenType') === 'bot' || !session) return ctx.get('authTokenType') ?? 'none';
|
||||
return Buffer.from(session.sessionIdHash).toString('base64url');
|
||||
}
|
||||
|
||||
export async function resolveAttachmentForRequester(
|
||||
ctx: Context<HonoEnv>,
|
||||
location: ApiAttachmentLocation,
|
||||
): Promise<string | null> {
|
||||
const request: AttachmentMediaAccessRequest = {
|
||||
user: ctx.get('user'),
|
||||
viewer: viewerFromCtx(ctx),
|
||||
credentialKey: credentialKey(ctx),
|
||||
location,
|
||||
requestCache: ctx.get('requestCache'),
|
||||
};
|
||||
const key = decisionKey(request);
|
||||
const cached = readCachedDecision(key);
|
||||
if (cached !== null) return cached;
|
||||
const storageKey = await resolveUncached(
|
||||
{
|
||||
messageRequestService: ctx.get('messageRequestService'),
|
||||
channelRepository: ctx.get('channelRepository'),
|
||||
favoriteMemeRepository: getFavoriteMemeRepository(),
|
||||
},
|
||||
request,
|
||||
);
|
||||
if (storageKey !== null) rememberDecision(key, storageKey);
|
||||
return storageKey;
|
||||
}
|
||||
|
||||
export async function mediaUrlForRequester(ctx: Context<HonoEnv>, url: string): Promise<string | null | undefined> {
|
||||
if (!isApiProxiedAttachments()) return undefined;
|
||||
const location = ownAttachmentLocation(url);
|
||||
if (location === null) return undefined;
|
||||
const storageKey = await resolveAttachmentForRequester(ctx, location);
|
||||
if (storageKey === null) return null;
|
||||
return `${Config.endpoints.media.replace(/\/+$/u, '')}/${encodeStorageKey(storageKey)}`;
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {resolveAttachmentForRequester} from '@app/api/attachment/AttachmentMediaAccess';
|
||||
import {ATTACHMENT_MEDIA_TOKEN_TTL_MS, sealAttachmentMediaToken} from '@app/api/attachment/AttachmentMediaToken';
|
||||
import {fetchAttachmentFromMediaProxy, readTransformQuery} from '@app/api/attachment/AttachmentMediaUpstream';
|
||||
import {
|
||||
type ApiAttachmentLocation,
|
||||
parseApiAttachmentRoute,
|
||||
parseApiAttachmentUrl,
|
||||
} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {isApiProxiedAttachments} from '@app/api/instance/AttachmentAccessMode';
|
||||
import {DefaultUserOnly, LoginRequired} from '@app/api/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';
|
||||
import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';
|
||||
import {RateLimitConfigs} from '@app/api/RateLimitConfig';
|
||||
import type {HonoApp, HonoEnv} from '@app/api/types/HonoEnv';
|
||||
import {stripApiPrefix} from '@app/api/utils/RequestPathUtils';
|
||||
import {Validator} from '@app/api/Validator';
|
||||
import {AccessDeniedError} from '@fluxer/errors/src/domains/core/AccessDeniedError';
|
||||
import {
|
||||
AttachmentAccessTokenRequest,
|
||||
AttachmentAccessTokenResponse,
|
||||
AttachmentMediaResponse,
|
||||
} from '@fluxer/schema/src/domains/message/AttachmentSchemas';
|
||||
import type {Context, MiddlewareHandler} from 'hono';
|
||||
|
||||
const MAX_REQUESTS_PER_USER = 64;
|
||||
const MAX_STREAMS_PER_USER = 16;
|
||||
const PASSED_THROUGH_STATUSES = new Set([200, 206, 304, 416]);
|
||||
const DENIED_UPSTREAM_STATUSES = new Set([403, 404]);
|
||||
const PASSED_THROUGH_HEADERS = [
|
||||
'content-type',
|
||||
'content-length',
|
||||
'content-range',
|
||||
'accept-ranges',
|
||||
'content-disposition',
|
||||
'etag',
|
||||
'last-modified',
|
||||
] as const;
|
||||
const STREAMED_MEDIA_PREFIXES = ['audio/', 'video/'];
|
||||
|
||||
const openRequests = new Map<string, number>();
|
||||
const openStreams = new Map<string, number>();
|
||||
|
||||
function enabledOnly(): MiddlewareHandler<HonoEnv> {
|
||||
return async (ctx, next) => {
|
||||
if (!isApiProxiedAttachments()) return ctx.notFound();
|
||||
return next();
|
||||
};
|
||||
}
|
||||
|
||||
function emptyResponse(status: number, extra: Record<string, string> = {}): Response {
|
||||
return new Response(null, {status, headers: {'Cache-Control': 'no-store', ...extra}});
|
||||
}
|
||||
|
||||
function notFound(): Response {
|
||||
return emptyResponse(404);
|
||||
}
|
||||
|
||||
function requestedLocation(ctx: Context<HonoEnv>): ApiAttachmentLocation | null {
|
||||
const url = new URL(ctx.req.url);
|
||||
const mids = url.searchParams.getAll('mid');
|
||||
if (mids.length > 1) return null;
|
||||
return parseApiAttachmentRoute(stripApiPrefix(url.pathname), mids[0] ?? null);
|
||||
}
|
||||
|
||||
function scopeMatches(ctx: Context<HonoEnv>, location: ApiAttachmentLocation): boolean {
|
||||
if (ctx.get('authTokenType') !== 'attachment_media') return true;
|
||||
const token = ctx.get('attachmentMediaToken');
|
||||
return (
|
||||
token !== undefined && token.c === location.channelId.toString() && token.a === location.attachmentId.toString()
|
||||
);
|
||||
}
|
||||
|
||||
function acquireSlot(counts: Map<string, number>, limit: number, userKey: string): (() => void) | null {
|
||||
const current = counts.get(userKey) ?? 0;
|
||||
if (current >= limit) return null;
|
||||
counts.set(userKey, current + 1);
|
||||
let released = false;
|
||||
return () => {
|
||||
if (released) return;
|
||||
released = true;
|
||||
const remaining = (counts.get(userKey) ?? 1) - 1;
|
||||
if (remaining <= 0) counts.delete(userKey);
|
||||
else counts.set(userKey, remaining);
|
||||
};
|
||||
}
|
||||
|
||||
function isStreamedMedia(headers: Headers): boolean {
|
||||
const contentType = headers.get('content-type')?.toLowerCase() ?? '';
|
||||
return STREAMED_MEDIA_PREFIXES.some((prefix) => contentType.startsWith(prefix));
|
||||
}
|
||||
|
||||
function trackedBody(body: ReadableStream<Uint8Array>, release: () => void): ReadableStream<Uint8Array> {
|
||||
const reader = body.getReader();
|
||||
return new ReadableStream<Uint8Array>(
|
||||
{
|
||||
async pull(controller) {
|
||||
try {
|
||||
const {done, value} = await reader.read();
|
||||
if (done) {
|
||||
release();
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
controller.enqueue(value);
|
||||
} catch (error) {
|
||||
release();
|
||||
controller.error(error);
|
||||
}
|
||||
},
|
||||
async cancel(reason) {
|
||||
release();
|
||||
await reader.cancel(reason);
|
||||
},
|
||||
},
|
||||
{highWaterMark: 0},
|
||||
);
|
||||
}
|
||||
|
||||
function cacheControl(streamed: boolean, viaFileToken: boolean): string {
|
||||
if (viaFileToken) return streamed ? 'private, no-store, no-transform' : 'private, no-store';
|
||||
return streamed ? 'private, max-age=3600, no-transform' : 'private, max-age=3600';
|
||||
}
|
||||
|
||||
function mediaHeaders(upstream: Response, viaFileToken: boolean): Headers {
|
||||
const headers = new Headers();
|
||||
for (const name of PASSED_THROUGH_HEADERS) {
|
||||
const value = upstream.headers.get(name);
|
||||
if (value !== null) headers.set(name, value);
|
||||
}
|
||||
headers.set('Cache-Control', cacheControl(isStreamedMedia(headers), viaFileToken));
|
||||
headers.set('CDN-Cache-Control', 'no-store');
|
||||
headers.set('Vary', 'Authorization');
|
||||
headers.set('X-Content-Type-Options', 'nosniff');
|
||||
headers.set('Referrer-Policy', 'no-referrer');
|
||||
headers.set('Content-Security-Policy', "default-src 'none'; sandbox");
|
||||
headers.set('Cross-Origin-Resource-Policy', 'cross-origin');
|
||||
return headers;
|
||||
}
|
||||
|
||||
export function AttachmentMediaController(app: HonoApp) {
|
||||
app.get(
|
||||
'/channels/:channel_id/attachments/:attachment_id/:filename{.+}',
|
||||
enabledOnly(),
|
||||
async (ctx, next) => {
|
||||
if (!ctx.get('user') && ctx.req.query('mt') !== undefined) return emptyResponse(401);
|
||||
return next();
|
||||
},
|
||||
LoginRequired,
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_ATTACHMENT_GET),
|
||||
OpenAPI({
|
||||
operationId: 'get_attachment_file',
|
||||
summary: 'Fetch an attachment file',
|
||||
responseSchema: AttachmentMediaResponse,
|
||||
responseContentType: 'application/octet-stream',
|
||||
statusCode: [200, 206],
|
||||
security: ['botToken', 'sessionToken'],
|
||||
tags: ['Messages'],
|
||||
description:
|
||||
'Streams an attachment file after checking that the requester can read the message it belongs to. Only active when the instance serves attachments through the API. Takes the same width, height, format, quality, animated and download query parameters as the media proxy, and supports range requests. A link opened where no header can be sent, such as a video player or a download, can add the mt query parameter with a token for that one file from the attachment token endpoint.',
|
||||
}),
|
||||
async (ctx) => {
|
||||
const location = requestedLocation(ctx);
|
||||
if (!location || !scopeMatches(ctx, location)) {
|
||||
return location ? emptyResponse(401) : notFound();
|
||||
}
|
||||
const query = readTransformQuery(new URL(ctx.req.url).searchParams);
|
||||
if (query === null) return emptyResponse(400);
|
||||
const user = ctx.get('user');
|
||||
const storageKey = await resolveAttachmentForRequester(ctx, location);
|
||||
if (storageKey === null) return notFound();
|
||||
const userKey = user.id.toString();
|
||||
const release = acquireSlot(openRequests, MAX_REQUESTS_PER_USER, userKey);
|
||||
if (release === null) return emptyResponse(429, {'Retry-After': '1'});
|
||||
let upstream: Response;
|
||||
try {
|
||||
upstream = await fetchAttachmentFromMediaProxy({
|
||||
storageKey,
|
||||
query,
|
||||
method: ctx.req.method === 'HEAD' ? 'HEAD' : 'GET',
|
||||
headers: ctx.req.raw.headers,
|
||||
signal: ctx.req.raw.signal,
|
||||
});
|
||||
} catch (error) {
|
||||
release();
|
||||
throw error;
|
||||
}
|
||||
if (!PASSED_THROUGH_STATUSES.has(upstream.status)) {
|
||||
release();
|
||||
await upstream.body?.cancel();
|
||||
return DENIED_UPSTREAM_STATUSES.has(upstream.status) ? notFound() : emptyResponse(502);
|
||||
}
|
||||
const viaFileToken = ctx.get('authTokenType') === 'attachment_media';
|
||||
if (!upstream.body || ctx.req.method === 'HEAD') {
|
||||
release();
|
||||
await upstream.body?.cancel();
|
||||
return new Response(null, {status: upstream.status, headers: mediaHeaders(upstream, viaFileToken)});
|
||||
}
|
||||
const releaseStream = isStreamedMedia(upstream.headers)
|
||||
? acquireSlot(openStreams, MAX_STREAMS_PER_USER, userKey)
|
||||
: () => {};
|
||||
if (releaseStream === null) {
|
||||
release();
|
||||
await upstream.body.cancel();
|
||||
return emptyResponse(429, {'Retry-After': '1'});
|
||||
}
|
||||
return new Response(
|
||||
trackedBody(upstream.body, () => {
|
||||
releaseStream();
|
||||
release();
|
||||
}),
|
||||
{status: upstream.status, headers: mediaHeaders(upstream, viaFileToken)},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/users/@me/attachment-token',
|
||||
enabledOnly(),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
RateLimitMiddleware(RateLimitConfigs.USER_ATTACHMENT_TOKEN),
|
||||
Validator('json', AttachmentAccessTokenRequest),
|
||||
OpenAPI({
|
||||
operationId: 'create_attachment_token',
|
||||
summary: 'Create an attachment token',
|
||||
responseSchema: AttachmentAccessTokenResponse,
|
||||
statusCode: 200,
|
||||
security: ['sessionToken'],
|
||||
tags: ['Messages'],
|
||||
description:
|
||||
'Creates a token for the one attachment in the given link, for places that cannot send a header, such as a video player, a download or a link opened in the browser. The token works for five minutes, only for that file and only while the session that asked for it stays signed in and can still read the message. Only active when the instance serves attachments through the API.',
|
||||
}),
|
||||
async (ctx) => {
|
||||
const session = ctx.get('authSession');
|
||||
if (ctx.get('authTokenType') !== 'session' || !session) throw new AccessDeniedError();
|
||||
const user = ctx.get('user');
|
||||
const location = parseApiAttachmentUrl(ctx.req.valid('json').url);
|
||||
if (location === null) return notFound();
|
||||
if ((await resolveAttachmentForRequester(ctx, location)) === null) return notFound();
|
||||
const expiresAt = Date.now() + ATTACHMENT_MEDIA_TOKEN_TTL_MS;
|
||||
const token = sealAttachmentMediaToken({
|
||||
u: user.id.toString(),
|
||||
s: Buffer.from(session.sessionIdHash).toString('base64url'),
|
||||
f: [...ctx.get('clientFeatures')],
|
||||
c: location.channelId.toString(),
|
||||
a: location.attachmentId.toString(),
|
||||
expiresAt,
|
||||
});
|
||||
return ctx.json({token, expires_at: new Date(expiresAt).toISOString()});
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {createCipheriv, createDecipheriv, createHmac, randomBytes} from 'node:crypto';
|
||||
import {SNOWFLAKE_REGEX} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {Config} from '@app/api/Config';
|
||||
import {isJsonRecord, parseJsonWithGuard} from '@app/api/utils/JsonBoundaryUtils';
|
||||
|
||||
const KEY_DERIVATION_LABEL = 'fluxer.attachment-media.v2';
|
||||
const CIPHER = 'aes-256-gcm';
|
||||
const IV_BYTES = 12;
|
||||
const TAG_BYTES = 16;
|
||||
const MAX_FEATURES = 32;
|
||||
export const ATTACHMENT_MEDIA_TOKEN_TTL_MS = 5 * 60 * 1000;
|
||||
|
||||
export interface AttachmentMediaTokenPayload {
|
||||
u: string;
|
||||
s: string;
|
||||
f: Array<string>;
|
||||
c: string;
|
||||
a: string;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
function deriveKey(): Buffer {
|
||||
return createHmac('sha256', Config.auth.connectionInitiationSecret).update(KEY_DERIVATION_LABEL).digest();
|
||||
}
|
||||
|
||||
function isSnowflake(value: unknown): boolean {
|
||||
return typeof value === 'string' && SNOWFLAKE_REGEX.test(value);
|
||||
}
|
||||
|
||||
function isAttachmentMediaTokenPayload(value: unknown): value is AttachmentMediaTokenPayload {
|
||||
if (!isJsonRecord(value)) return false;
|
||||
const features = value['f'];
|
||||
return (
|
||||
isSnowflake(value['u']) &&
|
||||
typeof value['s'] === 'string' &&
|
||||
value['s'].length > 0 &&
|
||||
Array.isArray(features) &&
|
||||
features.length <= MAX_FEATURES &&
|
||||
features.every((feature) => typeof feature === 'string') &&
|
||||
isSnowflake(value['c']) &&
|
||||
isSnowflake(value['a']) &&
|
||||
typeof value['expiresAt'] === 'number' &&
|
||||
Number.isFinite(value['expiresAt'])
|
||||
);
|
||||
}
|
||||
|
||||
export function sealAttachmentMediaToken(payload: AttachmentMediaTokenPayload): string {
|
||||
const iv = randomBytes(IV_BYTES);
|
||||
const cipher = createCipheriv(CIPHER, deriveKey(), iv);
|
||||
const sealed = Buffer.concat([cipher.update(JSON.stringify(payload), 'utf8'), cipher.final()]);
|
||||
return Buffer.concat([iv, sealed, cipher.getAuthTag()]).toString('base64url');
|
||||
}
|
||||
|
||||
export function openAttachmentMediaToken(token: string): AttachmentMediaTokenPayload | null {
|
||||
const bytes = Buffer.from(token, 'base64url');
|
||||
if (bytes.length <= IV_BYTES + TAG_BYTES || bytes.toString('base64url') !== token) return null;
|
||||
let payload: AttachmentMediaTokenPayload | null;
|
||||
try {
|
||||
const decipher = createDecipheriv(CIPHER, deriveKey(), bytes.subarray(0, IV_BYTES));
|
||||
decipher.setAuthTag(bytes.subarray(bytes.length - TAG_BYTES));
|
||||
const opened = Buffer.concat([
|
||||
decipher.update(bytes.subarray(IV_BYTES, bytes.length - TAG_BYTES)),
|
||||
decipher.final(),
|
||||
]);
|
||||
payload = parseJsonWithGuard(opened.toString('utf8'), isAttachmentMediaTokenPayload);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (!payload || Date.now() > payload.expiresAt) return null;
|
||||
return payload;
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {TRAILING_SLASHES_REGEX} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {Config} from '@app/api/Config';
|
||||
|
||||
const UPSTREAM_HEADERS_TIMEOUT_MS = 10_000;
|
||||
const MAX_DIMENSION = 8192;
|
||||
const IMAGE_FORMATS = new Set([
|
||||
'png',
|
||||
'jpg',
|
||||
'jpeg',
|
||||
'webp',
|
||||
'gif',
|
||||
'apng',
|
||||
'avif',
|
||||
'heic',
|
||||
'heif',
|
||||
'jxl',
|
||||
'svg',
|
||||
'auto',
|
||||
]);
|
||||
const QUALITIES = new Set(['low', 'high', 'lossless', 'auto']);
|
||||
const BOOLEANS = new Set(['true', 'false', '1', '0']);
|
||||
const FORWARDED_REQUEST_HEADERS = ['range', 'if-range', 'if-none-match', 'if-modified-since'] as const;
|
||||
|
||||
type ParamCheck = (value: string) => boolean;
|
||||
|
||||
function isDimension(value: string): boolean {
|
||||
if (!/^[0-9]{1,5}$/u.test(value)) return false;
|
||||
const parsed = Number(value);
|
||||
return parsed >= 1 && parsed <= MAX_DIMENSION;
|
||||
}
|
||||
|
||||
const TRANSFORM_PARAMS: ReadonlyMap<string, ParamCheck> = new Map<string, ParamCheck>([
|
||||
['width', isDimension],
|
||||
['height', isDimension],
|
||||
['format', (value) => IMAGE_FORMATS.has(value.toLowerCase())],
|
||||
['fmt', (value) => IMAGE_FORMATS.has(value.toLowerCase())],
|
||||
['quality', (value) => QUALITIES.has(value)],
|
||||
['animated', (value) => BOOLEANS.has(value.toLowerCase())],
|
||||
['effort', (value) => /^[0-9]$/u.test(value)],
|
||||
['download', (value) => BOOLEANS.has(value.toLowerCase())],
|
||||
]);
|
||||
|
||||
export type TransformQuery = Array<[string, string]>;
|
||||
|
||||
export function readTransformQuery(params: URLSearchParams): TransformQuery | null {
|
||||
const query: TransformQuery = [];
|
||||
for (const [name, check] of TRANSFORM_PARAMS) {
|
||||
const values = params.getAll(name);
|
||||
if (values.length === 0) continue;
|
||||
if (values.length > 1 || !check(values[0]!)) return null;
|
||||
query.push([name, values[0]!]);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
export function encodeStorageKey(storageKey: string): string {
|
||||
return storageKey.split('/').map(encodeURIComponent).join('/');
|
||||
}
|
||||
|
||||
function attachmentUpstreamUrl(storageKey: string, query: TransformQuery): string {
|
||||
const mediaEndpoint = Config.endpoints.media.replace(TRAILING_SLASHES_REGEX, '');
|
||||
const search = new URLSearchParams(query).toString();
|
||||
const publicUrl = `${mediaEndpoint}/${encodeStorageKey(storageKey)}${search ? `?${search}` : ''}`;
|
||||
const signed = signAttachmentUrl(publicUrl);
|
||||
return `http://${Config.mediaProxy.host}:${Config.mediaProxy.port}${signed.slice(mediaEndpoint.length)}`;
|
||||
}
|
||||
|
||||
export function fetchAttachmentFromMediaProxy(params: {
|
||||
storageKey: string;
|
||||
query: TransformQuery;
|
||||
method: 'GET' | 'HEAD';
|
||||
headers: Headers;
|
||||
signal: AbortSignal;
|
||||
}): Promise<Response> {
|
||||
const headers = new Headers({'accept-encoding': 'identity'});
|
||||
for (const name of FORWARDED_REQUEST_HEADERS) {
|
||||
const value = params.headers.get(name);
|
||||
if (value !== null) headers.set(name, value);
|
||||
}
|
||||
const controller = new AbortController();
|
||||
const abort = () => controller.abort(params.signal.reason);
|
||||
if (params.signal.aborted) abort();
|
||||
params.signal.addEventListener('abort', abort, {once: true});
|
||||
const timeout = setTimeout(
|
||||
() => controller.abort(new Error('Media proxy did not answer in time')),
|
||||
UPSTREAM_HEADERS_TIMEOUT_MS,
|
||||
);
|
||||
return fetch(attachmentUpstreamUrl(params.storageKey, params.query), {
|
||||
method: params.method,
|
||||
headers,
|
||||
redirect: 'error',
|
||||
signal: controller.signal,
|
||||
}).finally(() => clearTimeout(timeout));
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {makeSignedAttachmentCdnUrl, signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {
|
||||
type AttachmentID,
|
||||
type ChannelID,
|
||||
createAttachmentID,
|
||||
createChannelID,
|
||||
createMessageID,
|
||||
type MessageID,
|
||||
} from '@app/api/BrandedTypes';
|
||||
import {Config} from '@app/api/Config';
|
||||
import {isApiProxiedAttachments} from '@app/api/instance/AttachmentAccessMode';
|
||||
import {stripApiPrefix} from '@app/api/utils/RequestPathUtils';
|
||||
import {
|
||||
attachmentStorageKeyFromUrl,
|
||||
isSignatureParameterName,
|
||||
percentDecodeStorageKey,
|
||||
} from '@pkgs/media_proxy_utils/src/AttachmentUrlSignature';
|
||||
|
||||
export const SNOWFLAKE_REGEX = /^[0-9]{1,20}$/u;
|
||||
export const TRAILING_SLASHES_REGEX = /\/+$/u;
|
||||
const LEADING_SLASHES_REGEX = /^\/+/u;
|
||||
const VERSION_PREFIX_REGEX = /^\/v[0-9]+(?=\/)/u;
|
||||
const API_ATTACHMENT_PATH_REGEX = /^\/channels\/([0-9]{1,20})\/attachments\/([0-9]{1,20})\/(.+)$/u;
|
||||
const RESERVED_QUERY_NAMES = new Set(['mid', 'mt']);
|
||||
const ENCODED_SEPARATOR_REGEX = /%(?:2e|2f|5c)/iu;
|
||||
|
||||
export interface ApiAttachmentLocation {
|
||||
channelId: ChannelID;
|
||||
attachmentId: AttachmentID;
|
||||
filename: string;
|
||||
messageId: MessageID | null;
|
||||
}
|
||||
|
||||
function apiAttachmentBase(): string {
|
||||
return Config.endpoints.apiClient.replace(TRAILING_SLASHES_REGEX, '');
|
||||
}
|
||||
|
||||
function splitQuery(url: string): {head: string; query: string} {
|
||||
const withoutFragment = url.split('#', 1)[0] ?? '';
|
||||
const queryIndex = withoutFragment.indexOf('?');
|
||||
return queryIndex < 0
|
||||
? {head: withoutFragment, query: ''}
|
||||
: {head: withoutFragment.slice(0, queryIndex), query: withoutFragment.slice(queryIndex + 1)};
|
||||
}
|
||||
|
||||
function rawPathOf(url: string): string {
|
||||
const {head} = splitQuery(url);
|
||||
const authorityStart = head.indexOf('://');
|
||||
if (authorityStart < 0) return '';
|
||||
const pathStart = head.indexOf('/', authorityStart + 3);
|
||||
return pathStart < 0 ? '' : head.slice(pathStart);
|
||||
}
|
||||
|
||||
function queryFieldName(field: string): string {
|
||||
const separator = field.indexOf('=');
|
||||
return separator < 0 ? field : field.slice(0, separator);
|
||||
}
|
||||
|
||||
function preservedQueryFields(query: string): Array<string> {
|
||||
return query
|
||||
.split('&')
|
||||
.filter((field) => field.length > 0 && field !== '=')
|
||||
.filter((field) => {
|
||||
const name = queryFieldName(field);
|
||||
return !isSignatureParameterName(name) && !RESERVED_QUERY_NAMES.has(name);
|
||||
});
|
||||
}
|
||||
|
||||
function isSafeFilename(filename: string): boolean {
|
||||
return filename
|
||||
.split('/')
|
||||
.every((segment) => segment.length > 0 && segment !== '.' && segment !== '..' && !segment.includes('\0'));
|
||||
}
|
||||
|
||||
export function makeApiAttachmentUrl(
|
||||
channelId: ChannelID,
|
||||
attachmentId: AttachmentID | bigint,
|
||||
rawFilename: string,
|
||||
messageId: MessageID | null,
|
||||
preservedQuery: ReadonlyArray<string> = [],
|
||||
): string {
|
||||
const fields = [...(messageId === null ? [] : [`mid=${messageId}`]), ...preservedQuery];
|
||||
const url = `${apiAttachmentBase()}/channels/${channelId}/attachments/${attachmentId}/${rawFilename}`;
|
||||
return fields.length === 0 ? url : `${url}?${fields.join('&')}`;
|
||||
}
|
||||
|
||||
export function parseApiAttachmentRoute(routePath: string, mid: string | null): ApiAttachmentLocation | null {
|
||||
const match = API_ATTACHMENT_PATH_REGEX.exec(routePath.replace(VERSION_PREFIX_REGEX, ''));
|
||||
if (!match || ENCODED_SEPARATOR_REGEX.test(match[3]!)) return null;
|
||||
const filename = percentDecodeStorageKey(match[3]!);
|
||||
if (filename === null || !isSafeFilename(filename)) return null;
|
||||
if (mid !== null && !SNOWFLAKE_REGEX.test(mid)) return null;
|
||||
return {
|
||||
channelId: createChannelID(BigInt(match[1]!)),
|
||||
attachmentId: createAttachmentID(BigInt(match[2]!)),
|
||||
filename,
|
||||
messageId: mid === null ? null : createMessageID(BigInt(mid)),
|
||||
};
|
||||
}
|
||||
|
||||
export function parseApiAttachmentUrl(url: string): ApiAttachmentLocation | null {
|
||||
let target: URL;
|
||||
let base: URL;
|
||||
try {
|
||||
target = new URL(url);
|
||||
base = new URL(apiAttachmentBase());
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (target.origin !== base.origin) return null;
|
||||
const basePath = rawPathOf(apiAttachmentBase()).replace(TRAILING_SLASHES_REGEX, '');
|
||||
const rawPath = rawPathOf(url);
|
||||
if (!rawPath.startsWith(`${basePath}/`)) return null;
|
||||
return parseApiAttachmentRoute(rawPath.slice(basePath.length), target.searchParams.get('mid'));
|
||||
}
|
||||
|
||||
export function isAttachmentMediaRequest(method: string, path: string): boolean {
|
||||
return (
|
||||
isApiProxiedAttachments() &&
|
||||
(method === 'GET' || method === 'HEAD') &&
|
||||
API_ATTACHMENT_PATH_REGEX.test(stripApiPrefix(path))
|
||||
);
|
||||
}
|
||||
|
||||
export function parseMediaAttachmentUrl(url: string): ApiAttachmentLocation | null {
|
||||
const storageKey = attachmentStorageKeyFromUrl(url, Config.endpoints.media.replace(TRAILING_SLASHES_REGEX, ''));
|
||||
if (storageKey === null) return null;
|
||||
const [prefix, channelSegment, attachmentSegment, ...filenameSegments] = storageKey.split('/');
|
||||
const filename = filenameSegments.join('/');
|
||||
if (
|
||||
prefix !== 'attachments' ||
|
||||
!SNOWFLAKE_REGEX.test(channelSegment ?? '') ||
|
||||
!SNOWFLAKE_REGEX.test(attachmentSegment ?? '') ||
|
||||
!isSafeFilename(filename)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
channelId: createChannelID(BigInt(channelSegment!)),
|
||||
attachmentId: createAttachmentID(BigInt(attachmentSegment!)),
|
||||
filename,
|
||||
messageId: null,
|
||||
};
|
||||
}
|
||||
|
||||
export function ownAttachmentLocation(url: string): ApiAttachmentLocation | null {
|
||||
return parseApiAttachmentUrl(url) ?? parseMediaAttachmentUrl(url);
|
||||
}
|
||||
|
||||
export function isGuardedAttachmentUrl(url: string): boolean {
|
||||
return isApiProxiedAttachments() && ownAttachmentLocation(url) !== null;
|
||||
}
|
||||
|
||||
export interface AttachmentUrlOwner {
|
||||
channelId: ChannelID;
|
||||
messageId: MessageID;
|
||||
storageChannelId: ChannelID;
|
||||
}
|
||||
|
||||
export function apiFormOfMediaUrl(url: string, owner: AttachmentUrlOwner | null = null): string | null {
|
||||
const mediaEndpoint = Config.endpoints.media.replace(TRAILING_SLASHES_REGEX, '');
|
||||
const storageKey = attachmentStorageKeyFromUrl(url, mediaEndpoint);
|
||||
if (storageKey === null) return null;
|
||||
const [, channelSegment, attachmentSegment] = storageKey.split('/');
|
||||
if (!SNOWFLAKE_REGEX.test(channelSegment ?? '') || !SNOWFLAKE_REGEX.test(attachmentSegment ?? '')) return null;
|
||||
const {query} = splitQuery(url);
|
||||
const endpointPath = rawPathOf(mediaEndpoint).replace(TRAILING_SLASHES_REGEX, '');
|
||||
const rawKeyPath = rawPathOf(url).slice(endpointPath.length).replace(LEADING_SLASHES_REGEX, '');
|
||||
const rawFilename = rawKeyPath.split('/').slice(3).join('/');
|
||||
if (rawFilename.length === 0) return null;
|
||||
const keyChannelId = createChannelID(BigInt(channelSegment!));
|
||||
const ownedHere = owner !== null && owner.storageChannelId === keyChannelId;
|
||||
return makeApiAttachmentUrl(
|
||||
ownedHere ? owner.channelId : keyChannelId,
|
||||
BigInt(attachmentSegment!),
|
||||
rawFilename,
|
||||
ownedHere ? owner.messageId : null,
|
||||
preservedQueryFields(query),
|
||||
);
|
||||
}
|
||||
|
||||
export function publicAttachmentUrl(url: string): string {
|
||||
if (!isApiProxiedAttachments()) return signAttachmentUrl(url);
|
||||
return apiFormOfMediaUrl(url) ?? url;
|
||||
}
|
||||
|
||||
export function makePublicAttachmentUrl(
|
||||
channelId: ChannelID,
|
||||
attachmentId: AttachmentID | bigint,
|
||||
filename: string,
|
||||
): string {
|
||||
if (!isApiProxiedAttachments()) return makeSignedAttachmentCdnUrl(channelId, attachmentId, filename);
|
||||
return makeApiAttachmentUrl(channelId, attachmentId, filename, null);
|
||||
}
|
||||
@@ -0,0 +1,548 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {createHmac, randomBytes} from 'node:crypto';
|
||||
import {clearAttachmentMediaDecisionsForTesting} from '@app/api/attachment/AttachmentMediaAccess';
|
||||
import {
|
||||
type AttachmentMediaTokenPayload,
|
||||
openAttachmentMediaToken,
|
||||
sealAttachmentMediaToken,
|
||||
} from '@app/api/attachment/AttachmentMediaToken';
|
||||
import {createTestAccount, setUserACLs, type TestAccount} from '@app/api/auth/tests/AuthTestUtils';
|
||||
import {Config} from '@app/api/Config';
|
||||
import {
|
||||
createChannel,
|
||||
createGuild,
|
||||
loadFixture,
|
||||
sendMessageWithAttachments,
|
||||
} from '@app/api/channel/tests/AttachmentTestUtils';
|
||||
import {createDmChannel, createFriendship} from '@app/api/channel/tests/ChannelTestUtils';
|
||||
import {acceptInvite, createChannelInvite, leaveGuild} from '@app/api/guild/tests/GuildTestUtils';
|
||||
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
|
||||
import {server} from '@app/api/test/msw/server';
|
||||
import {TestMediaService} from '@app/api/test/TestMediaService';
|
||||
import {createBuilder, createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';
|
||||
import {createWebhook} from '@app/api/webhook/tests/WebhookTestUtils';
|
||||
import {AdminACLs} from '@fluxer/constants/src/AdminACLs';
|
||||
import {createSnowflakeGenerator} from '@fluxer/snowflake/src/Snowflake';
|
||||
import {canonicalInput} from '@pkgs/media_proxy_utils/src/AttachmentUrlSignature';
|
||||
import {HttpResponse, http} from 'msw';
|
||||
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest';
|
||||
|
||||
const FILE_BYTES = randomBytes(64);
|
||||
const IGNORED_HEADERS = new Set(['date', 'x-request-id', 'x-fluxer-version']);
|
||||
|
||||
interface UpstreamRequest {
|
||||
method: string;
|
||||
url: URL;
|
||||
headers: Headers;
|
||||
}
|
||||
|
||||
interface SentMessage {
|
||||
channelId: string;
|
||||
messageId: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
function upstreamBase(): string {
|
||||
return `http://${Config.mediaProxy.host}:${Config.mediaProxy.port}`;
|
||||
}
|
||||
|
||||
function stubMediaProxy(requests: Array<UpstreamRequest>) {
|
||||
return http.all(`${upstreamBase()}/attachments/*`, ({request}) => {
|
||||
requests.push({method: request.method, url: new URL(request.url), headers: request.headers});
|
||||
const range = request.headers.get('range');
|
||||
const headers = {
|
||||
'Content-Type': 'image/png',
|
||||
'Cache-Control': 'public, max-age=31536000',
|
||||
'CDN-Cache-Control': 'public, max-age=31536000',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Accept-Ranges': 'bytes',
|
||||
'Set-Cookie': 'tracking=1',
|
||||
};
|
||||
if (range === 'bytes=0-9') {
|
||||
return new HttpResponse(FILE_BYTES.subarray(0, 10), {
|
||||
status: 206,
|
||||
headers: {...headers, 'Content-Range': `bytes 0-9/${FILE_BYTES.length}`, 'Content-Length': '10'},
|
||||
});
|
||||
}
|
||||
if (range !== null) {
|
||||
return new HttpResponse(null, {status: 416, headers: {'Content-Range': `bytes */${FILE_BYTES.length}`}});
|
||||
}
|
||||
const disposition = request.url.includes('download=1') ? {'Content-Disposition': 'attachment'} : {};
|
||||
return new HttpResponse(request.method === 'HEAD' ? null : FILE_BYTES, {
|
||||
status: 200,
|
||||
headers: {...headers, ...disposition, 'Content-Length': String(FILE_BYTES.length)},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function comparableHeaders(response: Response): Record<string, string> {
|
||||
return Object.fromEntries([...response.headers.entries()].filter(([name]) => !IGNORED_HEADERS.has(name)));
|
||||
}
|
||||
|
||||
async function snapshot(response: Response) {
|
||||
return {status: response.status, headers: comparableHeaders(response), body: await response.text()};
|
||||
}
|
||||
|
||||
function routePath(url: string): string {
|
||||
const parsed = new URL(url);
|
||||
return `${parsed.pathname.replace(/^\/api/u, '')}${parsed.search}`;
|
||||
}
|
||||
|
||||
describe('attachments served through the API', () => {
|
||||
let harness: ApiTestHarness;
|
||||
let upstream: Array<UpstreamRequest>;
|
||||
let admin: TestAccount;
|
||||
let owner: TestAccount;
|
||||
let member: TestAccount;
|
||||
let outsider: TestAccount;
|
||||
let channelId: string;
|
||||
let guildId: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
harness = await createApiTestHarness();
|
||||
upstream = [];
|
||||
server.use(stubMediaProxy(upstream));
|
||||
admin = await setUserACLs(harness, await createTestAccount(harness), [
|
||||
AdminACLs.AUTHENTICATE,
|
||||
AdminACLs.INSTANCE_CONFIG_VIEW,
|
||||
AdminACLs.INSTANCE_CONFIG_UPDATE,
|
||||
]);
|
||||
owner = await createTestAccount(harness);
|
||||
member = await createTestAccount(harness);
|
||||
outsider = await createTestAccount(harness);
|
||||
const guild = await createGuild(harness, owner.token, 'Proxy Guild');
|
||||
guildId = guild.id;
|
||||
channelId = (await createChannel(harness, owner.token, guild.id, 'media')).id;
|
||||
const invite = await createChannelInvite(harness, owner.token, channelId);
|
||||
await acceptInvite(harness, member.token, invite.code);
|
||||
const otherGuild = await createGuild(harness, outsider.token, 'Other Guild');
|
||||
await createChannel(harness, outsider.token, otherGuild.id, 'elsewhere');
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
clearAttachmentMediaDecisionsForTesting();
|
||||
await harness?.shutdown();
|
||||
});
|
||||
|
||||
async function setProxied(enabled: boolean): Promise<void> {
|
||||
await createBuilder(harness, admin.token)
|
||||
.patch('/admin/instance/config')
|
||||
.body({media: {proxy_attachments_through_api: enabled}})
|
||||
.execute();
|
||||
}
|
||||
|
||||
async function sendImage(token: string, targetChannelId: string): Promise<SentMessage> {
|
||||
const {response, json} = await sendMessageWithAttachments(
|
||||
harness,
|
||||
token,
|
||||
targetChannelId,
|
||||
{content: 'image', attachments: [{id: 0, filename: 'yeah.png'}]},
|
||||
[{index: 0, filename: 'yeah.png', data: loadFixture('yeah.png')}],
|
||||
);
|
||||
expect(response.status).toBe(200);
|
||||
return {channelId: targetChannelId, messageId: json.id, url: json.attachments![0]!.url!};
|
||||
}
|
||||
|
||||
async function get(path: string, token?: string, headers: Record<string, string> = {}): Promise<Response> {
|
||||
return await harness.app.request(path, {
|
||||
headers: {'x-forwarded-for': '127.0.0.1', ...(token ? {Authorization: token} : {}), ...headers},
|
||||
});
|
||||
}
|
||||
|
||||
async function mintToken(token: string, body: Record<string, unknown> = {}): Promise<Response> {
|
||||
return harness.app.request('/users/@me/attachment-token', {
|
||||
method: 'POST',
|
||||
headers: {'x-forwarded-for': '127.0.0.1', Authorization: token, 'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
test('with the setting off links, the route and the token endpoint stay as before', async () => {
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
expect(sent.url.startsWith(`${Config.endpoints.media}/attachments/${channelId}/`)).toBe(true);
|
||||
expect(new URL(sent.url).searchParams.get('hm')).not.toBeNull();
|
||||
const apiPath = `/channels/${channelId}/attachments/${new URL(sent.url).pathname.split('/').slice(-2).join('/')}`;
|
||||
const unknownPath = apiPath.replace('/attachments/', '/attachments-unknown/');
|
||||
for (const token of [member.token, undefined]) {
|
||||
expect(await snapshot(await get(`${apiPath}?mid=${sent.messageId}`, token))).toEqual(
|
||||
await snapshot(await get(`${unknownPath}?mid=${sent.messageId}`, token)),
|
||||
);
|
||||
expect(await snapshot(await get(`${apiPath}?mt=abc.def`, token))).toEqual(
|
||||
await snapshot(await get(`${unknownPath}?mt=abc.def`, token)),
|
||||
);
|
||||
}
|
||||
const minted = await snapshot(await mintToken(member.token));
|
||||
const unknownPost = await snapshot(
|
||||
await harness.app.request('/users/@me/attachment-token-unknown', {
|
||||
method: 'POST',
|
||||
headers: {'x-forwarded-for': '127.0.0.1', Authorization: member.token, 'Content-Type': 'application/json'},
|
||||
body: '{}',
|
||||
}),
|
||||
);
|
||||
expect(minted).toEqual(unknownPost);
|
||||
const refreshed = await createBuilder<{refreshed_urls: Array<{refreshed: string}>}>(harness, member.token)
|
||||
.post('/attachments/refresh-urls')
|
||||
.body({attachment_urls: [sent.url]})
|
||||
.execute();
|
||||
expect(refreshed.refreshed_urls[0]!.refreshed.startsWith(`${Config.endpoints.media}/attachments/`)).toBe(true);
|
||||
const discovery = await createBuilder<{features: Record<string, unknown>}>(harness, '')
|
||||
.get('/.well-known/fluxer')
|
||||
.execute();
|
||||
expect('proxy_attachments_through_api' in discovery.features).toBe(false);
|
||||
expect(upstream).toEqual([]);
|
||||
});
|
||||
|
||||
test('with the setting on a member gets the file and the media proxy gets a signed request', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
expect(sent.url.startsWith(`${Config.endpoints.apiClient}/channels/${channelId}/attachments/`)).toBe(true);
|
||||
expect(sent.url.endsWith(`?mid=${sent.messageId}`)).toBe(true);
|
||||
const response = await get(`${routePath(sent.url)}&ex=1&is=2&hm=3&uc=dp&mt=x&other=1&width=64`, member.token, {
|
||||
Cookie: 'session=1',
|
||||
'X-Extra': '1',
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
expect(Buffer.from(await response.arrayBuffer())).toEqual(FILE_BYTES);
|
||||
expect(response.headers.get('cache-control')).toBe('private, max-age=3600');
|
||||
expect(response.headers.get('cdn-cache-control')).toBe('no-store');
|
||||
expect(response.headers.get('content-security-policy')).toBe("default-src 'none'; sandbox");
|
||||
expect(response.headers.get('x-content-type-options')).toBe('nosniff');
|
||||
expect(response.headers.get('referrer-policy')).toBe('no-referrer');
|
||||
expect(response.headers.get('set-cookie')).toBeNull();
|
||||
expect(response.headers.get('access-control-allow-credentials')).toBeNull();
|
||||
expect(upstream).toHaveLength(1);
|
||||
const request = upstream[0]!;
|
||||
const key = `attachments/${channelId}/${new URL(sent.url).pathname.split('/').slice(-2).join('/')}`;
|
||||
expect(request.url.pathname).toBe(`/${key}`);
|
||||
expect([...request.url.searchParams.keys()].sort()).toEqual(['ex', 'hm', 'is', 'width']);
|
||||
const secret = Buffer.from(Config.mediaProxy.attachmentUrls.secretsBase64[0]!, 'base64');
|
||||
const expected = createHmac('sha256', secret)
|
||||
.update(canonicalInput(key, request.url.searchParams.get('ex')!, request.url.searchParams.get('is')!, ''))
|
||||
.digest('hex');
|
||||
expect(request.url.searchParams.get('hm')).toBe(expected);
|
||||
expect(request.headers.get('cookie')).toBeNull();
|
||||
expect(request.headers.get('authorization')).toBeNull();
|
||||
expect(request.headers.get('x-extra')).toBeNull();
|
||||
});
|
||||
|
||||
test('with the setting on everyone who cannot read the message gets the same 404', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const path = routePath(sent.url);
|
||||
const missing = await get(path, outsider.token);
|
||||
expect(missing.status).toBe(404);
|
||||
const notFound = await snapshot(missing);
|
||||
const filename = new URL(sent.url).pathname.split('/').pop()!;
|
||||
for (const attempt of [
|
||||
path.replace(filename, `other-${filename}`),
|
||||
path.replace(`mid=${sent.messageId}`, `mid=${BigInt(sent.messageId) + 1n}`),
|
||||
path.replace(filename, `..%2F${filename}`),
|
||||
]) {
|
||||
expect(await snapshot(await get(attempt, member.token))).toEqual(notFound);
|
||||
}
|
||||
await createFriendship(harness, owner, member);
|
||||
const dm = await createDmChannel(harness, owner.token, member.userId);
|
||||
const dmSent = await sendImage(owner.token, dm.id);
|
||||
expect((await get(routePath(dmSent.url), member.token)).status).toBe(200);
|
||||
expect(await snapshot(await get(routePath(dmSent.url), outsider.token))).toEqual(notFound);
|
||||
await createBuilder(harness, owner.token)
|
||||
.delete(`/channels/${channelId}/messages/${sent.messageId}`)
|
||||
.expect(204)
|
||||
.execute();
|
||||
clearAttachmentMediaDecisionsForTesting();
|
||||
expect(await snapshot(await get(path, member.token))).toEqual(notFound);
|
||||
expect(upstream.every((request) => request.url.pathname.includes(dm.id))).toBe(true);
|
||||
});
|
||||
|
||||
test('with the setting on ranges, heads and transforms pass through', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const path = routePath(sent.url);
|
||||
const partial = await get(path, member.token, {Range: 'bytes=0-9'});
|
||||
expect(partial.status).toBe(206);
|
||||
expect(partial.headers.get('content-range')).toBe(`bytes 0-9/${FILE_BYTES.length}`);
|
||||
expect(Buffer.from(await partial.arrayBuffer())).toEqual(FILE_BYTES.subarray(0, 10));
|
||||
expect(upstream.at(-1)!.headers.get('range')).toBe('bytes=0-9');
|
||||
expect((await get(path, member.token, {Range: 'bytes=999-1000'})).status).toBe(416);
|
||||
const head = await harness.app.request(path, {
|
||||
method: 'HEAD',
|
||||
headers: {'x-forwarded-for': '127.0.0.1', Authorization: member.token},
|
||||
});
|
||||
expect(head.status).toBe(200);
|
||||
expect(await head.text()).toBe('');
|
||||
expect(upstream.at(-1)!.method).toBe('HEAD');
|
||||
for (let attempt = 0; attempt < 20; attempt++) {
|
||||
const repeated = await harness.app.request(path, {
|
||||
method: attempt % 2 === 0 ? 'HEAD' : 'GET',
|
||||
headers: {'x-forwarded-for': '127.0.0.1', Authorization: member.token},
|
||||
});
|
||||
await repeated.arrayBuffer();
|
||||
expect(repeated.status).toBe(200);
|
||||
}
|
||||
const download = await get(`${path}&download=1&format=webp&quality=low&height=32`, member.token);
|
||||
expect(download.headers.get('content-disposition')).toBe('attachment');
|
||||
const forwarded = upstream.at(-1)!.url.searchParams;
|
||||
expect([
|
||||
forwarded.get('download'),
|
||||
forwarded.get('format'),
|
||||
forwarded.get('quality'),
|
||||
forwarded.get('height'),
|
||||
]).toEqual(['1', 'webp', 'low', '32']);
|
||||
const before = upstream.length;
|
||||
for (const bad of ['width=0', 'width=abc', 'format=exe', 'quality=max', 'width=1&width=2']) {
|
||||
expect((await get(`${path}&${bad}`, member.token)).status).toBe(400);
|
||||
}
|
||||
expect(upstream.length).toBe(before);
|
||||
});
|
||||
|
||||
test('with the setting on a grid of images loading at once is not refused', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const path = routePath(sent.url);
|
||||
const responses = await Promise.all(Array.from({length: 24}, () => get(path, member.token)));
|
||||
expect(responses.map((response) => response.status)).toEqual(responses.map(() => 200));
|
||||
const bodies = await Promise.all(responses.map(async (response) => Buffer.from(await response.arrayBuffer())));
|
||||
expect(bodies.every((body) => body.equals(FILE_BYTES))).toBe(true);
|
||||
});
|
||||
|
||||
async function mintFor(token: string, url: string): Promise<string> {
|
||||
const minted = await mintToken(token, {url});
|
||||
expect(minted.status).toBe(200);
|
||||
return ((await minted.json()) as {token: string}).token;
|
||||
}
|
||||
|
||||
function withToken(path: string, token: string): string {
|
||||
return `${path}&mt=${encodeURIComponent(token)}`;
|
||||
}
|
||||
|
||||
test('with the setting on a file token opens that one file for five minutes', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const path = routePath(sent.url);
|
||||
const before = Date.now();
|
||||
const minted = await mintToken(member.token, {url: sent.url});
|
||||
expect(minted.status).toBe(200);
|
||||
const body = (await minted.json()) as {token: string; expires_at: string};
|
||||
const lifetimeMs = Date.parse(body.expires_at) - before;
|
||||
expect(lifetimeMs).toBeGreaterThan(4 * 60 * 1000);
|
||||
expect(lifetimeMs).toBeLessThanOrEqual(5 * 60 * 1000 + 1000);
|
||||
expect(openAttachmentMediaToken(body.token)).toMatchObject({
|
||||
c: channelId,
|
||||
a: new URL(sent.url).pathname.split('/').at(-2),
|
||||
});
|
||||
const readable = Buffer.from(body.token, 'base64url').toString('latin1');
|
||||
for (const secret of [member.userId, channelId, sent.messageId]) {
|
||||
expect(readable.includes(secret)).toBe(false);
|
||||
}
|
||||
const opened = await get(withToken(path, body.token));
|
||||
expect(opened.status).toBe(200);
|
||||
expect(opened.headers.get('cache-control')).toBe('private, no-store');
|
||||
expect(Buffer.from(await opened.arrayBuffer())).toEqual(FILE_BYTES);
|
||||
expect((await get(withToken(path, body.token), undefined, {Range: 'bytes=0-9'})).status).toBe(206);
|
||||
const tampered = `${body.token.slice(0, -2)}${body.token.endsWith('AA') ? 'BB' : 'AA'}`;
|
||||
const rejected = await get(withToken(path, tampered));
|
||||
expect(rejected.status).toBe(401);
|
||||
expect(rejected.headers.get('cache-control')).toBe('no-store');
|
||||
expect((await get(`/users/@me?mt=${encodeURIComponent(body.token)}`)).status).toBe(401);
|
||||
const reissue = await harness.app.request('/users/@me/attachment-token', {
|
||||
method: 'POST',
|
||||
headers: {'x-forwarded-for': '127.0.0.1', 'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({url: `${sent.url}&mt=${encodeURIComponent(body.token)}`}),
|
||||
});
|
||||
expect(reissue.status).toBe(401);
|
||||
});
|
||||
|
||||
test('with the setting on a file token does not open another file', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const other = await sendImage(owner.token, channelId);
|
||||
const token = await mintFor(member.token, sent.url);
|
||||
expect((await get(withToken(routePath(other.url), token))).status).toBe(401);
|
||||
const otherPath = routePath(other.url).replace(`mid=${other.messageId}`, `mid=${sent.messageId}`);
|
||||
expect((await get(withToken(otherPath, token))).status).toBe(401);
|
||||
expect((await get(withToken(routePath(sent.url), token))).status).toBe(200);
|
||||
});
|
||||
|
||||
test('with the setting on a file token is only issued to someone who can read the file', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
expect((await mintToken(outsider.token, {url: sent.url})).status).toBe(404);
|
||||
const ownGuild = await createGuild(harness, outsider.token, 'Own Guild');
|
||||
const ownChannelId = (await createChannel(harness, outsider.token, ownGuild.id, 'own')).id;
|
||||
const own = await sendImage(outsider.token, ownChannelId);
|
||||
const outsiderToken = await mintFor(outsider.token, own.url);
|
||||
expect((await get(withToken(routePath(own.url), outsiderToken))).status).toBe(200);
|
||||
expect((await get(withToken(routePath(sent.url), outsiderToken))).status).toBe(401);
|
||||
const swapped = routePath(own.url).replace(`/channels/${ownChannelId}/`, `/channels/${channelId}/`);
|
||||
expect((await get(withToken(swapped, outsiderToken))).status).toBe(401);
|
||||
const memberToken = await mintFor(member.token, sent.url);
|
||||
const payload = openAttachmentMediaToken(memberToken)!;
|
||||
expect(payload.u).toBe(member.userId);
|
||||
const forged = sealAttachmentMediaToken({...payload, u: outsider.userId});
|
||||
expect((await get(withToken(routePath(sent.url), forged))).status).toBe(401);
|
||||
});
|
||||
|
||||
test('with the setting on an expired file token is refused', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const payload = openAttachmentMediaToken(await mintFor(member.token, sent.url))!;
|
||||
const expired = sealAttachmentMediaToken({...payload, expiresAt: Date.now() - 1});
|
||||
expect((await get(withToken(routePath(sent.url), expired))).status).toBe(401);
|
||||
});
|
||||
|
||||
test('with the setting on a file token stops working once access is gone', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const path = routePath(sent.url);
|
||||
const token = await mintFor(member.token, sent.url);
|
||||
expect((await get(withToken(path, token))).status).toBe(200);
|
||||
await leaveGuild(harness, member.token, guildId);
|
||||
clearAttachmentMediaDecisionsForTesting();
|
||||
expect((await get(withToken(path, token))).status).toBe(404);
|
||||
const ownerToken = await mintFor(owner.token, sent.url);
|
||||
expect((await get(withToken(path, ownerToken))).status).toBe(200);
|
||||
await createBuilder(harness, owner.token).post('/auth/logout').expect(204).execute();
|
||||
clearAttachmentMediaDecisionsForTesting();
|
||||
expect((await get(withToken(path, ownerToken))).status).toBe(401);
|
||||
await setProxied(false);
|
||||
expect((await get(withToken(path, token))).status).toBe(404);
|
||||
});
|
||||
|
||||
test('with the setting on no token covers the whole account', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
expect((await mintToken(member.token)).status).toBe(400);
|
||||
expect((await mintToken(member.token, {url: `${Config.endpoints.apiClient}/users/@me`})).status).toBe(404);
|
||||
const payload = openAttachmentMediaToken(await mintFor(member.token, sent.url))!;
|
||||
const {c: _channel, a: _attachment, ...accountWide} = payload;
|
||||
const accountToken = sealAttachmentMediaToken(accountWide as unknown as AttachmentMediaTokenPayload);
|
||||
expect(openAttachmentMediaToken(accountToken)).toBeNull();
|
||||
expect((await get(withToken(routePath(sent.url), accountToken))).status).toBe(401);
|
||||
const viaHeader = await get(routePath(sent.url), member.token);
|
||||
expect(viaHeader.status).toBe(200);
|
||||
expect(Buffer.from(await viaHeader.arrayBuffer())).toEqual(FILE_BYTES);
|
||||
});
|
||||
|
||||
test('with the setting on refreshed links point at the API and never at the media proxy', async () => {
|
||||
await setProxied(true);
|
||||
const attachmentId = createSnowflakeGenerator().generate();
|
||||
const mediaUrl = `${Config.endpoints.media}/attachments/${channelId}/${attachmentId}/cat.png?ex=1&is=2&hm=3`;
|
||||
const refreshed = await createBuilder<{refreshed_urls: Array<{refreshed: string}>}>(harness, member.token)
|
||||
.post('/attachments/refresh-urls')
|
||||
.body({attachment_urls: [mediaUrl]})
|
||||
.execute();
|
||||
expect(refreshed.refreshed_urls[0]!.refreshed).toBe(
|
||||
`${Config.endpoints.apiClient}/channels/${channelId}/attachments/${attachmentId}/cat.png`,
|
||||
);
|
||||
const discovery = await createBuilder<{features: Record<string, unknown>}>(harness, '')
|
||||
.get('/.well-known/fluxer')
|
||||
.execute();
|
||||
expect(discovery.features.proxy_attachments_through_api).toBe(true);
|
||||
});
|
||||
|
||||
function watchPrivateFetches(sent: SentMessage): () => Array<string> {
|
||||
const spy = vi.spyOn(TestMediaService.prototype, 'getMetadata');
|
||||
const key = `/attachments/${sent.channelId}/`;
|
||||
return () => {
|
||||
const urls = spy.mock.calls.flatMap(([request]) =>
|
||||
request.type === 'external' && request.url.includes(key) ? [request.url] : [],
|
||||
);
|
||||
spy.mockRestore();
|
||||
return urls;
|
||||
};
|
||||
}
|
||||
|
||||
function mediaFormOf(sent: SentMessage): string {
|
||||
const [attachmentId, filename] = new URL(sent.url).pathname.split('/').slice(-2);
|
||||
return `${Config.endpoints.media}/attachments/${sent.channelId}/${attachmentId}/${filename}`;
|
||||
}
|
||||
|
||||
test('with the setting on a copied link cannot be turned into a meme, a GIF or an avatar by an outsider', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const before = upstream.length;
|
||||
const privateFetches = watchPrivateFetches(sent);
|
||||
for (const url of [mediaFormOf(sent), `${mediaFormOf(sent)}?ex=1&is=2&hm=3`, sent.url]) {
|
||||
const meme = await harness.app.request('/users/@me/memes', {
|
||||
method: 'POST',
|
||||
headers: {'x-forwarded-for': '127.0.0.1', Authorization: outsider.token, 'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({url, name: 'copy'}),
|
||||
});
|
||||
expect(meme.status).toBe(400);
|
||||
const resolved = await createBuilder<{
|
||||
entries: Array<{content_type: string; width: number; placeholder: unknown}>;
|
||||
}>(harness, outsider.token)
|
||||
.post('/users/@me/favorite-gifs/resolve')
|
||||
.body({urls: [url]})
|
||||
.execute();
|
||||
expect(resolved.entries[0]).toMatchObject({content_type: '', width: 0, placeholder: null});
|
||||
}
|
||||
const memes = await createBuilder<Array<unknown>>(harness, outsider.token).get('/users/@me/memes').execute();
|
||||
expect(memes).toEqual([]);
|
||||
expect(upstream.length).toBe(before);
|
||||
expect(privateFetches()).toEqual([]);
|
||||
});
|
||||
|
||||
test('with the setting on a linked file never borrows the access of the message that links it', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const edited = await createBuilder<{attachments: Array<unknown> | null}>(harness, owner.token)
|
||||
.patch(`/channels/${channelId}/messages/${sent.messageId}`)
|
||||
.body({content: 'edited', attachments: []})
|
||||
.execute();
|
||||
expect(edited.attachments ?? []).toEqual([]);
|
||||
clearAttachmentMediaDecisionsForTesting();
|
||||
expect((await get(routePath(sent.url), owner.token)).status).toBe(404);
|
||||
const linking = await createBuilder<{embeds: Array<{image?: {url: string; proxy_url: string}}>}>(
|
||||
harness,
|
||||
owner.token,
|
||||
)
|
||||
.post(`/channels/${channelId}/messages`)
|
||||
.body({embeds: [{title: 'linked', image: {url: mediaFormOf(sent)}}]})
|
||||
.execute();
|
||||
const image = linking.embeds[0]!.image!;
|
||||
for (const url of [image.url, image.proxy_url]) {
|
||||
expect(new URL(url).searchParams.has('mid')).toBe(false);
|
||||
expect(url.startsWith(`${Config.endpoints.apiClient}/channels/${channelId}/attachments/`)).toBe(true);
|
||||
expect((await get(routePath(url), owner.token)).status).toBe(404);
|
||||
expect((await get(routePath(url), member.token)).status).toBe(404);
|
||||
}
|
||||
});
|
||||
|
||||
test('with the setting on a linked file cannot be saved or previewed by someone who cannot read it', async () => {
|
||||
await setProxied(true);
|
||||
const sent = await sendImage(owner.token, channelId);
|
||||
const ownGuild = await createGuild(harness, outsider.token, 'Linking Guild');
|
||||
const ownChannelId = (await createChannel(harness, outsider.token, ownGuild.id, 'links')).id;
|
||||
const privateFetches = watchPrivateFetches(sent);
|
||||
const linking = await createBuilder<{id: string}>(harness, outsider.token)
|
||||
.post(`/channels/${ownChannelId}/messages`)
|
||||
.body({embeds: [{title: 'linked', image: {url: mediaFormOf(sent)}}]})
|
||||
.execute();
|
||||
const meme = await harness.app.request(`/channels/${ownChannelId}/messages/${linking.id}/memes`, {
|
||||
method: 'POST',
|
||||
headers: {'x-forwarded-for': '127.0.0.1', Authorization: outsider.token, 'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({embed_index: 0, name: 'copy'}),
|
||||
});
|
||||
expect(meme.status).not.toBe(201);
|
||||
const memes = await createBuilder<Array<unknown>>(harness, outsider.token).get('/users/@me/memes').execute();
|
||||
expect(memes).toEqual([]);
|
||||
const webhook = await createWebhook(harness, ownChannelId, outsider.token, 'linker');
|
||||
for (const url of [mediaFormOf(sent), sent.url]) {
|
||||
const posted = await createBuilderWithoutAuth<{author: {avatar: string | null}}>(harness)
|
||||
.post(`/webhooks/${webhook.id}/${webhook.token}?wait=true`)
|
||||
.body({content: 'hello', avatar_url: url})
|
||||
.execute();
|
||||
expect(posted.author.avatar).toBeNull();
|
||||
}
|
||||
for (const url of [mediaFormOf(sent), sent.url]) {
|
||||
const unfurled = await createBuilder<Array<unknown>>(harness, outsider.token)
|
||||
.post('/unfurl')
|
||||
.body({url})
|
||||
.execute();
|
||||
expect(unfurled).toEqual([]);
|
||||
}
|
||||
expect(privateFetches()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,172 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {
|
||||
apiFormOfMediaUrl,
|
||||
isAttachmentMediaRequest,
|
||||
isGuardedAttachmentUrl,
|
||||
makeApiAttachmentUrl,
|
||||
makePublicAttachmentUrl,
|
||||
parseApiAttachmentUrl,
|
||||
parseMediaAttachmentUrl,
|
||||
publicAttachmentUrl,
|
||||
} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {makeSignedAttachmentCdnUrl, signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {createChannelID, createMessageID} from '@app/api/BrandedTypes';
|
||||
import {Config} from '@app/api/Config';
|
||||
import {setCachedProxyAttachmentsThroughApi} from '@app/api/instance/AttachmentAccessMode';
|
||||
import {extractURLs} from '@app/api/utils/UnfurlerUtils';
|
||||
import {createSnowflakeGenerator} from '@fluxer/snowflake/src/Snowflake';
|
||||
import {afterEach, describe, expect, test} from 'vitest';
|
||||
|
||||
const snowflakes = createSnowflakeGenerator();
|
||||
const CHANNEL_ID = snowflakes.generate();
|
||||
const ATTACHMENT_ID = snowflakes.generate();
|
||||
const MESSAGE_ID = snowflakes.generate();
|
||||
|
||||
function media(path: string): string {
|
||||
return `${Config.endpoints.media}${path}`;
|
||||
}
|
||||
|
||||
function api(path: string): string {
|
||||
return `${Config.endpoints.apiClient}${path}`;
|
||||
}
|
||||
|
||||
const INPUTS = [
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png`),
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png?ex=1&is=2&hm=3`),
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png?width=64&format=webp`),
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/a%20b.png`),
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/../cat.png`),
|
||||
media(`/avatars/${CHANNEL_ID}/abc.png`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png?mid=${MESSAGE_ID}`),
|
||||
'https://example.test/attachments/1/2/cat.png',
|
||||
'not a url',
|
||||
];
|
||||
|
||||
describe('attachment links', () => {
|
||||
afterEach(() => {
|
||||
setCachedProxyAttachmentsThroughApi(null);
|
||||
});
|
||||
|
||||
test('with the setting off every link matches the media proxy form', () => {
|
||||
for (const input of INPUTS) {
|
||||
expect(publicAttachmentUrl(input)).toBe(signAttachmentUrl(input));
|
||||
}
|
||||
expect(makePublicAttachmentUrl(createChannelID(CHANNEL_ID), ATTACHMENT_ID, 'cat.png')).toBe(
|
||||
makeSignedAttachmentCdnUrl(createChannelID(CHANNEL_ID), ATTACHMENT_ID, 'cat.png'),
|
||||
);
|
||||
setCachedProxyAttachmentsThroughApi(false);
|
||||
for (const input of INPUTS) {
|
||||
expect(publicAttachmentUrl(input)).toBe(signAttachmentUrl(input));
|
||||
expect(isGuardedAttachmentUrl(input)).toBe(false);
|
||||
}
|
||||
const routePath = `/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png`;
|
||||
for (const path of [routePath, `/v1${routePath}`, `/api${routePath}`]) {
|
||||
expect(isAttachmentMediaRequest('GET', path)).toBe(false);
|
||||
expect(isAttachmentMediaRequest('HEAD', path)).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
test('with the setting on no media proxy link is handed out', () => {
|
||||
setCachedProxyAttachmentsThroughApi(true);
|
||||
expect(publicAttachmentUrl(media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png?ex=1&is=2&hm=3`))).toBe(
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png`),
|
||||
);
|
||||
expect(publicAttachmentUrl(media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png?width=64&mt=x&mid=1`))).toBe(
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png?width=64`),
|
||||
);
|
||||
expect(makePublicAttachmentUrl(createChannelID(CHANNEL_ID), ATTACHMENT_ID, 'cat.png')).toBe(
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png`),
|
||||
);
|
||||
for (const input of INPUTS) {
|
||||
expect(publicAttachmentUrl(input).includes('hm=')).toBe(false);
|
||||
}
|
||||
const routePath = `/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png`;
|
||||
expect(isAttachmentMediaRequest('GET', `/api/v1${routePath}`)).toBe(true);
|
||||
expect(isAttachmentMediaRequest('POST', routePath)).toBe(false);
|
||||
expect(isAttachmentMediaRequest('GET', `/channels/${CHANNEL_ID}/messages`)).toBe(false);
|
||||
});
|
||||
|
||||
test('with the setting on links to stored attachments in either form need the requester checked', () => {
|
||||
setCachedProxyAttachmentsThroughApi(true);
|
||||
const guarded = [
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png`),
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png?ex=1&is=2&hm=3`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png?mid=${MESSAGE_ID}`),
|
||||
];
|
||||
for (const url of guarded) expect(isGuardedAttachmentUrl(url)).toBe(true);
|
||||
for (const url of [
|
||||
media(`/avatars/${CHANNEL_ID}/abc.png`),
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/../cat.png`),
|
||||
'https://example.test/attachments/1/2/cat.png',
|
||||
'not a url',
|
||||
]) {
|
||||
expect(isGuardedAttachmentUrl(url)).toBe(false);
|
||||
}
|
||||
expect(parseMediaAttachmentUrl(media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/a%20b.png?ex=1`))).toEqual({
|
||||
channelId: CHANNEL_ID,
|
||||
attachmentId: ATTACHMENT_ID,
|
||||
filename: 'a b.png',
|
||||
messageId: null,
|
||||
});
|
||||
});
|
||||
|
||||
test('links to stored attachments are only sent to the unfurler while the setting is off', () => {
|
||||
const stored = media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png`);
|
||||
const proxied = api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png?mid=${MESSAGE_ID}`);
|
||||
const content = `${stored}?ex=1&is=2&hm=3 ${proxied} https://example.test/page`;
|
||||
const off = extractURLs(content);
|
||||
expect(off).toContain(stored);
|
||||
expect(off).toContain(proxied);
|
||||
setCachedProxyAttachmentsThroughApi(true);
|
||||
expect(extractURLs(content)).toEqual(['https://example.test/page']);
|
||||
});
|
||||
|
||||
test('a media link names its message only when the message owns the stored file', () => {
|
||||
const storageChannelId = createChannelID(CHANNEL_ID);
|
||||
const copyChannelId = createChannelID(snowflakes.generate());
|
||||
const owner = {channelId: copyChannelId, messageId: createMessageID(MESSAGE_ID), storageChannelId};
|
||||
const url = media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png?ex=1&is=2&hm=3&mt=4&width=64`);
|
||||
expect(apiFormOfMediaUrl(url, owner)).toBe(
|
||||
api(`/channels/${copyChannelId}/attachments/${ATTACHMENT_ID}/cat.png?mid=${MESSAGE_ID}&width=64`),
|
||||
);
|
||||
expect(apiFormOfMediaUrl(url, null)).toBe(
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png?width=64`),
|
||||
);
|
||||
expect(apiFormOfMediaUrl(url, {...owner, storageChannelId: copyChannelId})).toBe(
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png?width=64`),
|
||||
);
|
||||
});
|
||||
|
||||
test('an api link parses back to its parts and rejects tricks', () => {
|
||||
const url = makeApiAttachmentUrl(
|
||||
createChannelID(CHANNEL_ID),
|
||||
ATTACHMENT_ID,
|
||||
'a%20b.png',
|
||||
createMessageID(MESSAGE_ID),
|
||||
);
|
||||
expect(parseApiAttachmentUrl(url)).toEqual({
|
||||
channelId: CHANNEL_ID,
|
||||
attachmentId: ATTACHMENT_ID,
|
||||
filename: 'a b.png',
|
||||
messageId: MESSAGE_ID,
|
||||
});
|
||||
expect(
|
||||
parseApiAttachmentUrl(api(`/v1/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png`)),
|
||||
).not.toBeNull();
|
||||
for (const rejected of [
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/%2E%2E/cat.png`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/a/../cat.png`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat%00.png`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/..%2f..%2Fusers%2F%40me`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/..%5c..%5Cusers`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat%2epng`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png?mid=abc`),
|
||||
api(`/channels/${CHANNEL_ID}/attachments/complete`),
|
||||
`https://example.test/channels/${CHANNEL_ID}/attachments/${ATTACHMENT_ID}/cat.png`,
|
||||
media(`/attachments/${CHANNEL_ID}/${ATTACHMENT_ID}/cat.png`),
|
||||
]) {
|
||||
expect(parseApiAttachmentUrl(rejected)).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import {Config} from '@app/api/Config';
|
||||
import type {ChannelRepository} from '@app/api/channel/ChannelRepository';
|
||||
import type {AuthenticatedChannel} from '@app/api/channel/services/AuthenticatedChannel';
|
||||
import {requestSvcJson} from '@app/api/infrastructure/SvcErrorReply';
|
||||
import {isApiProxiedAttachments} from '@app/api/instance/AttachmentAccessMode';
|
||||
import {getInstanceProductName} from '@app/api/instance/ProductName';
|
||||
import {Logger} from '@app/api/Logger';
|
||||
import type {Channel} from '@app/api/models/Channel';
|
||||
@@ -99,6 +100,9 @@ export class MessageResponseDataService {
|
||||
attachment_url_secret_base64: Config.mediaProxy.attachmentUrls.secretsBase64[0],
|
||||
attachment_decay: await attachmentDecayEnabled(),
|
||||
instance_name: getInstanceProductName(),
|
||||
...(isApiProxiedAttachments() && access.includeHidden !== true
|
||||
? {attachment_api_base: Config.endpoints.apiClient}
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
constructor(private readonly connectionManager: INatsConnectionManager) {}
|
||||
|
||||
@@ -4,7 +4,7 @@ import type {GuildID, UserID} from '@app/api/BrandedTypes';
|
||||
import {executeConditional, fetchOne} from '@app/api/database/CassandraQueryExecution';
|
||||
import type {GuildThreadStateRow} from '@app/api/database/types/ThreadTypes';
|
||||
import {GuildThreadState} from '@app/api/Tables';
|
||||
import type {HonoEnv} from '@app/api/types/HonoEnv';
|
||||
import type {AuthTokenType, HonoEnv} from '@app/api/types/HonoEnv';
|
||||
import {CHANNEL_THREADS_CLIENT_FEATURE} from '@app/api/utils/featureUtils';
|
||||
import {
|
||||
type ChannelThreadsConfig,
|
||||
@@ -56,11 +56,15 @@ export function viewerActive(viewer: ThreadViewer, _guildId: GuildID | bigint |
|
||||
return userViewerActive(viewer);
|
||||
}
|
||||
|
||||
function isSessionBacked(authTokenType: AuthTokenType | undefined): boolean {
|
||||
return authTokenType === 'session' || authTokenType === 'attachment_media';
|
||||
}
|
||||
|
||||
export function viewerFromCtx(ctx: Context<HonoEnv>): ThreadViewer {
|
||||
const user = ctx.get('user');
|
||||
const bot = user.isBot;
|
||||
const capable =
|
||||
bot || (ctx.get('authTokenType') === 'session' && ctx.get('clientFeatures').has(CHANNEL_THREADS_CLIENT_FEATURE));
|
||||
bot || (isSessionBacked(ctx.get('authTokenType')) && ctx.get('clientFeatures').has(CHANNEL_THREADS_CLIENT_FEATURE));
|
||||
return {kind: 'user', userId: user.id, bot, capable};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {resolveFavoriteGifEntry} from '@app/api/favorite_gif/FavoriteGifResolver';
|
||||
import {mediaUrlForRequester} from '@app/api/attachment/AttachmentMediaAccess';
|
||||
import {resolveFavoriteGifEntry, unresolvedFavoriteGifEntry} from '@app/api/favorite_gif/FavoriteGifResolver';
|
||||
import {resolveGifRequestCountry} from '@app/api/gif/GifRequestCountry';
|
||||
import {DefaultUserOnly, LoginRequired} from '@app/api/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';
|
||||
@@ -44,9 +45,18 @@ export function FavoriteGifController(app: HonoApp) {
|
||||
const locale = getLocale(ctx);
|
||||
const country = await resolveGifRequestCountry(ctx.req.raw);
|
||||
const uniqueUrls = [...new Set(urls)];
|
||||
const resolved = await mapWithConcurrency(uniqueUrls, FAVORITE_GIF_RESOLVE_CONCURRENCY, (url) =>
|
||||
resolveFavoriteGifEntry({url, locale, country, gifService, mediaService, unfurlerService}),
|
||||
);
|
||||
const resolved = await mapWithConcurrency(uniqueUrls, FAVORITE_GIF_RESOLVE_CONCURRENCY, async (url) => {
|
||||
const mediaUrl = await mediaUrlForRequester(ctx, url);
|
||||
if (mediaUrl === null) return unresolvedFavoriteGifEntry(url);
|
||||
return resolveFavoriteGifEntry({
|
||||
url: mediaUrl ?? url,
|
||||
locale,
|
||||
country,
|
||||
gifService,
|
||||
mediaService,
|
||||
unfurlerService,
|
||||
});
|
||||
});
|
||||
const entriesByUrl = new Map(uniqueUrls.map((url, index) => [url, resolved[index]!]));
|
||||
const entries = urls.map((url) => entriesByUrl.get(url)!);
|
||||
return ctx.json({entries});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {publicAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {tryExtractGifProviderSlug} from '@app/api/gif/GifProviderUtils';
|
||||
import type {NatsGifProvider} from '@app/api/gif/NatsGifProvider';
|
||||
import type {IMediaService, MediaProxyMetadataResponse} from '@app/api/infrastructure/IMediaService';
|
||||
@@ -71,6 +71,10 @@ export async function resolveFavoriteGifEntry({
|
||||
return favoriteGifEntryFromExternalMedia({url, mediaService, metadata});
|
||||
}
|
||||
|
||||
export function unresolvedFavoriteGifEntry(url: string): ResolvedGifEntrySchema {
|
||||
return {url, proxy_url: url, width: 0, height: 0, media: {}, content_type: '', placeholder: null};
|
||||
}
|
||||
|
||||
async function resolveProviderGifUrl({
|
||||
url,
|
||||
locale,
|
||||
@@ -108,8 +112,8 @@ function favoriteGifEntryFromExternalMedia({
|
||||
mediaService: IMediaService;
|
||||
metadata: MediaProxyMetadataResponse | null;
|
||||
}): ResolvedGifEntrySchema {
|
||||
const signedUrl = signAttachmentUrl(url);
|
||||
const proxyUrl = signAttachmentUrl(mediaService.getExternalMediaProxyURL(url));
|
||||
const signedUrl = publicAttachmentUrl(url);
|
||||
const proxyUrl = publicAttachmentUrl(mediaService.getExternalMediaProxyURL(url));
|
||||
const media = directMediaFormatFromMetadata({url: signedUrl, proxyUrl, metadata});
|
||||
return {
|
||||
url: signedUrl,
|
||||
@@ -154,16 +158,16 @@ function favoriteGifEntryFromEmbedMedia({
|
||||
mediaService: IMediaService;
|
||||
media: EmbedMediaResponse;
|
||||
}): ResolvedGifEntrySchema {
|
||||
const proxyUrl = signAttachmentUrl(media.proxy_url ?? mediaService.getExternalMediaProxyURL(media.url));
|
||||
const proxyUrl = publicAttachmentUrl(media.proxy_url ?? mediaService.getExternalMediaProxyURL(media.url));
|
||||
const width = media.width ?? 0;
|
||||
const height = media.height ?? 0;
|
||||
const contentType = media.content_type ?? '';
|
||||
return {
|
||||
url: signAttachmentUrl(url),
|
||||
url: publicAttachmentUrl(url),
|
||||
proxy_url: proxyUrl,
|
||||
width,
|
||||
height,
|
||||
media: directMediaFormatFromDetails({url: signAttachmentUrl(media.url), proxyUrl, contentType, width, height}),
|
||||
media: directMediaFormatFromDetails({url: publicAttachmentUrl(media.url), proxyUrl, contentType, width, height}),
|
||||
content_type: contentType,
|
||||
placeholder: media.placeholder ?? null,
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {mediaUrlForRequester} from '@app/api/attachment/AttachmentMediaAccess';
|
||||
import {signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {createChannelID, createMemeID, createMessageID} from '@app/api/BrandedTypes';
|
||||
import {viewerFromCtx} from '@app/api/experiment/ChannelThreadsGate';
|
||||
import {DefaultUserOnly, LoginRequired} from '@app/api/middleware/AuthMiddleware';
|
||||
@@ -8,6 +10,7 @@ import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';
|
||||
import {RateLimitConfigs} from '@app/api/RateLimitConfig';
|
||||
import type {HonoApp} from '@app/api/types/HonoEnv';
|
||||
import {Validator} from '@app/api/Validator';
|
||||
import {MediaMetadataError} from '@fluxer/errors/src/domains/core/MediaMetadataError';
|
||||
import {ChannelIdMessageIdParam, MemeIdParam} from '@fluxer/schema/src/domains/common/CommonParamSchemas';
|
||||
import {
|
||||
CreateFavoriteMemeBodySchema,
|
||||
@@ -55,9 +58,12 @@ export function FavoriteMemeController(app: HonoApp) {
|
||||
description: 'Saves a new meme to favorites from a provided URL.',
|
||||
}),
|
||||
async (ctx) => {
|
||||
const data = ctx.req.valid('json');
|
||||
const mediaUrl = await mediaUrlForRequester(ctx, data.url);
|
||||
if (mediaUrl === null) throw new MediaMetadataError('URL');
|
||||
const meme = await ctx.get('favoriteMemeRequestService').createFromUrl({
|
||||
user: ctx.get('user'),
|
||||
data: ctx.req.valid('json'),
|
||||
data: mediaUrl === undefined ? data : {...data, url: signAttachmentUrl(mediaUrl)},
|
||||
});
|
||||
return ctx.json(meme, 201);
|
||||
},
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {makeSignedAttachmentCdnUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {makePublicAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {userIdToChannelId} from '@app/api/BrandedTypes';
|
||||
import type {FavoriteMeme} from '@app/api/models/FavoriteMeme';
|
||||
import {assertSafeByteSize} from '@app/api/utils/ByteSizeUtils';
|
||||
import type {FavoriteMemeResponse} from '@fluxer/schema/src/domains/meme/MemeSchemas';
|
||||
|
||||
export function mapFavoriteMemeToResponse(meme: FavoriteMeme): FavoriteMemeResponse {
|
||||
const url = makeSignedAttachmentCdnUrl(userIdToChannelId(meme.userId), meme.attachmentId, meme.filename);
|
||||
const url = makePublicAttachmentUrl(userIdToChannelId(meme.userId), meme.attachmentId, meme.filename);
|
||||
return {
|
||||
id: meme.id.toString(),
|
||||
user_id: meme.userId.toString(),
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import type {ApiContext} from '@app/api/ApiContext';
|
||||
import {isGuardedAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {makeSignedAttachmentCdnUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import type {ChannelID, MemeID, MessageID, UserID} from '@app/api/BrandedTypes';
|
||||
import {createAttachmentID, createMemeID, userIdToChannelId} from '@app/api/BrandedTypes';
|
||||
import {Config} from '@app/api/Config';
|
||||
import type {ChannelService} from '@app/api/channel/services/ChannelService';
|
||||
import {attachmentStorageChannelId, makeAttachmentCdnKey} from '@app/api/channel/services/message/MessageHelpers';
|
||||
import {
|
||||
attachmentStorageChannelId,
|
||||
isOwnedEmbedAttachment,
|
||||
makeAttachmentCdnKey,
|
||||
} from '@app/api/channel/services/message/MessageHelpers';
|
||||
import type {ThreadViewer} from '@app/api/experiment/ChannelThreadsGate';
|
||||
import {mapFavoriteMemeToResponse} from '@app/api/favorite_meme/FavoriteMemeModel';
|
||||
import type {FavoriteMemeRepository} from '@app/api/favorite_meme/FavoriteMemeRepository';
|
||||
@@ -637,7 +642,7 @@ export class FavoriteMemeService {
|
||||
channelId: ChannelID,
|
||||
): Promise<FavoriteMemeMedia | null> {
|
||||
const media = embed.image || embed.video || embed.thumbnail;
|
||||
if (!media?.url) {
|
||||
if (!media?.url || (isGuardedAttachmentUrl(media.url) && !isOwnedEmbedAttachment(media))) {
|
||||
return null;
|
||||
}
|
||||
const filename = this.extractFilenameFromUrl(media.url) || fallbackFilename;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {isGuardedAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {stripOwnAttachmentSignature} from '@app/api/attachment/AttachmentUrls';
|
||||
import type {ChannelID, MessageID} from '@app/api/BrandedTypes';
|
||||
import type {RichEmbedMediaWithMetadata} from '@app/api/channel/EmbedTypes';
|
||||
@@ -358,6 +359,7 @@ export class EmbedService {
|
||||
metadata: MediaProxyMetadataResponse | null;
|
||||
}> {
|
||||
const stored = stripOwnAttachmentSignature(url);
|
||||
if (isGuardedAttachmentUrl(url)) return {url: stored, metadata: null};
|
||||
const directMetadata = await this.mediaService.getMetadata({
|
||||
type: 'external',
|
||||
url,
|
||||
@@ -373,7 +375,7 @@ export class EmbedService {
|
||||
for (const embed of unfurled) {
|
||||
const candidates = [embed.video?.url, embed.audio?.url, embed.image?.url, embed.thumbnail?.url];
|
||||
for (const candidate of candidates) {
|
||||
if (!candidate || candidate === url) continue;
|
||||
if (!candidate || candidate === url || isGuardedAttachmentUrl(candidate)) continue;
|
||||
const candidateMetadata = await this.mediaService.getMetadata({
|
||||
type: 'external',
|
||||
url: candidate,
|
||||
@@ -398,7 +400,9 @@ export class EmbedService {
|
||||
): Promise<EmbedAuthor | null> {
|
||||
if (!author) return null;
|
||||
let iconUrl: string | null = null;
|
||||
if (author.icon_url) {
|
||||
if (author.icon_url && isGuardedAttachmentUrl(author.icon_url)) {
|
||||
iconUrl = stripOwnAttachmentSignature(author.icon_url);
|
||||
} else if (author.icon_url) {
|
||||
const metadata = await this.mediaService.getMetadata({
|
||||
type: 'external',
|
||||
url: author.icon_url,
|
||||
@@ -419,7 +423,9 @@ export class EmbedService {
|
||||
): Promise<EmbedFooter | null> {
|
||||
if (!footer) return null;
|
||||
let iconUrl: string | null = null;
|
||||
if (footer.icon_url) {
|
||||
if (footer.icon_url && isGuardedAttachmentUrl(footer.icon_url)) {
|
||||
iconUrl = stripOwnAttachmentSignature(footer.icon_url);
|
||||
} else if (footer.icon_url) {
|
||||
const metadata = await this.mediaService.getMetadata({
|
||||
type: 'external',
|
||||
url: footer.icon_url,
|
||||
|
||||
@@ -4,7 +4,7 @@ import fs from 'node:fs';
|
||||
import {Readable} from 'node:stream';
|
||||
import {pipeline} from 'node:stream/promises';
|
||||
import type {ReadableStream as NodeReadableStream} from 'node:stream/web';
|
||||
import {signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {publicAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {Config} from '@app/api/Config';
|
||||
import {
|
||||
IMediaService,
|
||||
@@ -188,7 +188,7 @@ export class MediaService extends IMediaService {
|
||||
|
||||
getExternalMediaProxyURL(url: string): string {
|
||||
if (attachmentStorageKeyFromUrl(url, Config.endpoints.media) !== null) {
|
||||
return signAttachmentUrl(url);
|
||||
return publicAttachmentUrl(url);
|
||||
}
|
||||
let urlObj: URL;
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
let cachedProxyAttachmentsThroughApi: boolean | null = null;
|
||||
|
||||
export function isApiProxiedAttachments(): boolean {
|
||||
return cachedProxyAttachmentsThroughApi === true;
|
||||
}
|
||||
|
||||
export function setCachedProxyAttachmentsThroughApi(value: boolean | null): void {
|
||||
cachedProxyAttachmentsThroughApi = value;
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
setCachedAccountsConfig,
|
||||
setCachedEmailDeliveryState,
|
||||
} from '@app/api/instance/AccountsConfigCache';
|
||||
import {setCachedProxyAttachmentsThroughApi} from '@app/api/instance/AttachmentAccessMode';
|
||||
import {getLegacyDateOfBirthCollectionDefault} from '@app/api/instance/DateOfBirthCollectionCache';
|
||||
import {
|
||||
describeIncompleteSmtpConfig,
|
||||
@@ -426,6 +427,7 @@ interface InstanceAttachmentDecayConfig {
|
||||
|
||||
interface InstanceMediaConfig {
|
||||
attachment_decay: InstanceAttachmentDecayConfig;
|
||||
proxy_attachments_through_api?: boolean | undefined;
|
||||
}
|
||||
|
||||
export interface InstanceAttachmentDecayEffectiveConfig {
|
||||
@@ -444,6 +446,7 @@ interface InstanceMediaAdminConfig {
|
||||
attachment_decay: InstanceAttachmentDecayConfig & {
|
||||
effective: InstanceAttachmentDecayEffectiveConfig;
|
||||
};
|
||||
proxy_attachments_through_api: boolean;
|
||||
}
|
||||
|
||||
export interface InstanceIntegrationsConfigPatch {
|
||||
@@ -461,6 +464,7 @@ export interface InstanceIntegrationsConfigPatch {
|
||||
|
||||
interface InstanceMediaConfigPatch {
|
||||
attachment_decay?: Partial<InstanceAttachmentDecayConfig>;
|
||||
proxy_attachments_through_api?: boolean;
|
||||
}
|
||||
|
||||
export interface InstanceRegistrationUrl extends RegistrationUrlResponse {
|
||||
@@ -1327,6 +1331,7 @@ const StoredAttachmentDecaySchema = z
|
||||
);
|
||||
const StoredInstanceMediaSchema = z.object({
|
||||
attachment_decay: StoredAttachmentDecaySchema.prefault({}),
|
||||
proxy_attachments_through_api: z.boolean().optional(),
|
||||
}) satisfies z.ZodType<InstanceMediaConfig>;
|
||||
|
||||
function normalizeAttachmentDecayBounds(value: InstanceAttachmentDecayConfig): InstanceAttachmentDecayConfig {
|
||||
@@ -1906,7 +1911,13 @@ export class InstanceConfigRepository {
|
||||
checkStoredConfig('integrations', () =>
|
||||
parseStoredInstanceIntegrationsConfig(snapshot.get(INSTANCE_INTEGRATIONS_CONFIG_KEY) ?? null),
|
||||
);
|
||||
checkStoredConfig('media', () => parseStoredInstanceMediaConfig(snapshot.get(INSTANCE_MEDIA_CONFIG_KEY) ?? null));
|
||||
setCachedProxyAttachmentsThroughApi(null);
|
||||
checkStoredConfig('media', () =>
|
||||
setCachedProxyAttachmentsThroughApi(
|
||||
parseStoredInstanceMediaConfig(snapshot.get(INSTANCE_MEDIA_CONFIG_KEY) ?? null).proxy_attachments_through_api ??
|
||||
null,
|
||||
),
|
||||
);
|
||||
setStoredBillingConfig(parseStoredInstanceBillingConfig(snapshot.get(INSTANCE_BILLING_CONFIG_KEY) ?? null));
|
||||
const appPublic = parseStoredAppPublicConfig(snapshot.get(APP_PUBLIC_CONFIG_KEY) ?? null);
|
||||
setCachedConfiguredLegalUrls(appPublic.legal);
|
||||
@@ -1940,6 +1951,7 @@ export class InstanceConfigRepository {
|
||||
|
||||
clearCacheForTesting(): void {
|
||||
setCachedProductName(null);
|
||||
setCachedProxyAttachmentsThroughApi(null);
|
||||
setCachedAccountIdentity(null);
|
||||
setCachedAccountsConfig(null);
|
||||
setCachedEmailDeliveryState(null);
|
||||
@@ -2842,16 +2854,22 @@ export class InstanceConfigRepository {
|
||||
return parseStoredInstanceMediaConfig(raw);
|
||||
}
|
||||
|
||||
setInstanceMediaConfig(config: InstanceMediaConfigPatch): Promise<InstanceMediaConfig> {
|
||||
return this.updateStoredConfig(INSTANCE_MEDIA_CONFIG_KEY, (raw) => {
|
||||
async setInstanceMediaConfig(config: InstanceMediaConfigPatch): Promise<InstanceMediaConfig> {
|
||||
const next = await this.updateStoredConfig(INSTANCE_MEDIA_CONFIG_KEY, (raw) => {
|
||||
const current = parseStoredInstanceMediaConfig(raw);
|
||||
const proxyAttachmentsThroughApi = config.proxy_attachments_through_api ?? current.proxy_attachments_through_api;
|
||||
return decodeInstanceMediaConfig({
|
||||
attachment_decay: {
|
||||
...current.attachment_decay,
|
||||
...(config.attachment_decay ?? {}),
|
||||
},
|
||||
...(proxyAttachmentsThroughApi === undefined
|
||||
? {}
|
||||
: {proxy_attachments_through_api: proxyAttachmentsThroughApi}),
|
||||
});
|
||||
});
|
||||
setCachedProxyAttachmentsThroughApi(next.proxy_attachments_through_api ?? null);
|
||||
return next;
|
||||
}
|
||||
|
||||
async getEffectiveAttachmentDecayConfig(): Promise<InstanceAttachmentDecayEffectiveConfig> {
|
||||
@@ -2873,6 +2891,7 @@ export class InstanceConfigRepository {
|
||||
...media.attachment_decay,
|
||||
effective: attachmentDecay,
|
||||
},
|
||||
proxy_attachments_through_api: media.proxy_attachments_through_api === true,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import {Config} from '@app/api/Config';
|
||||
import type {AgePolicyConfig} from '@app/api/config/APIConfig';
|
||||
import type {NatsGifProvider} from '@app/api/gif/NatsGifProvider';
|
||||
import {withAccountIdentitySetupLock} from '@app/api/instance/AccountIdentitySetupLock';
|
||||
import {isApiProxiedAttachments} from '@app/api/instance/AttachmentAccessMode';
|
||||
import {type CustomBadge, compareBigInts} from '@app/api/instance/custom_badges/CustomBadgeRepository';
|
||||
import {
|
||||
type DiscoveryValidators,
|
||||
@@ -131,6 +132,7 @@ function buildDiscoveryStaticInput(
|
||||
stripe_serviceable: isStripeServiceable(),
|
||||
self_hosted: Config.instance.selfHosted,
|
||||
presigned_attachment_uploads: Config.presignedAttachmentUploadsEnabled,
|
||||
...(isApiProxiedAttachments() ? {proxy_attachments_through_api: true} : {}),
|
||||
emails_enabled: runtime.emailDeliveryState === EmailDeliveryStates.ON,
|
||||
phone_verification_enabled: false,
|
||||
discovery_enabled: Config.discovery.enabled,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {isAttachmentMediaRequest} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import type {HonoEnv} from '@app/api/types/HonoEnv';
|
||||
import {normalizeRequestPath} from '@app/api/utils/RequestPathUtils';
|
||||
import {ServiceUnavailableError} from '@fluxer/errors/src/domains/core/ServiceUnavailableError';
|
||||
@@ -15,7 +16,7 @@ interface ConcurrencyLimitOptions {
|
||||
export function ConcurrencyLimitMiddleware({maxInflightRequests}: ConcurrencyLimitOptions) {
|
||||
let inflight = 0;
|
||||
return createMiddleware<HonoEnv>(async (ctx, next) => {
|
||||
if (PROBE_PATHS.has(normalizeRequestPath(ctx.req.path))) {
|
||||
if (PROBE_PATHS.has(normalizeRequestPath(ctx.req.path)) || isAttachmentMediaRequest(ctx.req.method, ctx.req.path)) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {isOperatorPath, isOperatorRequest} from '@app/api/admin/operator/OperatorRequests';
|
||||
import {openAttachmentMediaToken} from '@app/api/attachment/AttachmentMediaToken';
|
||||
import {isAttachmentMediaRequest} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import * as AuthSession from '@app/api/auth/AuthSession';
|
||||
import {SYSTEM_USER_ID} from '@app/api/constants/Core';
|
||||
import {Logger} from '@app/api/Logger';
|
||||
@@ -84,6 +86,23 @@ function setUserInContext(ctx: Context<HonoEnv>, user: User, trackActivity: bool
|
||||
}
|
||||
}
|
||||
|
||||
async function authenticateAttachmentMediaToken(ctx: Context<HonoEnv>): Promise<void> {
|
||||
const token = ctx.req.query('mt');
|
||||
if (!token) return;
|
||||
const payload = openAttachmentMediaToken(token);
|
||||
if (!payload) return;
|
||||
const apiContext = ctx.get('apiContext');
|
||||
const authSession = await apiContext.services.users.getAuthSessionByToken(Buffer.from(payload.s, 'base64url'));
|
||||
if (!authSession || authSession.isExpiredSsoSession() || authSession.userId.toString() !== payload.u) return;
|
||||
const user = await apiContext.services.users.findUnique(authSession.userId);
|
||||
if (!user || isSignInRefused(user)) return;
|
||||
ctx.set('authSession', authSession);
|
||||
ctx.set('authTokenType', 'attachment_media');
|
||||
ctx.set('attachmentMediaToken', payload);
|
||||
ctx.set('clientFeatures', new Set(payload.f));
|
||||
setUserInContext(ctx, user, false);
|
||||
}
|
||||
|
||||
export const UserMiddleware = createMiddleware<HonoEnv>(async (ctx, next) => {
|
||||
const path = stripApiPrefix(ctx.req.path);
|
||||
if (SKIP_PATHS.has(path)) {
|
||||
@@ -111,6 +130,8 @@ export const UserMiddleware = createMiddleware<HonoEnv>(async (ctx, next) => {
|
||||
if (!parsed) {
|
||||
if (rawAuthHeader) {
|
||||
recordAuthFailure(resolvedClientIp, hashRequestToken(rawAuthHeader));
|
||||
} else if (isAttachmentMediaRequest(ctx.req.method, ctx.req.path)) {
|
||||
await authenticateAttachmentMediaToken(ctx);
|
||||
}
|
||||
return next();
|
||||
}
|
||||
|
||||
Generated
+87
-1
@@ -156,7 +156,7 @@
|
||||
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
|
||||
}
|
||||
},
|
||||
"description": "Reissues the expiring signature on attachment URLs. Returns one entry per requested URL, in the order they were requested, each pairing the URL exactly as it was sent with a freshly signed copy. A URL that is not an attachment URL of this instance is returned unchanged. No membership or existence check is performed.",
|
||||
"description": "Reissues the expiring signature on attachment URLs. Returns one entry per requested URL, in the order they were requested, each pairing the URL exactly as it was sent with a freshly signed copy. A URL that is not an attachment URL of this instance is returned unchanged. No membership or existence check is performed. When the instance serves attachments through the API, the refreshed URL points at the API and has no signature.",
|
||||
"security": [{"botToken": []}, {"sessionToken": []}],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
@@ -18254,6 +18254,63 @@
|
||||
"security": [{"sessionToken": []}]
|
||||
}
|
||||
},
|
||||
"/users/@me/attachment-token": {
|
||||
"post": {
|
||||
"operationId": "create_attachment_token",
|
||||
"summary": "Create an attachment token",
|
||||
"tags": ["Messages"],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Success",
|
||||
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/AttachmentAccessTokenResponse"}}}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request - The request was malformed or contained invalid data",
|
||||
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized - Authentication is required or the token is invalid",
|
||||
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
|
||||
},
|
||||
"403": {
|
||||
"description": "Forbidden - You do not have permission to perform this action",
|
||||
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
|
||||
},
|
||||
"429": {
|
||||
"description": "Too Many Requests - You are being rate limited",
|
||||
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},
|
||||
"headers": {
|
||||
"Retry-After": {
|
||||
"description": "Number of seconds to wait before retrying (only on 429)",
|
||||
"schema": {"type": "integer"}
|
||||
},
|
||||
"X-RateLimit-Limit": {
|
||||
"description": "The number of requests that can be made in the current window",
|
||||
"schema": {"type": "integer"}
|
||||
},
|
||||
"X-RateLimit-Remaining": {
|
||||
"description": "The number of remaining requests that can be made",
|
||||
"schema": {"type": "integer"}
|
||||
},
|
||||
"X-RateLimit-Reset": {
|
||||
"description": "Unix timestamp when the rate limit resets",
|
||||
"schema": {"type": "integer"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"500": {
|
||||
"description": "Internal Server Error - An unexpected error occurred",
|
||||
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}
|
||||
}
|
||||
},
|
||||
"description": "Creates a token for the one attachment in the given link, for places that cannot send a header, such as a video player, a download or a link opened in the browser. The token works for five minutes, only for that file and only while the session that asked for it stays signed in and can still read the message. Only active when the instance serves attachments through the API.",
|
||||
"security": [{"sessionToken": []}],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/AttachmentAccessTokenRequest"}}}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/users/@me/authorized-ips": {
|
||||
"delete": {
|
||||
"operationId": "forget_authorized_ips",
|
||||
@@ -32206,6 +32263,31 @@
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/BillingDocumentResponse"}
|
||||
},
|
||||
"AttachmentAccessTokenRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"url": {
|
||||
"type": "string",
|
||||
"maxLength": 2048,
|
||||
"description": "The attachment link on the API that the token is for"
|
||||
}
|
||||
},
|
||||
"required": ["url"]
|
||||
},
|
||||
"AttachmentAccessTokenResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"token": {"type": "string", "description": "Value for the mt query parameter on the link it was made for"},
|
||||
"expires_at": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$",
|
||||
"description": "When the token stops working"
|
||||
}
|
||||
},
|
||||
"required": ["token", "expires_at"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"ApplicationListResponse": {"type": "array", "items": {"$ref": "#/components/schemas/ApplicationResponse"}},
|
||||
"AppAttestKeyRegisterRequest": {
|
||||
"type": "object",
|
||||
@@ -40054,6 +40136,10 @@
|
||||
"type": "boolean",
|
||||
"description": "Whether clients can request presigned attachment upload URLs"
|
||||
},
|
||||
"proxy_attachments_through_api": {
|
||||
"description": "Whether attachment links point at the API and need the viewer to be signed in. Missing means false",
|
||||
"type": "boolean"
|
||||
},
|
||||
"emails_enabled": {
|
||||
"type": "boolean",
|
||||
"description": "Whether the instance sends emails (verification, password reset, etc.)"
|
||||
|
||||
@@ -68,6 +68,10 @@ export const ChannelRateLimitConfigs = {
|
||||
bucket: 'channel:attachment:upload::channel_id',
|
||||
config: {limit: 10, windowMs: ms('10 seconds')},
|
||||
} as RouteRateLimitConfig,
|
||||
CHANNEL_ATTACHMENT_GET: {
|
||||
bucket: 'channel:attachment:read',
|
||||
config: {limit: 600, windowMs: ms('10 seconds')},
|
||||
} as RouteRateLimitConfig,
|
||||
ATTACHMENT_URLS_REFRESH: {
|
||||
bucket: 'attachment:refresh_urls',
|
||||
config: {limit: 60, windowMs: ms('10 seconds')},
|
||||
|
||||
@@ -12,6 +12,10 @@ export const UserRateLimitConfigs = {
|
||||
bucket: 'user:profile::target_id',
|
||||
config: {limit: 100, windowMs: ms('10 seconds')},
|
||||
} as RouteRateLimitConfig,
|
||||
USER_ATTACHMENT_TOKEN: {
|
||||
bucket: 'user:attachment_token',
|
||||
config: {limit: 120, windowMs: ms('10 seconds')},
|
||||
} as RouteRateLimitConfig,
|
||||
USER_CHECK_TAG: {
|
||||
bucket: 'user:check_tag',
|
||||
config: {limit: 60, windowMs: ms('10 seconds')},
|
||||
|
||||
@@ -2,17 +2,20 @@
|
||||
|
||||
import {AttachmentDecayRepository} from '@app/api/attachment/AttachmentDecayRepository';
|
||||
import {AttachmentDecayService, resolveDefaultAttachmentDecayConfig} from '@app/api/attachment/AttachmentDecayService';
|
||||
import {apiFormOfMediaUrl, makeApiAttachmentUrl, parseApiAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {makeSignedAttachmentCdnUrl, signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import type {ChannelID, GuildID, MessageID, UserID} from '@app/api/BrandedTypes';
|
||||
import {
|
||||
attachmentStorageChannelId,
|
||||
EMBED_MEDIA_OWNED_ATTACHMENT_FLAG,
|
||||
isCrosspostCopy,
|
||||
isOwnedEmbedAttachment,
|
||||
} from '@app/api/channel/services/message/MessageHelpers';
|
||||
import {
|
||||
type MessageResponseAccessContext,
|
||||
MessageResponseDataService,
|
||||
} from '@app/api/channel/services/message/MessageResponseDataService';
|
||||
import {isApiProxiedAttachments} from '@app/api/instance/AttachmentAccessMode';
|
||||
import {getChannelRepository, getUserRepository} from '@app/api/middleware/ServiceSingletons';
|
||||
import type {Attachment} from '@app/api/models/Attachment';
|
||||
import type {CallInfo} from '@app/api/models/CallInfo';
|
||||
@@ -61,12 +64,30 @@ class NoopNatsConnectionManager implements INatsConnectionManager {
|
||||
}
|
||||
}
|
||||
|
||||
function signOwnUrl(url: string | null | undefined): string | null {
|
||||
return url == null ? null : signAttachmentUrl(url);
|
||||
function withoutMediaToken(url: string): string {
|
||||
if (parseApiAttachmentUrl(url) === null) return url;
|
||||
const parsed = new URL(url);
|
||||
parsed.searchParams.delete('mt');
|
||||
return parsed.toString();
|
||||
}
|
||||
|
||||
function mediaProxyUrl(url: string | null | undefined): string | null {
|
||||
return url?.startsWith('http') ? signAttachmentUrl(url) : null;
|
||||
function apiOwnMediaUrl(url: string, message: Message, owned: boolean): string {
|
||||
const owner = owned
|
||||
? {channelId: message.channelId, messageId: message.id, storageChannelId: attachmentStorageChannelId(message)}
|
||||
: null;
|
||||
return apiFormOfMediaUrl(url, owner) ?? withoutMediaToken(url);
|
||||
}
|
||||
|
||||
function signOwnUrl(url: string | null | undefined, message: Message, owned = false): string | null {
|
||||
if (url == null) return null;
|
||||
if (isApiProxiedAttachments()) return apiOwnMediaUrl(url, message, owned);
|
||||
return signAttachmentUrl(url);
|
||||
}
|
||||
|
||||
function mediaProxyUrl(url: string | null | undefined, message: Message, owned = false): string | null {
|
||||
if (!url?.startsWith('http')) return null;
|
||||
if (isApiProxiedAttachments()) return apiOwnMediaUrl(url, message, owned);
|
||||
return signAttachmentUrl(url);
|
||||
}
|
||||
|
||||
export class RepositoryBackedMessageResponseDataService extends MessageResponseDataService {
|
||||
@@ -341,6 +362,9 @@ export class RepositoryBackedMessageResponseDataService extends MessageResponseD
|
||||
}
|
||||
|
||||
private mapAttachmentUrl(message: Message, attachment: Attachment): string {
|
||||
if (isApiProxiedAttachments()) {
|
||||
return makeApiAttachmentUrl(message.channelId, attachment.id, attachment.filename, message.id);
|
||||
}
|
||||
return makeSignedAttachmentCdnUrl(attachmentStorageChannelId(message), attachment.id, attachment.filename);
|
||||
}
|
||||
|
||||
@@ -398,17 +422,17 @@ export class RepositoryBackedMessageResponseDataService extends MessageResponseD
|
||||
private mapEmbed(embed: Embed, message: Message): MessageEmbedResponse {
|
||||
return {
|
||||
type: embed.type ?? 'rich',
|
||||
url: signOwnUrl(embed.url),
|
||||
url: signOwnUrl(embed.url, message),
|
||||
title: embed.title,
|
||||
color: embed.color,
|
||||
timestamp: embed.timestamp?.toISOString() ?? null,
|
||||
description: embed.description,
|
||||
author: this.mapEmbedAuthor(embed.author),
|
||||
author: this.mapEmbedAuthor(embed.author, message),
|
||||
image: this.mapEmbedMedia(embed.image, message),
|
||||
thumbnail: this.mapEmbedMedia(embed.thumbnail, message),
|
||||
footer: this.mapEmbedFooter(embed.footer),
|
||||
footer: this.mapEmbedFooter(embed.footer, message),
|
||||
fields: embed.fields.length > 0 ? embed.fields.map((field) => this.mapEmbedField(field)) : null,
|
||||
provider: this.mapEmbedProvider(embed.provider),
|
||||
provider: this.mapEmbedProvider(embed.provider, message),
|
||||
video: this.mapEmbedMedia(embed.video, message),
|
||||
audio: this.mapEmbedMedia(embed.audio, message),
|
||||
html: embed.html,
|
||||
@@ -419,30 +443,30 @@ export class RepositoryBackedMessageResponseDataService extends MessageResponseD
|
||||
};
|
||||
}
|
||||
|
||||
private mapEmbedAuthor(author: EmbedAuthor | null) {
|
||||
private mapEmbedAuthor(author: EmbedAuthor | null, message: Message) {
|
||||
if (!author?.name) return null;
|
||||
return {
|
||||
name: author.name,
|
||||
url: signOwnUrl(author.url),
|
||||
icon_url: signOwnUrl(author.iconUrl),
|
||||
proxy_icon_url: mediaProxyUrl(author.iconUrl),
|
||||
url: signOwnUrl(author.url, message),
|
||||
icon_url: signOwnUrl(author.iconUrl, message),
|
||||
proxy_icon_url: mediaProxyUrl(author.iconUrl, message),
|
||||
};
|
||||
}
|
||||
|
||||
private mapEmbedProvider(provider: EmbedProvider | null) {
|
||||
private mapEmbedProvider(provider: EmbedProvider | null, message: Message) {
|
||||
if (!provider?.name) return null;
|
||||
return {
|
||||
name: provider.name,
|
||||
url: signOwnUrl(provider.url),
|
||||
url: signOwnUrl(provider.url, message),
|
||||
};
|
||||
}
|
||||
|
||||
private mapEmbedFooter(footer: EmbedFooter | null) {
|
||||
private mapEmbedFooter(footer: EmbedFooter | null, message: Message) {
|
||||
if (!footer?.text) return null;
|
||||
return {
|
||||
text: footer.text,
|
||||
icon_url: signOwnUrl(footer.iconUrl),
|
||||
proxy_icon_url: mediaProxyUrl(footer.iconUrl),
|
||||
icon_url: signOwnUrl(footer.iconUrl, message),
|
||||
proxy_icon_url: mediaProxyUrl(footer.iconUrl, message),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -457,9 +481,10 @@ export class RepositoryBackedMessageResponseDataService extends MessageResponseD
|
||||
private mapEmbedMedia(media: EmbedMedia | null, message: Message) {
|
||||
if (!media?.url) return null;
|
||||
const resolved = this.resolveAttachmentUrl(media.url, message);
|
||||
const owned = isOwnedEmbedAttachment(media);
|
||||
return {
|
||||
url: signAttachmentUrl(resolved),
|
||||
proxy_url: mediaProxyUrl(resolved),
|
||||
url: signOwnUrl(resolved, message, owned) ?? resolved,
|
||||
proxy_url: mediaProxyUrl(resolved, message, owned),
|
||||
content_type: media.contentType,
|
||||
content_hash: media.contentHash,
|
||||
width: media.width,
|
||||
|
||||
@@ -4,6 +4,7 @@ import type {ApiContext} from '@app/api/ApiContext';
|
||||
import type {AdminService} from '@app/api/admin/AdminService';
|
||||
import type {AdminApiKeyService} from '@app/api/admin/services/AdminApiKeyService';
|
||||
import type {AdminArchiveService} from '@app/api/admin/services/AdminArchiveService';
|
||||
import type {AttachmentMediaTokenPayload} from '@app/api/attachment/AttachmentMediaToken';
|
||||
import type {AuthRequestService} from '@app/api/auth/AuthRequestService';
|
||||
import type {DesktopHandoffService} from '@app/api/auth/services/DesktopHandoffService';
|
||||
import type {SsoService} from '@app/api/auth/services/SsoService';
|
||||
@@ -85,7 +86,7 @@ import type {IRateLimitService} from '@pkgs/rate_limit/src/IRateLimitService';
|
||||
import type {IWorkerService} from '@pkgs/worker/src/contracts/IWorkerService';
|
||||
import type {Hono} from 'hono';
|
||||
|
||||
export type AuthTokenType = 'session' | 'bearer' | 'bot' | 'admin_api_key' | 'operator';
|
||||
export type AuthTokenType = 'session' | 'bearer' | 'bot' | 'admin_api_key' | 'operator' | 'attachment_media';
|
||||
|
||||
export interface HonoEnv {
|
||||
Variables: {
|
||||
@@ -102,6 +103,7 @@ export interface HonoEnv {
|
||||
adminUserId: UserID;
|
||||
adminUserAcls: Set<string>;
|
||||
authTokenType?: AuthTokenType;
|
||||
attachmentMediaToken?: AttachmentMediaTokenPayload;
|
||||
clientFeatures: ReadonlySet<string>;
|
||||
authToken?: string;
|
||||
oauthBearerAllowed?: boolean;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {signAttachmentUrl} from '@app/api/attachment/AttachmentUrls';
|
||||
import {isGuardedAttachmentUrl, publicAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {DefaultUserOnly, LoginRequired} from '@app/api/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';
|
||||
import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';
|
||||
@@ -16,11 +16,11 @@ import type {
|
||||
import {UnfurlRequest, UnfurlResponse} from '@fluxer/schema/src/domains/unfurl/UnfurlSchemas';
|
||||
|
||||
function signIfSet<T extends string | null | undefined>(url: T): T | string {
|
||||
return url ? signAttachmentUrl(url) : url;
|
||||
return url ? publicAttachmentUrl(url) : url;
|
||||
}
|
||||
|
||||
function signMedia(media: EmbedMediaResponse | null | undefined) {
|
||||
return media && {...media, url: signAttachmentUrl(media.url), proxy_url: signIfSet(media.proxy_url)};
|
||||
return media && {...media, url: publicAttachmentUrl(media.url), proxy_url: signIfSet(media.proxy_url)};
|
||||
}
|
||||
|
||||
function signEmbedUrls(embed: MessageEmbedChildResponse): MessageEmbedChildResponse {
|
||||
@@ -69,6 +69,7 @@ export function UnfurlController(app: HonoApp) {
|
||||
Validator('json', UnfurlRequest),
|
||||
async (ctx) => {
|
||||
const {url} = ctx.req.valid('json');
|
||||
if (isGuardedAttachmentUrl(url)) return ctx.json([]);
|
||||
const result = await getUnfurlerService().unfurlWithCachePolicy(url, 'block', {bypassCache: true});
|
||||
return ctx.json(result.embeds.map(signEmbedTree));
|
||||
},
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {createHash} from 'node:crypto';
|
||||
import {isGuardedAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {stripOwnAttachmentSignature} from '@app/api/attachment/AttachmentUrls';
|
||||
import {Config} from '@app/api/Config';
|
||||
import {Logger} from '@app/api/Logger';
|
||||
@@ -105,7 +106,7 @@ export function extractURLs(inputText: string) {
|
||||
for (const url of urls) {
|
||||
if (!isValidURL(url)) continue;
|
||||
if (InviteUtils.findInvite(url) != null) continue;
|
||||
if (isFluxerAppExcludedURL(url)) continue;
|
||||
if (isFluxerAppExcludedURL(url) || isGuardedAttachmentUrl(url)) continue;
|
||||
const encoded = idnaEncodeURL(url);
|
||||
if (!encoded) continue;
|
||||
const canonical = stripOwnAttachmentSignature(encoded);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {isGuardedAttachmentUrl} from '@app/api/attachment/AttachmentPublicUrl';
|
||||
import {stripOwnAttachmentSignature} from '@app/api/attachment/AttachmentUrls';
|
||||
import {
|
||||
type ChannelID,
|
||||
@@ -911,7 +912,7 @@ export class WebhookService {
|
||||
webhookId: WebhookID;
|
||||
avatarUrl: string | null;
|
||||
}): Promise<string | null> {
|
||||
if (!requestedAvatarUrl) return null;
|
||||
if (!requestedAvatarUrl || isGuardedAttachmentUrl(requestedAvatarUrl)) return null;
|
||||
const avatarUrl = stripOwnAttachmentSignature(requestedAvatarUrl);
|
||||
try {
|
||||
const cacheKey = `webhook:${webhookId}:avatar:${avatarUrl}`;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import SudoPrompt from '@app/features/auth/state/SudoPrompt';
|
||||
import {startApiAttachmentDesktopAuthorization} from '@app/features/messaging/state/ApiAttachmentDesktopAuthorization';
|
||||
import SessionManager from '@app/features/platform/state/AuthSession';
|
||||
import {http} from '@app/features/platform/transport/RestTransport';
|
||||
|
||||
export function setupHttp(): void {
|
||||
http.installAuth(() => SessionManager.token);
|
||||
SudoPrompt.init();
|
||||
startApiAttachmentDesktopAuthorization();
|
||||
}
|
||||
@@ -7,6 +7,7 @@ export const Endpoints = {
|
||||
INSTANCE_SETUP_ACCOUNT_IDENTITY: '/instance/setup/account-identity',
|
||||
INSTANCE_SETUP_TOKEN_CHECKS: '/instance/setup/token-checks',
|
||||
ATTACHMENTS_REFRESH_URLS: '/attachments/refresh-urls',
|
||||
USER_ATTACHMENT_TOKEN: '/users/@me/attachment-token',
|
||||
AUTH_LOGIN: '/auth/login',
|
||||
AUTH_LOGIN_MFA_TOTP: '/auth/login/mfa/totp',
|
||||
AUTH_LOGIN_MFA_WEBAUTHN_OPTIONS: '/auth/login/mfa/webauthn/authentication-options',
|
||||
|
||||
@@ -285,6 +285,7 @@ function readFeatures(value: unknown): InstanceFeatures {
|
||||
'email_codes',
|
||||
'date_of_birth_step',
|
||||
'discovery_enabled',
|
||||
'proxy_attachments_through_api',
|
||||
] as const) {
|
||||
const value = readOptionalBoolean(source, key, `features.${key}`);
|
||||
if (value !== undefined) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import {useAnimatedMediaVideoPlayback} from '@app/features/app/hooks/useAnimated
|
||||
import {useShouldAnimate} from '@app/features/app/hooks/useShouldAnimate';
|
||||
import styles from '@app/features/channel/components/AutocompleteEmoji.module.css';
|
||||
import type {FavoriteMeme} from '@app/features/expressions/models/FavoriteMeme';
|
||||
import {useApiAttachmentPlaybackSrc, useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {buildStaticGifPreviewURL} from '@app/features/messaging/utils/MediaProxyUtils';
|
||||
import {MusicNoteIcon} from '@phosphor-icons/react';
|
||||
@@ -13,6 +14,7 @@ import {useRef} from 'react';
|
||||
const AutocompleteMemeVideo = ({src}: {src: string}) => {
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
const playbackAllowed = useAnimatedMediaVideoPlayback(videoRef);
|
||||
const playbackSrc = useApiAttachmentPlaybackSrc(src || undefined);
|
||||
return (
|
||||
<video
|
||||
ref={videoRef}
|
||||
@@ -22,7 +24,7 @@ const AutocompleteMemeVideo = ({src}: {src: string}) => {
|
||||
loop
|
||||
playsInline
|
||||
preload="auto"
|
||||
src={src || undefined}
|
||||
src={playbackSrc}
|
||||
data-flx="channel.autocomplete-meme-preview.meme-video"
|
||||
/>
|
||||
);
|
||||
@@ -34,6 +36,9 @@ export const AutocompleteMemePreview = observer(({meme}: {meme: FavoriteMeme}) =
|
||||
const isAnimatedImageMeme = !isVideoMeme && !isAudioMeme && contentType.includes('gif');
|
||||
const motionAllowed = useShouldAnimate({kind: 'gif', isAnimated: isVideoMeme || isAnimatedImageMeme});
|
||||
const memeUrl = AttachmentUrlRefresher.freshWhenReady(meme.url, {refreshUnsigned: true}) ?? '';
|
||||
const needsStillFrame = (isVideoMeme || isAnimatedImageMeme) && !motionAllowed;
|
||||
const imageSrc = needsStillFrame && memeUrl ? buildStaticGifPreviewURL(memeUrl) : memeUrl;
|
||||
const imageDisplaySrc = useApiAttachmentSrc(imageSrc, !isAudioMeme && !(isVideoMeme && motionAllowed));
|
||||
if (isAudioMeme) {
|
||||
return (
|
||||
<div className={styles.audioIconWrapper} data-flx="channel.autocomplete-meme-preview.audio-icon-wrapper">
|
||||
@@ -48,12 +53,11 @@ export const AutocompleteMemePreview = observer(({meme}: {meme: FavoriteMeme}) =
|
||||
if (isVideoMeme && motionAllowed) {
|
||||
return <AutocompleteMemeVideo src={memeUrl} data-flx="channel.autocomplete-meme-preview.autocomplete-meme-video" />;
|
||||
}
|
||||
const needsStillFrame = (isVideoMeme || isAnimatedImageMeme) && !motionAllowed;
|
||||
return (
|
||||
<img
|
||||
draggable={false}
|
||||
className={styles.memeIcon}
|
||||
src={(needsStillFrame && memeUrl ? buildStaticGifPreviewURL(memeUrl) : memeUrl) || undefined}
|
||||
src={imageDisplaySrc || undefined}
|
||||
alt={meme.name}
|
||||
aria-hidden={true}
|
||||
data-flx="channel.autocomplete-meme-preview.meme-icon"
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
import type {FavoriteMeme} from '@app/features/expressions/models/FavoriteMeme';
|
||||
import FavoriteMemes from '@app/features/expressions/state/FavoriteMemes';
|
||||
import {isKeyboardActivationKey} from '@app/features/input/utils/KeyboardUtils';
|
||||
import {useApiAttachmentPlaybackSrc, useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {useNearViewport} from '@app/features/messaging/hooks/useNearViewport';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {buildStaticGifPreviewURL} from '@app/features/messaging/utils/MediaProxyUtils';
|
||||
@@ -309,6 +310,10 @@ const GridItem = observer(
|
||||
const memeUrl = AttachmentUrlRefresher.freshWhenReady(url, {refreshUnsigned: true});
|
||||
const thumbnailSrc =
|
||||
memeUrl !== null && isGifImage && !shouldAnimateGif ? buildStaticGifPreviewURL(memeUrl) : memeUrl;
|
||||
const videoPlaybackSrc = useApiAttachmentPlaybackSrc(
|
||||
isVisible && !isAudio && isVideo ? (memeUrl ?? undefined) : undefined,
|
||||
);
|
||||
const imageDisplaySrc = useApiAttachmentSrc(thumbnailSrc ?? '', isVisible && !isAudio && !isVideo);
|
||||
const videoPlaybackAllowed = useAnimatedMediaVideoPlayback(videoRef, {
|
||||
enabled: isVisible && !isAudio && isVideo,
|
||||
shouldPlay: shouldAnimateGif,
|
||||
@@ -351,14 +356,14 @@ const GridItem = observer(
|
||||
disablePictureInPicture={true}
|
||||
disableRemotePlayback={true}
|
||||
preload={shouldAnimateGif ? 'auto' : 'metadata'}
|
||||
src={memeUrl ?? undefined}
|
||||
src={videoPlaybackSrc}
|
||||
data-flx="channel.mobile-memes-picker.grid-item.gif"
|
||||
/>
|
||||
)}
|
||||
{isVisible && !isAudio && !isVideo && (
|
||||
<img
|
||||
className={styles.gif}
|
||||
src={thumbnailSrc ?? undefined}
|
||||
src={thumbnailSrc === null ? undefined : imageDisplaySrc}
|
||||
alt={title}
|
||||
data-flx="channel.mobile-memes-picker.grid-item.gif--2"
|
||||
/>
|
||||
|
||||
+2
-1
@@ -171,6 +171,7 @@ export const AttachmentGridItem: FC<AttachmentGridItemProps> = observer(
|
||||
const {ref: visibilityRef, isNearViewport} = useNearViewport<HTMLDivElement>({rememberKey: thumbnailSrc});
|
||||
const shouldLoadMedia = isNearViewport && !shouldBlur && !spoilerHidden;
|
||||
const {
|
||||
src: displaySrc,
|
||||
loaded,
|
||||
error,
|
||||
thumbHashURL,
|
||||
@@ -401,7 +402,7 @@ export const AttachmentGridItem: FC<AttachmentGridItemProps> = observer(
|
||||
</div>
|
||||
)}
|
||||
<img
|
||||
src={shouldLoadMedia ? thumbnailSrc : undefined}
|
||||
src={shouldLoadMedia ? displaySrc : undefined}
|
||||
ref={mediaRef}
|
||||
alt={attachment.filename}
|
||||
loading="eager"
|
||||
|
||||
+11
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {fetchApiAttachmentBlob, fetchedApiAttachmentKey} from '@app/features/messaging/state/ApiAttachmentMedia';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {TEXT_PREVIEW_MAX_BYTES} from '@app/features/messaging/utils/AttachmentPreviewUtils';
|
||||
import {wrapDesktopLocalResourceURL} from '@app/features/messaging/utils/DesktopResourceUrl';
|
||||
@@ -60,12 +61,22 @@ async function readPreviewText(response: Response): Promise<string> {
|
||||
return new TextDecoder().decode(bytes);
|
||||
}
|
||||
|
||||
async function readPreviewBlob(blob: Blob): Promise<string> {
|
||||
if (blob.size > TEXT_PREVIEW_MAX_BYTES) {
|
||||
throw new PreviewSizeLimitError();
|
||||
}
|
||||
return new TextDecoder().decode(await blob.arrayBuffer());
|
||||
}
|
||||
|
||||
function requestFailed(response: Response): Error {
|
||||
return new Error(`Attachment preview request failed: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
export async function fetchTextualPreviewText(url: string, signal: AbortSignal): Promise<string> {
|
||||
const target = await AttachmentUrlRefresher.refresh(url);
|
||||
if (fetchedApiAttachmentKey(target) !== null) {
|
||||
return readPreviewBlob(await fetchApiAttachmentBlob(target, signal));
|
||||
}
|
||||
const response = await fetch(wrapDesktopLocalResourceURL(target), {signal});
|
||||
if (response.ok) {
|
||||
return readPreviewText(response);
|
||||
|
||||
@@ -6,6 +6,7 @@ import type {LinkComponentProps} from '@app/features/channel/components/embeds/c
|
||||
import {EmbedLink} from '@app/features/channel/components/embeds/channel_embed/EmbedLink';
|
||||
import {SafeMarkdown} from '@app/features/messaging/components/markdown';
|
||||
import {MarkdownContext} from '@app/features/messaging/components/markdown/renderers/RendererTypes';
|
||||
import {useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {buildMediaProxyURL} from '@app/features/messaging/utils/MediaProxyUtils';
|
||||
import * as DateUtils from '@app/features/user/utils/DateFormatting';
|
||||
import type {EmbedAuthor, EmbedField, EmbedFooter} from '@fluxer/schema/src/domains/message/EmbedSchemas';
|
||||
@@ -43,7 +44,7 @@ export const EmbedProvider: FC<{provider?: EmbedAuthor} & LinkContext> = observe
|
||||
});
|
||||
export const EmbedAuthorComponent: FC<{author?: EmbedAuthor} & LinkContext> = observer(({author, ...context}) => {
|
||||
const allowIconMotion = useShouldAnimate({kind: 'gif'});
|
||||
const iconSrc = resolveEmbedIconSrc(author?.proxy_icon_url, allowIconMotion);
|
||||
const iconSrc = useApiAttachmentSrc(resolveEmbedIconSrc(author?.proxy_icon_url, allowIconMotion) ?? '');
|
||||
if (!author) return null;
|
||||
return (
|
||||
<div className={styles.embedAuthor} data-flx="channel.embeds.embed.embed-author-component.embed-author">
|
||||
@@ -210,7 +211,7 @@ export const EmbedFooterComponent: FC<{
|
||||
const {i18n} = useLingui();
|
||||
const allowIconMotion = useShouldAnimate({kind: 'gif'});
|
||||
const formattedTimestamp = timestamp ? DateUtils.getRelativeDateString(timestamp, i18n) : undefined;
|
||||
const iconSrc = resolveEmbedIconSrc(footer?.proxy_icon_url, allowIconMotion);
|
||||
const iconSrc = useApiAttachmentSrc(resolveEmbedIconSrc(footer?.proxy_icon_url, allowIconMotion) ?? '');
|
||||
if (!(footer || formattedTimestamp)) return null;
|
||||
return (
|
||||
<div
|
||||
|
||||
@@ -19,6 +19,7 @@ import {safePause, safePlay} from '@app/features/channel/components/GifVideoPool
|
||||
import {useMaybeMessageViewContext} from '@app/features/channel/components/MessageViewContext';
|
||||
import type {Channel} from '@app/features/channel/models/Channel';
|
||||
import {isKeyboardActivationKey} from '@app/features/input/utils/KeyboardUtils';
|
||||
import {useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {useAttachmentRefreshOnError} from '@app/features/messaging/hooks/useAttachmentRefreshOnError';
|
||||
import {useDeleteAttachment} from '@app/features/messaging/hooks/useDeleteAttachment';
|
||||
import {useMatureMedia} from '@app/features/messaging/hooks/useMatureMedia';
|
||||
@@ -27,6 +28,7 @@ import {useMediaLoading} from '@app/features/messaging/hooks/useMediaLoading';
|
||||
import {useMediaViewerHoverWarm} from '@app/features/messaging/hooks/useMediaViewerHoverWarm';
|
||||
import {useOpenInBrowserOnMiddleClick} from '@app/features/messaging/hooks/useOpenInBrowserOnMiddleClick';
|
||||
import type {Message} from '@app/features/messaging/models/MessagingMessage';
|
||||
import apiAttachmentMedia, {fetchedApiAttachmentKey} from '@app/features/messaging/state/ApiAttachmentMedia';
|
||||
import {createDownloadHandler} from '@app/features/messaging/utils/FileDownloadUtils';
|
||||
import * as ImageCacheUtils from '@app/features/messaging/utils/ImageCacheUtils';
|
||||
import {getEmbedMediaDimensions} from '@app/features/messaging/utils/MediaDimensionConfig';
|
||||
@@ -629,7 +631,10 @@ export const EmbedGifv: FC<
|
||||
);
|
||||
}, [dimensions.width, naturalHeight, naturalWidth, posterSource]);
|
||||
const thumbHashURL = useMemo(() => decodeThumbHashDataURL(placeholder), [placeholder]);
|
||||
const thumbHashCurtain = useThumbHashCurtain(posterURL, shouldLoadMedia);
|
||||
const posterDisplayURL = useApiAttachmentSrc(posterURL, shouldLoadMedia);
|
||||
const videoDisplayURL = useApiAttachmentSrc(shouldLoadMedia ? videoProxyURL : '', shouldLoadMedia);
|
||||
const videoPlaybackURL = shouldLoadMedia ? videoDisplayURL : undefined;
|
||||
const thumbHashCurtain = useThumbHashCurtain(posterDisplayURL ?? '', shouldLoadMedia);
|
||||
const aspectRatioStyle = useAspectRatioStyle(dimensions.width, dimensions.height);
|
||||
const defaultName = deriveDefaultNameFromMessage({
|
||||
message,
|
||||
@@ -754,8 +759,8 @@ export const EmbedGifv: FC<
|
||||
<video
|
||||
ref={videoRef}
|
||||
className={styles.videoElement}
|
||||
src={shouldLoadMedia ? videoProxyURL : undefined}
|
||||
poster={shouldLoadMedia ? posterURL : thumbHashURL}
|
||||
src={videoPlaybackURL}
|
||||
poster={shouldLoadMedia ? posterDisplayURL : thumbHashURL}
|
||||
preload={videoConfig?.preload ?? 'none'}
|
||||
loop={videoConfig?.loop ?? true}
|
||||
muted={videoConfig?.muted ?? true}
|
||||
@@ -772,7 +777,7 @@ export const EmbedGifv: FC<
|
||||
) : (
|
||||
<img
|
||||
className={styles.videoElement}
|
||||
src={shouldLoadMedia ? posterURL : thumbHashURL}
|
||||
src={shouldLoadMedia ? posterDisplayURL : thumbHashURL}
|
||||
alt=""
|
||||
width={dimensions.width}
|
||||
height={dimensions.height}
|
||||
@@ -879,6 +884,7 @@ export const EmbedGif: FC<
|
||||
const shouldShowAnimated = shouldAnimate && animationPolicyAllowed;
|
||||
const activeSource = shouldShowAnimated ? optimizedAnimatedURL : optimizedStaticURL;
|
||||
const {
|
||||
src: activeDisplaySrc,
|
||||
loaded,
|
||||
cachedOnMount,
|
||||
thumbHashURL,
|
||||
@@ -900,7 +906,10 @@ export const EmbedGif: FC<
|
||||
if (!shouldLoadMedia) return;
|
||||
if (!animationPolicyAllowed) return;
|
||||
if (activeSource === optimizedAnimatedURL) return;
|
||||
return ImageCacheUtils.pinImage(optimizedAnimatedURL);
|
||||
const fetchedKey = fetchedApiAttachmentKey(optimizedAnimatedURL);
|
||||
return fetchedKey === null
|
||||
? ImageCacheUtils.pinImage(optimizedAnimatedURL)
|
||||
: apiAttachmentMedia.retain(fetchedKey);
|
||||
}, [activeSource, animationPolicyAllowed, optimizedAnimatedURL, shouldLoadMedia]);
|
||||
const defaultName = deriveDefaultNameFromMessage({
|
||||
message,
|
||||
@@ -1010,7 +1019,7 @@ export const EmbedGif: FC<
|
||||
<img
|
||||
ref={mediaRef}
|
||||
alt={i18n._(ANIMATED_GIF_DESCRIPTOR)}
|
||||
src={shouldLoadMedia ? activeSource : undefined}
|
||||
src={shouldLoadMedia ? activeDisplaySrc : undefined}
|
||||
className={styles.videoElement}
|
||||
data-embed-media="gif"
|
||||
loading="eager"
|
||||
|
||||
@@ -337,6 +337,7 @@ export const EmbedImage: FC<EmbedImageProps> = observer(
|
||||
const animatedImageSource =
|
||||
animated && !src.startsWith('blob:') && shouldAnimateImage && shouldLoadMedia && effectiveSrc === src ? src : '';
|
||||
const {
|
||||
src: staticDisplaySrc,
|
||||
loaded,
|
||||
error,
|
||||
thumbHashURL,
|
||||
@@ -345,6 +346,7 @@ export const EmbedImage: FC<EmbedImageProps> = observer(
|
||||
onError: handleImageError,
|
||||
} = useMediaLoading(staticImageSrc, placeholder, {enabled: shouldLoadMedia});
|
||||
const {
|
||||
src: animatedDisplaySrc,
|
||||
loaded: animatedLoaded,
|
||||
error: animatedError,
|
||||
cachedOnMount: animatedCachedOnMount,
|
||||
@@ -551,7 +553,7 @@ export const EmbedImage: FC<EmbedImageProps> = observer(
|
||||
>
|
||||
<img
|
||||
alt={alt || i18n._(IMAGE_DESCRIPTOR)}
|
||||
src={shouldLoadMedia ? staticImageSrc : undefined}
|
||||
src={shouldLoadMedia ? staticDisplaySrc : undefined}
|
||||
ref={mediaRef}
|
||||
width={naturalWidth}
|
||||
height={naturalHeight}
|
||||
@@ -583,11 +585,11 @@ export const EmbedImage: FC<EmbedImageProps> = observer(
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
{animatedImageSource.length > 0 && (
|
||||
{animatedDisplaySrc !== undefined && animatedDisplaySrc.length > 0 && (
|
||||
<motion.img
|
||||
alt=""
|
||||
aria-hidden="true"
|
||||
src={animatedImageSource}
|
||||
src={animatedDisplaySrc}
|
||||
ref={animatedMediaRef}
|
||||
width={naturalWidth}
|
||||
height={naturalHeight}
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
} from '@app/features/channel/components/embeds/media/VideoDimensionUtils';
|
||||
import {useMaybeMessageViewContext} from '@app/features/channel/components/MessageViewContext';
|
||||
import {isKeyboardActivationKey} from '@app/features/input/utils/KeyboardUtils';
|
||||
import {useApiAttachmentPlaybackSrc, useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {useAttachmentRefreshOnError} from '@app/features/messaging/hooks/useAttachmentRefreshOnError';
|
||||
import {useDeleteAttachment} from '@app/features/messaging/hooks/useDeleteAttachment';
|
||||
import {useMatureMedia} from '@app/features/messaging/hooks/useMatureMedia';
|
||||
@@ -225,10 +226,12 @@ const EmbedVideo: FC<EmbedVideoProps> = observer(
|
||||
maxHeight,
|
||||
});
|
||||
const posterRequestWidth = Math.max(1, Math.round(declaredLayout.renderDimensions.width * mediaDevicePixelRatio()));
|
||||
const posterSrc = isBlob ? null : buildMediaProxyURL(src, {format: 'webp', width: posterRequestWidth});
|
||||
const requestedPosterSrc = isBlob ? null : buildMediaProxyURL(src, {format: 'webp', width: posterRequestWidth});
|
||||
const {ref: visibilityRef, isNearViewport} = useNearViewport<HTMLDivElement>({
|
||||
rememberKey: posterSrc ?? effectiveSrc,
|
||||
rememberKey: requestedPosterSrc ?? effectiveSrc,
|
||||
});
|
||||
const posterDisplaySrc = useApiAttachmentSrc(requestedPosterSrc ?? '', isNearViewport);
|
||||
const posterSrc = requestedPosterSrc === null ? null : (posterDisplaySrc ?? null);
|
||||
const [posterCacheAtMount] = useState<PosterCacheAtMount>(() => ({
|
||||
src: posterSrc,
|
||||
cached: posterSrc ? ImageCacheUtils.hasImage(posterSrc) : false,
|
||||
@@ -262,6 +265,9 @@ const EmbedVideo: FC<EmbedVideoProps> = observer(
|
||||
reveal: revealSensitiveMedia,
|
||||
} = useMatureMedia(nsfw, channelId);
|
||||
const shouldLoadMedia = isNearViewport && !shouldBlur;
|
||||
const playbackSrc = useApiAttachmentPlaybackSrc(
|
||||
(metadataProbeArmed && shouldLoadMedia) || isPlayingInline ? effectiveSrc : undefined,
|
||||
);
|
||||
const defaultName =
|
||||
title || deriveDefaultNameFromMessage({message, attachmentId, embedIndex, url: embedUrl || src, proxyUrl: src});
|
||||
const {
|
||||
@@ -574,7 +580,7 @@ const EmbedVideo: FC<EmbedVideoProps> = observer(
|
||||
muted
|
||||
playsInline
|
||||
preload="metadata"
|
||||
src={resolveDesktopCrossOriginMediaURL(effectiveSrc)}
|
||||
src={resolveDesktopCrossOriginMediaURL(playbackSrc)}
|
||||
tabIndex={-1}
|
||||
onLoadedMetadata={handleMetadataProbeLoaded}
|
||||
onError={handleVideoError}
|
||||
@@ -609,7 +615,7 @@ const EmbedVideo: FC<EmbedVideoProps> = observer(
|
||||
<video
|
||||
ref={inlineVideoRef}
|
||||
className={styles.inlineVideo}
|
||||
src={resolveDesktopCrossOriginMediaURL(effectiveSrc)}
|
||||
src={resolveDesktopCrossOriginMediaURL(playbackSrc)}
|
||||
poster={presentedPosterSrc ?? undefined}
|
||||
autoPlay
|
||||
playsInline
|
||||
|
||||
@@ -6,6 +6,7 @@ import styles from '@app/features/channel/components/embeds/media/VoiceMessagePl
|
||||
import {useMaybeMessageViewContext} from '@app/features/channel/components/MessageViewContext';
|
||||
import {PAUSE_DESCRIPTOR, PLAY_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';
|
||||
import {getCachedNumberFormat} from '@app/features/i18n/utils/IntlCache';
|
||||
import {useApiAttachmentPlaybackSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {resolveDesktopCrossOriginMediaURL} from '@app/features/messaging/utils/DesktopResourceUrl';
|
||||
import {buildMediaProxyURL} from '@app/features/messaging/utils/MediaProxyUtils';
|
||||
import {Logger} from '@app/features/platform/utils/AppLogger';
|
||||
@@ -123,6 +124,7 @@ const VoiceMessagePlayer: React.FC<VoiceMessagePlayerProps> = observer(
|
||||
const effectiveSrc = buildMediaProxyURL(src);
|
||||
const [hasStarted, setHasStarted] = useState(false);
|
||||
const [wantsMetadata, setWantsMetadata] = useState(false);
|
||||
const playbackSrc = useApiAttachmentPlaybackSrc(hasStarted || wantsMetadata ? effectiveSrc : undefined);
|
||||
const [prePlayCurrentTime, setPrePlayCurrentTime] = useState(0);
|
||||
const pendingPlayRef = useRef(false);
|
||||
const pendingSeekPercentageRef = useRef<number | null>(null);
|
||||
@@ -180,7 +182,7 @@ const VoiceMessagePlayer: React.FC<VoiceMessagePlayerProps> = observer(
|
||||
}
|
||||
}, [state.isPlaying]);
|
||||
useEffect(() => {
|
||||
if (hasStarted && pendingPlayRef.current) {
|
||||
if (hasStarted && playbackSrc !== undefined && pendingPlayRef.current) {
|
||||
const timer = setTimeout(() => {
|
||||
if (pendingPlayRef.current) {
|
||||
pendingPlayRef.current = false;
|
||||
@@ -190,7 +192,7 @@ const VoiceMessagePlayer: React.FC<VoiceMessagePlayerProps> = observer(
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
return undefined;
|
||||
}, [hasStarted, play]);
|
||||
}, [hasStarted, play, playbackSrc]);
|
||||
useEffect(() => {
|
||||
if (!hasStarted || pendingSeekPercentageRef.current === null) {
|
||||
return;
|
||||
@@ -324,7 +326,7 @@ const VoiceMessagePlayer: React.FC<VoiceMessagePlayerProps> = observer(
|
||||
>
|
||||
<audio
|
||||
ref={mediaRef as React.RefObject<HTMLAudioElement>}
|
||||
src={hasStarted || wantsMetadata ? resolveDesktopCrossOriginMediaURL(effectiveSrc) : undefined}
|
||||
src={resolveDesktopCrossOriginMediaURL(playbackSrc)}
|
||||
preload={wantsMetadata ? 'metadata' : 'none'}
|
||||
data-flx="channel.embeds.media.voice-message-player.audio"
|
||||
>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
|
||||
import {isApiAttachmentUrl, withoutAttachmentToken} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {isAttachmentCdnUrl, stripAttachmentSignature} from '@app/features/messaging/utils/AttachmentCdnUrl';
|
||||
import {inferFormatContentType, PREVIEW_FORMAT_PRIORITY} from '@fluxer/schema/src/domains/gif/GifMediaFormatKeys';
|
||||
|
||||
@@ -110,6 +111,7 @@ export function pickCanonicalPreviewFormat(
|
||||
const STORED_PREVIEW_DEVICE_PIXEL_RATIO = 2;
|
||||
|
||||
export function stripOwnAttachmentSignature(url: string): string {
|
||||
if (isApiAttachmentUrl(url)) return withoutAttachmentToken(url);
|
||||
const mediaEndpoint = RuntimeConfig.getSnapshotOrNull()?.mediaEndpoint;
|
||||
if (mediaEndpoint === undefined) return stripAttachmentSignature(url);
|
||||
return isAttachmentCdnUrl(url, mediaEndpoint) ? stripAttachmentSignature(url) : url;
|
||||
|
||||
@@ -125,6 +125,7 @@ export const GifPickerGridItem = observer(function GifPickerGridItem({
|
||||
videoPool,
|
||||
autoPlay: true,
|
||||
enabled: usesVideoElement,
|
||||
wholeFile: true,
|
||||
});
|
||||
const handleClick = useCallback(
|
||||
(event: React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>) => {
|
||||
|
||||
@@ -16,13 +16,21 @@ interface PickerThumbnailProps {
|
||||
}
|
||||
|
||||
export const PickerThumbnail: FC<PickerThumbnailProps> = observer(({src, alt, className, placeholder}) => {
|
||||
const {loaded, error, thumbHashURL, ref, onLoad, onError} = useMediaLoading(src, placeholder ?? undefined);
|
||||
const {
|
||||
src: displaySrc,
|
||||
loaded,
|
||||
error,
|
||||
thumbHashURL,
|
||||
ref,
|
||||
onLoad,
|
||||
onError,
|
||||
} = useMediaLoading(src, placeholder ?? undefined);
|
||||
const showThumbHash = (!loaded || error) && Boolean(thumbHashURL);
|
||||
return (
|
||||
<>
|
||||
{!error && (
|
||||
<img
|
||||
src={src || undefined}
|
||||
src={displaySrc || undefined}
|
||||
ref={ref}
|
||||
alt={alt}
|
||||
className={className}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {safePause, safePlay} from '@app/features/channel/components/GifVideoPool';
|
||||
import {useApiAttachmentPlaybackSrc, useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {useEffect, useRef} from 'react';
|
||||
|
||||
@@ -22,6 +23,7 @@ export function usePooledVideo({
|
||||
enabled = true,
|
||||
preload,
|
||||
playbackStartTime = null,
|
||||
wholeFile = false,
|
||||
}: {
|
||||
src: string | null | undefined;
|
||||
containerRef: React.RefObject<HTMLDivElement | null>;
|
||||
@@ -30,12 +32,20 @@ export function usePooledVideo({
|
||||
enabled?: boolean;
|
||||
preload?: HTMLVideoElement['preload'];
|
||||
playbackStartTime?: number | null;
|
||||
wholeFile?: boolean;
|
||||
}) {
|
||||
const videoRef = useRef<HTMLVideoElement | null>(null);
|
||||
const effectivePreload = preload ?? (autoPlay ? 'auto' : 'metadata');
|
||||
const requestedSrc = enabled && src ? src : undefined;
|
||||
const streamedSrc = useApiAttachmentPlaybackSrc(wholeFile ? undefined : requestedSrc);
|
||||
const wholeFileSrc = useApiAttachmentSrc(
|
||||
wholeFile ? (requestedSrc ?? '') : '',
|
||||
wholeFile && requestedSrc !== undefined,
|
||||
);
|
||||
const playbackSrc = wholeFile ? requestedSrc && wholeFileSrc : streamedSrc;
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
if (!src) return;
|
||||
if (!src || !playbackSrc) return;
|
||||
const container = containerRef.current;
|
||||
if (!container) return;
|
||||
const video = videoPool.takeElement();
|
||||
@@ -46,7 +56,7 @@ export function usePooledVideo({
|
||||
void AttachmentUrlRefresher.refresh(src, {force: true});
|
||||
};
|
||||
video.addEventListener('error', refreshOnError, {once: true});
|
||||
video.src = src;
|
||||
video.src = playbackSrc;
|
||||
container.appendChild(video);
|
||||
videoPool.registerActive(video);
|
||||
return () => {
|
||||
@@ -57,7 +67,7 @@ export function usePooledVideo({
|
||||
videoRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [src, enabled, containerRef, videoPool, effectivePreload]);
|
||||
}, [src, playbackSrc, enabled, containerRef, videoPool, effectivePreload]);
|
||||
useEffect(() => {
|
||||
const video = videoRef.current;
|
||||
if (!video) return;
|
||||
|
||||
@@ -13,7 +13,9 @@ import {isPinnedPost} from '@app/features/forum/utils/ForumPostList';
|
||||
import {MESSAGE_COUNT_DESCRIPTOR as D_POST_COUNT_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';
|
||||
import {formatNumber, getCachedNumberFormat} from '@app/features/i18n/utils/IntlCache';
|
||||
import * as ReactionCommands from '@app/features/messaging/commands/ReactionCommands';
|
||||
import {useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {useAttachmentRefreshOnError} from '@app/features/messaging/hooks/useAttachmentRefreshOnError';
|
||||
import {useNearViewport} from '@app/features/messaging/hooks/useNearViewport';
|
||||
import {Message} from '@app/features/messaging/models/MessagingMessage';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {buildFitInsideMediaProxyURL, resolvePreferredImageFormat} from '@app/features/messaging/utils/MediaProxyUtils';
|
||||
@@ -173,6 +175,10 @@ export const ForumPostCard = observer(({forum, post, grid}: ForumPostCardProps)
|
||||
})
|
||||
: null;
|
||||
const handleThumbnailError = useAttachmentRefreshOnError(media?.src);
|
||||
const {ref: thumbnailRef, isNearViewport: thumbnailNearViewport} = useNearViewport<HTMLImageElement>({
|
||||
rememberKey: thumbnail,
|
||||
});
|
||||
const thumbnailDisplaySrc = useApiAttachmentSrc(thumbnail ?? '', thumbnailNearViewport);
|
||||
return (
|
||||
<>
|
||||
<FocusRing offset={-2} data-flx="forum.forum-post-card.focus-ring">
|
||||
@@ -195,7 +201,8 @@ export const ForumPostCard = observer(({forum, post, grid}: ForumPostCardProps)
|
||||
<div className={styles.cardMedia} data-flx="forum.forum-post-card.card-media">
|
||||
{thumbnail ? (
|
||||
<img
|
||||
src={thumbnail}
|
||||
ref={thumbnailRef}
|
||||
src={thumbnailDisplaySrc}
|
||||
alt=""
|
||||
className={styles.cardMediaImage}
|
||||
loading="lazy"
|
||||
@@ -260,7 +267,8 @@ export const ForumPostCard = observer(({forum, post, grid}: ForumPostCardProps)
|
||||
{!grid && thumbnail && (
|
||||
<div className={styles.cardThumb} data-flx="forum.forum-post-card.card-thumb">
|
||||
<img
|
||||
src={thumbnail}
|
||||
ref={thumbnailRef}
|
||||
src={thumbnailDisplaySrc}
|
||||
alt=""
|
||||
className={styles.cardMediaImage}
|
||||
loading="lazy"
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
type ForwardMessagePreviewThumbnail,
|
||||
resolveForwardMessagePreviewContent,
|
||||
} from '@app/features/messaging/components/modals/ForwardMessagePreviewContent';
|
||||
import {useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import type {Message} from '@app/features/messaging/models/MessagingMessage';
|
||||
import Permission from '@app/features/permissions/state/Permission';
|
||||
import {remFromPx} from '@app/features/theme/layout/RemFromPx';
|
||||
@@ -137,6 +138,10 @@ function ForwardMessagePreviewAttachmentRow({summary, hasText}: ForwardMessagePr
|
||||
function ForwardMessagePreviewThumbnailFrame({thumbnail, overflowCount}: ForwardMessagePreviewThumbnailFrameProps) {
|
||||
const {i18n} = useLingui();
|
||||
const hasOverflow = overflowCount > 0;
|
||||
const imageSrc = useApiAttachmentSrc(
|
||||
thumbnail.showsFilePlaceholder ? '' : buildForwardMessagePreviewThumbnailURL(thumbnail),
|
||||
!thumbnail.showsFilePlaceholder,
|
||||
);
|
||||
return (
|
||||
<div
|
||||
className={styles.thumbnailStack}
|
||||
@@ -163,7 +168,7 @@ function ForwardMessagePreviewThumbnailFrame({thumbnail, overflowCount}: Forward
|
||||
) : (
|
||||
<img
|
||||
className={styles.thumbnailImage}
|
||||
src={buildForwardMessagePreviewThumbnailURL(thumbnail)}
|
||||
src={imageSrc}
|
||||
alt=""
|
||||
data-flx="messaging.forward-message-preview.forward-message-preview-thumbnail-frame.thumbnail-image"
|
||||
/>
|
||||
|
||||
@@ -32,6 +32,8 @@ import {
|
||||
VIDEO_PREVIEW_DESCRIPTOR,
|
||||
type ZoomState,
|
||||
} from '@app/features/messaging/components/modals/media_modal/shared';
|
||||
import {useApiAttachmentPlaybackSrc, useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {useNearViewport} from '@app/features/messaging/hooks/useNearViewport';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {remFromPx} from '@app/features/theme/layout/RemFromPx';
|
||||
import * as MediaViewerCommands from '@app/features/ui/commands/MediaViewerCommands';
|
||||
@@ -85,6 +87,38 @@ const FITTED_PAN_ZOOM_INFO: PanZoomInfo = {
|
||||
canZoomOut: false,
|
||||
};
|
||||
|
||||
const ThumbnailVideo: FC<{src: string; label: string}> = ({src, label}) => {
|
||||
const playbackSrc = useApiAttachmentPlaybackSrc(src);
|
||||
return (
|
||||
<video
|
||||
className={styles.thumbnailVideo}
|
||||
src={playbackSrc}
|
||||
muted
|
||||
playsInline
|
||||
preload="metadata"
|
||||
aria-label={label}
|
||||
onError={() => void AttachmentUrlRefresher.refresh(src, {force: true})}
|
||||
data-flx="messaging.media-modal.thumbnail-video"
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
const ThumbnailImage: FC<{src: string; alt: string}> = ({src, alt}) => {
|
||||
const {ref, isNearViewport} = useNearViewport<HTMLImageElement>({rememberKey: src});
|
||||
const displaySrc = useApiAttachmentSrc(src, isNearViewport);
|
||||
return (
|
||||
<img
|
||||
ref={ref}
|
||||
src={displaySrc}
|
||||
alt={alt}
|
||||
className={styles.thumbnailImage}
|
||||
draggable={false}
|
||||
onError={() => void AttachmentUrlRefresher.refresh(src, {force: true})}
|
||||
data-flx="messaging.media-modal.thumbnail-image"
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export const MediaModal: FC<MediaModalProps> = observer(
|
||||
({
|
||||
title,
|
||||
@@ -617,16 +651,7 @@ export const MediaModal: FC<MediaModalProps> = observer(
|
||||
let thumbnailContent: ReactNode;
|
||||
if (thumb.type === 'video' || thumb.type === 'gifv') {
|
||||
thumbnailContent = (
|
||||
<video
|
||||
className={styles.thumbnailVideo}
|
||||
src={thumb.src}
|
||||
muted
|
||||
playsInline
|
||||
preload="metadata"
|
||||
aria-label={thumb.alt ?? i18n._(VIDEO_PREVIEW_DESCRIPTOR)}
|
||||
onError={() => void AttachmentUrlRefresher.refresh(thumb.src, {force: true})}
|
||||
data-flx="messaging.media-modal.thumbnail-video"
|
||||
/>
|
||||
<ThumbnailVideo src={thumb.src} label={thumb.alt ?? i18n._(VIDEO_PREVIEW_DESCRIPTOR)} />
|
||||
);
|
||||
} else if (thumb.type === 'audio') {
|
||||
thumbnailContent = (
|
||||
@@ -638,16 +663,7 @@ export const MediaModal: FC<MediaModalProps> = observer(
|
||||
</div>
|
||||
);
|
||||
} else {
|
||||
thumbnailContent = (
|
||||
<img
|
||||
src={thumb.src}
|
||||
alt={thumb.alt ?? ''}
|
||||
className={styles.thumbnailImage}
|
||||
draggable={false}
|
||||
onError={() => void AttachmentUrlRefresher.refresh(thumb.src, {force: true})}
|
||||
data-flx="messaging.media-modal.thumbnail-image"
|
||||
/>
|
||||
);
|
||||
thumbnailContent = <ThumbnailImage src={thumb.src} alt={thumb.alt ?? ''} />;
|
||||
}
|
||||
return (
|
||||
<FocusRing
|
||||
|
||||
@@ -16,10 +16,12 @@ import type {ForwardModalSuccess} from '@app/features/messaging/components/modal
|
||||
import {MediaModal} from '@app/features/messaging/components/modals/MediaModal';
|
||||
import styles from '@app/features/messaging/components/modals/MediaViewerModal.module.css';
|
||||
import {getMediaViewerPortalRoot} from '@app/features/messaging/components/modals/MediaViewerPortal';
|
||||
import {useApiAttachmentSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {useAttachmentRefreshOnError} from '@app/features/messaging/hooks/useAttachmentRefreshOnError';
|
||||
import {useDeleteAttachment} from '@app/features/messaging/hooks/useDeleteAttachment';
|
||||
import {useMediaFavorite} from '@app/features/messaging/hooks/useMediaFavorite';
|
||||
import type {Message} from '@app/features/messaging/models/MessagingMessage';
|
||||
import apiAttachmentMedia, {fetchedApiAttachmentKey} from '@app/features/messaging/state/ApiAttachmentMedia';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {attachmentUrlMayNeedRefresh} from '@app/features/messaging/utils/AttachmentCdnUrl';
|
||||
import {formatAttachmentDate} from '@app/features/messaging/utils/AttachmentExpiryUtils';
|
||||
@@ -141,9 +143,11 @@ const RenderReadyImage: FC<RenderReadyImageProps> = ({
|
||||
className,
|
||||
onError,
|
||||
onLoad,
|
||||
src,
|
||||
src: requestedSrc,
|
||||
...imageProps
|
||||
}: RenderReadyImageProps) => {
|
||||
const displaySrc = useApiAttachmentSrc(requestedSrc ?? '');
|
||||
const src = requestedSrc === undefined ? undefined : displaySrc;
|
||||
const [isReady, setIsReady] = useState(false);
|
||||
const [renderedSource, setRenderedSource] = useState(src);
|
||||
const [decodedBeforeSwap, setDecodedBeforeSwap] = useState(() => ImageCacheUtils.hasImage(src));
|
||||
@@ -323,7 +327,10 @@ const MediaViewerModalComponent: FC = observer(() => {
|
||||
const adjacentPinUrls = resolveAdjacentPinUrls(freshItems, currentIndex, isOpen);
|
||||
useEffect(() => {
|
||||
if (adjacentPinUrls.length === 0) return;
|
||||
const unpinCallbacks = adjacentPinUrls.split('\n').map((url) => ImageCacheUtils.pinImage(url));
|
||||
const unpinCallbacks = adjacentPinUrls.split('\n').map((url) => {
|
||||
const fetchedKey = fetchedApiAttachmentKey(url);
|
||||
return fetchedKey === null ? ImageCacheUtils.pinImage(url) : apiAttachmentMedia.retain(fetchedKey);
|
||||
});
|
||||
return () => {
|
||||
for (const unpin of unpinCallbacks) unpin();
|
||||
};
|
||||
@@ -507,6 +514,9 @@ const MediaViewerModalComponent: FC = observer(() => {
|
||||
const imageSrc = useMemo(() => (currentItem ? buildViewerMediaURL(currentItem) : ''), [currentItem]);
|
||||
const handleImageError = useAttachmentRefreshOnError(imageSrc);
|
||||
const handleGifvVideoError = useAttachmentRefreshOnError(currentItem?.src);
|
||||
const gifvVideoSrc = currentItem?.type === 'gifv' && !currentGifvIsActualGif ? currentItem.src : undefined;
|
||||
const gifvDisplaySrc = useApiAttachmentSrc(gifvVideoSrc ?? '', gifvVideoSrc !== undefined);
|
||||
const gifvPlaybackSrc = gifvVideoSrc === undefined ? undefined : gifvDisplaySrc;
|
||||
const freshItemSources = freshItems.map((item) => item.src).join('\n');
|
||||
const thumbnails = useMemo(() => {
|
||||
const snappedThumbnailSize = snapMediaProxyImageSize(MEDIA_VIEWER_THUMBNAIL_SIZE);
|
||||
@@ -587,7 +597,7 @@ const MediaViewerModalComponent: FC = observer(() => {
|
||||
return (
|
||||
<video
|
||||
key={currentItem.src}
|
||||
src={currentItem.src}
|
||||
src={gifvPlaybackSrc}
|
||||
className={styles.gifvVideo}
|
||||
style={{
|
||||
objectFit: 'contain',
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import apiAttachmentFileTokens, {isFreshFileToken} from '@app/features/messaging/state/ApiAttachmentFileTokens';
|
||||
import apiAttachmentMedia, {fetchedApiAttachmentKey} from '@app/features/messaging/state/ApiAttachmentMedia';
|
||||
import {withAttachmentToken} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {useEffect, useRef, useState, useSyncExternalStore} from 'react';
|
||||
|
||||
interface ApiAttachmentSource {
|
||||
src: string | undefined;
|
||||
failed: boolean;
|
||||
}
|
||||
|
||||
interface AttachedPlaybackSource {
|
||||
target: string;
|
||||
epoch: number;
|
||||
url: string;
|
||||
}
|
||||
|
||||
function useFetchedApiAttachmentKey(src: string | undefined): string | null {
|
||||
useSyncExternalStore(apiAttachmentMedia.subscribe, apiAttachmentMedia.isHeaderMode);
|
||||
return src === undefined || src.length === 0 ? null : fetchedApiAttachmentKey(src);
|
||||
}
|
||||
|
||||
export function useApiAttachmentSource(src: string, enabled = true): ApiAttachmentSource {
|
||||
const key = useFetchedApiAttachmentKey(src);
|
||||
const target = enabled ? key : null;
|
||||
const generation = useSyncExternalStore(apiAttachmentMedia.subscribe, apiAttachmentMedia.currentGeneration);
|
||||
useEffect(() => (target === null ? undefined : apiAttachmentMedia.retain(target)), [target, generation]);
|
||||
const state = useSyncExternalStore(apiAttachmentMedia.subscribe, () =>
|
||||
target === null ? null : apiAttachmentMedia.read(target),
|
||||
);
|
||||
if (key === null) return {src, failed: false};
|
||||
if (state === null) return {src: undefined, failed: false};
|
||||
if (state.status === 'ready') return {src: state.objectUrl, failed: false};
|
||||
return {src: undefined, failed: state.status === 'failed'};
|
||||
}
|
||||
|
||||
export function useApiAttachmentSrc(src: string, enabled = true): string | undefined {
|
||||
return useApiAttachmentSource(src, enabled).src;
|
||||
}
|
||||
|
||||
export function useApiAttachmentPlaybackSrc(src: string | undefined): string | undefined {
|
||||
const target = useFetchedApiAttachmentKey(src);
|
||||
const issued = useSyncExternalStore(apiAttachmentFileTokens.subscribe, () =>
|
||||
target === null ? null : apiAttachmentFileTokens.read(target),
|
||||
);
|
||||
const epoch = useSyncExternalStore(apiAttachmentFileTokens.subscribe, () =>
|
||||
target === null ? 0 : apiAttachmentFileTokens.epochOf(target),
|
||||
);
|
||||
const attachedRef = useRef<AttachedPlaybackSource | null>(null);
|
||||
const [retryRound, setRetryRound] = useState(0);
|
||||
const attached = attachedRef.current;
|
||||
const attachedUrl = attached !== null && attached.target === target && attached.epoch === epoch ? attached.url : null;
|
||||
const usableToken = issued !== null && issued !== 'failed' && isFreshFileToken(issued) ? issued : null;
|
||||
const needsToken = target !== null && attachedUrl === null && usableToken === null;
|
||||
useEffect(() => {
|
||||
if (!needsToken || target === null) return;
|
||||
apiAttachmentFileTokens.ensure(target);
|
||||
if (issued !== 'failed') return;
|
||||
const timer = setTimeout(() => setRetryRound((round) => round + 1), apiAttachmentFileTokens.retryInMs(target));
|
||||
return () => clearTimeout(timer);
|
||||
}, [needsToken, target, issued, retryRound]);
|
||||
if (target === null) return src;
|
||||
if (attachedUrl !== null) return attachedUrl;
|
||||
if (usableToken === null) return undefined;
|
||||
const url = withAttachmentToken(target, usableToken.token);
|
||||
attachedRef.current = {target, epoch, url};
|
||||
return url;
|
||||
}
|
||||
@@ -1,14 +1,25 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {useCallback, useRef} from 'react';
|
||||
import {isApiAttachmentUrl} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {unwrapDesktopLocalResourceURL} from '@app/features/messaging/utils/DesktopResourceUrl';
|
||||
import {type SyntheticEvent, useCallback, useRef} from 'react';
|
||||
|
||||
export function useAttachmentRefreshOnError(src: string | undefined): () => void {
|
||||
type MediaErrorEvent = SyntheticEvent<HTMLImageElement | HTMLVideoElement>;
|
||||
|
||||
export function useAttachmentRefreshOnError(src: string | undefined): (event?: MediaErrorEvent) => void {
|
||||
const refreshedSourceRef = useRef<string | null>(null);
|
||||
return useCallback(() => {
|
||||
if (src === undefined || src.length === 0) return;
|
||||
if (refreshedSourceRef.current === src) return;
|
||||
refreshedSourceRef.current = src;
|
||||
void AttachmentUrlRefresher.refresh(src, {force: true});
|
||||
}, [src]);
|
||||
return useCallback(
|
||||
(event?: MediaErrorEvent) => {
|
||||
if (src === undefined || src.length === 0) return;
|
||||
const failedSource =
|
||||
event !== undefined && isApiAttachmentUrl(unwrapDesktopLocalResourceURL(src))
|
||||
? event.currentTarget.currentSrc || src
|
||||
: src;
|
||||
if (refreshedSourceRef.current === failedSource) return;
|
||||
refreshedSourceRef.current = failedSource;
|
||||
void AttachmentUrlRefresher.refresh(src, {force: true});
|
||||
},
|
||||
[src],
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {forgetRestoredImageSource, wasShownBeforeRestart} from '@app/features/boot_state/BootViewport';
|
||||
import {useApiAttachmentSource} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import AttachmentUrlRefresher, {sameAttachmentSource} from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import * as ImageCacheUtils from '@app/features/messaging/utils/ImageCacheUtils';
|
||||
import {decodeThumbHashDataURL} from '@app/features/messaging/utils/ThumbHashUtils';
|
||||
@@ -10,6 +11,7 @@ type MediaLoadingElement = HTMLImageElement | HTMLVideoElement;
|
||||
type MediaElementStatus = 'loaded' | 'error' | 'pending';
|
||||
|
||||
export interface MediaLoadingState {
|
||||
src: string | undefined;
|
||||
loaded: boolean;
|
||||
error: boolean;
|
||||
cached: boolean;
|
||||
@@ -122,11 +124,13 @@ function pendingMediaLoadingState(src: string, loadEnabled: boolean): MediaLoadi
|
||||
}
|
||||
|
||||
export function useMediaLoading(
|
||||
src: string,
|
||||
requestedSrc: string,
|
||||
placeholder?: string,
|
||||
options: UseMediaLoadingOptions = {},
|
||||
): MediaLoadingState {
|
||||
const {enabled = true} = options;
|
||||
const {src: displaySrc, failed: fetchFailed} = useApiAttachmentSource(requestedSrc, enabled);
|
||||
const src = displaySrc ?? '';
|
||||
const mediaElementRef = useRef<MediaLoadingElement | null>(null);
|
||||
const refreshedSourceRef = useRef<string | null>(null);
|
||||
const loadEnabled = enabled && src.length > 0;
|
||||
@@ -198,7 +202,7 @@ export function useMediaLoading(
|
||||
forgetRestoredImageSource(src);
|
||||
if (refreshedSourceRef.current !== src) {
|
||||
refreshedSourceRef.current = src;
|
||||
void AttachmentUrlRefresher.refresh(src, {force: true});
|
||||
void AttachmentUrlRefresher.refresh(requestedSrc, {force: true});
|
||||
}
|
||||
setSourceState((currentState) => {
|
||||
if (currentState.src === src && currentState.loadEnabled === loadEnabled && currentState.loaded) {
|
||||
@@ -207,11 +211,12 @@ export function useMediaLoading(
|
||||
return {src, loadEnabled, cached: false, loaded: false, error: true};
|
||||
});
|
||||
},
|
||||
[loadEnabled, sourceIdentity, src],
|
||||
[loadEnabled, requestedSrc, sourceIdentity, src],
|
||||
);
|
||||
return {
|
||||
src: displaySrc,
|
||||
loaded: currentSourceState.loaded,
|
||||
error: currentSourceState.error,
|
||||
error: currentSourceState.error || fetchFailed,
|
||||
cached: currentSourceState.cached,
|
||||
cachedOnMount,
|
||||
thumbHashURL,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {useSaveData} from '@app/features/app/hooks/useSaveData';
|
||||
import apiAttachmentMedia, {fetchedApiAttachmentKey} from '@app/features/messaging/state/ApiAttachmentMedia';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import * as ImageCacheUtils from '@app/features/messaging/utils/ImageCacheUtils';
|
||||
import {buildViewerMediaURL, isViewerImageItem} from '@app/features/messaging/utils/MediaViewerItemUtils';
|
||||
@@ -69,6 +70,11 @@ export function useMediaViewerHoverWarm(
|
||||
if (url == null) {
|
||||
return;
|
||||
}
|
||||
const fetchedKey = fetchedApiAttachmentKey(url);
|
||||
if (fetchedKey !== null) {
|
||||
apiAttachmentMedia.warm(fetchedKey);
|
||||
return;
|
||||
}
|
||||
ImageCacheUtils.warmImage(url);
|
||||
}, [allowAnimated, cancelViewerWarm, item, saveData]);
|
||||
const scheduleViewerWarm = useCallback(() => {
|
||||
|
||||
@@ -6,6 +6,7 @@ import type {GuildSticker} from '@app/features/expressions/models/GuildSticker';
|
||||
import * as GifSlugUtils from '@app/features/expressions/utils/GifSlugUtils';
|
||||
import {UploadingAttachment} from '@app/features/messaging/models/UploadingAttachment';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {isApiAttachmentUrl} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {attachmentUrlMayNeedRefresh} from '@app/features/messaging/utils/AttachmentCdnUrl';
|
||||
import {TextareaSegmentManager} from '@app/features/messaging/utils/TextareaSegmentManager';
|
||||
import {ComponentBus} from '@app/features/platform/utils/ComponentBus';
|
||||
@@ -101,27 +102,25 @@ export const useTextareaExpressionHandlers = ({
|
||||
};
|
||||
const providerShareUrl =
|
||||
meme.gifProvider && meme.gifSlug ? GifSlugUtils.buildShareUrl(meme.gifProvider, meme.gifSlug) : null;
|
||||
if (autoSend) {
|
||||
if (providerShareUrl) {
|
||||
if (providerShareUrl) {
|
||||
if (autoSend) {
|
||||
sendOptimisticMessage({content: providerShareUrl}, {hasAttachments: false});
|
||||
} else {
|
||||
const uploadingAttachment = UploadingAttachment.fromDescriptor({
|
||||
filename: meme.filename,
|
||||
title: meme.name,
|
||||
size: meme.size,
|
||||
contentType: meme.contentType,
|
||||
}).toJSON();
|
||||
sendOptimisticMessage(
|
||||
{content: '', attachments: [uploadingAttachment]},
|
||||
{hasAttachments: false, favoriteMemeId: meme.id},
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (providerShareUrl) {
|
||||
appendText(providerShareUrl);
|
||||
} else {
|
||||
void insertMemeUrl();
|
||||
}
|
||||
} else if (autoSend || isApiAttachmentUrl(meme.url)) {
|
||||
const uploadingAttachment = UploadingAttachment.fromDescriptor({
|
||||
filename: meme.filename,
|
||||
title: meme.name,
|
||||
size: meme.size,
|
||||
contentType: meme.contentType,
|
||||
}).toJSON();
|
||||
sendOptimisticMessage(
|
||||
{content: '', attachments: [uploadingAttachment]},
|
||||
{hasAttachments: false, favoriteMemeId: meme.id},
|
||||
);
|
||||
} else {
|
||||
void insertMemeUrl();
|
||||
}
|
||||
};
|
||||
return ComponentBus.subscribe('FAVORITE_MEME_SELECT', handleFavoriteMemeSelect);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
|
||||
import apiAttachmentMedia from '@app/features/messaging/state/ApiAttachmentMedia';
|
||||
import {isApiProxiedAttachmentsEnabled} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import SessionManager from '@app/features/platform/state/AuthSession';
|
||||
import {Logger} from '@app/features/platform/utils/AppLogger';
|
||||
import {getElectronAPI} from '@app/features/ui/utils/NativeUtils';
|
||||
import {reaction} from 'mobx';
|
||||
|
||||
const logger = new Logger('ApiAttachmentDesktopAuthorization');
|
||||
|
||||
interface DesktopGrant {
|
||||
apiEndpoint: string | null;
|
||||
token: string | null;
|
||||
}
|
||||
|
||||
let generation = 0;
|
||||
let grantedEndpoint: string | null = null;
|
||||
|
||||
type SetAuthorization = (apiEndpoint: string, token: string | null) => Promise<boolean>;
|
||||
|
||||
async function pushGrant(setAuthorization: SetAuthorization, {apiEndpoint, token}: DesktopGrant): Promise<void> {
|
||||
generation += 1;
|
||||
const current = generation;
|
||||
const endpoint = apiEndpoint ?? grantedEndpoint;
|
||||
const nextToken = apiEndpoint === null ? null : token;
|
||||
if (nextToken === null) apiAttachmentMedia.setHeaderMode(false);
|
||||
if (endpoint === null) return;
|
||||
grantedEndpoint = nextToken === null ? null : endpoint;
|
||||
let granted = false;
|
||||
try {
|
||||
granted = (await setAuthorization(endpoint, nextToken)) === true;
|
||||
} catch (error) {
|
||||
logger.warn('Could not hand the session to the desktop app for attachments', error);
|
||||
}
|
||||
if (current === generation) apiAttachmentMedia.setHeaderMode(granted && nextToken !== null);
|
||||
}
|
||||
|
||||
export function startApiAttachmentDesktopAuthorization(): void {
|
||||
const setAuthorization = getElectronAPI()?.setApiAttachmentAuthorization;
|
||||
if (setAuthorization === undefined) return;
|
||||
reaction(
|
||||
(): DesktopGrant => {
|
||||
const enabled = isApiProxiedAttachmentsEnabled();
|
||||
return {
|
||||
apiEndpoint: enabled ? (RuntimeConfig.getSnapshotOrNull()?.apiEndpoint ?? null) : null,
|
||||
token: enabled ? SessionManager.token : null,
|
||||
};
|
||||
},
|
||||
(grant) => void pushGrant(setAuthorization, grant),
|
||||
{fireImmediately: true, equals: (a, b) => a.apiEndpoint === b.apiEndpoint && a.token === b.token},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {Endpoints} from '@app/features/app/constants/Endpoints';
|
||||
import {ResettableStates} from '@app/features/app/state/ResettableStates';
|
||||
import {
|
||||
apiAttachmentFileKey,
|
||||
readAttachmentToken,
|
||||
withAttachmentToken,
|
||||
withoutAttachmentToken,
|
||||
} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {http} from '@app/features/platform/transport/RestTransport';
|
||||
import {Logger} from '@app/features/platform/utils/AppLogger';
|
||||
import type {AttachmentAccessTokenResponse} from '@fluxer/schema/src/domains/message/AttachmentSchemas';
|
||||
|
||||
const logger = new Logger('ApiAttachmentFileTokens');
|
||||
|
||||
const RENEW_MARGIN_MS = 30_000;
|
||||
const RETRY_DELAY_MS = 10_000;
|
||||
const MAX_ISSUED_TOKENS = 64;
|
||||
const TOKEN_REQUEST_RETRIES = 2;
|
||||
|
||||
interface IssuedFileToken {
|
||||
readonly token: string;
|
||||
readonly expiresAtMs: number;
|
||||
}
|
||||
|
||||
type FileTokenState = IssuedFileToken | 'failed' | null;
|
||||
|
||||
function readIssuedToken(body: AttachmentAccessTokenResponse | undefined): IssuedFileToken | null {
|
||||
if (!body?.token) return null;
|
||||
const expiresAtMs = Date.parse(body.expires_at);
|
||||
return Number.isFinite(expiresAtMs) ? {token: body.token, expiresAtMs} : null;
|
||||
}
|
||||
|
||||
export function isFreshFileToken(issued: IssuedFileToken, nowMs = Date.now()): boolean {
|
||||
return issued.expiresAtMs - RENEW_MARGIN_MS > nowMs;
|
||||
}
|
||||
|
||||
class ApiAttachmentFileTokens {
|
||||
private issued = new Map<string, IssuedFileToken>();
|
||||
private readonly failedUntilMs = new Map<string, number>();
|
||||
private readonly pending = new Map<string, Promise<string | null>>();
|
||||
private readonly epochs = new Map<string, number>();
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private generation = 0;
|
||||
|
||||
subscribe = (listener: () => void): (() => void) => {
|
||||
this.listeners.add(listener);
|
||||
return () => {
|
||||
this.listeners.delete(listener);
|
||||
};
|
||||
};
|
||||
|
||||
read(url: string): FileTokenState {
|
||||
const key = apiAttachmentFileKey(url);
|
||||
if (key === null) return null;
|
||||
return this.issued.get(key) ?? (this.failedUntilMs.has(key) ? 'failed' : null);
|
||||
}
|
||||
|
||||
epochOf(url: string): number {
|
||||
const key = apiAttachmentFileKey(url);
|
||||
return key === null ? 0 : (this.epochs.get(key) ?? 0);
|
||||
}
|
||||
|
||||
retryInMs(url: string): number {
|
||||
const key = apiAttachmentFileKey(url);
|
||||
return key === null ? 0 : Math.max(0, (this.failedUntilMs.get(key) ?? 0) - Date.now());
|
||||
}
|
||||
|
||||
ensure(url: string): void {
|
||||
const key = apiAttachmentFileKey(url);
|
||||
if (key === null) return;
|
||||
const issued = this.issued.get(key);
|
||||
if (issued !== undefined && isFreshFileToken(issued)) return;
|
||||
if (Date.now() < (this.failedUntilMs.get(key) ?? 0)) return;
|
||||
void this.request(key);
|
||||
}
|
||||
|
||||
async linkUrl(url: string): Promise<string> {
|
||||
const key = apiAttachmentFileKey(url);
|
||||
if (key === null) return url;
|
||||
const bare = withoutAttachmentToken(url);
|
||||
const issued = this.issued.get(key);
|
||||
const token = issued !== undefined && isFreshFileToken(issued) ? issued.token : await this.request(key);
|
||||
return token === null ? bare : withAttachmentToken(bare, token);
|
||||
}
|
||||
|
||||
renew(url: string): boolean {
|
||||
const key = apiAttachmentFileKey(url);
|
||||
const token = readAttachmentToken(url);
|
||||
if (key === null || token === null) return false;
|
||||
const issued = this.issued.get(key);
|
||||
if (issued !== undefined && isFreshFileToken(issued) && issued.token === token) return false;
|
||||
this.invalidate(key);
|
||||
void this.request(key);
|
||||
return true;
|
||||
}
|
||||
|
||||
invalidate(url: string): void {
|
||||
const key = apiAttachmentFileKey(url);
|
||||
if (key === null) return;
|
||||
this.failedUntilMs.delete(key);
|
||||
this.issued.delete(key);
|
||||
this.epochs.set(key, (this.epochs.get(key) ?? 0) + 1);
|
||||
this.notify();
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this.generation += 1;
|
||||
this.issued = new Map();
|
||||
this.failedUntilMs.clear();
|
||||
this.pending.clear();
|
||||
this.epochs.clear();
|
||||
this.notify();
|
||||
}
|
||||
|
||||
private request(key: string): Promise<string | null> {
|
||||
const existing = this.pending.get(key);
|
||||
if (existing) return existing;
|
||||
const generation = this.generation;
|
||||
const pending = http
|
||||
.post<AttachmentAccessTokenResponse>(Endpoints.USER_ATTACHMENT_TOKEN, {
|
||||
body: {url: key},
|
||||
mode: 'auto-retry',
|
||||
retries: TOKEN_REQUEST_RETRIES,
|
||||
})
|
||||
.then((response) => {
|
||||
const issued = readIssuedToken(response.body);
|
||||
if (issued === null) throw new Error('The attachment token response was empty');
|
||||
if (generation !== this.generation) return null;
|
||||
this.failedUntilMs.delete(key);
|
||||
this.remember(key, issued);
|
||||
return issued.token;
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
if (generation !== this.generation) return null;
|
||||
logger.warn('Could not get a token for an attachment', error);
|
||||
this.failedUntilMs.set(key, Date.now() + RETRY_DELAY_MS);
|
||||
this.notify();
|
||||
return null;
|
||||
})
|
||||
.finally(() => {
|
||||
if (this.pending.get(key) === pending) this.pending.delete(key);
|
||||
});
|
||||
this.pending.set(key, pending);
|
||||
return pending;
|
||||
}
|
||||
|
||||
private remember(key: string, issued: IssuedFileToken): void {
|
||||
this.issued.delete(key);
|
||||
this.issued.set(key, issued);
|
||||
for (const key of this.issued.keys()) {
|
||||
if (this.issued.size <= MAX_ISSUED_TOKENS) break;
|
||||
this.issued.delete(key);
|
||||
}
|
||||
this.notify();
|
||||
}
|
||||
|
||||
private notify(): void {
|
||||
for (const listener of this.listeners) listener();
|
||||
}
|
||||
}
|
||||
|
||||
const apiAttachmentFileTokens = new ApiAttachmentFileTokens();
|
||||
ResettableStates.register(apiAttachmentFileTokens, () => apiAttachmentFileTokens.reset());
|
||||
|
||||
export default apiAttachmentFileTokens;
|
||||
@@ -0,0 +1,274 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {ResettableStates} from '@app/features/app/state/ResettableStates';
|
||||
import {
|
||||
apiAttachmentRequestPath,
|
||||
isApiAttachmentUrl,
|
||||
withoutAttachmentToken,
|
||||
} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {unwrapDesktopLocalResourceURL} from '@app/features/messaging/utils/DesktopLocalResourceTarget';
|
||||
import {http} from '@app/features/platform/transport/RestTransport';
|
||||
import {HttpError} from '@app/features/platform/types/EndpointError';
|
||||
import {Logger} from '@app/features/platform/utils/AppLogger';
|
||||
|
||||
const logger = new Logger('ApiAttachmentMedia');
|
||||
|
||||
const MAX_CONCURRENT_LOADS = 6;
|
||||
const MAX_CACHED_BYTES = 192 * 1024 * 1024;
|
||||
const MAX_CACHED_ENTRIES = 400;
|
||||
const LOAD_RETRIES = 2;
|
||||
const FAILURE_BACKOFF_MS = 5_000;
|
||||
const MAX_FAILURE_BACKOFF_MS = 60_000;
|
||||
|
||||
type ApiAttachmentMediaState = {status: 'loading'} | {status: 'ready'; objectUrl: string} | {status: 'failed'};
|
||||
|
||||
const LOADING: ApiAttachmentMediaState = Object.freeze({status: 'loading'});
|
||||
const FAILED: ApiAttachmentMediaState = Object.freeze({status: 'failed'});
|
||||
|
||||
interface MediaEntry {
|
||||
readonly url: string;
|
||||
state: ApiAttachmentMediaState;
|
||||
queued: boolean;
|
||||
controller: AbortController | null;
|
||||
refs: number;
|
||||
size: number;
|
||||
failures: number;
|
||||
retryAtMs: number;
|
||||
retryTimer: ReturnType<typeof setTimeout> | null;
|
||||
}
|
||||
|
||||
class NotAnApiAttachmentError extends Error {
|
||||
constructor() {
|
||||
super('The link is not an attachment link on the API');
|
||||
this.name = 'NotAnApiAttachmentError';
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchApiAttachmentBlob(url: string, signal?: AbortSignal): Promise<Blob> {
|
||||
const path = apiAttachmentRequestPath(unwrapDesktopLocalResourceURL(url));
|
||||
if (path === null) throw new NotAnApiAttachmentError();
|
||||
const response = await http.get<Blob>(path, {parse: 'binary', mode: 'auto-retry', retries: LOAD_RETRIES, signal});
|
||||
if (!(response.body instanceof Blob)) throw new Error('The attachment response had no body');
|
||||
return response.body;
|
||||
}
|
||||
|
||||
function isPermanentFailure(error: unknown): boolean {
|
||||
if (error instanceof NotAnApiAttachmentError) return true;
|
||||
return error instanceof HttpError && (error.status === 400 || error.status === 403 || error.status === 404);
|
||||
}
|
||||
|
||||
class ApiAttachmentMedia {
|
||||
private entries = new Map<string, MediaEntry>();
|
||||
private readonly waiting: Array<MediaEntry> = [];
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private active = 0;
|
||||
private cachedBytes = 0;
|
||||
private generation = 0;
|
||||
private headerMode = false;
|
||||
|
||||
subscribe = (listener: () => void): (() => void) => {
|
||||
this.listeners.add(listener);
|
||||
return () => {
|
||||
this.listeners.delete(listener);
|
||||
};
|
||||
};
|
||||
|
||||
isHeaderMode = (): boolean => this.headerMode;
|
||||
|
||||
currentGeneration = (): number => this.generation;
|
||||
|
||||
setHeaderMode(headerMode: boolean): void {
|
||||
if (this.headerMode === headerMode) return;
|
||||
this.headerMode = headerMode;
|
||||
if (headerMode) this.reset();
|
||||
else this.notify();
|
||||
}
|
||||
|
||||
read(url: string): ApiAttachmentMediaState {
|
||||
return this.entries.get(url)?.state ?? LOADING;
|
||||
}
|
||||
|
||||
retain(url: string): () => void {
|
||||
const entry = this.entries.get(url) ?? this.create(url);
|
||||
entry.refs += 1;
|
||||
this.touch(entry);
|
||||
if (entry.state.status === 'failed' && Date.now() >= entry.retryAtMs) this.restart(entry);
|
||||
let released = false;
|
||||
return () => {
|
||||
if (released) return;
|
||||
released = true;
|
||||
this.release(entry);
|
||||
};
|
||||
}
|
||||
|
||||
warm(url: string): void {
|
||||
if (!this.entries.has(url)) this.create(url);
|
||||
}
|
||||
|
||||
retry(url: string): void {
|
||||
const entry = this.entries.get(url);
|
||||
if (entry === undefined || entry.state.status !== 'failed') return;
|
||||
entry.retryAtMs = 0;
|
||||
if (entry.refs > 0) this.restart(entry);
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this.generation += 1;
|
||||
for (const entry of this.entries.values()) this.discard(entry);
|
||||
this.entries = new Map();
|
||||
this.waiting.length = 0;
|
||||
this.active = 0;
|
||||
this.cachedBytes = 0;
|
||||
this.notify();
|
||||
}
|
||||
|
||||
private create(url: string): MediaEntry {
|
||||
const entry: MediaEntry = {
|
||||
url,
|
||||
state: LOADING,
|
||||
queued: false,
|
||||
controller: null,
|
||||
refs: 0,
|
||||
size: 0,
|
||||
failures: 0,
|
||||
retryAtMs: 0,
|
||||
retryTimer: null,
|
||||
};
|
||||
this.entries.set(url, entry);
|
||||
this.enqueue(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
private touch(entry: MediaEntry): void {
|
||||
this.entries.delete(entry.url);
|
||||
this.entries.set(entry.url, entry);
|
||||
}
|
||||
|
||||
private restart(entry: MediaEntry): void {
|
||||
this.cancelRetry(entry);
|
||||
entry.state = LOADING;
|
||||
this.enqueue(entry);
|
||||
this.notify();
|
||||
}
|
||||
|
||||
private enqueue(entry: MediaEntry): void {
|
||||
if (entry.queued || entry.controller !== null) return;
|
||||
entry.queued = true;
|
||||
this.waiting.push(entry);
|
||||
this.pump();
|
||||
}
|
||||
|
||||
private release(entry: MediaEntry): void {
|
||||
entry.refs = Math.max(0, entry.refs - 1);
|
||||
if (entry.refs > 0 || this.entries.get(entry.url) !== entry) return;
|
||||
if (entry.state.status !== 'loading') {
|
||||
this.evict();
|
||||
return;
|
||||
}
|
||||
this.discard(entry);
|
||||
this.entries.delete(entry.url);
|
||||
}
|
||||
|
||||
private discard(entry: MediaEntry): void {
|
||||
if (entry.queued) {
|
||||
entry.queued = false;
|
||||
const index = this.waiting.indexOf(entry);
|
||||
if (index >= 0) this.waiting.splice(index, 1);
|
||||
}
|
||||
entry.controller?.abort();
|
||||
entry.controller = null;
|
||||
this.cancelRetry(entry);
|
||||
if (entry.state.status === 'ready') {
|
||||
URL.revokeObjectURL(entry.state.objectUrl);
|
||||
this.cachedBytes -= entry.size;
|
||||
}
|
||||
entry.state = LOADING;
|
||||
}
|
||||
|
||||
private evict(): void {
|
||||
for (const entry of this.entries.values()) {
|
||||
if (this.cachedBytes <= MAX_CACHED_BYTES && this.entries.size <= MAX_CACHED_ENTRIES) return;
|
||||
if (entry.refs > 0 || entry.state.status === 'loading') continue;
|
||||
this.discard(entry);
|
||||
this.entries.delete(entry.url);
|
||||
}
|
||||
}
|
||||
|
||||
private pump(): void {
|
||||
while (this.active < MAX_CONCURRENT_LOADS && this.waiting.length > 0) {
|
||||
const entry = this.waiting.shift()!;
|
||||
entry.queued = false;
|
||||
void this.load(entry);
|
||||
}
|
||||
}
|
||||
|
||||
private async load(entry: MediaEntry): Promise<void> {
|
||||
const generation = this.generation;
|
||||
const controller = new AbortController();
|
||||
entry.controller = controller;
|
||||
this.active += 1;
|
||||
let retryNow = false;
|
||||
try {
|
||||
const blob = await fetchApiAttachmentBlob(entry.url, controller.signal);
|
||||
if (generation !== this.generation || controller.signal.aborted) return;
|
||||
entry.state = {status: 'ready', objectUrl: URL.createObjectURL(blob)};
|
||||
entry.size = blob.size;
|
||||
entry.failures = 0;
|
||||
this.cachedBytes += entry.size;
|
||||
this.evict();
|
||||
this.notify();
|
||||
} catch (error) {
|
||||
if (generation !== this.generation || controller.signal.aborted) return;
|
||||
if (error instanceof HttpError && error.status === 401 && entry.failures === 0) {
|
||||
entry.failures = 1;
|
||||
retryNow = true;
|
||||
return;
|
||||
}
|
||||
logger.warn('Could not load an attachment', error);
|
||||
this.fail(entry, isPermanentFailure(error));
|
||||
} finally {
|
||||
if (generation === this.generation) {
|
||||
if (entry.controller === controller) entry.controller = null;
|
||||
this.active -= 1;
|
||||
if (retryNow && entry.refs > 0) this.enqueue(entry);
|
||||
else if (retryNow) this.entries.delete(entry.url);
|
||||
this.pump();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private cancelRetry(entry: MediaEntry): void {
|
||||
if (entry.retryTimer === null) return;
|
||||
clearTimeout(entry.retryTimer);
|
||||
entry.retryTimer = null;
|
||||
}
|
||||
|
||||
private fail(entry: MediaEntry, permanent: boolean): void {
|
||||
entry.failures += 1;
|
||||
const delayMs = Math.min(FAILURE_BACKOFF_MS * 2 ** (entry.failures - 1), MAX_FAILURE_BACKOFF_MS);
|
||||
entry.retryAtMs = permanent ? Number.POSITIVE_INFINITY : Date.now() + delayMs;
|
||||
entry.state = FAILED;
|
||||
if (!permanent) {
|
||||
entry.retryTimer = setTimeout(() => {
|
||||
entry.retryTimer = null;
|
||||
if (entry.refs > 0 && entry.state === FAILED && this.entries.get(entry.url) === entry) this.restart(entry);
|
||||
}, delayMs);
|
||||
}
|
||||
this.notify();
|
||||
}
|
||||
|
||||
private notify(): void {
|
||||
for (const listener of this.listeners) listener();
|
||||
}
|
||||
}
|
||||
|
||||
const apiAttachmentMedia = new ApiAttachmentMedia();
|
||||
ResettableStates.register(apiAttachmentMedia, () => apiAttachmentMedia.reset());
|
||||
|
||||
export function fetchedApiAttachmentKey(url: string): string | null {
|
||||
if (apiAttachmentMedia.isHeaderMode()) return null;
|
||||
const target = unwrapDesktopLocalResourceURL(url);
|
||||
return isApiAttachmentUrl(target) ? withoutAttachmentToken(target) : null;
|
||||
}
|
||||
|
||||
export default apiAttachmentMedia;
|
||||
@@ -4,6 +4,9 @@ import {Endpoints} from '@app/features/app/constants/Endpoints';
|
||||
import Initialization from '@app/features/app/state/Initialization';
|
||||
import {ResettableStates} from '@app/features/app/state/ResettableStates';
|
||||
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
|
||||
import apiAttachmentFileTokens from '@app/features/messaging/state/ApiAttachmentFileTokens';
|
||||
import apiAttachmentMedia from '@app/features/messaging/state/ApiAttachmentMedia';
|
||||
import {isApiAttachmentUrl, withoutAttachmentToken} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {
|
||||
ATTACHMENT_URL_REFRESH_MARGIN_MS,
|
||||
type AttachmentUrlSignature,
|
||||
@@ -47,8 +50,8 @@ interface QueuedRequest {
|
||||
export function sameAttachmentSource(first: string, second: string): boolean {
|
||||
if (first === second) return true;
|
||||
return (
|
||||
stripAttachmentSignature(unwrapDesktopLocalResourceURL(first)) ===
|
||||
stripAttachmentSignature(unwrapDesktopLocalResourceURL(second))
|
||||
stripAttachmentSignature(withoutAttachmentToken(unwrapDesktopLocalResourceURL(first))) ===
|
||||
stripAttachmentSignature(withoutAttachmentToken(unwrapDesktopLocalResourceURL(second)))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -103,6 +106,7 @@ class AttachmentUrlRefresher {
|
||||
}
|
||||
|
||||
fresh(url: string, options: {refreshUnsigned?: boolean} = {}): string {
|
||||
if (isApiAttachmentUrl(url)) return withoutAttachmentToken(url);
|
||||
if (!url || !isOwnAttachmentUrl(url)) return url;
|
||||
const key = attachmentCacheKey(url);
|
||||
if (key === null) return url;
|
||||
@@ -130,6 +134,7 @@ class AttachmentUrlRefresher {
|
||||
}
|
||||
|
||||
freshWhenReady(url: string, options: {refreshUnsigned?: boolean} = {}): string | null {
|
||||
if (isApiAttachmentUrl(url)) return withoutAttachmentToken(url);
|
||||
const current = this.fresh(url, options);
|
||||
if (!current || !isOwnAttachmentUrl(current)) return current;
|
||||
const nowMs = Date.now();
|
||||
@@ -158,6 +163,14 @@ class AttachmentUrlRefresher {
|
||||
async refresh(url: string, options: {force?: boolean} = {}): Promise<string> {
|
||||
if (!url) return url;
|
||||
const target = unwrapDesktopLocalResourceURL(url);
|
||||
if (isApiAttachmentUrl(target)) {
|
||||
const bare = withoutAttachmentToken(target);
|
||||
if (options.force) {
|
||||
apiAttachmentFileTokens.invalidate(bare);
|
||||
apiAttachmentMedia.retry(bare);
|
||||
}
|
||||
return bare;
|
||||
}
|
||||
if (!isOwnAttachmentUrl(target)) return url;
|
||||
const key = attachmentCacheKey(target);
|
||||
if (key === null) return url;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
|
||||
import {parseEndpoint} from '@app/features/messaging/utils/AttachmentCdnUrl';
|
||||
|
||||
const ACCESS_TOKEN_PARAMETER = 'mt';
|
||||
const MESSAGE_PARAMETER = 'mid';
|
||||
const VERSION_PREFIX_PATTERN = /^\/v[0-9]+(?=\/)/u;
|
||||
const ATTACHMENT_ROUTE_PATTERN = /^\/channels\/[0-9]+\/attachments\/[0-9]+\/[^/]/u;
|
||||
const ENCODED_SEPARATOR_PATTERN = /%(?:2e|2f|5c)/iu;
|
||||
|
||||
interface ParsedApiAttachmentUrl {
|
||||
url: URL;
|
||||
routePath: string;
|
||||
}
|
||||
|
||||
export function isApiProxiedAttachmentsEnabled(): boolean {
|
||||
return RuntimeConfig.getSnapshotOrNull()?.features.proxy_attachments_through_api === true;
|
||||
}
|
||||
|
||||
function parseApiAttachmentUrl(url: string): ParsedApiAttachmentUrl | null {
|
||||
if (!isApiProxiedAttachmentsEnabled()) return null;
|
||||
const apiEndpoint = RuntimeConfig.getSnapshotOrNull()?.apiEndpoint;
|
||||
const endpoint = apiEndpoint ? parseEndpoint(apiEndpoint) : null;
|
||||
if (endpoint === null) return null;
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (parsed.origin !== endpoint.origin || parsed.username !== '' || parsed.password !== '') return null;
|
||||
const basePath = endpoint.basePath === '/' ? '' : endpoint.basePath;
|
||||
if (!parsed.pathname.startsWith(`${basePath}/`)) return null;
|
||||
const routePath = parsed.pathname.slice(basePath.length).replace(VERSION_PREFIX_PATTERN, '');
|
||||
return ATTACHMENT_ROUTE_PATTERN.test(routePath) && !ENCODED_SEPARATOR_PATTERN.test(routePath)
|
||||
? {url: parsed, routePath}
|
||||
: null;
|
||||
}
|
||||
|
||||
export function isApiAttachmentUrl(url: string): boolean {
|
||||
return parseApiAttachmentUrl(url) !== null;
|
||||
}
|
||||
|
||||
export function apiAttachmentRequestPath(url: string): string | null {
|
||||
const parsed = parseApiAttachmentUrl(url);
|
||||
if (parsed === null) return null;
|
||||
parsed.url.searchParams.delete(ACCESS_TOKEN_PARAMETER);
|
||||
return `${parsed.routePath}${parsed.url.search}`;
|
||||
}
|
||||
|
||||
export function apiAttachmentFileKey(url: string): string | null {
|
||||
const parsed = parseApiAttachmentUrl(url);
|
||||
if (parsed === null) return null;
|
||||
const messageIds = parsed.url.searchParams.getAll(MESSAGE_PARAMETER);
|
||||
parsed.url.search = '';
|
||||
for (const messageId of messageIds) parsed.url.searchParams.append(MESSAGE_PARAMETER, messageId);
|
||||
parsed.url.hash = '';
|
||||
return parsed.url.toString();
|
||||
}
|
||||
|
||||
export function readAttachmentToken(url: string): string | null {
|
||||
return parseApiAttachmentUrl(url)?.url.searchParams.get(ACCESS_TOKEN_PARAMETER) ?? null;
|
||||
}
|
||||
|
||||
export function withoutAttachmentToken(url: string): string {
|
||||
const parsed = parseApiAttachmentUrl(url);
|
||||
if (parsed === null || !parsed.url.searchParams.has(ACCESS_TOKEN_PARAMETER)) return url;
|
||||
parsed.url.searchParams.delete(ACCESS_TOKEN_PARAMETER);
|
||||
return parsed.url.toString();
|
||||
}
|
||||
|
||||
export function withAttachmentToken(url: string, token: string): string {
|
||||
const parsed = parseApiAttachmentUrl(url);
|
||||
if (parsed === null) return url;
|
||||
parsed.url.searchParams.set(ACCESS_TOKEN_PARAMETER, token);
|
||||
return parsed.url.toString();
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import {runtimeInstanceKey} from '@app/features/app/state/InstanceSnapshotStore';
|
||||
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
|
||||
import {isApiAttachmentUrl} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {unwrapDesktopLocalResourceURL} from '@app/features/messaging/utils/DesktopLocalResourceTarget';
|
||||
import {isDesktopLocalAppDocument} from '@app/features/platform/DesktopLocalAppRuntime';
|
||||
import {
|
||||
@@ -154,7 +155,7 @@ function isCurrentRuntimeRemoteResourceTarget(target: URL): boolean {
|
||||
const sourceOrigin = originSource(source);
|
||||
if (sourceOrigin != null && sourceOrigin === targetOrigin) return true;
|
||||
}
|
||||
return false;
|
||||
return isApiAttachmentUrl(target.toString());
|
||||
}
|
||||
|
||||
function originSource(value: string | null): string | null {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
|
||||
import apiAttachmentFileTokens from '@app/features/messaging/state/ApiAttachmentFileTokens';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {isApiAttachmentUrl} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {isUrlOnEndpoint, parseEndpoint} from '@app/features/messaging/utils/AttachmentCdnUrl';
|
||||
import {
|
||||
unwrapDesktopLocalResourceURL,
|
||||
@@ -20,7 +22,8 @@ type MediaType = 'image' | 'gif' | 'video' | 'audio' | 'file';
|
||||
function appendMediaProxyDownloadParam(src: string): string {
|
||||
return updateDesktopLocalResourceURLTarget(src, (target) => {
|
||||
const mediaEndpoint = parseEndpoint(RuntimeConfig.mediaEndpoint);
|
||||
if (!mediaEndpoint || !isUrlOnEndpoint(target, mediaEndpoint)) {
|
||||
const onMediaEndpoint = mediaEndpoint !== null && isUrlOnEndpoint(target, mediaEndpoint);
|
||||
if (!onMediaEndpoint && !isApiAttachmentUrl(target.toString())) {
|
||||
return false;
|
||||
}
|
||||
target.searchParams.set('download', 'true');
|
||||
@@ -197,7 +200,9 @@ function downloadViaAnchor(src: string, suggestedName: string, options?: {append
|
||||
export async function downloadFile(src: string, type: MediaType, providedFilename?: string): Promise<void> {
|
||||
if (!src) return;
|
||||
const suggestedName = deriveSuggestedName(src, type, providedFilename);
|
||||
const target = wrapDesktopLocalResourceURL(await AttachmentUrlRefresher.refresh(unwrapDesktopLocalResourceURL(src)));
|
||||
const target = wrapDesktopLocalResourceURL(
|
||||
await apiAttachmentFileTokens.linkUrl(await AttachmentUrlRefresher.refresh(unwrapDesktopLocalResourceURL(src))),
|
||||
);
|
||||
if (isElectron()) {
|
||||
try {
|
||||
const outcome = await downloadWithNative({url: unwrapDesktopLocalResourceURL(target), suggestedName});
|
||||
|
||||
@@ -19,7 +19,10 @@ import {
|
||||
} from '@app/features/i18n/utils/CommonMessageDescriptors';
|
||||
import {EditAltTextModal} from '@app/features/messaging/components/modals/EditAltTextModal';
|
||||
import type {Message} from '@app/features/messaging/models/MessagingMessage';
|
||||
import apiAttachmentFileTokens from '@app/features/messaging/state/ApiAttachmentFileTokens';
|
||||
import {fetchApiAttachmentBlob} from '@app/features/messaging/state/ApiAttachmentMedia';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {isApiAttachmentUrl} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {
|
||||
unwrapDesktopLocalResourceURL,
|
||||
wrapDesktopLocalResourceURL,
|
||||
@@ -253,15 +256,21 @@ async function convertImageBlobToPng(blob: Blob): Promise<Blob> {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchImageBlob(url: string): Promise<Blob> {
|
||||
const response = await fetch(wrapDesktopLocalResourceURL(url));
|
||||
if (!response.ok) {
|
||||
throw new Error(`Unexpected response status ${response.status} for ${url}`);
|
||||
}
|
||||
return response.blob();
|
||||
}
|
||||
|
||||
async function fetchFirstPngBlob(urls: Array<string>): Promise<Blob> {
|
||||
let lastError: unknown = null;
|
||||
for (const url of urls) {
|
||||
try {
|
||||
const response = await fetch(wrapDesktopLocalResourceURL(url));
|
||||
if (!response.ok) {
|
||||
throw new Error(`Unexpected response status ${response.status} for ${url}`);
|
||||
}
|
||||
const blob = await response.blob();
|
||||
const blob = isApiAttachmentUrl(unwrapDesktopLocalResourceURL(url))
|
||||
? await fetchApiAttachmentBlob(url)
|
||||
: await fetchImageBlob(url);
|
||||
return await convertImageBlobToPng(blob);
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
@@ -454,7 +463,7 @@ export async function copyMediaToClipboard({
|
||||
timeout: 0,
|
||||
});
|
||||
const result = await electronApi.clipboardWriteFile({
|
||||
url: unwrapDesktopLocalResourceURL(baseProxyURL || freshSrc),
|
||||
url: await apiAttachmentFileTokens.linkUrl(unwrapDesktopLocalResourceURL(baseProxyURL || freshSrc)),
|
||||
suggestedName: defaultName,
|
||||
mediaType: clipboardFileMediaType,
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
REVERSE_IMAGE_SEARCH_DESCRIPTOR,
|
||||
} from '@app/features/i18n/utils/CommonMessageDescriptors';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {isApiAttachmentUrl} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {attachmentUrlMayNeedRefresh} from '@app/features/messaging/utils/AttachmentCdnUrl';
|
||||
import {SearchProviderPickerModal} from '@app/features/search/components/modals/SearchProviderPickerModal';
|
||||
import ReverseImageSearch from '@app/features/search/state/ReverseImageSearch';
|
||||
@@ -99,7 +100,7 @@ export function buildReverseImageSearchMenuGroups(
|
||||
},
|
||||
});
|
||||
}
|
||||
if (state.enabledEngines.length > 0) {
|
||||
if (state.enabledEngines.length > 0 && !isApiAttachmentUrl(imageUrl)) {
|
||||
const handleDefault = () => {
|
||||
const defaultEngine = ReverseImageSearch.defaultEngine;
|
||||
if (defaultEngine) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {isApiAttachmentUrl} from '@app/features/messaging/utils/ApiAttachmentUrl';
|
||||
import {attachmentUrlMayNeedRefresh} from '@app/features/messaging/utils/AttachmentCdnUrl';
|
||||
import {unwrapDesktopLocalResourceURL} from '@app/features/messaging/utils/DesktopLocalResourceTarget';
|
||||
import {Logger} from '@app/features/platform/utils/AppLogger';
|
||||
@@ -194,14 +195,25 @@ export async function navigateToExternalURL(url: string): Promise<void> {
|
||||
}
|
||||
|
||||
async function refreshAttachmentUrl(url: string): Promise<string> {
|
||||
const {default: AttachmentUrlRefresher} = await import('@app/features/messaging/state/AttachmentUrlRefresher');
|
||||
return AttachmentUrlRefresher.refresh(url);
|
||||
const [{default: AttachmentUrlRefresher}, {default: apiAttachmentFileTokens}] = await Promise.all([
|
||||
import('@app/features/messaging/state/AttachmentUrlRefresher'),
|
||||
import('@app/features/messaging/state/ApiAttachmentFileTokens'),
|
||||
]);
|
||||
return apiAttachmentFileTokens.linkUrl(await AttachmentUrlRefresher.refresh(url));
|
||||
}
|
||||
|
||||
export async function openExternalUrl(url: string, target: string = '_blank') {
|
||||
const safeUrl = getSafeExternalUrl(url);
|
||||
if (!safeUrl) return;
|
||||
const electronApi = getElectronAPI();
|
||||
if (!electronApi && isApiAttachmentUrl(safeUrl)) {
|
||||
const opened = window.open('about:blank', target);
|
||||
if (opened) opened.opener = null;
|
||||
const scopedUrl = await refreshAttachmentUrl(safeUrl);
|
||||
if (opened) opened.location.replace(scopedUrl);
|
||||
else window.open(scopedUrl, target, 'noopener,noreferrer');
|
||||
return;
|
||||
}
|
||||
if (!electronApi && !attachmentUrlMayNeedRefresh(safeUrl, Date.now())) {
|
||||
window.open(safeUrl, target, 'noopener,noreferrer');
|
||||
return;
|
||||
|
||||
@@ -79,7 +79,7 @@ export function AudioPlayer({
|
||||
const {volume, isMuted, setVolume, toggleMute} = useMediaVolume({mediaRef});
|
||||
const {escalateToMetadata, sourceAttribute, preloadAttribute} = useMetadataPreload(src, hasStarted);
|
||||
useEffect(() => {
|
||||
if (hasStarted && pendingPlayRef.current) {
|
||||
if (hasStarted && sourceAttribute !== undefined && pendingPlayRef.current) {
|
||||
const timer = setTimeout(() => {
|
||||
if (pendingPlayRef.current) {
|
||||
pendingPlayRef.current = false;
|
||||
@@ -89,7 +89,7 @@ export function AudioPlayer({
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
return undefined;
|
||||
}, [hasStarted, play]);
|
||||
}, [hasStarted, play, sourceAttribute]);
|
||||
useEffect(() => {
|
||||
if (!hasStarted || pendingSeekPercentageRef.current === null) {
|
||||
return;
|
||||
|
||||
+2
-2
@@ -120,7 +120,7 @@ export function InlineAudioPlayer({
|
||||
: splitFilename(title);
|
||||
const fileSizeString = fileSize ? formatFileSize(getFormattingLocale(), fileSize) : '';
|
||||
useEffect(() => {
|
||||
if (hasStarted && pendingPlayRef.current) {
|
||||
if (hasStarted && sourceAttribute !== undefined && pendingPlayRef.current) {
|
||||
const timer = setTimeout(() => {
|
||||
if (pendingPlayRef.current) {
|
||||
pendingPlayRef.current = false;
|
||||
@@ -130,7 +130,7 @@ export function InlineAudioPlayer({
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
return undefined;
|
||||
}, [hasStarted, play]);
|
||||
}, [hasStarted, play, sourceAttribute]);
|
||||
const handlePlayToggle = useCallback(
|
||||
(e: React.MouseEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import Accessibility from '@app/features/accessibility/state/Accessibility';
|
||||
import {useApiAttachmentPlaybackSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {resolveDesktopCrossOriginMediaURL} from '@app/features/messaging/utils/DesktopResourceUrl';
|
||||
import * as ImageCacheUtils from '@app/features/messaging/utils/ImageCacheUtils';
|
||||
import {decodeThumbHashDataURL} from '@app/features/messaging/utils/ThumbHashUtils';
|
||||
@@ -138,6 +139,7 @@ interface VideoSeekPreviewThumbnailProps {
|
||||
|
||||
function VideoSeekPreviewThumbnail({src, poster, time}: VideoSeekPreviewThumbnailProps) {
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
const playbackSrc = useApiAttachmentPlaybackSrc(src);
|
||||
const seekPreviewVideo = useCallback((targetTime: number) => {
|
||||
const video = videoRef.current;
|
||||
if (!video || video.readyState < 1) return;
|
||||
@@ -156,7 +158,7 @@ function VideoSeekPreviewThumbnail({src, poster, time}: VideoSeekPreviewThumbnai
|
||||
<video
|
||||
ref={videoRef}
|
||||
className={styles.seekPreviewVideo}
|
||||
src={resolveDesktopCrossOriginMediaURL(src)}
|
||||
src={resolveDesktopCrossOriginMediaURL(playbackSrc)}
|
||||
poster={poster}
|
||||
preload="metadata"
|
||||
muted
|
||||
@@ -358,6 +360,7 @@ export const VideoPlayer = observer(function VideoPlayer({
|
||||
}),
|
||||
[autoPlay, hasPlayed, wantsMetadata, state.error, state.isEnded, state.isPaused, state.isPlaying],
|
||||
);
|
||||
const playbackSrc = useApiAttachmentPlaybackSrc(renderModel.shouldAttachSource ? src : undefined);
|
||||
useEffect(() => {
|
||||
const snapshot = transitionVideoPlayerRenderSnapshot(renderSnapshotRef.current, {
|
||||
type: 'video.observePlayback',
|
||||
@@ -451,7 +454,7 @@ export const VideoPlayer = observer(function VideoPlayer({
|
||||
}, [handleResize, handleObservedResize]);
|
||||
const hasAutoPlayedRef = useRef(autoPlay);
|
||||
useEffect(() => {
|
||||
if (hasPlayed && !hasAutoPlayedRef.current) {
|
||||
if (hasPlayed && playbackSrc !== undefined && !hasAutoPlayedRef.current) {
|
||||
hasAutoPlayedRef.current = true;
|
||||
const timer = setTimeout(() => {
|
||||
play();
|
||||
@@ -459,7 +462,7 @@ export const VideoPlayer = observer(function VideoPlayer({
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
return undefined;
|
||||
}, [hasPlayed, play]);
|
||||
}, [hasPlayed, play, playbackSrc]);
|
||||
const handlePosterClick = useCallback(() => {
|
||||
containerRef.current?.focus({preventScroll: true});
|
||||
setHasPlayed(true);
|
||||
@@ -603,7 +606,7 @@ export const VideoPlayer = observer(function VideoPlayer({
|
||||
<video
|
||||
ref={mediaRef as React.RefObject<HTMLVideoElement>}
|
||||
className={clsx(styles.video, renderModel.shouldHideVideo && styles.videoHidden)}
|
||||
src={renderModel.shouldAttachSource ? resolveDesktopCrossOriginMediaURL(src) : undefined}
|
||||
src={resolveDesktopCrossOriginMediaURL(playbackSrc)}
|
||||
preload={renderModel.preloadAttribute}
|
||||
crossOrigin="anonymous"
|
||||
playsInline
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import apiAttachmentFileTokens from '@app/features/messaging/state/ApiAttachmentFileTokens';
|
||||
import AttachmentUrlRefresher from '@app/features/messaging/state/AttachmentUrlRefresher';
|
||||
import {unwrapDesktopLocalResourceURL} from '@app/features/messaging/utils/DesktopResourceUrl';
|
||||
import AppStorage from '@app/features/platform/state/PersistentStorage';
|
||||
import {Logger} from '@app/features/platform/utils/AppLogger';
|
||||
import {useMediaSourceResume} from '@app/features/voice/components/media_player/hooks/useMediaSourceResume';
|
||||
@@ -113,7 +115,7 @@ export function useMediaPlayer(options: UseMediaPlayerOptions = {}): UseMediaPla
|
||||
} = options;
|
||||
const mediaRef = useRef<HTMLMediaElement | null>(null);
|
||||
useInAppMediaSoundCapture(mediaRef);
|
||||
const sourceResumeRef = useMediaSourceResume(mediaRef);
|
||||
const {resumeRef: sourceResumeRef, playOnSwapRef} = useMediaSourceResume(mediaRef);
|
||||
const refreshedSourceRef = useRef<string | null>(null);
|
||||
const callbacksRef = useRef({onEnded, onError, onPlay, onLoadedMetadata});
|
||||
callbacksRef.current = {onEnded, onError, onPlay, onLoadedMetadata};
|
||||
@@ -233,7 +235,7 @@ export function useMediaPlayer(options: UseMediaPlayerOptions = {}): UseMediaPla
|
||||
};
|
||||
const handleResourceLoadStart = () => {
|
||||
clearPendingSeekTarget(media);
|
||||
holdReasonRef.current = null;
|
||||
if (sourceResumeRef.current === null) holdReasonRef.current = null;
|
||||
recoverPlaybackWhenReadyRef.current = false;
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
@@ -341,6 +343,10 @@ export function useMediaPlayer(options: UseMediaPlayerOptions = {}): UseMediaPla
|
||||
const play = useCallback(async () => {
|
||||
const media = mediaRef.current;
|
||||
if (!media) return;
|
||||
if (media.paused && apiAttachmentFileTokens.renew(unwrapDesktopLocalResourceURL(media.currentSrc))) {
|
||||
playOnSwapRef.current = true;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await media.play();
|
||||
setState((prev) => (prev.error === null ? prev : {...prev, error: null}));
|
||||
@@ -357,13 +363,17 @@ export function useMediaPlayer(options: UseMediaPlayerOptions = {}): UseMediaPla
|
||||
logger.error('Play failed:', normalizedError);
|
||||
setState((prev) => ({...prev, error: normalizedError}));
|
||||
}
|
||||
}, []);
|
||||
const pause = useCallback((reason: PlaybackHoldReason = 'user') => {
|
||||
const media = mediaRef.current;
|
||||
if (!media) return;
|
||||
holdReasonRef.current = reason;
|
||||
media.pause();
|
||||
}, []);
|
||||
}, [playOnSwapRef]);
|
||||
const pause = useCallback(
|
||||
(reason: PlaybackHoldReason = 'user') => {
|
||||
const media = mediaRef.current;
|
||||
if (!media) return;
|
||||
playOnSwapRef.current = false;
|
||||
holdReasonRef.current = reason;
|
||||
media.pause();
|
||||
},
|
||||
[playOnSwapRef],
|
||||
);
|
||||
const resumeHold = useCallback(
|
||||
(reason: PlaybackHoldReason) => {
|
||||
if (holdReasonRef.current !== reason) return false;
|
||||
|
||||
+23
-2
@@ -11,26 +11,41 @@ interface MediaSourceResume {
|
||||
playing: boolean;
|
||||
}
|
||||
|
||||
interface MediaSourceResumeRefs {
|
||||
resumeRef: RefObject<MediaSourceResume | null>;
|
||||
playOnSwapRef: RefObject<boolean>;
|
||||
}
|
||||
|
||||
export function useMediaSourceResume(
|
||||
mediaRef: RefObject<HTMLMediaElement | null>,
|
||||
attached = true,
|
||||
): RefObject<MediaSourceResume | null> {
|
||||
): MediaSourceResumeRefs {
|
||||
const resumeRef = useRef<MediaSourceResume | null>(null);
|
||||
const playOnSwapRef = useRef(false);
|
||||
useEffect(() => {
|
||||
const media = mediaRef.current;
|
||||
if (!media || !attached) return;
|
||||
let source = media.currentSrc;
|
||||
let time = 0;
|
||||
let playing = !media.paused;
|
||||
let pausedByError = false;
|
||||
const recordTime = () => {
|
||||
if (media.readyState >= HAVE_METADATA_READY_STATE) time = media.currentTime;
|
||||
};
|
||||
const markPlaying = () => {
|
||||
pausedByError = false;
|
||||
playing = true;
|
||||
};
|
||||
const markStopped = () => {
|
||||
if (pausedByError) {
|
||||
pausedByError = false;
|
||||
return;
|
||||
}
|
||||
playing = false;
|
||||
};
|
||||
const handleError = () => {
|
||||
pausedByError = playing;
|
||||
};
|
||||
const handleEnded = () => {
|
||||
playing = false;
|
||||
time = 0;
|
||||
@@ -38,6 +53,9 @@ export function useMediaSourceResume(
|
||||
const handleLoadStart = () => {
|
||||
const nextSource = media.currentSrc;
|
||||
const swapped = source !== '' && source !== nextSource && sameAttachmentSource(source, nextSource);
|
||||
const playOnSwap = playOnSwapRef.current;
|
||||
playOnSwapRef.current = false;
|
||||
pausedByError = false;
|
||||
source = nextSource;
|
||||
if (!swapped) {
|
||||
resumeRef.current = null;
|
||||
@@ -45,6 +63,7 @@ export function useMediaSourceResume(
|
||||
playing = !media.paused;
|
||||
return;
|
||||
}
|
||||
playing = playing || playOnSwap;
|
||||
resumeRef.current = {time, playing};
|
||||
if (playing) void Promise.resolve(media.play()).catch(() => {});
|
||||
};
|
||||
@@ -61,6 +80,7 @@ export function useMediaSourceResume(
|
||||
media.addEventListener('play', markPlaying);
|
||||
media.addEventListener('pause', markStopped);
|
||||
media.addEventListener('ended', handleEnded);
|
||||
media.addEventListener('error', handleError);
|
||||
media.addEventListener('loadstart', handleLoadStart);
|
||||
media.addEventListener('loadedmetadata', handleLoadedMetadata);
|
||||
return () => {
|
||||
@@ -69,9 +89,10 @@ export function useMediaSourceResume(
|
||||
media.removeEventListener('play', markPlaying);
|
||||
media.removeEventListener('pause', markStopped);
|
||||
media.removeEventListener('ended', handleEnded);
|
||||
media.removeEventListener('error', handleError);
|
||||
media.removeEventListener('loadstart', handleLoadStart);
|
||||
media.removeEventListener('loadedmetadata', handleLoadedMetadata);
|
||||
};
|
||||
}, [attached, mediaRef]);
|
||||
return resumeRef;
|
||||
return {resumeRef, playOnSwapRef};
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {useApiAttachmentPlaybackSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {useCallback, useState} from 'react';
|
||||
|
||||
type MediaPreloadAttribute = 'none' | 'metadata';
|
||||
@@ -15,9 +16,10 @@ export function useMetadataPreload(src: string, hasStarted: boolean): UseMetadat
|
||||
const escalateToMetadata = useCallback(() => {
|
||||
setWantsMetadata(true);
|
||||
}, []);
|
||||
const sourceAttribute = useApiAttachmentPlaybackSrc(hasStarted || wantsMetadata ? src : undefined);
|
||||
return {
|
||||
escalateToMetadata,
|
||||
sourceAttribute: hasStarted || wantsMetadata ? src : undefined,
|
||||
sourceAttribute,
|
||||
preloadAttribute: wantsMetadata ? 'metadata' : 'none',
|
||||
};
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import {getCachedNumberFormat} from '@app/features/i18n/utils/IntlCache';
|
||||
import {isKeyboardActivationKey} from '@app/features/input/utils/KeyboardUtils';
|
||||
import {PanZoomSurface} from '@app/features/messaging/components/modals/media_modal/pan_zoom/PanZoomSurface';
|
||||
import type {ZoomState} from '@app/features/messaging/components/modals/media_modal/shared';
|
||||
import {useApiAttachmentPlaybackSrc} from '@app/features/messaging/hooks/useApiAttachmentSources';
|
||||
import {useAttachmentRefreshOnError} from '@app/features/messaging/hooks/useAttachmentRefreshOnError';
|
||||
import {resolveDesktopCrossOriginMediaURL} from '@app/features/messaging/utils/DesktopResourceUrl';
|
||||
import {remFromPx} from '@app/features/theme/layout/RemFromPx';
|
||||
@@ -78,6 +79,7 @@ export const MobileVideoViewer = observer(function MobileVideoViewer({
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
useMediaSourceResume(videoRef);
|
||||
const handleVideoError = useAttachmentRefreshOnError(src);
|
||||
const playbackSrc = useApiAttachmentPlaybackSrc(src);
|
||||
useInAppMediaSoundCapture(videoRef);
|
||||
const [isPlaying, setIsPlaying] = useState(false);
|
||||
const [zoomState, setZoomState] = useState<ZoomState>('fit');
|
||||
@@ -397,7 +399,7 @@ export const MobileVideoViewer = observer(function MobileVideoViewer({
|
||||
<video
|
||||
ref={videoRef}
|
||||
className={styles.video}
|
||||
src={resolveDesktopCrossOriginMediaURL(src)}
|
||||
src={resolveDesktopCrossOriginMediaURL(playbackSrc)}
|
||||
autoPlay={initialTime === undefined}
|
||||
playsInline
|
||||
loop={loop}
|
||||
|
||||
Vendored
+1
@@ -402,6 +402,7 @@ export interface ElectronAPI {
|
||||
desktopModules?: DesktopModuleAPI;
|
||||
desktopUpdate?: DesktopUpdateAPI;
|
||||
reportLastRoute?: (routePath: string) => void;
|
||||
setApiAttachmentAuthorization?: (apiEndpoint: string, token: string | null) => Promise<boolean>;
|
||||
reportInstanceProductName?: (instanceKey: string, productName: string) => void;
|
||||
notifyFirstContentPainted?: (view?: 'restored') => void;
|
||||
localAppUpload?: DesktopLocalAppUploadAPI;
|
||||
|
||||
@@ -111,9 +111,16 @@ fn instance_sources_from_discovery(discovery: &serde_json::Value) -> InstanceCsp
|
||||
connect_origins.push(origin);
|
||||
}
|
||||
}
|
||||
let proxies_attachments = discovery
|
||||
.pointer("/features/proxy_attachments_through_api")
|
||||
.and_then(serde_json::Value::as_bool)
|
||||
== Some(true);
|
||||
InstanceCspSources {
|
||||
static_cdn_endpoint: http_origin("static_cdn"),
|
||||
media_endpoint: http_origin("media"),
|
||||
attachment_api_endpoint: proxies_attachments
|
||||
.then(|| http_origin("api_client").or_else(|| http_origin("api")))
|
||||
.flatten(),
|
||||
connect_origins,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,6 +84,7 @@ fn has_src_attribute(attributes: &str) -> bool {
|
||||
pub struct InstanceCspSources {
|
||||
pub static_cdn_endpoint: Option<HttpEndpoint>,
|
||||
pub media_endpoint: Option<HttpEndpoint>,
|
||||
pub attachment_api_endpoint: Option<HttpEndpoint>,
|
||||
pub connect_origins: Vec<String>,
|
||||
}
|
||||
|
||||
@@ -306,11 +307,16 @@ fn build_csp_directives(
|
||||
for origin in branding_image_origins {
|
||||
push_endpoint_source(&mut img, Some(origin));
|
||||
}
|
||||
push_endpoint_source(&mut img, instance_sources.attachment_api_endpoint.as_ref());
|
||||
directives.push(format!("img-src {}", img.join(" ")));
|
||||
|
||||
let mut media = vec!["'self'".to_owned(), "blob:".to_owned()];
|
||||
extend_from(&mut media, &config.extra_media_src, MEDIA_SOURCES);
|
||||
extend_runtime_sources(&mut media, runtime_sources, instance_sources, true, true);
|
||||
push_endpoint_source(
|
||||
&mut media,
|
||||
instance_sources.attachment_api_endpoint.as_ref(),
|
||||
);
|
||||
directives.push(format!("media-src {}", media.join(" ")));
|
||||
|
||||
let mut font = vec!["'self'".to_owned(), "blob:".to_owned(), "data:".to_owned()];
|
||||
@@ -532,4 +538,29 @@ mod tests {
|
||||
.contains("example.test")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_attachment_api_origin_is_granted_to_images_and_media_only_when_set() {
|
||||
let policy = CompiledCspPolicy::compile(default_csp_config(), &runtime_sources()).unwrap();
|
||||
let without = policy.spa_headers(&[], &[], &InstanceCspSources::default());
|
||||
let api = endpoint("https://api.example.test");
|
||||
let with = policy.spa_headers(
|
||||
&[],
|
||||
&[],
|
||||
&InstanceCspSources {
|
||||
attachment_api_endpoint: Some(api.clone()),
|
||||
..InstanceCspSources::default()
|
||||
},
|
||||
);
|
||||
let without = without.enforced.to_str().unwrap().to_owned();
|
||||
let with = with.enforced.to_str().unwrap().to_owned();
|
||||
assert!(!without.contains(api.csp_origin()));
|
||||
for directive in with.split("; ") {
|
||||
let grants = directive
|
||||
.split(' ')
|
||||
.any(|source| source == api.csp_origin());
|
||||
let expected = directive.starts_with("img-src ") || directive.starts_with("media-src ");
|
||||
assert_eq!(grants, expected, "{directive}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -410,6 +410,7 @@ export interface ElectronAPI {
|
||||
desktopModules: DesktopModuleAPI;
|
||||
desktopUpdate: DesktopUpdateAPI;
|
||||
reportLastRoute: (routePath: string) => void;
|
||||
setApiAttachmentAuthorization: (apiEndpoint: string, token: string | null) => Promise<boolean>;
|
||||
reportInstanceProductName: (instanceKey: string, productName: string) => void;
|
||||
notifyFirstContentPainted: (view?: 'restored') => void;
|
||||
desktopAccounts: DesktopAccountStorageAPI;
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
const AUTHORIZATION_HEADER = 'Authorization';
|
||||
export const ATTACHMENT_WINDOW_HEADER = 'X-Fluxer-Desktop-Attachment-Window';
|
||||
const ENCODED_SEPARATOR_PATTERN = /%(?:2e|2f|5c)/iu;
|
||||
const VERSION_PREFIX_PATTERN = /^\/v[0-9]+(?=\/)/u;
|
||||
const ATTACHMENT_ROUTE_PATTERN = /^\/channels\/[0-9]+\/attachments\/[0-9]+\/[^/]/u;
|
||||
const READ_METHODS: ReadonlySet<string> = new Set(['GET', 'HEAD']);
|
||||
const TRAILING_SLASHES_PATTERN = /\/+$/u;
|
||||
|
||||
interface ApiAttachmentGrant {
|
||||
readonly origin: string;
|
||||
readonly basePath: string;
|
||||
readonly token: string;
|
||||
}
|
||||
|
||||
export interface ApiAttachmentRequest {
|
||||
readonly topFrameWebContentsId: number | null;
|
||||
readonly method: string;
|
||||
readonly url: string;
|
||||
}
|
||||
|
||||
function parseHttpURL(value: string): URL | null {
|
||||
try {
|
||||
const url = new URL(value);
|
||||
if (url.protocol !== 'https:' && url.protocol !== 'http:') return null;
|
||||
if (url.username !== '' || url.password !== '') return null;
|
||||
return url;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function isAttachmentTarget(grant: ApiAttachmentGrant, method: string, value: string): boolean {
|
||||
if (!READ_METHODS.has(method.toUpperCase())) return false;
|
||||
const url = parseHttpURL(value);
|
||||
if (url === null || url.origin !== grant.origin) return false;
|
||||
if (!url.pathname.startsWith(`${grant.basePath}/`)) return false;
|
||||
const routePath = url.pathname.slice(grant.basePath.length).replace(VERSION_PREFIX_PATTERN, '');
|
||||
return ATTACHMENT_ROUTE_PATTERN.test(routePath) && !ENCODED_SEPARATOR_PATTERN.test(routePath);
|
||||
}
|
||||
|
||||
function hasAuthorizationHeader(headers: Record<string, string>): boolean {
|
||||
return Object.keys(headers).some((name) => name.toLowerCase() === AUTHORIZATION_HEADER.toLowerCase());
|
||||
}
|
||||
|
||||
export class ApiAttachmentAuthorization {
|
||||
private readonly grants = new Map<number, ApiAttachmentGrant>();
|
||||
private readonly childWindows = new Map<number, number>();
|
||||
private readonly listeners = new Set<() => void>();
|
||||
|
||||
grant(webContentsId: number, apiEndpoint: string, token: string): boolean {
|
||||
const url = parseHttpURL(apiEndpoint);
|
||||
if (url === null) return false;
|
||||
this.grants.set(webContentsId, {
|
||||
origin: url.origin,
|
||||
basePath: url.pathname.replace(TRAILING_SLASHES_PATTERN, ''),
|
||||
token,
|
||||
});
|
||||
this.notify();
|
||||
return true;
|
||||
}
|
||||
|
||||
revoke(webContentsId: number): void {
|
||||
if (this.grants.delete(webContentsId)) this.notify();
|
||||
}
|
||||
|
||||
linkChildWindow(childWebContentsId: number, parentWebContentsId: number): void {
|
||||
this.childWindows.set(childWebContentsId, parentWebContentsId);
|
||||
}
|
||||
|
||||
unlinkChildWindow(childWebContentsId: number): void {
|
||||
this.childWindows.delete(childWebContentsId);
|
||||
}
|
||||
|
||||
hasGrants(): boolean {
|
||||
return this.grants.size > 0;
|
||||
}
|
||||
|
||||
hasGrantFor(webContentsId: number): boolean {
|
||||
return this.grantFor(webContentsId) !== undefined;
|
||||
}
|
||||
|
||||
urlPatterns(): Array<string> {
|
||||
const patterns = new Set<string>();
|
||||
for (const grant of this.grants.values()) {
|
||||
const url = new URL(grant.origin);
|
||||
patterns.add(`${url.protocol}//${url.hostname}/*`);
|
||||
}
|
||||
return [...patterns].sort();
|
||||
}
|
||||
|
||||
onChange(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => {
|
||||
this.listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
applyRequestHeaders(request: ApiAttachmentRequest, headers: Record<string, string>): Record<string, string> {
|
||||
if (request.topFrameWebContentsId === null || hasAuthorizationHeader(headers)) return headers;
|
||||
const grant = this.grantFor(request.topFrameWebContentsId);
|
||||
if (grant === undefined || !isAttachmentTarget(grant, request.method, request.url)) return headers;
|
||||
return {...headers, [AUTHORIZATION_HEADER]: grant.token};
|
||||
}
|
||||
|
||||
authorizationForProxiedRequest(windowHeader: string | null, method: string, url: string): string | null {
|
||||
if (windowHeader === null || !/^[0-9]+$/u.test(windowHeader)) return null;
|
||||
const grant = this.grantFor(Number(windowHeader));
|
||||
return grant !== undefined && isAttachmentTarget(grant, method, url) ? grant.token : null;
|
||||
}
|
||||
|
||||
private grantFor(webContentsId: number): ApiAttachmentGrant | undefined {
|
||||
const parentId = this.childWindows.get(webContentsId);
|
||||
return this.grants.get(webContentsId) ?? (parentId === undefined ? undefined : this.grants.get(parentId));
|
||||
}
|
||||
|
||||
private notify(): void {
|
||||
for (const listener of this.listeners) listener();
|
||||
}
|
||||
}
|
||||
|
||||
let instance: ApiAttachmentAuthorization | null = null;
|
||||
|
||||
export function getApiAttachmentAuthorization(): ApiAttachmentAuthorization {
|
||||
instance ??= new ApiAttachmentAuthorization();
|
||||
return instance;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {getApiAttachmentAuthorization} from '@electron/main/ApiAttachmentAuthorization';
|
||||
import {isPrivilegedRendererDocumentSender} from '@electron/main/PrivilegedRendererDocuments';
|
||||
import {
|
||||
DESKTOP_API_ATTACHMENT_AUTHORIZATION_CHANNEL,
|
||||
readDesktopApiAttachmentAuthorization,
|
||||
} from '@fluxer/desktop_ipc/src/ApiAttachmentAuthorizationContract';
|
||||
import {ipcMain, type WebContents} from 'electron';
|
||||
|
||||
const watchedSenders = new WeakSet<WebContents>();
|
||||
|
||||
function onDocumentGone(contents: WebContents, forget: () => void): void {
|
||||
contents.once('destroyed', forget);
|
||||
contents.on('render-process-gone', forget);
|
||||
contents.on(
|
||||
'did-start-navigation',
|
||||
(_event: Electron.Event, _url: string, isSameDoc: boolean, isMainFrame: boolean) => {
|
||||
if (isMainFrame && !isSameDoc) forget();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function watchSender(sender: WebContents): void {
|
||||
if (watchedSenders.has(sender)) return;
|
||||
watchedSenders.add(sender);
|
||||
const id = sender.id;
|
||||
const authorization = getApiAttachmentAuthorization();
|
||||
onDocumentGone(sender, () => authorization.revoke(id));
|
||||
sender.on('did-create-window', (window) => {
|
||||
const childId = window.webContents.id;
|
||||
authorization.linkChildWindow(childId, id);
|
||||
onDocumentGone(window.webContents, () => authorization.unlinkChildWindow(childId));
|
||||
});
|
||||
}
|
||||
|
||||
export function registerApiAttachmentAuthorizationIpc(): void {
|
||||
ipcMain.handle(DESKTOP_API_ATTACHMENT_AUTHORIZATION_CHANNEL, (event, value: unknown): boolean => {
|
||||
if (!isPrivilegedRendererDocumentSender(event)) return false;
|
||||
const request = readDesktopApiAttachmentAuthorization(value);
|
||||
const authorization = getApiAttachmentAuthorization();
|
||||
if (request === null || request.token === null) {
|
||||
authorization.revoke(event.sender.id);
|
||||
return false;
|
||||
}
|
||||
watchSender(event.sender);
|
||||
if (authorization.grant(event.sender.id, request.apiEndpoint, request.token)) return true;
|
||||
authorization.revoke(event.sender.id);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
import {DESKTOP_APP_SCHEME, STABLE_APP_URL} from '@electron/common/Constants';
|
||||
import {isPlainRecord} from '@electron/common/PlainRecord';
|
||||
import {type ApiAttachmentAuthorization, ATTACHMENT_WINDOW_HEADER} from '@electron/main/ApiAttachmentAuthorization';
|
||||
import {parseLocalAppRuntimeRoute} from '@electron/main/LocalAppRuntimePlans';
|
||||
import {isLocalAppURL} from '@electron/main/LocalAppURL';
|
||||
import {LOCAL_APP_REMOTE_PROXY_PATH_PREFIX} from '@fluxer/desktop_ipc/src/LocalAppRouteContract';
|
||||
|
||||
const SUB_FRAME_RESOURCE_TYPE = 'subFrame';
|
||||
|
||||
@@ -25,6 +29,7 @@ interface DesktopLocalAppRequestAuthorization {
|
||||
interface DesktopRuntimeSecurityDependencies {
|
||||
readonly logger: DesktopRuntimeSecurityLogger;
|
||||
readonly localAppAuthorization?: DesktopLocalAppRequestAuthorization;
|
||||
readonly attachmentAuthorization?: ApiAttachmentAuthorization;
|
||||
}
|
||||
|
||||
class InvalidWebRequestDetailsError extends TypeError {
|
||||
@@ -44,11 +49,23 @@ class InvalidWebRequestCallbackError extends TypeError {
|
||||
export class DesktopRuntimeSecurity {
|
||||
private readonly logger: DesktopRuntimeSecurityLogger;
|
||||
private readonly localAppAuthorization: DesktopLocalAppRequestAuthorization | null;
|
||||
private readonly attachmentAuthorization: ApiAttachmentAuthorization | null;
|
||||
private readonly installedSessions = new Set<Electron.Session>();
|
||||
|
||||
constructor(dependencies: DesktopRuntimeSecurityDependencies) {
|
||||
this.logger = dependencies.logger;
|
||||
this.localAppAuthorization = dependencies.localAppAuthorization ?? null;
|
||||
this.attachmentAuthorization = dependencies.attachmentAuthorization ?? null;
|
||||
this.attachmentAuthorization?.onChange(() => {
|
||||
for (const session of [...this.installedSessions]) {
|
||||
try {
|
||||
this.listen(session);
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to update desktop runtime security listeners', error);
|
||||
this.uninstall(session);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
install(session: Electron.Session): void {
|
||||
@@ -57,18 +74,27 @@ export class DesktopRuntimeSecurity {
|
||||
}
|
||||
this.installedSessions.add(session);
|
||||
try {
|
||||
session.webRequest.onBeforeSendHeaders(
|
||||
{urls: DESKTOP_RUNTIME_SECURITY_REQUEST_URL_PATTERNS},
|
||||
(details: unknown, callback: unknown) => {
|
||||
this.settleRequestHeaders(details, callback);
|
||||
},
|
||||
);
|
||||
this.listen(session);
|
||||
} catch (installationError) {
|
||||
this.uninstall(session);
|
||||
throw installationError;
|
||||
}
|
||||
}
|
||||
|
||||
private listen(session: Electron.Session): void {
|
||||
session.webRequest.onBeforeSendHeaders(
|
||||
{
|
||||
urls: [
|
||||
...DESKTOP_RUNTIME_SECURITY_REQUEST_URL_PATTERNS,
|
||||
...(this.attachmentAuthorization?.urlPatterns() ?? []),
|
||||
],
|
||||
},
|
||||
(details: unknown, callback: unknown) => {
|
||||
this.settleRequestHeaders(details, callback);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
private uninstall(session: Electron.Session): void {
|
||||
this.installedSessions.delete(session);
|
||||
try {
|
||||
@@ -94,7 +120,38 @@ export class DesktopRuntimeSecurity {
|
||||
}
|
||||
|
||||
private rewriteRequestHeaders(detailsValue: unknown): Record<string, unknown> {
|
||||
return injectEmbedFrameReferrer(detailsValue, this.authorizeLocalAppRequest(detailsValue));
|
||||
return injectEmbedFrameReferrer(
|
||||
detailsValue,
|
||||
this.authorizeAttachmentRequest(detailsValue, this.authorizeLocalAppRequest(detailsValue)),
|
||||
);
|
||||
}
|
||||
|
||||
private authorizeAttachmentRequest(
|
||||
detailsValue: unknown,
|
||||
settlement: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
const authorization = this.attachmentAuthorization;
|
||||
if (authorization == null || !authorization.hasGrants()) {
|
||||
return settlement;
|
||||
}
|
||||
const details = readWebRequestDetails(detailsValue);
|
||||
const headers = readRequestHeaderRecord(settlement.requestHeaders ?? details.requestHeadersValue);
|
||||
if (headers == null) {
|
||||
return settlement;
|
||||
}
|
||||
const topFrameWebContentsId = readTopFrameWebContentsId(detailsValue, details.resourceType);
|
||||
if (isRemoteProxyURL(details.url)) {
|
||||
const next = withoutHeader(headers, ATTACHMENT_WINDOW_HEADER);
|
||||
if (topFrameWebContentsId !== null && authorization.hasGrantFor(topFrameWebContentsId)) {
|
||||
next[ATTACHMENT_WINDOW_HEADER] = String(topFrameWebContentsId);
|
||||
}
|
||||
return {...settlement, requestHeaders: next};
|
||||
}
|
||||
const next = authorization.applyRequestHeaders(
|
||||
{topFrameWebContentsId, method: readRequestMethod(detailsValue), url: details.url},
|
||||
headers,
|
||||
);
|
||||
return next === headers ? settlement : {...settlement, requestHeaders: next};
|
||||
}
|
||||
|
||||
private authorizeLocalAppRequest(detailsValue: unknown): Record<string, unknown> {
|
||||
@@ -175,6 +232,46 @@ interface WebRequestDetails {
|
||||
readonly requestHeadersValue: unknown;
|
||||
}
|
||||
|
||||
function isRemoteProxyURL(value: string): boolean {
|
||||
return isLocalAppURL(value) && parseLocalAppRuntimeRoute(value, LOCAL_APP_REMOTE_PROXY_PATH_PREFIX) != null;
|
||||
}
|
||||
|
||||
function withoutHeader(headers: Record<string, string>, name: string): Record<string, string> {
|
||||
const lowerName = name.toLowerCase();
|
||||
const next: Record<string, string> = Object.create(null) as Record<string, string>;
|
||||
for (const [headerName, value] of Object.entries(headers)) {
|
||||
if (headerName.toLowerCase() !== lowerName) {
|
||||
next[headerName] = value;
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
function readRequestMethod(detailsValue: unknown): string {
|
||||
return isPlainRecord(detailsValue) && typeof detailsValue.method === 'string' ? detailsValue.method : '';
|
||||
}
|
||||
|
||||
function readTopFrameWebContentsId(detailsValue: unknown, resourceType: string): number | null {
|
||||
if (!isPlainRecord(detailsValue) || resourceType === SUB_FRAME_RESOURCE_TYPE) {
|
||||
return null;
|
||||
}
|
||||
const webContentsId = detailsValue.webContentsId;
|
||||
const frame = detailsValue.frame;
|
||||
if (
|
||||
typeof webContentsId !== 'number' ||
|
||||
!Number.isInteger(webContentsId) ||
|
||||
frame == null ||
|
||||
typeof frame !== 'object'
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Reflect.get(frame, 'parent') == null ? webContentsId : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function readWebRequestDetails(value: unknown): WebRequestDetails {
|
||||
if (!isPlainRecord(value)) {
|
||||
throw new InvalidWebRequestDetailsError();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {createChildLogger} from '@electron/common/Logger';
|
||||
import {type ApiAttachmentAuthorization, ATTACHMENT_WINDOW_HEADER} from '@electron/main/ApiAttachmentAuthorization';
|
||||
import {type DesktopLocalAppProxyClient, emitLocalAppUploadFailure} from '@electron/main/LocalAppProxyClient';
|
||||
import {
|
||||
buildProxyResponseHeaders,
|
||||
@@ -30,6 +31,7 @@ interface LocalAppRemoteResourceRuntimePlans {
|
||||
interface DesktopLocalAppRemoteResourceRequestHandlerDependencies {
|
||||
readonly proxyClient: DesktopLocalAppProxyClient;
|
||||
readonly runtimePlans: LocalAppRemoteResourceRuntimePlans;
|
||||
readonly attachmentAuthorization: ApiAttachmentAuthorization;
|
||||
}
|
||||
|
||||
interface ForwardRemoteResourceRequest {
|
||||
@@ -51,10 +53,12 @@ function isRefusedMidFileRead(request: Request, response: Response): boolean {
|
||||
export class DesktopLocalAppRemoteResourceRequestHandler {
|
||||
private readonly proxyClient: DesktopLocalAppProxyClient;
|
||||
private readonly runtimePlans: LocalAppRemoteResourceRuntimePlans;
|
||||
private readonly attachmentAuthorization: ApiAttachmentAuthorization;
|
||||
|
||||
public constructor(dependencies: DesktopLocalAppRemoteResourceRequestHandlerDependencies) {
|
||||
this.proxyClient = dependencies.proxyClient;
|
||||
this.runtimePlans = dependencies.runtimePlans;
|
||||
this.attachmentAuthorization = dependencies.attachmentAuthorization;
|
||||
}
|
||||
|
||||
public async handle(request: Request, signal: AbortSignal): Promise<Response> {
|
||||
@@ -94,12 +98,20 @@ export class DesktopLocalAppRemoteResourceRequestHandler {
|
||||
});
|
||||
}
|
||||
const uploadId = readLocalAppUploadId(request.headers);
|
||||
const requestHeaders = buildRemoteResourceRequestHeaders(request.headers);
|
||||
requestHeaders.delete(ATTACHMENT_WINDOW_HEADER);
|
||||
const authorization = this.attachmentAuthorization.authorizationForProxiedRequest(
|
||||
request.headers.get(ATTACHMENT_WINDOW_HEADER),
|
||||
request.method,
|
||||
targetURL,
|
||||
);
|
||||
if (authorization !== null) requestHeaders.set('Authorization', authorization);
|
||||
let response: Response | null = null;
|
||||
try {
|
||||
response = await this.proxyClient.fetch({
|
||||
targetURL,
|
||||
method: request.method,
|
||||
headers: buildRemoteResourceRequestHeaders(request.headers),
|
||||
headers: requestHeaders,
|
||||
body: request.body,
|
||||
signal,
|
||||
acceptEncoding: null,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import {createChildLogger} from '@electron/common/Logger';
|
||||
import {getApiAttachmentAuthorization} from '@electron/main/ApiAttachmentAuthorization';
|
||||
import {DesktopLocalAppAPIProxyRequestHandler} from '@electron/main/LocalAppAPIProxyRequestHandler';
|
||||
import {DesktopLocalAppFileRequestHandler, type LocalAppIndexContext} from '@electron/main/LocalAppFileRequestHandler';
|
||||
import type {DesktopLocalAppFiles} from '@electron/main/LocalAppFileResolver';
|
||||
@@ -74,6 +75,7 @@ export class DesktopLocalAppRequestHandler {
|
||||
this.remoteResourceRequestHandler = new DesktopLocalAppRemoteResourceRequestHandler({
|
||||
proxyClient: dependencies.resourceProxyClient,
|
||||
runtimePlans: dependencies.runtimePlans,
|
||||
attachmentAuthorization: getApiAttachmentAuthorization(),
|
||||
});
|
||||
this.fileRequestHandler = new DesktopLocalAppFileRequestHandler({
|
||||
files: dependencies.files,
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
import {getDesktopWindowBehaviorSettings, getLaunchProductName, getPrebootTheme} from '@electron/common/DesktopConfig';
|
||||
import {createChildLogger} from '@electron/common/Logger';
|
||||
import type {DesktopWindowBehaviorSettings} from '@electron/common/Types';
|
||||
import {getApiAttachmentAuthorization} from '@electron/main/ApiAttachmentAuthorization';
|
||||
import {createAppLoadRetry} from '@electron/main/AppLoadRetry';
|
||||
import {readBootFrameInputs, replayBootFrameInputs} from '@electron/main/BootFrameInputReplay';
|
||||
import {prepareBootStateReload, requestBootStateFlush} from '@electron/main/BootState';
|
||||
@@ -56,6 +57,7 @@ const logger = createChildLogger('Window');
|
||||
const runtimeSecurity = new DesktopRuntimeSecurity({
|
||||
logger: createChildLogger('DesktopRuntimeSecurity'),
|
||||
localAppAuthorization: getDesktopLocalAppAuthorization(),
|
||||
attachmentAuthorization: getApiAttachmentAuthorization(),
|
||||
});
|
||||
const LIVE_RESIZE_IDLE_MS = 400;
|
||||
const MAXIMIZED_REVEAL_REPAINT_TIMEOUT_MS = 250;
|
||||
|
||||
@@ -8,6 +8,7 @@ import {errorMessage} from '@electron/common/ErrorMessage';
|
||||
import {configureUserDataPath} from '@electron/common/UserDataPath';
|
||||
import {isAccountEngineAvailable} from '@electron/main/AccountEngineHost';
|
||||
import {startAccountEngine, stopAccountEngine} from '@electron/main/AccountEngineIpc';
|
||||
import {registerApiAttachmentAuthorizationIpc} from '@electron/main/ApiAttachmentAuthorizationIpc';
|
||||
import {
|
||||
APP_STORE_ADDON_PACKAGE,
|
||||
createAppStoreBoundary,
|
||||
@@ -720,6 +721,11 @@ export async function startMainApp(
|
||||
} catch (error) {
|
||||
log.error('[Init] Failed to register IPC handlers:', error);
|
||||
}
|
||||
try {
|
||||
runStartupPhase('api-attachment-authorization', registerApiAttachmentAuthorizationIpc);
|
||||
} catch (error) {
|
||||
log.error('[Init] Failed to register the attachment authorization handler:', error);
|
||||
}
|
||||
try {
|
||||
runStartupPhase('autostart-handlers', registerAutostartHandlers);
|
||||
} catch (error) {
|
||||
|
||||
@@ -75,6 +75,7 @@ import {applyLegacyRawLocalStorage} from '@electron/preload/LegacyRawStorageSeed
|
||||
import {createLocalAppPreloadAPI} from '@electron/preload/LocalAppPreload';
|
||||
import {createNativeGatewayPreloadAPI} from '@electron/preload/NativeGatewayPreload';
|
||||
import {createOtherPlatformTemplatePreloadAPI} from '@electron/preload/OtherPlatformTemplatePreload';
|
||||
import {DESKTOP_API_ATTACHMENT_AUTHORIZATION_CHANNEL} from '@fluxer/desktop_ipc/src/ApiAttachmentAuthorizationContract';
|
||||
import type {DesktopCapabilityManifest} from '@fluxer/desktop_ipc/src/CapabilityManifest';
|
||||
import {DESKTOP_CAPABILITY_MANIFEST_CHANNEL} from '@fluxer/desktop_ipc/src/CapabilityManifest';
|
||||
import {DESKTOP_DIRECT_MESSAGES_AVAILABLE_CHANNEL} from '@fluxer/desktop_ipc/src/DirectMessagesContract';
|
||||
@@ -427,6 +428,8 @@ const api: ElectronAPI = {
|
||||
reportLastRoute: (routePath: string): void => {
|
||||
ipcRenderer.send(DESKTOP_LAST_ROUTE_CHANNEL, routePath);
|
||||
},
|
||||
setApiAttachmentAuthorization: (apiEndpoint: string, token: string | null): Promise<boolean> =>
|
||||
ipcRenderer.invoke(DESKTOP_API_ATTACHMENT_AUTHORIZATION_CHANNEL, {apiEndpoint, token}) as Promise<boolean>,
|
||||
reportInstanceProductName: (instanceKey: string, productName: string): void => {
|
||||
ipcRenderer.send(DESKTOP_INSTANCE_PRODUCT_NAME_CHANNEL, {instanceKey, productName});
|
||||
},
|
||||
|
||||
@@ -567,9 +567,12 @@ Attachment retention overrides and the values in use.
|
||||
| Field | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| attachment_decay<sup>1</sup> | object | Nullable overrides, plus an `effective` object with the values in use |
|
||||
| proxy_attachments_through_api<sup>2</sup> | boolean | Whether [attachment URLs point at the API](/http-api/messages/#attachment-urls-on-the-api) |
|
||||
|
||||
<sup>1</sup> The keys are `enabled`, `min_size_mb`, `max_size_mb`, `max_eligible_size_mb`, `min_lifetime_days`, `max_lifetime_days`, `curve` (0-1), `renew_threshold_days` and `renew_window_days`. Null uses the default
|
||||
|
||||
<sup>2</sup> Data harvest links and admin views keep Media Proxy URLs. URLs handed out before a change keep working until they expire, so run the Media Proxy with the [signature mode](/media-proxy/overview/#signed-attachment-urls) set to `enforce`. A cache in front of the API must not store these files, and access logs should leave out the `mt` query parameter. Clients pick up a change when they next load the instance discovery document, and a web app tab opened before the change needs a reload when the API has its own domain. No URL holds a token for the whole account. The web app loads images with the session header and shows them from memory, and the desktop app adds the header itself. Videos, audio, downloads and files opened in the browser use a token that opens one file for 5 minutes. Copy actions in Fluxer leave the token out. Turning the setting off again breaks the API URLs handed out while it was on. Messages show their files again once clients load them again, but favorite GIFs saved from attachments while it was on stay broken
|
||||
|
||||
Fluxer drops an override that would make a maximum smaller than its minimum.
|
||||
|
||||
## Branding asset kinds
|
||||
@@ -717,7 +720,7 @@ Merges the body into the saved configuration and returns the [instance configura
|
||||
| registration? | object | `mode`, `admin_registration_urls_enabled` and `allowed_email_domains` |
|
||||
| app_public?<sup>2</sup> | object | `branding`, `setup`, `legal` and `registration` objects, each merged field by field |
|
||||
| integrations?<sup>3</sup> | object | `gif`, `youtube`, `email` and `bluesky` objects, `bluesky.keys` and `connection_providers` |
|
||||
| media? | object | `attachment_decay` overrides, null to restore a default |
|
||||
| media? | object | `attachment_decay` overrides, null to restore a default, and `proxy_attachments_through_api` |
|
||||
| policy? | [instance policy update](#instance-policy-update-structure) object | Community, direct message, premium and gating policy |
|
||||
| billing?<sup>4</sup> | object | [Instance billing](#instance-billing-object) fields, plus `stripe_secret_key` and `stripe_webhook_secret` |
|
||||
| distribution_pricing?<sup>6</sup> | object | `rules`, each with `id`, `active`, `rate_bps` and `products` from the [distribution rule](#distribution-rule-object) |
|
||||
|
||||
@@ -148,6 +148,7 @@ Instance-wide switches to check before you offer a feature.
|
||||
| stripe_serviceable | boolean | Whether existing Stripe subscriptions can be managed and billed |
|
||||
| self_hosted | boolean | Whether the instance is self-hosted |
|
||||
| presigned_attachment_uploads | boolean | Whether you can request presigned attachment upload URLs |
|
||||
| proxy_attachments_through_api? | boolean | Whether [attachment URLs point at the API](/http-api/messages/#attachment-urls-on-the-api), false when missing |
|
||||
| emails_enabled | boolean | Whether the instance sends email |
|
||||
| phone_verification_enabled | boolean | Deprecated. Always false |
|
||||
| discovery_enabled? | boolean | Whether the Explore community directory is available, true when missing |
|
||||
|
||||
@@ -41,7 +41,7 @@ A meme object is one saved file. You can change only `name`, `alt_text` and `tag
|
||||
| placeholder | ?string | The thumbhash placeholder recorded at save time |
|
||||
| nsfw | boolean | Whether the media is flagged as explicit, from the source message or the media scan |
|
||||
|
||||
<sup>1</sup> A new [signed attachment URL](/http-api/messages/#signed-attachment-urls) on every read
|
||||
<sup>1</sup> A new [signed attachment URL](/http-api/messages/#signed-attachment-urls) on every read, or an [attachment URL on the API](/http-api/messages/#attachment-urls-on-the-api) that only you can open
|
||||
|
||||
<sup>2</sup> Both are set or both are null. The value is `klipy` or `tenor`
|
||||
|
||||
@@ -286,7 +286,7 @@ The rate limit is 30 requests per 10 seconds, on the `favorite_meme:delete` buck
|
||||
|
||||
<RouteHeader method="POST" path="/v1/users/@me/favorite-gifs/resolve" />
|
||||
|
||||
Looks up GIF URLs and returns a [resolved GIF entry](#resolved-gif-entry-object) for each one without saving anything. A URL Fluxer can't find still gets an entry, with an empty `media` map.
|
||||
Looks up GIF URLs and returns a [resolved GIF entry](#resolved-gif-entry-object) for each one without saving anything. A URL Fluxer can't find still gets an entry, with an empty `media` map. When [attachment URLs point at the API](/http-api/messages/#attachment-urls-on-the-api), an attachment you can't read gets that empty entry too, and the entries for attachments you can read point at the API.
|
||||
|
||||
### Request headers
|
||||
|
||||
|
||||
@@ -161,6 +161,18 @@ A URL is valid for 24 hours from `is`. Keep the four parameters as they are, and
|
||||
|
||||
A [data harvest](/http-api/users/data-harvest/) archive links each attachment with `uc=dp` and `ex=0`. These URLs don't expire and work until the instance removes the secret that signed them.
|
||||
|
||||
### Attachment URLs on the API
|
||||
|
||||
When [`proxy_attachments_through_api`](/http-api/instance/#instance-features-object) is true, `url` and `proxy_url` point at [Get attachment file](#get-attachment-file) instead of the Media Proxy, and have no signature.
|
||||
|
||||
```text
|
||||
/v1/channels/{channel_id}/attachments/{attachment_id}/{filename}?mid={message_id}
|
||||
```
|
||||
|
||||
Fetching one needs the same access as reading the message. Send your token in the `Authorization` header. When a request can't send a header, such as a video player, a download or a link opened in the browser, add `mt` from [Create attachment token](#create-attachment-token), which opens only that one file for a few minutes. Keep `mid` as issued. A link copied for someone else should leave out `mt`.
|
||||
|
||||
An embed image uploaded with `attachment://` names its own message in `mid`. Any other embed URL that points to a stored file names the channel of that file and has no `mid`, so it opens only for people who can read the message the file came from. Fluxer doesn't build link previews for attachment URLs in message text while the setting is on.
|
||||
|
||||
### Example
|
||||
|
||||
```json
|
||||
@@ -377,7 +389,7 @@ An embed is a link preview Fluxer builds, or a rich embed you send.
|
||||
| placeholder? | string | An encoded placeholder image |
|
||||
| flags? | integer | `CONTAINS_EXPLICIT_MEDIA` and `IS_ANIMATED` from [attachment flags](#attachment-flags) |
|
||||
|
||||
Fluxer signs every embed URL that points to an attachment on this instance, the same way as [attachment URLs](#signed-attachment-urls).
|
||||
Fluxer signs every embed URL that points to an attachment on this instance, the same way as [attachment URLs](#signed-attachment-urls). When [attachment URLs point at the API](#attachment-urls-on-the-api), these URLs point at the API instead.
|
||||
|
||||
## Embed footer object
|
||||
|
||||
@@ -863,8 +875,75 @@ Signs 1 to 50 attachment URLs again. A refreshed URL is valid until its `ex`, wh
|
||||
| --- | --- | --- |
|
||||
| 200 | response body | The URLs were signed |
|
||||
|
||||
When [`proxy_attachments_through_api`](/http-api/instance/#instance-features-object) is true, a URL of this instance comes back as an [attachment URL on the API](#attachment-urls-on-the-api) with no `mid`.
|
||||
|
||||
The rate limit is 60 requests per 10 seconds, on the `attachment:refresh_urls` bucket.
|
||||
|
||||
## Get attachment file
|
||||
|
||||
<RouteHeader method="GET" path="/v1/channels/{channel_id}/attachments/{attachment_id}/{filename}" bot />
|
||||
|
||||
Streams an attachment file. Only works when [`proxy_attachments_through_api`](/http-api/instance/#instance-features-object) is true, and returns 404 otherwise. You need to be able to [read the message](#access-and-history) the file belongs to. `HEAD` and `Range` work as they do on the Media Proxy.
|
||||
|
||||
### Query string
|
||||
|
||||
| Field | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| mid? | snowflake | The message the file belongs to, as issued in the URL |
|
||||
| mt? | string | A token for this file from [Create attachment token](#create-attachment-token), for a request without an `Authorization` header |
|
||||
| width? | integer | Resize to this width (1-8,192) |
|
||||
| height? | integer | Resize to this height (1-8,192) |
|
||||
| format? | string | A [transformation](/media-proxy/transformations/) output format |
|
||||
| quality? | string | `low`, `high`, `lossless` or `auto` |
|
||||
| fmt? | string | The same as `format` |
|
||||
| animated? | boolean | Whether to keep animation |
|
||||
| effort? | integer | Encoder effort, 0 to 9 |
|
||||
| download? | boolean | Whether to send the file as a download |
|
||||
|
||||
### Response
|
||||
|
||||
| Status | Body | Condition |
|
||||
| --- | --- | --- |
|
||||
| 200 | file | The file |
|
||||
| 206 | file | Part of the file, for a `Range` request |
|
||||
| 400 | empty | A query value is out of range |
|
||||
| 401 | empty | `mt` is expired or invalid, or was made for another file |
|
||||
| 404 | empty | The file doesn't exist or you can't read its message |
|
||||
| 416 | empty | The range is outside the file |
|
||||
|
||||
The response has `Cache-Control: private`, and `Cache-Control: private, no-store` for a request with `mt`. The rate limit is 600 requests per 10 seconds, on the `channel:attachment:read` bucket.
|
||||
|
||||
## Create attachment token
|
||||
|
||||
<RouteHeader method="POST" path="/v1/users/@me/attachment-token" />
|
||||
|
||||
Creates a token for the `mt` query parameter of one [attachment URL on the API](#attachment-urls-on-the-api). Only works when [`proxy_attachments_through_api`](/http-api/instance/#instance-features-object) is true, and returns 404 otherwise. Bot and OAuth2 tokens can't use this route.
|
||||
|
||||
### JSON body
|
||||
|
||||
| Field | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| url | string | The attachment URL the token is for |
|
||||
|
||||
The token opens only the file in `url`, and only for 5 minutes. Get a new one before `expires_at` if a player still needs the file. It stops working when the session it came from signs out, and requests with it fail within 15 seconds of losing access to the message.
|
||||
|
||||
### Response body
|
||||
|
||||
| Field | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| token | string | The value for `mt` |
|
||||
| expires_at | ISO8601 timestamp | When the token stops working |
|
||||
|
||||
### Response
|
||||
|
||||
| Status | Body | Condition |
|
||||
| --- | --- | --- |
|
||||
| 200 | response body | The token |
|
||||
| 400 | error | `url` is missing |
|
||||
| 404 | empty | The setting is off, `url` isn't an attachment URL on the API, or you can't read its file |
|
||||
|
||||
The rate limit is 120 requests per 10 seconds, on the `user:attachment_token` bucket.
|
||||
|
||||
## Create message
|
||||
|
||||
<RouteHeader method="POST" path="/v1/channels/{channel_id}/messages" bot />
|
||||
|
||||
@@ -7,17 +7,35 @@
|
||||
|
||||
-define(MAX_IMAGE_URL_BYTES, 1024).
|
||||
|
||||
-type own_link() :: {binary(), binary()} | none.
|
||||
|
||||
-spec extract(map()) -> binary() | undefined.
|
||||
extract(MessageData) ->
|
||||
bounded_image_url(resolve_image_url(MessageData)).
|
||||
|
||||
-spec resolve_image_url(map()) -> binary() | undefined.
|
||||
resolve_image_url(MessageData) ->
|
||||
case extract_attachment_image_url(maps:get(<<"attachments">>, MessageData, [])) of
|
||||
undefined -> extract_embed_image_url(maps:get(<<"embeds">>, MessageData, []));
|
||||
Own = own_link(MessageData),
|
||||
case extract_attachment_image_url(maps:get(<<"attachments">>, MessageData, []), Own) of
|
||||
undefined -> extract_embed_image_url(maps:get(<<"embeds">>, MessageData, []), Own);
|
||||
ImageUrl -> ImageUrl
|
||||
end.
|
||||
|
||||
-spec own_link(map()) -> own_link().
|
||||
own_link(MessageData) ->
|
||||
ChannelId = maps:get(<<"channel_id">>, MessageData, undefined),
|
||||
MessageId = maps:get(<<"id">>, MessageData, undefined),
|
||||
case is_snowflake(ChannelId) andalso is_snowflake(MessageId) of
|
||||
true -> {ChannelId, MessageId};
|
||||
false -> none
|
||||
end.
|
||||
|
||||
-spec is_snowflake(term()) -> boolean().
|
||||
is_snowflake(Value) when is_binary(Value), byte_size(Value) > 0, byte_size(Value) =< 20 ->
|
||||
lists:all(fun(Char) -> Char >= $0 andalso Char =< $9 end, binary_to_list(Value));
|
||||
is_snowflake(_Value) ->
|
||||
false.
|
||||
|
||||
-spec bounded_image_url(binary() | undefined) -> binary() | undefined.
|
||||
bounded_image_url(ImageUrl) when
|
||||
is_binary(ImageUrl), byte_size(ImageUrl) =< ?MAX_IMAGE_URL_BYTES
|
||||
@@ -26,49 +44,49 @@ bounded_image_url(ImageUrl) when
|
||||
bounded_image_url(_ImageUrl) ->
|
||||
undefined.
|
||||
|
||||
-spec extract_attachment_image_url(term()) -> binary() | undefined.
|
||||
extract_attachment_image_url([Attachment | Rest]) when is_map(Attachment) ->
|
||||
case check_media_safe_url(Attachment) of
|
||||
-spec extract_attachment_image_url(term(), own_link()) -> binary() | undefined.
|
||||
extract_attachment_image_url([Attachment | Rest], Own) when is_map(Attachment) ->
|
||||
case check_media_safe_url(Attachment, Own) of
|
||||
{ok, Url} -> Url;
|
||||
unsafe -> extract_attachment_image_url(Rest)
|
||||
unsafe -> extract_attachment_image_url(Rest, Own)
|
||||
end;
|
||||
extract_attachment_image_url([_ | Rest]) ->
|
||||
extract_attachment_image_url(Rest);
|
||||
extract_attachment_image_url(_) ->
|
||||
extract_attachment_image_url([_ | Rest], Own) ->
|
||||
extract_attachment_image_url(Rest, Own);
|
||||
extract_attachment_image_url(_, _Own) ->
|
||||
undefined.
|
||||
|
||||
-spec extract_embed_image_url(term()) -> binary() | undefined.
|
||||
extract_embed_image_url([Embed | Rest]) when is_map(Embed) ->
|
||||
case first_embed_media_url(Embed) of
|
||||
undefined -> extract_embed_image_url(Rest);
|
||||
-spec extract_embed_image_url(term(), own_link()) -> binary() | undefined.
|
||||
extract_embed_image_url([Embed | Rest], Own) when is_map(Embed) ->
|
||||
case first_embed_media_url(Embed, Own) of
|
||||
undefined -> extract_embed_image_url(Rest, Own);
|
||||
Url -> Url
|
||||
end;
|
||||
extract_embed_image_url([_ | Rest]) ->
|
||||
extract_embed_image_url(Rest);
|
||||
extract_embed_image_url(_) ->
|
||||
extract_embed_image_url([_ | Rest], Own) ->
|
||||
extract_embed_image_url(Rest, Own);
|
||||
extract_embed_image_url(_, _Own) ->
|
||||
undefined.
|
||||
|
||||
-spec first_embed_media_url(map()) -> binary() | undefined.
|
||||
first_embed_media_url(Embed) ->
|
||||
-spec first_embed_media_url(map(), own_link()) -> binary() | undefined.
|
||||
first_embed_media_url(Embed, Own) ->
|
||||
Candidates = [
|
||||
maps:get(<<"image">>, Embed, undefined),
|
||||
maps:get(<<"thumbnail">>, Embed, undefined)
|
||||
],
|
||||
first_embed_media_url_from_candidates(Candidates).
|
||||
first_embed_media_url_from_candidates(Candidates, Own).
|
||||
|
||||
-spec first_embed_media_url_from_candidates(list()) -> binary() | undefined.
|
||||
first_embed_media_url_from_candidates([Media | Rest]) when is_map(Media) ->
|
||||
case check_media_safe_url(Media) of
|
||||
-spec first_embed_media_url_from_candidates(list(), own_link()) -> binary() | undefined.
|
||||
first_embed_media_url_from_candidates([Media | Rest], Own) when is_map(Media) ->
|
||||
case check_media_safe_url(Media, Own) of
|
||||
{ok, Url} -> Url;
|
||||
unsafe -> first_embed_media_url_from_candidates(Rest)
|
||||
unsafe -> first_embed_media_url_from_candidates(Rest, Own)
|
||||
end;
|
||||
first_embed_media_url_from_candidates([_ | Rest]) ->
|
||||
first_embed_media_url_from_candidates(Rest);
|
||||
first_embed_media_url_from_candidates([]) ->
|
||||
first_embed_media_url_from_candidates([_ | Rest], Own) ->
|
||||
first_embed_media_url_from_candidates(Rest, Own);
|
||||
first_embed_media_url_from_candidates([], _Own) ->
|
||||
undefined.
|
||||
|
||||
-spec check_media_safe_url(map()) -> {ok, binary()} | unsafe.
|
||||
check_media_safe_url(Media) ->
|
||||
-spec check_media_safe_url(map(), own_link()) -> {ok, binary()} | unsafe.
|
||||
check_media_safe_url(Media, Own) ->
|
||||
ProxyUrl = maps:get(<<"proxy_url">>, Media, undefined),
|
||||
FallbackUrl = maps:get(<<"url">>, Media, undefined),
|
||||
Url = push_utils:normalize_binary(
|
||||
@@ -83,11 +101,26 @@ check_media_safe_url(Media) ->
|
||||
maps:get(<<"flags">>, Media, 0)
|
||||
),
|
||||
Nsfw = maps:get(<<"nsfw">>, Media, false),
|
||||
case is_safe_media_url(Url, ContentType, Flags, Nsfw) of
|
||||
case
|
||||
is_safe_media_url(Url, ContentType, Flags, Nsfw) andalso not is_own_api_link(Url, Own)
|
||||
of
|
||||
true when is_binary(Url) -> {ok, Url};
|
||||
_ -> unsafe
|
||||
end.
|
||||
|
||||
-spec is_own_api_link(binary() | undefined, own_link()) -> boolean().
|
||||
is_own_api_link(Url, {ChannelId, MessageId}) when is_binary(Url) ->
|
||||
Pattern = <<
|
||||
"^https?://[^/?#]+(?:/[^?#]*)?/channels/",
|
||||
ChannelId/binary,
|
||||
"/attachments/[0-9]+/[^?#]+\\?(?:[^#]*&)?mid=",
|
||||
MessageId/binary,
|
||||
"(?:[&#]|$)"
|
||||
>>,
|
||||
re:run(Url, Pattern, [caseless, {capture, none}]) =:= match;
|
||||
is_own_api_link(_Url, _Own) ->
|
||||
false.
|
||||
|
||||
-spec is_safe_media_url(
|
||||
binary() | undefined,
|
||||
binary() | undefined,
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
%% SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
-module(push_image_url_tests).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-define(MEDIA_URL, <<"https://media.example.test/attachments/11/22/cat.png?ex=1&is=2&hm=3">>).
|
||||
-define(API_URL, <<"https://api.example.test/api/channels/11/attachments/22/cat.png?mid=33">>).
|
||||
-define(PLAIN_API_URL, <<"http://api.example.test/channels/11/attachments/22/cat.png?mid=33">>).
|
||||
|
||||
image(Url) ->
|
||||
#{
|
||||
<<"url">> => Url,
|
||||
<<"proxy_url">> => Url,
|
||||
<<"content_type">> => <<"image/png">>,
|
||||
<<"flags">> => 0
|
||||
}.
|
||||
|
||||
message(Fields) ->
|
||||
maps:merge(#{<<"channel_id">> => <<"11">>, <<"id">> => <<"33">>}, Fields).
|
||||
|
||||
media_proxy_attachment_test() ->
|
||||
?assertEqual(
|
||||
?MEDIA_URL, push_image_url:extract(message(#{<<"attachments">> => [image(?MEDIA_URL)]}))
|
||||
).
|
||||
|
||||
own_api_attachment_has_no_image_test() ->
|
||||
?assertEqual(
|
||||
undefined, push_image_url:extract(message(#{<<"attachments">> => [image(?API_URL)]}))
|
||||
),
|
||||
?assertEqual(
|
||||
undefined,
|
||||
push_image_url:extract(message(#{<<"attachments">> => [image(?PLAIN_API_URL)]}))
|
||||
),
|
||||
?assertEqual(
|
||||
undefined,
|
||||
push_image_url:extract(message(#{<<"embeds">> => [#{<<"image">> => image(?API_URL)}]}))
|
||||
).
|
||||
|
||||
own_api_attachment_falls_back_to_the_next_image_test() ->
|
||||
?assertEqual(
|
||||
?MEDIA_URL,
|
||||
push_image_url:extract(
|
||||
message(#{
|
||||
<<"attachments">> => [image(?API_URL)],
|
||||
<<"embeds">> => [#{<<"image">> => image(?MEDIA_URL)}]
|
||||
})
|
||||
)
|
||||
).
|
||||
|
||||
links_of_other_messages_keep_their_image_test() ->
|
||||
?assertEqual(
|
||||
?API_URL,
|
||||
push_image_url:extract(
|
||||
message(#{<<"id">> => <<"34">>, <<"attachments">> => [image(?API_URL)]})
|
||||
)
|
||||
),
|
||||
?assertEqual(
|
||||
?API_URL,
|
||||
push_image_url:extract(
|
||||
message(#{<<"channel_id">> => <<"12">>, <<"attachments">> => [image(?API_URL)]})
|
||||
)
|
||||
),
|
||||
?assertEqual(?API_URL, push_image_url:extract(#{<<"attachments">> => [image(?API_URL)]})),
|
||||
Url = <<"https://example.test/channels/11/attachments/22/cat.png">>,
|
||||
?assertEqual(Url, push_image_url:extract(message(#{<<"attachments">> => [image(Url)]}))).
|
||||
@@ -57,9 +57,11 @@ impl RouterService for MessagesRouter {
|
||||
include_hidden,
|
||||
threads_mask,
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
..
|
||||
} => Some(format!(
|
||||
"api-get:{channel_id}:{message_id}:{viewer_user_id}:{source_guild_id:?}:{message_history_cutoff_ms:?}:{can_read_message_history}:{media_endpoint}:{include_reactions:?}:{attachment_decay:?}:{nonce:?}:{tts:?}:{include_hidden}:{threads_mask}:{instance_name}"
|
||||
"api-get:{channel_id}:{message_id}:{viewer_user_id}:{source_guild_id:?}:{message_history_cutoff_ms:?}:{can_read_message_history}:{media_endpoint}:{include_reactions:?}:{attachment_decay:?}:{nonce:?}:{tts:?}:{include_hidden}:{threads_mask}:{instance_name}{}",
|
||||
attachment_api_base_suffix(attachment_api_base.as_deref())
|
||||
)),
|
||||
MessageRequest::BuildResponse { .. } => None,
|
||||
MessageRequest::BuildResponses { .. } => None,
|
||||
@@ -80,11 +82,70 @@ impl RouterService for MessagesRouter {
|
||||
threads_mask,
|
||||
exclude_types,
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
..
|
||||
} => Some(format!(
|
||||
"api-list:{channel_id}:{viewer_user_id}:{limit}:{before_id:?}:{after_id:?}:{around_id:?}:{source_guild_id:?}:{message_history_cutoff_ms:?}:{can_read_message_history}:{media_endpoint}:{include_reactions:?}:{attachment_decay:?}:{include_hidden}:{threads_mask}:{exclude_types:?}:{instance_name}"
|
||||
"api-list:{channel_id}:{viewer_user_id}:{limit}:{before_id:?}:{after_id:?}:{around_id:?}:{source_guild_id:?}:{message_history_cutoff_ms:?}:{can_read_message_history}:{media_endpoint}:{include_reactions:?}:{attachment_decay:?}:{include_hidden}:{threads_mask}:{exclude_types:?}:{instance_name}{}",
|
||||
attachment_api_base_suffix(attachment_api_base.as_deref())
|
||||
)),
|
||||
MessageRequest::ExtractMentions { .. } => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn attachment_api_base_suffix(attachment_api_base: Option<&str>) -> String {
|
||||
attachment_api_base
|
||||
.map(|base| format!(":{base}"))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn list_request(attachment_api_base: Option<&str>) -> MessageRequest {
|
||||
let mut request = json!({
|
||||
"op": "ListResponses",
|
||||
"channel_id": "11",
|
||||
"viewer_user_id": "22",
|
||||
"limit": 50,
|
||||
"before_id": null,
|
||||
"after_id": null,
|
||||
"around_id": null,
|
||||
"source_guild_id": null,
|
||||
"message_history_cutoff_ms": null,
|
||||
"can_read_message_history": true,
|
||||
"media_endpoint": "https://media.test",
|
||||
"media_proxy_secret_key": "",
|
||||
"include_reactions": null,
|
||||
"attachment_decay": null,
|
||||
});
|
||||
if let Some(base) = attachment_api_base {
|
||||
request["attachment_api_base"] = json!(base);
|
||||
}
|
||||
serde_json::from_value(request).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_request_without_an_api_base_keeps_its_cache_key_and_wire_form() {
|
||||
let request = list_request(None);
|
||||
assert_eq!(
|
||||
Some(
|
||||
"api-list:11:22:50:None:None:None:None:None:true:https://media.test:None:None:false:false:[]:Fluxer"
|
||||
.to_owned()
|
||||
),
|
||||
MessagesRouter::coalesce_key(&request)
|
||||
);
|
||||
let encoded = serde_json::to_value(&request).unwrap();
|
||||
assert!(encoded.get("attachment_api_base").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_api_base_never_shares_a_cache_key_with_media_urls() {
|
||||
assert_ne!(
|
||||
MessagesRouter::coalesce_key(&list_request(None)),
|
||||
MessagesRouter::coalesce_key(&list_request(Some("https://api.test/api")))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -341,6 +341,32 @@ struct ResponseBuildOptions {
|
||||
excluded_types: Vec<i32>,
|
||||
attachment_decay: bool,
|
||||
instance_name: String,
|
||||
attachment_api_base: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct AttachmentUrlScope {
|
||||
message_id: i64,
|
||||
channel_id: i64,
|
||||
storage_channel_id: i64,
|
||||
}
|
||||
|
||||
impl AttachmentUrlScope {
|
||||
fn for_message(message: &Message) -> Self {
|
||||
Self {
|
||||
message_id: message.message_id,
|
||||
channel_id: message.channel_id,
|
||||
storage_channel_id: attachment_storage_channel_id(message),
|
||||
}
|
||||
}
|
||||
|
||||
fn for_snapshot(message_id: i64, channel_id: i64) -> Self {
|
||||
Self {
|
||||
message_id,
|
||||
channel_id,
|
||||
storage_channel_id: channel_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseBuildOptions {
|
||||
@@ -1306,15 +1332,13 @@ impl<T: Transport> MessagesShard<T> {
|
||||
include_referenced_message: bool,
|
||||
) -> ApiMessageResponse {
|
||||
let author = self.resolve_author(message, options, context);
|
||||
let storage_channel_id = attachment_storage_channel_id(message);
|
||||
let scope = AttachmentUrlScope::for_message(message);
|
||||
let attachments = message
|
||||
.attachments
|
||||
.as_deref()
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.filter_map(|attachment| {
|
||||
self.map_attachment(storage_channel_id, attachment, options, context)
|
||||
})
|
||||
.filter_map(|attachment| self.map_attachment(scope, attachment, options, context))
|
||||
.collect();
|
||||
let embeds = if (message.flags.unwrap_or_default() & MESSAGE_FLAG_SUPPRESS_EMBEDS) == 0 {
|
||||
message
|
||||
@@ -1322,7 +1346,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
.as_deref()
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.map(|embed| self.map_embed(embed, options))
|
||||
.map(|embed| self.map_embed(embed, options, scope))
|
||||
.collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
@@ -1474,7 +1498,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
|
||||
fn map_attachment(
|
||||
&self,
|
||||
channel_id: i64,
|
||||
scope: AttachmentUrlScope,
|
||||
attachment: &MessageAttachment,
|
||||
options: &ResponseBuildOptions,
|
||||
context: &ResponseContext,
|
||||
@@ -1485,13 +1509,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
let expired =
|
||||
decay.is_some_and(|expires_at| expires_at.timestamp_millis() <= unix_millis());
|
||||
let url = (!expired).then(|| {
|
||||
attachment_cdn_url(
|
||||
channel_id,
|
||||
attachment_id,
|
||||
&filename,
|
||||
options,
|
||||
unix_seconds(),
|
||||
)
|
||||
scoped_attachment_url(scope, attachment_id, &filename, options, unix_seconds())
|
||||
});
|
||||
let content_type = attachment.content_type.clone().unwrap_or_else(|| {
|
||||
mime_guess::from_path(&filename)
|
||||
@@ -1526,6 +1544,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
&self,
|
||||
embed: &MessageEmbed,
|
||||
options: &ResponseBuildOptions,
|
||||
scope: AttachmentUrlScope,
|
||||
) -> ApiMessageEmbedResponse {
|
||||
let base = self.map_embed_child_like(
|
||||
embed.embed_type.clone(),
|
||||
@@ -1547,6 +1566,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
embed.html_width,
|
||||
embed.html_height,
|
||||
options,
|
||||
scope,
|
||||
);
|
||||
let children = embed.children.as_ref().map(|children| {
|
||||
children
|
||||
@@ -1572,6 +1592,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
child.html_width,
|
||||
child.html_height,
|
||||
options,
|
||||
scope,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
@@ -1604,45 +1625,54 @@ impl<T: Transport> MessagesShard<T> {
|
||||
html_width: Option<i32>,
|
||||
html_height: Option<i32>,
|
||||
options: &ResponseBuildOptions,
|
||||
scope: AttachmentUrlScope,
|
||||
) -> ApiMessageEmbedChildResponse {
|
||||
ApiMessageEmbedChildResponse {
|
||||
embed_type: embed_type.unwrap_or_else(|| "rich".to_owned()),
|
||||
title,
|
||||
description,
|
||||
url: url.map(|url| sign_own_url(&url, options)),
|
||||
url: url.map(|url| own_media_url(&url, options, scope, false)),
|
||||
timestamp: timestamp.map(epoch_millis_to_iso),
|
||||
color,
|
||||
author: author.and_then(|author| {
|
||||
author.name.map(|name| ApiEmbedAuthorResponse {
|
||||
name,
|
||||
url: author.url.map(|url| sign_own_url(&url, options)),
|
||||
url: author
|
||||
.url
|
||||
.map(|url| own_media_url(&url, options, scope, false)),
|
||||
proxy_icon_url: author
|
||||
.icon_url
|
||||
.as_ref()
|
||||
.map(|url| external_media_proxy_url(url, options)),
|
||||
icon_url: author.icon_url.map(|url| sign_own_url(&url, options)),
|
||||
.map(|url| own_media_proxy_url(url, options, scope, false)),
|
||||
icon_url: author
|
||||
.icon_url
|
||||
.map(|url| own_media_url(&url, options, scope, false)),
|
||||
})
|
||||
}),
|
||||
provider: provider.and_then(|provider| {
|
||||
provider.name.map(|name| ApiEmbedProviderResponse {
|
||||
name,
|
||||
url: provider.url.map(|url| sign_own_url(&url, options)),
|
||||
url: provider
|
||||
.url
|
||||
.map(|url| own_media_url(&url, options, scope, false)),
|
||||
icon_url: None,
|
||||
proxy_icon_url: None,
|
||||
})
|
||||
}),
|
||||
thumbnail: thumbnail.and_then(|media| self.map_embed_media(media, options)),
|
||||
image: image.and_then(|media| self.map_embed_media(media, options)),
|
||||
video: video.and_then(|media| self.map_embed_media(media, options)),
|
||||
audio: audio.and_then(|media| self.map_embed_media(media, options)),
|
||||
thumbnail: thumbnail.and_then(|media| self.map_embed_media(media, options, scope)),
|
||||
image: image.and_then(|media| self.map_embed_media(media, options, scope)),
|
||||
video: video.and_then(|media| self.map_embed_media(media, options, scope)),
|
||||
audio: audio.and_then(|media| self.map_embed_media(media, options, scope)),
|
||||
footer: footer.and_then(|footer| {
|
||||
footer.text.map(|text| ApiEmbedFooterResponse {
|
||||
text,
|
||||
proxy_icon_url: footer
|
||||
.icon_url
|
||||
.as_ref()
|
||||
.map(|url| external_media_proxy_url(url, options)),
|
||||
icon_url: footer.icon_url.map(|url| sign_own_url(&url, options)),
|
||||
.map(|url| own_media_proxy_url(url, options, scope, false)),
|
||||
icon_url: footer
|
||||
.icon_url
|
||||
.map(|url| own_media_url(&url, options, scope, false)),
|
||||
})
|
||||
}),
|
||||
fields: fields.map(|fields| fields.into_iter().map(map_embed_field_response).collect()),
|
||||
@@ -1657,11 +1687,15 @@ impl<T: Transport> MessagesShard<T> {
|
||||
&self,
|
||||
media: MessageEmbedMedia,
|
||||
options: &ResponseBuildOptions,
|
||||
scope: AttachmentUrlScope,
|
||||
) -> Option<ApiEmbedMediaResponse> {
|
||||
let url = media.url?;
|
||||
let owned = media
|
||||
.flags
|
||||
.is_some_and(|flags| flags & EMBED_MEDIA_OWNED_ATTACHMENT_FLAG != 0);
|
||||
Some(ApiEmbedMediaResponse {
|
||||
proxy_url: external_media_proxy_url(&url, options),
|
||||
url: sign_own_url(&url, options),
|
||||
proxy_url: own_media_proxy_url(&url, options, scope, owned),
|
||||
url: own_media_url(&url, options, scope, owned),
|
||||
width: media.width,
|
||||
height: media.height,
|
||||
duration: media.duration,
|
||||
@@ -1690,6 +1724,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
.and_then(|mentions| mentions.snapshots.get(snapshot_index))
|
||||
.cloned()
|
||||
.unwrap_or_else(|| extract_mentions_from_markdown(snapshot.content.as_deref()));
|
||||
let scope = AttachmentUrlScope::for_snapshot(message_id, channel_id);
|
||||
let mention_channels =
|
||||
ids_present_in_set(&snapshot.mention_channels, &snapshot_mentions.channels)
|
||||
.into_iter()
|
||||
@@ -1700,7 +1735,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
snapshot.embeds.as_deref().map(|embeds| {
|
||||
embeds
|
||||
.iter()
|
||||
.map(|embed| self.map_embed(embed, options))
|
||||
.map(|embed| self.map_embed(embed, options, scope))
|
||||
.collect()
|
||||
})
|
||||
} else {
|
||||
@@ -1709,9 +1744,7 @@ impl<T: Transport> MessagesShard<T> {
|
||||
let attachments = snapshot.attachments.as_ref().map(|attachments| {
|
||||
attachments
|
||||
.iter()
|
||||
.filter_map(|attachment| {
|
||||
self.map_attachment(channel_id, attachment, options, context)
|
||||
})
|
||||
.filter_map(|attachment| self.map_attachment(scope, attachment, options, context))
|
||||
.collect()
|
||||
});
|
||||
ApiMessageSnapshotResponse {
|
||||
@@ -2308,6 +2341,7 @@ impl<T: Transport> ShardService for MessagesShard<T> {
|
||||
include_hidden,
|
||||
threads_mask,
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
} => {
|
||||
let channel_id = parse_i64(&channel_id, "channel_id")?;
|
||||
let message_id = parse_i64(&message_id, "message_id")?;
|
||||
@@ -2338,6 +2372,7 @@ impl<T: Transport> ShardService for MessagesShard<T> {
|
||||
threads_mask,
|
||||
excluded_types: Vec::new(),
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
@@ -2362,6 +2397,7 @@ impl<T: Transport> ShardService for MessagesShard<T> {
|
||||
include_hidden,
|
||||
threads_mask,
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
} => {
|
||||
let viewer_user_id = parse_i64(&viewer_user_id, "viewer_user_id")?;
|
||||
let source_guild_id = source_guild_id
|
||||
@@ -2389,6 +2425,7 @@ impl<T: Transport> ShardService for MessagesShard<T> {
|
||||
threads_mask,
|
||||
excluded_types: Vec::new(),
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
@@ -2411,6 +2448,7 @@ impl<T: Transport> ShardService for MessagesShard<T> {
|
||||
include_hidden,
|
||||
threads_mask,
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
} => {
|
||||
let viewer_user_id = parse_i64(&viewer_user_id, "viewer_user_id")?;
|
||||
let source_guild_id = source_guild_id
|
||||
@@ -2438,6 +2476,7 @@ impl<T: Transport> ShardService for MessagesShard<T> {
|
||||
threads_mask,
|
||||
excluded_types: Vec::new(),
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
@@ -2462,6 +2501,7 @@ impl<T: Transport> ShardService for MessagesShard<T> {
|
||||
threads_mask,
|
||||
mut exclude_types,
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
} => {
|
||||
let channel_id = parse_i64(&channel_id, "channel_id")?;
|
||||
if threads_mask && !exclude_types.contains(&MESSAGE_TYPE_THREAD_CREATED) {
|
||||
@@ -2509,6 +2549,7 @@ impl<T: Transport> ShardService for MessagesShard<T> {
|
||||
threads_mask,
|
||||
excluded_types: exclude_types,
|
||||
instance_name,
|
||||
attachment_api_base,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
@@ -3085,6 +3126,184 @@ fn attachment_cdn_url(
|
||||
)
|
||||
}
|
||||
|
||||
fn api_attachment_url(
|
||||
api_base: &str,
|
||||
channel_id: i64,
|
||||
attachment_id: i64,
|
||||
raw_filename: &str,
|
||||
message_id: Option<i64>,
|
||||
preserved_query: &[&str],
|
||||
) -> String {
|
||||
let mut url = format!(
|
||||
"{}/channels/{channel_id}/attachments/{attachment_id}/{raw_filename}",
|
||||
api_base.trim_end_matches('/')
|
||||
);
|
||||
let mid = message_id.map(|id| format!("mid={id}"));
|
||||
let fields: Vec<&str> = mid
|
||||
.as_deref()
|
||||
.into_iter()
|
||||
.chain(preserved_query.iter().copied())
|
||||
.collect();
|
||||
if !fields.is_empty() {
|
||||
url.push('?');
|
||||
url.push_str(&fields.join("&"));
|
||||
}
|
||||
url
|
||||
}
|
||||
|
||||
fn scoped_attachment_url(
|
||||
scope: AttachmentUrlScope,
|
||||
attachment_id: i64,
|
||||
filename: &str,
|
||||
options: &ResponseBuildOptions,
|
||||
now_secs: u64,
|
||||
) -> String {
|
||||
match options.attachment_api_base.as_deref() {
|
||||
Some(api_base) => api_attachment_url(
|
||||
api_base,
|
||||
scope.channel_id,
|
||||
attachment_id,
|
||||
filename,
|
||||
Some(scope.message_id),
|
||||
&[],
|
||||
),
|
||||
None => attachment_cdn_url(
|
||||
scope.storage_channel_id,
|
||||
attachment_id,
|
||||
filename,
|
||||
options,
|
||||
now_secs,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_api_attachment_url(api_base: &str, input_url: &str) -> bool {
|
||||
let api_base = api_base.trim_end_matches('/');
|
||||
let (Ok(base), Ok(target)) = (Url::parse(api_base), Url::parse(input_url)) else {
|
||||
return false;
|
||||
};
|
||||
if base.origin() != target.origin() {
|
||||
return false;
|
||||
}
|
||||
let base_path = raw_url_path(api_base).trim_end_matches('/');
|
||||
let Some(rest) = raw_url_path(input_url)
|
||||
.strip_prefix(base_path)
|
||||
.and_then(|path| path.strip_prefix("/channels/"))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
let mut segments = rest.splitn(4, '/');
|
||||
matches!(
|
||||
(segments.next(), segments.next(), segments.next(), segments.next()),
|
||||
(Some(channel_id), Some("attachments"), Some(attachment_id), Some(filename))
|
||||
if is_snowflake_segment(channel_id)
|
||||
&& is_snowflake_segment(attachment_id)
|
||||
&& !filename.is_empty()
|
||||
)
|
||||
}
|
||||
|
||||
fn is_reserved_api_query_name(name: &str) -> bool {
|
||||
name == "mid" || name == "mt"
|
||||
}
|
||||
|
||||
fn without_media_token(input_url: &str) -> String {
|
||||
let (head, fragment) = input_url
|
||||
.split_once('#')
|
||||
.map_or((input_url, None), |(head, fragment)| (head, Some(fragment)));
|
||||
let Some((base, query)) = head.split_once('?') else {
|
||||
return input_url.to_owned();
|
||||
};
|
||||
let kept: Vec<&str> = query
|
||||
.split('&')
|
||||
.filter(|field| !field.is_empty())
|
||||
.filter(|field| field.split_once('=').map_or(*field, |(name, _)| name) != "mt")
|
||||
.collect();
|
||||
let mut url = base.to_owned();
|
||||
if !kept.is_empty() {
|
||||
url.push('?');
|
||||
url.push_str(&kept.join("&"));
|
||||
}
|
||||
if let Some(fragment) = fragment {
|
||||
url.push('#');
|
||||
url.push_str(fragment);
|
||||
}
|
||||
url
|
||||
}
|
||||
|
||||
fn api_own_media_url(
|
||||
input_url: &str,
|
||||
options: &ResponseBuildOptions,
|
||||
api_base: &str,
|
||||
scope: AttachmentUrlScope,
|
||||
owned: bool,
|
||||
) -> Option<String> {
|
||||
let media_endpoint = options.media_endpoint.trim_end_matches('/');
|
||||
let (storage_key, attachment_id) = own_attachment_key(input_url, media_endpoint)?;
|
||||
let key_channel_id: i64 = storage_key.split('/').nth(1)?.parse().ok()?;
|
||||
let raw_path =
|
||||
raw_url_path(input_url).strip_prefix(raw_url_path(media_endpoint).trim_end_matches('/'))?;
|
||||
let mut raw_segments = raw_path.trim_start_matches('/').splitn(4, '/');
|
||||
let raw_filename = raw_segments.nth(3)?;
|
||||
let query = input_url
|
||||
.split_once('?')
|
||||
.map(|(_, query)| query.split_once('#').map_or(query, |(query, _)| query))
|
||||
.unwrap_or_default();
|
||||
let preserved: Vec<&str> = query
|
||||
.split('&')
|
||||
.filter(|field| !field.is_empty())
|
||||
.filter(|field| {
|
||||
let name = field.split_once('=').map_or(*field, |(name, _)| name);
|
||||
!fluxer_common::attachment_url_signature::is_signature_parameter_name(name)
|
||||
&& !is_reserved_api_query_name(name)
|
||||
})
|
||||
.collect();
|
||||
let (channel_id, message_id) = if owned && key_channel_id == scope.storage_channel_id {
|
||||
(scope.channel_id, Some(scope.message_id))
|
||||
} else {
|
||||
(key_channel_id, None)
|
||||
};
|
||||
Some(api_attachment_url(
|
||||
api_base,
|
||||
channel_id,
|
||||
attachment_id,
|
||||
raw_filename,
|
||||
message_id,
|
||||
&preserved,
|
||||
))
|
||||
}
|
||||
|
||||
fn own_media_url(
|
||||
input_url: &str,
|
||||
options: &ResponseBuildOptions,
|
||||
scope: AttachmentUrlScope,
|
||||
owned: bool,
|
||||
) -> String {
|
||||
match options.attachment_api_base.as_deref() {
|
||||
Some(api_base) if is_api_attachment_url(api_base, input_url) => {
|
||||
without_media_token(input_url)
|
||||
}
|
||||
Some(api_base) => api_own_media_url(input_url, options, api_base, scope, owned)
|
||||
.unwrap_or_else(|| input_url.to_owned()),
|
||||
None => sign_own_url(input_url, options),
|
||||
}
|
||||
}
|
||||
|
||||
fn own_media_proxy_url(
|
||||
input_url: &str,
|
||||
options: &ResponseBuildOptions,
|
||||
scope: AttachmentUrlScope,
|
||||
owned: bool,
|
||||
) -> String {
|
||||
match options.attachment_api_base.as_deref() {
|
||||
Some(api_base) if is_api_attachment_url(api_base, input_url) => {
|
||||
without_media_token(input_url)
|
||||
}
|
||||
Some(api_base) => api_own_media_url(input_url, options, api_base, scope, owned)
|
||||
.unwrap_or_else(|| external_media_proxy_url(input_url, options)),
|
||||
None => external_media_proxy_url(input_url, options),
|
||||
}
|
||||
}
|
||||
|
||||
fn raw_url_path(url: &str) -> &str {
|
||||
let Some((_, after_scheme)) = url.split_once("://") else {
|
||||
return "";
|
||||
@@ -3565,6 +3784,7 @@ mod tests {
|
||||
excluded_types: Vec::new(),
|
||||
attachment_decay: true,
|
||||
instance_name: "Fluxer".to_owned(),
|
||||
attachment_api_base: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3583,6 +3803,10 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn signed_scope() -> AttachmentUrlScope {
|
||||
AttachmentUrlScope::for_snapshot(SIGNED_ATTACHMENT_ID, SIGNED_CHANNEL_ID)
|
||||
}
|
||||
|
||||
fn signed_attachment() -> MessageAttachment {
|
||||
serde_json::from_value(json!({
|
||||
"attachment_id": SIGNED_ATTACHMENT_ID.to_string(),
|
||||
@@ -3710,7 +3934,7 @@ mod tests {
|
||||
};
|
||||
|
||||
let mapped = shard
|
||||
.map_attachment(SIGNED_CHANNEL_ID, &signed_attachment(), &options, &context)
|
||||
.map_attachment(signed_scope(), &signed_attachment(), &options, &context)
|
||||
.expect("a decayed attachment still maps");
|
||||
|
||||
assert_eq!(None, mapped.url);
|
||||
@@ -3731,7 +3955,7 @@ mod tests {
|
||||
}))
|
||||
.unwrap();
|
||||
|
||||
let mapped = shard.map_embed(&embed, &options);
|
||||
let mapped = shard.map_embed(&embed, &options, signed_scope());
|
||||
|
||||
let base = mapped.base;
|
||||
assert_eq!(
|
||||
@@ -3820,4 +4044,212 @@ mod tests {
|
||||
assert_eq!(input, media_proxy_url_at(input, &options, now), "{input}");
|
||||
}
|
||||
}
|
||||
|
||||
const API_BASE: &str = "https://api.test/api";
|
||||
const COPY_CHANNEL_ID: i64 = 1_544_725_486_800_732_999;
|
||||
const MESSAGE_ID: i64 = 1_544_971_349_200_470_123;
|
||||
|
||||
fn api_options() -> ResponseBuildOptions {
|
||||
ResponseBuildOptions {
|
||||
attachment_api_base: Some(API_BASE.to_owned()),
|
||||
..signing_options()
|
||||
}
|
||||
}
|
||||
|
||||
fn crosspost_scope() -> AttachmentUrlScope {
|
||||
AttachmentUrlScope {
|
||||
message_id: MESSAGE_ID,
|
||||
channel_id: COPY_CHANNEL_ID,
|
||||
storage_channel_id: SIGNED_CHANNEL_ID,
|
||||
}
|
||||
}
|
||||
|
||||
fn own_embed(url: &str) -> MessageEmbed {
|
||||
serde_json::from_value(json!({
|
||||
"type": "image",
|
||||
"url": url,
|
||||
"image": {"url": url},
|
||||
"footer": {"text": "footer", "icon_url": url},
|
||||
}))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn uploaded_embed(url: &str) -> MessageEmbed {
|
||||
serde_json::from_value(json!({
|
||||
"type": "rich",
|
||||
"url": url,
|
||||
"image": {"url": url, "flags": EMBED_MEDIA_OWNED_ATTACHMENT_FLAG},
|
||||
"footer": {"text": "footer", "icon_url": url},
|
||||
}))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_api_attachment_url_names_the_viewed_channel_and_message() {
|
||||
let deleted = DeletedMessageKeys::default();
|
||||
let shard = recording_shard(&deleted);
|
||||
let mapped = shard
|
||||
.map_attachment(
|
||||
crosspost_scope(),
|
||||
&signed_attachment(),
|
||||
&api_options(),
|
||||
&ResponseContext::default(),
|
||||
)
|
||||
.expect("the attachment maps");
|
||||
|
||||
let expected = format!(
|
||||
"{API_BASE}/channels/{COPY_CHANNEL_ID}/attachments/{SIGNED_ATTACHMENT_ID}/{SIGNED_FILENAME}?mid={MESSAGE_ID}"
|
||||
);
|
||||
assert_eq!(Some(expected.clone()), mapped.url);
|
||||
assert_eq!(Some(expected), mapped.proxy_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_decayed_attachment_has_no_api_url() {
|
||||
let deleted = DeletedMessageKeys::default();
|
||||
let shard = recording_shard(&deleted);
|
||||
let context = ResponseContext {
|
||||
attachment_decay: [(
|
||||
SIGNED_ATTACHMENT_ID,
|
||||
DateTime::<Utc>::from_timestamp_millis(unix_millis() - 1_000).unwrap(),
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
..ResponseContext::default()
|
||||
};
|
||||
let mapped = shard
|
||||
.map_attachment(
|
||||
crosspost_scope(),
|
||||
&signed_attachment(),
|
||||
&api_options(),
|
||||
&context,
|
||||
)
|
||||
.expect("a decayed attachment still maps");
|
||||
|
||||
assert_eq!(None, mapped.url);
|
||||
assert_eq!(None, mapped.proxy_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_uploaded_embed_image_points_at_the_message_and_drops_the_signature() {
|
||||
let deleted = DeletedMessageKeys::default();
|
||||
let shard = recording_shard(&deleted);
|
||||
let own = format!(
|
||||
"https://media.test/{}?ex=1&is=2&hm=3&mt=4&width=64",
|
||||
signed_storage_key()
|
||||
);
|
||||
|
||||
let mapped = shard.map_embed(&uploaded_embed(&own), &api_options(), crosspost_scope());
|
||||
|
||||
let owned = format!(
|
||||
"{API_BASE}/channels/{COPY_CHANNEL_ID}/attachments/{SIGNED_ATTACHMENT_ID}/{SIGNED_FILENAME}?mid={MESSAGE_ID}&width=64"
|
||||
);
|
||||
let linked = format!(
|
||||
"{API_BASE}/channels/{SIGNED_CHANNEL_ID}/attachments/{SIGNED_ATTACHMENT_ID}/{SIGNED_FILENAME}?width=64"
|
||||
);
|
||||
let base = mapped.base;
|
||||
let image = base.image.expect("the embed has an image");
|
||||
assert_eq!(owned, image.url);
|
||||
assert_eq!(owned, image.proxy_url);
|
||||
assert_eq!(Some(linked.clone()), base.url);
|
||||
let footer = base.footer.expect("the embed has a footer");
|
||||
assert_eq!(Some(linked.clone()), footer.icon_url);
|
||||
assert_eq!(Some(linked), footer.proxy_icon_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_linked_file_from_the_same_channel_never_names_the_message() {
|
||||
let deleted = DeletedMessageKeys::default();
|
||||
let shard = recording_shard(&deleted);
|
||||
let own = format!("https://media.test/{}", signed_storage_key());
|
||||
|
||||
let mapped = shard.map_embed(&own_embed(&own), &api_options(), crosspost_scope());
|
||||
|
||||
let expected = format!(
|
||||
"{API_BASE}/channels/{SIGNED_CHANNEL_ID}/attachments/{SIGNED_ATTACHMENT_ID}/{SIGNED_FILENAME}"
|
||||
);
|
||||
let base = mapped.base;
|
||||
let image = base.image.expect("the embed has an image");
|
||||
assert_eq!(expected, image.url);
|
||||
assert_eq!(expected, image.proxy_url);
|
||||
assert_eq!(Some(expected), base.url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_embed_url_from_another_channel_keeps_that_channel_and_no_message() {
|
||||
let deleted = DeletedMessageKeys::default();
|
||||
let shard = recording_shard(&deleted);
|
||||
let other_channel_id = COPY_CHANNEL_ID - 1;
|
||||
let own = format!(
|
||||
"https://media.test/attachments/{other_channel_id}/{SIGNED_ATTACHMENT_ID}/{SIGNED_FILENAME}"
|
||||
);
|
||||
|
||||
let mapped = shard.map_embed(&own_embed(&own), &api_options(), crosspost_scope());
|
||||
|
||||
let expected = format!(
|
||||
"{API_BASE}/channels/{other_channel_id}/attachments/{SIGNED_ATTACHMENT_ID}/{SIGNED_FILENAME}"
|
||||
);
|
||||
let image = mapped.base.image.expect("the embed has an image");
|
||||
assert_eq!(expected, image.url);
|
||||
assert_eq!(expected, image.proxy_url);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_api_url_in_an_embed_is_never_sent_through_the_external_proxy() {
|
||||
let deleted = DeletedMessageKeys::default();
|
||||
let shard = recording_shard(&deleted);
|
||||
let pasted = format!(
|
||||
"{API_BASE}/channels/{COPY_CHANNEL_ID}/attachments/{SIGNED_ATTACHMENT_ID}/{SIGNED_FILENAME}"
|
||||
);
|
||||
let foreign = "https://example.test/cat.png";
|
||||
|
||||
let mapped = shard.map_embed(
|
||||
&own_embed(&format!("{pasted}?mt=secret")),
|
||||
&api_options(),
|
||||
crosspost_scope(),
|
||||
);
|
||||
let image = mapped.base.image.expect("the embed has an image");
|
||||
assert_eq!(pasted, image.url);
|
||||
assert_eq!(pasted, image.proxy_url);
|
||||
|
||||
let mapped = shard.map_embed(&own_embed(foreign), &api_options(), crosspost_scope());
|
||||
let image = mapped.base.image.expect("the embed has an image");
|
||||
assert_eq!(foreign, image.url);
|
||||
assert_eq!(
|
||||
external_media_proxy_url(foreign, &signing_options()),
|
||||
image.proxy_url
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_an_api_base_every_url_matches_the_media_proxy_form() {
|
||||
let deleted = DeletedMessageKeys::default();
|
||||
let shard = recording_shard(&deleted);
|
||||
let options = signing_options();
|
||||
let now = unix_seconds();
|
||||
let mapped = shard
|
||||
.map_attachment(
|
||||
crosspost_scope(),
|
||||
&signed_attachment(),
|
||||
&options,
|
||||
&ResponseContext::default(),
|
||||
)
|
||||
.expect("the attachment maps");
|
||||
assert_eq!(
|
||||
Some(attachment_cdn_url(
|
||||
SIGNED_CHANNEL_ID,
|
||||
SIGNED_ATTACHMENT_ID,
|
||||
SIGNED_FILENAME,
|
||||
&options,
|
||||
now
|
||||
)),
|
||||
mapped.url
|
||||
);
|
||||
|
||||
let own = format!("https://media.test/{}", signed_storage_key());
|
||||
let mapped = shard.map_embed(&own_embed(&own), &options, crosspost_scope());
|
||||
let image = mapped.base.image.expect("the embed has an image");
|
||||
assert_eq!(sign_own_url(&own, &options), image.url);
|
||||
assert_eq!(external_media_proxy_url(&own, &options), image.proxy_url);
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,8 @@ pub enum MessageRequest {
|
||||
threads_mask: bool,
|
||||
#[serde(default = "default_instance_name")]
|
||||
instance_name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
attachment_api_base: Option<String>,
|
||||
},
|
||||
BuildResponse {
|
||||
message: Message,
|
||||
@@ -116,6 +118,8 @@ pub enum MessageRequest {
|
||||
threads_mask: bool,
|
||||
#[serde(default = "default_instance_name")]
|
||||
instance_name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
attachment_api_base: Option<String>,
|
||||
},
|
||||
BuildResponses {
|
||||
messages: Vec<Message>,
|
||||
@@ -135,6 +139,8 @@ pub enum MessageRequest {
|
||||
threads_mask: bool,
|
||||
#[serde(default = "default_instance_name")]
|
||||
instance_name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
attachment_api_base: Option<String>,
|
||||
},
|
||||
ListResponses {
|
||||
channel_id: String,
|
||||
@@ -160,6 +166,8 @@ pub enum MessageRequest {
|
||||
exclude_types: Vec<i32>,
|
||||
#[serde(default = "default_instance_name")]
|
||||
instance_name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
attachment_api_base: Option<String>,
|
||||
},
|
||||
ExtractMentions {
|
||||
contents: Vec<String>,
|
||||
|
||||
Loaded 100 of 107 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user