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

Cookies don't send after redirect #437

Open
sheldhur opened this issue Aug 22, 2024 · 2 comments
Open

Cookies don't send after redirect #437

sheldhur opened this issue Aug 22, 2024 · 2 comments

Comments

@sheldhur
Copy link

sheldhur commented Aug 22, 2024

  const options = {
    follow_set_cookies: true,
    cookies: {
      test: new Date().valueOf()
    }
  };

  const response = await needle('GET', 'https://setcookie.net/', undefined, options);
  console.log(response.body.match(/<code>(.+)<\/code>/i)?.[1]);

  const response2 = await needle('GET', 'https://shorturl.at/BQUI3', undefined, options);
  console.log(response2.body.match(/<code>(.+)<\/code>/i)?.[1]);

The example can looks strange but in real case it's a redirect from www.host.com to host.com

It was broken in 3.1.0 after had added the challenge host_and_ports_match

needle/lib/needle.js

Lines 542 to 553 in 9454d7b

// if follow_set_cookies is true, insert cookies in the next request's headers.
// we set both the original request cookies plus any response cookies we might have received.
if (config.follow_set_cookies && utils.host_and_ports_match(headers.location, uri)) {
var request_cookies = cookies.read(config.headers['cookie']);
config.previous_resp_cookies = resp.cookies;
if (Object.keys(request_cookies).length || Object.keys(resp.cookies || {}).length) {
config.headers['cookie'] = cookies.write(extend(request_cookies, resp.cookies));
}
} else if (config.headers['cookie']) {
debug('Clearing original request cookie', config.headers['cookie']);
delete config.headers['cookie'];
}

#428

@sheldhur
Copy link
Author

@tomas can you(or I?) just remove this challenge or add a callback with the challenge in config? It's wrong because for example SSO usually located an another domain

@tomas
Copy link
Owner

tomas commented Aug 23, 2024

I'm not sure what the problem is. Feel free to submit a PR (with tests) and I'll be happy to merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants