Option C Tools
Loading...
Searching...
No Matches
test_secret_match_word.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_linter/test_secret_match_word.py
4
5"""
6Purpose
7-------
8OI-529 regression coverage — :func:`_name_matches_secret` and the
9``--secret-match {substring,word}`` CLI flag.
10
11The legacy ``substring`` matcher flagged any name that happened to
12contain a secret-shaped substring, so ordinary identifiers like
13``tokenizer`` (contains ``token``) or ``pwdistance`` (contains ``pwd``)
14tripped the scanner. The ``word`` mode splits on ``_``/``-``/camelCase
15boundaries and only flags whole-segment hits, so ``user_password``
16still flags but ``tokenizer`` does not.
17
18Responsibilities
19----------------
20- ``substring`` mode flags ``compass``.
21- ``word`` mode does not flag ``compass``.
22- ``word`` mode still flags ``compass_password`` and ``apiKey``.
23- ``check_no_hardcoded_secrets`` threads the mode through unchanged.
24
25Diagnostics
26-----------
27Domain: LINT-TESTS
28Levels:
29 L2 — test lifecycle
30 L3 — assertion details
31 L4 — deep tracing
32
33Contracts
34---------
35- Tests operate on in-memory strings and AST snippets; no files are
36 written.
37"""
38
39from __future__ import annotations
40
41from oct.tools.secret_scanner import (
42 _name_matches_secret,
43 check_no_hardcoded_secrets,
44)
45
46
47# ---------------------------------------------------------------------
48# _name_matches_secret
49# ---------------------------------------------------------------------
50
51
53 # Legacy behaviour: ``tokenizer`` contains ``token`` → flags.
54 assert _name_matches_secret("tokenizer", mode="substring") is True
55
56
58 # OI-529 fix: ``tokenizer`` is a single segment, not in the pattern
59 # set, so ``word`` mode returns False.
60 assert _name_matches_secret("tokenizer", mode="word") is False
61
62
64 assert _name_matches_secret("user_password", mode="word") is True
65 assert _name_matches_secret("db_connection_string", mode="word") is True
66
67
69 assert _name_matches_secret("apiKey", mode="word") is True
70 assert _name_matches_secret("userToken", mode="word") is True
71
72
74 # ``pwdistance`` contains ``pwd`` under substring matching.
75 assert _name_matches_secret("pwdistance", mode="substring") is True
76 assert _name_matches_secret("pwdistance", mode="word") is False
77
78
79# ---------------------------------------------------------------------
80# check_no_hardcoded_secrets threading
81# ---------------------------------------------------------------------
82
83
85 source = 'tokenizer = "not-a-secret"\n'
86 # substring: flags.
87 ok_sub, msg_sub = check_no_hardcoded_secrets(source, mode="substring")
88 assert ok_sub is False, msg_sub
89 # word: clean.
90 ok_word, msg_word = check_no_hardcoded_secrets(source, mode="word")
91 assert ok_word is True, msg_word
92
93
95 source = 'api_key = "AKIA1234567890"\n'
96 ok, msg = check_no_hardcoded_secrets(source, mode="word")
97 assert ok is False, "word mode must still flag real secrets"
98 assert "api_key" in msg