From bd2475b3c62cd06acea6716dea167d74fa646aca Mon Sep 17 00:00:00 2001 From: Elysia <71698422+aiko-chan-ai@users.noreply.github.com> Date: Tue, 28 Mar 2023 19:20:05 +0700 Subject: [PATCH] feat: parseCookie --- package.json | 1 + src/rest/RequestHandler.js | 46 +++++++++++++------------------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 38551d3..0f65939 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "json-bigint": "^1.0.0", "node-fetch": "^2.6.9", "safe-base64": "^2.0.1-0", + "set-cookie-parser": "^2.6.0", "string_decoder": "^1.3.0", "string-similarity": "^4.0.4", "ws": "^8.13.0" diff --git a/src/rest/RequestHandler.js b/src/rest/RequestHandler.js index 9d3c07a..c3b677b 100644 --- a/src/rest/RequestHandler.js +++ b/src/rest/RequestHandler.js @@ -4,6 +4,7 @@ const { setTimeout } = require('node:timers'); const { setTimeout: sleep } = require('node:timers/promises'); const { inspect } = require('util'); const { AsyncQueue } = require('@sapphire/async-queue'); +const parseCookie = require('set-cookie-parser'); const DiscordAPIError = require('./DiscordAPIError'); const HTTPError = require('./HTTPError'); const RateLimitError = require('./RateLimitError'); @@ -11,25 +12,6 @@ const { Events: { DEBUG, RATE_LIMIT, INVALID_REQUEST_WARNING, API_RESPONSE, API_REQUEST, CAPTCHA_REQUIRED }, } = require('../util/Constants'); -const cookieFilter = str => { - const blackList = ['expires', 'path', 'domain', 'httponly', 'secure', 'max-age', 'samesite']; - if (blackList.some(s => str.toLowerCase().includes(`${s}`))) return false; - return true; -}; - -function parseCookie(str, old) { - const oldProps = old.split(';').filter(cookieFilter); - const allProps = str.split(';').filter(cookieFilter); - // Update data from all to old - allProps.forEach(prop => { - const key = prop.split('=')[0]; - const index = oldProps.findIndex(s => s.startsWith(key)); - if (index !== -1) oldProps[index] = prop; - else oldProps.push(prop); - }); - return oldProps.filter(s => s).join('; '); -} - const captchaMessage = [ 'incorrect-captcha', 'response-already-used', @@ -259,19 +241,21 @@ class RequestHandler { let sublimitTimeout; if (res.headers) { - // Cookie: - const cookie = res.headers.get('set-cookie'); - if (cookie) { - if (typeof cookie == 'string') { - this.manager.client.options.http.headers.Cookie = parseCookie( - cookie, - this.manager.client.options.http.headers.Cookie || '', - ); - this.manager.client.emit( - 'debug', - `[REST] Set new cookie: ${this.manager.client.options.http.headers.Cookie}`, - ); + const cookie = res.headers.raw()['set-cookie']; + if (cookie && Array.isArray(cookie)) { + const oldCookie = parseCookie((this.manager.client.options.http.headers.Cookie || '').split('; '), { + map: true, + }); + const parse = parseCookie(cookie, { + map: true, + }); + for (const key in parse) { + oldCookie[key] = parse[key]; } + this.manager.client.options.http.headers.Cookie = Object.entries(oldCookie) + .map(([key, value]) => `${key}=${value.value}`) + .join('; '); + this.manager.client.emit('debug', `[REST] Set new cookie: ${this.manager.client.options.http.headers.Cookie}`); } const serverDate = res.headers.get('date'); const limit = res.headers.get('x-ratelimit-limit');