mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-09-18 16:26:05 +02:00
Added draft for CSP health checker
This commit is contained in:
43
roles/health-csp/files/health-csp.js
Normal file
43
roles/health-csp/files/health-csp.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const puppeteer = require('puppeteer');
|
||||
const domains = process.argv.slice(2);
|
||||
|
||||
(async () => {
|
||||
let errorCounter = 0;
|
||||
const browser = await puppeteer.launch({ headless: 'new' });
|
||||
|
||||
for (const domain of domains) {
|
||||
const page = await browser.newPage();
|
||||
const blockedResources = [];
|
||||
|
||||
page.on('requestfailed', request => {
|
||||
const reason = request.failure()?.errorText || '';
|
||||
if (reason.includes('blocked')) {
|
||||
blockedResources.push({ url: request.url(), reason });
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const url = `https://${domain}`;
|
||||
await page.goto(url, { waitUntil: 'networkidle2', timeout: 20000 });
|
||||
} catch (e) {
|
||||
console.error(`${domain}: ERROR visiting site - ${e.message}`);
|
||||
errorCounter++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (blockedResources.length > 0) {
|
||||
console.warn(`${domain}: Blocked resources detected:`);
|
||||
blockedResources.forEach(r =>
|
||||
console.log(` BLOCKED by CSP: ${r.url} (${r.reason})`)
|
||||
);
|
||||
errorCounter++;
|
||||
} else {
|
||||
console.log(`${domain}: ✅ No CSP blocks detected.`);
|
||||
}
|
||||
|
||||
await page.close();
|
||||
}
|
||||
|
||||
await browser.close();
|
||||
process.exit(errorCounter);
|
||||
})();
|
Reference in New Issue
Block a user