Skip to content

Commit

Permalink
fix: enclose ipv6 host with brackets on client request (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 authored Nov 24, 2023
1 parent cab0fe5 commit f5356ea
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
uriEscape,
uriResourceEscape,
} from './helper.ts'
import { joinHostPort } from './join-host-port.ts'
import { request } from './request.ts'
import { drainResponse, readAsBuffer, readAsString } from './response.ts'
import type { Region } from './s3-endpoints.ts'
Expand Down Expand Up @@ -417,8 +418,9 @@ export class TypedClient {
}
reqOptions.headers.host = host
if ((reqOptions.protocol === 'http:' && port !== 80) || (reqOptions.protocol === 'https:' && port !== 443)) {
reqOptions.headers.host = `${host}:${port}`
reqOptions.headers.host = joinHostPort(host, port)
}

reqOptions.headers['user-agent'] = this.userAgent
if (headers) {
// have all header keys in lower case - to make signing easy
Expand Down
23 changes: 23 additions & 0 deletions src/internal/join-host-port.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* joinHostPort combines host and port into a network address of the
* form "host:port". If host contains a colon, as found in literal
* IPv6 addresses, then JoinHostPort returns "[host]:port".
*
* @param host
* @param port
* @returns Cleaned up host
* @internal
*/
export function joinHostPort(host: string, port?: number): string {
if (port === undefined) {
return host
}

// We assume that host is a literal IPv6 address if host has
// colons.
if (host.includes(':')) {
return `[${host}]:${port.toString()}`
}

return `${host}:${port.toString()}`
}
32 changes: 32 additions & 0 deletions tests/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
makeDateShort,
partsRequired,
} from '../../src/internal/helper.ts'
import { joinHostPort } from '../../src/internal/join-host-port.ts'
import * as Minio from '../../src/minio.js'
import { parseListObjects } from '../../src/xml-parsers.js'

Expand Down Expand Up @@ -2254,3 +2255,34 @@ describe('xml-parser', () => {
})
})
})

describe('join-host-port', () => {
it('should be able to parse valid ipv4', () => {
assert.equal(joinHostPort('192.168.1.1', 3000), '192.168.1.1:3000')
assert.equal(joinHostPort('192.168.1.1'), '192.168.1.1')
assert.equal(joinHostPort('01.102.103.104', 3000), '01.102.103.104:3000')
})

it('should be able to parse valid ipv6', () => {
assert.equal(
joinHostPort('2001:db8:3333:4444:5555:6666:7777:8888', 1234),
'[2001:db8:3333:4444:5555:6666:7777:8888]:1234',
)
assert.equal(joinHostPort('::', 1234), '[::]:1234')
assert.equal(joinHostPort('2001:db8::', 1234), '[2001:db8::]:1234')
assert.equal(joinHostPort('::1234:5678', 1234), '[::1234:5678]:1234')
assert.equal(
joinHostPort('2001:0db8:0001:0000:0000:0ab9:C0A8:0102', 1234),
'[2001:0db8:0001:0000:0000:0ab9:C0A8:0102]:1234',
)
assert.equal(
joinHostPort('2001:db8:3333:4444:5555:6666:1.2.3.4', 1234),
'[2001:db8:3333:4444:5555:6666:1.2.3.4]:1234',
)
})

it('should be able to parse domain', () => {
assert.equal(joinHostPort('internal.company.com', 5000), 'internal.company.com:5000')
assert.equal(joinHostPort('internal.company.com'), 'internal.company.com')
})
})

0 comments on commit f5356ea

Please sign in to comment.