diff --git a/.distignore b/.distignore new file mode 100644 index 0000000..759c6fa --- /dev/null +++ b/.distignore @@ -0,0 +1,7 @@ +/.git +/.github +/.wordpress.org +/.distignore +/tests +/README.md +/composer.json diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6dad457..da73546 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -5,31 +5,34 @@ on: tags: - "v*" +permissions: + contents: read + jobs: tag: name: New tag runs-on: ubuntu-latest + permissions: + contents: read steps: - - uses: actions/checkout@main + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: WordPress Plugin Deploy - uses: 10up/action-wordpress-plugin-deploy@stable + uses: 10up/action-wordpress-plugin-deploy@54bd289b8525fd23a5c365ec369185f2966529c2 # 2.3.0 env: SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }} SVN_USERNAME: ${{ secrets.SVN_USERNAME }} -# SLUG: pp-disable-xml-rpc # optional, remove if GitHub repo name matches SVN slug, including capitalization build: name: Create Release + needs: tag runs-on: ubuntu-latest + permissions: + contents: write steps: - - name: Checkout code - uses: actions/checkout@v2 - name: Create Release - id: create_release - uses: actions/create-release@v1 env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token - with: - tag_name: ${{ github.ref }} - release_name: Release ${{ github.ref }} - draft: false - prerelease: false + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + RELEASE_TAG: ${{ github.ref_name }} + run: gh release create "$RELEASE_TAG" --verify-tag --title "Release $RELEASE_TAG" --notes "" diff --git a/.github/workflows/lint-php.yml b/.github/workflows/lint-php.yml index d834de8..53c98f7 100644 --- a/.github/workflows/lint-php.yml +++ b/.github/workflows/lint-php.yml @@ -7,6 +7,9 @@ on: pull_request: workflow_dispatch: +permissions: + contents: read + concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true @@ -22,10 +25,12 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Setup PHP - uses: shivammathur/setup-php@v2 + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # v2 with: php-version: ${{ matrix.php }} tools: parallel-lint @@ -33,3 +38,9 @@ jobs: - name: Lint PHP files run: parallel-lint . + + - name: Test plugin bootstrap + run: | + for scenario in direct xmlrpc normal false; do + php tests/plugin.php "$scenario" || exit 1 + done diff --git a/.github/workflows/playground.yml b/.github/workflows/playground.yml index 2539fa9..f7781c2 100644 --- a/.github/workflows/playground.yml +++ b/.github/workflows/playground.yml @@ -3,6 +3,9 @@ name: Playground Comment on: pull_request: +permissions: + contents: read + jobs: playground: runs-on: ubuntu-latest @@ -14,16 +17,19 @@ jobs: LANDING_PAGE: "/wp-admin/" steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false # Prepare a folder named exactly like the repo as the plugin root. # If the repo already has such a folder (common for WP plugins), use it. # Otherwise, stage one and copy root files into it (excluding CI junk). - name: Prepare plugin folder id: prep + env: + REPO: ${{ github.event.repository.name }} run: | set -euxo pipefail - REPO="${{ github.event.repository.name }}" if [ -d "$REPO" ]; then # Plugin already lives in a subfolder named like the repo echo "PKG_DIR=$REPO" >> "$GITHUB_OUTPUT" @@ -35,6 +41,7 @@ jobs: --exclude='.git' \ --exclude='.github' \ --exclude='node_modules' \ + --exclude='tests' \ --exclude="${STAGE}" \ ./ "$STAGE/$REPO/" echo "PKG_DIR=$STAGE/$REPO" >> "$GITHUB_OUTPUT" @@ -43,7 +50,7 @@ jobs: # Upload the FOLDER (not a .zip). The artifact service zips it for us, # keeping the top-level folder name inside the archive. - name: Upload plugin artifact - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: ${{ github.event.repository.name }} path: ${{ steps.prep.outputs.PKG_DIR }} @@ -51,9 +58,12 @@ jobs: compression-level: 0 # Comment with a Playground link that installs from the artifact ZIP - - uses: mshick/add-pr-comment@v2 + - uses: mshick/add-pr-comment@b8f338c590a895d50bcbfa6c5859251edc8952fc # v2 + if: github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' with: message: | **Test on Playground** [Test this pull request on the Playground](https://playground.wordpress.net/#{"landingPage":"${{ env.LANDING_PAGE }}","features":{"networking":true},"steps":[{"step":"defineWpConfigConsts","consts":{"IS_PLAYGROUND_PREVIEW":true}},{"step":"login","username":"admin","password":"password"},{"step":"installPlugin","pluginZipFile":{"resource":"url","url":"https://nightly.link/${{ github.repository }}/actions/runs/${{ github.run_id }}/${{ github.event.repository.name }}.zip"},"options":{"activate":true}}]}) or [download the zip](https://nightly.link/${{ github.repository }}/actions/runs/${{ github.run_id }}/${{ github.event.repository.name }}.zip) + + This disposable preview downloads the PR artifact through the third-party nightly.link service; do not enter production credentials or private data. diff --git a/.github/workflows/wpcs.yml b/.github/workflows/wpcs.yml index b3ade22..6606733 100644 --- a/.github/workflows/wpcs.yml +++ b/.github/workflows/wpcs.yml @@ -7,6 +7,9 @@ on: pull_request: workflow_dispatch: +permissions: + contents: read + concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true @@ -17,12 +20,13 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 + persist-credentials: false - name: WPCS check - uses: 10up/wpcs-action@stable + uses: 10up/wpcs-action@39f1a9eb6139795e6798d11b51eeecf5a9678ccc # stable with: enable_warnings: true standard: WordPress diff --git a/README.md b/README.md index 1c34298..a05dba8 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Once activated, the plugin: - disables WordPress XML-RPC through the relevant filter - blocks XML-RPC requests directly -- shows a clear “XML-RPC Disabled” response when access is attempted +- returns HTTP 403 with an empty body when XML-RPC access is attempted There are no settings to configure. @@ -72,6 +72,28 @@ This is a deliberately small utility plugin. If you want to review behavior, sta - `pp-disable-xml-rpc.php` +XML-RPC requests receive HTTP 403 and stop when this plugin loads. Some WordPress +bootstrap work has already happened at that point. For protection against request +floods, block `xmlrpc.php` at your web server or edge before PHP runs. + +Releases are deployed by pushing a `v*` tag. The repository's active release-tag +ruleset restricts creating, changing, and deleting these tags to repository admins. +Review the tagged commit before pushing it. The GitHub release is created only +after the WordPress.org deployment succeeds. + +Playground previews use nightly.link to serve the workflow artifact publicly. +Use them only as disposable test sites without production credentials or private +data. Fork and Dependabot PRs still upload artifacts but skip posting comments +because their workflow tokens cannot write PR comments. + +Run the plugin regression checks with: + +```sh +for scenario in direct xmlrpc normal false; do + php tests/plugin.php "$scenario" || exit 1 +done +``` + ## License MIT. See [LICENSE](LICENSE). diff --git a/pp-disable-xml-rpc.php b/pp-disable-xml-rpc.php index d5df1bc..cb056ce 100644 --- a/pp-disable-xml-rpc.php +++ b/pp-disable-xml-rpc.php @@ -18,33 +18,14 @@ * Text Domain: pp-disable-xml-rpc */ -// Disable XML-RPC using the 'xmlrpc_enabled' filter. -add_filter( 'xmlrpc_enabled', '__return_false' ); - -/** - * Die to prevent XMLRPC requests. - */ -function pp_disable_xml_rpc_die() { - wp_die( - __( 'XML-RPC services are disabled on this site.', 'pp-disable-xml-rpc' ), - __( 'XML-RPC Disabled', 'pp-disable-xml-rpc' ), - array( 'response' => 403 ) - ); -} +defined( 'ABSPATH' ) || exit; -// Add an empty class to bypass Core. -class pp_wp_xmlrpc_server { - public function serve_request() { - pp_disable_xml_rpc_die(); - } +// Stop XML-RPC during plugin loading, before the remaining WordPress bootstrap. +// The XML-RPC wp_die() handler cannot send this response before its server exists. +if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { + status_header( 403 ); + exit; } -add_filter( 'wp_xmlrpc_server_class', function() { - return 'pp_wp_xmlrpc_server'; -} ); -// Bail early if XMLRPC_REQUEST is defined. -add_action( 'init', function() { - if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { - pp_disable_xml_rpc_die(); - } -} ); \ No newline at end of file +// Disable XML-RPC using the 'xmlrpc_enabled' filter. +add_filter( 'xmlrpc_enabled', '__return_false' ); diff --git a/tests/plugin.php b/tests/plugin.php new file mode 100644 index 0000000..00c88d7 --- /dev/null +++ b/tests/plugin.php @@ -0,0 +1,71 @@ + '__return_false' ) ) === $filters; + fwrite( $valid ? STDOUT : STDERR, ( $valid ? 'PASS: ' : 'FAIL: ' ) . $scenario . "\n" ); + exit( $valid ? 0 : 1 ); + } +); + +/** + * Record filter registration without bootstrapping WordPress. + * + * @param string $hook Filter name. + * @param string $callback Callback name. + */ +function add_filter( $hook, $callback ) { + $GLOBALS['filters'][ $hook ] = $callback; +} + +/** + * Set a response code without bootstrapping WordPress. + * + * @param int $status HTTP response code. + */ +function status_header( $status ) { + http_response_code( $status ); +} + +http_response_code( 200 ); +ob_start(); +if ( 'direct' !== $scenario ) { + define( 'ABSPATH', __DIR__ . '/' ); +} +if ( in_array( $scenario, array( 'xmlrpc', 'false' ), true ) ) { + define( 'XMLRPC_REQUEST', 'xmlrpc' === $scenario ); +} + +require dirname( __DIR__ ) . '/pp-disable-xml-rpc.php'; +$completed = true;