File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * Ports blocked by the WHATWG Fetch spec for HTTP/HTTPS requests.
3+ *
4+ * See: https://fetch.spec.whatwg.org/#port-blocking
5+ */
6+ export const BAD_PORTS : string [ ] = [
7+ '1' ,
8+ '7' ,
9+ '9' ,
10+ '11' ,
11+ '13' ,
12+ '15' ,
13+ '17' ,
14+ '19' ,
15+ '20' ,
16+ '21' ,
17+ '22' ,
18+ '23' ,
19+ '25' ,
20+ '37' ,
21+ '42' ,
22+ '43' ,
23+ '53' ,
24+ '69' ,
25+ '77' ,
26+ '79' ,
27+ '87' ,
28+ '95' ,
29+ '101' ,
30+ '102' ,
31+ '103' ,
32+ '104' ,
33+ '109' ,
34+ '110' ,
35+ '111' ,
36+ '113' ,
37+ '115' ,
38+ '117' ,
39+ '119' ,
40+ '123' ,
41+ '135' ,
42+ '137' ,
43+ '139' ,
44+ '143' ,
45+ '161' ,
46+ '179' ,
47+ '389' ,
48+ '427' ,
49+ '465' ,
50+ '512' ,
51+ '513' ,
52+ '514' ,
53+ '515' ,
54+ '526' ,
55+ '530' ,
56+ '531' ,
57+ '532' ,
58+ '540' ,
59+ '548' ,
60+ '554' ,
61+ '556' ,
62+ '563' ,
63+ '587' ,
64+ '601' ,
65+ '636' ,
66+ '989' ,
67+ '990' ,
68+ '993' ,
69+ '995' ,
70+ '1719' ,
71+ '1720' ,
72+ '1723' ,
73+ '2049' ,
74+ '3659' ,
75+ '4045' ,
76+ '4190' ,
77+ '5060' ,
78+ '5061' ,
79+ '6000' ,
80+ '6566' ,
81+ '6665' ,
82+ '6666' ,
83+ '6667' ,
84+ '6668' ,
85+ '6669' ,
86+ '6679' ,
87+ '6697' ,
88+ '10080' ,
89+ ]
Original file line number Diff line number Diff line change 1+ import { BAD_PORTS } from './bad-ports'
12import { BeeArgumentError } from './error'
23
34/**
@@ -21,6 +22,29 @@ export function isValidBeeUrl(url: unknown): url is URL {
2122 }
2223}
2324
25+ /**
26+ * Validates that passed string is valid port to use Bee and is not a bad port.
27+ * We support only HTTP and HTTPS protocols. Bad ports are defined in
28+ * the WHATWG Fetch spec (https://fetch.spec.whatwg.org/#port-blocking)
29+ * and are blocked for HTTP/HTTPS requests.
30+ *
31+ * @param port
32+ */
33+
34+ export function isValidBeePort ( port : unknown ) : port is string {
35+ if ( typeof port !== 'string' ) {
36+ return false
37+ }
38+
39+ if ( BAD_PORTS . includes ( port ) ) {
40+ return false
41+ }
42+
43+ const portNumber = parseInt ( port , 10 )
44+
45+ return portNumber > 0 && portNumber < 65536 && ! isNaN ( portNumber )
46+ }
47+
2448/**
2549 * Validates that passed string is valid URL of Bee, if not it throws BeeArgumentError.
2650 * We support only HTTP and HTTPS protocols.
@@ -31,6 +55,12 @@ export function assertBeeUrl(url: unknown): asserts url is URL {
3155 if ( ! isValidBeeUrl ( url ) ) {
3256 throw new BeeArgumentError ( 'URL is not valid!' , url )
3357 }
58+
59+ const port = new URL ( url as unknown as string ) . port
60+
61+ if ( port && ! isValidBeePort ( port ) ) {
62+ throw new BeeArgumentError ( 'Port in URL is considered bad port and cannot be used!' , port )
63+ }
3464}
3565
3666/**
Original file line number Diff line number Diff line change 1+ import { Bee } from '../../src'
2+ import { assertBeeUrl } from '../../src/utils/url'
3+
4+ describe ( 'URL utils' , ( ) => {
5+ test ( 'when URL contains bad port, assertBeeUrl throws' , ( ) => {
6+ expect ( ( ) => assertBeeUrl ( 'http://localhost:25' ) ) . toThrow ( 'Port in URL is considered bad port and cannot be used!' )
7+ expect ( ( ) => assertBeeUrl ( 'http://localhost:69' ) ) . toThrow ( 'Port in URL is considered bad port and cannot be used!' )
8+ } )
9+
10+ test ( 'Bee constructor rejects bad ports' , ( ) => {
11+ expect ( ( ) => new Bee ( 'http://localhost:25' ) ) . toThrow ( 'Port in URL is considered bad port and cannot be used!' )
12+ expect ( ( ) => new Bee ( 'http://localhost:69' ) ) . toThrow ( 'Port in URL is considered bad port and cannot be used!' )
13+ } )
14+ } )
You can’t perform that action at this time.
0 commit comments