mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-11-16 05:31:14 +01:00
Version the stored request on send, not the one being sent
A plugin can send a request it has already rendered, such as the response template function. resolve_send_inputs snapshotted whatever request it was handed, so those versions held rendered values, secrets included, where the user had templates, and a restore would have written the literals back. snapshot_request_for_send now takes a request id and reads the stored row inside its transaction. A request with no id or no stored row gets no version, as a failed snapshot already did. gRPC and WebSocket connect go through the same method, so all three capture the same thing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
07662257c1
commit
34d0aed906
4 files changed
+100
-31
No files matched your search
@@ -40,7 +40,6 @@ use yaak_models::models::{
|
||||
CookieJar, Environment, GrpcConnection, GrpcConnectionState, GrpcEvent, GrpcEventType,
|
||||
HttpRequest, HttpResponse, HttpResponseState, Workspace,
|
||||
};
|
||||
use yaak_models::queries::any_request::AnyRequest;
|
||||
use yaak_models::util::{BatchUpsertResult, ImportDestination, ImportPlan, UpdateSource};
|
||||
use yaak_plugins::events::{
|
||||
Color, ErrorResponse, FilterResponse, InternalEvent, InternalEventPayload, PluginContext,
|
||||
@@ -335,8 +334,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
let conn = app_handle.with_tx(|tx| {
|
||||
// Capture the stored request, not the rendered one: what a restore should
|
||||
// put back is what the user typed
|
||||
let version_id =
|
||||
tx.snapshot_request_for_send(&AnyRequest::GrpcRequest(unrendered_request.clone()));
|
||||
let version_id = tx.snapshot_request_for_send(&unrendered_request.id);
|
||||
tx.upsert_grpc_connection(
|
||||
&GrpcConnection {
|
||||
workspace_id: request.workspace_id.clone(),
|
||||
|
||||
@@ -21,7 +21,6 @@ use yaak_models::models::{
|
||||
HttpResponseHeader, WebsocketConnection, WebsocketConnectionState, WebsocketEvent,
|
||||
WebsocketEventType,
|
||||
};
|
||||
use yaak_models::queries::any_request::AnyRequest;
|
||||
use yaak_models::util::UpdateSource;
|
||||
use yaak_plugins::events::{CallHttpAuthenticationRequest, HttpHeader, RenderPurpose};
|
||||
use yaak_plugins::template_callback::PluginTemplateCallback;
|
||||
@@ -176,8 +175,7 @@ pub async fn cmd_ws_connect<R: Runtime>(
|
||||
let connection = app_handle.with_tx(|tx| {
|
||||
// Capture the stored request, not the rendered one: what a restore should
|
||||
// put back is what the user typed
|
||||
let version_id =
|
||||
tx.snapshot_request_for_send(&AnyRequest::WebsocketRequest(unrendered_request.clone()));
|
||||
let version_id = tx.snapshot_request_for_send(&unrendered_request.id);
|
||||
tx.upsert_websocket_connection(
|
||||
&WebsocketConnection {
|
||||
workspace_id: request.workspace_id.clone(),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::client_db::{ClientDb, WriteDb};
|
||||
use crate::content::content_hash;
|
||||
use crate::error::Result;
|
||||
use crate::error::{Error, Result};
|
||||
use crate::models::{
|
||||
GrpcRequest, HttpRequest, ModelVersion, ModelVersionIden, ModelVersionReason, UpsertModelInfo,
|
||||
WebsocketRequest,
|
||||
@@ -106,14 +106,28 @@ impl<'a> WriteDb<'a> {
|
||||
|
||||
/// What every send calls: capture the request, and don't make a fuss.
|
||||
///
|
||||
/// Takes an id rather than a request because the request a send holds is
|
||||
/// not always the stored one: a plugin can send a request it has already
|
||||
/// rendered, and a version of that would hold literal values, secrets
|
||||
/// included, where the user wrote templates. The stored row is what a
|
||||
/// restore should put back, so that is what gets captured.
|
||||
///
|
||||
/// A send is not worth failing over history that couldn't be written, and
|
||||
/// a request with no id is ephemeral and has nothing to version. Either way
|
||||
/// the response just has no version to offer.
|
||||
pub fn snapshot_request_for_send(&self, request: &AnyRequest) -> Option<String> {
|
||||
if request.id().is_empty() {
|
||||
/// a request with no id or no stored row is ephemeral and has nothing to
|
||||
/// version. Either way the response just has no version to offer.
|
||||
pub fn snapshot_request_for_send(&self, request_id: &str) -> Option<String> {
|
||||
if request_id.is_empty() {
|
||||
return None;
|
||||
}
|
||||
match self.snapshot_request(request, ModelVersionReason::Send) {
|
||||
let stored = match self.get_any_request(request_id) {
|
||||
Ok(stored) => stored,
|
||||
Err(Error::ModelNotFound(_)) => return None,
|
||||
Err(err) => {
|
||||
warn!("Failed to load request to snapshot before send: {err}");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
match self.snapshot_request(&stored, ModelVersionReason::Send) {
|
||||
Ok(version) => Some(version.id),
|
||||
Err(err) => {
|
||||
warn!("Failed to snapshot request before send: {err}");
|
||||
|
||||
+78
-19
@@ -27,7 +27,6 @@ use yaak_models::models::{
|
||||
HttpResponseEvent, HttpResponseEventData, HttpResponseHeader, HttpResponseState, ProxySetting,
|
||||
ProxySettingAuth, ResolvedHttpRequestSettings,
|
||||
};
|
||||
use yaak_models::queries::any_request::AnyRequest;
|
||||
use yaak_models::query_manager::QueryManager;
|
||||
use yaak_models::render::render_http_request;
|
||||
use yaak_models::util::{UpdateSource, generate_prefixed_id};
|
||||
@@ -437,28 +436,24 @@ pub fn resolve_send_inputs(
|
||||
client_certificates: settings.client_certificates,
|
||||
},
|
||||
cookie_store: cookies.map(CookieStore::from_cookies),
|
||||
// Captured here rather than deeper in the send because this is the last place that
|
||||
// still holds the *stored* request: further down it has been resolved against its
|
||||
// folder and workspace and then rendered, and neither of those is what a restore
|
||||
// should put back. Every host reaches sending through this function — the desktop,
|
||||
// the CLI, plugin-triggered sends — so every response gets a version without each
|
||||
// of them remembering to ask for one.
|
||||
version_id: snapshot_request_for_send(query_manager, request),
|
||||
// Every host reaches sending through this function — the desktop, the CLI,
|
||||
// plugin-triggered sends — so every response gets a version without each of them
|
||||
// remembering to ask for one. The version is of the stored row, not of `request`:
|
||||
// what a send holds may already be resolved or rendered (a plugin can send a
|
||||
// request it rendered itself), and neither is what a restore should put back.
|
||||
version_id: snapshot_request_for_send(query_manager, &request.id),
|
||||
})
|
||||
}
|
||||
|
||||
/// Capture the request in its own transaction. Like the snapshot itself, a failure
|
||||
/// to get the write connection is not worth failing the send over.
|
||||
fn snapshot_request_for_send(
|
||||
query_manager: &QueryManager,
|
||||
request: &HttpRequest,
|
||||
) -> Option<String> {
|
||||
/// Capture the stored request in its own transaction. Like the snapshot itself, a
|
||||
/// failure to get the write connection is not worth failing the send over.
|
||||
fn snapshot_request_for_send(query_manager: &QueryManager, request_id: &str) -> Option<String> {
|
||||
// An unsaved request has nothing to version; don't take the writer for it
|
||||
if request_id.is_empty() {
|
||||
return None;
|
||||
}
|
||||
query_manager
|
||||
.with_tx(|tx| {
|
||||
Ok::<_, yaak_models::error::Error>(
|
||||
tx.snapshot_request_for_send(&AnyRequest::HttpRequest(request.clone())),
|
||||
)
|
||||
})
|
||||
.with_tx(|tx| Ok::<_, yaak_models::error::Error>(tx.snapshot_request_for_send(request_id)))
|
||||
.unwrap_or_else(|err| {
|
||||
warn!("Failed to snapshot request before send: {err}");
|
||||
None
|
||||
@@ -1536,6 +1531,70 @@ mod tests {
|
||||
assert!(!db.request_matches_version(&version).expect("Failed to compare"));
|
||||
}
|
||||
|
||||
/// A plugin can send a request it has already rendered. The version must
|
||||
/// still be of the stored row, templates and all, or a restore would write
|
||||
/// literal values (secrets included) over what the user typed.
|
||||
#[tokio::test]
|
||||
async fn a_send_of_a_rendered_request_versions_the_stored_templates() {
|
||||
let (query_manager, blob_manager, temp_dir) = seed_send_storage();
|
||||
let header = |value: &str| yaak_models::models::HttpRequestHeader {
|
||||
name: "Authorization".to_string(),
|
||||
value: value.to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
let stored = query_manager
|
||||
.with_tx(|tx| {
|
||||
tx.upsert_http_request(
|
||||
&HttpRequest {
|
||||
workspace_id: "wk_test".to_string(),
|
||||
url: "${[ base_url ]}/test".to_string(),
|
||||
headers: vec![header("Bearer ${[ token ]}")],
|
||||
..Default::default()
|
||||
},
|
||||
&UpdateSource::Sync,
|
||||
)
|
||||
})
|
||||
.expect("Failed to seed request");
|
||||
|
||||
let rendered = HttpRequest {
|
||||
url: "http://localhost/test".to_string(),
|
||||
headers: vec![header("Bearer s3cret")],
|
||||
..stored.clone()
|
||||
};
|
||||
let response = stored_send(&query_manager, &blob_manager, temp_dir.path(), &rendered).await;
|
||||
|
||||
let version_id = response.version_id.expect("a stored request must record a version");
|
||||
let version =
|
||||
query_manager.connect().get_model_version(&version_id).expect("Failed to load version");
|
||||
assert_eq!(version.document.get("url").unwrap(), "${[ base_url ]}/test");
|
||||
let headers = version.document.get("headers").unwrap().to_string();
|
||||
assert!(headers.contains("Bearer ${[ token ]}"), "headers were {headers}");
|
||||
assert!(!headers.contains("s3cret"), "a rendered secret reached version history");
|
||||
assert!(
|
||||
query_manager.connect().request_matches_version(&version).unwrap(),
|
||||
"an untouched request must not look changed",
|
||||
);
|
||||
}
|
||||
|
||||
/// A request with an id but no stored row has nothing to version, and
|
||||
/// resolving its send must not fail for it.
|
||||
#[test]
|
||||
fn resolving_a_send_of_an_unstored_request_records_no_version() {
|
||||
let (query_manager, _blob_manager, _temp_dir) = seed_send_storage();
|
||||
let transient = HttpRequest {
|
||||
id: "rq_not_stored".to_string(),
|
||||
workspace_id: "wk_test".to_string(),
|
||||
url: "http://localhost/test".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let inputs = resolve_send_inputs(&query_manager, &transient, None, None)
|
||||
.expect("Failed to resolve send inputs");
|
||||
|
||||
assert_eq!(inputs.version_id, None);
|
||||
assert!(query_manager.connect().list_model_versions("rq_not_stored").unwrap().is_empty());
|
||||
}
|
||||
|
||||
async fn stored_send(
|
||||
query_manager: &QueryManager,
|
||||
blob_manager: &BlobManager,
|
||||
|
||||
Reference in new issue
Block a user