Skip to content

Commit a06d5fc

Browse files
committed
fix: allow more origins in CSP
1 parent 010723b commit a06d5fc

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

app/composables/useRepoMeta.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ProviderId, RepoRef } from '#shared/utils/git-providers'
2-
import { parseRepoUrl, GITLAB_HOSTS } from '#shared/utils/git-providers'
2+
import { GIT_PROVIDER_API_ORIGINS, parseRepoUrl, GITLAB_HOSTS } from '#shared/utils/git-providers'
33

44
// TTL for git repo metadata (10 minutes - repo stats don't change frequently)
55
const REPO_META_TTL = 60 * 10
@@ -132,7 +132,7 @@ const githubAdapter: ProviderAdapter = {
132132
let res: UnghRepoResponse | null = null
133133
try {
134134
const { data } = await cachedFetch<UnghRepoResponse>(
135-
`https://ungh.cc/repos/${ref.owner}/${ref.repo}`,
135+
`${GIT_PROVIDER_API_ORIGINS.github}/repos/${ref.owner}/${ref.repo}`,
136136
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
137137
REPO_META_TTL,
138138
)
@@ -254,7 +254,7 @@ const bitbucketAdapter: ProviderAdapter = {
254254
let res: BitbucketRepoResponse | null = null
255255
try {
256256
const { data } = await cachedFetch<BitbucketRepoResponse>(
257-
`https://api.bitbucket.org/2.0/repositories/${ref.owner}/${ref.repo}`,
257+
`${GIT_PROVIDER_API_ORIGINS.bitbucket}/2.0/repositories/${ref.owner}/${ref.repo}`,
258258
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
259259
REPO_META_TTL,
260260
)
@@ -312,7 +312,7 @@ const codebergAdapter: ProviderAdapter = {
312312
let res: GiteaRepoResponse | null = null
313313
try {
314314
const { data } = await cachedFetch<GiteaRepoResponse>(
315-
`https://codeberg.org/api/v1/repos/${ref.owner}/${ref.repo}`,
315+
`${GIT_PROVIDER_API_ORIGINS.codeberg}/api/v1/repos/${ref.owner}/${ref.repo}`,
316316
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
317317
REPO_META_TTL,
318318
)
@@ -370,7 +370,7 @@ const giteeAdapter: ProviderAdapter = {
370370
let res: GiteeRepoResponse | null = null
371371
try {
372372
const { data } = await cachedFetch<GiteeRepoResponse>(
373-
`https://gitee.com/api/v5/repos/${ref.owner}/${ref.repo}`,
373+
`${GIT_PROVIDER_API_ORIGINS.gitee}/api/v5/repos/${ref.owner}/${ref.repo}`,
374374
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
375375
REPO_META_TTL,
376376
)
@@ -623,7 +623,7 @@ const radicleAdapter: ProviderAdapter = {
623623
let res: RadicleProjectResponse | null = null
624624
try {
625625
const { data } = await cachedFetch<RadicleProjectResponse>(
626-
`https://seed.radicle.at/api/v1/projects/${ref.repo}`,
626+
`${GIT_PROVIDER_API_ORIGINS.radicle}/api/v1/projects/${ref.repo}`,
627627
{ headers: { 'User-Agent': 'npmx', ...options.headers }, ...options },
628628
REPO_META_TTL,
629629
)

server/middleware/security-headers.global.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ALL_KNOWN_GIT_API_ORIGINS } from '#shared/utils/git-providers'
12
import { TRUSTED_IMAGE_DOMAINS } from '../utils/image-proxy'
23

34
/**
@@ -23,7 +24,15 @@ const imgSrc = [
2324

2425
const connectSrc = [
2526
"'self'",
26-
'https://*.algolia.net', // Algolia npm-search client
27+
// Algolia npm-search client
28+
'https://*.algolia.net',
29+
// npm registry & API (client-side fetches via $npmRegistry, $npmApi, useCachedFetch)
30+
'https://registry.npmjs.org',
31+
'https://api.npmjs.org',
32+
// fast-npm-meta (version resolution)
33+
'https://npm.antfu.dev',
34+
// Git hosting APIs (repo metadata on client-side navigation)
35+
...ALL_KNOWN_GIT_API_ORIGINS,
2736
].join(' ')
2837

2938
const frameSrc = [

shared/utils/git-providers.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,24 @@ export function convertBlobOrFileToRawUrl(url: string, providerId: ProviderId):
404404
export function isKnownGitProvider(url: string): boolean {
405405
return parseRepoUrl(url) !== null
406406
}
407+
408+
/**
409+
* API origins used by each provider for client-side repo metadata fetches.
410+
* Self-hosted providers are excluded because their origins can be anything.
411+
*/
412+
export const GIT_PROVIDER_API_ORIGINS = {
413+
github: 'https://ungh.cc', // via UNGH proxy to avoid rate limits
414+
bitbucket: 'https://api.bitbucket.org',
415+
codeberg: 'https://codeberg.org',
416+
gitee: 'https://gitee.com',
417+
radicle: 'https://seed.radicle.at',
418+
} as const satisfies Partial<Record<ProviderId, string>>
419+
420+
/**
421+
* All known external API origins that git provider adapters may fetch from.
422+
* Includes both the per-provider origins and known self-hosted instances.
423+
*/
424+
export const ALL_KNOWN_GIT_API_ORIGINS: readonly string[] = [
425+
...Object.values(GIT_PROVIDER_API_ORIGINS),
426+
...GITLAB_HOSTS.map(host => `https://${host}`),
427+
]

0 commit comments

Comments
 (0)