Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

fix(installer): reject cross-origin redirects (#794)

Priyanshu Agrawal96d16d0718e4
diff --git a/scripts/install.js b/scripts/install.js
index 2b86296b7..fe824fcbc 100644
--- a/scripts/install.js
+++ b/scripts/install.js
@@ -20,6 +20,7 @@ const fs = require("fs");
const path = require("path");
const os = require("os");
const https = require("https");
+const { URL } = require("url");
// ── Config ────────────────────────────────────────────────────
const REPO = "microsoft/power-platform-skills";
@@ -172,16 +173,54 @@ function run(cmd, args = [], opts = {}) {
}
}
+const MAX_REDIRECTS = 3;
+
function httpsGet(url) {
return new Promise((resolve, reject) => {
- const request = (target) => {
+ let initialUrl;
+ try {
+ initialUrl = new URL(url);
+ } catch {
+ reject(new Error("Invalid HTTPS URL"));
+ return;
+ }
+ if (initialUrl.protocol !== "https:") {
+ reject(new Error("Only HTTPS URLs are allowed"));
+ return;
+ }
+
+ // Keep fetched manifests and metadata on their configured origin; bounded
+ // redirects also prevent an origin from trapping the installer in a loop.
+ const expectedOrigin = initialUrl.origin;
+ const request = (target, redirectCount = 0) => {
https
- .get(target, { headers: { "User-Agent": "power-platform-skills-installer" } }, (res) => {
+ .get(target.href, { headers: { "User-Agent": "power-platform-skills-installer" } }, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
- return request(res.headers.location);
+ let nextUrl;
+ try {
+ nextUrl = new URL(res.headers.location, target);
+ } catch {
+ res.resume();
+ reject(new Error("Invalid redirect URL"));
+ return;
+ }
+ if (redirectCount >= MAX_REDIRECTS) {
+ res.resume();
+ reject(new Error(`Too many redirects from ${expectedOrigin}`));
+ return;
+ }
+ if (nextUrl.protocol !== "https:" || nextUrl.origin !== expectedOrigin
+ || nextUrl.username || nextUrl.password) {
+ res.resume();
+ reject(new Error(`Refusing redirect to ${nextUrl.origin}`));
+ return;
+ }
+ res.resume();
+ return request(nextUrl, redirectCount + 1);
}
if (res.statusCode !== 200) {
- return reject(new Error(`HTTP ${res.statusCode} from ${target}`));
+ res.resume();
+ return reject(new Error(`HTTP ${res.statusCode} from ${target.href}`));
}
let data = "";
res.on("data", (chunk) => (data += chunk));
@@ -189,7 +228,7 @@ function httpsGet(url) {
})
.on("error", reject);
};
- request(url);
+ request(initialUrl);
});
}
diff --git a/scripts/tests/install.test.js b/scripts/tests/install.test.js
index 8a47b2a70..61a782d7c 100644
--- a/scripts/tests/install.test.js
+++ b/scripts/tests/install.test.js
@@ -53,6 +53,7 @@ function loadInstaller({
const module = { id: 'installer-test', exports: {} };
const requireMock = (name) => {
if (name === 'path') return paths;
+ if (name === 'url') return { URL };
if (name === 'os') return { homedir: () => paths.join(root, 'home') };
if (name === 'fs') return {
constants: fs.constants,
@@ -78,12 +79,17 @@ function loadInstaller({
};
if (name === 'https') return {
get: (url, options, callback) => {
- requests.push(url);
+ const target = String(url);
+ requests.push(target);
const res = new EventEmitter();
- res.statusCode = Object.hasOwn(remote, url) ? 200 : 404;
+ const response = remote[target];
+ const isRedirect = response && typeof response === 'object';
+ res.statusCode = isRedirect ? response.statusCode : Object.hasOwn(remote, target) ? 200 : 404;
+ res.headers = isRedirect ? { location: response.location } : {};
+ res.resume = () => {};
callback(res);
- queueMicrotask(() => {
- res.emit('data', remote[url]);
+ if (!isRedirect) queueMicrotask(() => {
+ res.emit('data', response);
res.emit('end');
});
return new EventEmitter();
@@ -208,6 +214,45 @@ for (const relative of ['marketplace.json', '.plugin/marketplace.json', '.claude
});
}
+test('downloaded manifests reject redirects outside the configured HTTPS origin', async () => {
+ const remote = Object.fromEntries(
+ ['marketplace.json', '.plugin/marketplace.json', '.claude-plugin/marketplace.json']
+ .map((relative) => [RAW_ROOT + relative, {
+ statusCode: 302,
+ location: 'https://redirect-target.invalid/received-location',
+ }])
+ );
+ const fixture = loadInstaller({ remote });
+ await assert.rejects(fixture.installer.loadMarketplace(), /Refusing redirect to https:\/\/redirect-target\.invalid/);
+ assert.deepEqual(fixture.requests, [
+ RAW_ROOT + 'marketplace.json',
+ RAW_ROOT + '.plugin/marketplace.json',
+ RAW_ROOT + '.claude-plugin/marketplace.json',
+ ]);
+});
+
+test('downloaded manifests follow bounded redirects within the configured HTTPS origin', async () => {
+ const redirected = RAW_ROOT + 'marketplace.json?source=redirect';
+ const fixture = loadInstaller({
+ remote: {
+ [RAW_ROOT + 'marketplace.json']: { statusCode: 302, location: redirected },
+ [redirected]: JSON.stringify(manifest()),
+ },
+ });
+ assert.deepEqual(JSON.parse(JSON.stringify(await fixture.installer.loadMarketplace())), manifest());
+ assert.deepEqual(fixture.requests, [RAW_ROOT + 'marketplace.json', redirected]);
+});
+
+test('downloaded manifests stop redirect loops after the configured limit', async () => {
+ const remote = Object.fromEntries(
+ ['marketplace.json', '.plugin/marketplace.json', '.claude-plugin/marketplace.json']
+ .map((relative) => [RAW_ROOT + relative, { statusCode: 302, location: RAW_ROOT + relative }])
+ );
+ const fixture = loadInstaller({ remote });
+ await assert.rejects(fixture.installer.loadMarketplace(), /Too many redirects/);
+ assert.equal(fixture.requests.length, 12);
+});
+
test('invalid local manifests are reported and never returned instead of a valid fallback', async () => {
const fixture = loadInstaller({
files: { '/project/marketplace.json': JSON.stringify(manifest([INJECTION_NAMES[0]])) },