-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate args for system UDFs (#23235)
Validate arguments for system UDFs GitOrigin-RevId: 1c482648ad68cbf544f1eb256f059c35a8283bcb
- Loading branch information
1 parent
d0a9549
commit 0f1854b
Showing
16 changed files
with
219 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -16,5 +16,6 @@ mod storage; | |
mod stream; | ||
mod text; | ||
mod time; | ||
mod validate_args; | ||
|
||
pub use self::crypto::CryptoOps; |
This file contains 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,72 @@ | ||
use std::convert::{ | ||
TryFrom, | ||
TryInto, | ||
}; | ||
|
||
use anyhow::Context; | ||
use common::{ | ||
self, | ||
runtime::Runtime, | ||
}; | ||
use model::{ | ||
self, | ||
modules::args_validator::ArgsValidator, | ||
}; | ||
use serde_json::{ | ||
json, | ||
Value as JsonValue, | ||
}; | ||
use value::ConvexArray; | ||
|
||
use crate::{ | ||
environment::IsolateEnvironment, | ||
execution_scope::ExecutionScope, | ||
helpers::UdfArgsJson, | ||
}; | ||
|
||
impl<'a, 'b: 'a, RT: Runtime, E: IsolateEnvironment<RT>> ExecutionScope<'a, 'b, RT, E> { | ||
#[convex_macro::v8_op] | ||
|
||
pub fn op_validate_args( | ||
&mut self, | ||
validator: JsonValue, | ||
args: UdfArgsJson, | ||
) -> anyhow::Result<JsonValue> { | ||
let JsonValue::String(validator_string) = validator.clone() else { | ||
return Err(anyhow::anyhow!("export_args result not a string")); | ||
}; | ||
|
||
let args_validator: ArgsValidator = | ||
match serde_json::from_str::<JsonValue>(&validator_string) { | ||
Ok(args_json) => match ArgsValidator::try_from(args_json) { | ||
Ok(validator) => validator, | ||
Err(err) => return Err(err), | ||
}, | ||
Err(json_error) => { | ||
let message = | ||
format!("Unable to parse JSON returned from `exportArgs`: {json_error}"); | ||
return Err(anyhow::anyhow!(message)); | ||
}, | ||
}; | ||
|
||
let args_array = args | ||
.into_arg_vec() | ||
.into_iter() | ||
.map(|arg| arg.try_into()) | ||
.collect::<anyhow::Result<Vec<_>>>() | ||
.and_then(ConvexArray::try_from) | ||
.map_err(|err| anyhow::anyhow!(format!("{}", err)))?; | ||
|
||
let state = self.state_mut()?; | ||
let (table_mapping, virtual_table_mapping) = state.environment.get_all_table_mappings()?; | ||
match args_validator.check_args(&args_array, &table_mapping, &virtual_table_mapping)? { | ||
Some(js_error) => Ok(json!({ | ||
"valid": false, | ||
"message": format!("{}", js_error) | ||
})), | ||
None => Ok(json!({ | ||
"valid": true, | ||
})), | ||
} | ||
} | ||
} |
This file contains 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 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,51 @@ | ||
use std::collections::HashSet; | ||
|
||
use common::{ | ||
assert_obj, | ||
types::EnvironmentVariable, | ||
value::ConvexValue, | ||
}; | ||
use keybroker::Identity; | ||
use model::environment_variables::EnvironmentVariablesModel; | ||
use must_let::must_let; | ||
use runtime::testing::TestRuntime; | ||
use value::ConvexObject; | ||
|
||
use crate::test_helpers::UdfTest; | ||
|
||
#[convex_macro::test_runtime] | ||
async fn test_system_udf(rt: TestRuntime) -> anyhow::Result<()> { | ||
let t = UdfTest::default(rt).await?; | ||
let mut tx = t.database.begin(Identity::system()).await?; | ||
let environment_variable = EnvironmentVariable::new("A".parse()?, "B".parse()?); | ||
EnvironmentVariablesModel::new(&mut tx) | ||
.create(environment_variable, &HashSet::new()) | ||
.await?; | ||
t.database.commit(tx).await?; | ||
|
||
// nonexistent returns Null | ||
must_let!(let ConvexValue::Null = t.query( | ||
"_system/cli/queryEnvironmentVariables:get", | ||
assert_obj!("name" => "nonexistent".to_string()), | ||
) | ||
.await?); | ||
|
||
// return environment variable | ||
must_let!(let ConvexValue::Object(obj) = t.query( | ||
"_system/cli/queryEnvironmentVariables:get", | ||
assert_obj!("name" => "A".to_string()), | ||
) | ||
.await?); | ||
must_let!(let Some(ConvexValue::String(name)) = ConvexObject::get(&obj, "name")); | ||
assert_eq!(name.to_string(), "A"); | ||
must_let!(let Some(ConvexValue::String(value)) = ConvexObject::get(&obj, "value")); | ||
assert_eq!(value.to_string(), "B"); | ||
|
||
// calling with empty argument fails | ||
let error = t | ||
.query_js_error("_system/cli/queryEnvironmentVariables:get", assert_obj!()) | ||
.await?; | ||
|
||
assert!(error.message.contains("missing the required field `name`")); | ||
Ok(()) | ||
} |
This file contains 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 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 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.