简体   繁体   English

来自节点获取的 Session cookie 无效?

[英]Session cookie from node-fetch is invalid?

I am writing a javascript program (for a github action) right now but ran into a problem.我现在正在编写一个 javascript 程序(用于 github 操作),但遇到了问题。
I was trying to log into www.overleaf.com and access the page https://www.overleaf.com/project after generating a session cookie by sending a POST request to https://www.overleaf.com/login with my credentials and the csrf token. I was trying to log into www.overleaf.com and access the page https://www.overleaf.com/project after generating a session cookie by sending a POST request to https://www.overleaf.com/login with my凭据和 csrf 令牌。
The response contained the requested token in the set-cookie header as expected, however, when I tried to access https://www.overleaf.com/project via GET, I get redirected back to https://www.overleaf.com/login正如预期的那样,响应在 set-cookie header 中包含所请求的令牌,但是,当我尝试通过 GET 访问https://www.overleaf.com/project时,我被重定向回Z5E056C500A1C4B6A7110B。 /登录
When copying a session cookie saved in my browser, the request works just fine as expected.复制保存在我的浏览器中的 session cookie 时,请求按预期工作。

I tried doing the same thing in the command line with cURL and it worked there.我尝试在命令行中使用 cURL 做同样的事情,它在那里工作。

I am fairly certain my authentication request is accepted by Overleaf's server, because I have tried intentionally incorrectly sending the password or the csrf token and in both cases, the response does not give me a new session cookie but sends the old one.我相当确定 Overleaf 的服务器接受了我的身份验证请求,因为我曾尝试故意错误地发送密码或 csrf 令牌,在这两种情况下,响应都没有给我新的 session cookie,而是发送旧的。

If anyone has any clue what is going wrong, I'd be very thankful for your input.如果有人知道出了什么问题,我将非常感谢您的意见。

This is what worked in the terminal, which I'm trying to replicate in javascript with node-fetch:这是在终端中起作用的,我试图在 javascript 中使用 node-fetch 复制:
curl -v --header "Content-Type: application/json" --cookie "GCLB=someothercookie;overleaf_session2=firstsessioncookie" --data '{"_csrf":"the_csrf_token", "email": "MYEMAIL", "password":"MYPASSWORD"}' https://www.overleaf.com/login
to get the cookie and csrf token and获取 cookie 和 csrf 令牌和
curl -v https://www.overleaf.com/project --cookie "overleaf_session2=returnedsessioncookie; GCLB=someothercookie" as the request that returns the html page of my projects. curl -v https://www.overleaf.com/project --cookie "overleaf_session2=returnedsessioncookie; GCLB=someothercookie"作为返回项目的ZFC35FDC70D5FC69D2698888A页面的ZFC35FDC70D5FC69D26988888A页面的请求

This is my javascript code, I have double, triple, quadruple checked it but I think I'm missing something.这是我的 javascript 代码,我检查了两次、三次、四次,但我认为我遗漏了一些东西。

const fetch = require("node-fetch");
const parser = require("node-html-parser");
const scparser = require("set-cookie-parser");
 
async function run() {
 
  const email = process.env.EMAIL;
  const password = process.env.PASSWORD;
 
  var cookies = await login(email, password);
 
  console.log(await all_projects(cookies));
 
}
 
async function login(email, password) {
  const login_get = await fetch("https://www.overleaf.com/login");
 
  const get_cookies = login_get.headers.raw()["set-cookie"];
  const parsed_get_cookies = scparser.parse(get_cookies, {
    decodeValues: false
  });
  const overleaf_session2_get = parsed_get_cookies.find(
    (element) => element.name == "overleaf_session2"
  ).value;
  const gclb = parsed_get_cookies.find(
    (element) => element.name == "GCLB"
  ).value;
  console.log("overleaf_session2_get:", overleaf_session2_get, "gclb:", gclb);
 
  const get_responsetext = await login_get.text();
  const _csrf = parser
    .parse(get_responsetext)
    .querySelector("input[name=_csrf]")
    .getAttribute("value");
 
  login_json = { _csrf: _csrf, email: email, password: password };
  console.log(login_json);
  const login_post = await fetch("https://www.overleaf.com/login", {
    method: "post",
    body: JSON.stringify(login_json),
    headers: {
      "Content-Type": "application/json",
      "Cookie": "GCLB=" + gclb + ";overleaf_session2=" + overleaf_session2_get
    }
  });
 
  const post_cookies = login_post.headers.raw()["set-cookie"];
  const parsed_post_cookies = scparser.parse(post_cookies, {
    decodeValues: false
  });
  const overleaf_session2_post = parsed_post_cookies.find(
    (element) => element.name == "overleaf_session2"
  ).value;
 
  console.log(
    "successful:",
    overleaf_session2_get != overleaf_session2_post ? "true" : "false"
  );
  console.log(await fetch("https://www.overleaf.com/project", {
    headers: {
      "Cookie": "overleaf_session2=" + overleaf_session2_post
    }
  }))
  return "overleaf_session2=" + overleaf_session2_post;
}
 
async function all_projects(cookies) {
  const res = await fetch("https://www.overleaf.com/project", {
    headers: {
      Cookie: cookies
    }
  });
  return res;
}
 
run();

Yes your authentication request is probably valid however this is likely to be a security issue which browsers do not allow you to do such thing and freely access another website's cookie.是的,您的身份验证请求可能是有效的,但这可能是一个安全问题,浏览器不允许您这样做并自由访问另一个网站的 cookie。

Browsers do not allow you to access other domain's cookies, If they did then web would be an unsafe place because for example Stackoverflow could access my Facebook account cookie and extract my personal information.浏览器不允许您访问其他域的 cookies,如果他们这样做了,那么 web 将是一个不安全的地方,因为例如 Stackoverflow 可以访问我的 Facebook 并提取我的个人帐户信息。

I fixed my issue by not using node-fetch and switching to https .我通过不使用node-fetch并切换到https解决了我的问题。

Here is what worked:这是有效的:

async function login(email, password) {
  //GET login page
  const get = await get_login();
  //get necessary info from response
  const csrf = parser
    .parse(get.html)
    .querySelector(`meta[name="ol-csrfToken"]`)
    .getAttribute("content");
  const session1 = scparser
    .parse(get.headers["set-cookie"], { decodeValues: false })
    .find((cookie) => cookie.name == "overleaf_session2").value;
  const gclb = scparser
    .parse(get.headers["set-cookie"], { decodeValues: false })
    .find((cookie) => cookie.name == "GCLB").value;

  //POST login data
  const post = await post_login(csrf, email, password, session1, gclb);
  //get necessary data from response
  const session2 = scparser
    .parse(post["set-cookie"], { decodeValues: false })
    .find((cookie) => cookie.name == "overleaf_session2").value;

  //GET new csrf token from project page
  const projects = await get_projects(session2, gclb);
  const csrf2 = parser
    .parse(projects.html)
    .querySelector(`meta[name="ol-csrfToken"]`)
    .getAttribute("content");

  //return data
  return {
    session: session2,
    gclb: gclb,
    csrf: csrf2,
    projects: projects.html
  };
}

async function get_login() {
  const url = "https://www.overleaf.com/login";
  return new Promise((resolve) => {
    https.get(url, (res) => {
      var data;
      res.on("data", (chunk) => {
        data += chunk;
      });
      res.on("end", () => {
        resolve({ html: data, headers: res.headers });
      });
    });
  });
}

async function get_projects(session2, gclb) {
  const url = "https://www.overleaf.com/project";
  return new Promise((resolve) => {
    https.get(
      url,
      { headers: { Cookie: `GCLB=${gclb};overleaf_session2=${session2}` } },
      (res) => {
        var data;
        res.on("data", (chunk) => {
          data += chunk;
        });
        res.on("end", () => {
          resolve({ html: data, headers: res.headers });
        });
      }
    );
  });
}

async function post_login(_csrf, email, password, session1, gclb) {
  const url = "https://www.overleaf.com/login";
  const options = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Cookie: `GCLB=${gclb};overleaf_session2=${session1}`
    }
  };
  const postData = {
    _csrf: _csrf,
    email: email,
    password: password
  };
  return new Promise((resolve) => {
    var req = https.request(url, options, (res) => {
      resolve(res.headers);
    });

    req.on("error", (e) => {
      console.error(e);
    });

    req.write(JSON.stringify(postData));
    req.end();
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM