Skip to content
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

fix bundle validation #902

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-eggs-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sigstore/bundle': patch
---

Update `bundleFromJSON` to perform full bundle validation
108 changes: 78 additions & 30 deletions packages/bundle/src/__tests__/serialized.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,42 +369,90 @@ describe('bundleToJSON', () => {
});

describe('bundleFromJSON', () => {
const bundle: Bundle = {
mediaType: 'application/vnd.dev.sigstore.bundle+json;version=0.1',
verificationMaterial: {
describe('when the bundle is a v0.1 bundle', () => {
const bundle: Bundle = {
mediaType: 'application/vnd.dev.sigstore.bundle+json;version=0.1',
verificationMaterial: {
content: {
$case: 'x509CertificateChain',
x509CertificateChain: {
certificates: [{ rawBytes: Buffer.from('FOO') }],
},
},
tlogEntries: [
{
logIndex: '123',
logId: { keyId: Buffer.from('123') },
kindVersion: { kind: 'intoto', version: '0.1.0' },
canonicalizedBody: Buffer.from(''),
integratedTime: '0',
inclusionPromise: { signedEntryTimestamp: Buffer.from('') },
inclusionProof: undefined,
},
],
timestampVerificationData: undefined,
},
content: {
$case: 'x509CertificateChain',
x509CertificateChain: {
certificates: [{ rawBytes: Buffer.from('FOO') }],
$case: 'dsseEnvelope',
dsseEnvelope: {
payload: Buffer.from('ABC'),
payloadType: 'application/json',
signatures: [{ sig: Buffer.from('BAR'), keyid: '' }],
},
},
tlogEntries: [
{
logIndex: '123',
logId: { keyId: Buffer.from('123') },
kindVersion: { kind: 'intoto', version: '0.1.0' },
canonicalizedBody: Buffer.from(''),
integratedTime: '0',
inclusionPromise: { signedEntryTimestamp: Buffer.from('') },
inclusionProof: undefined,
};

it('matches the deserialized form of the Bundle', () => {
const json = bundleToJSON(bundle);
const deserializedBundle = bundleFromJSON(json);
expect(deserializedBundle).toEqual(bundle);
});
});

describe('when the bundle is a v0.2 bundle', () => {
const bundle: Bundle = {
mediaType: 'application/vnd.dev.sigstore.bundle+json;version=0.2',
verificationMaterial: {
content: {
$case: 'x509CertificateChain',
x509CertificateChain: {
certificates: [{ rawBytes: Buffer.from('FOO') }],
},
},
],
timestampVerificationData: undefined,
},
content: {
$case: 'dsseEnvelope',
dsseEnvelope: {
payload: Buffer.from('ABC'),
payloadType: 'application/json',
signatures: [{ sig: Buffer.from('BAR'), keyid: '' }],
tlogEntries: [
{
logIndex: '123',
logId: { keyId: Buffer.from('123') },
kindVersion: { kind: 'intoto', version: '0.1.0' },
canonicalizedBody: Buffer.from(''),
integratedTime: '0',
inclusionPromise: undefined,
inclusionProof: {
checkpoint: { envelope: '' },
hashes: [],
logIndex: '',
rootHash: Buffer.from(''),
treeSize: '',
},
},
],
timestampVerificationData: undefined,
},
},
};
content: {
$case: 'dsseEnvelope',
dsseEnvelope: {
payload: Buffer.from('ABC'),
payloadType: 'application/json',
signatures: [{ sig: Buffer.from('BAR'), keyid: '' }],
},
},
};

it('matches the deserialized form of the Bundle', () => {
const json = bundleToJSON(bundle);
const deserializedBundle = bundleFromJSON(json);
expect(deserializedBundle).toEqual(bundle);
it('matches the deserialized form of the Bundle', () => {
const json = bundleToJSON(bundle);
const deserializedBundle = bundleFromJSON(json);
expect(deserializedBundle).toEqual(bundle);
});
});
});

Expand Down
11 changes: 9 additions & 2 deletions packages/bundle/src/serialized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { Envelope, Bundle as ProtoBundle } from '@sigstore/protobuf-specs';
import { assertBundle } from './validate';
import { BUNDLE_V01_MEDIA_TYPE, Bundle } from './bundle';
import { assertBundle, assertBundleLatest, assertBundleV01 } from './validate';

import type { Bundle } from './bundle';
import type { OneOf } from './utility';

export const bundleFromJSON = (obj: unknown): Bundle => {
const bundle = ProtoBundle.fromJSON(obj);
assertBundle(bundle);

if (bundle.mediaType === BUNDLE_V01_MEDIA_TYPE) {
assertBundleV01(bundle);
} else {
assertBundleLatest(bundle);
}

return bundle;
};

Expand Down