Skip to content
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

fix: decrypt bound args before generating a cache key #72463

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 27 additions & 125 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,78 +618,23 @@ impl<C: Comments> ServerActions<C> {
});
}

// export const $ACTION_myAction = async () => {}
let mut new_params: Vec<Param> = vec![];
let mut new_body: BlockStmtOrExpr = *arrow.body.take();

// Add the collected closure variables as the first parameters to the
// function. They are unencrypted and passed into this function by the
// cache wrapper.
if !ids_from_closure.is_empty() {
// First argument is the encrypted closure variables
new_params.push(Param {
span: DUMMY_SP,
decorators: vec![],
pat: Pat::Ident(IdentName::new("$$ACTION_CLOSURE_BOUND".into(), DUMMY_SP).into()),
});

// Also prepend the decryption decl into the body.
// var [arg1, arg2, arg3] = await decryptActionBoundArgs(actionId,
// $$ACTION_CLOSURE_BOUND)
let mut pats = vec![];
for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident(
Ident::new(
new_params.push(Param {
span: DUMMY_SP,
decorators: vec![],
pat: Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
)));
}
let decryption_decl = VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Array(ArrayPat {
span: DUMMY_SP,
elems: pats,
optional: false,
type_ann: None,
}),
init: Some(Box::new(Expr::Await(AwaitExpr {
span: DUMMY_SP,
arg: Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: quote_ident!("decryptActionBoundArgs").as_callee(),
args: vec![
reference_id.clone().as_arg(),
quote_ident!("$$ACTION_CLOSURE_BOUND").as_arg(),
],
..Default::default()
})),
}))),
definite: Default::default(),
}],
..Default::default()
};

match &mut new_body {
BlockStmtOrExpr::BlockStmt(body) => {
body.stmts.insert(0, decryption_decl.into());
}
BlockStmtOrExpr::Expr(body_expr) => {
new_body = BlockStmtOrExpr::BlockStmt(BlockStmt {
span: DUMMY_SP,
stmts: vec![
decryption_decl.into(),
Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(body_expr.take()),
}),
],
..Default::default()
});
}
});
}
}

Expand Down Expand Up @@ -717,7 +662,7 @@ impl<C: Comments> ServerActions<C> {
ident: None,
function: Box::new(Function {
params: new_params,
body: match new_body {
body: match *arrow.body.take() {
BlockStmtOrExpr::BlockStmt(body) => Some(body),
BlockStmtOrExpr::Expr(expr) => Some(BlockStmt {
span: DUMMY_SP,
Expand All @@ -737,6 +682,7 @@ impl<C: Comments> ServerActions<C> {
})),
cache_type,
&reference_id,
ids_from_closure.len(),
)),
definite: false,
}],
Expand Down Expand Up @@ -825,69 +771,24 @@ impl<C: Comments> ServerActions<C> {
private_ctxt: self.private_ctxt,
});

// export async function $ACTION_myAction () {}
let mut new_params: Vec<Param> = vec![];
let mut new_body: Option<BlockStmt> = function.body.clone();

// add params from closure collected ids
// Add the collected closure variables as the first parameters to the
// function. They are unencrypted and passed into this function by the
// cache wrapper.
if !ids_from_closure.is_empty() {
// First argument is the encrypted closure variables
new_params.push(Param {
span: DUMMY_SP,
decorators: vec![],
pat: Pat::Ident(IdentName::new("$$ACTION_CLOSURE_BOUND".into(), DUMMY_SP).into()),
});

// Also prepend the decryption decl into the body.
// var [arg1, arg2, arg3] = await decryptActionBoundArgs(actionId,
// $$ACTION_CLOSURE_BOUND)
let mut pats = vec![];
for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident(
Ident::new(
// $$ACTION_ARG_0
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
)));
}
let decryption_decl = VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: vec![VarDeclarator {
new_params.push(Param {
span: DUMMY_SP,
name: Pat::Array(ArrayPat {
span: DUMMY_SP,
elems: pats,
optional: false,
type_ann: None,
}),
init: Some(Box::new(Expr::Await(AwaitExpr {
span: DUMMY_SP,
arg: Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: quote_ident!("decryptActionBoundArgs").as_callee(),
args: vec![
reference_id.clone().as_arg(),
quote_ident!("$$ACTION_CLOSURE_BOUND").as_arg(),
],
..Default::default()
})),
}))),
definite: Default::default(),
}],
..Default::default()
};

if let Some(body) = &mut new_body {
body.stmts.insert(0, decryption_decl.into());
} else {
new_body = Some(BlockStmt {
span: DUMMY_SP,
stmts: vec![decryption_decl.into()],
..Default::default()
decorators: vec![],
pat: Pat::Ident(
Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
),
});
}
}
Expand All @@ -911,12 +812,12 @@ impl<C: Comments> ServerActions<C> {
ident: fn_name.clone(),
function: Box::new(Function {
params: new_params,
body: new_body,
..*function.take()
}),
})),
cache_type,
&reference_id,
ids_from_closure.len(),
)),
definite: false,
}],
Expand Down Expand Up @@ -2270,8 +2171,8 @@ fn retain_names_from_declared_idents(
*child_names = retained_names;
}

fn wrap_cache_expr(expr: Box<Expr>, name: &str, id: &str) -> Box<Expr> {
// expr -> $$cache__("name", "id", expr)
fn wrap_cache_expr(expr: Box<Expr>, name: &str, id: &str, bound_args_len: usize) -> Box<Expr> {
// expr -> $$cache__("name", "id", 0, expr)
Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: quote_ident!("$$cache__").as_callee(),
Expand All @@ -2284,6 +2185,7 @@ fn wrap_cache_expr(expr: Box<Expr>, name: &str, id: &str) -> Box<Expr> {
spread: None,
expr: Box::new(id.into()),
},
Number::from(bound_args_len).as_arg(),
expr.as_arg(),
],
..Default::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
import React from 'react';
import inter from '@next/font/google/target.css?{"path":"app/test.tsx","import":"Inter","arguments":[],"variableName":"inter"}';
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "c0dd5bb6fef67f5ab84327f5164ac2c3111a159337", async function Cached({ children }) {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "c0dd5bb6fef67f5ab84327f5164ac2c3111a159337", 0, async function Cached({ children }) {
return <div className={inter.className}>{children}</div>;
});
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", {
"value": "Cached",
"writable": false
});
export var Cached = registerServerReference($$RSC_SERVER_CACHE_0, "c0dd5bb6fef67f5ab84327f5164ac2c3111a159337", null);
export var Cached = registerServerReference($$RSC_SERVER_CACHE_0, "c0dd5bb6fef67f5ab84327f5164ac2c3111a159337", null);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
const v = 'world';
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", async function fn() {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 0, async function fn() {
return 'hello, ' + v;
});
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"8012a8d21b6362b4cc8f5b15560525095bc48dba80":"$$RSC_SERVER_CACHE_3","803128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0","8069348c79fce073bae2f70f139565a2fda1c74c74":"$$RSC_SERVER_CACHE_2","80951c375b4a6a6e89d67b743ec5808127cfde405d":"$$RSC_SERVER_CACHE_1"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", async function() {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 0, async function() {
return 'foo';
});
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", {
Expand All @@ -10,7 +10,7 @@ Object.defineProperty($$RSC_SERVER_CACHE_0, "name", {
});
const foo = registerServerReference($$RSC_SERVER_CACHE_0, "803128060c414d59f8552e4788b846c0d2b7f74743", null);
export { bar };
export var $$RSC_SERVER_CACHE_1 = $$cache__("default", "80951c375b4a6a6e89d67b743ec5808127cfde405d", async function bar() {
export var $$RSC_SERVER_CACHE_1 = $$cache__("default", "80951c375b4a6a6e89d67b743ec5808127cfde405d", 0, async function bar() {
return 'bar';
});
Object.defineProperty($$RSC_SERVER_CACHE_1, "name", {
Expand All @@ -22,15 +22,15 @@ var bar = registerServerReference($$RSC_SERVER_CACHE_1, "80951c375b4a6a6e89d67b7
const qux = async function qux() {
return 'qux';
};
export var $$RSC_SERVER_CACHE_2 = $$cache__("default", "8069348c79fce073bae2f70f139565a2fda1c74c74", async function baz() {
export var $$RSC_SERVER_CACHE_2 = $$cache__("default", "8069348c79fce073bae2f70f139565a2fda1c74c74", 0, async function baz() {
return qux() + 'baz';
});
Object.defineProperty($$RSC_SERVER_CACHE_2, "name", {
"value": "baz",
"writable": false
});
const baz = registerServerReference($$RSC_SERVER_CACHE_2, "8069348c79fce073bae2f70f139565a2fda1c74c74", null);
export var $$RSC_SERVER_CACHE_3 = $$cache__("default", "8012a8d21b6362b4cc8f5b15560525095bc48dba80", async function() {
export var $$RSC_SERVER_CACHE_3 = $$cache__("default", "8012a8d21b6362b4cc8f5b15560525095bc48dba80", 0, async function() {
return 'quux';
});
Object.defineProperty($$RSC_SERVER_CACHE_3, "name", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"803128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", async function() {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 0, async function() {
return 'data';
});
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/* __next_internal_action_entry_do_not_use__ {"8012a8d21b6362b4cc8f5b15560525095bc48dba80":"$$RSC_SERVER_CACHE_3","803128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0","80951c375b4a6a6e89d67b743ec5808127cfde405d":"$$RSC_SERVER_CACHE_1","c069348c79fce073bae2f70f139565a2fda1c74c74":"$$RSC_SERVER_CACHE_2"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", async function foo() {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 0, async function foo() {
return 'data A';
});
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", {
"value": "foo",
"writable": false
});
export var foo = registerServerReference($$RSC_SERVER_CACHE_0, "803128060c414d59f8552e4788b846c0d2b7f74743", null);
export var $$RSC_SERVER_CACHE_1 = $$cache__("default", "80951c375b4a6a6e89d67b743ec5808127cfde405d", async function bar() {
export var $$RSC_SERVER_CACHE_1 = $$cache__("default", "80951c375b4a6a6e89d67b743ec5808127cfde405d", 0, async function bar() {
return 'data B';
});
Object.defineProperty($$RSC_SERVER_CACHE_1, "name", {
"value": "bar",
"writable": false
});
export var bar = registerServerReference($$RSC_SERVER_CACHE_1, "80951c375b4a6a6e89d67b743ec5808127cfde405d", null);
export var $$RSC_SERVER_CACHE_2 = $$cache__("default", "c069348c79fce073bae2f70f139565a2fda1c74c74", async function Cached({ children }) {
export var $$RSC_SERVER_CACHE_2 = $$cache__("default", "c069348c79fce073bae2f70f139565a2fda1c74c74", 0, async function Cached({ children }) {
return children;
});
Object.defineProperty($$RSC_SERVER_CACHE_2, "name", {
"value": "Cached",
"writable": false
});
export default registerServerReference($$RSC_SERVER_CACHE_2, "c069348c79fce073bae2f70f139565a2fda1c74c74", null);
export var $$RSC_SERVER_CACHE_3 = $$cache__("default", "8012a8d21b6362b4cc8f5b15560525095bc48dba80", async function baz() {
export var $$RSC_SERVER_CACHE_3 = $$cache__("default", "8012a8d21b6362b4cc8f5b15560525095bc48dba80", 0, async function baz() {
return 'data C';
});
Object.defineProperty($$RSC_SERVER_CACHE_3, "name", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"803128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", async function fn() {
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 0, async function fn() {
return 'foo';
});
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"803128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("x", "803128060c414d59f8552e4788b846c0d2b7f74743", async function foo() {
export var $$RSC_SERVER_CACHE_0 = $$cache__("x", "803128060c414d59f8552e4788b846c0d2b7f74743", 0, async function foo() {
return 'data';
});
Object.defineProperty($$RSC_SERVER_CACHE_0, "name", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"803128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", async function fn($$ACTION_CLOSURE_BOUND) {
var [$$ACTION_ARG_0, $$ACTION_ARG_1] = await decryptActionBoundArgs("803128060c414d59f8552e4788b846c0d2b7f74743", $$ACTION_CLOSURE_BOUND);
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "803128060c414d59f8552e4788b846c0d2b7f74743", 2, async function fn($$ACTION_ARG_0, $$ACTION_ARG_1) {
console.log($$ACTION_ARG_0);
return {
foo: $$ACTION_ARG_1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* __next_internal_action_entry_do_not_use__ {"401c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","c03128060c414d59f8552e4788b846c0d2b7f74743":"$$RSC_SERVER_CACHE_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "c03128060c414d59f8552e4788b846c0d2b7f74743", async function cache($$ACTION_CLOSURE_BOUND, e) {
var [$$ACTION_ARG_0, $$ACTION_ARG_1] = await decryptActionBoundArgs("c03128060c414d59f8552e4788b846c0d2b7f74743", $$ACTION_CLOSURE_BOUND);
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "c03128060c414d59f8552e4788b846c0d2b7f74743", 2, async function cache($$ACTION_ARG_0, $$ACTION_ARG_1, e) {
const f = $$ACTION_ARG_0 + e;
return [
f,
Expand Down
Loading
Loading