NextJS 'use server' Trap (Accidentally Leaking Data)

Поделиться
HTML-код
  • Опубликовано: 9 янв 2025

Комментарии • 66

  • @svarupdesai
    @svarupdesai 3 месяца назад +1

    We can define "getAllUser" with export in a separate file that does not have "use server" and then reuse it in the server action, and to make sure we called "getAllUser" only from the server side we can use import "server-only"; in that file

  • @deepjyotideb1173
    @deepjyotideb1173 6 месяцев назад +23

    Security threat starts from 9:30

    • @JollyCoding
      @JollyCoding  6 месяцев назад +4

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

  • @풍월상신
    @풍월상신 6 месяцев назад +2

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

  • @michaelfrieze
    @michaelfrieze 6 месяцев назад +26

    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  6 месяцев назад +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 6 месяцев назад +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 6 месяцев назад

      ​@@michaelfriezewhos the instructor

    • @pjosxyz
      @pjosxyz 6 месяцев назад +1

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

    • @MichaelKire
      @MichaelKire 6 месяцев назад +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

  • @nasko235679
    @nasko235679 6 месяцев назад +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.

  • @williandamascenomusic
    @williandamascenomusic 6 месяцев назад +4

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

  • @virtual5754
    @virtual5754 6 месяцев назад +2

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

  • @rawallon
    @rawallon 6 месяцев назад +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  6 месяцев назад +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

  • @capability-snob
    @capability-snob 6 месяцев назад

    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.

  • @HugoDuprez449
    @HugoDuprez449 6 месяцев назад

    SvelteKit handles that separation of concern so much better

  • @tolulopemalomo8922
    @tolulopemalomo8922 5 месяцев назад

    thanks for the great info!

  • @dennisgonzales9521
    @dennisgonzales9521 5 месяцев назад

    thanks for the video!

  • @ziacodes
    @ziacodes 6 месяцев назад +1

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

  • @codePerfect
    @codePerfect 6 месяцев назад +3

    Informative

  • @oumardicko5593
    @oumardicko5593 6 месяцев назад +10

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

    • @JollyCoding
      @JollyCoding  6 месяцев назад +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. ruclips.net/video/zqhE-CepH2g/видео.htmlsi=ZreHb7iXEBAIDFeA

    • @oumardicko5593
      @oumardicko5593 6 месяцев назад +1

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

  • @creatorsremose
    @creatorsremose 6 месяцев назад

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

  • @isekaijoucho4812
    @isekaijoucho4812 6 месяцев назад

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

  • @matthewnicastro2489
    @matthewnicastro2489 6 месяцев назад +2

    Just put auth in middleware like a chad

    • @sraj5923
      @sraj5923 6 месяцев назад

      exactly !!!

  • @mrlectus
    @mrlectus 6 месяцев назад

    can't middleware solve this?

  • @gofullstack
    @gofullstack 6 месяцев назад

    Reminds me of PHP

  • @adnanearef2959
    @adnanearef2959 6 месяцев назад

    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  6 месяцев назад

      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

  • @greendsnow
    @greendsnow 6 месяцев назад

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

    • @JollyCoding
      @JollyCoding  6 месяцев назад

      Good choice, each has their own strengths!

  • @BarakaAndrew
    @BarakaAndrew 6 месяцев назад

    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.

  • @salman0ansari
    @salman0ansari 6 месяцев назад

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

  • @gopallohar5534
    @gopallohar5534 6 месяцев назад +3

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

    • @JollyCoding
      @JollyCoding  6 месяцев назад +1

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

    • @gopallohar5534
      @gopallohar5534 6 месяцев назад +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 6 месяцев назад

      @@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)

  • @denisblack9897
    @denisblack9897 6 месяцев назад

    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 😅

  • @abdelmounaimammari9064
    @abdelmounaimammari9064 6 месяцев назад

    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  6 месяцев назад +1

      Will have to check that out thanks!

  • @masaeedi1
    @masaeedi1 6 месяцев назад

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

  • @zman-1x1
    @zman-1x1 6 месяцев назад

    This is so easy to mess up!

  • @henriquematias1986
    @henriquematias1986 6 месяцев назад

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

  • @kal.leroux
    @kal.leroux 6 месяцев назад

    that's just people using the tool tge wrong way

    • @JollyCoding
      @JollyCoding  6 месяцев назад

      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

  • @dimitrisefstathiadis6562
    @dimitrisefstathiadis6562 6 месяцев назад

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

  • @dimitrisefstathiadis6562
    @dimitrisefstathiadis6562 6 месяцев назад

    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..?

  • @krtirtho
    @krtirtho 6 месяцев назад

    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  6 месяцев назад

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

  • @ticler
    @ticler 6 месяцев назад +4

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

    • @JollyCoding
      @JollyCoding  6 месяцев назад

      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 6 месяцев назад

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

    • @lodosdurak7913
      @lodosdurak7913 6 месяцев назад

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

    • @RadTwin
      @RadTwin 6 месяцев назад

      Always one of those people 😅

  • @ghostinplainsight4803
    @ghostinplainsight4803 6 месяцев назад

    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 () => {}
    ```