The Problem with Using Layouts for Auth

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ต.ค. 2024

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

  • @Huntabyte
    @Huntabyte  ปีที่แล้ว +69

    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 TH-cam 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 ปีที่แล้ว +14

    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 🙏🙏🙏

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว +1

      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.

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

  • @adamshand
    @adamshand ปีที่แล้ว +2

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

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You're welcome!

  • @Hugos68
    @Hugos68 ปีที่แล้ว +3

    Thanks for covering this in a video

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You’re very welcome!

  • @somebodyHereFor
    @somebodyHereFor 3 หลายเดือนก่อน

    MAN, IDK HOW TO THANK YOU ENOUGH

  • @_phenomen
    @_phenomen ปีที่แล้ว +41

    Supabase's SvelteKit starter guide is a security disaster

    • @lukatoseus
      @lukatoseus ปีที่แล้ว +1

      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 9 หลายเดือนก่อน

      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

  • @jangxyz
    @jangxyz 5 หลายเดือนก่อน

    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)

  • @チョリパン-j4f
    @チョリパン-j4f 10 หลายเดือนก่อน

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

  • @whatever7338
    @whatever7338 ปีที่แล้ว +5

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

    • @Huntabyte
      @Huntabyte  21 วันที่ผ่านมา

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

  • @lwinklly
    @lwinklly 8 หลายเดือนก่อน +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

  • @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!

  • @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.

  • @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.

  • @FisshatsyonHaileamlak
    @FisshatsyonHaileamlak 11 หลายเดือนก่อน

    do you think this happens for react router too?

  • @mihaiandrei434
    @mihaiandrei434 ปีที่แล้ว +1

    Dang. Didn’t knew this. Thank you!

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You’re welcome!

  • @--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.

  • @MikeHTMLAllTheThings
    @MikeHTMLAllTheThings ปีที่แล้ว +1

    Great insight, totally didn't consider this.

  • @IBakeCookies
    @IBakeCookies ปีที่แล้ว

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

  • @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.

  • @FlygOnLiTe
    @FlygOnLiTe ปีที่แล้ว +1

    High value as always!!

  • @sourabh8044
    @sourabh8044 ปีที่แล้ว +1

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

  • @kvetoslavnovak423
    @kvetoslavnovak423 8 หลายเดือนก่อน

    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 3 หลายเดือนก่อน

      there is, use server side hook checks 🙂

  • @weistrass
    @weistrass ปีที่แล้ว +1

    Does this apply to server.js API routes?

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Yes!

    • @7on
      @7on 9 หลายเดือนก่อน

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

  • @ricardoamendoeira3800
    @ricardoamendoeira3800 11 หลายเดือนก่อน

    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  11 หลายเดือนก่อน

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

  • @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 TH-cam 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.

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

  • @lahcencodery
    @lahcencodery ปีที่แล้ว +2

    Thanks for this video, was making this mistake

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      You’re very welcome!

  • @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!

  • @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.

  • @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 5 หลายเดือนก่อน

    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 11 หลายเดือนก่อน +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  11 หลายเดือนก่อน

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

    • @7on
      @7on 9 หลายเดือนก่อน

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

  • @Hugos68
    @Hugos68 ปีที่แล้ว +1

    Hahahaha nice :)

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

  • @cloudfumes7367
    @cloudfumes7367 ปีที่แล้ว

    Surreldb with sveltekit tutorial..

  • @flogginga_dead_horse4022
    @flogginga_dead_horse4022 ปีที่แล้ว +1

    great video

    • @Huntabyte
      @Huntabyte  ปีที่แล้ว

      Glad you enjoyed it

  • @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?