コンテンツにスキップ

Insecure hostname validation check

安全ではないホスト名検証チェック

概要

アプリケーションは、startsWith または endsWith のようなバイパスしやすいメソッドを使用して、安全ではないホスト名検証を実行しています。攻撃者は、チェックパターンに一致するドメインを登録することで、このチェックを容易にバイパスできます。

startsWithendsWith の両方を使用した複合チェックも同様に安全ではありません。攻撃者は、チェックされたパターンに一致するランダムな中間入力を伴うドメインを引き続き作成できるためです。

推奨事項

ホスト名の安全な検証を確保するために、以下の実装を検討してください。

  1. 正規表現 (Regex) 検証の実装: startsWith または endsWith のような単純なメソッドを使用する代わりに、正規表現を選択して徹底的なホスト名検証を実行します。正規表現パターンにより、正確なマッチング基準が可能になります。

  2. 標準化された検証ライブラリの検討: 堅牢なホスト名検証機能を提供する確立されたライブラリまたはフレームワークを活用します。これらのライブラリは適切に保守されており、潜在的なセキュリティの欠陥に対処するために定期的に更新されることがよくあります。

  3. ホワイトリストの実装: ホワイトリストに登録されたホストの数が限られている場合は、アプリケーションによって既知の信頼できるホスト名のみが受け入れられるホワイトリストアプローチの実装を検討してください。

import java.util.regex.*;

public class SubdomainValidator {
    public static void main(String[] args) {
        String userInput = "sub.example.com"; // replace this with user input

        // Regular expression pattern to match subdomains of example.com
        String pattern = "^([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\\.)+example\\.com$";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Create Matcher object
        Matcher m = r.matcher(userInput);

        // Check if input matches the pattern
        if (m.find()) {
            System.out.println("Valid subdomain of example.com");
        } else {
            System.out.println("Invalid subdomain of example.com");
        }
    }
}
import Foundation

func isValidSubdomain(_ userInput: String) -> Bool {
    let pattern = #"^([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+example\.com$"# // Regular expression pattern
    let regex = try! NSRegularExpression(pattern: pattern)
    let range = NSRange(location: 0, length: userInput.utf16.count)
    return regex.firstMatch(in: userInput, options: [], range: range) != nil
}

let userInput = "sub.example.com" // replace this with user input
if isValidSubdomain(userInput) {
    print("Valid subdomain of example.com")
} else {
    print("Invalid subdomain of example.com");
}
import 'package:flutter/material.dart';

bool isValidSubdomain(String userInput) {
  RegExp regex = RegExp(r'^([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+example\.com$');
  return regex.hasMatch(userInput);
}

void main() {
  String userInput = "sub.example.com"; // replace this with user input
  if (isValidSubdomain(userInput)) {
    print("Valid subdomain of example.com");
  } else {
    print("Invalid subdomain of example.com");
  }
}

リンク

標準

  • OWASP_MASVS_L1:
    • MSTG_PLATFORM_2
  • OWASP_MASVS_L2:
    • MSTG_PLATFORM_2
  • CWE_TOP_25:
    • CWE_20
  • PCI_STANDARDS:
    • REQ_6_2
    • REQ_6_3
    • REQ_11_3
  • OWASP_MASVS_v2_1:
    • MASVS_CODE_4
  • SOC2_CONTROLS:
    • CC_2_1
    • CC_4_1
    • CC_7_1
    • CC_7_2
    • CC_7_4
    • CC_7_5
  • HIPAA_CONTROLS:
    • SECURITY252
    • SECURITY212
    • SECURITY213