惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Forbes - Security
Forbes - Security
WordPress大学
WordPress大学
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
Spread Privacy
Spread Privacy
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
P
Privacy International News Feed
A
About on SuperTechFans
T
Tailwind CSS Blog
I
InfoQ
S
Securelist
云风的 BLOG
云风的 BLOG
罗磊的独立博客
Recent Announcements
Recent Announcements
T
The Exploit Database - CXSecurity.com
B
Blog RSS Feed
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
腾讯CDC
Cyberwarzone
Cyberwarzone
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
博客园 - 【当耐特】
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
S
Secure Thoughts
博客园 - 司徒正美
J
Java Code Geeks
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
Help Net Security
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Project Zero
Project Zero
T
Tenable Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tor Project blog
MyScale Blog
MyScale Blog
Scott Helme
Scott Helme
小众软件
小众软件
K
Kaspersky official blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
The 26-Dimensional Feature Vector: How a Machine Learns to Recognise a Secret
Patience Mpo · 2026-05-14 · via DEV Community

hen my secrets detector evaluates a candidate string, it doesn't see code.

It sees a vector of 26 numbers.

That vector is the bridge between human intuition — "this looks like a secret" — and machine classification. Every insight a security engineer uses when reading code to spot exposed credentials has been translated into a numerical feature that the Random Forest classifier can reason about.

This article is a complete walkthrough of those 26 features: what each one measures, why it matters, what it catches, and what it misses. By the end, you'll understand exactly what the model sees when it evaluates any candidate value — and why the combination of features catches things that no single signal could.


How Feature Extraction Works

Before the classifier sees anything, every candidate string goes through a feature extraction pipeline in features.py. The pipeline takes two inputs: the string value itself, and the name of the variable holding it.

def extract_features(value: str, key_name: str) -> np.ndarray:
    features = []

    # Entropy features
    features.append(shannon_entropy(value))
    features.append(math.log(len(value) + 1))
    features.append(repetition_ratio(value))
    features.append(longest_run_normalized(value))

    # Character distribution features (8 features)
    features.extend(character_ratios(value))

    # Key name context
    features.append(key_name_risk_score(key_name))

    # Pattern match flags (16 features)
    features.extend(pattern_match_flags(value))

    return np.array(features)

Enter fullscreen mode Exit fullscreen mode

The output is a fixed-length array of 26 floating point numbers. The classifier never sees the original string — only this vector. That's both a strength (the model generalises across different string formats) and a limitation (some context that a human would use is deliberately excluded).

Let me walk through each group.


Group 1: Entropy Features (4 features)

These four features capture the statistical "randomness" of the string — the property that real secrets share with random data.

Feature 1: Shannon Entropy

Shannon entropy measures the unpredictability of the character sequence. For a string of length n with character frequencies p_i:

H = -Σ p_i × log₂(p_i)

Enter fullscreen mode Exit fullscreen mode

A perfectly random string of alphanumeric characters has entropy around 5.7–6.0 bits. Common English words have entropy around 3.5–4.5 bits. Cryptographically generated secrets cluster at the high end.

# High entropy — likely a secret or hash
"sk-proj-abc123XYZ789..."  entropy: 5.82

# Low entropy — likely a password or human-chosen value  
"Winter2019!"             entropy: 3.21

# Very low entropy — definitely not a secret
"aaaaaaaaaaaaa"           entropy: 0.00

Enter fullscreen mode Exit fullscreen mode

Entropy alone is a weak classifier — UUIDs, SHA-256 hashes, and base64 image data all have high entropy but are not secrets. That's why it's one of 26 features, not the only feature.

Feature 2: Log-Scaled Length

Raw string length would give too much weight to very long strings. Log-scaling (math.log(len(value) + 1)) compresses the range so that the difference between a 32-character key and a 64-character key has roughly the same weight as the difference between a 4-character and 8-character string.

Secrets tend to fall in predictable length ranges: AWS access keys are 20 characters, GitHub PATs are 40, JWT tokens are variable but typically 200+. Length contributes signal, but it's a soft signal — there's no length that definitively indicates "secret."

Feature 3: Repetition Ratio

repetition_ratio = len(set(value)) / len(value)

Enter fullscreen mode Exit fullscreen mode

This is the proportion of unique characters to total characters. A perfectly random string of 32 characters will have close to 32 unique characters (ratio ≈ 1.0). A string like "aababcababc" has low unique character count relative to its length (ratio ≈ 0.3).

Low repetition ratio is a strong signal that a string is not a secret — real secrets don't repeat characters predictably. High repetition ratio is a necessary but not sufficient condition for being a secret.

Feature 4: Longest Run (Normalised)

The length of the longest consecutive run of the same character, divided by string length:

longest_run = max(len(list(g)) for _, g in itertools.groupby(value))
longest_run_normalized = longest_run / len(value)

Enter fullscreen mode Exit fullscreen mode

"aaabbbccc" has a longest run of 3 out of 9 characters — normalised run of 0.33.
"sk-abc123XYZ789def456" has a longest run of 1 out of 21 characters — normalised run of 0.05.

Long runs of repeated characters are a strong signal of non-random data. No cryptographically generated secret will have a long run. Human-readable strings often will.


Group 2: Character Distribution Features (8 features)

These eight features describe the composition of the string across character classes. Together they capture the "shape" of the character set that a human eye uses to distinguish secrets from benign strings.

Feature What It Measures
uppercase_ratio Proportion of A–Z characters
lowercase_ratio Proportion of a–z characters
digit_ratio Proportion of 0–9 characters
special_ratio Proportion of non-alphanumeric characters
hex_ratio Proportion of valid hexadecimal characters (0–9, a–f, A–F)
base64_ratio Proportion of base64-safe characters (alphanumeric + /+=)
printable_ratio Proportion of printable ASCII characters
whitespace_ratio Proportion of whitespace characters

Why these specific ratios matter:

hex_ratio is particularly useful for distinguishing hash values from secrets. A SHA-256 hash has a hex_ratio of 1.0 — every character is a valid hex digit. An AWS access key has a hex_ratio of approximately 0.6 (uppercase letters reduce it). A JWT token has a hex_ratio near 0.0 (it's base64url-encoded, using characters outside the hex alphabet).

special_ratio catches secrets that include special characters — a strong signal for human-chosen passwords ("P@ssw0rd!") versus machine-generated tokens (which typically avoid special characters for compatibility reasons).

base64_ratio is the mirror of hex_ratio for base64-encoded content. Base64-encoded image data has a base64_ratio near 1.0. An API key that uses only alphanumeric characters has a high base64_ratio too — which is where the key name and other features need to disambiguate.

The classifier learns the interaction between these ratios. A string with high entropy, high hex_ratio, and a key name that scores 0.0 is almost certainly a hash. A string with high entropy, mixed character ratios, and a key name that scores 1.0 is almost certainly a secret.


Group 3: Key Name Risk Score (1 feature)

This is the single most important feature in the model — feature importance 0.28, more than the entropy and character features combined.

KEY_NAME_RISK = {
    # Score 1.0 — unambiguously sensitive
    "password": 1.0, "passwd": 1.0, "secret": 1.0,
    "private_key": 1.0, "privkey": 1.0,

    # Score 0.9 — very likely sensitive  
    "api_key": 0.9, "apikey": 0.9, "token": 0.9,
    "credential": 0.9, "auth_token": 0.9,

    # Score 0.85 — likely sensitive
    "access_key": 0.85, "client_secret": 0.85,
    "bearer": 0.85, "authorization": 0.85,

    # Score 0.7 — possibly sensitive
    "key": 0.7, "auth": 0.7, "login": 0.7,

    # Score 0.2 — unlikely sensitive
    "config": 0.2, "setting": 0.2, "value": 0.1,

    # Score 0.0 — not sensitive
    "checksum": 0.0, "hash": 0.0, "version": 0.0,
    "id": 0.0, "uuid": 0.0, "color": 0.0
}

def key_name_risk_score(key_name: str) -> float:
    normalised = key_name.lower().strip("_")
    for keyword, score in KEY_NAME_RISK.items():
        if keyword in normalised:
            return score
    return 0.3  # Unknown key names get a moderate default

Enter fullscreen mode Exit fullscreen mode

The scoring function does substring matching, so DB_PASSWORD, database_password, and user_passwd all score 1.0. API_KEY_V2 and service_api_key both score 0.9.

Unknown variable names — ones that don't contain any recognised keyword — get a default score of 0.3. This is deliberately moderate: an unknown variable name is mild evidence that the string might not be sensitive (if it were, it would likely have a recognisable name), but it's not strong evidence either way.

The impact of this feature on classification decisions is substantial:

# Same value, wildly different classifications
password = "d8e8fca2dc0f896fd7cb4cb0031ba249"  # → flagged at 94% confidence
checksum = "d8e8fca2dc0f896fd7cb4cb0031ba249"  # → passed at 8% confidence

Enter fullscreen mode Exit fullscreen mode

Without the key name feature, these two lines are identical to the classifier. With it, they're completely distinguishable.


Group 4: Pattern Match Flags (16 features)

These are binary features — 0 or 1 — indicating whether the value matches any of 16 known secret format patterns.

Flag Pattern
pattern_aws_access_key AKIA[0-9A-Z]{16}
pattern_github_pat gh[pousr]_[A-Za-z0-9]{36}
pattern_github_fine_grained github_pat_[A-Za-z0-9]{82}
pattern_jwt eyJ[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+
pattern_openai_key sk-[A-Za-z0-9]{48}
pattern_slack_token xox[baprs]-[A-Za-z0-9-]+
pattern_stripe_secret sk_live_[A-Za-z0-9]{24}
pattern_stripe_publishable pk_live_[A-Za-z0-9]{24}
pattern_google_api AIza[0-9A-Za-z-_]{35}
pattern_heroku_api [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
pattern_private_key_header `-----BEGIN (RSA\
{% raw %}pattern_db_connection `(postgresql\
{% raw %}pattern_basic_auth [A-Za-z0-9+/]{20,}={0,2} (base64 basic auth)
pattern_bearer_token Bearer [A-Za-z0-9-._~+/]+=*
pattern_hex_key_32 [0-9a-f]{32} (32-char hex — common key length)
pattern_hex_key_64 [0-9a-f]{64} (64-char hex — SHA-256 length)

When any of these flags fire, the classifier has strong prior evidence that the value is a known secret format. A value that matches pattern_aws_access_key will be classified as a secret at very high confidence regardless of what the other features say.

The last two flags — pattern_hex_key_32 and pattern_hex_key_64 — deserve special mention. These match the lengths of common cryptographic keys but also match MD5 and SHA-256 hashes, which are not secrets. This is where the key name feature does critical disambiguation work: a 32-character hex string with key name checksum has pattern_hex_key_32 = 1 but key_name_risk = 0.0, and the classifier correctly passes it. The same string with key name encryption_key gets flagged.


How the Features Interact: Three Case Studies

Understanding individual features is useful. Understanding how they interact is where the real insight lives.

Case Study 1: The Human-Chosen Password

SMTP_PASSWORD = "Winter2019!"

Enter fullscreen mode Exit fullscreen mode

Feature Value Signal
shannon_entropy 3.4 Weak — below threshold for "looks random"
repetition_ratio 0.91 Neutral
special_ratio 0.09 Slightly elevated
key_name_risk 1.0 Very strong — "password" scores maximum
pattern_* flags All 0 No known format match
Classification Secret — 91% confidence

The entropy would cause a pure entropy scanner to miss this. The key name saves it.

Case Study 2: The UUID False Positive

session_correlation_id = "550e8400-e29b-41d4-a716-446655440000"

Enter fullscreen mode Exit fullscreen mode

Feature Value Signal
shannon_entropy 4.1 Moderate — looks somewhat random
hex_ratio 0.89 Very high — almost all hex characters
special_ratio 0.08 Low — only hyphens
key_name_risk 0.0 Minimal — "id" scores 0.0
pattern_heroku_api 1 Fires — Heroku API keys are UUID-format
Classification Benign — 23% confidence

The pattern flag fires (Heroku API keys look like UUIDs), but the key name score is 0.0 and the classifier correctly suppresses the finding. A regex scanner using only the Heroku pattern would flag this. The ML classifier does not.

Case Study 3: The Ambiguous High-Entropy String

encryption_key = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"

Enter fullscreen mode Exit fullscreen mode

Feature Value Signal
shannon_entropy 3.9 Moderate
hex_ratio 1.0 Maximum — pure hex
key_name_risk 0.9 Very high — "key" scores 0.7, "encryption" modifier pushes it higher
pattern_hex_key_32 1 Fires — 32-char hex matches
repetition_ratio 0.44 Low — repeating pattern visible
Classification Secret — 78% confidence

The repetition ratio is low (the value has a repeating a1b2c3... pattern that reduces uniqueness), which pulls the confidence down from what it would be for a truly random key. But the key name and pattern flag are strong enough to push it above the reporting threshold. The finding would be reported at MEDIUM confidence — a prompt for human review rather than a guaranteed finding.


What the Feature Vector Cannot See

Intellectual honesty requires being clear about the limits.

Cross-variable context. The feature vector sees one value at a time. It can't see that key = config["encryption_key"] is loading the key from a config object rather than hardcoding it. A human engineer would immediately see that's not a hardcoded secret; the feature vector has no way to represent that.

File context. The feature vector doesn't know it's in a test file, a mock object, or a README code example. TEST_API_KEY = "fake-key-for-testing" might have a high key name risk score despite being explicitly for testing. The inline suppression annotation (# secrets-ignore) is the escape hatch for this case.

Semantic intent. version = "1.0.0" will correctly score a low key name risk. But release_token = "1.0.0-beta" might score higher because "token" is a high-risk keyword — even though in context this is clearly a version string. The feature vector sees the word "token" in the variable name without understanding that it's used semantically differently here.

These limitations are why the classifier is a signal generator rather than an oracle. Every finding above the confidence threshold warrants a human review. The classifier reduces the review burden dramatically — from "look at every high-entropy string in the codebase" to "look at these 20 high-confidence findings" — but it doesn't eliminate the need for human judgment.


Retraining on Your Own Data

The feature vector approach makes retraining practical in a way that deep learning approaches don't. Because the features are hand-engineered and interpretable, adding new training samples has predictable effects.

If your codebase has a pattern of false positives — say, your internal logging library uses variable names like log_token that consistently score high key name risk despite being benign — you can add synthetic examples of that pattern to the benign training set and retrain in seconds:

# Add your custom generators to trainer.py, then:
python main.py train --samples 5000

Enter fullscreen mode Exit fullscreen mode

The retrained model immediately incorporates your organisation-specific context. That's a capability that's practically unavailable with regex-based tools (you'd have to modify pattern files and accept increased miss rates) and theoretically possible but operationally impractical with deep learning (retraining takes hours and requires ML expertise).


The complete feature extraction code is in secrets_detector/features.py at github.com/pgmpofu/secrets-detector.

Next up: why the variable name is the single most important feature in secrets detection — and what that tells us about how developers accidentally expose credentials.