-
Notifications
You must be signed in to change notification settings - Fork 319
Fix semaphore permit leak in API Security span post-processor #10010
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
jandro996
wants to merge
15
commits into
master
Choose a base branch
from
alejandro.gonzalez/APPSEC-57815
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
54e8a5d
fix
jandro996 ebcd198
fix spotless
jandro996 a5adf0e
not sure
jandro996 4f0dbb7
add logs
jandro996 7d9ad68
Merge branch 'master' into alejandro.gonzalez/APPSEC-57815
jandro996 7127442
fix logs
jandro996 3337e0a
fix PROPAGATED_TRACE_SOURCE
jandro996 2f32571
fix PROPAGATED_TRACE_SOURCE and test
jandro996 8f0abf6
change logs
jandro996 fe75494
change logs
jandro996 cc5a6bc
iteration 1
jandro996 bc5f9b3
iteration 2
jandro996 7d7782a
fix tests
jandro996 4d1ba30
self review
jandro996 69a96c9
Merge branch 'master' into alejandro.gonzalez/APPSEC-57815
jandro996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,20 +31,26 @@ public AppSecSpanPostProcessor(ApiSecuritySampler sampler, EventProducerService | |
|
|
||
| @Override | ||
| public void process(@Nonnull AgentSpan span, @Nonnull BooleanSupplier timeoutCheck) { | ||
| final RequestContext ctx_ = span.getRequestContext(); | ||
| if (ctx_ == null) { | ||
| return; | ||
| } | ||
| final AppSecRequestContext ctx = ctx_.getData(RequestContextSlot.APPSEC); | ||
| if (ctx == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (!ctx.isKeepOpenForApiSecurityPostProcessing()) { | ||
| return; | ||
| } | ||
| AppSecRequestContext ctx = null; | ||
| RequestContext ctx_ = null; | ||
| boolean needsRelease = false; | ||
|
|
||
| try { | ||
| ctx_ = span.getRequestContext(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand why introducing this logic in the try/catch helps, in the end |
||
| if (ctx_ == null) { | ||
| return; | ||
| } | ||
| ctx = ctx_.getData(RequestContextSlot.APPSEC); | ||
| if (ctx == null) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if we acquired a permit for this request - must be inside try to ensure finally runs | ||
| needsRelease = ctx.isKeepOpenForApiSecurityPostProcessing(); | ||
| if (!needsRelease) { | ||
| return; | ||
| } | ||
|
|
||
| if (timeoutCheck.getAsBoolean()) { | ||
| log.debug("Timeout detected, skipping API security post-processing"); | ||
| return; | ||
|
|
@@ -56,17 +62,25 @@ public void process(@Nonnull AgentSpan span, @Nonnull BooleanSupplier timeoutChe | |
| log.debug("Request sampled, processing API security post-processing"); | ||
| extractSchemas(ctx, ctx_.getTraceSegment()); | ||
| } finally { | ||
| ctx.setKeepOpenForApiSecurityPostProcessing(false); | ||
| try { | ||
| // XXX: Close the additive first. This is not strictly needed, but it'll prevent getting it | ||
| // detected as a | ||
| // missed request-ended event. | ||
| ctx.closeWafContext(); | ||
| ctx.close(); | ||
| } catch (Exception e) { | ||
| log.debug("Error closing AppSecRequestContext", e); | ||
| // Always release the semaphore permit if we acquired one | ||
| if (needsRelease) { | ||
| if (ctx != null) { | ||
| ctx.setKeepOpenForApiSecurityPostProcessing(false); | ||
| // XXX: Close the additive first. This is not strictly needed, but it'll prevent getting | ||
| // it detected as a missed request-ended event. | ||
| try { | ||
| ctx.closeWafContext(); | ||
| } catch (Exception e) { | ||
| log.debug("Error closing WAF context", e); | ||
| } | ||
| try { | ||
| ctx.close(); | ||
| } catch (Exception e) { | ||
| log.debug("Error closing AppSecRequestContext", e); | ||
| } | ||
| } | ||
| sampler.releaseOne(); | ||
| } | ||
| sampler.releaseOne(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method seems useless with the new approach but, I decided to maintain it to keep the checks although updateApiAccessIfExpired is not necessary anymore