Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ”’ Re-check host blocklist on redirects and in status-check

Alicia Sykes5f013449f929
diff --git a/services/cors-proxy.js b/services/cors-proxy.js
index fedc18d8..e90ac2f2 100644
--- a/services/cors-proxy.js
+++ b/services/cors-proxy.js
@@ -5,19 +5,7 @@
*/
const request = require('./request');
-
-// List of hosts to disallow by default, for cloud instances
-// Covers AWS, Azure, GCP, DO, Hetzner, Oracle, etc on IPv4/6
-const BLOCKED_HOSTS = new Set([
- '169.254.169.254',
- '::ffff:a9fe:a9fe',
- 'fd00:ec2::254',
- 'metadata.google.internal',
- '100.100.100.200',
-]);
-
-// Operator escape hatch, set this env var to bypass all proxy restrictions
-const restrictionsDisabled = !!process.env.DANGEROUSLY_DISABLE_PROXY_RESTRICTIONS;
+const { validateTargetUrl } = request;
// If reference to env var is present, substitute for env var value if set
const PLACEHOLDER_RE = /\b(?:DASHY_|VITE_APP_|VUE_APP_)\w+/g;
@@ -43,31 +31,6 @@ const substituteEnv = (val) => {
return val;
};
-// Validate the target URL against scheme and host policies
-// Returns { ok: true } on success, or { ok: false, status, error } on rejection
-const validateTargetUrl = (raw) => {
- if (restrictionsDisabled) return { ok: true };
- let url;
- try { url = new URL(raw); } catch (e) {
- return { ok: false, status: 400, error: 'Target-URL is not a valid URL' };
- }
- if (url.protocol !== 'http:' && url.protocol !== 'https:') {
- return { ok: false, status: 400, error: 'Target-URL must use http:// or https://' };
- }
- // url.hostname includes brackets for IPv6 (e.g. '[fd00:ec2::254]') - strip em
- const host = url.hostname.toLowerCase().replace(/^\[|\]$/g, '');
- if (BLOCKED_HOSTS.has(host)) {
- return {
- ok: false,
- status: 403,
- error: `Target-URL host '${host}' is blocked by the CORS proxy. `
- + 'This address is reserved for cloud instance metadata services. '
- + 'To bypass, set DANGEROUSLY_DISABLE_PROXY_RESTRICTIONS=true.',
- };
- }
- return { ok: true };
-};
-
const handler = (req, res) => {
// Apply allow-all response headers
res.header('Access-Control-Allow-Origin', '*');
@@ -115,6 +78,7 @@ const handler = (req, res) => {
headers,
timeout: 30000,
maxResponseSize: 10 * 1024 * 1024, // 10 MB
+ validateUrl: validateTargetUrl,
};
// Make the request, and respond with result
diff --git a/services/request.js b/services/request.js
index 68a0c245..e8dd1611 100644
--- a/services/request.js
+++ b/services/request.js
@@ -13,6 +13,44 @@ const https = require('https');
const zlib = require('zlib');
const { URL } = require('url');
+// List of hosts to disallow by default, for cloud instances
+// Covers AWS, Azure, GCP, DO, Hetzner, Oracle, etc on IPv4/6
+const BLOCKED_HOSTS = new Set([
+ '169.254.169.254',
+ '::ffff:a9fe:a9fe',
+ 'fd00:ec2::254',
+ 'metadata.google.internal',
+ '100.100.100.200',
+]);
+
+// Operator escape hatch, set this env var to bypass all proxy restrictions
+const restrictionsDisabled = !!process.env.DANGEROUSLY_DISABLE_PROXY_RESTRICTIONS;
+
+// Validate a target URL against scheme and host policies
+// Returns { ok: true } on success, or { ok: false, status, error } on rejection
+const validateTargetUrl = (raw) => {
+ if (restrictionsDisabled) return { ok: true };
+ let url;
+ try { url = new URL(raw); } catch (e) {
+ return { ok: false, status: 400, error: 'Target-URL is not a valid URL' };
+ }
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
+ return { ok: false, status: 400, error: 'Target-URL must use http:// or https://' };
+ }
+ // url.hostname includes brackets for IPv6 (e.g. '[fd00:ec2::254]') - strip em
+ const host = url.hostname.toLowerCase().replace(/^\[|\]$/g, '');
+ if (BLOCKED_HOSTS.has(host)) {
+ return {
+ ok: false,
+ status: 403,
+ error: `Target-URL host '${host}' is blocked by the CORS proxy. `
+ + 'This address is reserved for cloud instance metadata services. '
+ + 'To bypass, set DANGEROUSLY_DISABLE_PROXY_RESTRICTIONS=true.',
+ };
+ }
+ return { ok: true };
+};
+
class RequestError extends Error {
constructor(message, {
response, code, errno, timeout,
@@ -63,6 +101,7 @@ function request(config) {
timeout = 0,
maxResponseSize = 0,
httpsAgent,
+ validateUrl,
} = config;
return new Promise((resolve, reject) => {
@@ -75,6 +114,15 @@ function request(config) {
return;
}
+ // Re-apply the host policy on every hop, so redirects can't reach a blocked host
+ if (validateUrl) {
+ const verdict = validateUrl(targetUrl);
+ if (verdict && verdict.ok === false) {
+ reject(new RequestError(verdict.error, { code: 'EBLOCKED' }));
+ return;
+ }
+ }
+
const isHttps = parsed.protocol === 'https:';
const transport = isHttps ? https : http;
@@ -236,3 +284,4 @@ request.put = (url, data, config = {}) => request({ ...config, method: 'PUT', ur
module.exports = request;
module.exports.RequestError = RequestError;
+module.exports.validateTargetUrl = validateTargetUrl;
diff --git a/services/status-check.js b/services/status-check.js
index 5382cc74..47dd639c 100644
--- a/services/status-check.js
+++ b/services/status-check.js
@@ -4,6 +4,7 @@
* endpoint, and then resolve the response status code, time taken, and short message
*/
const request = require('./request');
+const { validateTargetUrl } = request;
/* Determines if successful from the HTTP response code */
const getResponseType = (code, validCodes) => {
@@ -39,6 +40,7 @@ const makeRequest = (url, options, render) => {
maxRedirects,
timeout: 10000,
httpsAgent: { rejectUnauthorized: !enableInsecure },
+ validateUrl: validateTargetUrl,
})
.then((response) => {
const statusCode = response.status;
diff --git a/tests/server/cors-proxy.test.js b/tests/server/cors-proxy.test.js
index d86f3f93..274c3d54 100644
--- a/tests/server/cors-proxy.test.js
+++ b/tests/server/cors-proxy.test.js
@@ -165,6 +165,17 @@ describe('CORS proxy error classification', () => {
expect(res.body.error.type).toBe('upstream_error');
});
+ it('blocks a redirect to a metadata host (does not follow it)', async () => {
+ handler = (req, res) => {
+ res.statusCode = 302;
+ res.setHeader('location', 'http://169.254.169.254/latest/meta-data/');
+ res.end();
+ };
+ const res = await request(app).get('/cors-proxy').set('Target-URL', targetUrl);
+ expect(res.body.error.code).toBe('EBLOCKED');
+ expect(res.body.error.message).toContain('blocked');
+ });
+
});
// Verify the request.js β†’ cors-proxy timeout contract end-to-end. The proxy