validate.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.assertBundle = assertBundle;
  4. exports.assertBundleV01 = assertBundleV01;
  5. exports.isBundleV01 = isBundleV01;
  6. exports.assertBundleV02 = assertBundleV02;
  7. exports.assertBundleLatest = assertBundleLatest;
  8. /*
  9. Copyright 2023 The Sigstore Authors.
  10. Licensed under the Apache License, Version 2.0 (the "License");
  11. you may not use this file except in compliance with the License.
  12. You may obtain a copy of the License at
  13. http://www.apache.org/licenses/LICENSE-2.0
  14. Unless required by applicable law or agreed to in writing, software
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. */
  20. const error_1 = require("./error");
  21. // Performs basic validation of a Sigstore bundle to ensure that all required
  22. // fields are populated. This is not a complete validation of the bundle, but
  23. // rather a check that the bundle is in a valid state to be processed by the
  24. // rest of the code.
  25. function assertBundle(b) {
  26. const invalidValues = validateBundleBase(b);
  27. if (invalidValues.length > 0) {
  28. throw new error_1.ValidationError('invalid bundle', invalidValues);
  29. }
  30. }
  31. // Asserts that the given bundle conforms to the v0.1 bundle format.
  32. function assertBundleV01(b) {
  33. const invalidValues = [];
  34. invalidValues.push(...validateBundleBase(b));
  35. invalidValues.push(...validateInclusionPromise(b));
  36. if (invalidValues.length > 0) {
  37. throw new error_1.ValidationError('invalid v0.1 bundle', invalidValues);
  38. }
  39. }
  40. // Type guard to determine if Bundle is a v0.1 bundle.
  41. function isBundleV01(b) {
  42. try {
  43. assertBundleV01(b);
  44. return true;
  45. }
  46. catch (e) {
  47. return false;
  48. }
  49. }
  50. // Asserts that the given bundle conforms to the v0.2 bundle format.
  51. function assertBundleV02(b) {
  52. const invalidValues = [];
  53. invalidValues.push(...validateBundleBase(b));
  54. invalidValues.push(...validateInclusionProof(b));
  55. if (invalidValues.length > 0) {
  56. throw new error_1.ValidationError('invalid v0.2 bundle', invalidValues);
  57. }
  58. }
  59. // Asserts that the given bundle conforms to the newest (0.3) bundle format.
  60. function assertBundleLatest(b) {
  61. const invalidValues = [];
  62. invalidValues.push(...validateBundleBase(b));
  63. invalidValues.push(...validateInclusionProof(b));
  64. invalidValues.push(...validateNoCertificateChain(b));
  65. if (invalidValues.length > 0) {
  66. throw new error_1.ValidationError('invalid bundle', invalidValues);
  67. }
  68. }
  69. function validateBundleBase(b) {
  70. const invalidValues = [];
  71. // Media type validation
  72. if (b.mediaType === undefined ||
  73. (!b.mediaType.match(/^application\/vnd\.dev\.sigstore\.bundle\+json;version=\d\.\d/) &&
  74. !b.mediaType.match(/^application\/vnd\.dev\.sigstore\.bundle\.v\d\.\d\+json/))) {
  75. invalidValues.push('mediaType');
  76. }
  77. // Content-related validation
  78. if (b.content === undefined) {
  79. invalidValues.push('content');
  80. }
  81. else {
  82. switch (b.content.$case) {
  83. case 'messageSignature':
  84. if (b.content.messageSignature.messageDigest === undefined) {
  85. invalidValues.push('content.messageSignature.messageDigest');
  86. }
  87. else {
  88. if (b.content.messageSignature.messageDigest.digest.length === 0) {
  89. invalidValues.push('content.messageSignature.messageDigest.digest');
  90. }
  91. }
  92. if (b.content.messageSignature.signature.length === 0) {
  93. invalidValues.push('content.messageSignature.signature');
  94. }
  95. break;
  96. case 'dsseEnvelope':
  97. if (b.content.dsseEnvelope.payload.length === 0) {
  98. invalidValues.push('content.dsseEnvelope.payload');
  99. }
  100. if (b.content.dsseEnvelope.signatures.length !== 1) {
  101. invalidValues.push('content.dsseEnvelope.signatures');
  102. }
  103. else {
  104. if (b.content.dsseEnvelope.signatures[0].sig.length === 0) {
  105. invalidValues.push('content.dsseEnvelope.signatures[0].sig');
  106. }
  107. }
  108. break;
  109. }
  110. }
  111. // Verification material-related validation
  112. if (b.verificationMaterial === undefined) {
  113. invalidValues.push('verificationMaterial');
  114. }
  115. else {
  116. if (b.verificationMaterial.content === undefined) {
  117. invalidValues.push('verificationMaterial.content');
  118. }
  119. else {
  120. switch (b.verificationMaterial.content.$case) {
  121. case 'x509CertificateChain':
  122. if (b.verificationMaterial.content.x509CertificateChain.certificates
  123. .length === 0) {
  124. invalidValues.push('verificationMaterial.content.x509CertificateChain.certificates');
  125. }
  126. b.verificationMaterial.content.x509CertificateChain.certificates.forEach((cert, i) => {
  127. if (cert.rawBytes.length === 0) {
  128. invalidValues.push(`verificationMaterial.content.x509CertificateChain.certificates[${i}].rawBytes`);
  129. }
  130. });
  131. break;
  132. case 'certificate':
  133. if (b.verificationMaterial.content.certificate.rawBytes.length === 0) {
  134. invalidValues.push('verificationMaterial.content.certificate.rawBytes');
  135. }
  136. break;
  137. }
  138. }
  139. if (b.verificationMaterial.tlogEntries === undefined) {
  140. invalidValues.push('verificationMaterial.tlogEntries');
  141. }
  142. else {
  143. if (b.verificationMaterial.tlogEntries.length > 0) {
  144. b.verificationMaterial.tlogEntries.forEach((entry, i) => {
  145. if (entry.logId === undefined) {
  146. invalidValues.push(`verificationMaterial.tlogEntries[${i}].logId`);
  147. }
  148. if (entry.kindVersion === undefined) {
  149. invalidValues.push(`verificationMaterial.tlogEntries[${i}].kindVersion`);
  150. }
  151. });
  152. }
  153. }
  154. }
  155. return invalidValues;
  156. }
  157. // Necessary for V01 bundles
  158. function validateInclusionPromise(b) {
  159. const invalidValues = [];
  160. if (b.verificationMaterial &&
  161. b.verificationMaterial.tlogEntries?.length > 0) {
  162. b.verificationMaterial.tlogEntries.forEach((entry, i) => {
  163. if (entry.inclusionPromise === undefined) {
  164. invalidValues.push(`verificationMaterial.tlogEntries[${i}].inclusionPromise`);
  165. }
  166. });
  167. }
  168. return invalidValues;
  169. }
  170. // Necessary for V02 and later bundles
  171. function validateInclusionProof(b) {
  172. const invalidValues = [];
  173. if (b.verificationMaterial &&
  174. b.verificationMaterial.tlogEntries?.length > 0) {
  175. b.verificationMaterial.tlogEntries.forEach((entry, i) => {
  176. if (entry.inclusionProof === undefined) {
  177. invalidValues.push(`verificationMaterial.tlogEntries[${i}].inclusionProof`);
  178. }
  179. else {
  180. if (entry.inclusionProof.checkpoint === undefined) {
  181. invalidValues.push(`verificationMaterial.tlogEntries[${i}].inclusionProof.checkpoint`);
  182. }
  183. }
  184. });
  185. }
  186. return invalidValues;
  187. }
  188. // Necessary for V03 and later bundles
  189. function validateNoCertificateChain(b) {
  190. const invalidValues = [];
  191. /* istanbul ignore next */
  192. if (b.verificationMaterial?.content?.$case === 'x509CertificateChain') {
  193. invalidValues.push('verificationMaterial.content.$case');
  194. }
  195. return invalidValues;
  196. }