-
-
Notifications
You must be signed in to change notification settings - Fork 290
feat: add endpoint badge type for external json data #1796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8245d7e
86439b4
8635172
9d00a33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,14 @@ const QUERY_SCHEMA = v.object({ | |
| label: v.optional(SafeStringSchema), | ||
| }) | ||
|
|
||
| const EndpointResponseSchema = v.object({ | ||
| schemaVersion: v.literal(1), | ||
| label: v.string(), | ||
| message: v.string(), | ||
| color: v.optional(v.string()), | ||
| labelColor: v.optional(v.string()), | ||
| }) | ||
|
|
||
| const COLORS = { | ||
| blue: '#3b82f6', | ||
| green: '#22c55e', | ||
|
|
@@ -248,6 +256,21 @@ async function fetchInstallSize(packageName: string, version: string): Promise<n | |
| } | ||
| } | ||
|
|
||
| async function fetchEndpointBadge(url: string) { | ||
| const response = await fetch(url, { headers: { Accept: 'application/json' } }) | ||
| if (!response.ok) { | ||
| throw createError({ statusCode: 502, message: `Endpoint returned ${response.status}` }) | ||
| } | ||
| const data = await response.json() | ||
| const parsed = v.parse(EndpointResponseSchema, data) | ||
| return { | ||
| label: parsed.label, | ||
| value: parsed.message, | ||
| color: parsed.color, | ||
| labelColor: parsed.labelColor, | ||
| } | ||
|
Comment on lines
+259
to
+271
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Escape and validate endpoint-derived values before SVG interpolation.
🛡️ Proposed fix+function escapeXml(value: string): string {
+ return value
+ .replaceAll('&', '&')
+ .replaceAll('<', '<')
+ .replaceAll('>', '>')
+ .replaceAll('"', '"')
+ .replaceAll("'", ''')
+}
+
+function normalizeBadgeColor(value: string, fallback: string): string {
+ const trimmed = value.trim()
+ const isSafeColour = /^#?[0-9a-fA-F]{3,8}$|^[a-zA-Z]+$/.test(trimmed)
+ if (!isSafeColour) return fallback
+ return trimmed.startsWith('#') ? trimmed : `#${trimmed}`
+}
+
async function fetchEndpointBadge(url: string) {
const response = await fetch(url, { headers: { Accept: 'application/json' } })
if (!response.ok) {
throw createError({ statusCode: 502, message: `Endpoint returned ${response.status}` })
}
const data = await response.json()
const parsed = v.parse(EndpointResponseSchema, data)
return {
- label: parsed.label,
- value: parsed.message,
+ label: escapeXml(parsed.label),
+ value: escapeXml(parsed.message),
color: parsed.color,
labelColor: parsed.labelColor,
}
}
@@
- const finalLabel = userLabel ?? strategyResult.label
- const finalValue = strategyResult.value
- const rawColor = userColor ?? strategyResult.color ?? COLORS.slate
- const finalColor = rawColor.startsWith('#') ? rawColor : `#${rawColor}`
+ const finalLabel = escapeXml(userLabel ?? strategyResult.label)
+ const finalValue = escapeXml(strategyResult.value)
+ const rawColor = userColor ?? strategyResult.color ?? COLORS.slate
+ const finalColor = normalizeBadgeColor(rawColor, COLORS.slate)
@@
- const rawLabelColor = labelColor ?? strategyResult.labelColor ?? defaultLabelColor
- const finalLabelColor = rawLabelColor.startsWith('#') ? rawLabelColor : `#${rawLabelColor}`
+ const rawLabelColor = labelColor ?? strategyResult.labelColor ?? defaultLabelColor
+ const finalLabelColor = normalizeBadgeColor(rawLabelColor, defaultLabelColor)Also applies to: 474-483 |
||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const badgeStrategies = { | ||
| 'version': async (pkgData: globalThis.Packument, requestedVersion?: string) => { | ||
| const version = requestedVersion ?? getLatestVersion(pkgData) ?? 'unknown' | ||
|
|
@@ -388,65 +411,85 @@ export default defineCachedEventHandler( | |
| async event => { | ||
| const query = getQuery(event) | ||
| const typeParam = getRouterParam(event, 'type') | ||
| const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? [] | ||
|
|
||
| if (pkgParamSegments.length === 0) { | ||
| // TODO: throwing 404 rather than 400 as it's cacheable | ||
| throw createError({ statusCode: 404, message: 'Package name is required.' }) | ||
| const queryParams = v.safeParse(QUERY_SCHEMA, query) | ||
| const userColor = queryParams.success ? queryParams.output.color : undefined | ||
| const userLabel = queryParams.success ? queryParams.output.label : undefined | ||
| const labelColor = queryParams.success ? queryParams.output.labelColor : undefined | ||
| const badgeStyleResult = v.safeParse(BadgeStyleSchema, query.style) | ||
| const badgeStyle = badgeStyleResult.success ? badgeStyleResult.output : 'default' | ||
|
|
||
| let strategyResult: { label: string; value: string; color?: string; labelColor?: string } | ||
|
|
||
| if (typeParam === 'endpoint') { | ||
| const endpointUrl = typeof query.url === 'string' ? query.url : undefined | ||
| if (!endpointUrl || !endpointUrl.startsWith('https://')) { | ||
| throw createError({ statusCode: 400, message: 'Missing or invalid "url" query parameter.' }) | ||
|
Comment on lines
+425
to
+427
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Harden endpoint URL validation to prevent SSRF. The current 🛡️ Proposed fix+function validateEndpointUrl(input: string): string {
+ let url: URL
+ try {
+ url = new URL(input)
+ } catch {
+ throw createError({ statusCode: 400, message: 'Invalid "url" query parameter.' })
+ }
+
+ if (url.protocol !== 'https:') {
+ throw createError({ statusCode: 400, message: 'Only HTTPS endpoint URLs are allowed.' })
+ }
+
+ if (url.username || url.password) {
+ throw createError({ statusCode: 400, message: 'Credentials are not allowed in endpoint URLs.' })
+ }
+
+ const blockedHosts = new Set(['localhost', '127.0.0.1', '::1'])
+ if (blockedHosts.has(url.hostname)) {
+ throw createError({ statusCode: 400, message: 'Local endpoint URLs are not allowed.' })
+ }
+
+ return url.toString()
+}
+
if (typeParam === 'endpoint') {
const endpointUrl = typeof query.url === 'string' ? query.url : undefined
- if (!endpointUrl || !endpointUrl.startsWith('https://')) {
+ if (!endpointUrl) {
throw createError({ statusCode: 400, message: 'Missing or invalid "url" query parameter.' })
}
+ const validatedEndpointUrl = validateEndpointUrl(endpointUrl)
try {
- strategyResult = await fetchEndpointBadge(endpointUrl)
+ strategyResult = await fetchEndpointBadge(validatedEndpointUrl)
} catch (error: unknown) {
handleApiError(error, { statusCode: 502, message: 'Failed to fetch endpoint data.' })
} |
||
| } | ||
|
|
||
| try { | ||
| strategyResult = await fetchEndpointBadge(endpointUrl) | ||
| } catch (error: unknown) { | ||
| handleApiError(error, { statusCode: 502, message: 'Failed to fetch endpoint data.' }) | ||
| } | ||
Moshyfawn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? [] | ||
|
|
||
| if (pkgParamSegments.length === 0) { | ||
| // TODO: throwing 404 rather than 400 as it's cacheable | ||
| throw createError({ statusCode: 404, message: 'Package name is required.' }) | ||
| } | ||
|
|
||
| const { rawPackageName, rawVersion } = parsePackageParams(pkgParamSegments) | ||
|
|
||
| try { | ||
| const { packageName, version: requestedVersion } = v.parse(PackageRouteParamsSchema, { | ||
| packageName: rawPackageName, | ||
| version: rawVersion, | ||
| }) | ||
|
|
||
| const showName = queryParams.success && queryParams.output.name === 'true' | ||
|
|
||
| const badgeTypeResult = v.safeParse(BadgeTypeSchema, typeParam) | ||
| const strategyKey = badgeTypeResult.success ? badgeTypeResult.output : 'version' | ||
| const strategy = badgeStrategies[strategyKey as keyof typeof badgeStrategies] | ||
|
|
||
| assertValidPackageName(packageName) | ||
|
|
||
| const pkgData = await fetchNpmPackage(packageName) | ||
| const result = await strategy(pkgData, requestedVersion) | ||
| strategyResult = { | ||
| label: showName ? packageName : result.label, | ||
| value: result.value, | ||
| color: result.color, | ||
| } | ||
| } catch (error: unknown) { | ||
| handleApiError(error, { | ||
| statusCode: 502, | ||
| message: ERROR_NPM_FETCH_FAILED, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| const { rawPackageName, rawVersion } = parsePackageParams(pkgParamSegments) | ||
|
|
||
| try { | ||
| const { packageName, version: requestedVersion } = v.parse(PackageRouteParamsSchema, { | ||
| packageName: rawPackageName, | ||
| version: rawVersion, | ||
| }) | ||
|
|
||
| const queryParams = v.safeParse(QUERY_SCHEMA, query) | ||
| const userColor = queryParams.success ? queryParams.output.color : undefined | ||
| const labelColor = queryParams.success ? queryParams.output.labelColor : undefined | ||
| const showName = queryParams.success && queryParams.output.name === 'true' | ||
| const userLabel = queryParams.success ? queryParams.output.label : undefined | ||
| const badgeStyleResult = v.safeParse(BadgeStyleSchema, query.style) | ||
| const badgeStyle = badgeStyleResult.success ? badgeStyleResult.output : 'default' | ||
|
|
||
| const badgeTypeResult = v.safeParse(BadgeTypeSchema, typeParam) | ||
| const strategyKey = badgeTypeResult.success ? badgeTypeResult.output : 'version' | ||
| const strategy = badgeStrategies[strategyKey as keyof typeof badgeStrategies] | ||
|
|
||
| assertValidPackageName(packageName) | ||
|
|
||
| const pkgData = await fetchNpmPackage(packageName) | ||
| const strategyResult = await strategy(pkgData, requestedVersion) | ||
|
|
||
| const finalLabel = userLabel ? userLabel : showName ? packageName : strategyResult.label | ||
| const finalValue = strategyResult.value | ||
|
|
||
| const rawColor = userColor ?? strategyResult.color | ||
| const finalColor = rawColor?.startsWith('#') ? rawColor : `#${rawColor}` | ||
|
|
||
| const defaultLabelColor = badgeStyle === 'shieldsio' ? '#555' : '#0a0a0a' | ||
| const rawLabelColor = labelColor ?? defaultLabelColor | ||
| const finalLabelColor = rawLabelColor.startsWith('#') ? rawLabelColor : `#${rawLabelColor}` | ||
|
|
||
| const renderFn = badgeStyle === 'shieldsio' ? renderShieldsBadgeSvg : renderDefaultBadgeSvg | ||
| const svg = renderFn({ finalColor, finalLabel, finalLabelColor, finalValue }) | ||
|
|
||
| setHeader(event, 'Content-Type', 'image/svg+xml') | ||
| setHeader( | ||
| event, | ||
| 'Cache-Control', | ||
| `public, max-age=${CACHE_MAX_AGE_ONE_HOUR}, s-maxage=${CACHE_MAX_AGE_ONE_HOUR}`, | ||
| ) | ||
|
|
||
| return svg | ||
| } catch (error: unknown) { | ||
| handleApiError(error, { | ||
| statusCode: 502, | ||
| message: ERROR_NPM_FETCH_FAILED, | ||
| }) | ||
| } | ||
| const finalLabel = userLabel ?? strategyResult.label | ||
| const finalValue = strategyResult.value | ||
| const rawColor = userColor ?? strategyResult.color ?? COLORS.slate | ||
| const finalColor = rawColor.startsWith('#') ? rawColor : `#${rawColor}` | ||
| const defaultLabelColor = badgeStyle === 'shieldsio' ? '#555' : '#0a0a0a' | ||
| const rawLabelColor = labelColor ?? strategyResult.labelColor ?? defaultLabelColor | ||
| const finalLabelColor = rawLabelColor.startsWith('#') ? rawLabelColor : `#${rawLabelColor}` | ||
|
|
||
| const renderFn = badgeStyle === 'shieldsio' ? renderShieldsBadgeSvg : renderDefaultBadgeSvg | ||
| const svg = renderFn({ finalColor, finalLabel, finalLabelColor, finalValue }) | ||
|
|
||
| setHeader(event, 'Content-Type', 'image/svg+xml') | ||
| setHeader( | ||
| event, | ||
| 'Cache-Control', | ||
| `public, max-age=${CACHE_MAX_AGE_ONE_HOUR}, s-maxage=${CACHE_MAX_AGE_ONE_HOUR}`, | ||
| ) | ||
|
|
||
| return svg | ||
| }, | ||
| { | ||
| maxAge: CACHE_MAX_AGE_ONE_HOUR, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add timeout and response-size guards for endpoint fetches.
The outbound call is currently unbounded. Slow or very large responses can tie up workers and increase memory pressure.
🧯 Proposed fix
async function fetchEndpointBadge(url: string) { - const response = await fetch(url, { headers: { Accept: 'application/json' } }) + const controller = new AbortController() + const timeout = setTimeout(() => controller.abort(), 5000) + const response = await fetch(url, { + headers: { Accept: 'application/json' }, + signal: controller.signal, + }) + clearTimeout(timeout) + if (!response.ok) { throw createError({ statusCode: 502, message: `Endpoint returned ${response.status}` }) } + + const contentLength = Number(response.headers.get('content-length') ?? 0) + if (Number.isFinite(contentLength) && contentLength > 64_000) { + throw createError({ statusCode: 502, message: 'Endpoint response is too large.' }) + } + const data = await response.json()📝 Committable suggestion