-
Notifications
You must be signed in to change notification settings - Fork 21
Support draft-directory-04 with sf-dictionary signature-agent #63
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
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 |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| import { Component, HeaderValue, Parameter, Parameters } from "./types"; | ||
| import { | ||
| Component, | ||
| HeaderValue, | ||
| Parameter, | ||
| Parameters, | ||
| StructuredFieldComponent, | ||
| } from "./types"; | ||
| import { decode as base64Decode } from "./base64"; | ||
|
|
||
| function parseEntry( | ||
| headerName: string, | ||
| entry: string | ||
| ): [string, string | number | true | (string | number)[]] { | ||
| ): [ | ||
| string, | ||
| string | number | true | (string | number | StructuredFieldComponent)[], | ||
| ] { | ||
| // this is wrong. it should only split the first `=` | ||
| const equalsIndex = entry.indexOf("="); | ||
| if (equalsIndex === -1) { | ||
|
|
@@ -19,19 +28,34 @@ function parseEntry( | |
| if (value.match(/^".*"$/)) return [key.trim(), value.slice(1, -1)]; | ||
| if (value.match(/^\d+$/)) return [key.trim(), parseInt(value)]; | ||
|
|
||
| // TODO: this is restricted to components array. Per RFC9421, there could be more | ||
| if (value.match(/^\(.*\)$/)) { | ||
| const arr = value | ||
| .slice(1, -1) | ||
| .split(/\s+/) | ||
| .map((entry) => entry.match(/^"(.*)"$/)?.[1] ?? parseInt(entry)); | ||
| const arr = value.slice(1, -1).split(/\s+/); | ||
|
|
||
| const res = []; | ||
| for (const item of arr) { | ||
| const match = item.match(/^"(.*)"$/); | ||
| let toPush; | ||
| if (!match) { | ||
| toPush = parseInt(item); | ||
| } else if (match[1].includes('";key="')) { | ||
| toPush = { | ||
| key: match[1].split('";key="')[1], | ||
|
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. nit: this works if But for cases of two or more keys, where order is not fixed, this will break. I think a proper parser of innerlist parameters for incoming requests should handle that. |
||
| header: match[1].split('";key="')[0], | ||
| }; | ||
| } else { | ||
| toPush = match[1]; | ||
| } | ||
| res.push(toPush); | ||
| } | ||
|
|
||
| if (arr.some((value) => typeof value === "number" && isNaN(value))) { | ||
| if (res.some((value) => typeof value === "number" && isNaN(value))) { | ||
| throw new Error( | ||
| `Invalid ${headerName} header. Invalid value ${key}=${value}` | ||
| ); | ||
| } | ||
|
|
||
| return [key.trim(), arr]; | ||
| return [key.trim(), res]; | ||
| } | ||
|
|
||
| throw new Error( | ||
|
|
@@ -43,27 +67,20 @@ function parseParametersHeader( | |
| name: string, | ||
| header: HeaderValue | ||
| ): { key: string; components: Component[]; parameters: Parameters } { | ||
| const entries = header | ||
| .toString() | ||
| const rawHeader = header.toString(); | ||
| const [rawComponents, rawParameters] = rawHeader.split(/(?<=\))/, 2); | ||
| const [key, components] = parseEntry(name, rawComponents.trim()) as [ | ||
| string, | ||
| Component[], | ||
| ]; | ||
|
|
||
| const entries = rawParameters | ||
| // eslint-disable-next-line security/detect-unsafe-regex | ||
| .match(/(?:[^;"]+|"[^"]+")+/g) | ||
| ?.map((entry) => parseEntry(name, entry.trim())); | ||
|
|
||
| if (!entries) throw new Error(`Invalid ${name} header. Invalid value`); | ||
|
|
||
| const componentsIndex = entries.findIndex(([, value]) => | ||
| Array.isArray(value) | ||
| ); | ||
| if (componentsIndex === -1) | ||
| throw new Error(`Invalid ${name} header. Missing components`); | ||
| const [[key, components]] = entries.splice(componentsIndex, 1) as [ | ||
| [string, Component[]], | ||
| ]; | ||
|
|
||
| if (entries.some(([, value]) => Array.isArray(value))) { | ||
| throw new Error(`Multiple signatures is not supported`); | ||
| } | ||
|
|
||
| const parameters = Object.fromEntries(entries) as Record< | ||
| Parameter, | ||
| string | number | Date | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,11 @@ export type Parameter = | |
| | "keyid" | ||
| | string; | ||
|
|
||
| export interface StructuredFieldComponent { | ||
|
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. The name |
||
| header: string; | ||
| key: string; | ||
| } | ||
|
|
||
| export type Component = | ||
| | "@method" | ||
| | "@target-uri" | ||
|
|
@@ -67,7 +72,8 @@ export type Component = | |
| | "@query-param" | ||
| | "@status" | ||
| | "@request-response" | ||
| | string; | ||
| | string | ||
| | StructuredFieldComponent; | ||
|
|
||
| interface StandardParameters { | ||
| expires?: Date; | ||
|
|
||
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.
A small comment to clarify
valuehere has shape of an SFV innerlist would help - I didn't realize this was parsing e.g.foo=("a" "b";key="...")for a while.