Skip to content

Instantly share code, notes, and snippets.

@freakynit
Created February 25, 2025 03:06
Show Gist options
  • Save freakynit/c1adc90093abff12a9e0826fe149d4e0 to your computer and use it in GitHub Desktop.
Save freakynit/c1adc90093abff12a9e0826fe149d4e0 to your computer and use it in GitHub Desktop.

Key Observations:

  1. The code implements a basic reverse proxy that:
    • Routes requests through a specific domain (stratosphericus.workers.dev)
    • Rewrites URLs in HTML responses
    • Handles CORS
    • Manages cookies
    • Supports redirects

Potential Issues & Improvements:

  1. URL Parsing Logic
if (url.pathname.startsWith('/')) {
  targetURL = new URL(url.pathname.substring(1))
} else {
  targetURL = new URL(url.pathname)
}
  • This assumes the path always contains a valid URL after the first character
  • Could throw an error if the substring isn't a valid URL Suggested fix:
try {
  targetURL = new URL(url.pathname.startsWith('/') ? url.pathname.substring(1) : url.pathname);
} catch (e) {
  return new Response('Invalid target URL', { status: 400 });
}
  1. Protocol Handling
if (!targetURL.toString().startsWith('http')) {
  targetURL = new URL(targetURL, `https://${myURL.hostname}/${new URL(request.url).protocol}////${request.url.host.toString()}/`)
}
  • Multiple slashes (////) might cause parsing issues
  • Complex base URL construction could be simplified Suggested fix:
if (!targetURL.protocol) {
  targetURL = new URL(`${targetURL}`, `https://${myURL.hostname}/`);
}
  1. Header Management
  • Setting both 'host' and 'origin' to the same value might not be necessary
  • Consider preserving the original Origin header for some use cases Suggestion:
newHeader.set('Host', targetURL.host);
// Only set Origin if needed
if (request.headers.get('Origin')) {
  newHeader.set('Origin', targetURL.origin);
}
  1. CORS Configuration
newRespHeader.set('Access-Control-Allow-Origin', '*');
newRespHeader.set('Access-Control-Allow-Methods', '*');
  • Wildcard (*) might be too permissive for some use cases
  • Consider mirroring the request's Origin or using specific allowed methods Suggestion:
newRespHeader.set('Access-Control-Allow-Origin', request.headers.get('Origin') || '*');
newRespHeader.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  1. HTML Rewriting
  • The rewriteSomething class has inconsistent protocol checking:
if (!eleThings.startsWith('http') || !eleThings.startsWith('https'))
  • This will always be true due to OR condition (should be AND) Suggested fix:
if (!eleThings.startsWith('http://') && !eleThings.startsWith('https://'))
  1. Error Handling
  • Current try/catch is good but could provide more specific error messages Suggestion:
try {
  let response = await fetch(newRequest);
  // ... rest of code
} catch (error) {
  let message = 'Internal Server Error';
  if (error.name === 'TypeError') message = 'Invalid URL or network error';
  return new Response(`Proxy Error: ${message}`, {
    status: 500,
    headers: {
      'Content-Type': 'text/plain',
      'Access-Control-Allow-Origin': '*'
    }
  });
}

Additional Recommendations:

  1. Add input validation for URLs
  2. Implement rate limiting
  3. Add caching where appropriate
  4. Consider adding request timeout handling
  5. Add logging for debugging/monitoring

Here's a cleaned-up version of the critical section:

async function handleRequest(request) {
  const url = new URL(request.url);
  
  // Determine target URL
  let targetURL;
  if (url.host === myURL.host) {
    try {
      targetURL = new URL(url.pathname.startsWith('/') ? url.pathname.substring(1) : url.pathname);
    } catch (e) {
      return new Response('Invalid target URL', { status: 400 });
    }
  } else {
    targetURL = url;
  }

  // Ensure protocol
  if (!targetURL.protocol) {
    targetURL = new URL(`${targetURL}`, `https://${myURL.hostname}/`);
  }

  // Prepare headers
  const newHeader = new Headers(request.headers);
  newHeader.set('Host', targetURL.host);

  const newRequest = new Request(targetURL, {
    method: request.method,
    headers: newHeader,
    body: request.body,
    cf: { ssl: { verify: false } }
  });

  // ... rest of the function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment