const test = require('node:test'); const assert = require('node:assert/strict'); const { assertDavObjectReference } = require('../desktop/dav/remote-reference.cjs'); const config = { id: 'dav-1', serverUrl: 'https://dav.example.test/' }; const collection = { url: 'https://dav.example.test/calendars/user/main/', displayName: 'Main' }; const object = { url: 'https://dav.example.test/calendars/user/main/event-1.ics', etag: '"v4"' }; const remote = { accountId: 'dav-1', collectionUrl: collection.url, objectUrl: object.url, etag: object.etag }; test('accepts a resource verified against the configured server, collection, and current ETag', () => { const result = assertDavObjectReference({ config, remote, collections: [collection], objects: [object] }); assert.equal(result.collection, collection); assert.equal(result.object, object); assert.equal(result.objectUrl, object.url); }); test('accepts a relative server-returned object URL within its verified collection', () => { const result = assertDavObjectReference({ config, remote: { ...remote, objectUrl: 'event-1.ics' }, collections: [collection], objects: [{ url: 'event-1.ics', etag: '"v4"' }] }); assert.equal(result.objectUrl, object.url); }); test('rejects account, collection, and resource references not returned by the server', () => { assert.throws(() => assertDavObjectReference({ config, remote: { ...remote, accountId: 'other-account' }, collections: [collection], objects: [object] }), /does not belong/); assert.throws(() => assertDavObjectReference({ config, remote: { ...remote, collectionUrl: 'https://dav.example.test/other/' }, collections: [collection], objects: [object] }), /collection is not available/); assert.throws(() => assertDavObjectReference({ config, remote: { ...remote, objectUrl: 'https://dav.example.test/calendars/user/main/unknown.ics' }, collections: [collection], objects: [object] }), /no longer available/); }); test('rejects cross-origin, credential-bearing, query, traversal, and out-of-collection URLs', () => { for (const objectUrl of [ 'https://attacker.example/event.ics', 'https://user:pass@dav.example.test/calendars/user/main/event-1.ics', 'https://dav.example.test/calendars/user/main/event-1.ics?redirect=1', '../other/event.ics', 'https://dav.example.test/calendars/user/main/', 'https://dav.example.test/calendars/user/other/event.ics' ]) { assert.throws(() => assertDavObjectReference({ config, remote: { ...remote, objectUrl }, collections: [collection], objects: [object] })); } }); test('rejects stale ETags and ETags with unexpected types', () => { assert.throws(() => assertDavObjectReference({ config, remote: { ...remote, etag: '"v3"' }, collections: [collection], objects: [object] }), /changed on the server/); assert.throws(() => assertDavObjectReference({ config, remote: { ...remote, etag: 4 }, collections: [collection], objects: [{ ...object, etag: undefined }] }), /version is invalid/); });