NextJS 'use server' Trap (Accidentally Leaking Data)

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ก.ค. 2024
  • Ever since React moved onto the server, the new directives "use client" and "use server" have caused some confusion. Lets look at how the top level directive in an actions.ts file may leak some data you may not want, and why this is more an issue of developer confusion rather than an issue with NextJS.
    Rhys Tweet: x.com/RhysSullivan/status/180...
    -------
    🐦 Twitter (X): jollycod.ing/x
    🤓 Personal Site: jollycod.ing/me
    💻 GitHub: jollycod.ing/git
    JollyUI: jollycod.ing/ui

ความคิดเห็น • 65

  • @deepjyotideb1173
    @deepjyotideb1173 18 วันที่ผ่านมา +19

    Security threat starts from 9:30

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา +3

      Sorry, considering the mistake is geared more to beginners I think an explainer of the concepts is needed first

  • @user-qj4rr1rm8i
    @user-qj4rr1rm8i 17 วันที่ผ่านมา +2

    Auth for every server action functions. I've got it.
    Thank you very much. Maybe you just saved my life.

  • @nasko235679
    @nasko235679 17 วันที่ผ่านมา +2

    That's basic javascript. You're exporting 2 separate functions. Even if they weren't separate endpoints the authentication check of one function is clearly outside the scope of the second. Another mistake is fetching data from server actions. Next have explicitly stated in their docs that server actions are only to be used for mutations. They get queued and can't run in parallel so if you start fetching with them and you have 2 different server actions for fetching data inside 1 component one has to wait for the other to complete. Instead fetching in server components can be done with simple helper functions that then get imported at the top level of the server component and fetching in client components (if you want it to be secure) still has to be done through api endpoints so the cookie can be send to the route handler and checked there.

  • @michaelfrieze
    @michaelfrieze 18 วันที่ผ่านมา +23

    I wish people would read the docs. Server Actions are not meant for fetching data. Regardless, you should always check for auth in any exported server action function and you should always check auth before fetching data.

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา +6

      In an ideal world we would all read the docs!
      It is an interesting one, as I said it’s a trap for beginners and experts will be aware of this. In terms of reading the docs I think it’s up to educators to make sure this point is clear now.
      At the end of the day beginners will probably find a way to leak data somehow and learn from that, I just hope they have experts up the chain to teach them.

    • @michaelfrieze
      @michaelfrieze 18 วันที่ผ่านมา +2

      @@JollyCoding I saw a very popular next course on Udemy teaching to use server actions for data fetching. The instructor had no idea they were meant to be used for mutations.

    • @JohnMurphy04f
      @JohnMurphy04f 18 วันที่ผ่านมา

      ​@@michaelfriezewhos the instructor

    • @pjosxyz
      @pjosxyz 18 วันที่ผ่านมา +1

      @@michaelfrieze saw a udemy course teaching people to use floats for money too, oh lord

    • @MichaelKire
      @MichaelKire 18 วันที่ผ่านมา +1

      Server actions can be used for data fetching no problem. There is even several doc examples showing use with react/tanstack query etc. You should just not think that theres any connection between two different function that only has being in the same file in common, it’s literally just a way of defining endpoints

  • @capability-snob
    @capability-snob 18 วันที่ผ่านมา

    Thanks for covering this. Making clear what the API is - and therefore where the security audit should happen - is really important. I worry about tools that hide this for the sake of convenience. I want this boundary to scream loudly at me so I know what calls can fail due to network partitions or leak data through their parameters or return values. This makes it look like I could maybe understand what the attack surface is with server components, with some effort.

  • @williandamascenomusic
    @williandamascenomusic 17 วันที่ผ่านมา +3

    Is there some kind of middleware for server actions? Middlewares can potentially solve this problem.

  • @codePerfect
    @codePerfect 18 วันที่ผ่านมา +3

    Informative

  • @HugoDuprez449
    @HugoDuprez449 16 วันที่ผ่านมา

    SvelteKit handles that separation of concern so much better

  • @rawallon
    @rawallon 18 วันที่ผ่านมา +5

    Man, when react added hooks I seen a few arguments complaining they added footguns, but to me every "proof" felt like wasnt an actual use-case. Sadly this time I'm on the oposite camp, server actions feels so cool, but feels like there are oopsies that could happen and no one notice that makes me avoid it completely.
    Maybe it has something to do with experience, I know that a year down the line most we'll all just gotten used to its quirks, but it makes me scared because I know there are projects being started now, without knowledge of any of those quirks, and that will fall onto someone to sift trhough all of it...

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา +2

      There’s a good tweet from Ryan Florence that says “I’ve seen people leak server stuff my entire career”. Just seems another area that we need to educate people on and be very careful with. Think the issue here is how fast this server stuff has moved and how much of a change it is for frontend devs.
      x.com/ryanflorence/status/1808538800005730612?s=46&t=5NfomTO38hUdG85aPtK2SA

  • @user-ik7rp8qz5g
    @user-ik7rp8qz5g 16 วันที่ผ่านมา +1

    Or just store helper functions in functions.js file. It should work too, right?

  • @tolulopemalomo8922
    @tolulopemalomo8922 7 วันที่ผ่านมา

    thanks for the great info!

  • @alaskandonut
    @alaskandonut 18 วันที่ผ่านมา

    Great! Thank you.

  • @isekaijoucho4812
    @isekaijoucho4812 17 วันที่ผ่านมา

    how could server side rendering leaks data? it is for rendering, nothing more

  • @oumardicko5593
    @oumardicko5593 18 วันที่ผ่านมา +10

    the issue is: how the heck react became this complicated ?

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา +3

      A growing demand for performance and a move to a full stack framework. Really good talk by Ryan Florence here on the journey of it all and what reacts new role is. th-cam.com/video/zqhE-CepH2g/w-d-xo.htmlsi=ZreHb7iXEBAIDFeA

    • @oumardicko5593
      @oumardicko5593 18 วันที่ผ่านมา +1

      @@JollyCoding oh thanks, i'll be sure to have a look

  • @ZiaCodes
    @ZiaCodes 18 วันที่ผ่านมา

    I don't use it here, I've a util function, I don't add everything under 'use server'

  • @mrlectus
    @mrlectus 17 วันที่ผ่านมา

    can't middleware solve this?

  • @matthewnicastro2489
    @matthewnicastro2489 18 วันที่ผ่านมา +2

    Just put auth in middleware like a chad

    • @sraj5923
      @sraj5923 17 วันที่ผ่านมา

      exactly !!!

  • @creatorsremose
    @creatorsremose 17 วันที่ผ่านมา

    Drinking game suggestion - whenever he says "go ahead", you take a shot. Bye bye liver.

  • @gofullstack
    @gofullstack 16 วันที่ผ่านมา

    Reminds me of PHP

  • @zman-1x1
    @zman-1x1 18 วันที่ผ่านมา

    This is so easy to mess up!

  • @greendsnow
    @greendsnow 18 วันที่ผ่านมา

    There's a fix: use the npm package called astro

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา

      Good choice, each has their own strengths!

  • @salman0ansari
    @salman0ansari 17 วันที่ผ่านมา

    i don;t see how server actions are a trap.
    if you are not check auth when fetching data its your mistake clearly

  • @BarakaAndrew
    @BarakaAndrew 18 วันที่ผ่านมา

    Everything that I said would happen is happening 😂😂
    Things JS devs would do not to split server and client code. I'm a golang backend, JS frontend guy, never mixing code, easy to secure, easy to code, easy to manage.

  • @henriquematias1986
    @henriquematias1986 18 วันที่ผ่านมา

    If you going to use server actions you better instead use tRPC

  • @abdelmounaimammari9064
    @abdelmounaimammari9064 18 วันที่ผ่านมา

    I’ve found the use zsa very helpful on that matter. The pattern it introduced makes it almost impossible to make these kind of mistakes. Take a look at it as it’s game changing when working with server actions in NextJs

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา +1

      Will have to check that out thanks!

  • @masaeedi1
    @masaeedi1 18 วันที่ผ่านมา

    That is why i use authentication in middleware. Problem solved.

  • @denisblack9897
    @denisblack9897 17 วันที่ผ่านมา

    I dont get it, why use this server stuff anyway?
    You can pick up FastAPI in less than an hour and make a real backend that supports tons of new ai libraries, which your are definately making a wrapper around 😅

  • @adnanearef2959
    @adnanearef2959 17 วันที่ผ่านมา

    This is not called a TRAP, please read the docs and dont ever use something without knowing exactly what it does.
    Server actions are one hell of a cool feature and I love them. The thing here is that you are explictly exporting two functions within a single "use server" directive.
    Which makes NextJS thinks that you are aware of what you are doing and proceeds to give you exactly what you are looking for. If you don't want to fall for that, then just export one single function and mark it as a default export instead. What I mean, is instead of exporting multiple action in the module, you can export only one default per module/file and that automatically solve your (issuem eventhough it is not an issue).
    One more thing you can do, is add your authentication in the middleware and you are GUCCI, no more similiar data leaking.
    Conclusion: Please stop blaming the framework for your mistakes, read the docs carefully and only when something behaves differently that it's described you can blame them.

    • @JollyCoding
      @JollyCoding  17 วันที่ผ่านมา

      To be clear I wasn’t blaming the framework, I was explaining a technique that it seems people have done that isn’t correct. This video educates them on that. I hope I was clear in the video this isn’t NextJS fault.
      The only thing they could change is tree shake the unused functions which will be a quality of life improvement

  • @kal.leroux
    @kal.leroux 18 วันที่ผ่านมา

    that's just people using the tool tge wrong way

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา

      Yes correct, but it is easy to see how people can fall into this. It’s that typical battle of being beginner friendly and stopping dangerous errors where we can, but at the same time not holding your hand too much that it limits advanced developers doing cool things and customisability

  • @gopallohar5534
    @gopallohar5534 18 วันที่ผ่านมา +2

    This should've been just a 1-2 minute video!!!!

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา +1

      Sorry, considering the mistake is geared more to beginners I think an explainer of the concepts is needed first

    • @gopallohar5534
      @gopallohar5534 18 วันที่ผ่านมา +1

      @@JollyCoding yeah not 1-2 minute but the beginning of the video is unnecessary, you could have directly made the application which *has* the problem!!! The statement for the problem is really simple...

    • @gopallohar5534
      @gopallohar5534 18 วันที่ผ่านมา

      @@JollyCoding btw can we use server actions to replace the /APIs ?
      I've seen this suggestion on many yt videos and tried and it works fine...
      I mean the page isn't server rendered, we call server actions on click or some other events (not only on form submition)

  • @dimitrisefstathiadis6562
    @dimitrisefstathiadis6562 17 วันที่ผ่านมา

    And how is this different from looking the damn Network Tab? Come on. Have y people build real apps? I don’t get the security issues with Server Actions. People put their Google API Keys in their code plain. Y can see them.. what’s the fuss about..?

  • @dimitrisefstathiadis6562
    @dimitrisefstathiadis6562 17 วันที่ผ่านมา

    Well, the problem is solved from just using the inline “use server”, and then inline the function. Problem solved! Drama again!😂

  • @krtirtho
    @krtirtho 18 วันที่ผ่านมา

    Regardless human stupidity this is actually a really stupid API design. Instead of supporting multi exports as different endpoints, Nextjs server actions should only accept a specifically named function. Like export function action() {} and ignore anything else in the file. It's the same concept as Vue's one component per file.
    This is needlessly complicated and less readable

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา

      NestJS does this using decorators. Does remind me a lot of .Net controllers though!

  • @ticler
    @ticler 18 วันที่ผ่านมา +3

    React nerds leaking data? What's new about programmers without basic understanding of underlying protocols and technologies doing crappy programming?

    • @JollyCoding
      @JollyCoding  18 วันที่ผ่านมา

      It’s always happened and will continue too. All part of the learning process, you just hope that someone experienced doesn’t let it get to production haha

    • @JarheadCrayonEater
      @JarheadCrayonEater 18 วันที่ผ่านมา

      That has absolutely nothing to do with a particular programming language, compiler, or framework.
      Absurd comment.

    • @lodosdurak7913
      @lodosdurak7913 18 วันที่ผ่านมา

      Yea if they are not planning and writing in machine language I aint hiring

    • @RadTwin
      @RadTwin 18 วันที่ผ่านมา

      Always one of those people 😅

  • @ghostinplainsight4803
    @ghostinplainsight4803 17 วันที่ผ่านมา

    This is a job for decorators. It should be closed by default and opened with an explicit decorator.
    ```
    @serverAction
    @auth(USER)
    export const getUser = async () => {}
    export const getAllUsers = async () => {}
    ```