-
Notifications
You must be signed in to change notification settings - Fork 0
Support Vercel Agent Readability spec end-to-end (sitemap, llms.txt, markdown mirrors) #19
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
Merged
KayleeWilliams
merged 5 commits into
main
from
KayleeWilliams/support-vercel-agent-readability-spec-sitemap.xm
May 10, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2fe6a17
Add agent readability site support
KayleeWilliams 667a907
Harden agent-readability runtime: edge-safe APIs, runtime sitemap reb…
KayleeWilliams 7ecbdbb
Update agent readability sitemap support
KayleeWilliams 552ab85
Merge remote-tracking branch 'origin/main' into KayleeWilliams/suppor…
KayleeWilliams ab64bca
Surface agent markdown read errors
KayleeWilliams 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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { | ||
| createAgentMarkdownResponse, | ||
| createRobotsTxtResponse, | ||
| createSitemapMarkdownResponse, | ||
| createSitemapXmlResponse, | ||
| } from "leadtype/llm/readability"; | ||
| import { | ||
| defineEventHandler, | ||
| getHeaders, | ||
| getMethod, | ||
| getRequestURL, | ||
| } from "nitro/h3"; | ||
| import { | ||
| agentReadabilityManifest, | ||
| getRequestOrigin, | ||
| readMarkdownFile, | ||
| } from "../utils/agent-readability"; | ||
|
|
||
| export default defineEventHandler(async (event) => { | ||
| const method = getMethod(event); | ||
| if (method !== "GET" && method !== "HEAD") { | ||
| return; | ||
| } | ||
|
|
||
| const url = getRequestURL(event); | ||
| const requestOrigin = getRequestOrigin(event); | ||
| const pathname = url.pathname; | ||
|
|
||
| switch (pathname) { | ||
| case "/sitemap.xml": | ||
| case "/docs/sitemap.xml": | ||
| return createSitemapXmlResponse({ | ||
| manifest: agentReadabilityManifest, | ||
| requestOrigin, | ||
| }); | ||
| case "/sitemap.md": | ||
| case "/docs/sitemap.md": | ||
| return createSitemapMarkdownResponse({ | ||
| manifest: agentReadabilityManifest, | ||
| requestOrigin, | ||
| }); | ||
| case "/robots.txt": | ||
| return createRobotsTxtResponse({ | ||
| manifest: agentReadabilityManifest, | ||
| requestOrigin, | ||
| }); | ||
| case "/docs/robots.txt": | ||
| return createRobotsTxtResponse({ | ||
| manifest: agentReadabilityManifest, | ||
| requestOrigin, | ||
| sitemapUrlPath: "/docs/sitemap.xml", | ||
| }); | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| const response = await createAgentMarkdownResponse({ | ||
| urlPath: pathname, | ||
| method, | ||
| headers: getHeaders(event), | ||
| manifest: agentReadabilityManifest, | ||
| readMarkdownFile, | ||
| requestOrigin, | ||
| }); | ||
| return response ?? undefined; | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { readFile } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
| import type { | ||
| AgentReadabilityManifest, | ||
| MarkdownMirrorTarget, | ||
| } from "leadtype/llm/readability"; | ||
| import { | ||
| getHeader, | ||
| getRequestProtocol, | ||
| getRequestURL, | ||
| type H3Event, | ||
| } from "nitro/h3"; | ||
| import manifestJson from "../../src/generated/agent-readability.json" with { | ||
| type: "json", | ||
| }; | ||
|
|
||
| export const agentReadabilityManifest: AgentReadabilityManifest = { | ||
| ...manifestJson, | ||
| version: 1, | ||
| }; | ||
|
|
||
| export function getRequestOrigin(event: H3Event): string | undefined { | ||
| const forwardedHost = getHeader(event, "x-forwarded-host") | ||
| ?.split(",")[0] | ||
| ?.trim(); | ||
| const forwardedProto = getHeader(event, "x-forwarded-proto") | ||
| ?.split(",")[0] | ||
| ?.trim(); | ||
| if (forwardedHost) { | ||
| const protocol = forwardedProto || getRequestProtocol(event) || "http"; | ||
| return `${protocol}://${forwardedHost}`; | ||
| } | ||
| const url = getRequestURL(event); | ||
| return url.origin; | ||
| } | ||
|
|
||
| function isMissingFileError(error: unknown): boolean { | ||
| return ( | ||
| typeof error === "object" && | ||
| error !== null && | ||
| "code" in error && | ||
| (error.code === "ENOENT" || error.code === "ENOTDIR") | ||
| ); | ||
| } | ||
|
|
||
| export async function readMarkdownFile( | ||
| target: MarkdownMirrorTarget | ||
| ): Promise<string | null> { | ||
| try { | ||
| return await readFile( | ||
| join(process.cwd(), "public", target.filePath), | ||
| "utf8" | ||
| ); | ||
| } catch (error) { | ||
| if (isMissingFileError(error)) { | ||
| return null; | ||
| } | ||
|
|
||
| throw error; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Harden forwarded-header origin derivation against spoofed hosts/protocols.
Line 23 and Line 26 trust
x-forwarded-*directly. If those headers are not strictly sanitized by infrastructure, responses can emit attacker-controlled absolute URLs.🔐 Suggested hardening
export function getRequestOrigin(event: H3Event): string | undefined { + const requestUrl = getRequestURL(event); const forwardedHost = getHeader(event, "x-forwarded-host") ?.split(",")[0] ?.trim(); const forwardedProto = getHeader(event, "x-forwarded-proto") ?.split(",")[0] ?.trim(); if (forwardedHost) { - const protocol = forwardedProto || getRequestProtocol(event) || "http"; - return `${protocol}://${forwardedHost}`; + const protocol = + forwardedProto === "https" || forwardedProto === "http" + ? forwardedProto + : getRequestProtocol(event) ?? "http"; + try { + // Parse to reject malformed injected values. + return new URL(`${protocol}://${forwardedHost}`).origin; + } catch { + return requestUrl.origin; + } } - const url = getRequestURL(event); - return url.origin; + return requestUrl.origin; }