Skip to content

Commit 2c3d2ff

Browse files
committed
Make Secure-local-cookies work in CLI as well
1 parent a4fff01 commit 2c3d2ff

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

packages/bruno-cli/src/utils/cookies.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { Cookie, CookieJar } = require('tough-cookie');
22
const each = require('lodash/each');
3+
const { isPotentiallyTrustworthyOrigin } = require('@usebruno/requests').utils;
34

45
const cookieJar = new CookieJar();
56

@@ -11,7 +12,9 @@ const addCookieToJar = (setCookieHeader, requestUrl) => {
1112
};
1213

1314
const getCookiesForUrl = (url) => {
14-
return cookieJar.getCookiesSync(url);
15+
return cookieJar.getCookiesSync(url, {
16+
secure: isPotentiallyTrustworthyOrigin(url)
17+
});
1518
};
1619

1720
const getCookieStringForUrl = (url) => {

packages/bruno-electron/src/utils/cookies.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { Cookie, CookieJar } = require('tough-cookie');
2-
const { isPotentiallyTrustworthy } = require('./trustworthy-util');
32
const each = require('lodash/each');
43
const moment = require('moment');
4+
const { isPotentiallyTrustworthyOrigin } = require('@usebruno/requests').utils;
55

66
const cookieJar = new CookieJar();
77

@@ -14,7 +14,7 @@ const addCookieToJar = (setCookieHeader, requestUrl) => {
1414

1515
const getCookiesForUrl = (url) => {
1616
return cookieJar.getCookiesSync(url, {
17-
secure: isPotentiallyTrustworthy(url)
17+
secure: isPotentiallyTrustworthyOrigin(url)
1818
});
1919
};
2020

packages/bruno-requests/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export { addDigestInterceptor, getOAuth2Token } from './auth';
2+
3+
export * as utils from './utils';

packages/bruno-electron/src/utils/trustworthy-util.js renamed to packages/bruno-requests/src/utils/cookie-utils.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { URL } = require('url');
2-
const net = require('net');
1+
const { URL } = require('node:url');
2+
const net = require('node:net');
33

44
const isLoopbackV4 = (address) => {
55
// 127.0.0.0/8: first octet = 127
@@ -64,14 +64,16 @@ const hostNoBrackets = (host) => {
6464
* @returns {boolean}
6565
* @see {@link https://w3c.github.io/webappsec-secure-contexts/#potentially-trustworthy-origin W3C Spec}
6666
*/
67-
const isPotentiallyTrustworthy = (urlString) => {
67+
const isPotentiallyTrustworthyOrigin = (urlString) => {
6868
let url;
6969

7070
// try ... catch doubles as an opaque origin check
7171
try {
7272
url = new URL(urlString);
73-
} catch {
74-
return false;
73+
} catch (e) {
74+
if (e instanceof TypeError && e.code === 'ERR_INVALID_URL') {
75+
return false;
76+
} else throw e;
7577
}
7678

7779
const scheme = url.protocol.replace(':', '').toLowerCase();
@@ -99,5 +101,5 @@ const isPotentiallyTrustworthy = (urlString) => {
99101
}
100102

101103
module.exports = {
102-
isPotentiallyTrustworthy
104+
isPotentiallyTrustworthyOrigin
103105
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './cookie-utils';

0 commit comments

Comments
 (0)