0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AIツールの許可ドメイン差分をPythonで出す

0
Posted at

AIツールを使っている人に、「どのURLを開いていますか」と聞いて、すぐ答えられるでしょうか。

僕は今朝、AIサービスまわりのトラッキングや学習利用のニュースを見て、まず入力内容より前に外部ドメインを見たほうが早いなと思いました。誰が何を貼ったかを完璧に追うのは重いです。最初の一歩は、アクセスログや利用申請CSVから「許可していないAI系ドメイン」を出すだけで十分です。

ここ地味に効きます。

入力ファイル

許可済みドメインを1行ずつ書きます。

# allow_domains.txt
chatgpt.com
openai.com
anthropic.com
perplexity.ai

利用ログは、最低限 url 列があれば動きます。

user,url
sales,https://chatgpt.com/c/abc
sales,https://platform.openai.com/docs
plan,https://claude.ai/new
plan,https://www.perplexity.ai/search/test
sales,https://example-ai-tool.com/upload
sales,https://notopenai.com/login

Pythonで許可ドメインとの差分を出す

# check_ai_domains.py
import csv
import sys
from collections import Counter, defaultdict
from urllib.parse import urlparse

log_path = sys.argv[1]
allow_path = sys.argv[2]

allow_domains = {
    line.strip().lower()
    for line in open(allow_path, encoding="utf-8")
    if line.strip() and not line.startswith("#")
}

def host_of(url):
    host = urlparse(url).netloc.lower().split("@")[-1].split(":")[0]
    return host[4:] if host.startswith("www.") else host

def allowed(host):
    return any(host == d or host.endswith("." + d) for d in allow_domains)

counts = Counter()
samples = defaultdict(list)

with open(log_path, newline="", encoding="utf-8-sig") as f:
    for row in csv.DictReader(f):
        url = (row.get("url") or "").strip()
        host = host_of(url)
        if not host:
            continue
        counts[host] += 1
        if len(samples[host]) < 2:
            samples[host].append(url)

for host, count in counts.most_common():
    status = "OK" if allowed(host) else "CHECK"
    print(f"{status}\t{count}\t{host}\t{samples[host][0]}")

実行します。

python check_ai_domains.py access_log.csv allow_domains.txt

手元ではこう出ました。

OK      1       chatgpt.com              https://chatgpt.com/c/abc
OK      1       platform.openai.com      https://platform.openai.com/docs
CHECK   1       claude.ai                https://claude.ai/new
OK      1       perplexity.ai            https://www.perplexity.ai/search/test
CHECK   1       example-ai-tool.com      https://example-ai-tool.com/upload
CHECK   1       notopenai.com            https://notopenai.com/login

流れはこうです。

ただの部分一致にしない

ここで大事なのは、openai.com を許可したときに platform.openai.com は通し、notopenai.com は通さないことです。

雑に if "openai.com" in host と書くと、notopenai.com までOKになります。逆に完全一致だけだと、platform.openai.com を毎回別登録することになります。だから host == d or host.endswith("." + d) にしています。

もうひとつ、会社名とサービスURLはズレます。上の例だと anthropic.com を許可していても、実際に利用者が開くのは claude.ai でした。ここは人間が確認して、許可リストへ足すか、社内ルール側を直すところです。コードで勝手にOKにしないほうがいいです。

現場で使うなら

このチェックは、最初から監査システムを作る話ではありません。Chrome履歴のエクスポート、プロキシログ、SaaS利用申請のCSVでも始められます。週1回でも CHECK だけ見れば、「知らないAIツールが増えているか」「許可リストが古くなっているか」が分かります。

AI活用のルール作りは、抽象的な禁止事項だけだと現場で止まります。URLをドメインに丸めて、許可済みかどうかを見る。まずそこまで落とすと、営業や企画でも会話に参加できます。いきなり完璧な監査ログを目指すより、この小さい差分表から始めるほうが早いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?