Skip to content

Commit

Permalink
Merge pull request #661 from aguingand/update-skip-encoding
Browse files Browse the repository at this point in the history
Make slash "not encoding" less restrictive
  • Loading branch information
bakerkretzmar committed Aug 18, 2023
2 parents 7e34b12 + e26df66 commit 3573e2a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/js/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class Route {
const [location, query] = url.replace(/^\w+:\/\//, '').split('?');

const matches = new RegExp(`^${pattern}/?$`).exec(location);

if (matches) {
for (const k in matches.groups) {
matches.groups[k] = typeof matches.groups[k] === 'string' ? decodeURIComponent(matches.groups[k]) : matches.groups[k];
Expand Down Expand Up @@ -109,12 +109,14 @@ export default class Route {
throw new Error(`Ziggy error: '${segment}' parameter is required for route '${this.name}'.`)
}

if (segments[segments.length - 1].name === segment && this.wheres[segment] === '.*') {
return encodeURIComponent(params[segment] ?? '').replace(/%2F/g, '/');
}
if (this.wheres[segment]) {
if (!new RegExp(`^${optional ? `(${this.wheres[segment]})?` : this.wheres[segment]}$`).test(params[segment] ?? '')) {
throw new Error(`Ziggy error: '${segment}' parameter does not match required format '${this.wheres[segment]}' for route '${this.name}'.`)
}

if (this.wheres[segment] && !new RegExp(`^${optional ? `(${this.wheres[segment]})?` : this.wheres[segment]}$`).test(params[segment] ?? '')) {
throw new Error(`Ziggy error: '${segment}' parameter does not match required format '${this.wheres[segment]}' for route '${this.name}'.`)
if (segments[segments.length - 1].name === segment) {
return encodeURIComponent(params[segment] ?? '').replace(/%2F/g, '/');
}
}

return encodeURIComponent(params[segment] ?? '');
Expand Down
28 changes: 26 additions & 2 deletions tests/js/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ const defaultZiggy = {
slug: '.*',
},
},
slashesOtherRegex: {
uri: 'slashes/{encoded}/{slug}',
methods: ['GET', 'HEAD'],
wheres: {
slug: '.+',
},
},
slashesMiddleParam: {
uri: 'slashes/{encoded}/{slug}',
methods: ['GET', 'HEAD'],
wheres: {
encoded: '.+',
slug: '.+',
},
},
},
};

Expand Down Expand Up @@ -622,11 +637,20 @@ describe('route()', () => {
throws(() => route('pages.requiredExtensionWhere', { extension: '.pdf' }), /'extension' parameter does not match required format/);
});


test('can skip encoding slashes inside last parameter when explicitly allowed', () => {
test('skip encoding slashes inside last parameter when explicitly allowed', () => {
same(route('slashes', ['one/two', 'three/four']), 'https://ziggy.dev/slashes/one%2Ftwo/three/four');
same(route('slashes', ['one/two', 'Fun&Games/venues']), 'https://ziggy.dev/slashes/one%2Ftwo/Fun%26Games/venues');
same(route('slashes', ['one/two/three', 'Fun&Games/venues/outdoors']), 'https://ziggy.dev/slashes/one%2Ftwo%2Fthree/Fun%26Games/venues/outdoors');

same(route('slashesOtherRegex', ['one/two', 'three/four']), 'https://ziggy.dev/slashes/one%2Ftwo/three/four');
same(route('slashesOtherRegex', ['one/two', 'Fun&Games/venues']), 'https://ziggy.dev/slashes/one%2Ftwo/Fun%26Games/venues');
same(route('slashesOtherRegex', ['one/two/three', 'Fun&Games/venues/outdoors']), 'https://ziggy.dev/slashes/one%2Ftwo%2Fthree/Fun%26Games/venues/outdoors');
});

test.skip('skip encoding slashes inside middle parameter when explicitly allowed', () => {
same(route('slashesMiddleParam', ['one/two', 'three/four']), 'https://ziggy.dev/slashes/one/two/three/four');
same(route('slashesMiddleParam', ['one/two', 'Fun&Games/venues']), 'https://ziggy.dev/slashes/one/two/Fun%26Games/venues');
same(route('slashesMiddleParam', ['one/two/three', 'Fun&Games/venues/outdoors']), 'https://ziggy.dev/slashes/one/two/three/Fun%26Games/venues/outdoors');
});
});

Expand Down

0 comments on commit 3573e2a

Please sign in to comment.