Commit bde4b76c by Benjamin Schweizer

based on encodeURIComponent() using strict RFC 3986 sub-delims

parent 8a09d847
...@@ -52,7 +52,7 @@ Filter Option | Example | Raw | Interpolated | Description ...@@ -52,7 +52,7 @@ Filter Option | Example | Raw | Interpolated | Description
`csv`| ${servers:csv} | `'test1', 'test2'` | `test1,test2` | Formats multi-value variable as a comma-separated string `csv`| ${servers:csv} | `'test1', 'test2'` | `test1,test2` | Formats multi-value variable as a comma-separated string
`distributed`| ${servers:distributed} | `'test1', 'test2'` | `test1,servers=test2` | Formats multi-value variable in custom format for OpenTSDB. `distributed`| ${servers:distributed} | `'test1', 'test2'` | `test1,servers=test2` | Formats multi-value variable in custom format for OpenTSDB.
`lucene`| ${servers:lucene} | `'test', 'test2'` | `("test" OR "test2")` | Formats multi-value variable as a lucene expression. `lucene`| ${servers:lucene} | `'test', 'test2'` | `("test" OR "test2")` | Formats multi-value variable as a lucene expression.
`percentencode` | ${servers:percentencode} | `'foo()bar BAZ', 'test2'` | `{foo%28%29bar%20BAZ%2Ctest2}` | Formats multi-value variable into a glob, percent-escaped `percentencode` | ${servers:percentencode} | `'foo()bar BAZ', 'test2'` | `{foo%28%29bar%20BAZ%2Ctest2}` | Formats multi-value variable into a glob, percent-encoded.
Test the formatting options on the [Grafana Play site](http://play.grafana.org/d/cJtIfcWiz/template-variable-formatting-options?orgId=1). Test the formatting options on the [Grafana Play site](http://play.grafana.org/d/cJtIfcWiz/template-variable-formatting-options?orgId=1).
......
...@@ -277,7 +277,7 @@ describe('templateSrv', () => { ...@@ -277,7 +277,7 @@ describe('templateSrv', () => {
it('multi value and percentencode format should render percent-encoded string', () => { it('multi value and percentencode format should render percent-encoded string', () => {
const result = _templateSrv.formatValue(['foo()bar BAZ', 'test2'], 'percentencode'); const result = _templateSrv.formatValue(['foo()bar BAZ', 'test2'], 'percentencode');
expect(result).toBe('%7bfoo%28%29bar%20BAZ%2ctest2%7d'); expect(result).toBe('%7Bfoo%28%29bar%20BAZ%2Ctest2%7D');
}); });
it('slash should be properly escaped in regex format', () => { it('slash should be properly escaped in regex format', () => {
......
...@@ -77,10 +77,12 @@ export class TemplateSrv { ...@@ -77,10 +77,12 @@ export class TemplateSrv {
return '(' + quotedValues.join(' OR ') + ')'; return '(' + quotedValues.join(' OR ') + ')';
} }
// like encodeURIComponent() but for all characters except alpha-numerics // encode string according to RFC 3986; in contrast to encodeURIComponent()
encodeURIQueryValue(str) { // also the sub-delims "!", "'", "(", ")" and "*" are encoded;
return str.replace(/[^a-z0-9]/gi, function(c) { // unicode handling uses UTF-8 as in ECMA-262.
return '%' + c.charCodeAt(0).toString(16); encodeURIComponentStrict(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, (c) => {
return '%' + c.charCodeAt(0).toString(16).toUpperCase();
}); });
} }
...@@ -128,9 +130,9 @@ export class TemplateSrv { ...@@ -128,9 +130,9 @@ export class TemplateSrv {
case 'percentencode': { case 'percentencode': {
// like glob, but url escaped // like glob, but url escaped
if (_.isArray(value)) { if (_.isArray(value)) {
return this.encodeURIQueryValue('{' + value.join(',') + '}'); return this.encodeURIComponentStrict('{' + value.join(',') + '}');
} }
return this.encodeURIQueryValue(value); return this.encodeURIComponentStrict(value);
} }
default: { default: {
if (_.isArray(value)) { if (_.isArray(value)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment