mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Support the Forwarded header.
This commit is contained in:
committed by
Joachim Van Herwegen
parent
3362eee2c2
commit
ecfe3cfc46
@@ -6,15 +6,16 @@ import { instantiateFromConfig } from '../configs/Util';
|
||||
|
||||
const port = 6001;
|
||||
const serverUrl = `http://localhost:${port}/`;
|
||||
const headers = { forwarded: 'host=example.pod;proto=https' };
|
||||
|
||||
describe('A server with the Solid WebSockets API', (): void => {
|
||||
describe('A server with the Solid WebSockets API behind a proxy', (): void => {
|
||||
let server: Server;
|
||||
|
||||
beforeAll(async(): Promise<void> => {
|
||||
const factory = await instantiateFromConfig(
|
||||
'urn:solid-server:default:ServerFactory', 'websockets.json', {
|
||||
'urn:solid-server:default:variable:port': port,
|
||||
'urn:solid-server:default:variable:baseUrl': 'http://example.pod/',
|
||||
'urn:solid-server:default:variable:baseUrl': 'https://example.pod/',
|
||||
},
|
||||
) as HttpServerFactory;
|
||||
server = factory.startServer(port);
|
||||
@@ -27,17 +28,17 @@ describe('A server with the Solid WebSockets API', (): void => {
|
||||
});
|
||||
|
||||
it('returns a 200.', async(): Promise<void> => {
|
||||
const response = await fetch(serverUrl, { headers: { host: 'example.pod' }});
|
||||
const response = await fetch(serverUrl, { headers });
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
it('sets the Updates-Via header.', async(): Promise<void> => {
|
||||
const response = await fetch(serverUrl, { headers: { host: 'example.pod' }});
|
||||
expect(response.headers.get('Updates-Via')).toBe('ws://example.pod/');
|
||||
const response = await fetch(serverUrl, { headers });
|
||||
expect(response.headers.get('Updates-Via')).toBe('wss://example.pod/');
|
||||
});
|
||||
|
||||
it('exposes the Updates-Via header via CORS.', async(): Promise<void> => {
|
||||
const response = await fetch(serverUrl, { headers: { host: 'example.pod' }});
|
||||
const response = await fetch(serverUrl, { headers });
|
||||
expect(response.headers.get('Access-Control-Expose-Headers')!.split(','))
|
||||
.toContain('Updates-Via');
|
||||
});
|
||||
@@ -47,7 +48,7 @@ describe('A server with the Solid WebSockets API', (): void => {
|
||||
const messages = new Array<string>();
|
||||
|
||||
beforeAll(async(): Promise<void> => {
|
||||
client = new WebSocket(`ws://localhost:${port}`, [ 'solid/0.1.0-alpha' ], { headers: { host: 'example.pod' }});
|
||||
client = new WebSocket(`ws://localhost:${port}`, [ 'solid/0.1.0-alpha' ], { headers });
|
||||
client.on('message', (message: string): any => messages.push(message));
|
||||
await new Promise((resolve): any => client.on('open', resolve));
|
||||
});
|
||||
@@ -69,24 +70,24 @@ describe('A server with the Solid WebSockets API', (): void => {
|
||||
|
||||
describe('when the client subscribes to a resource', (): void => {
|
||||
beforeAll(async(): Promise<void> => {
|
||||
client.send(`sub http://example.pod/my-resource`);
|
||||
client.send(`sub https://example.pod/my-resource`);
|
||||
await new Promise((resolve): any => client.once('message', resolve));
|
||||
});
|
||||
|
||||
it('acknowledges the subscription.', async(): Promise<void> => {
|
||||
expect(messages).toEqual([ `ack http://example.pod/my-resource` ]);
|
||||
expect(messages).toEqual([ `ack https://example.pod/my-resource` ]);
|
||||
});
|
||||
|
||||
it('notifies the client of resource updates.', async(): Promise<void> => {
|
||||
await fetch(`${serverUrl}my-resource`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
host: 'example.pod',
|
||||
...headers,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: '{}',
|
||||
});
|
||||
expect(messages).toEqual([ `pub http://example.pod/my-resource` ]);
|
||||
expect(messages).toEqual([ `pub https://example.pod/my-resource` ]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user