Skip to content

Commit efbb2d3

Browse files
authored
fix: prevent port replacement from corrupting IP addresses (#3035)
the naive string replacement in checkPorts() could corrupt multiaddr IP addresses when the port number appeared in the IP portion. for example, replacing port `0` in `/ip4/127.0.0.1/tcp/0` would match the first `0` in `127.0.0.1`, producing `/ip4/127.5001.0.1/tcp/0`. this caused "invalid ip address" errors on startup when ports were busy. fix by using `/tcp/${port}` pattern to match only the port component. ref: https://discuss.ipfs.tech/t/invalid-ip-address/19925
1 parent bcc1f32 commit efbb2d3

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

src/daemon/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,11 @@ async function checkPorts (ipfsd) {
419419
}
420420

421421
if (busyApiPort) {
422-
config.Addresses.API = config.Addresses.API.replace(apiPort.toString(), freeApiPort.toString())
422+
config.Addresses.API = config.Addresses.API.replace(`/tcp/${apiPort}`, `/tcp/${freeApiPort}`)
423423
}
424424

425425
if (busyGatewayPort) {
426-
config.Addresses.Gateway = config.Addresses.Gateway.replace(gatewayPort.toString(), freeGatewayPort.toString())
426+
config.Addresses.Gateway = config.Addresses.Gateway.replace(`/tcp/${gatewayPort}`, `/tcp/${freeGatewayPort}`)
427427
}
428428

429429
writeConfigFile(ipfsd, config)

0 commit comments

Comments
 (0)