-
Notifications
You must be signed in to change notification settings - Fork 29
feat: validate directories from verification page #83
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
Closed
MukundaKatta
wants to merge
1
commit into
cloudflare:main
from
MukundaKatta:codex/validate-directory-ui
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,103 @@ async function fetchDirectory(signatureAgent: string): Promise<Directory> { | |
| return response.json(); | ||
| } | ||
|
|
||
| function validateDirectory(directory: unknown): string[] { | ||
| const errors: string[] = []; | ||
|
|
||
| if (directory === null || typeof directory !== "object") { | ||
| return ["Directory must be a JSON object"]; | ||
| } | ||
|
|
||
| const value = directory as Partial<Directory>; | ||
|
|
||
| if (!Array.isArray(value.keys)) { | ||
| errors.push("Directory must include a keys array"); | ||
| } else if (value.keys.length === 0) { | ||
| errors.push("Directory keys array must not be empty"); | ||
| } else { | ||
| for (const [index, key] of value.keys.entries()) { | ||
| if (key === null || typeof key !== "object") { | ||
| errors.push(`keys[${index}] must be a JSON object`); | ||
| continue; | ||
| } | ||
| if (typeof key.kty !== "string") { | ||
| errors.push(`keys[${index}].kty must be a string`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (typeof value.purpose !== "string" || value.purpose.length === 0) { | ||
| errors.push("Directory must include a non-empty purpose string"); | ||
| } | ||
|
|
||
| return errors; | ||
| } | ||
|
|
||
| async function validateDirectoryURL(request: Request): Promise<Response> { | ||
| const url = new URL(request.url); | ||
| const directoryURL = url.searchParams.get("url"); | ||
|
|
||
| if (directoryURL === null || directoryURL.length === 0) { | ||
| return Response.json( | ||
| { ok: false, errors: ["Missing url query parameter"] }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| let parsed: URL; | ||
| try { | ||
| parsed = new URL(directoryURL); | ||
| } catch (_e) { | ||
| return Response.json( | ||
| { ok: false, errors: ["URL must be valid"] }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| if (parsed.protocol !== "https:") { | ||
| return Response.json( | ||
| { ok: false, errors: ['Directory URL must use "https:"'] }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| if (parsed.pathname !== HTTP_MESSAGE_SIGNATURES_DIRECTORY) { | ||
| return Response.json( | ||
| { | ||
| ok: false, | ||
| errors: [ | ||
| `Directory URL path must be ${HTTP_MESSAGE_SIGNATURES_DIRECTORY}`, | ||
| ], | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const response = await fetch(parsed); | ||
|
Collaborator
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. this is an arbitrary fetch, which creates a DoS vector |
||
| if (!response.ok) { | ||
| return Response.json( | ||
| { | ||
| ok: false, | ||
| errors: [`Directory returned HTTP ${response.status}`], | ||
| }, | ||
| { status: 502 } | ||
| ); | ||
| } | ||
|
|
||
| let directory: unknown; | ||
| try { | ||
| directory = await response.json(); | ||
| } catch (_e) { | ||
| return Response.json( | ||
| { ok: false, errors: ["Directory response must be valid JSON"] }, | ||
| { status: 502 } | ||
| ); | ||
| } | ||
|
|
||
| const errors = validateDirectory(directory); | ||
| return Response.json({ ok: errors.length === 0, errors }); | ||
| } | ||
|
|
||
| async function getSigner(): Promise<Signer> { | ||
| return Ed25519Signer.fromJWK(jwk); | ||
| } | ||
|
|
@@ -180,6 +277,10 @@ export default { | |
| return new Response(status); | ||
| } | ||
|
|
||
| if (url.pathname.startsWith("/v0/api/validate-directory")) { | ||
| return validateDirectoryURL(request); | ||
| } | ||
|
|
||
| if (url.pathname.startsWith(HTTP_MESSAGE_SIGNATURES_DIRECTORY)) { | ||
| const directory = await getExampleDirectory(); | ||
|
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.