Skip to content

Commit

Permalink
feat: Network Intercept: handle special schemes (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Perrotta authored Aug 29, 2023
1 parent 60071ea commit 27c6ccb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/bidiMapper/domains/network/NetworkProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export class NetworkProcessor {
switch (urlPattern.type) {
case 'string': {
try {
// XXX: Switch to URLPattern.
// Note: This would require polyfilling as URLPattern is not yet in Node.
// https://github.com/nodejs/node/issues/40844
new URL(urlPattern.pattern);
} catch (error) {
throw new InvalidArgumentException(
Expand Down
10 changes: 10 additions & 0 deletions src/bidiMapper/domains/network/NetworkStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,14 @@ describe('NetworkStorage', () => {
});
});
});

describe('isSpecialScheme', () => {
it('http', () => {
expect(NetworkStorage.isSpecialScheme('http')).to.be.true;
});

it('sftp', () => {
expect(NetworkStorage.isSpecialScheme('sftp')).to.be.false;
});
});
});
18 changes: 17 additions & 1 deletion src/bidiMapper/domains/network/NetworkStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ export class NetworkStorage {
url += ':';
}

url += '//';
if (NetworkStorage.isSpecialScheme(protocol)) {
url += '//';
}
}

if (hostname) {
Expand Down Expand Up @@ -218,6 +220,20 @@ export class NetworkStorage {
}
}

/**
* Returns true if the given protocol is special.
* Special protocols are those that have a default port.
*
* Example inputs: 'http', 'http:'
*
* @see https://url.spec.whatwg.org/#special-scheme
*/
static isSpecialScheme(protocol: string): boolean {
return ['ftp', 'file', 'http', 'https', 'ws', 'wss'].includes(
protocol.replace(/:$/, '')
);
}

// XXX: Replace getters with custom operations, follow suit of Browsing Context Storage.
get blockedRequestMap() {
return this.#blockedRequestMap;
Expand Down
22 changes: 22 additions & 0 deletions tests/network/test_add_intercept.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ async def test_add_intercept_type_pattern_protocol_empty_invalid(websocket):
} == exception_info.value.args[0]


@pytest.mark.asyncio
async def test_add_intercept_type_pattern_protocol_non_special_success(
websocket):
result = await execute_command(
websocket, {
"method": "network.addIntercept",
"params": {
"phases": ["beforeRequestSent"],
"urlPatterns": [{
"type": "pattern",
"protocol": "sftp",
"hostname": "www.example.com",
"port": "22",
}],
},
})

assert result == {
"intercept": ANY_UUID,
}


@pytest.mark.asyncio
async def test_add_intercept_type_pattern_hostname_empty_invalid(websocket):
with pytest.raises(Exception) as exception_info:
Expand Down

0 comments on commit 27c6ccb

Please sign in to comment.