elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.27k stars 219 forks source link

onError handler isn't getting called during INVALID_COOKIE_SIGNATURE #707

Open binamralamsal opened 3 months ago

binamralamsal commented 3 months ago

What version of Elysia.JS is running?

1.0.26

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?

 .onError(({ code, set, cookie: { accessToken, refreshToken } }) => {
    if (code === "NOT_FOUND")
      return { error: "Route not found :(", status: STATUS.ERROR };

    if (code === "INVALID_COOKIE_SIGNATURE")
      return { error: "Your cookies has been altered", status: STATUS.ERROR };

    set.status = 500;
    return { error: "Internal Server Error", status: STATUS.ERROR };
  })

What is the expected behavior?

When cookies have been altered, it should respond with the format { error: "...", status: "ERROR" }

What do you see instead?

I am just getting raw text when cookies have been altered. onError is not getting called.

Additional information

No response

binamralamsal commented 3 months ago

I am not sure if it's related but when I have separate UI and backend, CORS error is occuring if cookies are altered.

nxht commented 4 weeks ago

Same issue here for WSL, on elysia 1.1.13. Also, it seems it works fine on aot: false case

Minimal reproduction example:

import Elysia from 'elysia';

export const app = new Elysia({
  cookie: { secrets: 'secret', sign: ['session'] },
})
  .onError(({ error }) => {
    console.log(error);
    return {};
  })
  .get('', ({ cookie: { session } }) => {
    console.log(session);
  })
  .listen(3000);
console.log('listening on port 3000');

Other errors prints the error and return {} as response But when try sending request with cookie session=1234, error is not caught at all