-
Notifications
You must be signed in to change notification settings - Fork 22
add function to handle redirection of xsl schemas #261
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
poshul
wants to merge
2
commits into
main
Choose a base branch
from
add_edge_xsls
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Netlify helpfully redirects www.* request to the non-www version. This breaks resolving XSLs for our schemas if strict CORS is used. | ||
| // the entries in _redirects don't solve this since it happens at the edge, instead rewrite the request to not use a 301 redirect | ||
|
|
||
| /** Validate and normalise path segments from the request suffix. | ||
| * Returns the joined safe path string, or null if any segment is invalid. */ | ||
| function validatePathSegments(rest: string): string | null { | ||
| const segments = rest.split("/"); | ||
| for (const seg of segments) { | ||
| // Reject empty segments, dot-traversal, and any percent-encoded slash (%2F / %2f) | ||
| if (seg === "" || seg === "." || seg === "..") return null; | ||
| if (/%2f/i.test(seg)) return null; | ||
| let decoded: string; | ||
| try { | ||
| decoded = decodeURIComponent(seg); | ||
| } catch { | ||
| return null; | ||
| } | ||
| // After decoding, reject traversal components that sneak through encoding | ||
| if (decoded === "" || decoded === "." || decoded === "..") return null; | ||
| if (decoded.includes("/")) return null; | ||
| } | ||
| return segments.join("/"); | ||
| } | ||
|
|
||
| export default async (request: Request) => { | ||
| const url = new URL(request.url); | ||
| const path = url.pathname; | ||
|
|
||
| let upstream: string | null = null; | ||
|
|
||
| if (path.startsWith("/xml-stylesheet/")) { | ||
| const safe = validatePathSegments(path.slice("/xml-stylesheet/".length)); | ||
| if (!safe) { | ||
| return new Response("Bad Request: invalid path", { status: 400 }); | ||
| } | ||
| upstream = | ||
| "https://raw.githubusercontent.com/OpenMS/OpenMS/develop/share/OpenMS/XSL/" + | ||
| safe; | ||
| } else if (path.startsWith("/xml-schema/")) { | ||
| const safe = validatePathSegments(path.slice("/xml-schema/".length)); | ||
| if (!safe) { | ||
| return new Response("Bad Request: invalid path", { status: 400 }); | ||
| } | ||
| upstream = | ||
| "https://raw.githubusercontent.com/OpenMS/OpenMS/develop/share/OpenMS/SCHEMAS/" + | ||
| safe; | ||
| } | ||
|
|
||
| if (!upstream) { | ||
| return; | ||
| } | ||
|
|
||
| const corsHeaders = { | ||
| "Access-Control-Allow-Origin": "*", | ||
| "Vary": "Origin", | ||
| "Access-Control-Allow-Methods": "GET, HEAD, OPTIONS", | ||
| }; | ||
|
|
||
| if (request.method === "OPTIONS") { | ||
| return new Response(null, { status: 204, headers: corsHeaders }); | ||
| } | ||
|
|
||
| let upstreamRes: Response; | ||
| try { | ||
| upstreamRes = await fetch(upstream, { | ||
| method: request.method === "HEAD" ? "HEAD" : "GET", | ||
| headers: { "User-Agent": "openms-netlify-edge" }, | ||
| }); | ||
| } catch { | ||
| return new Response("Bad gateway", { status: 502, headers: corsHeaders }); | ||
| } | ||
|
|
||
| if (!upstreamRes.ok) { | ||
| return new Response(`Upstream error: ${upstreamRes.status}`, { | ||
| status: upstreamRes.status, | ||
| headers: corsHeaders, | ||
| }); | ||
| } | ||
|
|
||
| const headers = new Headers(upstreamRes.headers); | ||
|
|
||
| for (const [k, v] of Object.entries(corsHeaders)) headers.set(k, v); | ||
| if (path.startsWith("/xml-stylesheet/")) { | ||
| headers.set("Content-Type", "text/xsl; charset=utf-8"); | ||
| } else if (path.endsWith(".xsd")) { | ||
| headers.set("Content-Type", "application/xml; charset=utf-8"); | ||
| } | ||
|
|
||
| return new Response(request.method === "HEAD" ? null : upstreamRes.body, { | ||
| status: 200, | ||
| headers, | ||
| }); | ||
| }; | ||
|
|
||
| export const config = { | ||
| path: ["/xml-stylesheet/*", "/xml-schema/*"], | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| [build] | ||
| edge_functions = ".netlify/functions/edge-functions" | ||
|
|
||
| [build.environment] | ||
| HUGO_VERSION = "0.155.0" |
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.
Uh oh!
There was an error while loading. Please reload this page.