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

Add log suppression to VIP_Request_Block #5478

Merged
merged 12 commits into from
May 9, 2024

Conversation

maxschmeling
Copy link
Contributor

@maxschmeling maxschmeling commented Apr 19, 2024

Description

This PR introduces a new method that allows suppression of the logs. In case it's too noisy a new helper can be used in vip-config.php

/* vip-config.php */
// Supress the logs
VIP_Request_Block::should_log( false );

// Resume the logs
VIP_Request_Block::should_log( true );

Changelog Description

VIP_Request_Block Log Output Control

Added an option to suppress logging for requests blocked by VIP_Request_Block:

/* In vip-config.php */

// Supress the logs
VIP_Request_Block::should_log( false );

// Resume the logs
VIP_Request_Block::should_log( true );

Pre-review checklist

Please make sure the items below have been covered before requesting a review:

  • This change works and has been tested locally (or has an appropriate fallback).
  • This change works and has been tested on a Go sandbox.
  • This change has relevant unit tests (if applicable).
  • This change uses a rollout method to ease with deployment (if applicable - especially for large scale actions that require writes).
  • This change has relevant documentation additions / updates (if applicable).
  • I've created a changelog description that aligns with the provided examples.

Pre-deploy checklist

  • VIP staff: Ensure any alerts added/updated conform to internal standards (see internal documentation).

Steps to Test

  1. Check out the PR
  2. Spin up a dev environment
  3. Add VIP_Request_Block::should_log( false ); to vip-config.php
  4. Add a block for a local request (I used user-agent, but there's probably a simpler option)
  5. Make a request, verify it was blocked, then verify no log entry for the block
  6. Remove the VIP_Request_Block::should_log( false ); line, repeat step 5, verify the block was logged.

Copy link

codecov bot commented Apr 19, 2024

Codecov Report

Attention: Patch coverage is 20.00000% with 8 lines in your changes are missing coverage. Please review.

Project coverage is 29.42%. Comparing base (e52f028) to head (8778670).

Files Patch % Lines
lib/class-vip-request-block.php 20.00% 8 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #5478      +/-   ##
=============================================
+ Coverage      29.41%   29.42%   +0.01%     
- Complexity      4759     4764       +5     
=============================================
  Files            281      281              
  Lines          20533    20530       -3     
=============================================
+ Hits            6039     6041       +2     
+ Misses         14494    14489       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maxschmeling maxschmeling marked this pull request as ready for review April 19, 2024 16:32
@maxschmeling maxschmeling requested a review from a team as a code owner April 19, 2024 16:32
@maxschmeling
Copy link
Contributor Author

maxschmeling commented Apr 19, 2024

I wanted to add a test for this change, but there currently isn't a test for the error_log call and from what Copilot told me, I'd have to introduce a wrapper for it that can be mocked and then pass that into VIP_Request_Block . I can do that, but wanted to get feedback first.

@sjinks
Copy link
Member

sjinks commented Apr 19, 2024

Yes, that is correct.

@sjinks
Copy link
Member

sjinks commented Apr 19, 2024

Maybe smth like

public static function log( string $criteria, string $value ): void {
    // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
    error_log( sprintf( 'VIP Request Block: request was blocked based on "%s" with value of "%s"', $criteria, $value ) );
}
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
-				error_log( sprintf( 'VIP Request Block: request was blocked based on "%s" with value of "%s"', $criteria, $value ) );
+				static::log( $criteria, $value );

Then in the test:

class MyRequestBlock extends VIP_Request_Block {
    static $log_called = false;

    public static function log( string $criteria, string $value ): void {
        self::$log_called = true;
    }
}

and in the test case in setUp() (or wherever is appropriate) you set MyRequestBlock::$log_called = false; and in a test case you do static::assertTrue( MyRequestBlock::$log_called ); or static::assertFalse( MyRequestBlock::$log_called );

This is probably easier than mocking.

@maxschmeling
Copy link
Contributor Author

@sjinks I've tried a few different things but couldn't get the test to work. I haz no PHP skills.

If you could give me a little help getting that across the finish line, that would be much appreciated. Thanks

@rinatkhaziev rinatkhaziev added the [Status] Needs Docs Signal to Yoli the docs need to be updated label Apr 29, 2024
@rinatkhaziev
Copy link
Contributor

@yolih for awareness

@rebeccahum rebeccahum force-pushed the develop branch 2 times, most recently from eec6e85 to 0b231ce Compare April 30, 2024 13:24
@rinatkhaziev rinatkhaziev force-pushed the add/BB8-11232-suppress-request-block-logs branch from 4c8f120 to aa1ccb0 Compare May 7, 2024 16:55
Copy link

sonarcloud bot commented May 8, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@rinatkhaziev rinatkhaziev merged commit 98fd1d3 into develop May 9, 2024
38 of 39 checks passed
@rinatkhaziev rinatkhaziev deleted the add/BB8-11232-suppress-request-block-logs branch May 9, 2024 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Status] Deployed to production [Status] Needs Docs Signal to Yoli the docs need to be updated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants