From a39b1bb60acdf1a61ac7d2bd0c25d4508e45499f Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Fri, 10 Jan 2025 14:07:52 +0100 Subject: [PATCH] feat(website): escape url parts --- docs/src/content/docs/guides/self-hosting-registries.mdx | 4 ++-- src/source/pesde/mod.rs | 6 +++--- .../src/routes/(app)/packages/[scope]/[name]/+layout.ts | 2 +- .../src/routes/(app)/packages/[scope]/[name]/Tab.svelte | 2 +- .../(app)/packages/[scope]/[name]/TargetSelector.svelte | 4 ++-- .../[name]/[[version]]/[[target]]/(docs)/+layout.ts | 9 ++++++--- .../[name]/[[version]]/[[target]]/(docs)/+layout@.svelte | 6 +++++- .../[[version]]/[[target]]/(docs)/Hamburger.svelte | 2 +- .../[name]/[[version]]/[[target]]/(docs)/Tab.svelte | 2 +- .../[[version]]/[[target]]/(docs)/TargetSelector.svelte | 2 +- .../[[version]]/[[target]]/(docs)/VersionSelector.svelte | 4 ++-- .../[name]/[[version]]/[[target]]/(docs)/docs/+layout.ts | 2 +- .../[[version]]/[[target]]/(docs)/docs/[...doc]/+page.ts | 4 ++-- .../[[version]]/[[target]]/(docs)/reference/+layout.ts | 2 +- .../[scope]/[name]/[[version]]/[[target]]/+page.ts | 7 +------ .../[[version]]/[[target]]/dependencies/+page.svelte | 2 +- .../(app)/packages/[scope]/[name]/versions/+page.svelte | 2 +- 17 files changed, 32 insertions(+), 30 deletions(-) diff --git a/docs/src/content/docs/guides/self-hosting-registries.mdx b/docs/src/content/docs/guides/self-hosting-registries.mdx index 7f435e5..c2e1d18 100644 --- a/docs/src/content/docs/guides/self-hosting-registries.mdx +++ b/docs/src/content/docs/guides/self-hosting-registries.mdx @@ -23,7 +23,7 @@ the following content: api = "https://registry.acme.local/" # package download URL (optional) -download = "{API_URL}/v0/packages/{PACKAGE}/{PACKAGE_VERSION}/{PACKAGE_TARGET}" +download = "{API_URL}/v0/packages/{PACKAGE}/{PACKAGE_VERSION}/{PACKAGE_TARGET}/archive" # the client ID of the GitHub OAuth app (optional) github_oauth_client_id = "a1d648966fdfbdcd9295" @@ -58,7 +58,7 @@ scripts_packages = ["pesde/scripts_rojo"] - `{PACKAGE_VERSION}`: The package version. - `{PACKAGE_TARGET}`: The package target. - Defaults to `{API_URL}/v0/packages/{PACKAGE}/{PACKAGE_VERSION}/{PACKAGE_TARGET}`. + Defaults to `{API_URL}/v0/packages/{PACKAGE}/{PACKAGE_VERSION}/{PACKAGE_TARGET}/archive`. - **github_oauth_client_id**: This is required if you use GitHub OAuth for authentication. See below for more information. diff --git a/src/source/pesde/mod.rs b/src/source/pesde/mod.rs index 8952040..33a6ced 100644 --- a/src/source/pesde/mod.rs +++ b/src/source/pesde/mod.rs @@ -1,6 +1,6 @@ use gix::Url; use relative_path::RelativePathBuf; -use reqwest::header::{ACCEPT, AUTHORIZATION}; +use reqwest::header::AUTHORIZATION; use serde::{Deserialize, Serialize}; use std::{ collections::{BTreeMap, BTreeSet, HashSet}, @@ -220,7 +220,7 @@ impl PackageSource for PesdePackageSource { &urlencoding::encode(&id.version_id().target().to_string()), ); - let mut request = reqwest.get(&url).header(ACCEPT, "application/octet-stream"); + let mut request = reqwest.get(&url); if let Some(token) = project.auth_config().tokens().get(&self.repo_url) { tracing::debug!("using token for {}", self.repo_url); @@ -412,7 +412,7 @@ impl IndexConfig { pub fn download(&self) -> String { self.download .as_deref() - .unwrap_or("{API_URL}/v0/packages/{PACKAGE}/{PACKAGE_VERSION}/{PACKAGE_TARGET}") + .unwrap_or("{API_URL}/v0/packages/{PACKAGE}/{PACKAGE_VERSION}/{PACKAGE_TARGET}/archive") .replace("{API_URL}", self.api()) } } diff --git a/website/src/routes/(app)/packages/[scope]/[name]/+layout.ts b/website/src/routes/(app)/packages/[scope]/[name]/+layout.ts index b77e488..2c5ddf3 100644 --- a/website/src/routes/(app)/packages/[scope]/[name]/+layout.ts +++ b/website/src/routes/(app)/packages/[scope]/[name]/+layout.ts @@ -18,7 +18,7 @@ const fetchPackage = async (fetcher: typeof fetch, options: FetchPackageOptions) try { return await fetchRegistryJson( - `packages/${encodeURIComponent(`${scope}/${name}`)}/${version}/${target}`, + `packages/${encodeURIComponent(`${scope}/${name}`)}/${encodeURIComponent(version)}/${encodeURIComponent(target)}`, fetcher, ) } catch (e) { diff --git a/website/src/routes/(app)/packages/[scope]/[name]/Tab.svelte b/website/src/routes/(app)/packages/[scope]/[name]/Tab.svelte index cb73d6e..29eff8b 100644 --- a/website/src/routes/(app)/packages/[scope]/[name]/Tab.svelte +++ b/website/src/routes/(app)/packages/[scope]/[name]/Tab.svelte @@ -11,7 +11,7 @@ const basePath = $derived.by(() => { const { scope, name } = $page.params - return `/packages/${scope}/${name}` + return `/packages/${encodeURIComponent(scope)}/${encodeURIComponent(name)}` }) const activeTab = $derived( diff --git a/website/src/routes/(app)/packages/[scope]/[name]/TargetSelector.svelte b/website/src/routes/(app)/packages/[scope]/[name]/TargetSelector.svelte index 7b52df6..b0776d4 100644 --- a/website/src/routes/(app)/packages/[scope]/[name]/TargetSelector.svelte +++ b/website/src/routes/(app)/packages/[scope]/[name]/TargetSelector.svelte @@ -13,9 +13,9 @@ const { scope, name } = $page.params if ("target" in $page.params) { const { version } = $page.params - return `/packages/${scope}/${name}/${version}` + return `/packages/${encodeURIComponent(scope)}/${encodeURIComponent(name)}/${encodeURIComponent(version)}` } - return `/packages/${scope}/${name}/latest` + return `/packages/${encodeURIComponent(scope)}/${encodeURIComponent(name)}/latest` }) const items = ($page.data.pkg.targets as TargetInfo[]).map((target) => { diff --git a/website/src/routes/(app)/packages/[scope]/[name]/[[version]]/[[target]]/(docs)/+layout.ts b/website/src/routes/(app)/packages/[scope]/[name]/[[version]]/[[target]]/(docs)/+layout.ts index 50674dc..0ff6dd5 100644 --- a/website/src/routes/(app)/packages/[scope]/[name]/[[version]]/[[target]]/(docs)/+layout.ts +++ b/website/src/routes/(app)/packages/[scope]/[name]/[[version]]/[[target]]/(docs)/+layout.ts @@ -54,13 +54,16 @@ export const load: LayoutLoad = async ({ params, url, fetch }) => { if (version === undefined || version === "latest" || !isTargetKind(target)) { const pkg = await fetchRegistryJson( - `packages/${encodeURIComponent(`${scope}/${name}`)}/${version ?? "latest"}/${target ?? "any"}`, + `packages/${encodeURIComponent(`${scope}/${name}`)}/${encodeURIComponent(version ?? "latest")}/${encodeURIComponent(target ?? "any")}`, fetch, ) - const path = url.pathname.split("/").slice(6).join("/") + const path = url.pathname.split("/").slice(6).map(encodeURIComponent).join("/") - return redirect(303, `/packages/${scope}/${name}/${pkg.version}/${pkg.targets[0].kind}/${path}`) + return redirect( + 303, + `/packages/${encodeURIComponent(scope)}/${encodeURIComponent(name)}/${encodeURIComponent(pkg.version)}/${encodeURIComponent(pkg.targets[0].kind)}/${path}`, + ) } const { pkg, versions } = await fetchPackageAndVersions(fetch, { scope, name, version, target }) diff --git a/website/src/routes/(app)/packages/[scope]/[name]/[[version]]/[[target]]/(docs)/+layout@.svelte b/website/src/routes/(app)/packages/[scope]/[name]/[[version]]/[[target]]/(docs)/+layout@.svelte index accfc08..717fe02 100644 --- a/website/src/routes/(app)/packages/[scope]/[name]/[[version]]/[[target]]/(docs)/+layout@.svelte +++ b/website/src/routes/(app)/packages/[scope]/[name]/[[version]]/[[target]]/(docs)/+layout@.svelte @@ -13,6 +13,7 @@ import Toc from "./Toc.svelte" import TocObserver from "./TocObserver.svelte" import VersionSelector from "./VersionSelector.svelte" + import { TARGET_KIND_DISPLAY_NAMES, type TargetInfo, type TargetKind } from "$lib/registry-api" const { children, data } = $props() const [scope, name] = data.pkg.name.split("/") @@ -84,7 +85,7 @@ @@ -98,6 +99,9 @@