Skip to content

Commit

Permalink
coverage fixup for node 22 (#1289)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian DeHamer <bdehamer@github.com>
  • Loading branch information
bdehamer authored Oct 11, 2024
1 parent 00edf93 commit 65327f0
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .changeset/olive-jokes-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- 18.x
- 20.5.0
- 20.x
- 22.x
platform:
- os: ubuntu-latest
shell: bash
Expand Down
66 changes: 66 additions & 0 deletions packages/bundle/src/__tests__/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ describe('assertBundle', () => {
certificates: [{ rawBytes: Buffer.from('FOO') }],
},
},
tlogEntries: undefined,
},
content: {
$case: 'messageSignature',
Expand Down Expand Up @@ -589,6 +590,39 @@ describe('assertBundleV01', () => {
});
});

describe('when the tlogEntries is missing', () => {
const bundle: Bundle = fromPartial({
mediaType: 'application/vnd.dev.sigstore.bundle+json;version=0.1',
verificationMaterial: {
content: {
$case: 'x509CertificateChain',
x509CertificateChain: {
certificates: [{ rawBytes: Buffer.from('FOO') }],
},
},
tlogEntries: undefined,
},
content: {
$case: 'messageSignature',
messageSignature: {
messageDigest: { digest: Buffer.from('ABC') },
signature: Buffer.from('ABC'),
},
},
});

it('throws an error', () => {
expect.assertions(2);
try {
assertBundleV01(bundle);
} catch (e) {
assert(e instanceof ValidationError);
expect(e.fields).toHaveLength(1);
expect(e.fields).toContain('verificationMaterial.tlogEntries');
}
});
});

describe('when everything is valid', () => {
const bundle: Bundle = fromPartial({
mediaType: 'application/vnd.dev.sigstore.bundle+json;version=0.1',
Expand Down Expand Up @@ -832,6 +866,38 @@ describe('assertBundleLatest', () => {
});
});

describe('when tlogEntries is missing', () => {
const bundle: Bundle = fromPartial({
mediaType: 'application/vnd.dev.sigstore.bundle+json;version=0.3',
verificationMaterial: {
content: {
$case: 'certificate',
certificate: { rawBytes: Buffer.from('FOO') },
},
tlogEntries: undefined,
},
content: {
$case: 'dsseEnvelope',
dsseEnvelope: {
payload: Buffer.from('ABC'),
payloadType: 'application/json',
signatures: [{ sig: Buffer.from('BAR'), keyid: '' }],
},
},
});

it('throws an error', () => {
expect.assertions(2);
try {
assertBundleLatest(bundle);
} catch (e) {
assert(e instanceof ValidationError);
expect(e.fields).toHaveLength(1);
expect(e.fields).toContain('verificationMaterial.tlogEntries');
}
});
});

describe('when everything is valid', () => {
const bundle: Bundle = fromPartial({
mediaType: 'application/vnd.dev.sigstore.bundle+json;version=0.3',
Expand Down
1 change: 1 addition & 0 deletions packages/bundle/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ function validateInclusionProof(b: ProtoBundle): string[] {
function validateNoCertificateChain(b: ProtoBundle): string[] {
const invalidValues: string[] = [];

/* istanbul ignore next */
if (b.verificationMaterial?.content?.$case === 'x509CertificateChain') {
invalidValues.push('verificationMaterial.content.$case');
}
Expand Down
4 changes: 4 additions & 0 deletions packages/client/src/__tests__/sigstore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ describe('signAttestation (non-legacy)', () => {
rekorURL,
tsaServerURL: tsaURL,
identityProvider: idp,
retry: 0,
timeout: 0,
};
const bundle = await attest(payload, payloadType, options);
expect(bundle).toBeDefined();
Expand Down Expand Up @@ -190,6 +192,8 @@ describe('#verify', () => {
const options: VerifyOptions = {
...tufOptions,
keySelector: (hint: string) => validBundles.publicKeys[hint],
retry: 0,
timeout: 0,
};

it('does not throw an error', async () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/x509/cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@ export class X509Certificate {

get subjectAltName(): string | undefined {
const ext = this.extSubjectAltName;
return ext?.uri || ext?.rfc822Name;
return ext?.uri || /* istanbul ignore next */ ext?.rfc822Name;
}

get extensions(): ASN1Obj[] {
// The extension list is the first (and only) element of the extensions
// context specific tag
/* istanbul ignore next */
const extSeq = this.extensionsObj?.subs[0];
return extSeq?.subs || /* istanbul ignore next */ [];
/* istanbul ignore next */
return extSeq?.subs || [];
}

get extKeyUsage(): X509KeyUsageExtension | undefined {
Expand Down
15 changes: 15 additions & 0 deletions packages/mock/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,34 @@ describe('exports mock functions', () => {
await mockFulcio();
expect(nock.pendingMocks()).toHaveLength(1);
});

it('mocks fulcio w/ options', async () => {
await mockFulcio({ strict: true });
expect(nock.pendingMocks()).toHaveLength(1);
});
});

describe('mockRekor', () => {
it('mocks rekor', async () => {
await mockRekor();
expect(nock.pendingMocks()).toHaveLength(1);
});

it('mocks rekor w/ options', async () => {
await mockRekor({ strict: true });
expect(nock.pendingMocks()).toHaveLength(1);
});
});

describe('mockTSA', () => {
it('mocks tsa', async () => {
await mockTSA();
expect(nock.pendingMocks()).toHaveLength(1);
});

it('mocks tsa', async () => {
await mockTSA({ strict: true });
expect(nock.pendingMocks()).toHaveLength(1);
});
});
});
1 change: 1 addition & 0 deletions packages/mock/src/timestamp/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function createTimestampHandler(tsa: TSA, opts: TSAHandlerOptions): HandlerFn {
function parseRequest(body: string): TimestampRequest {
const json = JSON.parse(body.toString());

/* istanbul ignore next */
return {
artifactHash: Buffer.from(json.artifactHash, 'base64'),
hashAlgorithmOID: hashToOID(json.hashAlgorithm),
Expand Down
4 changes: 2 additions & 2 deletions packages/oci/src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export const getRegistryCredentials = (imageName: string): Credentials => {
const dockerConfig: DockerConifg = JSON.parse(content);

const credKey =
Object.keys(dockerConfig?.auths || {}).find((key) =>
Object.keys(dockerConfig.auths || {}).find((key) =>
key.includes(registry)
) || registry;
const creds = dockerConfig?.auths?.[credKey];
const creds = dockerConfig.auths?.[credKey];

if (!creds) {
throw new Error(`No credentials found for registry ${registry}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/sign/src/external/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export async function fetchWithRetry(
// is found.
const errorFromResponse = async (response: Response): Promise<HTTPError> => {
let message = response.statusText;
const location = response.headers?.get(HTTP2_HEADER_LOCATION) || undefined;
const contentType = response.headers?.get(HTTP2_HEADER_CONTENT_TYPE);
const location = response.headers.get(HTTP2_HEADER_LOCATION) || undefined;
const contentType = response.headers.get(HTTP2_HEADER_CONTENT_TYPE);

// If response type is JSON, try to parse the body for a message
if (contentType?.includes('application/json')) {
Expand Down
20 changes: 20 additions & 0 deletions packages/verify/src/__tests__/tlog/dsse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,24 @@ describe('verifyDSSETLogBody', () => {
);
});
});

describe('when there are no signatures', () => {
const bundle = bundleFromJSON(bundles.V1.DSSE.WITH_SIGNING_CERT.TLOG_DSSE);
const tlogEntry = bundle.verificationMaterial.tlogEntries[0];
const body: ProposedDSSEEntry = JSON.parse(
tlogEntry.canonicalizedBody.toString('utf8')
);
const content = signatureContent(bundle);

beforeEach(() => {
delete body.spec.signatures;
});

it('throws an error', () => {
expect(() => verifyDSSETLogBody(body, content)).toThrowWithCode(
VerificationError,
'TLOG_BODY_ERROR'
);
});
});
});
24 changes: 24 additions & 0 deletions packages/verify/src/__tests__/tlog/intoto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ describe('verifyIntotoTLogBody', () => {
});
});

describe('when there are no signatures', () => {
const bundle = bundleFromJSON(
bundles.V1.DSSE.WITH_SIGNING_CERT.TLOG_INTOTO
);
const tlogEntry = bundle.verificationMaterial.tlogEntries[0];
const body: ProposedIntotoEntry = JSON.parse(
tlogEntry.canonicalizedBody.toString('utf8')
);
const content = signatureContent(bundle);

beforeEach(() => {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const env = body.spec.content.envelope as any;
delete env.signatures;
});

it('throws an error', () => {
expect(() => verifyIntotoTLogBody(body, content)).toThrowWithCode(
VerificationError,
'TLOG_BODY_ERROR'
);
});
});

describe('when the tlog entry body is missing the payload hash', () => {
const bundle = bundleFromJSON(
bundles.V1.DSSE.WITH_SIGNING_CERT.TLOG_INTOTO
Expand Down
1 change: 1 addition & 0 deletions packages/verify/src/key/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function getSigner(cert: X509Certificate): Signer {
let issuer: string | undefined;
const issuerExtension = cert.extension(OID_FULCIO_ISSUER_V2);

/* istanbul ignore next */
if (issuerExtension) {
issuer = issuerExtension.valueObj.subs?.[0]?.value.toString('ascii');
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/verify/src/trust/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function createTLogAuthority(
}

function createCertAuthority(ca: CertificateAuthority): CertAuthority {
/* istanbul ignore next */
return {
certChain: ca.certChain!.certificates.map((cert) => {
return X509Certificate.parse(cert.rawBytes);
Expand All @@ -108,6 +109,7 @@ function keyLocator(keys?: Record<string, PublicKey>): KeyFinderFunc {
return {
publicKey: crypto.createPublicKey(key.rawBytes!),
validFor: (date: Date) => {
/* istanbul ignore next */
return (
(key.validFor?.start || BEGINNING_OF_TIME) <= date &&
(key.validFor?.end || END_OF_TIME) >= date
Expand Down

0 comments on commit 65327f0

Please sign in to comment.