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.
Practice
Solution
Step 1: Understand clickjacking protection
Clickjacking is prevented by restricting framing of pages.Step 2: Identify the header for frame control
TheX-Frame-Optionsheader controls if a page can be framed.Final Answer:
X-Frame-Options -> Option CQuick Check:
Clickjacking protection = X-Frame-Options [OK]
- Confusing Content-Security-Policy with frame control
- Thinking Strict-Transport-Security controls framing
- Ignoring the role of X-Frame-Options
Content-Security-Policy header exists in the response?Solution
Step 1: Understand Postman header methods
pm.response.headers.get()returns the header value or null if missing.Step 2: Check for header existence correctly
Checking if the value is not null confirms header presence.Final Answer:
pm.test('CSP header present', () => pm.expect(pm.response.headers.get('Content-Security-Policy') !== null).to.be.true); -> Option AQuick Check:
Header exists if get() !== null [OK]
- Using headers.has() which returns boolean but not tested properly
- Checking for undefined instead of null
- Negating the condition incorrectly
X-Content-Type-Options is missing?
pm.test('X-Content-Type-Options header is set', () => {
pm.response.to.have.header('X-Content-Type-Options');
});Solution
Step 1: Understand the assertion used
pm.response.to.have.header()asserts the header must exist.Step 2: Analyze missing header effect
If the header is missing, the assertion fails and test fails.Final Answer:
Test fails because header is missing -> Option DQuick Check:
Missing required header = test failure [OK]
- Assuming test passes if header is missing
- Confusing syntax error with assertion failure
- Thinking warnings are logged instead of failure
Strict-Transport-Security header includes max-age=31536000:
pm.test('HSTS max-age check', () => {
pm.expect(pm.response.headers.get('Strict-Transport-Security')).to.include('max-age=31536000');
});Solution
Step 1: Understand get() behavior when header missing
If header is missing, get() returns null, causing error on to.include.Step 2: Identify missing existence check
Test should first check header exists before checking content.Final Answer:
Not checking if header exists before calling get() -> Option AQuick Check:
Check header presence before content check [OK]
- Assuming get() never returns null
- Ignoring null causing test errors
- Focusing on casing or semicolons instead
Content-Security-Policy header disallows inline scripts by including script-src 'self'. Which test script correctly validates this?Solution
Step 1: Understand CSP header content
The header can contain multiple directives; we check if it includesscript-src 'self'.Step 2: Validate correct assertion method
Usingto.includechecks if the string contains the directive, allowing other parts.Final Answer:
pm.test('CSP disallows inline scripts', () => { const csp = pm.response.headers.get('Content-Security-Policy'); pm.expect(csp).to.include("script-src 'self'"); }); -> Option BQuick Check:
Use include() to check partial CSP directive [OK]
- Using to.equal which requires exact match
- Only checking header presence without content
- Checking for 'unsafe-inline' which allows inline scripts
Ready to prove it?
Pass the test and this pattern moves unknown
