Skip to content

Offline Mode: Reject external http requests when network is offline #1491

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

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions vendor/wp-now/src/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import dns from 'dns';
/**
* Network utility function for checking online status and detecting offline mode
*/

/**
* Check if the system is online by attempting to resolve a known host
* @param hostname - The hostname to check (defaults to 'google.com')
* @returns Promise<boolean> - true if online, false if offline
*/
export async function isOnline(hostname: string = 'google.com'): Promise<boolean> {
try {
await dns.promises.lookup(hostname);

Choose a reason for hiding this comment

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

any chance this would still be served from a dns cache when offline?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is definitely possible. However, I haven't faced any issues during the development and testing of this PR, so I cannot confirm definitively.

return true;
} catch (err) {
return false;
}
}
5 changes: 5 additions & 0 deletions vendor/wp-now/src/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PHP } from '@php-wasm/universal';
import startWPNow from './wp-now';
import { output } from './output';
import { addTrailingSlash } from './add-trailing-slash';
import { isOnline } from './network';

const requestBodyToBytes = async ( req ): Promise< Uint8Array > =>
await new Promise( ( resolve ) => {
Expand Down Expand Up @@ -83,6 +84,10 @@ export async function startServer( options: WPNowOptions = {} ): Promise< WPNowS
}
}

if ( req.url.startsWith('/wp-admin') && ! await isOnline() ) {
requestHeaders['STUDIO_IS_OFFLINE'] = 'true';
}

const data = {
url: req.url,
headers: requestHeaders,
Expand Down
26 changes: 26 additions & 0 deletions vendor/wp-now/src/wp-now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
getPluginFile,
readFileHead,
} from './wp-playground-wordpress';
import { isOnline } from './network';

export default async function startWPNow(
options: Partial< WPNowOptions > = {}
Expand Down Expand Up @@ -583,6 +584,23 @@ async function mountInternalMuPlugins( php: PHP, options: WPNowOptions ) {
);
}

php.writeFile(
path.posix.join( PLAYGROUND_INTERNAL_MU_PLUGINS_FOLDER, '0-block-offline-external-request.php' ),
`<?php
add_filter( 'pre_http_request', function( $preempt, $parsed_args, $url ) {
if ( isset( $_SERVER['HTTP_STUDIO_IS_OFFLINE'] ) ) {
$parsed_url = parse_url( $url );
$host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : $url;
return new WP_Error(
'request_failed',
sprintf( __( 'Could not resolve host: %s' ), $host )
);
}
return $preempt;
}, 10, 3 );
`
);

php.writeFile(
path.posix.join( PLAYGROUND_INTERNAL_MU_PLUGINS_FOLDER, '0-http-request-timeout.php' ),
`<?php
Expand Down Expand Up @@ -676,9 +694,16 @@ async function installationSteps( php: PHP, options: WPNowOptions ) {
const siteLanguage = options.siteLanguage;

const executeStep = async ( step: 0 | 1 | 2 ) => {
const requestHeaders = {};

if ( ! await isOnline() ) {
requestHeaders['STUDIO_IS_OFFLINE'] = 'true';
}

return php.requestHandler.request( {
url: `/wp-admin/install.php?step=${ step }`,
method: 'POST',
headers: requestHeaders,
body:
step === 2
? {
Expand All @@ -695,6 +720,7 @@ async function installationSteps( php: PHP, options: WPNowOptions ) {
: {
language: siteLanguage,
},

} );
};
// First two steps are needed to download and set translations
Expand Down
Loading