TLS 인증서 검증 비활성화

Improper Certificate Validation

설명

rejectUnauthorized: false를 지정하면 신뢰할 수 없는 인증서나 호스트명이 일치하지 않는 인증서로도 TLS 연결이 성립할 수 있습니다. NODE_TLS_REJECT_UNAUTHORIZED=0은 프로세스 전체의 기본 검증에 영향을 줍니다.

잠재적 영향

통신은 암호화되더라도 서버를 가장한 상대가 로그인 정보나 세션 토큰을 받거나 응답을 바꿀 수 있습니다.

해결 방법

  • rejectUnauthorized는 기본값 true로 두고 검증을 끄는 환경 변수를 제거하세요. 테스트에 필요한 CA는 운영 설정과 분리하세요.
  • https.Agent 또는 tls.connect에 ca를 지정하면 기본 CA 목록이 교체됩니다. 연결에 필요한 CA를 모두 포함하고 호스트명 검증을 유지하세요. 서버에 SNI가 필요하면 tls.connect의 host와 함께 servername도 지정하세요.
  • 인증서나 공개키를 고정해야 한다면 신뢰할 수 있는 값을 별도로 확보하고 인증서 갱신 때의 동작을 확인하세요.

예시

변경 전

javascript
// 비준수 예시 1: 전역 인증서 검증 비활성화
const https = require('https');
const tls = require('tls');

function fetchInsecure() {
  // 전역으로 TLS 검증을 비활성화 -> 매우 위험
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

  https.get({ hostname: 'api.example.org', path: '/v1/data' }, (res) => {
    res.on('data', () => {});
  });
}

// 비준수 예시 2: 에이전트에서 인증서 검증 비활성화
function postInsecure() {
  const insecureAgent = new https.Agent({ keepAlive: true, rejectUnauthorized: false });

  const req = https.request({
    hostname: 'secure.example.org',
    method: 'POST',
    path: '/login',
    agent: insecureAgent,
    headers: { 'Content-Type': 'application/json' }
  }, (res) => res.resume());

  req.write(JSON.stringify({ user: 'alice', pass: 'secret' }));
  req.end();
}

변경 후

javascript
// 준수 예시: 기본 검증 유지 + 신뢰 가능한 CA 번들 사용
const https = require('https');
const fs = require('fs');

// 사내 또는 공인 CA 번들 경로(예시)
const caBundle = fs.readFileSync('/etc/ssl/certs/company-ca-bundle.pem');

function fetchSecure() {
  // rejectUnauthorized 기본값은 true
  const secureAgent = new https.Agent({
    keepAlive: true,
    ca: caBundle,           // 신뢰 가능한 CA 제공
    // servername은 요청 host에서 자동 설정되므로 따로 끄지 않음
  });

  https.get({
    hostname: 'api.example.org',
    path: '/v1/data',
    agent: secureAgent
  }, (res) => {
    let body = '';
    res.on('data', (c) => body += c);
    res.on('end', () => console.log('OK:', body.length));
  });
}

// 추가: 필요 시 인증서 핀닝(인증서 SHA-256 지문 비교) 예시
const tls = require('tls');
function connectWithPinning(expectedFingerprint256) {
  const socket = tls.connect({ host: 'secure.example.org', port: 443, ca: caBundle }, () => {
    const cert = socket.getPeerCertificate(true);
    if (!cert || !cert.fingerprint256 || cert.fingerprint256 !== expectedFingerprint256) {
      socket.destroy(new Error('Certificate pinning mismatch'));
      return;
    }
    socket.write('GET / HTTP/1.1\r\nHost: secure.example.org\r\n\r\n');
  });
}

예시는 CA 번들을 사용하고, 선택적으로 fingerprint256으로 인증서 전체의 SHA-256 지문을 비교합니다. 지문이 일치하지 않으면 요청을 보내기 전에 연결을 닫습니다.

참조