Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/n8n/libresign-external-signature.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }];"
}
},
{
Expand Down
12 changes: 12 additions & 0 deletions examples/n8n/test-workflow.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading