isomorphic-git 1.44.16 → 1.44.18

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.
@@ -207,37 +207,113 @@ async function request({
207
207
  // @ts-expect-error
208
208
  body = asyncIteratorToStream(body);
209
209
  }
210
+ const { followRedirects = true, maxRedirects = 10, ...options } = fetchOptions;
211
+ /** @type {RedirectableRequest} */
212
+ let next = { url, method: method.toUpperCase(), headers, body };
213
+ for (let redirects = 0; ; redirects++) {
214
+ const res = await send({
215
+ ...options,
216
+ ...next,
217
+ agent,
218
+ signal: signal ?? options.signal,
219
+ });
220
+ const location = res.headers.location;
221
+ if (
222
+ !followRedirects ||
223
+ res.statusCode < 300 ||
224
+ res.statusCode >= 400 ||
225
+ !location
226
+ ) {
227
+ return toResponse(res)
228
+ }
229
+ // Discard the redirect body. simple-get has already wrapped it in a
230
+ // decompressor, so a malformed body must not become an unhandled error.
231
+ res.on('error', () => {});
232
+ res.resume();
233
+ if (redirects >= maxRedirects) throw new Error('too many redirects')
234
+ next = redirectRequest(next, res.statusCode, location);
235
+ // simple-get builds a body from `form` on every call; like its own
236
+ // redirect handling, send it only with the first request.
237
+ delete options.form;
238
+ }
239
+ }
240
+
241
+ /**
242
+ * @typedef {Object} RedirectableRequest
243
+ * @property {string} url
244
+ * @property {string} method
245
+ * @property {Object<string, string>} headers
246
+ * @property {any} body
247
+ */
248
+
249
+ /**
250
+ * Follows a redirect the way simple-get did, except that credentials are scoped
251
+ * to an origin (scheme, host and port) rather than to a hostname, like curl,
252
+ * which canonical git uses. simple-get kept the Authorization header when a
253
+ * redirect changed only the port or the scheme.
254
+ *
255
+ * @param {RedirectableRequest} previous
256
+ * @param {number} statusCode
257
+ * @param {string} location
258
+ * @returns {RedirectableRequest}
259
+ */
260
+ function redirectRequest(previous, statusCode, location) {
261
+ const url = new URL(location, previous.url);
262
+ const sameOrigin = url.origin === new URL(previous.url).origin;
263
+ let { method, body } = previous;
264
+ const dropped = ['host'];
265
+ if (!sameOrigin) dropped.push('authorization', 'cookie');
266
+ // Like curl and RFC 7231 §6.4.4, a 303 always turns into a GET; a 301 or 302
267
+ // turns only a POST into one.
268
+ if (
269
+ (statusCode === 303 && method !== 'GET' && method !== 'HEAD') ||
270
+ (method === 'POST' && (statusCode === 301 || statusCode === 302))
271
+ ) {
272
+ method = 'GET';
273
+ body = undefined;
274
+ dropped.push('content-length', 'content-type');
275
+ } else if (body && !Buffer.isBuffer(body)) {
276
+ // A streamed body has been read by the first request and would be resent
277
+ // empty.
278
+ throw new Error(
279
+ `cannot follow a ${statusCode} redirect: the request body is a stream and cannot be replayed`
280
+ )
281
+ }
282
+ /** @type {Object<string, string>} */
283
+ const headers = {};
284
+ for (const [name, value] of Object.entries(previous.headers)) {
285
+ if (!dropped.includes(name.toLowerCase())) headers[name] = value;
286
+ }
287
+ return { url: url.href, method, headers, body }
288
+ }
289
+
290
+ /**
291
+ * @param {object} options
292
+ * @returns {Promise<any>}
293
+ */
294
+ function send(options) {
210
295
  return new Promise((resolve, reject) => {
211
- get(
212
- {
213
- ...fetchOptions,
214
- url,
215
- method,
216
- headers,
217
- agent,
218
- body,
219
- signal: signal ?? fetchOptions.signal,
220
- },
221
- (err, res) => {
222
- if (err) return reject(err)
223
- try {
224
- const iter = fromNodeStream(res);
225
- resolve({
226
- url: res.url,
227
- method: res.method,
228
- statusCode: res.statusCode,
229
- statusMessage: res.statusMessage,
230
- body: iter,
231
- headers: res.headers,
232
- });
233
- } catch (e) {
234
- reject(e);
235
- }
236
- }
296
+ get({ ...options, followRedirects: false }, (err, res) =>
297
+ err ? reject(err) : resolve(res)
237
298
  );
238
299
  })
239
300
  }
240
301
 
302
+ /**
303
+ * @param {any} res
304
+ * @returns {GitHttpResponse}
305
+ */
306
+ function toResponse(res) {
307
+ return {
308
+ url: res.url,
309
+ method: res.method,
310
+ statusCode: res.statusCode,
311
+ statusMessage: res.statusMessage,
312
+ body: fromNodeStream(res),
313
+ headers: res.headers,
314
+ }
315
+ }
316
+
241
317
  var index = { request };
242
318
 
243
319
  exports.default = index;
@@ -74,6 +74,14 @@ export type HttpFetch = (request: GitHttpRequest) => Promise<GitHttpResponse>;
74
74
  export type HttpClient = {
75
75
  request: HttpFetch;
76
76
  };
77
+ export type RedirectableRequest = {
78
+ url: string;
79
+ method: string;
80
+ headers: {
81
+ [x: string]: string;
82
+ };
83
+ body: any;
84
+ };
77
85
  declare namespace index {
78
86
  export { request };
79
87
  }
@@ -73,6 +73,14 @@ export type HttpFetch = (request: GitHttpRequest) => Promise<GitHttpResponse>;
73
73
  export type HttpClient = {
74
74
  request: HttpFetch;
75
75
  };
76
+ export type RedirectableRequest = {
77
+ url: string;
78
+ method: string;
79
+ headers: {
80
+ [x: string]: string;
81
+ };
82
+ body: any;
83
+ };
76
84
  declare namespace index {
77
85
  export { request };
78
86
  }
@@ -201,37 +201,113 @@ async function request({
201
201
  // @ts-expect-error
202
202
  body = asyncIteratorToStream(body);
203
203
  }
204
+ const { followRedirects = true, maxRedirects = 10, ...options } = fetchOptions;
205
+ /** @type {RedirectableRequest} */
206
+ let next = { url, method: method.toUpperCase(), headers, body };
207
+ for (let redirects = 0; ; redirects++) {
208
+ const res = await send({
209
+ ...options,
210
+ ...next,
211
+ agent,
212
+ signal: signal ?? options.signal,
213
+ });
214
+ const location = res.headers.location;
215
+ if (
216
+ !followRedirects ||
217
+ res.statusCode < 300 ||
218
+ res.statusCode >= 400 ||
219
+ !location
220
+ ) {
221
+ return toResponse(res)
222
+ }
223
+ // Discard the redirect body. simple-get has already wrapped it in a
224
+ // decompressor, so a malformed body must not become an unhandled error.
225
+ res.on('error', () => {});
226
+ res.resume();
227
+ if (redirects >= maxRedirects) throw new Error('too many redirects')
228
+ next = redirectRequest(next, res.statusCode, location);
229
+ // simple-get builds a body from `form` on every call; like its own
230
+ // redirect handling, send it only with the first request.
231
+ delete options.form;
232
+ }
233
+ }
234
+
235
+ /**
236
+ * @typedef {Object} RedirectableRequest
237
+ * @property {string} url
238
+ * @property {string} method
239
+ * @property {Object<string, string>} headers
240
+ * @property {any} body
241
+ */
242
+
243
+ /**
244
+ * Follows a redirect the way simple-get did, except that credentials are scoped
245
+ * to an origin (scheme, host and port) rather than to a hostname, like curl,
246
+ * which canonical git uses. simple-get kept the Authorization header when a
247
+ * redirect changed only the port or the scheme.
248
+ *
249
+ * @param {RedirectableRequest} previous
250
+ * @param {number} statusCode
251
+ * @param {string} location
252
+ * @returns {RedirectableRequest}
253
+ */
254
+ function redirectRequest(previous, statusCode, location) {
255
+ const url = new URL(location, previous.url);
256
+ const sameOrigin = url.origin === new URL(previous.url).origin;
257
+ let { method, body } = previous;
258
+ const dropped = ['host'];
259
+ if (!sameOrigin) dropped.push('authorization', 'cookie');
260
+ // Like curl and RFC 7231 §6.4.4, a 303 always turns into a GET; a 301 or 302
261
+ // turns only a POST into one.
262
+ if (
263
+ (statusCode === 303 && method !== 'GET' && method !== 'HEAD') ||
264
+ (method === 'POST' && (statusCode === 301 || statusCode === 302))
265
+ ) {
266
+ method = 'GET';
267
+ body = undefined;
268
+ dropped.push('content-length', 'content-type');
269
+ } else if (body && !Buffer.isBuffer(body)) {
270
+ // A streamed body has been read by the first request and would be resent
271
+ // empty.
272
+ throw new Error(
273
+ `cannot follow a ${statusCode} redirect: the request body is a stream and cannot be replayed`
274
+ )
275
+ }
276
+ /** @type {Object<string, string>} */
277
+ const headers = {};
278
+ for (const [name, value] of Object.entries(previous.headers)) {
279
+ if (!dropped.includes(name.toLowerCase())) headers[name] = value;
280
+ }
281
+ return { url: url.href, method, headers, body }
282
+ }
283
+
284
+ /**
285
+ * @param {object} options
286
+ * @returns {Promise<any>}
287
+ */
288
+ function send(options) {
204
289
  return new Promise((resolve, reject) => {
205
- get(
206
- {
207
- ...fetchOptions,
208
- url,
209
- method,
210
- headers,
211
- agent,
212
- body,
213
- signal: signal ?? fetchOptions.signal,
214
- },
215
- (err, res) => {
216
- if (err) return reject(err)
217
- try {
218
- const iter = fromNodeStream(res);
219
- resolve({
220
- url: res.url,
221
- method: res.method,
222
- statusCode: res.statusCode,
223
- statusMessage: res.statusMessage,
224
- body: iter,
225
- headers: res.headers,
226
- });
227
- } catch (e) {
228
- reject(e);
229
- }
230
- }
290
+ get({ ...options, followRedirects: false }, (err, res) =>
291
+ err ? reject(err) : resolve(res)
231
292
  );
232
293
  })
233
294
  }
234
295
 
296
+ /**
297
+ * @param {any} res
298
+ * @returns {GitHttpResponse}
299
+ */
300
+ function toResponse(res) {
301
+ return {
302
+ url: res.url,
303
+ method: res.method,
304
+ statusCode: res.statusCode,
305
+ statusMessage: res.statusMessage,
306
+ body: fromNodeStream(res),
307
+ headers: res.headers,
308
+ }
309
+ }
310
+
235
311
  var index = { request };
236
312
 
237
313
  export default index;
package/index.cjs CHANGED
@@ -2854,6 +2854,60 @@ function mode2type$1(mode) {
2854
2854
  throw new InternalError(`Unexpected GitTree entry mode: ${mode}`)
2855
2855
  }
2856
2856
 
2857
+ // Drop the trailing dots and spaces of a name, the way Win32 does. A backward
2858
+ // scan, because `/[. ]+$/` costs O(n^2) on a name that is a long run of dots
2859
+ // followed by anything else: the engine retries the run from every start offset,
2860
+ // and an entry name arrives off the wire with no length bound. Only the
2861
+ // reserved-name checks below see this value; the stored path keeps its own
2862
+ // spelling.
2863
+ function stripTrailingDotsAndSpaces(name) {
2864
+ let end = name.length;
2865
+ while (end > 0) {
2866
+ const c = name.charCodeAt(end - 1);
2867
+ if (c !== 46 /* . */ && c !== 32 /* space */) break
2868
+ end--;
2869
+ }
2870
+ return end === name.length ? name : name.slice(0, end)
2871
+ }
2872
+
2873
+ // Reject reserved entry names, matching git's verify_path(): path separators,
2874
+ // "." and ".." (which resolve outside the working directory), and ".git" (which git
2875
+ // disallows as a path component). Checks mirror git's is_ntfs_dotgit() and
2876
+ // is_hfs_dotgit(): case-insensitive, trailing dots/spaces stripped (NTFS),
2877
+ // NTFS 8.3 short name aliases (git~1..git~9), NTFS Alternate Data Streams, and
2878
+ // HFS+ ignorable Unicode characters stripped (zero-width joiners, directional
2879
+ // marks, BOM, etc.).
2880
+ //
2881
+ // Every entry name passes through here, whether it was parsed off the wire or
2882
+ // handed in as an object, so the same tree cannot be written and then refused on
2883
+ // the way back in.
2884
+ function assertSafeEntryPath(path) {
2885
+ const hfsClean = path.replace(
2886
+ /[\u200C-\u200F\u202A-\u202E\u206A-\u206F\uFEFF]/g,
2887
+ ''
2888
+ );
2889
+ // NTFS Alternate Data Streams are referenced as `<name>:<stream>:<type>`, and a
2890
+ // directory's default stream lets `.git::$INDEX_ALLOCATION` resolve to the `.git`
2891
+ // directory itself. git forbids *any* ADS, so only the portion before the first
2892
+ // colon names the actual entry we normalize and check.
2893
+ const ntfsClean = hfsClean.split(':')[0];
2894
+ const normalized = stripTrailingDotsAndSpaces(ntfsClean.toLowerCase());
2895
+ // Win32 drops the trailing dots and spaces of a path component, so every name
2896
+ // built only from dots and spaces aliases "." or "..", not just those two exact
2897
+ // spellings: `.. ` resolves to the parent directory. git rejects the whole class
2898
+ // in is_valid_win32_path(). `normalized` is empty for exactly that class, which
2899
+ // is why the test is on it rather than on another trailing-run pattern.
2900
+ if (
2901
+ path.includes('\\') ||
2902
+ path.includes('/') ||
2903
+ (normalized === '' && ntfsClean !== '') ||
2904
+ normalized === '.git' ||
2905
+ /^\.?git~[1-9]$/.test(normalized)
2906
+ ) {
2907
+ throw new UnsafeFilepathError(path)
2908
+ }
2909
+ }
2910
+
2857
2911
  function parseBuffer(buffer) {
2858
2912
  const _entries = [];
2859
2913
  let cursor = 0;
@@ -2874,34 +2928,7 @@ function parseBuffer(buffer) {
2874
2928
  if (mode === '40000') mode = '040000'; // makes it line up neater in printed output
2875
2929
  const type = mode2type$1(mode);
2876
2930
  const path = buffer.slice(space + 1, nullchar).toString('utf8');
2877
-
2878
- // Reject reserved entry names, matching git's verify_path(): path separators,
2879
- // "." and ".." (which resolve outside the working directory), and ".git" (which git
2880
- // disallows as a path component). Checks mirror git's is_ntfs_dotgit() and
2881
- // is_hfs_dotgit(): case-insensitive, trailing dots/spaces stripped (NTFS),
2882
- // NTFS 8.3 short name aliases (git~1..git~9), NTFS Alternate Data Streams, and
2883
- // HFS+ ignorable Unicode characters stripped (zero-width joiners, directional
2884
- // marks, BOM, etc.).
2885
- const hfsClean = path.replace(
2886
- /[\u200C-\u200F\u202A-\u202E\u206A-\u206F\uFEFF]/g,
2887
- ''
2888
- );
2889
- // NTFS Alternate Data Streams are referenced as `<name>:<stream>:<type>`, and a
2890
- // directory's default stream lets `.git::$INDEX_ALLOCATION` resolve to the `.git`
2891
- // directory itself. git forbids *any* ADS, so only the portion before the first
2892
- // colon names the actual entry we normalize and check.
2893
- const ntfsClean = hfsClean.split(':')[0];
2894
- const normalized = ntfsClean.toLowerCase().replace(/[. ]+$/, '');
2895
- if (
2896
- path.includes('\\') ||
2897
- path.includes('/') ||
2898
- hfsClean === '.' ||
2899
- hfsClean === '..' ||
2900
- normalized === '.git' ||
2901
- /^\.?git~[1-9]$/.test(normalized)
2902
- ) {
2903
- throw new UnsafeFilepathError(path)
2904
- }
2931
+ assertSafeEntryPath(path);
2905
2932
 
2906
2933
  const oid = buffer.slice(nullchar + 1, nullchar + 21).toString('hex');
2907
2934
  cursor = nullchar + 21;
@@ -2931,6 +2958,7 @@ function nudgeIntoShape(entry) {
2931
2958
  if (!entry.type) {
2932
2959
  entry.type = mode2type$1(entry.mode); // index
2933
2960
  }
2961
+ assertSafeEntryPath(entry.path);
2934
2962
  return entry
2935
2963
  }
2936
2964
 
@@ -10441,8 +10469,8 @@ function filterCapabilities(server, client) {
10441
10469
 
10442
10470
  const pkg = {
10443
10471
  name: 'isomorphic-git',
10444
- version: '1.44.16',
10445
- agent: 'git/isomorphic-git@1.44.16',
10472
+ version: '1.44.18',
10473
+ agent: 'git/isomorphic-git@1.44.18',
10446
10474
  };
10447
10475
 
10448
10476
  class FIFO {
package/index.js CHANGED
@@ -2848,6 +2848,60 @@ function mode2type$1(mode) {
2848
2848
  throw new InternalError(`Unexpected GitTree entry mode: ${mode}`)
2849
2849
  }
2850
2850
 
2851
+ // Drop the trailing dots and spaces of a name, the way Win32 does. A backward
2852
+ // scan, because `/[. ]+$/` costs O(n^2) on a name that is a long run of dots
2853
+ // followed by anything else: the engine retries the run from every start offset,
2854
+ // and an entry name arrives off the wire with no length bound. Only the
2855
+ // reserved-name checks below see this value; the stored path keeps its own
2856
+ // spelling.
2857
+ function stripTrailingDotsAndSpaces(name) {
2858
+ let end = name.length;
2859
+ while (end > 0) {
2860
+ const c = name.charCodeAt(end - 1);
2861
+ if (c !== 46 /* . */ && c !== 32 /* space */) break
2862
+ end--;
2863
+ }
2864
+ return end === name.length ? name : name.slice(0, end)
2865
+ }
2866
+
2867
+ // Reject reserved entry names, matching git's verify_path(): path separators,
2868
+ // "." and ".." (which resolve outside the working directory), and ".git" (which git
2869
+ // disallows as a path component). Checks mirror git's is_ntfs_dotgit() and
2870
+ // is_hfs_dotgit(): case-insensitive, trailing dots/spaces stripped (NTFS),
2871
+ // NTFS 8.3 short name aliases (git~1..git~9), NTFS Alternate Data Streams, and
2872
+ // HFS+ ignorable Unicode characters stripped (zero-width joiners, directional
2873
+ // marks, BOM, etc.).
2874
+ //
2875
+ // Every entry name passes through here, whether it was parsed off the wire or
2876
+ // handed in as an object, so the same tree cannot be written and then refused on
2877
+ // the way back in.
2878
+ function assertSafeEntryPath(path) {
2879
+ const hfsClean = path.replace(
2880
+ /[\u200C-\u200F\u202A-\u202E\u206A-\u206F\uFEFF]/g,
2881
+ ''
2882
+ );
2883
+ // NTFS Alternate Data Streams are referenced as `<name>:<stream>:<type>`, and a
2884
+ // directory's default stream lets `.git::$INDEX_ALLOCATION` resolve to the `.git`
2885
+ // directory itself. git forbids *any* ADS, so only the portion before the first
2886
+ // colon names the actual entry we normalize and check.
2887
+ const ntfsClean = hfsClean.split(':')[0];
2888
+ const normalized = stripTrailingDotsAndSpaces(ntfsClean.toLowerCase());
2889
+ // Win32 drops the trailing dots and spaces of a path component, so every name
2890
+ // built only from dots and spaces aliases "." or "..", not just those two exact
2891
+ // spellings: `.. ` resolves to the parent directory. git rejects the whole class
2892
+ // in is_valid_win32_path(). `normalized` is empty for exactly that class, which
2893
+ // is why the test is on it rather than on another trailing-run pattern.
2894
+ if (
2895
+ path.includes('\\') ||
2896
+ path.includes('/') ||
2897
+ (normalized === '' && ntfsClean !== '') ||
2898
+ normalized === '.git' ||
2899
+ /^\.?git~[1-9]$/.test(normalized)
2900
+ ) {
2901
+ throw new UnsafeFilepathError(path)
2902
+ }
2903
+ }
2904
+
2851
2905
  function parseBuffer(buffer) {
2852
2906
  const _entries = [];
2853
2907
  let cursor = 0;
@@ -2868,34 +2922,7 @@ function parseBuffer(buffer) {
2868
2922
  if (mode === '40000') mode = '040000'; // makes it line up neater in printed output
2869
2923
  const type = mode2type$1(mode);
2870
2924
  const path = buffer.slice(space + 1, nullchar).toString('utf8');
2871
-
2872
- // Reject reserved entry names, matching git's verify_path(): path separators,
2873
- // "." and ".." (which resolve outside the working directory), and ".git" (which git
2874
- // disallows as a path component). Checks mirror git's is_ntfs_dotgit() and
2875
- // is_hfs_dotgit(): case-insensitive, trailing dots/spaces stripped (NTFS),
2876
- // NTFS 8.3 short name aliases (git~1..git~9), NTFS Alternate Data Streams, and
2877
- // HFS+ ignorable Unicode characters stripped (zero-width joiners, directional
2878
- // marks, BOM, etc.).
2879
- const hfsClean = path.replace(
2880
- /[\u200C-\u200F\u202A-\u202E\u206A-\u206F\uFEFF]/g,
2881
- ''
2882
- );
2883
- // NTFS Alternate Data Streams are referenced as `<name>:<stream>:<type>`, and a
2884
- // directory's default stream lets `.git::$INDEX_ALLOCATION` resolve to the `.git`
2885
- // directory itself. git forbids *any* ADS, so only the portion before the first
2886
- // colon names the actual entry we normalize and check.
2887
- const ntfsClean = hfsClean.split(':')[0];
2888
- const normalized = ntfsClean.toLowerCase().replace(/[. ]+$/, '');
2889
- if (
2890
- path.includes('\\') ||
2891
- path.includes('/') ||
2892
- hfsClean === '.' ||
2893
- hfsClean === '..' ||
2894
- normalized === '.git' ||
2895
- /^\.?git~[1-9]$/.test(normalized)
2896
- ) {
2897
- throw new UnsafeFilepathError(path)
2898
- }
2925
+ assertSafeEntryPath(path);
2899
2926
 
2900
2927
  const oid = buffer.slice(nullchar + 1, nullchar + 21).toString('hex');
2901
2928
  cursor = nullchar + 21;
@@ -2925,6 +2952,7 @@ function nudgeIntoShape(entry) {
2925
2952
  if (!entry.type) {
2926
2953
  entry.type = mode2type$1(entry.mode); // index
2927
2954
  }
2955
+ assertSafeEntryPath(entry.path);
2928
2956
  return entry
2929
2957
  }
2930
2958
 
@@ -10429,8 +10457,8 @@ function filterCapabilities(server, client) {
10429
10457
 
10430
10458
  const pkg = {
10431
10459
  name: 'isomorphic-git',
10432
- version: '1.44.16',
10433
- agent: 'git/isomorphic-git@1.44.16',
10460
+ version: '1.44.18',
10461
+ agent: 'git/isomorphic-git@1.44.18',
10434
10462
  };
10435
10463
 
10436
10464
  class FIFO {