Skip to content

Fix UpdatePort corrupting the host when the port digits appear inside the host/IP#752

Merged
Mzack9999 merged 1 commit into
projectdiscovery:mainfrom
SergoRoberto:fix-bug-with-port-update
Jun 22, 2026
Merged

Fix UpdatePort corrupting the host when the port digits appear inside the host/IP#752
Mzack9999 merged 1 commit into
projectdiscovery:mainfrom
SergoRoberto:fix-bug-with-port-update

Conversation

@SergoRoberto

@SergoRoberto SergoRoberto commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Closes #753

Fix UpdatePort corrupting the host when the port digits appear inside the host/IP

Problem

URL.UpdatePort replaced the port via strings.Replace(u.Host, u.Port(), newport, 1),
which swaps the first occurrence of the port substring in the host — not the actual
port. When the port digits also appear inside the IP/hostname, the wrong part gets rewritten.

Examples:

  • http://37.228.93.80:80/ + UpdatePort("443")http://37.228.93.443:80/
  • http://[2001:db8::80]:80/ + UpdatePort("8080")http://[2001:db8::8080]:80/

Fix

Build the host with net.JoinHostPort(u.Hostname(), newport). Hostname() strips the
existing port and unwraps IPv6 brackets, and JoinHostPort re-wraps IPv6 literals
correctly — so the port is always set on the right segment.

Tests

Added TestPortUpdateWhenPortMatchesHost covering IPv4 and IPv6 cases that previously
produced a corrupted host.

@SergoRoberto SergoRoberto changed the title Fix a bug with a port update Fix UpdatePort corrupting the host when the port digits appear inside the host/IP Jun 22, 2026
@dogancanbakir dogancanbakir requested a review from Mzack9999 June 22, 2026 12:58
@dogancanbakir dogancanbakir self-assigned this Jun 22, 2026
@Mzack9999 Mzack9999 merged commit c3dfebe into projectdiscovery:main Jun 22, 2026
7 checks passed
@SergoRoberto SergoRoberto deleted the fix-bug-with-port-update branch June 23, 2026 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UpdatePort corrupts the host when port digits appear inside the host/IP

3 participants