Skip to content

Commit

Permalink
fix: percent encode plus signs in path components (#223)
Browse files Browse the repository at this point in the history
While testing encoding behavior across the SDKs I found a failure to
encode this path: `"&$+,:;=?@#.jpg"`. Prior to this PR our encoding step
didn't replace plus signs (+). Now we replace plus signs with `%2B`.

To see that the original encoding step fails, replace the domain of this
URL `'https://test.imgix.net/&$+,%3A;=%3F@%23.jpg'` to see that it 404s.

Likewise, you can replace the domain of the this correctly encoded URL:
`'https://test.imgix.net/&$%2B,%3A;=%3F@%23.jpg'` to see that it 200s
(and the image is displayed).

For reference see [RF3986 - Appendix B](https://tools.ietf.org/html/rfc3986#appendix-B).
  • Loading branch information
ericdeansanchez authored Feb 2, 2021
1 parent 3aa5bab commit a5d756e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default class ImgixClient {
} else {
// Use de/encodeURI if we think the path is just a path,
// so it leaves legal characters like '/' and '@' alone
_path = encodeURI(_path).replace(/[#?:]/g, encodeURIComponent);
_path = encodeURI(_path).replace(/[#?:+]/g, encodeURIComponent);
}

return '/' + _path;
Expand Down
7 changes: 7 additions & 0 deletions test/test-buildURL.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,12 @@ describe('URL Builder:', function describeSuite() {
assert.strictEqual(Object.keys(params).length, 1);
assert.strictEqual(params.constructor, Object);
});

it('correctly encodes plus signs (+) in paths', function testSpec() {
const actual = client.buildURL('&$+,:;=?@#.jpg', {});
const expected = 'https://test.imgix.net/&$%2B,%3A;=%3F@%23.jpg';

assert.strictEqual(actual, expected);
});
});
});

0 comments on commit a5d756e

Please sign in to comment.