Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Cannot send secure cookie over unencrypted connection #825

Open
3 tasks done
jgcmarins opened this issue Sep 12, 2023 · 0 comments
Open
3 tasks done

[fix] Cannot send secure cookie over unencrypted connection #825

jgcmarins opened this issue Sep 12, 2023 · 0 comments
Labels

Comments

@jgcmarins
Copy link

Describe the bug

Node.js version: v18.17.1

OS version: macOS 13.5.2 (22G91)

Description: I am trying to test against a Koa API that sets a HTTP cookie with a secure flag true, but when the API does that, I get the error "Cannot send the secure cookie over unencrypted connection".
I know the cookie module throws this error, but the problem is related to the supertest request which is a HTTP request, not a HTTPS.

Actual behavior

  • Supertest request does not support secure HTTP request

Expected behavior

  • Supertest request supports HTTP request

Code to reproduce

setCookie function

const DEFAULT_MAX_AGE = 7 * 24 * 60 * 60 * 100;

export const setCookie =
  (koaContext: Context) =>
  (cookieName: string, token: string, maxAge: number = DEFAULT_MAX_AGE) => {
    try {
      const domain = null;
      const secure = config.NODE_ENV !== 'development';
      const sameSite = config.APP_ENV === 'development' ? 'Lax' : 'None';

      const options = {
        httpOnly: true,
        overwrite: true,
        maxAge,
        secure,
        domain,
        signed: false,
        sameSite,
      };

      koaContext.cookies.set(cookieName, token, options);
    } catch (err) {
      console.log('set cookie failed: ', err);
    }
  };

supertest request

const response = await request(app.callback())
    .post('/api')
    .set({
      Accept: 'application/json',
      'Content-Type': 'application/json',
    })
    .send(JSON.stringify(payload));

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@jgcmarins jgcmarins added the bug label Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant