Skip to content

Commit 0386f33

Browse files
stramelJs-Brecht
andauthored
Fix valid domain regex (#57)
* Fix valid domain regex Based on https://gist.github.com/dperini/729294 Stripped the following checks: - protocol (http, ftp) - short syntax (//) - basic auth (user:pass) - requirement for TLD (\. made optional) - port number (:port) - resource path (/path, #resource-path) * Revert Regex to just support localhost * Prevent matching only a few octets of the ip https://regexr.com/56tso * Add ipv4 regex * Add error for ip addresses * Add missing } Co-authored-by: Jeremy Albright <1935258+Js-Brecht@users.noreply.github.com> Co-authored-by: Jeremy Albright <1935258+Js-Brecht@users.noreply.github.com>
1 parent e138968 commit 0386f33

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/constants.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import applicationConfigPath = require('application-config-path');
66
import eol from 'eol';
77
import { mktmp } from './utils';
88

9-
export const VALID_DOMAIN = /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/;
9+
export const VALID_IP = /(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}/;
10+
export const VALID_DOMAIN = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.?)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i;
1011

1112
// Platform shortcuts
1213
export const isMac = process.platform === 'darwin';
@@ -77,4 +78,4 @@ export function ensureConfigDirs() {
7778
mkdirp(rootCADir);
7879
}
7980

80-
ensureConfigDirs();
81+
ensureConfigDirs();

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
domainsDir,
1111
rootCAKeyPath,
1212
rootCACertPath,
13-
VALID_DOMAIN
13+
VALID_DOMAIN,
14+
VALID_IP
1415
} from './constants';
1516
import currentPlatform from './platforms';
1617
import installCertificateAuthority, { ensureCACertReadable, uninstall } from './certificate-authority';
@@ -66,6 +67,9 @@ type IReturnData<O extends Options = {}> = (IDomainData) & (IReturnCa<O>) & (IRe
6667
* as { caPath: string }
6768
*/
6869
export async function certificateFor<O extends Options>(domain: string, options: O = {} as O): Promise<IReturnData<O>> {
70+
if (VALID_IP.test(domain)) {
71+
throw new Error('IP addresses are not supported currently');
72+
}
6973
if (!VALID_DOMAIN.test(domain)) {
7074
throw new Error(`"${domain}" is not a valid domain name.`);
7175
}

0 commit comments

Comments
 (0)