updater.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.Updater = void 0;
  30. const models_1 = require("@tufjs/models");
  31. const debug_1 = __importDefault(require("debug"));
  32. const fs = __importStar(require("fs"));
  33. const path = __importStar(require("path"));
  34. const config_1 = require("./config");
  35. const error_1 = require("./error");
  36. const fetcher_1 = require("./fetcher");
  37. const store_1 = require("./store");
  38. const url = __importStar(require("./utils/url"));
  39. const log = (0, debug_1.default)('tuf:cache');
  40. class Updater {
  41. constructor(options) {
  42. const { metadataDir, metadataBaseUrl, targetDir, targetBaseUrl, fetcher, config, } = options;
  43. this.dir = metadataDir;
  44. this.metadataBaseUrl = metadataBaseUrl;
  45. this.targetDir = targetDir;
  46. this.targetBaseUrl = targetBaseUrl;
  47. this.forceCache = options.forceCache ?? false;
  48. const data = this.loadLocalMetadata(models_1.MetadataKind.Root);
  49. this.trustedSet = new store_1.TrustedMetadataStore(data);
  50. this.config = { ...config_1.defaultConfig, ...config };
  51. this.fetcher =
  52. fetcher ||
  53. new fetcher_1.DefaultFetcher({
  54. timeout: this.config.fetchTimeout,
  55. retry: this.config.fetchRetries ?? this.config.fetchRetry,
  56. });
  57. }
  58. // refresh and load the metadata before downloading the target
  59. // refresh should be called once after the client is initialized
  60. async refresh() {
  61. // If forceCache is true, try to load the timestamp from local storage
  62. // without fetching it from the remote. Otherwise, load the root and
  63. // timestamp from the remote per the TUF spec.
  64. if (this.forceCache) {
  65. // If anything fails, load the root and timestamp from the remote. This
  66. // should cover any situation where the local metadata is corrupted or
  67. // expired.
  68. try {
  69. await this.loadTimestamp({ checkRemote: false });
  70. }
  71. catch (error) {
  72. await this.loadRoot();
  73. await this.loadTimestamp();
  74. }
  75. }
  76. else {
  77. await this.loadRoot();
  78. await this.loadTimestamp();
  79. }
  80. await this.loadSnapshot();
  81. await this.loadTargets(models_1.MetadataKind.Targets, models_1.MetadataKind.Root);
  82. }
  83. // Returns the TargetFile instance with information for the given target path.
  84. //
  85. // Implicitly calls refresh if it hasn't already been called.
  86. async getTargetInfo(targetPath) {
  87. if (!this.trustedSet.targets) {
  88. await this.refresh();
  89. }
  90. return this.preorderDepthFirstWalk(targetPath);
  91. }
  92. async downloadTarget(targetInfo, filePath, targetBaseUrl) {
  93. const targetPath = filePath || this.generateTargetPath(targetInfo);
  94. if (!targetBaseUrl) {
  95. if (!this.targetBaseUrl) {
  96. throw new error_1.ValueError('Target base URL not set');
  97. }
  98. targetBaseUrl = this.targetBaseUrl;
  99. }
  100. let targetFilePath = targetInfo.path;
  101. const consistentSnapshot = this.trustedSet.root.signed.consistentSnapshot;
  102. if (consistentSnapshot && this.config.prefixTargetsWithHash) {
  103. const hashes = Object.values(targetInfo.hashes);
  104. const { dir, base } = path.parse(targetFilePath);
  105. const filename = `${hashes[0]}.${base}`;
  106. targetFilePath = dir ? `${dir}/${filename}` : filename;
  107. }
  108. const targetUrl = url.join(targetBaseUrl, targetFilePath);
  109. // Client workflow 5.7.3: download target file
  110. await this.fetcher.downloadFile(targetUrl, targetInfo.length, async (fileName) => {
  111. // Verify hashes and length of downloaded file
  112. await targetInfo.verify(fs.createReadStream(fileName));
  113. // Copy file to target path
  114. log('WRITE %s', targetPath);
  115. fs.copyFileSync(fileName, targetPath);
  116. });
  117. return targetPath;
  118. }
  119. async findCachedTarget(targetInfo, filePath) {
  120. if (!filePath) {
  121. filePath = this.generateTargetPath(targetInfo);
  122. }
  123. try {
  124. if (fs.existsSync(filePath)) {
  125. await targetInfo.verify(fs.createReadStream(filePath));
  126. return filePath;
  127. }
  128. }
  129. catch (error) {
  130. return; // File not found
  131. }
  132. return; // File not found
  133. }
  134. loadLocalMetadata(fileName) {
  135. const filePath = path.join(this.dir, `${fileName}.json`);
  136. log('READ %s', filePath);
  137. return fs.readFileSync(filePath);
  138. }
  139. // Sequentially load and persist on local disk every newer root metadata
  140. // version available on the remote.
  141. // Client workflow 5.3: update root role
  142. async loadRoot() {
  143. // Client workflow 5.3.2: version of trusted root metadata file
  144. const rootVersion = this.trustedSet.root.signed.version;
  145. const lowerBound = rootVersion + 1;
  146. const upperBound = lowerBound + this.config.maxRootRotations;
  147. for (let version = lowerBound; version < upperBound; version++) {
  148. const rootUrl = url.join(this.metadataBaseUrl, `${version}.root.json`);
  149. try {
  150. // Client workflow 5.3.3: download new root metadata file
  151. const bytesData = await this.fetcher.downloadBytes(rootUrl, this.config.rootMaxLength);
  152. // Client workflow 5.3.4 - 5.4.7
  153. this.trustedSet.updateRoot(bytesData);
  154. // Client workflow 5.3.8: persist root metadata file
  155. this.persistMetadata(models_1.MetadataKind.Root, bytesData);
  156. }
  157. catch (error) {
  158. if (error instanceof error_1.DownloadHTTPError) {
  159. // 404/403 means current root is newest available
  160. if ([403, 404].includes(error.statusCode)) {
  161. break;
  162. }
  163. }
  164. throw error;
  165. }
  166. }
  167. }
  168. // Load local and remote timestamp metadata.
  169. // Client workflow 5.4: update timestamp role
  170. async loadTimestamp({ checkRemote } = { checkRemote: true }) {
  171. // Load local and remote timestamp metadata
  172. try {
  173. const data = this.loadLocalMetadata(models_1.MetadataKind.Timestamp);
  174. this.trustedSet.updateTimestamp(data);
  175. // If checkRemote is disabled, return here to avoid fetching the remote
  176. // timestamp metadata.
  177. if (!checkRemote) {
  178. return;
  179. }
  180. }
  181. catch (error) {
  182. // continue
  183. }
  184. //Load from remote (whether local load succeeded or not)
  185. const timestampUrl = url.join(this.metadataBaseUrl, 'timestamp.json');
  186. // Client workflow 5.4.1: download timestamp metadata file
  187. const bytesData = await this.fetcher.downloadBytes(timestampUrl, this.config.timestampMaxLength);
  188. try {
  189. // Client workflow 5.4.2 - 5.4.4
  190. this.trustedSet.updateTimestamp(bytesData);
  191. }
  192. catch (error) {
  193. // If new timestamp version is same as current, discardd the new one.
  194. // This is normal and should NOT raise an error.
  195. if (error instanceof error_1.EqualVersionError) {
  196. return;
  197. }
  198. // Re-raise any other error
  199. throw error;
  200. }
  201. // Client workflow 5.4.5: persist timestamp metadata
  202. this.persistMetadata(models_1.MetadataKind.Timestamp, bytesData);
  203. }
  204. // Load local and remote snapshot metadata.
  205. // Client workflow 5.5: update snapshot role
  206. async loadSnapshot() {
  207. //Load local (and if needed remote) snapshot metadata
  208. try {
  209. const data = this.loadLocalMetadata(models_1.MetadataKind.Snapshot);
  210. this.trustedSet.updateSnapshot(data, true);
  211. }
  212. catch (error) {
  213. if (!this.trustedSet.timestamp) {
  214. throw new ReferenceError('No timestamp metadata');
  215. }
  216. const snapshotMeta = this.trustedSet.timestamp.signed.snapshotMeta;
  217. const maxLength = snapshotMeta.length || this.config.snapshotMaxLength;
  218. const version = this.trustedSet.root.signed.consistentSnapshot
  219. ? snapshotMeta.version
  220. : undefined;
  221. const snapshotUrl = url.join(this.metadataBaseUrl, version ? `${version}.snapshot.json` : 'snapshot.json');
  222. try {
  223. // Client workflow 5.5.1: download snapshot metadata file
  224. const bytesData = await this.fetcher.downloadBytes(snapshotUrl, maxLength);
  225. // Client workflow 5.5.2 - 5.5.6
  226. this.trustedSet.updateSnapshot(bytesData);
  227. // Client workflow 5.5.7: persist snapshot metadata file
  228. this.persistMetadata(models_1.MetadataKind.Snapshot, bytesData);
  229. }
  230. catch (error) {
  231. throw new error_1.RuntimeError(`Unable to load snapshot metadata error ${error}`);
  232. }
  233. }
  234. }
  235. // Load local and remote targets metadata.
  236. // Client workflow 5.6: update targets role
  237. async loadTargets(role, parentRole) {
  238. if (this.trustedSet.getRole(role)) {
  239. return this.trustedSet.getRole(role);
  240. }
  241. try {
  242. const buffer = this.loadLocalMetadata(role);
  243. this.trustedSet.updateDelegatedTargets(buffer, role, parentRole);
  244. }
  245. catch (error) {
  246. // Local 'role' does not exist or is invalid: update from remote
  247. if (!this.trustedSet.snapshot) {
  248. throw new ReferenceError('No snapshot metadata');
  249. }
  250. const metaInfo = this.trustedSet.snapshot.signed.meta[`${role}.json`];
  251. // TODO: use length for fetching
  252. const maxLength = metaInfo.length || this.config.targetsMaxLength;
  253. const version = this.trustedSet.root.signed.consistentSnapshot
  254. ? metaInfo.version
  255. : undefined;
  256. const encodedRole = encodeURIComponent(role);
  257. const metadataUrl = url.join(this.metadataBaseUrl, version ? `${version}.${encodedRole}.json` : `${encodedRole}.json`);
  258. try {
  259. // Client workflow 5.6.1: download targets metadata file
  260. const bytesData = await this.fetcher.downloadBytes(metadataUrl, maxLength);
  261. // Client workflow 5.6.2 - 5.6.6
  262. this.trustedSet.updateDelegatedTargets(bytesData, role, parentRole);
  263. // Client workflow 5.6.7: persist targets metadata file
  264. this.persistMetadata(role, bytesData);
  265. }
  266. catch (error) {
  267. throw new error_1.RuntimeError(`Unable to load targets error ${error}`);
  268. }
  269. }
  270. return this.trustedSet.getRole(role);
  271. }
  272. async preorderDepthFirstWalk(targetPath) {
  273. // Interrogates the tree of target delegations in order of appearance
  274. // (which implicitly order trustworthiness), and returns the matching
  275. // target found in the most trusted role.
  276. // List of delegations to be interrogated. A (role, parent role) pair
  277. // is needed to load and verify the delegated targets metadata.
  278. const delegationsToVisit = [
  279. {
  280. roleName: models_1.MetadataKind.Targets,
  281. parentRoleName: models_1.MetadataKind.Root,
  282. },
  283. ];
  284. const visitedRoleNames = new Set();
  285. // Client workflow 5.6.7: preorder depth-first traversal of the graph of
  286. // target delegations
  287. while (visitedRoleNames.size <= this.config.maxDelegations &&
  288. delegationsToVisit.length > 0) {
  289. // Pop the role name from the top of the stack.
  290. const { roleName, parentRoleName } = delegationsToVisit.pop();
  291. // Skip any visited current role to prevent cycles.
  292. // Client workflow 5.6.7.1: skip already-visited roles
  293. if (visitedRoleNames.has(roleName)) {
  294. continue;
  295. }
  296. // The metadata for 'role_name' must be downloaded/updated before
  297. // its targets, delegations, and child roles can be inspected.
  298. const targets = (await this.loadTargets(roleName, parentRoleName))
  299. ?.signed;
  300. if (!targets) {
  301. continue;
  302. }
  303. const target = targets.targets?.[targetPath];
  304. if (target) {
  305. return target;
  306. }
  307. // After preorder check, add current role to set of visited roles.
  308. visitedRoleNames.add(roleName);
  309. if (targets.delegations) {
  310. const childRolesToVisit = [];
  311. // NOTE: This may be a slow operation if there are many delegated roles.
  312. const rolesForTarget = targets.delegations.rolesForTarget(targetPath);
  313. for (const { role: childName, terminating } of rolesForTarget) {
  314. childRolesToVisit.push({
  315. roleName: childName,
  316. parentRoleName: roleName,
  317. });
  318. // Client workflow 5.6.7.2.1
  319. if (terminating) {
  320. delegationsToVisit.splice(0); // empty the array
  321. break;
  322. }
  323. }
  324. childRolesToVisit.reverse();
  325. delegationsToVisit.push(...childRolesToVisit);
  326. }
  327. }
  328. return; // no matching target found
  329. }
  330. generateTargetPath(targetInfo) {
  331. if (!this.targetDir) {
  332. throw new error_1.ValueError('Target directory not set');
  333. }
  334. // URL encode target path
  335. const filePath = encodeURIComponent(targetInfo.path);
  336. return path.join(this.targetDir, filePath);
  337. }
  338. persistMetadata(metaDataName, bytesData) {
  339. const encodedName = encodeURIComponent(metaDataName);
  340. try {
  341. const filePath = path.join(this.dir, `${encodedName}.json`);
  342. log('WRITE %s', filePath);
  343. fs.writeFileSync(filePath, bytesData.toString('utf8'));
  344. }
  345. catch (error) {
  346. throw new error_1.PersistError(`Failed to persist metadata ${encodedName} error: ${error}`);
  347. }
  348. }
  349. }
  350. exports.Updater = Updater;