-
Notifications
You must be signed in to change notification settings - Fork 857
header_rewrite: fix set-body origin replacement across hooks and transforms #13116
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
bryancall
wants to merge
6
commits into
master
Choose a base branch
from
set-body-origin-response-clean
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
700f6bf
Enable robust set-body origin replacement across hooks and transforms.
bryancall 6a79af5
Address Copilot review feedback on transform safety and docs.
bryancall 6b115ce
Limit internal-body transform bypass to origin-response contexts.
bryancall ce43606
Restrict internal response-body override to response-hook contexts.
bryancall 112468f
Harden transform cleanup for set-body bypass path.
bryancall cf53d32
Document TRANSFORM_READ internal-body bypass flow.
bryancall 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -1687,9 +1687,37 @@ HttpSM::handle_api_return() | |
|
|
||
| switch (t_state.next_action) { | ||
| case HttpTransact::StateMachineAction_t::TRANSFORM_READ: { | ||
| HttpTunnelProducer *p = setup_transfer_from_transform(); | ||
| perform_transform_cache_write_action(); | ||
| tunnel.tunnel_run(p); | ||
| // A plugin has installed an internal response body (TSHttpTxnErrorBodySet()). | ||
| // In this branch we bypass transform streaming and switch to internal transfer. | ||
| if (t_state.internal_msg_buffer && !t_state.api_server_request_body_set && t_state.hdr_info.server_response.valid()) { | ||
| SMDbg(dbg_ctl_http, "plugin set internal body, bypassing response transform for internal transfer"); | ||
| t_state.api_info.cache_untransformed = true; | ||
| // If a tunnel was already set up for transform I/O, shut it down before we re-route. | ||
| if (tunnel.is_tunnel_active()) { | ||
| tunnel.kill_tunnel(); | ||
| } | ||
| // Drop transform VC table state because this path no longer drives transform reads. | ||
| if (transform_info.entry != nullptr) { | ||
| vc_table.cleanup_entry(transform_info.entry); | ||
| transform_info.entry = nullptr; | ||
| } | ||
| transform_info.vc = nullptr; | ||
| // Some downstream paths still read client_response; seed it from transform_response when missing. | ||
| if (t_state.hdr_info.client_response.valid() == 0 && t_state.hdr_info.transform_response.valid()) { | ||
| t_state.hdr_info.client_response.create(HTTPType::RESPONSE); | ||
| t_state.hdr_info.client_response.copy(&t_state.hdr_info.transform_response); | ||
| } | ||
| // The server session is not needed for internal body transfer if it was never tunneled. | ||
| if (server_entry != nullptr && server_entry->in_tunnel == false) { | ||
| release_server_session(); | ||
| } | ||
| // Serve the plugin-provided body through the internal tunnel handler. | ||
| setup_internal_transfer(&HttpSM::tunnel_handler); | ||
| } else { | ||
| HttpTunnelProducer *p = setup_transfer_from_transform(); | ||
| perform_transform_cache_write_action(); | ||
| tunnel.tunnel_run(p); | ||
| } | ||
| break; | ||
| } | ||
| case HttpTransact::StateMachineAction_t::SERVER_READ: { | ||
|
|
@@ -1722,6 +1750,17 @@ HttpSM::handle_api_return() | |
| } | ||
|
|
||
| setup_blind_tunnel(true, initial_data); | ||
| } else if (t_state.internal_msg_buffer && !t_state.api_server_request_body_set && t_state.hdr_info.server_response.valid() && | ||
| plugin_tunnel == nullptr && | ||
| (api_hooks.get(TS_HTTP_READ_RESPONSE_HDR_HOOK) != nullptr || | ||
| api_hooks.get(TS_HTTP_SEND_RESPONSE_HDR_HOOK) != nullptr)) { | ||
| // A plugin replaced the origin response body via TSHttpTxnErrorBodySet(). | ||
| // Serve the synthetic body before entering the response body tunnel. | ||
| SMDbg(dbg_ctl_http, "plugin set internal body, using internal transfer instead of server tunnel"); | ||
| if (server_entry != nullptr && server_entry->in_tunnel == false) { | ||
| release_server_session(); | ||
| } | ||
| setup_internal_transfer(&HttpSM::tunnel_handler); | ||
| } else { | ||
| HttpTunnelProducer *p = setup_server_transfer(); | ||
| perform_cache_write_action(); | ||
|
|
@@ -7578,12 +7617,19 @@ HttpSM::setup_client_request_plugin_agents(HttpTunnelProducer *p, int num_header | |
| inline void | ||
| HttpSM::transform_cleanup(TSHttpHookID hook, HttpTransformInfo *info) | ||
| { | ||
| if (info->entry == nullptr) { | ||
| return; | ||
| } | ||
| APIHook *t_hook = api_hooks.get(hook); | ||
| if (t_hook && info->vc == nullptr) { | ||
| do { | ||
| VConnection *t_vcon = t_hook->m_cont; | ||
| t_vcon->do_io_close(); | ||
| t_hook = t_hook->m_link.next; | ||
| APIHook *next = t_hook->m_link.next; | ||
| // Some transform hooks can already be detached by the time kill_this() runs. | ||
| // Guard against null continuations while still draining the remaining hooks. | ||
|
Contributor
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. The first comment seems useful, the second is slop. |
||
| if (auto *t_vcon = static_cast<VConnection *>(t_hook->m_cont); t_vcon != nullptr) { | ||
| t_vcon->do_io_close(); | ||
| } | ||
| t_hook = next; | ||
| } while (t_hook != nullptr); | ||
| } | ||
|
bryancall marked this conversation as resolved.
|
||
| } | ||
|
|
@@ -7664,7 +7710,11 @@ HttpSM::kill_this() | |
| // In that case, we need to manually close all the | ||
| // transforms to prevent memory leaks (INKqa06147) | ||
| if (hooks_set) { | ||
| transform_cleanup(TS_HTTP_RESPONSE_TRANSFORM_HOOK, &transform_info); | ||
| bool bypassed_response_transform = | ||
| t_state.api_info.cache_untransformed && t_state.internal_msg_buffer && !t_state.api_server_request_body_set; | ||
| if (!bypassed_response_transform) { | ||
| transform_cleanup(TS_HTTP_RESPONSE_TRANSFORM_HOOK, &transform_info); | ||
| } | ||
|
bryancall marked this conversation as resolved.
|
||
| transform_cleanup(TS_HTTP_REQUEST_TRANSFORM_HOOK, &post_transform_info); | ||
| plugin_agents_cleanup(); | ||
| } | ||
|
|
@@ -8230,6 +8280,21 @@ HttpSM::set_next_state() | |
| case HttpTransact::StateMachineAction_t::SERVER_READ: { | ||
| t_state.source = HttpTransact::Source_t::HTTP_ORIGIN_SERVER; | ||
|
|
||
| if (transform_info.vc && t_state.internal_msg_buffer && !t_state.api_server_request_body_set && | ||
| t_state.hdr_info.server_response.valid()) { | ||
| SMDbg(dbg_ctl_http, "plugin set internal body, bypassing response transform"); | ||
| t_state.api_info.cache_untransformed = true; | ||
| if (transform_info.entry != nullptr) { | ||
| vc_table.cleanup_entry(transform_info.entry); | ||
| transform_info.entry = nullptr; | ||
| } | ||
| transform_info.vc = nullptr; | ||
| if (t_state.hdr_info.client_response.valid() == 0 && t_state.hdr_info.transform_response.valid()) { | ||
| t_state.hdr_info.client_response.create(HTTPType::RESPONSE); | ||
| t_state.hdr_info.client_response.copy(&t_state.hdr_info.transform_response); | ||
| } | ||
| } | ||
|
|
||
| if (transform_info.vc) { | ||
| ink_assert(t_state.hdr_info.client_response.valid() == 0); | ||
| ink_assert((t_state.hdr_info.transform_response.valid() ? true : false) == true); | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.