-
-
Notifications
You must be signed in to change notification settings - Fork 314
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
Open
Moshyfawn
wants to merge
5
commits into
npmx-dev:main
Choose a base branch
from
Moshyfawn:feat/endpoint-badge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−56
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8245d7e
feat(badge): add endpoint badge type for external json data
Moshyfawn 86439b4
fix: check endpoint response status before parsing
Moshyfawn 8635172
Merge branch 'main' into feat/endpoint-badge
Moshyfawn 9d00a33
Merge branch 'main' into feat/endpoint-badge
Moshyfawn 17d929a
Merge branch 'main' into feat/endpoint-badge
Moshyfawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
|
|
@@ -260,6 +268,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, | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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' | ||
|
|
@@ -400,65 +423,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
+437
to
+439
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, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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