@azure/identity 4.2.0-alpha.20240425.2 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -12,6 +12,7 @@ var fs = require('fs');
12
12
  var os = require('os');
13
13
  var path = require('path');
14
14
  var msalCommon = require('@azure/msal-node');
15
+ var fs$1 = require('node:fs');
15
16
  var https = require('https');
16
17
  var promises = require('fs/promises');
17
18
  var child_process = require('child_process');
@@ -44,7 +45,7 @@ var child_process__namespace = /*#__PURE__*/_interopNamespaceDefault(child_proce
44
45
  /**
45
46
  * Current version of the `@azure/identity` package.
46
47
  */
47
- const SDK_VERSION = `4.2.0`;
48
+ const SDK_VERSION = `4.2.1`;
48
49
  /**
49
50
  * The default client ID for authentication
50
51
  * @internal
@@ -1175,18 +1176,6 @@ function prepareRequestOptions$3(scopes, clientId, resourceId) {
1175
1176
  }),
1176
1177
  });
1177
1178
  }
1178
- /**
1179
- * Retrieves the file contents at the given path using promises.
1180
- * Useful since `fs`'s readFileSync locks the thread, and to avoid extra dependencies.
1181
- */
1182
- function readFileAsync$1(path, options) {
1183
- return new Promise((resolve, reject) => fs.readFile(path, options, (err, data) => {
1184
- if (err) {
1185
- reject(err);
1186
- }
1187
- resolve(data);
1188
- }));
1189
- }
1190
1179
  /**
1191
1180
  * Does a request to the authentication provider that results in a file path.
1192
1181
  */
@@ -1207,6 +1196,43 @@ async function filePathRequest(identityClient, requestPrepareOptions) {
1207
1196
  throw Error(`Invalid www-authenticate header format: ${authHeader}`);
1208
1197
  }
1209
1198
  }
1199
+ function platformToFilePath() {
1200
+ switch (process.platform) {
1201
+ case "win32":
1202
+ if (!process.env.PROGRAMDATA) {
1203
+ throw new Error(`${msiName$4}: PROGRAMDATA environment variable has no value.`);
1204
+ }
1205
+ return `${process.env.PROGRAMDATA}\\AzureConnectedMachineAgent\\Tokens`;
1206
+ case "linux":
1207
+ return "/var/opt/azcmagent/tokens";
1208
+ default:
1209
+ throw new Error(`${msiName$4}: Unsupported platform ${process.platform}.`);
1210
+ }
1211
+ }
1212
+ /**
1213
+ * Validates that a given Azure Arc MSI file path is valid for use.
1214
+ *
1215
+ * A valid file will:
1216
+ * 1. Be in the expected path for the current platform.
1217
+ * 2. Have a `.key` extension.
1218
+ * 3. Be at most 4096 bytes in size.
1219
+ */
1220
+ function validateKeyFile(filePath) {
1221
+ if (!filePath) {
1222
+ throw new Error(`${msiName$4}: Failed to find the token file.`);
1223
+ }
1224
+ if (!filePath.endsWith(".key")) {
1225
+ throw new Error(`${msiName$4}: unexpected file path from HIMDS service: ${filePath}.`);
1226
+ }
1227
+ const expectedPath = platformToFilePath();
1228
+ if (!filePath.startsWith(expectedPath)) {
1229
+ throw new Error(`${msiName$4}: unexpected file path from HIMDS service: ${filePath}.`);
1230
+ }
1231
+ const stats = fs$1.statSync(filePath);
1232
+ if (stats.size > 4096) {
1233
+ throw new Error(`${msiName$4}: The file at ${filePath} is larger than expected at ${stats.size} bytes.`);
1234
+ }
1235
+ }
1210
1236
  /**
1211
1237
  * Defines how to determine whether the Azure Arc MSI is available, and also how to retrieve a token from the Azure Arc MSI.
1212
1238
  */
@@ -1236,10 +1262,8 @@ const arcMsi = {
1236
1262
  logger$l.info(`${msiName$4}: Authenticating.`);
1237
1263
  const requestOptions = Object.assign(Object.assign({ disableJsonStringifyOnBody: true, deserializationMapper: undefined, abortSignal: getTokenOptions.abortSignal }, prepareRequestOptions$3(scopes, clientId, resourceId)), { allowInsecureConnection: true });
1238
1264
  const filePath = await filePathRequest(identityClient, requestOptions);
1239
- if (!filePath) {
1240
- throw new Error(`${msiName$4}: Failed to find the token file.`);
1241
- }
1242
- const key = await readFileAsync$1(filePath, { encoding: "utf-8" });
1265
+ validateKeyFile(filePath);
1266
+ const key = await fs$1.promises.readFile(filePath, { encoding: "utf-8" });
1243
1267
  (_a = requestOptions.headers) === null || _a === void 0 ? void 0 : _a.set("Authorization", `Basic ${key}`);
1244
1268
  const request = coreRestPipeline.createPipelineRequest(Object.assign(Object.assign({}, requestOptions), {
1245
1269
  // Generally, MSI endpoints use the HTTP protocol, without transport layer security (TLS).