Skip to content

Commit 33aaaef

Browse files
committed
Fix urlencoding behavior for imgix
1 parent 68ff35c commit 33aaaef

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## Unreleased
7+
## [2.0.0] - 2019-12-09
8+
9+
### Changed
10+
- A breaking change has been introduced in the way URLs are generated. Previously we were calling `encodeURIComponent`
11+
on each part of the requested URL, we are now calling a modified `encodeURIComponent` that excludes some characters to
12+
be compliant with Imgix's official php SDK:
13+
```php
14+
$path = preg_replace_callback("/([^\w\-\/\:@])/", function ($match) {
15+
return rawurlencode($match[0]);
16+
}, $path);
17+
```
18+
19+
See https://locutus.io/php/url/rawurlencode/ for details
20+
821
### Added
922
- Added environment variable `SLS_IGNORE` with default value of `favicon.ico`
1023

24+
## Fixed
25+
- Issue with Imgix compatibility regarding URL encoding. See Changed section
26+
1127
## [1.0.2] - 2019-12-09
1228
### Added
1329
- Added support for `dpr`

src/helpers/security.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,28 @@ exports.calculateHash = (path, queryStringParameters, securityKey) => {
1515

1616
// Encode each part of the URI. (Note, we're not using URLEncode on the entire thing, as it doesn't
1717
// properly handle "+" signs
18-
const encodedPath = decodeURIComponent(path).split('/').map((comp) => {
19-
return encodeURIComponent(comp)
20-
}).join('/')
18+
const encodedPath = fixedEncodeURIComponent(decodeURIComponent(path))
2119
const source = process.env.SECURITY_KEY + encodedPath + query
2220
const parsed = crypto.createHash('md5').update(source).digest('hex')
2321
return parsed
2422
}
2523

24+
/**
25+
* RFC 3986 encodeURIComponent
26+
* @param str
27+
* @return {string}
28+
*/
29+
function fixedEncodeURIComponent (str) {
30+
return str.replace(/([^\w\-\/\:@])/gi, function (match) {
31+
return encodeURIComponent(match)
32+
.replace(/!/g, '%21')
33+
.replace(/'/g, '%27')
34+
.replace(/\(/g, '%28')
35+
.replace(/\)/g, '%29')
36+
.replace(/\*/g, '%2A')
37+
})
38+
}
39+
2640
/**
2741
*
2842
* @param path

0 commit comments

Comments
 (0)