diff --git a/CHANGELOG.md b/CHANGELOG.md index 729f3c5..4bbf7f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,13 +33,18 @@ Objects. Readers accept `0.4.0` and `0.5.0`; writers emit `0.5.0`. - `Vcon.migrate040To050()` static method, `Vcon.migrationReport`, and the `migrateLegacy` and `strictMigration` options on `new Vcon()` and `Vcon.buildFromJson()`. - `Vcon.isValid()` and `Vcon.getValidationWarnings()`, backed by `validateVconDict()`. -- Validation parity with the reference library vcon 1.0.5, with the same messages: +- Validation parity with the reference library vcon 1.0.6, with the same messages: - A malformed `content_hash` (not `-`, an empty list, or a `sha512`/`sha256` digest of the wrong length) on a Dialog, attachment, analysis, `redacted_from` or `amended_from` is an error. `contentHashProblems()` is exported. - - Warnings for a Dialog, analysis or inline attachment with a `url` but no `content_hash` - (an error from the next minor release), and for a serialized JSON string body under - `encoding: "json"`. + - A `url` without a `content_hash` is allowed on a Dialog, attachment or analysis, with no + error and no warning, following the working group's direction on + draft-ietf-vcon-vcon-core issue #40 (a producer cannot always obtain stable bytes for the + URL; core-05 itself still says MUST). An attachment needs `body`+`encoding` or `url` + (`Attachment at index N must include body+encoding or url.`). `Dialog.validate()` and + `Attachment.validate()` no longer require a `content_hash` with a `url`, but still reject a + malformed one. `redacted_from` and `amended_from` with a `url` still need a `content_hash`. + - A warning for a serialized JSON string body under `encoding: "json"`. - `isValid()` runs the lawful basis body validator on every `purpose: "lawful_basis"` attachment (`src/lawful-basis.ts`, a port of the reference `LawfulBasisValidator`), with errors and warnings prefixed `Attachment at index N (lawful_basis): `. Unknown proof diff --git a/CLAUDE.md b/CLAUDE.md index f87a771..13bc062 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -69,7 +69,7 @@ The library implements draft-ietf-vcon-vcon-core-05 (syntax `"0.5.0"`; 0.4.0 is - **JWS `alg` "none" is rejected** by `sign()` and `verify()`. - **0.4.0 input is migrated on load** (`migrate040To050`); signed 0.4.0 input is not. - **`group` has been removed** from the public surface (spec reserves it). -- **`content_hash` format** is checked by `Dialog.validate()` and `Attachment.validate()`. +- **`content_hash` format** is checked by `Dialog.validate()`, `Attachment.validate()` and `isValid()`. A `url` without a `content_hash` is allowed on Dialog, Attachment and Analysis (vcon 1.0.6, WG direction on core issue #40); `redacted_from`/`amended_from` with a `url` still need one. ## Development Commands diff --git a/README.md b/README.md index b445f32..e087a3e 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,8 @@ vcon.addAttachment({ vcon.addAttachment({ purpose: 'note', body: 'hi', encoding: 'none', mediatype: 'text/plain', party: 1, dialog: 0 }); vcon.addAttachment({ purpose: 'note', body: 'hi', encoding: 'none', mediatype: 'text/plain', party: 1, session: [0, 1] }); -// External attachment (url + content_hash both required; content_hash must -// match sha512-) +// External attachment (a content_hash is recommended where the bytes are stable; +// when present it must match sha512-) vcon.addAttachment({ purpose: 'transcript', url: 'https://example.com/transcript.txt', @@ -296,7 +296,7 @@ vcon.findLawfulBasisAttachments(0); // lawful_basis attachments for party 0 ### Content hashes and bodies -`isValid()` rejects a malformed `content_hash` (it must be `-`, or a non-empty list of them; `sha512` and `sha256` digests must have the right length) on Dialogs, attachments, analysis, `redacted_from` and `amended_from`. `getValidationWarnings()` notes a Dialog, analysis or inline attachment with a `url` but no `content_hash` (this will be an error in a later release), and a body under `encoding: "json"` that is a serialized JSON string rather than the JSON value. `contentHashProblems(value)` gives the reasons for one value. +`isValid()` rejects a malformed `content_hash` (it must be `-`, or a non-empty list of them; `sha512` and `sha256` digests must have the right length) on Dialogs, attachments, analysis, `redacted_from` and `amended_from`. A `url` without a `content_hash` is allowed on Dialogs, attachments and analysis (following the working group's direction on draft-ietf-vcon-vcon-core issue #40, since a producer cannot always obtain stable bytes for the URL); `redacted_from` and `amended_from` with a `url` still need one. An attachment needs `body`+`encoding` or `url`. `getValidationWarnings()` notes a body under `encoding: "json"` that is a serialized JSON string rather than the JSON value. `contentHashProblems(value)` gives the reasons for one value. ### Working with Tags @@ -547,7 +547,7 @@ Attachment.VALID_ENCODINGS // ['base64url', 'json', 'none'] - `filename?: string`: Original filename - `body?: any`: Inline content - `encoding?: string`: Content encoding (`base64url`, `json`, `none`) -- `url?: string`: External URL (requires `content_hash`) +- `url?: string`: External URL (`content_hash` recommended, not required) - `content_hash?: string | string[]`: Content hash (`sha512-`; single or array for multiple algorithms) #### Methods diff --git a/src/__tests__/content-and-lawful-basis.test.ts b/src/__tests__/content-and-lawful-basis.test.ts index 39407e2..9b80f2e 100644 --- a/src/__tests__/content-and-lawful-basis.test.ts +++ b/src/__tests__/content-and-lawful-basis.test.ts @@ -6,7 +6,7 @@ import { parsePyIsoformat, pyRepr } from '../util'; /** * Rules added to the reference library in vcon 1.0.4: content_hash form, - * url without content_hash, serialized JSON bodies, created_at kept as + * url without content_hash (allowed from vcon 1.0.6), serialized JSON bodies, created_at kept as * written, and the lawful basis body validator run by isValid(). */ @@ -56,14 +56,36 @@ describe('content checks in isValid() and getValidationWarnings()', () => { expect(v.isValid().valid).toBe(true); }); - it('warns about a Dialog url without content_hash', () => { + it('allows a Dialog url without content_hash, with no warning', () => { const v = withSession(); v.data.dialog = [recording({ url: 'https://x/a.wav' })]; v.data.sessions![0].dialog = [0]; - expect(v.isValid().valid).toBe(true); - expect(v.getValidationWarnings()).toContain( - 'Dialog at index 0 has a url but no content_hash (an external reference MUST have both; this will be an error in vcon 1.1).' - ); + expect(v.isValid()).toEqual({ valid: true, errors: [] }); + expect(v.getValidationWarnings().some(w => /content_hash/.test(w))).toBe(false); + }); + + it('allows an Attachment or Analysis url without content_hash', () => { + const v = withSession(); + v.data.attachments = [{ purpose: 'doc', start: T, party: 0, session: 0, url: 'https://x' }]; + v.data.analysis = [{ type: 'summary', vendor: 'v', sessions: 0, url: 'https://x' }]; + expect(v.isValid()).toEqual({ valid: true, errors: [] }); + expect(v.getValidationWarnings()).toEqual([]); + }); + + it('requires body+encoding or url on an Attachment', () => { + const v = withSession(); + v.data.attachments = [{ purpose: 'doc', start: T, party: 0, session: 0, content_hash: H512 }]; + expect(v.isValid().errors).toEqual(['Attachment at index 0 must include body+encoding or url.']); + }); + + it('still requires content_hash with a url on redacted_from and amended_from', () => { + const v = withSession(); + v.data.redacted_from = { type: 'pii', url: 'https://x' }; + expect(v.isValid().errors).toEqual(['redacted_from with url must include content_hash.']); + delete v.data.redacted_from; + v.data.amended_from = { url: 'https://x' }; + expect(v.isValid().errors).toEqual(['amended_from with url must include content_hash.']); + expect(v.getValidationWarnings().some(w => /content_hash/.test(w))).toBe(false); }); it('does not check an incomplete Dialog', () => { @@ -73,7 +95,7 @@ describe('content checks in isValid() and getValidationWarnings()', () => { expect(v.getValidationWarnings().some(w => /content_hash|serialized/.test(w))).toBe(false); }); - it('checks Attachment content_hash, and url only next to inline content', () => { + it('checks Attachment content_hash, and allows a url next to inline content', () => { const v = withSession(); v.data.attachments = [ { purpose: 'doc', start: T, party: 0, session: 0, url: 'https://x', content_hash: ['sha256-short'] }, @@ -82,9 +104,7 @@ describe('content checks in isValid() and getValidationWarnings()', () => { expect(v.isValid().errors).toEqual([ "Attachment at index 0 has an invalid content_hash: 'sha256-short' has a sha256 digest of 5 base64url characters, expected 43." ]); - expect(v.getValidationWarnings()).toEqual([ - 'Attachment at index 1 has a url but no content_hash (an external reference MUST have both; this will be an error in vcon 1.1).' - ]); + expect(v.getValidationWarnings()).toEqual([]); }); it('checks Analysis content and warns about a serialized JSON body', () => { diff --git a/src/__tests__/validation.test.ts b/src/__tests__/validation.test.ts index 5307eb5..a8808a9 100644 --- a/src/__tests__/validation.test.ts +++ b/src/__tests__/validation.test.ts @@ -20,9 +20,9 @@ describe('Dialog.validate error branches', () => { expect(r.errors.some(e => /Disposition is required/.test(e))).toBe(true); }); - it('rejects external url without content_hash', () => { + it('allows an external url without content_hash', () => { const r = new Dialog({ type: 'recording', start, parties: [0], url: 'https://x/a.wav' }).validate(); - expect(r.errors.some(e => /MUST include content_hash/.test(e))).toBe(true); + expect(r).toEqual({ valid: true, errors: [] }); }); it('rejects a malformed content_hash', () => { @@ -55,9 +55,16 @@ describe('Attachment.validate error branches', () => { expect(r.errors.some(e => /must have a purpose/.test(e))).toBe(true); }); - it('rejects external url without content_hash', () => { + it('allows an external url without content_hash', () => { const r = new Attachment({ purpose: 'transcript', party: 0, dialog: 0, url: 'https://x/t.txt' }).validate(); - expect(r.errors.some(e => /MUST include content_hash/.test(e))).toBe(true); + expect(r.errors.some(e => /content_hash/.test(e))).toBe(false); + }); + + it('rejects a malformed content_hash next to a url', () => { + const r = new Attachment({ + purpose: 'transcript', party: 0, dialog: 0, url: 'https://x/t.txt', content_hash: 'md5-abc' + }).validate(); + expect(r.errors.some(e => /Invalid content_hash format/.test(e))).toBe(true); }); it('throws on an invalid encoding at construction', () => { diff --git a/src/attachment.ts b/src/attachment.ts index 59483bd..1b65927 100644 --- a/src/attachment.ts +++ b/src/attachment.ts @@ -152,18 +152,15 @@ export class Attachment implements Partial { errors.push(`Invalid encoding: ${this.encoding}. Must be one of: ${Attachment.VALID_ENCODINGS.join(', ')}`); } - // External media MUST carry content_hash in spec-compliant format. - if (this.url !== undefined) { - if (this.content_hash === undefined) { - errors.push('Attachment with external url MUST include content_hash'); - } else { - const hashes = Array.isArray(this.content_hash) ? this.content_hash : [this.content_hash]; - const bad = hashes.filter(h => !isValidContentHash(h)); - if (bad.length > 0) { - errors.push( - `Invalid content_hash format: ${bad.join(', ')}. Expected sha512-` - ); - } + // A url without a content_hash is allowed (WG direction on core issue #40); + // a content_hash that is present MUST be well formed. + if (this.url !== undefined && this.content_hash !== undefined) { + const hashes = Array.isArray(this.content_hash) ? this.content_hash : [this.content_hash]; + const bad = hashes.filter(h => !isValidContentHash(h)); + if (bad.length > 0) { + errors.push( + `Invalid content_hash format: ${bad.join(', ')}. Expected sha512-` + ); } } diff --git a/src/dialog.ts b/src/dialog.ts index 3a00d5b..f26ab31 100644 --- a/src/dialog.ts +++ b/src/dialog.ts @@ -338,19 +338,15 @@ export class Dialog implements Partial { errors.push(`Invalid mediatype: ${this.mediatype}`); } - // External media MUST carry content_hash and the hash format MUST be - // sha512-. - if (this.url !== undefined) { - if (this.content_hash === undefined) { - errors.push('Dialog with external url MUST include content_hash'); - } else { - const hashes = Array.isArray(this.content_hash) ? this.content_hash : [this.content_hash]; - const bad = hashes.filter(h => !isValidContentHash(h)); - if (bad.length > 0) { - errors.push( - `Invalid content_hash format: ${bad.join(', ')}. Expected sha512-` - ); - } + // A url without a content_hash is allowed (WG direction on core issue #40); + // a content_hash that is present MUST be well formed. + if (this.url !== undefined && this.content_hash !== undefined) { + const hashes = Array.isArray(this.content_hash) ? this.content_hash : [this.content_hash]; + const bad = hashes.filter(h => !isValidContentHash(h)); + if (bad.length > 0) { + errors.push( + `Invalid content_hash format: ${bad.join(', ')}. Expected sha512-` + ); } } diff --git a/src/validation.ts b/src/validation.ts index 966d34b..3e14438 100644 --- a/src/validation.ts +++ b/src/validation.ts @@ -121,22 +121,19 @@ function isSerializedJson(body: string): boolean { /** * Integrity and body-form checks shared by Dialog, Attachment, Analysis and the * redacted_from / amended_from references (section 2.4). + * + * A `url` without a `content_hash` is allowed on Dialog, Attachment and + * Analysis: a producer cannot always obtain stable bytes for the URL (a + * streaming-service page, audio behind a login). This follows the working + * group's direction on draft-ietf-vcon-vcon-core issue #40; core-05 still + * says MUST. `redacted_from` and `amended_from` keep the requirement. */ function checkContent( obj: Record, label: string, errors: string[], - warnings: string[], - checkUrl = true + warnings: string[] ): void { - if (checkUrl && 'url' in obj && !('content_hash' in obj)) { - // A MUST in section 2.4. A warning in 1.0.x, as in the reference library, - // so producers that only hold a carrier URL keep validating. - warnings.push( - `${label} has a url but no content_hash (an external reference MUST have both; ` + - 'this will be an error in vcon 1.1).' - ); - } if ('content_hash' in obj) { for (const problem of contentHashProblems(obj.content_hash)) { errors.push(`${label} has an invalid content_hash: ${problem}.`); @@ -385,7 +382,7 @@ export function validateVconDict( } else { if (!('type' in rf)) errors.push('redacted_from is missing required field: type'); if ('url' in rf && !('content_hash' in rf)) errors.push('redacted_from with url must include content_hash.'); - checkContent(rf, 'redacted_from', errors, warnings, false); + checkContent(rf, 'redacted_from', errors, warnings); } } if ('amended_from' in vcon) { @@ -397,7 +394,7 @@ export function validateVconDict( errors.push('amended_from must include uuid, or an external reference (url).'); } if ('url' in af && !('content_hash' in af)) errors.push('amended_from with url must include content_hash.'); - checkContent(af, 'amended_from', errors, warnings, false); + checkContent(af, 'amended_from', errors, warnings); } } @@ -585,11 +582,11 @@ export function validateVconDict( } if (!('purpose' in attachment)) errors.push(`Attachment at index ${i} is missing required field: purpose`); const hasInline = 'body' in attachment && 'encoding' in attachment; - const hasExternal = 'url' in attachment && 'content_hash' in attachment; + const hasExternal = 'url' in attachment; if (!(hasInline || hasExternal || redacted)) { - errors.push(`Attachment at index ${i} must include body+encoding or url+content_hash.`); + errors.push(`Attachment at index ${i} must include body+encoding or url.`); } - checkContent(attachment, `Attachment at index ${i}`, errors, warnings, hasInline || redacted); + checkContent(attachment, `Attachment at index ${i}`, errors, warnings); if ('encoding' in attachment && !CORE_ENCODINGS.includes(attachment.encoding) && extListed.length === 0) { errors.push(`Attachment at index ${i} has invalid encoding: ${attachment.encoding}`); } diff --git a/src/vcon.ts b/src/vcon.ts index c862da9..10241e1 100644 --- a/src/vcon.ts +++ b/src/vcon.ts @@ -590,8 +590,8 @@ export class Vcon { * `dialog` or `session`, that `redacted_from` and `amended_from` are * exclusive, that `extensions` lists every extension in use (and every * `critical` entry), that every `__REDACTED_REF_#` has a `redacted_strings` - * entry, that every `url` has a `content_hash` and every `content_hash` is - * well formed, and the body of every `lawful_basis` attachment. + * entry, that every `content_hash` is well formed (a `url` without one is + * allowed, except on `redacted_from` and `amended_from`), and the body of every `lawful_basis` attachment. * * SHOULD-level findings are not errors; see getValidationWarnings(). */