Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions functions/audio-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ export async function onRequest(context) {
return new Response('Missing url parameter', { status: 400 });
}

// Basic security: only proxy from elearning.hakka.gov.tw
if (!targetUrl.startsWith('https://elearning.hakka.gov.tw/')) {
// Basic security: only proxy from elearning.hakka.gov.tw or localhost for development
const isHakkaGov = targetUrl.startsWith('https://elearning.hakka.gov.tw/');
const isLocalhost = /^https?:\/\/localhost([:/]|$)/.test(targetUrl);
Comment on lines +11 to +12
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and avoid duplicating magic strings and regexes, consider defining the allowed domain and the localhost regex as constants. This logic is also duplicated in main.js. If possible, creating a shared utility function like isAllowedAudioUrl(url) would be even better to ensure consistency between client and server validation.


if (!isHakkaGov && !isLocalhost) {
return new Response('Invalid target URL', { status: 403 });
}

Expand Down
7 changes: 6 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,12 @@ const LEVEL_INFO = {
* @returns {string} 套用 Proxy 後的網址,或原網址。
*/
function applyAudioProxy(url) {
if (!url || !url.startsWith('https://elearning.hakka.gov.tw/')) {
if (!url) return url;

const isHakkaGov = url.startsWith('https://elearning.hakka.gov.tw/');
const isLocalhost = /^https?:\/\/localhost([:/]|$)/.test(url);
Comment on lines +397 to +398
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This validation logic, including the domain string and regex, is duplicated from functions/audio-proxy.js. To improve maintainability, consider defining these as constants. If your project structure allows, creating a shared utility function would be the ideal way to keep client and server validation synchronized.


if (!isHakkaGov && !isLocalhost) {
return url;
}
// 在本機開發或特定測試環境下,可能需要根據環境調整路徑
Expand Down
Loading
Loading