Skip to content

Commit

Permalink
breaking: disallow external navigation using goto by default
Browse files Browse the repository at this point in the history
closes #8775
  • Loading branch information
dummdidumm committed Dec 6, 2023
1 parent 19be5e5 commit 6b7d066
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-games-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
---

breaking: disallow external navigation using `goto` by default
4 changes: 3 additions & 1 deletion packages/kit/src/runtime/app/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ export const disableScrollHandling = /* @__PURE__ */ client_method('disable_scro
* noScroll?: boolean;
* keepFocus?: boolean;
* invalidateAll?: boolean;
* external?: boolean;
* state?: any
* }) => Promise<void>}
* @param {string | URL} url Where to navigate to. Note that if you've set [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths) and the URL is root-relative, you need to prepend the base path if you want to navigate within the app.
* @param {Object} [opts] Options related to the navigation
* @param {boolean} [opts.replaceState] If `true`, will replace the current `history` entry rather than creating a new one with `pushState`
* @param {boolean} [opts.noScroll] If `true`, the browser will maintain its scroll position rather than scrolling to the top of the page after navigation
* @param {boolean} [opts.keepFocus] If `true`, the currently focused element will retain focus after navigation. Otherwise, focus will be reset to the body
* @param {boolean} [invalidateAll] If `true`, all `load` functions of the page will be rerun. See https://kit.svelte.dev/docs/load#rerunning-load-functions for more info on invalidation.
* @param {boolean} [opts.invalidateAll] If `true`, all `load` functions of the page will be rerun. See https://kit.svelte.dev/docs/load#rerunning-load-functions for more info on invalidation.
* @param {boolean} [opts.external] By default, `goto` will not navigate to external URLs for consistency and security reasons. Set this to `true` to allow navigating to external URLs.
* @param {any} [opts.state] The state of the new/updated history entry
* @returns {Promise<void>}
*/
Expand Down
13 changes: 11 additions & 2 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export function create_client(app, target) {

/**
* @param {string | URL} url
* @param {{ noScroll?: boolean; replaceState?: boolean; keepFocus?: boolean; state?: any; invalidateAll?: boolean }} opts
* @param {{ noScroll?: boolean; replaceState?: boolean; keepFocus?: boolean; state?: any; invalidateAll?: boolean, external?: boolean }} opts
* @param {number} redirect_count
* @param {{}} [nav_token]
*/
Expand All @@ -230,14 +230,23 @@ export function create_client(app, target) {
replaceState = false,
keepFocus = false,
state = {},
invalidateAll = false
invalidateAll = false,
external = false
},
redirect_count,
nav_token
) {
if (typeof url === 'string') {
url = new URL(url, get_base_uri(document));
}
if (!external && url.origin !== origin) {
if (DEV) {
throw new Error(
'Cannot navigate to an external URL using `goto` unless the `external` option is set'
);
}
return;
}

return navigate({
url,
Expand Down

0 comments on commit 6b7d066

Please sign in to comment.