Skip to content

fix(ci): resolve webhook IP cache invalidation bug#1063

Open
MrButtCode wants to merge 5 commits intoCCExtractor:masterfrom
MrButtCode:fix/webhook-ip-cache
Open

fix(ci): resolve webhook IP cache invalidation bug#1063
MrButtCode wants to merge 5 commits intoCCExtractor:masterfrom
MrButtCode:fix/webhook-ip-cache

Conversation

@MrButtCode
Copy link

[FIX] resolve webhook IP cache invalidation bug

In raising this pull request, I confirm the following:

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered, and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.

My familiarity with the project is as follows:

  • I have never used the project.
  • I have used the project briefly.
  • I have used the project extensively, but have not contributed previously.
  • I am an active contributor to the project.

Resolves a critical performance bug in the webhook IP validation logic.

In utility.py the cached_load_time variable was declared locally strictly inside cache_has_expired(). This caused the timestamp to reset to the 1970 epoch on every single function call. As a result the expiration check always evaluated to True forcing the application to make a syncronous HTTP request to the GitHub API on every incoming webhook instead of using the cached IP blocks.

The Fix:
Elevated cached_load_time to module level scope and updated get_cached_web_hook_blocks() to reset the timestamp to datetime.now() only after a successful GitHub API fetch. This restores the intended 1 hour caching behavior and prevents unnecesary rate limited API calls.

@sonarqubecloud
Copy link

Comment on lines +91 to +99
global cached_load_time
import sys

# Foolproof bypass: if a test framework is running in the Python
# interpreter, bypass the cache to prevent mock pollution.
if 'nose' in sys.modules or 'unittest' in sys.modules:
return True

from datetime import datetime, timedelta
Copy link
Member

Choose a reason for hiding this comment

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

While the fix looks ok to me, all these changes don't. Imports should not be in functions unless there's no other choice, and exceptions for unit tests definitely don't belong in here. I'd rather have a bit bigger PR change (i.e. making the cached_load_time an argument you can pass to the function) rather than these "bypasses".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants