Server Actions and Security #68155
Replies: 2 comments 2 replies
-
I can't see server actions are different from this. There's a bit magic on how Next.js creates them, but under the hood, they're all just POST route handlers or something. You shouldn't think that user can't guess the action URL or pass unsanitized data the same, as in normal route handlers. I would even bet that you actually can somehow debug the list of actions (at least that are used on the current page), because Next.js needs to pass some metadata about them to a client too. I think you're overthinking it, but I kinda agree that it's easy for newcomers to write unsafe code with them..
All exports, meaning all of them should be |
Beta Was this translation helpful? Give feedback.
-
Hey there, have you given this post a read? 🙏 https://nextjs.org/blog/security-nextjs-server-components-actions |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi NextJS Team, React Core team.
Sorry guys, I'm pinging @leerob @styfle @timneutkens, because I really think this is a really important subject for the community.
I'm posting this today because I'm really concerned about server actions and what can occurs.
It's been 10 times that I'm reading the doc and I'm not sure yet about how secure it is. I think we might be creating a demon that can be out of control
My goal: Trying to improve the documentation, especially for beginners and not the best engineers working with react and NextJS. Globally improve security overall for all apps on NextJS
How accessible is code from the function?
While there is a dedicated paragraph here it doesn't talk about a NO-GO scenario, when you can accidentally leak data.
I know things get encrypted, but this magic serialization and things scares me a bit.
Here are some questions I ask, maybe you can make me confident with it, and maybe we could improve the doc in the same manner?
1️⃣ When I define functions at top-level in a
use server
file:Important
See alfonsusac/nextjs-better-unstable-cache#9 and alfonsusac/nextjs-better-unstable-cache#10 which makes me not really confident about why callback are defined as
async
here, does it means they are exported and accessible outside? This can be really really security risky.poke @TheEdoRan Maybe you're concerned with next-safe-action as the action builder is passing a callback. Does it mean you can access the function without handling middleware or schema?
Note
Risks: Major breaches, hacking, data-leaking sneaky, and ability to interact with private methods.
Cons: Bad developer experience, thinking a function is not async where it is
They are queued, they are designed for mutation only
From here
I see plenty of tutorials and content about data fetching using server actions. Especially with things like Infinite paging.
The fact: It works, maybe it's not optimized, but there is no mention of it in nextJS doc here. Do you think maybe we could add some topic about it. Is there any risks hidden here in doing that? Or this is just the nature of "One by One processing" that makes it not optimized for querying async data after the initial page has loaded?
It's a very powerful tool, but with power comes great responsibility.
useTransition
is highly recommended.Note
Risks: Bad usage, low performance, data leak, hack breach in the backend, misconception.
No warning in runtime that it shouldn't export anything else than function.
If you export a constant, or anything. It simply doesn't work and most of the time fails silently. Try exporting an enumeration from a file with
use server
. You get an empty object. But no error.If you export not async function you get a warning. Why no more?
Note
Risk: Has no one yield, you might be missing runtime error here, very hard to debug.
That's a lot of content, but I'm really scared because even with almost 10 years of experience in React, I'm not confident with server action magic. I can only imagine newcomers, making breaches in their production apps and hackers having joy because we missed some key points showing server action as the go very easy tool.
tRPC
was already a great step, but it was not magic, that was only a wrapper for the network. Here it's a bit more risky.Thanks for reading me,
Hope we find the answers, improve this and mark the risks or counter them upfront :)
Andréas
Beta Was this translation helpful? Give feedback.
All reactions