forked from misskey-dev/media-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createHmac, timingSafeEqual } from 'node:crypto'; | ||
|
||
export function verifySignedProxyURL(signedURLString: string, signatureKey: string): boolean { | ||
const workingURL = new URL(signedURLString); | ||
|
||
// 提取签名参数 | ||
const sig = workingURL.searchParams.get('sig'); | ||
if (sig === null) { | ||
// 缺失签名 | ||
return false; | ||
} | ||
|
||
// 去掉签名参数 | ||
workingURL.searchParams.delete('sig'); | ||
|
||
// 提取过期时间 | ||
const exp = workingURL.searchParams.get('exp'); | ||
if (exp === null) { | ||
// 缺失过期时间 | ||
return false; | ||
} | ||
// 检查是否已过期 | ||
const expEpochSec = parseInt(exp); | ||
if (expEpochSec > Date.now() / 1000) { | ||
// 无效的时间,或者已经超时 | ||
return false; | ||
} | ||
|
||
// 检查是否有 static 参数:因为前端可能会追加这个参数,为避免参数影响,要把它删除掉。 | ||
if (workingURL.searchParams.has('static')) { | ||
workingURL.searchParams.delete('static'); | ||
} | ||
|
||
// 排序查询字符串 | ||
workingURL.searchParams.sort(); | ||
|
||
// 生成正确的签名用来对照 | ||
const sigCorrect = createHmac('sha256', signatureKey). | ||
update(workingURL.toString()).digest('hex'); | ||
|
||
// 检查签名是否匹配 | ||
if (!timingSafeEqual(Buffer.from(sigCorrect), Buffer.from(sig))) { | ||
// 不匹配 | ||
return false; | ||
} | ||
|
||
// 验证通过 | ||
return true; | ||
} |