From 560861b9ac0c4673cac490aaf8de226d227fa792 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:08:27 +0000 Subject: [PATCH] fix(n8n): validate Nextcloud origin without unavailable URL global Use n8n-compatible JavaScript in the Code node and cover the missing URL constructor with a dedicated regression test. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- examples/n8n/libresign-external-signature.json | 2 +- examples/n8n/test-workflow.mjs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/n8n/libresign-external-signature.json b/examples/n8n/libresign-external-signature.json index 4682d254..294877fa 100644 --- a/examples/n8n/libresign-external-signature.json +++ b/examples/n8n/libresign-external-signature.json @@ -41,7 +41,7 @@ ], "parameters": { "mode": "runOnceForAllItems", - "jsCode": "const c = $input.first().json;\nif (!['13', '14', '15', 'main'].includes(c.version)) throw new Error('Choose LibreSign 13, 14, 15 or main');\nlet url;\ntry { url = new URL(c.baseUrl); } catch { throw new Error('Invalid Nextcloud URL'); }\nif (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash || url.pathname !== '/') throw new Error('Use an HTTPS Nextcloud origin with no credentials, path or query');\nif (url.hostname.endsWith('.invalid') || url.hostname === 'example.com') throw new Error('Replace the placeholder Nextcloud host');\nif (!/^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$/.test(c.signerEmail) || /@(?:example\\.(?:test|invalid|com|org|net))$/i.test(c.signerEmail)) throw new Error('Use an inbox under your control');\nif (!/^[A-Za-z0-9_.@-]+$/.test(c.integrationUser)) throw new Error('Use a simple integration account ID; URL-encode it for DAV');\nif (typeof c.signedPdfPath !== 'string' || !c.signedPdfPath || c.signedPdfPath.startsWith('/') || c.signedPdfPath.split('/').some(p => !p || p === '.' || p === '..' || p.includes('\\\\') || p.includes('?') || p.includes('#'))) throw new Error('Set a verified relative WebDAV PDF path');\nif (!Number.isInteger(c.maxAttempts) || c.maxAttempts < 1 || c.maxAttempts > 120) throw new Error('maxAttempts must be 1 to 120');\nif (!Number.isInteger(c.waitSeconds) || c.waitSeconds < 5 || c.waitSeconds > 3600) throw new Error('waitSeconds must be 5 to 3600');\nif (typeof c.pdfBase64 !== 'string' || !c.pdfBase64.startsWith('JVBERi0')) throw new Error('Provide a base64-encoded synthetic PDF');\nconst method = { method: 'email', value: c.signerEmail };\nif (c.version === '13' || c.version === '14') method.mandatory = 1;\nelse method.requirement = 'required';\nconst requestBody = { name: 'Synthetic service agreement', status: 1, file: { name: 'synthetic-n8n.pdf', base64: c.pdfBase64 }, signers: [{ displayName: 'External Test Signer', notify: 0, identifyMethods: [method] }] };\nreturn [{ json: { ...c, baseUrl: url.origin, requestBody } }];" + "jsCode": "const c = $input.first().json;\nif (!['13', '14', '15', 'main'].includes(c.version)) throw new Error('Choose LibreSign 13, 14, 15 or main');\n// n8n Code nodes do not expose the URL constructor. Accept only an HTTPS origin.\nconst match = /^https:\\/\\/([a-z0-9](?:[a-z0-9.-]*[a-z0-9])?|\\[[0-9a-f:.]+\\])(?::([0-9]{1,5}))?\\/?$/i.exec(c.baseUrl);\nif (!match || (match[2] && (Number(match[2]) < 1 || Number(match[2]) > 65535))) throw new Error('Use an HTTPS Nextcloud origin with no credentials, path or query');\nconst hostname = match[1].toLowerCase();\nif (!hostname.startsWith('[') && hostname.split('.').some(label => !label || label.startsWith('-') || label.endsWith('-'))) throw new Error('Invalid Nextcloud hostname');\nif (hostname.endsWith('.invalid') || hostname === 'example.com') throw new Error('Replace the placeholder Nextcloud host');\nconst baseUrl = 'https://' + hostname + (match[2] ? ':' + Number(match[2]) : '');\nif (!/^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$/.test(c.signerEmail) || /@(?:example\\.(?:test|invalid|com|org|net))$/i.test(c.signerEmail)) throw new Error('Use an inbox under your control');\nif (!/^[A-Za-z0-9_.@-]+$/.test(c.integrationUser)) throw new Error('Use a simple integration account ID; URL-encode it for DAV');\nif (typeof c.signedPdfPath !== 'string' || !c.signedPdfPath || c.signedPdfPath.startsWith('/') || c.signedPdfPath.split('/').some(p => !p || p === '.' || p === '..' || p.includes('\\\\') || p.includes('?') || p.includes('#'))) throw new Error('Set a verified relative WebDAV PDF path');\nif (!Number.isInteger(c.maxAttempts) || c.maxAttempts < 1 || c.maxAttempts > 120) throw new Error('maxAttempts must be 1 to 120');\nif (!Number.isInteger(c.waitSeconds) || c.waitSeconds < 5 || c.waitSeconds > 3600) throw new Error('waitSeconds must be 5 to 3600');\nif (typeof c.pdfBase64 !== 'string' || !c.pdfBase64.startsWith('JVBERi0')) throw new Error('Provide a base64-encoded synthetic PDF');\nconst method = { method: 'email', value: c.signerEmail };\nif (c.version === '13' || c.version === '14') method.mandatory = 1;\nelse method.requirement = 'required';\nconst requestBody = { name: 'Synthetic service agreement', status: 1, file: { name: 'synthetic-n8n.pdf', base64: c.pdfBase64 }, signers: [{ displayName: 'External Test Signer', notify: 0, identifyMethods: [method] }] };\nreturn [{ json: { ...c, baseUrl, requestBody } }];" } }, { diff --git a/examples/n8n/test-workflow.mjs b/examples/n8n/test-workflow.mjs index dd8b9c95..4b1f05b8 100644 --- a/examples/n8n/test-workflow.mjs +++ b/examples/n8n/test-workflow.mjs @@ -72,6 +72,18 @@ test('request validation rejects placeholders and unsafe WebDAV paths', async () { maxAttempts: 0 }, ]) await assert.rejects(code('Prepare request', { ...config, ...bad })); }); +test('request validation works inside n8n Code without global URL', async () => { + const input = { ...config, baseUrl: 'https://nc.example.test:8443/' }; + const output = (await code('Prepare request', input, {}, { URL: undefined }))[0].json; + assert.equal(output.baseUrl, 'https://nc.example.test:8443'); + for (const baseUrl of [ + 'https://user:pass@nc.example.test', + 'https://nc.example.test/path', + 'https://nc.example.test?x=1', + 'https://nc.example.test:99999', + 'https://nc.example.test:0', + ]) await assert.rejects(code('Prepare request', { ...input, baseUrl }, {}, { URL: undefined })); +}); test('creation/notification require successful OCS envelope', async () => { const response = { ocs: { meta: { status: 'ok' }, data: { id: 17, uuid: created.fileUuid } } }; const out = (await code('Verify created', response))[0].json;