Skip to content

Commit

Permalink
Fix double-encoding of URLs (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
ltm committed May 16, 2020
1 parent d542970 commit 48b6d0e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -33,7 +33,7 @@ declare namespace open {
readonly app?: string | readonly string[];

/**
Uses `encodeURI` to encode the `target` before executing it.
Uses `URL` to encode the `target` before executing it.
The use with targets that are not URLs is not recommended.
Expand Down
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -3,6 +3,7 @@ const {promisify} = require('util');
const path = require('path');
const childProcess = require('child_process');
const fs = require('fs');
const url = require('url');
const isWsl = require('is-wsl');
const isDocker = require('is-docker');

Expand Down Expand Up @@ -45,7 +46,7 @@ module.exports = async (target, options) => {
// double-quotes through the “double-quotes on Windows caveat”, but it
// can be used on any platform.
if (options.url) {
target = encodeURI(target);
target = new url.URL(target).href;

if (isWsl) {
target = target.replace(/&/g, '^&');
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -97,7 +97,7 @@ You may also pass in the app's full path. For example on WSL, this can be `/mnt/
Type: `boolean`\
Default: `false`

Uses `encodeURI` to encode the target before executing it.<br>
Uses `URL` to encode the target before executing it.<br>
We do not recommend using it on targets that are not URLs.

Especially useful when dealing with the [double-quotes on Windows](#double-quotes-on-windows) caveat.
Expand Down
8 changes: 8 additions & 0 deletions test.js
Expand Up @@ -78,6 +78,14 @@ test('open URL with query strings, spaces, pipes and a fragment', async () => {
await open('https://sindresorhus.com/?abc=123&def=456&ghi=w|i|t|h spaces#projects');
});

test('open URL with query strings and URL reserved characters', async () => {
await open('https://httpbin.org/get?amp=%26&colon=%3A&comma=%2C&commat=%40&dollar=%24&equals=%3D&plus=%2B&quest=%3F&semi=%3B&sol=%2F');
});

test('open URL with query strings and URL reserved characters with `url` option', async () => {
await open('https://httpbin.org/get?amp=%26&colon=%3A&comma=%2C&commat=%40&dollar=%24&equals=%3D&plus=%2B&quest=%3F&semi=%3B&sol=%2F', {url: true});
});

if (isWsl) {
test('open URL in specified Windows app given a WSL path to the app', async () => {
await open('https://sindresorhus.com', {app: firefoxWslName});
Expand Down

0 comments on commit 48b6d0e

Please sign in to comment.