Skip to content

Commit

Permalink
raise error on root metadata error (#779)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian DeHamer <[email protected]>
  • Loading branch information
bdehamer authored Oct 14, 2024
1 parent 0e1dc0d commit cb54532
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curvy-ducks-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tuf-js': patch
---

Ensure errors are thrown when root metadata is invalid
21 changes: 21 additions & 0 deletions packages/client/src/__tests__/updater.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { clearMock, mockRepo } from '@tufjs/repo-mock';
import assert from 'assert';
import fs from 'fs';
import nock from 'nock';
import os from 'os';
import path from 'path';
import { Updater, UpdaterOptions } from '../updater';
Expand Down Expand Up @@ -76,6 +77,26 @@ describe('Updater', () => {
await expect(updater.refresh()).resolves.toBe(undefined);
});
});

describe('when repo serves invalid root metadata', () => {
beforeEach(() => {
// Remove 404 response for 2.root.json
const repoURL = new URL(baseURL);
nock.removeInterceptor({
hostname: repoURL.hostname,
port: repoURL.port,
path: '/metadata/2.root.json',
});

// Serve 2.root.json w/ invalid data
nock(baseURL).get('/metadata/2.root.json').reply(200, {});
});

it('resolves with no error', async () => {
const updater = new Updater(options);
await expect(updater.refresh()).rejects.toThrow(TypeError);
});
});
});

describe('#getTargetInfo', () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/client/src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { Config, defaultConfig } from './config';
import {
DownloadHTTPError,
EqualVersionError,
PersistError,
RuntimeError,
Expand Down Expand Up @@ -200,7 +201,14 @@ export class Updater {
// Client workflow 5.3.8: persist root metadata file
this.persistMetadata(MetadataKind.Root, bytesData);
} catch (error) {
break;
if (error instanceof DownloadHTTPError) {
// 404/403 means current root is newest available
if ([403, 404].includes(error.statusCode)) {
break;
}
}

throw error;
}
}
}
Expand Down

0 comments on commit cb54532

Please sign in to comment.