Skip to content

Commit 2456ab6

Browse files
nicholasserragkalpak
authored andcommitted
fix($resource): add semicolon to whitelist of delimiters to unencode in URL params
The unencoding happens in methods `encodeUriQuery`/`encodeUriSegment`. Both core and `ngResource` used to have identical implementations of these methods. Due to this duplication, the implementations got out-of-sync. Specifically, the semicolon has been added to the whitelist of unencoded characters in core since `v1.3.0-beta.18`. See 3625803 for more info. This commit fixes the problem and the underlying cause by reusing core's methods in `ngResource`. (The methods are exposed as private helpers on `window.angular`.) Closes angular#14309
1 parent 7a191eb commit 2456ab6

File tree

4 files changed

+14
-45
lines changed

4 files changed

+14
-45
lines changed

src/Angular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ function encodeUriSegment(val) {
13941394
* This method is intended for encoding *key* or *value* parts of query component. We need a custom
13951395
* method because encodeURIComponent is too aggressive and encodes stuff that doesn't have to be
13961396
* encoded per http://tools.ietf.org/html/rfc3986:
1397-
* query = *( pchar / "/" / "?" )
1397+
* query = *( pchar / "/" / "?" )
13981398
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
13991399
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
14001400
* pct-encoded = "%" HEXDIG HEXDIG

src/AngularPublic.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ function publishExternalAPI(angular) {
152152
'uppercase': uppercase,
153153
'callbacks': {$$counter: 0},
154154
'getTestability': getTestability,
155+
'reloadWithDebugInfo': reloadWithDebugInfo,
155156
'$$minErr': minErr,
156157
'$$csp': csp,
157-
'reloadWithDebugInfo': reloadWithDebugInfo
158+
'$$encodeUriSegment': encodeUriSegment,
159+
'$$encodeUriQuery': encodeUriQuery
158160
});
159161

160162
angularModule = setupModuleLoader(window);

src/ngResource/resource.js

+6-43
Original file line numberDiff line numberDiff line change
@@ -514,49 +514,12 @@ angular.module('ngResource', ['ng']).
514514
this.$get = ['$http', '$log', '$q', '$timeout', function($http, $log, $q, $timeout) {
515515

516516
var noop = angular.noop,
517-
forEach = angular.forEach,
518-
extend = angular.extend,
519-
copy = angular.copy,
520-
isFunction = angular.isFunction;
521-
522-
/**
523-
* We need our custom method because encodeURIComponent is too aggressive and doesn't follow
524-
* http://www.ietf.org/rfc/rfc3986.txt with regards to the character set
525-
* (pchar) allowed in path segments:
526-
* segment = *pchar
527-
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
528-
* pct-encoded = "%" HEXDIG HEXDIG
529-
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
530-
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
531-
* / "*" / "+" / "," / ";" / "="
532-
*/
533-
function encodeUriSegment(val) {
534-
return encodeUriQuery(val, true).
535-
replace(/%26/gi, '&').
536-
replace(/%3D/gi, '=').
537-
replace(/%2B/gi, '+');
538-
}
539-
540-
541-
/**
542-
* This method is intended for encoding *key* or *value* parts of query component. We need a
543-
* custom method because encodeURIComponent is too aggressive and encodes stuff that doesn't
544-
* have to be encoded per http://tools.ietf.org/html/rfc3986:
545-
* query = *( pchar / "/" / "?" )
546-
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
547-
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
548-
* pct-encoded = "%" HEXDIG HEXDIG
549-
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
550-
* / "*" / "+" / "," / ";" / "="
551-
*/
552-
function encodeUriQuery(val, pctEncodeSpaces) {
553-
return encodeURIComponent(val).
554-
replace(/%40/gi, '@').
555-
replace(/%3A/gi, ':').
556-
replace(/%24/g, '$').
557-
replace(/%2C/gi, ',').
558-
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
559-
}
517+
forEach = angular.forEach,
518+
extend = angular.extend,
519+
copy = angular.copy,
520+
isFunction = angular.isFunction,
521+
encodeUriQuery = angular.$$encodeUriQuery,
522+
encodeUriSegment = angular.$$encodeUriSegment;
560523

561524
function Route(template, defaults) {
562525
this.template = template;

test/ngResource/resourceSpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,14 @@ describe("basic usage", function() {
254254
$httpBackend.expect('GET', '/Path/foo%231').respond('{}');
255255
$httpBackend.expect('GET', '/Path/doh!@foo?bar=baz%231').respond('{}');
256256
$httpBackend.expect('GET', '/Path/herp$').respond('{}');
257+
$httpBackend.expect('GET', '/Path/foo;bar').respond('{}');
258+
$httpBackend.expect('GET', '/Path/foo?bar=baz;qux').respond('{}');
257259

258260
R.get({a: 'foo#1'});
259261
R.get({a: 'doh!@foo', bar: 'baz#1'});
260262
R.get({a: 'herp$'});
263+
R.get({a: 'foo;bar'});
264+
R.get({a: 'foo', bar: 'baz;qux'});
261265
});
262266

263267
it('should not encode @ in url params', function() {

0 commit comments

Comments
 (0)