迁移至passport-saml后SAML HTTP-POST绑定如何传递X509证书
迁移passport-saml HTTP-POST绑定缺X509证书问题排查
问题现象
从SimpleSAMLphp迁移到passport-saml时,使用HTTP-POST绑定发送AuthnRequest,通过SAML Chrome Inspector抓包对比发现:
- 旧的SimpleSAMLphp实现的请求主体会携带完整X509证书
- passport-saml生成的请求仅传递SignatureValue,没有对应证书字段
最终导致IdP无法校验签名,请求被拦截。
现有实现代码
const strategy = new passportSaml.Strategy( { callbackUrl: 'http://localhost:3000/assert', entryPoint: 'https://clientsaml.com/samljct/', passReqToCallback: true, cert: fs.readFileSync( path.resolve(__dirname, '../certs/cert.crt'), 'utf-8' ), privateKey: fs.readFileSync( path.resolve(__dirname, '../certs/privateKey.pem'), 'utf-8' ), authnRequestBinding: 'HTTP-POST', skipRequestCompression: true }, (profile, done) => { console.log(profile); return done(null, profile); } );
解决方案
该问题是passport-saml默认行为与旧SimpleSAMLphp实现的差异导致:passport-saml默认仅对AuthnRequest生成签名计算SignatureValue,不会主动将X509证书嵌入到签名块的<ds:KeyInfo>节点中,需要手动开启对应配置:
- 在策略配置对象中新增
includeSigCert: true参数,该参数会自动将你配置的cert内容,作为X509数据节点嵌入到SAML请求的签名块中,完全匹配旧实现的传参格式。 - 确认cert字段的传值格式:必须保留完整PEM格式的头尾标记(
-----BEGIN CERTIFICATE-----/-----END CERTIFICATE-----),不要提前做Base64转码、去除标记等处理,否则会导致嵌入的证书格式异常,无法被IdP识别。 - 如果使用的passport-saml版本低于v3.0,没有内置
includeSigCert配置项,建议升级到最新稳定版本,避免需要手动修改SAML模板的额外开发成本。
修改后的配置示例
const strategy = new passportSaml.Strategy( { callbackUrl: 'http://localhost:3000/assert', entryPoint: 'https://clientsaml.com/samljct/', passReqToCallback: true, cert: fs.readFileSync( path.resolve(__dirname, '../certs/cert.crt'), 'utf-8' ), privateKey: fs.readFileSync( path.resolve(__dirname, '../certs/privateKey.pem'), 'utf-8' ), authnRequestBinding: 'HTTP-POST', skipRequestCompression: true, // 新增配置,将X509证书嵌入到请求签名块 includeSigCert: true }, (profile, done) => { console.log(profile); return done(null, profile); } );
内容的提问来源于stack exchange,提问作者Jako
相关产品推荐
相关产品推荐

