Security header validation helps ensure that web responses include important headers that protect users from attacks. It checks if these headers are present and correctly set.
Security header validation in Postman
pm.test('Header Name is present', function () { pm.response.to.have.header('Header-Name'); }); pm.test('Header Name has correct value', function () { pm.expect(pm.response.headers.get('Header-Name')).to.eql('Expected-Value'); });
Use pm.response.to.have.header() to check if a header exists.
Use pm.expect() with pm.response.headers.get() to check header values.
pm.test('Content-Security-Policy header is present', function () { pm.response.to.have.header('Content-Security-Policy'); });
DENY, which prevents clickjacking.pm.test('X-Frame-Options header has value DENY', function () { pm.expect(pm.response.headers.get('X-Frame-Options')).to.eql('DENY'); });
max-age directive to enforce HTTPS.pm.test('Strict-Transport-Security header includes max-age', function () { const sts = pm.response.headers.get('Strict-Transport-Security'); pm.expect(sts).to.include('max-age'); });
This Postman test script checks three important security headers: Content-Security-Policy, X-Frame-Options, and Strict-Transport-Security. It verifies their presence and correct values to help protect the web application.
pm.test('Security headers validation', function () { pm.response.to.have.header('Content-Security-Policy'); pm.expect(pm.response.headers.get('Content-Security-Policy')).to.eql("default-src 'self'"); pm.response.to.have.header('X-Frame-Options'); pm.expect(pm.response.headers.get('X-Frame-Options')).to.eql('DENY'); pm.response.to.have.header('Strict-Transport-Security'); const sts = pm.response.headers.get('Strict-Transport-Security'); pm.expect(sts).to.include('max-age=31536000'); });
Always check for both presence and correct values of security headers.
Header names are case-insensitive but use the exact spelling for clarity.
Security headers help protect users from common web attacks like XSS and clickjacking.
Security header validation ensures important HTTP headers exist and have correct values.
Use Postman tests to automate checking headers like Content-Security-Policy and X-Frame-Options.
This helps keep web applications safer and compliant with security best practices.