Unverified Commit 250160f4 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Ensure all server entry exports are functions (#47364)Co-authored-by:…

Ensure all server entry exports are functions (#47364)Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>

When using a "use server" module, only exporting async functions is
permitted. However, since we cannot perform static analysis on all
exported value types (e.g., `export const foo = condition ? A : B`), we
have added a runtime ensure function to validate that these are indeed
valid functions.

By implementing this, we can prevent any strange errors like "Can't
access $$typeof of undefined or null" that may arise when exporting
`undefined` or `null` from a server entry. Moreover, this measure helps
avoid any potential mistakes that may occur.

fix NEXT-865 ([link](https://linear.app/vercel/issue/NEXT-865))
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 4942394b
......@@ -918,8 +918,56 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}
}
}
new.extend(self.annotations.drain(..).map(ModuleItem::Stmt));
new.append(&mut self.extra_items);
// Ensure that the exports are valid by appending a check
// import { ensureServerEntryExports } from 'private-next-rsc-action-proxy'
// ensureServerEntryExports([action1, action2, ...])
let ensure_ident = private_ident!("ensureServerEntryExports");
new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
specifiers: vec![ImportSpecifier::Default(ImportDefaultSpecifier {
span: DUMMY_SP,
local: ensure_ident.clone(),
})],
src: Box::new(Str {
span: DUMMY_SP,
value: "private-next-rsc-action-proxy".into(),
raw: None,
}),
type_only: false,
asserts: None,
})));
new.push(ModuleItem::Stmt(Stmt::Expr(ExprStmt {
span: DUMMY_SP,
expr: Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: Callee::Expr(Box::new(Expr::Ident(ensure_ident))),
args: vec![ExprOrSpread {
spread: None,
expr: Box::new(Expr::Array(ArrayLit {
span: DUMMY_SP,
elems: self
.exported_idents
.iter()
.map(|e| {
Some(ExprOrSpread {
spread: None,
expr: Box::new(Expr::Ident(Ident::new(
e.0 .0.clone(),
DUMMY_SP.with_ctxt(e.0 .1),
))),
})
})
.collect(),
})),
}],
type_args: None,
})),
})));
// Append annotations to the end of the file.
new.extend(self.annotations.drain(..).map(ModuleItem::Stmt));
}
*stmts = new;
......
/* __next_internal_action_entry_do_not_use__ foo */ export function foo() {}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "ab21efdafbe611287bc25c0462b1e0510d13e48b";
foo.$$bound = [];
foo.$$with_bound = false;
\ No newline at end of file
foo.$$with_bound = false;
/* __next_internal_action_entry_do_not_use__ bar */ 'use strict';
export function bar() {}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
bar
]);
bar.$$typeof = Symbol.for("react.server.reference");
bar.$$id = "ac840dcaf5e8197cb02b7f3a43c119b7a770b272";
bar.$$bound = [];
bar.$$with_bound = false;
\ No newline at end of file
bar.$$with_bound = false;
/* __next_internal_action_entry_do_not_use__ x */ export const x = 1;
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
x
]);
x.$$typeof = Symbol.for("react.server.reference");
x.$$id = "b78c261f135a7a852508c2920bd7228020ff4bd7";
x.$$bound = [];
x.$$with_bound = false;
\ No newline at end of file
x.$$with_bound = false;
......@@ -2,4 +2,6 @@
render() {
return null;
}
}
\ No newline at end of file
}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([]);
/* __next_internal_action_entry_do_not_use__ */ export * from 'foo';
\ No newline at end of file
/* __next_internal_action_entry_do_not_use__ */ export * from 'foo';
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([]);
/* __next_internal_action_entry_do_not_use__ */ export default (()=>{});
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([]);
......@@ -5,6 +5,11 @@
export default async function $$ACTION_0(...args) {
return __build_action__($$ACTION_0, args);
};
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
myAction,
$$ACTION_0
]);
myAction.$$typeof = Symbol.for("react.server.reference");
myAction.$$id = "e10665baac148856374b2789aceb970f66fec33e";
myAction.$$bound = [];
......
......@@ -2,6 +2,10 @@
/* __next_internal_action_entry_do_not_use__ foo */ export async function foo(...args) {
return __build_action__(foo, args);
}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "ab21efdafbe611287bc25c0462b1e0510d13e48b";
foo.$$bound = [];
......
/* __next_internal_action_entry_do_not_use__ default */ export default async function foo() {}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d";
foo.$$bound = [];
foo.$$with_bound = false;
\ No newline at end of file
foo.$$with_bound = false;
/* __next_internal_action_entry_do_not_use__ default */ export default async function $$ACTION_0() {}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
$$ACTION_0
]);
$$ACTION_0.$$typeof = Symbol.for("react.server.reference");
$$ACTION_0.$$id = "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d";
$$ACTION_0.$$bound = [];
$$ACTION_0.$$with_bound = false;
\ No newline at end of file
$$ACTION_0.$$with_bound = false;
/* __next_internal_action_entry_do_not_use__ default */ async function foo() {}
export default foo;
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d";
foo.$$bound = [];
foo.$$with_bound = false;
\ No newline at end of file
foo.$$with_bound = false;
......@@ -2,6 +2,11 @@
export default foo;
const bar = async function() {};
export { bar };
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo,
bar
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d";
foo.$$bound = [];
......
/* __next_internal_action_entry_do_not_use__ foo */ export async function foo() {
async function bar() {}
}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "ab21efdafbe611287bc25c0462b1e0510d13e48b";
foo.$$bound = [];
......
......@@ -2,6 +2,10 @@
console.log(a, b);
};
var $$ACTION_0;
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
$$ACTION_0
]);
$$ACTION_0.$$typeof = Symbol.for("react.server.reference");
$$ACTION_0.$$id = "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d";
$$ACTION_0.$$bound = [];
......
/* __next_internal_action_entry_do_not_use__ foo,bar */ export const foo = async ()=>{};
const bar = async ()=>{};
export { bar };
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo,
bar
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "ab21efdafbe611287bc25c0462b1e0510d13e48b";
foo.$$bound = [];
......@@ -8,4 +13,4 @@ foo.$$with_bound = false;
bar.$$typeof = Symbol.for("react.server.reference");
bar.$$id = "ac840dcaf5e8197cb02b7f3a43c119b7a770b272";
bar.$$bound = [];
bar.$$with_bound = false;
\ No newline at end of file
bar.$$with_bound = false;
/* __next_internal_action_entry_do_not_use__ default */ const [foo] = [
null
];
export default foo;
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d";
foo.$$bound = [];
foo.$$with_bound = false;
......@@ -2,7 +2,11 @@
/* __next_internal_action_entry_do_not_use__ myAction */ export async function myAction(a, b, c) {
console.log('a');
}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
myAction
]);
myAction.$$typeof = Symbol.for("react.server.reference");
myAction.$$id = "e10665baac148856374b2789aceb970f66fec33e";
myAction.$$bound = [];
myAction.$$with_bound = false;
\ No newline at end of file
myAction.$$with_bound = false;
......@@ -5,6 +5,12 @@ function d() {}
function Foo() {
async function e() {}
}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
a,
b,
c
]);
a.$$typeof = Symbol.for("react.server.reference");
a.$$id = "6e7bc104e4d6e7fda190c4a51be969cfd0be6d6d";
a.$$bound = [];
......
......@@ -5,6 +5,12 @@ async function bar() {}
export { bar as baz };
async function qux() {}
export { qux as default };
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo,
bar,
qux
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "ab21efdafbe611287bc25c0462b1e0510d13e48b";
foo.$$bound = [];
......
......@@ -12,6 +12,7 @@ import {
APP_DIR_ALIAS,
WEBPACK_LAYERS,
RSC_MOD_REF_PROXY_ALIAS,
RSC_ACTION_PROXY_ALIAS,
} from '../lib/constants'
import { fileExists } from '../lib/file-exists'
import { CustomRoutes } from '../lib/load-custom-routes.js'
......@@ -1053,6 +1054,9 @@ export default async function getBaseWebpackConfig(
[RSC_MOD_REF_PROXY_ALIAS]:
'next/dist/build/webpack/loaders/next-flight-loader/module-proxy',
[RSC_ACTION_PROXY_ALIAS]:
'next/dist/build/webpack/loaders/next-flight-loader/action-proxy',
...(isClient || isEdgeServer
? {
[clientResolveRewrites]: hasRewrites
......@@ -1237,7 +1241,7 @@ export default async function getBaseWebpackConfig(
}
const notExternalModules =
/^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|document|link|image|legacy\/image|constants|dynamic|script|navigation|headers)$)|string-hash|private-next-rsc-mod-ref-proxy$)/
/^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|document|link|image|legacy\/image|constants|dynamic|script|navigation|headers)$)|string-hash|private-next-rsc-mod-ref-proxy|private-next-rsc-action-proxy$)/
if (notExternalModules.test(request)) {
return
}
......
export default function ensureServerEntryExports(actions: any[]) {
for (let i = 0; i < actions.length; i++) {
const action = actions[i]
if (typeof action !== 'function') {
throw new Error(
`A "use server" file can only export async functions, found ${typeof action}.`
)
}
}
}
......@@ -18,6 +18,7 @@ export const DOT_NEXT_ALIAS = 'private-dot-next'
export const ROOT_DIR_ALIAS = 'private-next-root-dir'
export const APP_DIR_ALIAS = 'private-next-app-dir'
export const RSC_MOD_REF_PROXY_ALIAS = 'private-next-rsc-mod-ref-proxy'
export const RSC_ACTION_PROXY_ALIAS = 'private-next-rsc-action-proxy'
export const PUBLIC_DIR_MIDDLEWARE_CONFLICT = `You can not have a '_next' folder inside of your public folder. This conflicts with the internal '/_next' route. https://nextjs.org/docs/messages/public-next-folder-conflict`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment