Repository navigation
feat(logging): add opt-in logging of WordPress REST API requests - #863
Draft
josephfusco wants to merge 8 commits into
Draft
josephfusco wants to merge 8 commits into
josephfusco wants to merge 8 commits into
Conversation
Log REST API requests through the existing logger pipeline when the new "Log REST API Requests" setting is enabled (off by default). Entries are written to the wpgraphql_logging_rest channel, reuse the enabled, IP restriction, sampling and log response settings, and support a new "Exclude REST Routes" prefix list.
Contributor
📦 Plugin Artifacts Ready!Download from GitHub Actions run Available plugins:
See the "Artifacts" section at the bottom of the Actions run page |
- Accept mixed hook arguments so a third-party apply_filters() call with unexpected values cannot fatal a REST response. - Guard with is_graphql_http_request() instead of is_graphql_request(), which is also true while a REST callback runs graphql() and would have dropped those REST entries. - Skip CORS preflight (OPTIONS) requests. - Do not store error bodies twice when Log Response is enabled. - Anonymize response.password in the recommended rules so a newly created application password is not logged in plain text.
Fill coverage gaps for the REST logger (rule filters, Log Points not applying, timer reset and nesting, undispatched requests) and add tests for the new settings fields. Writing these exposed that route exclusions matched raw string prefixes, so /wp/v2/users also excluded /wp/v2/users-extra, and were case-sensitive while WordPress matches routes case-insensitively. Exclusions now match the route and the routes below it, ignoring case.
…EST logging - Let the REST logger override a get_logging_rules() hook in LoggingHelper instead of copying the rule manager setup. - Log only the parameters the client sent, not every endpoint default, to keep REST entries small. - Document the REST settings, what is logged, route exclusions, sanitization, filtering by channel and the extension points.
The REST channel logger is a shared singleton behind a BufferHandler. test_serve_filter_value_is_returned_unchanged wrote two records without reading them, so they stayed buffered past tearDown's delete and were flushed into the next test, which then saw three entries instead of one. Flush the buffer in tearDown before deleting entries.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
REST logging exposes reusable credentials through unredacted authorization headers, cookies, server fields, and nested batch or enveloped payloads despite recommended sanitization.
2 open issues
What changed in this PR
Extends WPGraphQL Logging to support opt-in WordPress REST API request logging through the existing logging pipeline and admin screen, addressing #799.
Changes:
- Adds a dedicated REST channel, request timing, error logging and optional response capture.
- Adds REST enablement and route-exclusion settings, plus recommended sanitization rules for top-level REST fields.
- Adds tests and documentation covering configuration, logging behavior and customization.
| File | Description |
|---|---|
plugins/wpgraphql-logging/tests/wpunit/Logger/Rules/LogRestRequestsRuleTest.php |
Tests REST logging enablement. |
plugins/wpgraphql-logging/tests/wpunit/Logger/Rules/ExcludeRestRouteRuleTest.php |
Tests route exclusions and normalization. |
plugins/wpgraphql-logging/tests/wpunit/Logger/Processors/DataSanizationProcessorTest.php |
Tests top-level REST credential masking. |
plugins/wpgraphql-logging/tests/wpunit/Events/RestRequestLoggerTest.php |
Tests REST logging, filtering, timing and integration. |
plugins/wpgraphql-logging/tests/wpunit/Core/PluginTest.php |
Verifies REST logging defaults to disabled. |
plugins/wpgraphql-logging/tests/wpunit/Admin/Settings/Fields/Tab/BasicConfigurationTabTest.php |
Tests REST settings registration and sanitization. |
plugins/wpgraphql-logging/src/Plugin.php |
Initializes REST logging and its disabled default. |
plugins/wpgraphql-logging/src/Logger/Rules/LogRestRequestsRule.php |
Adds the REST enablement rule. |
plugins/wpgraphql-logging/src/Logger/Rules/ExcludeRestRouteRule.php |
Adds route-prefix exclusions. |
plugins/wpgraphql-logging/src/Logger/Processors/DataSanitizationProcessor.php |
Adds recommended rules for REST credentials. |
plugins/wpgraphql-logging/src/Logger/LoggingHelper.php |
Extracts rule selection for REST-specific overrides. |
plugins/wpgraphql-logging/src/Events/RestRequestLogger.php |
Captures served REST requests on a dedicated channel. |
plugins/wpgraphql-logging/src/Events/Events.php |
Defines the REST logging event. |
plugins/wpgraphql-logging/src/Admin/Settings/Fields/Tab/BasicConfigurationTab.php |
Adds REST logging and route-exclusion controls. |
llms.txt |
Links the REST logging guide. |
docs/plugins/wpgraphql-logging/index.md |
Documents REST support and settings. |
docs/plugins/wpgraphql-logging/how-to/log-rest-requests/index.md |
Explains setup, logged data and customization. |
Review effort: Balanced
| return null; | ||
| } | ||
|
|
||
| self::$instance = new self( LoggerService::get_instance( self::CHANNEL ), $config ); |
Comment on lines
+73
to
+76
| 'params.username' => 'anonymize', | ||
| 'params.password' => 'anonymize', | ||
| 'params.email' => 'anonymize', | ||
| 'response.password' => 'anonymize', // e.g. a new REST application password. |
Contributor
There was a problem hiding this comment.
Added recommended sanitization for batch request passwords, batch response passwords, and enveloped response passwords, with regression coverage. Commit: c59e1d5.
…port # Conflicts: # docs/plugins/wpgraphql-logging/index.md
Co-authored-by: josephfusco <6676674+josephfusco@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Adds an opt-in setting, off by default, that logs WordPress REST API requests through the existing logger on their own channel. Request headers, including
AuthorizationandCookie, are stored unsanitized today, so that should be fixed before this ships.Closes #799
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Assisting with adding REST API request logging.