@@ -36,6 +36,15 @@ if (typeof globalThis.Buffer === 'undefined') {
3636 */
3737let gitHubAuthToken : string | undefined ;
3838
39+ /**
40+ * Known CORS proxy URL prefixes used by WordPress Playground.
41+ * Keep up this synced with packages/playground/website-extras/vite.config.ts
42+ */
43+ const KNOWN_CORS_PROXY_URLS = [
44+ 'https://wordpress-playground-cors-proxy.net/?' ,
45+ 'http://127.0.0.1:5263/cors-proxy.php?' ,
46+ ] ;
47+
3948/**
4049 * Sets the GitHub authentication token to use for git protocol requests.
4150 * This is intended to be called by browser-specific initialization code
@@ -61,7 +70,7 @@ export class GitHubAuthenticationError extends Error {
6170
6271/**
6372 * Checks if a URL is a GitHub URL by parsing the hostname.
64- * Handles both direct GitHub URLs and CORS-proxied URLs.
73+ * Handles both direct GitHub URLs and CORS-proxied GitHub URLs.
6574 *
6675 * @param url The URL to check
6776 * @returns true if the URL is definitively a GitHub URL, false otherwise
@@ -70,40 +79,32 @@ function isGitHubUrl(url: string): boolean {
7079 try {
7180 const parsedUrl = new URL ( url ) ;
7281
73- // Direct GitHub URL - check hostname
7482 if ( parsedUrl . hostname === 'github.com' ) {
7583 return true ;
7684 }
7785
78- // CORS-proxied GitHub URL - the actual GitHub URL should be in the query string
79- // Format: https://proxy.com/cors-proxy.php?https://github.com/...
80- // We need to extract and validate the proxied URL's hostname
81- const queryString = parsedUrl . search . substring ( 1 ) ; // Remove leading '?'
82- if ( queryString ) {
83- // Try to extract a URL from the query string
84- // Match URLs that start with http:// or https://
85- const urlMatch = queryString . match ( / ^ ( h t t p s ? : \/ \/ [ ^ \s & ] + ) / ) ;
86- if ( urlMatch ) {
86+ for ( const proxyUrl of KNOWN_CORS_PROXY_URLS ) {
87+ if ( url . startsWith ( proxyUrl ) ) {
88+ const proxiedUrl = url . substring ( proxyUrl . length ) ;
8789 try {
88- const proxiedUrl = new URL ( urlMatch [ 1 ] ) ;
89- if ( proxiedUrl . hostname === 'github.com' ) {
90- return true ;
91- }
90+ const proxiedParsedUrl = new URL ( proxiedUrl ) ;
91+ return proxiedParsedUrl . hostname === 'github.com' ;
9292 } catch {
93- // Invalid proxied URL, ignore
93+ return false ;
9494 }
9595 }
9696 }
9797
9898 return false ;
9999 } catch {
100- // If URL parsing fails, return false
101100 return false ;
102101 }
103102}
104103
105104/**
106105 * Returns GitHub authentication headers if a token is available and the URL is a GitHub URL.
106+ *
107+ * @param url The URL to check
107108 */
108109function getGitHubAuthHeaders ( url : string ) : Record < string , string > {
109110 if ( gitHubAuthToken && isGitHubUrl ( url ) ) {
0 commit comments