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

Handle cookies on redirection #3589

Merged
merged 8 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"express-static-gzip": "~2.1.7",
"form-data": "~4.0.0",
"gamedig": "~4.1.0",
"http-cookie-agent": "~5.0.4",
"html-escaper": "^3.0.3",
"http-graceful-shutdown": "~3.1.7",
"http-proxy-agent": "~5.0.0",
Expand Down Expand Up @@ -140,6 +141,7 @@
"tar": "~6.1.11",
"tcp-ping": "~0.1.1",
"thirty-two": "~1.0.2",
"tough-cookie": "~4.1.3",
"ws": "^8.13.0"
},
"devDependencies": {
Expand Down
10 changes: 8 additions & 2 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const https = require("https");
const dayjs = require("dayjs");
const axios = require("axios");
const { Prometheus } = require("../prometheus");
Expand All @@ -23,6 +22,8 @@ const jsonata = require("jsonata");
const jwt = require("jsonwebtoken");
const crypto = require("crypto");
const { UptimeCalculator } = require("../uptime-calculator");
const { CookieJar } = require("tough-cookie");
Copy link
Collaborator

@CommanderStorm CommanderStorm Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand how adding these two dependencies solves the problem.
(the implementation looks reasonable, I just don't understand the documentation of the two projects and why they are needed)

  • Why is http-cookie-agent needed?
    (what is the difference between https.Agent and HttpsCookieAgent)
  • How/is the CookieJar shared?
    (Can other requests leak cookies via this mechanism?)

Copy link
Owner

@louislam louislam Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tough-cookie should be just a pure standalone cookie api and storage. http-cookie-agent connects between tough-cookie and axios.

However, the problem is that a http agent and axios is an one-to-one relationship and it should be used by CacheableDnsHttpAgent already.

And the fun fact is that I just realized that I maybe messed up something in #1853, CacheableDnsHttpAgent does not seem to be used in http/https monitor.

Edit: Just realized again it is registered globally by CacheableDnsHttpAgent.registerGlobalAgent().

Copy link
Contributor Author

@dakriy dakriy Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new cookie jar is created for every request (variable is scoped to the monitor.js:beat function) and it uses an in memory storage implementation by default . It should be per request meaning that no cookies are ever shared outside of a single request if my understanding of the beat function is correct. I was under the impression that the CachableDnsHttpAgent was only used for docker and steam types of heartbeats meaning there shouldn't be any conflict since the beat types are mutually exclusive (else if). Those types also don't need cookie storage so I thought it wasn't needed there. I do think though there should be one central place to create an http agent rather than many things each creating one slightly differently but that's more a matter of design. I tested the changes and they worked as expected. Should I implement it differently?

Copy link
Contributor Author

@dakriy dakriy Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also must be missing something because I don't see any references for CacheableDnsHttpAgent.registerGlobalAgent() in the project in the 2.0.X branch or the master branch.

Oh I found it. In the commit 3b87209 it was changed to update. And it seems the only usages of it which are in the beat function in monitor.js pulls the agent and manually sets it on the axios options every beat anyway meaning there is no conflict and it's not really registered "globally" per se.

let res = await axios.get(steamApiUrl, {
timeout: this.timeout * 1000,
headers: {
"Accept": "*/*",
"User-Agent": "Uptime-Kuma/" + version,
},
httpsAgent: CacheableDnsHttpAgent.getHttpsAgent({
maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940)
rejectUnauthorized: !this.getIgnoreTls(),
}),
httpAgent: CacheableDnsHttpAgent.getHttpAgent({
maxCachedSessions: 0,
}),
maxRedirects: this.maxredirects,
validateStatus: (status) => {
return checkStatusCode(status, this.getAcceptedStatuscodes());
},
params: {
filter: filter,
key: steamAPIKey,
}
});

const { HttpsCookieAgent } = require("http-cookie-agent/http");

const rootCertificates = rootCertificatesFingerprints();

Expand Down Expand Up @@ -507,7 +508,12 @@ class Monitor extends BeanModel {
}

if (!options.httpsAgent) {
options.httpsAgent = new https.Agent(httpsAgentOptions);
let jar = new CookieJar();
let httpsCookieAgentOptions = {
...httpsAgentOptions,
cookies: { jar }
};
options.httpsAgent = new HttpsCookieAgent(httpsCookieAgentOptions);
}

if (this.auth_method === "mtls") {
Expand Down
20 changes: 16 additions & 4 deletions server/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const HttpsProxyAgent = require("https-proxy-agent");
const SocksProxyAgent = require("socks-proxy-agent");
const { debug } = require("../src/util");
const { UptimeKumaServer } = require("./uptime-kuma-server");
const { CookieJar } = require("tough-cookie");
const { createCookieAgent } = require("http-cookie-agent/http");

class Proxy {

Expand Down Expand Up @@ -95,10 +97,13 @@ class Proxy {
let httpAgent;
let httpsAgent;

let jar = new CookieJar();

const proxyOptions = {
protocol: proxy.protocol,
host: proxy.host,
port: proxy.port,
cookies: { jar },
};

if (proxy.auth) {
Expand All @@ -112,12 +117,17 @@ class Proxy {
switch (proxy.protocol) {
case "http":
case "https":
httpAgent = new HttpProxyAgent({
// eslint-disable-next-line no-case-declarations
const HttpCookieProxyAgent = createCookieAgent(HttpProxyAgent);
// eslint-disable-next-line no-case-declarations
const HttpsCookieProxyAgent = createCookieAgent(HttpsProxyAgent);

httpAgent = new HttpCookieProxyAgent({
...httpAgentOptions || {},
...proxyOptions
...proxyOptions,
});

httpsAgent = new HttpsProxyAgent({
httpsAgent = new HttpsCookieProxyAgent({
...httpsAgentOptions || {},
...proxyOptions,
});
Expand All @@ -126,7 +136,9 @@ class Proxy {
case "socks5":
case "socks5h":
case "socks4":
agent = new SocksProxyAgent({
// eslint-disable-next-line no-case-declarations
const SocksCookieProxyAgent = createCookieAgent(SocksProxyAgent);
agent = new SocksCookieProxyAgent({
...httpAgentOptions,
...httpsAgentOptions,
...proxyOptions,
Expand Down