@ -4,15 +4,57 @@ import type { SlackFile } from "../types.js";
import { fetchRemoteMedia } from "../../media/fetch.js" ;
import { saveMediaBuffer } from "../../media/store.js" ;
function normalizeHostname ( hostname : string ) : string {
const normalized = hostname . trim ( ) . toLowerCase ( ) . replace ( /\.$/ , "" ) ;
if ( normalized . startsWith ( "[" ) && normalized . endsWith ( "]" ) ) {
return normalized . slice ( 1 , - 1 ) ;
}
return normalized ;
}
function isSlackHostname ( hostname : string ) : boolean {
const normalized = normalizeHostname ( hostname ) ;
if ( ! normalized ) {
return false ;
}
// Slack-hosted files typically come from *.slack.com and redirect to Slack CDN domains.
// Include a small allowlist of known Slack domains to avoid leaking tokens if a file URL
// is ever spoofed or mishandled.
const allowedSuffixes = [ "slack.com" , "slack-edge.com" , "slack-files.com" ] ;
return allowedSuffixes . some (
( suffix ) = > normalized === suffix || normalized . endsWith ( ` . ${ suffix } ` ) ,
) ;
}
function assertSlackFileUrl ( rawUrl : string ) : URL {
let parsed : URL ;
try {
parsed = new URL ( rawUrl ) ;
} catch {
throw new Error ( ` Invalid Slack file URL: ${ rawUrl } ` ) ;
}
if ( parsed . protocol !== "https:" ) {
throw new Error ( ` Refusing Slack file URL with non-HTTPS protocol: ${ parsed . protocol } ` ) ;
}
if ( ! isSlackHostname ( parsed . hostname ) ) {
throw new Error (
` Refusing to send Slack token to non-Slack host " ${ parsed . hostname } " (url: ${ rawUrl } ) ` ,
) ;
}
return parsed ;
}
/ * *
* Fetches a URL with Authorization header , handling cross - origin redirects .
* Node . js fetch strips Authorization headers on cross - origin redirects for security .
* Slack ' s files . slack . com URLs redirect to CDN domains with pre - signed URLs that
* don ' t need the Authorization header , so we handle the initial auth request manually .
* Slack 's file URLs redirect to CDN domains with pre-signed URLs that don' t need the
* Authorization header , so we handle the initial auth request manually .
* /
export async function fetchWithSlackAuth ( url : string , token : string ) : Promise < Response > {
const parsed = assertSlackFileUrl ( url ) ;
// Initial request with auth and manual redirect handling
const initialRes = await fetch ( url , {
const initialRes = await fetch ( parsed. href , {
headers : { Authorization : ` Bearer ${ token } ` } ,
redirect : "manual" ,
} ) ;
@ -29,11 +71,16 @@ export async function fetchWithSlackAuth(url: string, token: string): Promise<Re
}
// Resolve relative URLs against the original
const resolvedUrl = new URL ( redirectUrl , url ) . toString ( ) ;
const resolvedUrl = new URL ( redirectUrl , parsed . href ) ;
// Only follow safe protocols (we do NOT include Authorization on redirects).
if ( resolvedUrl . protocol !== "https:" ) {
return initialRes ;
}
// Follow the redirect without the Authorization header
// (Slack's CDN URLs are pre-signed and don't need it)
return fetch ( resolvedUrl , { redirect : "follow" } ) ;
return fetch ( resolvedUrl .toString ( ) , { redirect : "follow" } ) ;
}
export async function resolveSlackMedia ( params : {
@ -52,8 +99,9 @@ export async function resolveSlackMedia(params: {
continue ;
}
try {
// Note: We ignore init options because fetchWithSlackAuth handles
// redirect behavior specially. fetchRemoteMedia only passes the URL.
// Note: fetchRemoteMedia calls fetchImpl(url) with the URL string today and
// handles size limits internally. We ignore init options because
// fetchWithSlackAuth handles redirect/auth behavior specially.
const fetchImpl : FetchLike = ( input ) = > {
const inputUrl =
typeof input === "string" ? input : input instanceof URL ? input.href : input.url ;
@ -63,6 +111,7 @@ export async function resolveSlackMedia(params: {
url ,
fetchImpl ,
filePathHint : file.name ,
maxBytes : params.maxBytes ,
} ) ;
if ( fetched . buffer . byteLength > params . maxBytes ) {
continue ;