The Problem with Using Layouts for Auth

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

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

  • @Huntabyte
    @Huntabyte  Год назад +70

    I don't know about you all, but I'm a for protecting large groups of routes in the handle Hook.

    • @benaloney
      @benaloney Год назад +11

      Is that a custom Svelte emoji in a RUclips comment???!
      🤯🤯🤯

    • @Huntabyte
      @Huntabyte  Год назад +10

      @@benaloney one of the perks of being a channel member

    • @swyxTV
      @swyxTV Год назад +2

      :simp:

    • @krazymeanie
      @krazymeanie Год назад +6

      @@swyxTV 😭

    • @theether5993
      @theether5993 Год назад +1

      Me too btw great tutorial

  • @maskman4821
    @maskman4821 Год назад +15

    Wow, I really learned a lot from your tutorials about Sveltekit, this is the best and the most valuable learning resources for people who want to quickly dive in Sveltekit application development, I have actually got stuck in authentication for quiet a while because I have no idea how hooks, locals, etc. work and what do they do until your tutorials come out and it save my day, thank you so much 🙏🙏🙏

  • @richardfeynman-sd3rg
    @richardfeynman-sd3rg Год назад +33

    This is high quality stuff, the kind of errors most people would make and gloss over. Hoping now that sveltekit 1.0 is out, it starts to get some serious industry traction.

  • @boian-inavov
    @boian-inavov Год назад +20

    Thanks for bringing exposure to this! Since the layout call is cached on continuous redirections it shouldn’t actually be used for auth, but hooks should be, since they’re running on every request as middleware. Still it seems like an easy mistake to be made, so people should definitely be mindful of this case 👏

    • @Huntabyte
      @Huntabyte  Год назад +2

      You’re welcome! Exactly, or check every page.server load and action.

    • @jrmc732
      @jrmc732 Год назад +4

      thb this is a framework issue (I know the Developer needs to know this), but this mistake is too easy to make, a framework should protect you from doing extremly dumb things.

  • @adamshand
    @adamshand Год назад +2

    I was making this mistake and being confused by why it wasn't working as I expected. Thanks!!

  • @RedBlade3
    @RedBlade3 Год назад +10

    Wow this was awesome! Thank you so much for this! Hopefully you'll do more security / structuring videos!
    I'll try to support you financially next year because these vids are so valuable for me! Keep it up

    • @Huntabyte
      @Huntabyte  Год назад +3

      Do not worry about supporting me financially! Just viewing my content and providing feedback is more than enough support!

    • @thothtrismegistus929
      @thothtrismegistus929 Год назад +1

      @@Huntabyte I could only find updated videos on your channel. Can you also do a video on implementing sveltekit auth with directus?

    • @Huntabyte
      @Huntabyte  Год назад

      @@thothtrismegistus929 this has been highly requested and is certainly something on my radar to work on very soon!

  • @codewithguillaume
    @codewithguillaume Год назад +1

    Dude gonna watch this as soon I am at home

    • @codewithguillaume
      @codewithguillaume Год назад +1

      As a svelte user I feel that it’s very interesting

  • @DavePerth
    @DavePerth Год назад +1

    Thanks Hunter - letting you know the github links are broken, but you can find them in all Repositories...

    • @Huntabyte
      @Huntabyte  Год назад +1

      Whoops! The repo was private!

  • @lwinklly
    @lwinklly 9 месяцев назад +1

    This video appeared in my recommended loads of times over the past year and I never clicked it, I only remembered now it because I just found out our site has this problem lel

  • @_phenomen
    @_phenomen Год назад +43

    Supabase's SvelteKit starter guide is a security disaster

    • @lukatoseus
      @lukatoseus Год назад +2

      Is that still the case?

    • @MatiasMeno
      @MatiasMeno Год назад

      ⁠@@lukatoseusno. It uses a combination of hooks, locals and depends to make sure your routes are invalidated.

    • @DevTesting-lw6ed
      @DevTesting-lw6ed 10 месяцев назад

      I had to read the whole github issue comments to understand the supabase implementation.

  • @SumanthChinthagunta
    @SumanthChinthagunta Год назад +2

    I was using login check wrongly in layout , thanks for explaining

  • @チョリパン-j4f
    @チョリパン-j4f 11 месяцев назад

    Thank you so much, I was doing exactly the example of the beginning

  • @somebodyHereFor
    @somebodyHereFor 4 месяца назад

    MAN, IDK HOW TO THANK YOU ENOUGH

  • @whatever7338
    @whatever7338 Год назад +5

    The problem is that you shouldnt be running these checks on the client anyway.

    • @Huntabyte
      @Huntabyte  Месяц назад

      None of these checks are done on the client in this video :)

  • @Hugos68
    @Hugos68 Год назад +3

    Thanks for covering this in a video

  • @samifouad
    @samifouad Год назад +1

    it's not necessary to make another request just to validate a user, it works but for latency reasons it's easier to work with JWT in an http only cookie as they can be a tool to quickly validate the request as authentic without an extra network request to do the same work

    • @Huntabyte
      @Huntabyte  Год назад +1

      There are a few different trade-offs with JWTs vs Sessions.

  • @MikeHTMLAllTheThings
    @MikeHTMLAllTheThings Год назад +1

    Great insight, totally didn't consider this.

  • @sourabh8044
    @sourabh8044 Год назад +1

    Thanks for another great tutorial. Could you please create a video for firebase and sveltekit authentication?

  • @mihaiandrei434
    @mihaiandrei434 Год назад +1

    Dang. Didn’t knew this. Thank you!

  • @ricardoamendoeira3800
    @ricardoamendoeira3800 Год назад

    The main issue here is the server returning data without making any security checks, that's the main issue to be addressed. The server should never return non-public data without checking for a valid token or session.
    Get that done and all this client side validation becomes purely a UX improvement, as it should be, because you can't force attackers to go through your client side code before making requests to your server.

    • @Huntabyte
      @Huntabyte  Год назад

      I have no clue what you're talking about in your comments as there is 0 client-side validation in this video.

  • @--Arthur
    @--Arthur Год назад +4

    I authenticate and authorize every request using `event.locals` - so even if an user somehow navigates to admin routes, they won't see any data.
    From my POV authorizing users in `layout.server.ts` in more of an accessibility feature to deny navigating somewhere in the first place.

  • @otockian
    @otockian Год назад +3

    The real problem here is that the database call isn't passing the auth token along before it pulls data so that the backend ensures your authenticated and have access to the data. That's the real issue, in fact even with the fixes you did, someone could still spoof and call that call to get customers and still get them. Fix that, and everything you showed here is fixed, and then yes you can do the rest of the stuff. But all backend calls for any sensitive data that should be scoped to a role, should pass that data along.

    • @Huntabyte
      @Huntabyte  Год назад

      No they couldn’t. The hook is pulling/“validating” the token in this example and went you run await parent it makes a request to the server, thus validating the token/session/whatever.

    • @PanosPitsi
      @PanosPitsi Год назад +3

      @@Huntabytestill if it’s really sensitive data don’t just expose it in an api with no validation whatsoever, client side validation is not for security it’s for user convenience. Never trust the client no matter what, all security implementations should be server side.

  • @jangxyz
    @jangxyz 7 месяцев назад

    1 year later, for some reason cloning the exact same repo#main does not seem to show the behavior. It always runs "Run Layout Auth Check" for the same scenario. Any idea why? Curious as it is running the same sveltekit version (1.0.0)

  • @SRG-Learn-Code
    @SRG-Learn-Code Год назад

    Cool! One odd/wrong question. How would you "protect" some roots in a static page? I know it's impossible to run auth on a CDN or make code not accesible if you know the url but... Knowing the code will be shown, what would you do to make harder the discoverability of new routes. Before anyone ask, yep, I know it's worthless, just wondering.

  • @FisshatsyonHaileamlak
    @FisshatsyonHaileamlak Год назад

    do you think this happens for react router too?

  • @tensor5113
    @tensor5113 Год назад

    If you're doing resource access/manipulation without any user checks and it lives close to the client(not a vpc) you're already introducing a lot of security issues into your app. Always make sure the user has permission, for example, what happens if the organization downgrades their access, the idea of a wildcard param thats actually queryable so close to the client will introduce security issues, be it sveltekit, or any framework. Because your basically saying, I'm trusting this small niche feature of a new webapp to always perform as expected through each update

  • @FlygOnLiTe
    @FlygOnLiTe Год назад +1

    High value as always!!

  • @IBakeCookies
    @IBakeCookies Год назад

    What if you copy +layout.server.ts, create +layout.ts and paste the same logic there? Wouldnt that solve the issue?

  • @swyxTV
    @swyxTV Год назад

    did you look at the svelte auth package the vercel guys put out? does that solve things

    • @CokaKoala
      @CokaKoala Год назад

      The example project thankfully demonstrates the correct usage by awaiting the parent in the protected +page.ts routes (I had a GitHub link here directing to the +page.ts in the example repo, but RUclips didn’t like that 😂)
      But with that being said, users are still capable of incorrectly setting up the auth by doing what Hunter demonstrated, even with the provided package. If you validate the session in +layout.ts , but don’t await the parent in the child routes, you’ll face this exact issue

    • @Huntabyte
      @Huntabyte  Год назад

      Exactly what @Adrian said. The handle wrapper svelteauth provides only sets the session/user into locals, it doesn’t actually protect anything - it’s up to the developer to implement that themselves.

  • @lahcencodery
    @lahcencodery Год назад +2

    Thanks for this video, was making this mistake

  • @weistrass
    @weistrass Год назад +1

    Does this apply to server.js API routes?

    • @Huntabyte
      @Huntabyte  Год назад

      Yes!

    • @7on
      @7on 10 месяцев назад

      No, +server api routes do not inherit from +layout.server files.

  • @karolus__
    @karolus__ Год назад

    nice vid, what about throwing errors from hooks.server? Are we able to display +error.svelte page?

    • @Huntabyte
      @Huntabyte  Год назад

      I actually don’t think I’ve had a reason to try that! Let me know if it works for you!

    • @karolus__
      @karolus__ Год назад

      Throwing an error in hooks.server displays default SvelteKit error, I wasn’t able to display error.svelte. The reason is actually simple for example instead of redirecting to login you would like to show 401, 403 or even 404. I guess you can always use page.server to do that, but then your auth logic is splitted a bit

  • @maskman4821
    @maskman4821 Год назад +1

    Sir, can you make a tutorial on Sveltekit + Mongodb CRUD/Auth and page/server route protection, I got reading mongodb data working, but I cant find answers about how to create models and perform Create/Update/Delete operations, hopefully you can help people who want to migrate MERN app into Sveltekit 🙏

    • @Huntabyte
      @Huntabyte  Год назад

      I will certainly look into this, thanks a ton for the suggestion!

  • @kvetoslavnovak423
    @kvetoslavnovak423 9 месяцев назад

    Trying to have the check in root layout.server.js but this causes infinite loop redirect for me 😞 It seems there is no way in SvelteKit to solve this.

    • @rafaelrocha3991
      @rafaelrocha3991 4 месяца назад

      there is, use server side hook checks 🙂

  • @1roOt
    @1roOt Год назад +1

    I thought about this as well. Then I thought: What would happen if I somehow disable/intercept redirects in my browser? Is the redirect the only thing protecting the route? Has anyone info about that?

    • @Huntabyte
      @Huntabyte  Год назад

      I actually haven’t. You could also throw an error to render out an error page in place!

    • @TimeoutMegagameplays
      @TimeoutMegagameplays Год назад

      The redirect is being returned _instead_ of the actual page or usual response. If your browser decides not redirect to the page it's simply going to sit on nothing as no other data has been return from that GET request.

  • @WooSup111
    @WooSup111 17 дней назад

    Looking this video end of 2024. Seems like the issue is fixed already. without await parent() load function doesn't proceed

  • @hufuhufu
    @hufuhufu Год назад

    What browser are you using there?

    • @Huntabyte
      @Huntabyte  Год назад +1

      Microsoft Edge

    • @hufuhufu
      @hufuhufu Год назад

      @@Huntabyte Cool, thanks.
      I like those side tabs, looks compact, never knew Edge had that kind of feature.

  • @yt-sh
    @yt-sh 6 месяцев назад

    TLDR Protected requests happens anyway when using Layout...

  • @sco1369
    @sco1369 Год назад +2

    Doesn't this assume that the API is insecure in the first place? I should never be able to access sensitive info without providing my JWT (or whatever mechanism you use). In your example, I could access all your data either way because you API exposes it.

    • @Huntabyte
      @Huntabyte  Год назад

      The API is an example. Pretend the API is a direct connection to a DB and the SvelteKit server is acting as our Authorization/authentication server

    • @ricardoamendoeira3800
      @ricardoamendoeira3800 Год назад +1

      ​@@HuntabyteThe server shouldn't return any data without a valid session or token, all this client side validation is just for user experience, it adds 0 security.

    • @Huntabyte
      @Huntabyte  Год назад

      @@ricardoamendoeira3800 it’s not client side validation. It’s validation in the server load function.

    • @7on
      @7on 10 месяцев назад

      @@Huntabyte Then your example will fail with a 403 because the token being sent to the server is A) invalid or B) deleted. With Pocketbase, it will invalidate the previous session with a logout. So even asynchronous it will fail because PB's auth checks are synchronous. And if you're implementing something by hand with a mysql socket connection or similar, you should really do the same - e.g. pass a token to a request and verify the token is still good before returning the data.

  • @cloudfumes7367
    @cloudfumes7367 Год назад

    Surreldb with sveltekit tutorial..

  • @Hugos68
    @Hugos68 Год назад +1

    Hahahaha nice :)

  • @flogginga_dead_horse4022
    @flogginga_dead_horse4022 Год назад +1

    great video

  • @Lukehagar
    @Lukehagar Год назад +4

    I'm in this video, and I don't like it :D

  • @ROX2
    @ROX2 Год назад +1

    SvelteKit route system is absolutely trashed at this moment. here is critical need one file route system with support of custom route guard\middlewares like it implemented in Laravel or Ember as example.

    • @PanosPitsi
      @PanosPitsi Год назад

      Never compare sveltekit with php again

  • @kvetoslavnovak423
    @kvetoslavnovak423 Год назад

    Thank you very much for this video and explanation of this issue.
    Do you think the official Supabase GitHub documentation and examples for SvelteKit auth-helpers supabase/auth-helpers/tree/main/packages/sveltekit is incorrect as well?