Skip to content

Node.js based forum software built for the modern web

fix(activitypub): use request protocol for rfc 9421 target uri

Julian Lam203e9a914383
diff --git a/src/activitypub/signatures.js b/src/activitypub/signatures.js
index 7f2b7da06f..ddbe1d9d6a 100644
--- a/src/activitypub/signatures.js
+++ b/src/activitypub/signatures.js
@@ -283,13 +283,24 @@ function getRequestUrl(req, { useHostHeader = false } = {}) {
// also succeeds when the dialed host differs from the configured URL
// (reverse proxies, www vs non-www, port changes)
if (useHostHeader && req.headers && req.headers.host) {
- const { protocol } = nconf.get('url_parsed') || { protocol: 'https:' };
+ const protocol = getRequestProtocol(req);
origin = `${protocol}//${req.headers.host}`;
}
return new URL(requestPath, origin).href;
}
+// Determines the scheme of the dialed request. The Host header carries no
+// scheme, so use the request's own protocol (express's req.protocol honors
+// trust proxy and X-Forwarded-Proto), falling back to the configured URL
+function getRequestProtocol(req) {
+ if (req && typeof req.protocol === 'string' && (req.protocol === 'http' || req.protocol === 'https')) {
+ return `${req.protocol}:`;
+ }
+ const { protocol } = nconf.get('url_parsed') || { protocol: 'https:' };
+ return protocol;
+}
+
async function tryVerifyDraft(req, fetchPublicKeyFn) {
try {
if (!req.headers.signature) {
diff --git a/test/activitypub/mitra-interop-sim.js b/test/activitypub/mitra-interop-sim.js
index 4c465bf3ed..a6a9411473 100644
--- a/test/activitypub/mitra-interop-sim.js
+++ b/test/activitypub/mitra-interop-sim.js
@@ -138,8 +138,12 @@ describe('Mitra-style incoming RFC 9421 request (interop simulation)', () => {
it('should verify when the signed @target-uri host differs from the configured URL (host mismatch)', async () => {
// Mitra signs the URL it dialed. If that differs from NodeBB's configured
// origin (reverse proxy, port, www vs. non-www, IP vs. domain), the
- // target URI is reconstructed from the Host header instead
- const dialedUrl = 'https://nodebb.example/uid/1/inbox';
+ // target URI is reconstructed from the Host header instead. The scheme
+ // comes from the (simulated) connection protocol, not the configured URL,
+ // so this stays correct for any test config (http or https, with or
+ // without a relative path)
+ const relativePath = nconf.get('relative_path') || '';
+ const dialedUrl = `https://nodebb.example${relativePath}/uid/1/inbox`;
const body = JSON.stringify({ id: 'https://mitra.example/activities/125', type: 'Announce', actor: 'https://mitra.example/users/alice', object: 'https://mitra.example/statuses/abc' });
const bodyBuf = Buffer.from(body, 'utf8');
const digestB64 = createHash('sha256').update(bodyBuf).digest('base64');
@@ -164,6 +168,8 @@ describe('Mitra-style incoming RFC 9421 request (interop simulation)', () => {
method: 'POST',
originalUrl: '/uid/1/inbox',
url: '/uid/1/inbox',
+ // Simulates a TLS-terminated dialed connection (express req.protocol)
+ protocol: 'https',
ip: '203.0.113.7',
rawBody: bodyBuf,
body: JSON.parse(body),