Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for CORS requests to api.wordpress.org to pass #1009

Closed
wants to merge 4 commits into from
Closed
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
27 changes: 14 additions & 13 deletions packages/playground/remote/src/lib/setup-fetch-network-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function setupFetchNetworkTransport(playground: UniversalPHP) {

export async function handleRequest(data: RequestData, fetchFn = fetch) {
const hostname = new URL(data.url).hostname;
const fetchUrl = ['w.org', 's.w.org'].includes(hostname)
let fetchUrl = ['w.org', 's.w.org'].includes(hostname)
? `/plugin-proxy.php?url=${encodeURIComponent(data.url)}`
: data.url;

Expand All @@ -60,19 +60,20 @@ export async function handleRequest(data: RequestData, fetchFn = fetch) {
const fetchHeaders = data.headers || {};
if (fetchMethod == 'POST') {
fetchHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
}

/**
* Removes a few custom request headers.
*
* This is required because the fetch API will send a CORS preflight
* request if the request is cross-origin and has custom headers.
*
* However, the api.wordpress.org/core/version-check/1.7/ endpoint
* doesn't support CORS preflight requests. These two headers
* aren't critical to the request, so we can just remove them.
*/
delete fetchHeaders['wp_install'];
delete fetchHeaders['wp_blog'];
/**
* Temporarily avoid CORS issues by using a specific URI.
*
* This is required because the fetch API will send a CORS preflight
* request if the request is cross-origin and has custom headers.
*
* nginx considers requests to "/" to be static, and doesn't pass the
* request to PHP, and so no OPTIONS request makes it to the API.
* we can bypass this, by making a request to `/index.php`.
*/
if ( hostname == 'api.wordpress.org' ) {
fetchUrl = fetchUrl.replace( /(\/[\d.]+\/)($|\?)/, '$1index.php$2' );
}

response = await fetchFn(fetchUrl, {
Expand Down
Loading