Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve checking entries in the hostfile #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/platforms/darwin.ts
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import { writeFileSync as writeFile, existsSync as exists, readFileSync as read } from 'fs';
import createDebug from 'debug';
import { sync as commandExists } from 'command-exists';
import { run } from '../utils';
import { isDomainInHostFile, run } from '../utils';
import { Options } from '../index';
import { addCertificateToNSSCertDB, openCertificateInFirefox, closeFirefox } from './shared';
import { Platform } from '.';
Expand Down Expand Up @@ -58,7 +58,7 @@ export default class MacOSPlatform implements Platform {

async addDomainToHostFileIfMissing(domain: string) {
let hostsFileContents = read(this.HOST_FILE_PATH, 'utf8');
if (!hostsFileContents.includes(domain)) {
if (!isDomainInHostFile(hostsFileContents, domain)) {
run(`echo '\n127.0.0.1 ${ domain }' | sudo tee -a "${ this.HOST_FILE_PATH }" > /dev/null`);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/linux.ts
Expand Up @@ -3,7 +3,7 @@ import { existsSync as exists, readFileSync as read, writeFileSync as writeFile
import createDebug from 'debug';
import { sync as commandExists } from 'command-exists';
import { addCertificateToNSSCertDB, openCertificateInFirefox, closeFirefox } from './shared';
import { run } from '../utils';
import { isDomainInHostFile, run } from '../utils';
import { Options } from '../index';
import UI from '../user-interface';
import { Platform } from '.';
Expand Down Expand Up @@ -70,7 +70,7 @@ export default class LinuxPlatform implements Platform {

async addDomainToHostFileIfMissing(domain: string) {
let hostsFileContents = read(this.HOST_FILE_PATH, 'utf8');
if (!hostsFileContents.includes(domain)) {
if (!isDomainInHostFile(hostsFileContents, domain)) {
run(`echo '127.0.0.1 ${ domain }' | sudo tee -a "${ this.HOST_FILE_PATH }" > /dev/null`);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/win32.ts
Expand Up @@ -4,7 +4,7 @@ import { writeFileSync as write, readFileSync as read } from 'fs';
import { Options } from '../index';
import { openCertificateInFirefox } from './shared';
import { Platform } from '.';
import { run, sudo } from '../utils';
import { isDomainInHostFile, run, sudo, } from '../utils';
import UI from '../user-interface';

const debug = createDebug('devcert:platforms:windows');
Expand Down Expand Up @@ -46,7 +46,7 @@ export default class WindowsPlatform implements Platform {

async addDomainToHostFileIfMissing(domain: string) {
let hostsFileContents = read(this.HOST_FILE_PATH, 'utf8');
if (!hostsFileContents.includes(domain)) {
if (!isDomainInHostFile(hostsFileContents, domain)) {
await sudo(`echo 127.0.0.1 ${ domain } > ${ this.HOST_FILE_PATH }`);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Expand Up @@ -49,3 +49,13 @@ export function sudo(cmd: string): Promise<string | null> {
});
});
}

export function isDomainInHostFile(hostFileContents: string, domain: string): boolean {
// Do a check for a full match since a string includes can be fooled by
// a subdomain being present in the host file.
const isPresent = hostFileContents
.replace(/\s+/g, " ")
.split(" ")
.filter(item => item === domain).length > 0;
return isPresent;
}
Comment on lines +53 to +61
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like pretty solid code, but I'm not 100% sure it'll work for all possible hostfile contents, such as tab characters, different encodings, or comments. Maybe it would be easier to bring in a tool like https://www.npmjs.com/package/hostile which has a well-tested hostfile parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a pretty quick and dirty way of improving the checking. Your are right that can be fooled by domains in comments, which the previous code had a problem with as well. I'll take a look hostile and see what that looks like to bring in.