Postman Linux版进行安全性测试实操指南
一 环境准备与代理抓包
二 认证与授权安全测试
// 认证成功/失败校验
pm.test("Auth success returns 200", () => pm.response.to.have.status(200));
pm.test("Auth failure returns 401", () => pm.response.to.have.status(401));
// 错误响应不应泄露敏感信息
pm.test("Error response no sensitive info", () => {
if (pm.response.code >= 400) {
const txt = pm.response.text();
pm.expect(txt).to.not.include("password");
pm.expect(txt).to.not.include("secret");
pm.expect(txt).to.not.include("stacktrace");
}
});
上述流程依托 Postman 对多种认证的原生支持与 Tests 脚本的断言能力,可系统化覆盖认证与授权场景。三 输入验证与加密配置
/users?name=normal_user' OR '1'='1<script>alert('xss');</script>pm.request.setProtocolVersion(1.2);)。// 检测响应是否意外包含未转义脚本
pm.test("No unescaped XSS payload in response", () => {
const body = pm.response.text();
pm.expect(body).to.not.include("<script>alert('xss');</script>");
});
以上方法可在 Postman 中直接构造用例并用脚本自动判定,覆盖输入验证与传输加密的关键风险点。四 批量执行与持续化安全回归
# 安装 Newman(示例)
npm i -D newman
# 运行集合与环境,并生成 HTML 报告
npx newman run "API Security Tests.postman_collection.json" \
--environment "test_env.postman_environment.json" \
--reporters cli,html \
--reporter-html-export newman-report.html
五 合规范围与工具边界