Make the add-to-hosts-file regex stricter to avoid incorrect matches#31
Open
andrew-nowak wants to merge 1 commit intomainfrom
Open
Make the add-to-hosts-file regex stricter to avoid incorrect matches#31andrew-nowak wants to merge 1 commit intomainfrom
andrew-nowak wants to merge 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in the add-to-hosts-file script where the grep regex was too permissive, causing false matches when checking for existing hosts file entries. The issue occurred when a domain was a substring of an already existing domain (e.g., media.local.dev-gutools.co.uk appearing to exist when only api.media.local.dev-gutools.co.uk was present).
- Replaces the naive grep pattern with a stricter extended regex that matches the complete line format
- Uses anchors and whitespace patterns to ensure exact domain matching rather than substring matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this change?
There's a grep call in add-to-hosts-file which aims to prevent adding duplicate entries. Unfortunately the regex is naive so will match a domain even if it doesn't exist and is only a substring of an existing domain. For example, if we've already added
api.media.local.dev-gutools.co.uk, then the command will not allow us to addmedia.local.dev-gutools.co.ukas that domain appears to already exist.Improve the match with grep's extended regex to match the entire line, from IP address, delimiting whitespace to domain name and end-of-line marker. Now dev-nginx should correctly deduplicate entries.
How to test
Run
add-to-hosts-file api.media.local.dev-gutools.co.ukthenadd-to-hosts-file media.local.dev-gutools.co.uk- you should not get a message that the entry already exists. (remember to clean up your /etc/hosts file afterwards!)