Skip to content

Fix getRootDomain incorrectly truncating IP address literals#63

Merged
ormidales merged 2 commits into1.0.3from
copilot/fix-root-domain-extraction
Feb 28, 2026
Merged

Fix getRootDomain incorrectly truncating IP address literals#63
ormidales merged 2 commits into1.0.3from
copilot/fix-root-domain-extraction

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

getRootDomain splits hostnames on . to extract the root domain, causing IP addresses like 192.168.1.100 to return 1.100 instead of the full address — breaking redirect domain comparison for IP-hosted servers.

Changes

utils.js

  • Moved IPV4_REGEX and IPV6_REGEX here from background.js (loads first per manifest script order, so both files share the same global scope)
  • Added early-return in getRootDomain for IP literals — returns hostname as-is

background.js

  • Removed now-duplicate IPV4_REGEX / IPV6_REGEX definitions
// Before
getRootDomain("192.168.1.100") // → "1.100"

// After
getRootDomain("192.168.1.100") // → "192.168.1.100"
getRootDomain("::1")           // → "::1"
Original prompt

This section details on the original issue you should resolve

<issue_title>Correction de l'extraction du domaine racine pour les adresses IP</issue_title>
<issue_description>## Contexte
L'extension intercepte les requêtes réseau et compare le domaine racine de l'URL initiale avec celui de l'URL de redirection pour déterminer s'il faut bloquer la requête.

Problème

La fonction getRootDomain dans utils.js divise le nom d'hôte en utilisant le point comme séparateur. Si l'utilisateur navigue sur une adresse IP (ex: 192.168.1.100), la fonction retourne 1.100. Cela fausse les comparaisons de domaines si la redirection pointe vers la même adresse IP ou une autre adresse.

Scénario de Reproduction

  1. Naviguer vers un serveur web local accessible via son adresse IP (ex: http://192.168.1.50).
  2. Déclencher une redirection HTTP 301 vers la même adresse IP avec un chemin différent.
  3. Analyser le comportement de la fonction de validation dans le background script.

Comportement Attendu

La fonction getRootDomain doit identifier que le nom d'hôte est une adresse IP et retourner l'adresse complète sans tenter d'extraire un TLD.

Pistes Techniques

  • Fichier concerné : utils.js.
  • Importer les expressions régulières IPV4_REGEX et IPV6_REGEX (déjà présentes dans background.js) dans utils.js.
  • Ajouter une condition en début de la fonction getRootDomain pour retourner le nom d'hôte tel quel s'il correspond à une adresse IP.

Environnement

Toutes les plateformes.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: ormidales <46538211+ormidales@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix root domain extraction for IP addresses Fix getRootDomain incorrectly truncating IP address literals Feb 28, 2026
@ormidales ormidales added this to the v1.0.3 milestone Feb 28, 2026
@ormidales ormidales linked an issue Feb 28, 2026 that may be closed by this pull request
@ormidales ormidales marked this pull request as ready for review February 28, 2026 21:01
Copilot AI review requested due to automatic review settings February 28, 2026 21:01
@ormidales ormidales merged commit a1c606e into 1.0.3 Feb 28, 2026
@ormidales ormidales deleted the copilot/fix-root-domain-extraction branch February 28, 2026 21:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes getRootDomain behavior for IP-literal hostnames so redirect-domain comparisons don’t incorrectly truncate IP addresses (e.g., 192.168.1.1001.100).

Changes:

  • Added IPv4/IPv6 literal detection to utils.js and early-return in getRootDomain for IPs.
  • Moved IPV4_REGEX / IPV6_REGEX definitions from background.js into utils.js to avoid duplication.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
utils.js Adds IP-literal handling to getRootDomain and hosts shared IP regexes.
background.js Removes duplicate IP regex definitions (now relying on utils.js being loaded first).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread utils.js
}

if (IPV4_REGEX.test(hostname) || IPV6_REGEX.test(hostname)) {
return hostname;
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRootDomain is documented as returning a normalized root domain, but for IP literals it now returns hostname as-is. This can lead to comparison mismatches (e.g., uppercase hex in IPv6, or bracketed IPv6 like [::1]) versus the lowercased/normalized values used elsewhere. Consider normalizing IP inputs too (lowercasing and stripping optional [/]) before returning.

Suggested change
return hostname;
const normalized = hostname.toLowerCase().replace(/^\[/, "").replace(/\]$/, "");
return normalized;

Copilot uses AI. Check for mistakes.
Comment thread background.js
Comment on lines 37 to 41
["uk", "en-GB,en;q=0.9"],
["za", "en-ZA,en;q=0.9"]
]);
const IPV4_REGEX = /^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$/;
const IPV6_REGEX =
/^\[?(?:(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}|(?:[a-f0-9]{1,4}:){1,7}:|(?:[a-f0-9]{1,4}:){1,6}:[a-f0-9]{1,4}|(?:[a-f0-9]{1,4}:){1,5}(?::[a-f0-9]{1,4}){1,2}|(?:[a-f0-9]{1,4}:){1,4}(?::[a-f0-9]{1,4}){1,3}|(?:[a-f0-9]{1,4}:){1,3}(?::[a-f0-9]{1,4}){1,4}|(?:[a-f0-9]{1,4}:){1,2}(?::[a-f0-9]{1,4}){1,5}|[a-f0-9]{1,4}:(?:(?::[a-f0-9]{1,4}){1,6})|:(?:(?::[a-f0-9]{1,4}){1,7}|:))\]?$/i;

const readLocationHeader = (headers = []) => {
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

background.js still uses IPV4_REGEX/IPV6_REGEX (e.g. in buildAcceptLanguage) but the definitions are now in utils.js, creating an implicit cross-file global dependency on manifest script order. To prevent accidental breakage if the load order changes, consider adding a short comment here noting the dependency, or centralizing the shared constants under an explicit shared namespace (e.g., globalThis.TRUEPATH_IPV4_REGEX).

Copilot uses AI. Check for mistakes.
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.

Correction de l'extraction du domaine racine pour les adresses IP

3 participants