Skip to content

Commit 6212220

Browse files
committed
Feat/fix: Adding signature validation to the "hook" option so the error is clear if users try to use an invalid function as a hook. Also fixing the request_hook trybuild test, which was not actually testing anything
1 parent 85d2959 commit 6212220

8 files changed

Lines changed: 91 additions & 11 deletions

File tree

lambda-appsync-proc/src/appsync_lambda_main/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,19 @@ impl AppsyncLambdaMain {
307307
fn appsync_event_handler(&self, tokens: &mut TokenStream2) {
308308
let call_hook = if let Some(ref hook) = self.options.hook {
309309
quote_spanned! {hook.span()=>
310-
if let Some(resp) = #hook(&event).await {
310+
mod _check_sig {
311+
use super::Operation;
312+
use ::lambda_appsync::{AppsyncEvent, AppsyncResponse};
313+
use ::core::future::Future;
314+
#[inline(always)]
315+
pub(super) async fn call_hook<'a, Fut, H>(hook: H, event: &'a AppsyncEvent<Operation>) -> Option<AppsyncResponse>
316+
where
317+
Fut: Future<Output = Option<AppsyncResponse>>,
318+
H: Fn(&'a AppsyncEvent<Operation>) -> Fut {
319+
hook(event).await
320+
}
321+
}
322+
if let Some(resp) = _check_sig::call_hook(#hook, &event).await{
311323
return resp;
312324
}
313325
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod no_run {
2+
use lambda_appsync::{appsync_lambda_main, AppsyncResponse};
3+
async fn verify_request(_event: &Operation) -> Option<AppsyncResponse> {
4+
None // Allow all requests
5+
}
6+
appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
7+
}
8+
9+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0631]: type mismatch in function arguments
2+
--> tests/fail/invalid_hook_args.rs:6:63
3+
|
4+
3 | async fn verify_request(_event: &Operation) -> Option<AppsyncResponse> {
5+
| ---------------------------------------------------------------------- found signature defined here
6+
...
7+
6 | appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
8+
| ^^^^^^^^^^^^^^ expected due to this
9+
|
10+
= note: expected function signature `fn(&AppsyncEvent<no_run::Operation>) -> _`
11+
found function signature `fn(&no_run::Operation) -> _`
12+
note: required by a bound in `call_hook`
13+
--> tests/fail/invalid_hook_args.rs:6:63
14+
|
15+
6 | appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
16+
| ^^^^^^^^^^^^^^ required by this bound in `call_hook`
17+
help: consider wrapping the function in a closure
18+
|
19+
6 | appsync_lambda_main!("../../../../schema.graphql", hook = |arg0: &AppsyncEvent<no_run::Operation>| verify_request(/* &no_run::Operation */));
20+
| ++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod no_run {
2+
use lambda_appsync::{appsync_lambda_main, AppsyncEvent, AppsyncResponse};
3+
fn verify_request(_event: &AppsyncEvent<Operation>) -> Option<AppsyncResponse> {
4+
None // Allow all requests
5+
}
6+
appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
7+
}
8+
9+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: `std::option::Option<AppsyncResponse>` is not a future
2+
--> tests/fail/invalid_hook_no_async.rs:6:63
3+
|
4+
6 | appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
5+
| ^^^^^^^^^^^^^^ `std::option::Option<AppsyncResponse>` is not a future
6+
|
7+
= help: the trait `Future` is not implemented for `std::option::Option<AppsyncResponse>`
8+
note: required by a bound in `call_hook`
9+
--> tests/fail/invalid_hook_no_async.rs:6:63
10+
|
11+
6 | appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
12+
| ^^^^^^^^^^^^^^ required by this bound in `call_hook`
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod no_run {
2+
use lambda_appsync::{appsync_lambda_main, AppsyncEvent};
3+
async fn verify_request(_event: &AppsyncEvent<Operation>) -> Option<String> {
4+
None // Allow all requests
5+
}
6+
appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
7+
}
8+
9+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0271]: expected `impl Future<Output = Option<String>>` to be a future that resolves to `Option<AppsyncResponse>`, but it resolves to `Option<String>`
2+
--> tests/fail/invalid_hook_ret.rs:6:63
3+
|
4+
6 | appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
5+
| ^^^^^^^^^^^^^^ expected `Option<AppsyncResponse>`, found `Option<String>`
6+
|
7+
= note: expected enum `std::option::Option<AppsyncResponse>`
8+
found enum `std::option::Option<std::string::String>`
9+
note: required by a bound in `call_hook`
10+
--> tests/fail/invalid_hook_ret.rs:6:63
11+
|
12+
6 | appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
13+
| ^^^^^^^^^^^^^^ required by this bound in `call_hook`
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
use lambda_appsync::{appsync_lambda_main, AppsyncEvent, AppsyncResponse};
2-
3-
async fn verify_request(_event: &AppsyncEvent<Operation>) -> Option<AppsyncResponse> {
4-
None // Allow all requests
1+
mod no_run {
2+
use lambda_appsync::{appsync_lambda_main, AppsyncEvent, AppsyncResponse};
3+
async fn verify_request(_event: &AppsyncEvent<Operation>) -> Option<AppsyncResponse> {
4+
None // Allow all requests
5+
}
6+
appsync_lambda_main!("../../../../schema.graphql", hook = verify_request);
57
}
68

7-
appsync_lambda_main!(
8-
"../../../../schema.graphql",
9-
exclude_lambda_handler = true,
10-
hook = verify_request
11-
);
12-
139
fn main() {}

0 commit comments

Comments
 (0)