Better malformed cookie handling

This commit is contained in:
Gregory Schier committed 2016-10-05 09:34:13 -07:00
1 parent 7bac7321bb
commit 2f67bceb75
7 files changed
+82 -11

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
import * as harUtils from '../export/har';
import * as db from '../database';
import * as render from '../render';
describe('exportHarWithRequest()', () => {
beforeEach(() => db.initDB({inMemoryOnly: true}, true));
it('renders does it correctly', async () => {
const workspace = await db.workspace.create();
const cookies = [{
creation: new Date('2016-10-05T04:40:49.505Z'),
key: 'foo',
value: 'barrrrr',
expires: new Date('2096-10-12T04:40:49.000Z'),
domain: 'google.com',
path: '/',
hostOnly: true,
lastAccessed: new Date('2096-10-05T04:40:49.505Z')
}];
await db.cookieJar.create({
parentId: workspace._id,
cookies
});
const request = Object.assign(db.request.init(), {
_id: 'req_123',
parentId: workspace._id,
headers: [{name: 'Content-Type', value: 'application/json'}],
parameters: [{name: 'foo bar', value: 'hello&world'}],
method: 'POST',
body: 'foo bar',
url: 'http://google.com',
authentication: {
username: 'user',
password: 'pass'
}
});
const renderedRequest = await render.getRenderedRequest(request);
const har = harUtils.exportHarWithRequest(renderedRequest);
expect(har.cookies.length).toBe(1);
expect(har).toEqual({
bodySize: -1,
cookies: [{
creation: '2016-10-05T04:40:49.505Z',
domain: 'google.com',
expires: '2096-10-12T04:40:49.000Z',
hostOnly: true,
key: 'foo',
name: 'foo',
path: '/',
value: 'barrrrr',
// lastAccessed is updated, so lets hack the assertion
lastAccessed: har.cookies[0].lastAccessed
}],
headers: [
{name: 'Content-Type', value: 'application/json'},
{name: 'Authorization', value: 'Basic dXNlcjpwYXNz'}
],
headersSize: -1,
httpVersion: 'HTTP/1.1',
method: 'POST',
postData: {text: 'foo bar'},
queryString: [{name: 'foo bar', value: 'hello&world'}],
url: 'http://google.com/'
});
});
});
+1 -2
View File
@@ -2,7 +2,6 @@ import * as networkUtils from '../network';
import * as db from '../database';
import nock from 'nock';
import {getRenderedRequest} from '../render';
import * as cookies from '../cookies';
describe('buildRequestConfig()', () => {
beforeEach(() => db.initDB({inMemoryOnly: true}, true));
@@ -103,7 +102,7 @@ describe('actuallySend()', () => {
mock = nock('http://127.0.0.1')
.matchHeader('Content-Type', 'application/json')
.matchHeader('Authorization', 'Basic dXNlcjpwYXNz')
.matchHeader('Cookie', 'foo=barrrrr; Expires=Fri, 12 Oct 2096 04:40:49 GMT; Path=/')
.matchHeader('Cookie', 'foo=barrrrr')
.post('/')
.query({'foo bar': 'hello&world'})
.reply(200, 'response body')
+1 -1
View File
@@ -30,7 +30,7 @@ export function cookieHeaderValueForUri (jar, uri) {
if (err) {
reject(err)
} else {
resolve(cookies.join('; '));
resolve(cookies.map(c => c.cookieString()).join('; '));
}
})
})
+2 -3
View File
@@ -38,8 +38,7 @@ export async function exportHar (requestId, addContentLength = false) {
function getCookies (renderedRequest) {
const jar = jarFromCookies(renderedRequest.cookieJar.cookies);
const domainCookies = jar.getCookiesSync(renderedRequest.url);
return domainCookies.map(c => Object.assign(c, {
name: c.key,
expires: c.expires && c.expires.toISOString ? c.expires.toISOString() : undefined
return domainCookies.map(c => Object.assign(c.toJSON(), {
name: c.key
}));
}
+5 -2
View File
@@ -8,7 +8,6 @@ import {jarFromCookies, cookiesFromJar} from './cookies';
import {setDefaultProtocol} from './util';
import {getRenderedRequest} from './render';
import {swapHost} from './dns';
import {cookieHeaderForUri} from './cookies';
import {cookieHeaderValueForUri} from './cookies';
let cancelRequestFunction = null;
@@ -142,7 +141,11 @@ export function _actuallySend (renderedRequest, settings) {
// Update the cookie jar
// NOTE: Since we're doing own DNS, we can't rely on Request to do this
for (const h of util.getSetCookieHeaders(headers)) {
jar.setCookieSync(h.value, originalUrl);
try {
jar.setCookieSync(h.value, originalUrl);
} catch (e) {
console.warn('Failed to parse set-cookie', h.value);
}
}
const cookies = await cookiesFromJar(jar);
await db.cookieJar.update(cookieJar, {cookies});
+1 -1
View File
@@ -1,7 +1,7 @@
{
"private": true,
"name": "insomnia",
"version": "3.5.8",
"version": "3.5.9",
"productName": "Insomnia",
"longName": "Insomnia REST Client",
"description": "A simple and beautiful REST API client",
@@ -23,10 +23,11 @@ const ResponseCookiesViewer = ({headers, showCookiesModal}) => {
<tbody>
{headers.map((h, i) => {
const cookie = Cookie.parse(h.value);
return (
<tr className="selectable" key={i}>
<td>{cookie.key}</td>
<td className="force-wrap">{cookie.value}</td>
<td>{cookie ? cookie.key : 'n/a'}</td>
<td className="force-wrap">{cookie ? cookie.value : 'malformed set-cookie header'}</td>
</tr>
);
})}