feat: Support the Forwarded header.

This commit is contained in:
Ruben Verborgh
2020-12-01 21:24:43 +01:00
committed by Joachim Van Herwegen
parent 3362eee2c2
commit ecfe3cfc46
7 changed files with 162 additions and 53 deletions

View File

@@ -120,52 +120,67 @@ describe('An UnsecureWebSocketsProtocol', (): void => {
});
it('unsubscribes when a socket closes.', async(): Promise<void> => {
const newSocket = new DummySocket();
await protocol.handle({ webSocket: newSocket, upgradeRequest: { headers: {}, socket: {}}} as any);
expect(newSocket.listenerCount('message')).toBe(1);
newSocket.emit('close');
expect(newSocket.listenerCount('message')).toBe(0);
expect(newSocket.listenerCount('close')).toBe(0);
expect(newSocket.listenerCount('error')).toBe(0);
const webSocket = new DummySocket();
await protocol.handle({ webSocket, upgradeRequest: { headers: {}, socket: {}}} as any);
expect(webSocket.listenerCount('message')).toBe(1);
webSocket.emit('close');
expect(webSocket.listenerCount('message')).toBe(0);
expect(webSocket.listenerCount('close')).toBe(0);
expect(webSocket.listenerCount('error')).toBe(0);
});
it('unsubscribes when a socket errors.', async(): Promise<void> => {
const newSocket = new DummySocket();
await protocol.handle({ webSocket: newSocket, upgradeRequest: { headers: {}, socket: {}}} as any);
expect(newSocket.listenerCount('message')).toBe(1);
newSocket.emit('error');
expect(newSocket.listenerCount('message')).toBe(0);
expect(newSocket.listenerCount('close')).toBe(0);
expect(newSocket.listenerCount('error')).toBe(0);
const webSocket = new DummySocket();
await protocol.handle({ webSocket, upgradeRequest: { headers: {}, socket: {}}} as any);
expect(webSocket.listenerCount('message')).toBe(1);
webSocket.emit('error');
expect(webSocket.listenerCount('message')).toBe(0);
expect(webSocket.listenerCount('close')).toBe(0);
expect(webSocket.listenerCount('error')).toBe(0);
});
it('emits a warning when no Sec-WebSocket-Protocol is supplied.', async(): Promise<void> => {
const newSocket = new DummySocket();
const webSocket = new DummySocket();
const upgradeRequest = {
headers: {},
socket: {},
} as any as HttpRequest;
await protocol.handle({ webSocket: newSocket, upgradeRequest } as any);
expect(newSocket.messages).toHaveLength(3);
expect(newSocket.messages.pop())
await protocol.handle({ webSocket, upgradeRequest } as any);
expect(webSocket.messages).toHaveLength(3);
expect(webSocket.messages.pop())
.toBe('warning Missing Sec-WebSocket-Protocol header, expected value \'solid/0.1.0-alpha\'');
expect(newSocket.close).toHaveBeenCalledTimes(0);
expect(webSocket.close).toHaveBeenCalledTimes(0);
});
it('emits an error and closes the connection with the wrong Sec-WebSocket-Protocol.', async(): Promise<void> => {
const newSocket = new DummySocket();
const webSocket = new DummySocket();
const upgradeRequest = {
headers: {
'sec-websocket-protocol': 'solid/1.0.0, other',
},
socket: {},
} as any as HttpRequest;
await protocol.handle({ webSocket: newSocket, upgradeRequest } as any);
expect(newSocket.messages).toHaveLength(3);
expect(newSocket.messages.pop()).toBe('error Client does not support protocol solid/0.1.0-alpha');
expect(newSocket.close).toHaveBeenCalledTimes(1);
expect(newSocket.listenerCount('message')).toBe(0);
expect(newSocket.listenerCount('close')).toBe(0);
expect(newSocket.listenerCount('error')).toBe(0);
await protocol.handle({ webSocket, upgradeRequest } as any);
expect(webSocket.messages).toHaveLength(3);
expect(webSocket.messages.pop()).toBe('error Client does not support protocol solid/0.1.0-alpha');
expect(webSocket.close).toHaveBeenCalledTimes(1);
expect(webSocket.listenerCount('message')).toBe(0);
expect(webSocket.listenerCount('close')).toBe(0);
expect(webSocket.listenerCount('error')).toBe(0);
});
it('respects the Forwarded header.', async(): Promise<void> => {
const webSocket = new DummySocket();
const upgradeRequest = {
headers: {
forwarded: 'proto=https;host=other.example',
'sec-websocket-protocol': 'solid/0.1.0-alpha',
},
socket: {},
} as any as HttpRequest;
await protocol.handle({ webSocket, upgradeRequest } as any);
webSocket.emit('message', 'sub https://other.example/protocol/foo');
expect(webSocket.messages).toHaveLength(3);
expect(webSocket.messages.pop()).toBe('ack https://other.example/protocol/foo');
});
});

View File

@@ -51,4 +51,22 @@ describe('A BasicTargetExtractor', (): void => {
await expect(extractor.handle({ url: '/', headers: { host: '點看' }} as any))
.resolves.toEqual({ path: 'http://xn--c1yn36f/' });
});
it('ignores an irrelevant Forwarded header.', async(): Promise<void> => {
const headers = {
host: 'test.com',
forwarded: 'by=203.0.113.60',
};
await expect(extractor.handle({ url: '/foo/bar', headers } as any))
.resolves.toEqual({ path: 'http://test.com/foo/bar' });
});
it('takes the Forwarded header into account.', async(): Promise<void> => {
const headers = {
host: 'test.com',
forwarded: 'proto=https;host=pod.example',
};
await expect(extractor.handle({ url: '/foo/bar', headers } as any))
.resolves.toEqual({ path: 'https://pod.example/foo/bar' });
});
});

View File

@@ -5,6 +5,7 @@ import {
parseAcceptCharset,
parseAcceptEncoding,
parseAcceptLanguage,
parseForwarded,
} from '../../../src/util/HeaderUtil';
describe('HeaderUtil', (): void => {
@@ -166,4 +167,35 @@ describe('HeaderUtil', (): void => {
expect(response.getHeader('names')).toEqual([ 'oldValue1', 'oldValue2', 'value1', 'values2' ]);
});
});
describe('parseForwarded', (): void => {
it('parses an undefined value.', (): void => {
expect(parseForwarded()).toEqual({});
});
it('parses an empty string.', (): void => {
expect(parseForwarded('')).toEqual({});
});
it('parses a Forwarded header value.', (): void => {
expect(parseForwarded('for=192.0.2.60;proto=http;by=203.0.113.43;host=example.org')).toEqual({
by: '203.0.113.43',
for: '192.0.2.60',
host: 'example.org',
proto: 'http',
});
});
it('skips empty fields.', (): void => {
expect(parseForwarded('for=192.0.2.60;proto=;by=;host=')).toEqual({
for: '192.0.2.60',
});
});
it('takes only the first value into account.', (): void => {
expect(parseForwarded('host=pod.example, for=192.0.2.43, host=other')).toEqual({
host: 'pod.example',
});
});
});
});