-
Notifications
You must be signed in to change notification settings - Fork 7
Context propagation primitives for async task execution #342
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
Open
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions
24
agent_api/src/main/java/dev/aikido/agent_api/context/ContextPropagatingCallable.java
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package dev.aikido.agent_api.context; | ||
|
|
||
| import java.util.concurrent.Callable; | ||
|
|
||
| public final class ContextPropagatingCallable<T> implements Callable<T> { | ||
| private final Callable<T> delegate; | ||
| private final ContextObject context; | ||
|
|
||
| public ContextPropagatingCallable(Callable<T> delegate, ContextObject context) { | ||
| this.delegate = delegate; | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public T call() throws Exception { | ||
| ContextObject previous = Context.get(); | ||
| try { | ||
| Context.set(context); | ||
| return delegate.call(); | ||
| } finally { | ||
| Context.restore(previous); | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
agent_api/src/main/java/dev/aikido/agent_api/context/ContextPropagatingRunnable.java
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package dev.aikido.agent_api.context; | ||
|
|
||
| public final class ContextPropagatingRunnable implements Runnable { | ||
| private final Runnable delegate; | ||
| private final ContextObject context; | ||
|
|
||
| public ContextPropagatingRunnable(Runnable delegate, ContextObject context) { | ||
| this.delegate = delegate; | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| ContextObject previous = Context.get(); | ||
| try { | ||
| Context.set(context); | ||
| delegate.run(); | ||
| } finally { | ||
| Context.restore(previous); | ||
| } | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
agent_api/src/main/java/dev/aikido/agent_api/context/ContextPropagation.java
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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package dev.aikido.agent_api.context; | ||
|
|
||
| import java.util.concurrent.Callable; | ||
|
|
||
| public final class ContextPropagation { | ||
| private ContextPropagation() {} | ||
|
|
||
| public static Runnable wrap(Runnable task) { | ||
| if (task == null || task instanceof ContextPropagatingRunnable) { | ||
| return task; | ||
| } | ||
|
|
||
| ContextObject context = Context.get(); | ||
| if (context == null) { | ||
| return task; | ||
| } | ||
|
|
||
| return new ContextPropagatingRunnable(task, context.copyForPropagation()); | ||
| } | ||
|
|
||
| public static <T> Callable<T> wrap(Callable<T> task) { | ||
| if (task == null || task instanceof ContextPropagatingCallable) { | ||
| return task; | ||
| } | ||
|
|
||
| ContextObject context = Context.get(); | ||
| if (context == null) { | ||
| return task; | ||
| } | ||
|
|
||
| return new ContextPropagatingCallable<>(task, context.copyForPropagation()); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
🟡 Medium - Re-wrapping a retained task keeps the first request's context
ContextPropagation.wrap(...)returns an existingContextPropagatingRunnable/Callableunchanged instead of rebinding it to the current request. If an application keeps a task instance and callswrap()before submitting it for later requests, the wrapper continues to install the originalContextObject, so later async work inherits stale route, user, IP, andforcedProtectionOffstate. That can misattribute attack reports to the wrong request and, when the first request had protection forced off, skip vulnerability scanning for subsequent requests.Show fix
Do not treat already-wrapped tasks as safe to reuse across requests. Either always create a fresh wrapper for each
wrap()call, or unwrap and rebind the delegate when the currentContextObjectdiffers from the one captured previously so each submission propagates the caller's current request context.More info - Reply on this comment to give feedback or ignore the issue.
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.
the wrapper is created inside the agent at submit time and never handed back to the app, so a task is only "already wrapped" within the same submit (same request/context). An app can't retain and resubmit a wrapped task across requests, so the stale-context path is unreachable.