check-type-juggling

检测 PHP 类型转换相关漏洞。识别用户输入处的松散比较、未启用严格模式的 in_array 用法、switch 语句的类型强制转换问题,以及可能的哈希比较绕过情形。

仓库最近推送
GitHub Stars
109
Forks
26
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "check-type-juggling":https://skillpi.com/zh/skills/deepskill-5d69dc8c5ed88fcd618cae1e33d1bcde07b4e56fa12e1818f3e71359d900d277。安装前核对上游来源和版本:https://github.com/dykyi-roman/awesome-claude-code。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

检测 PHP 类型转换相关漏洞。识别用户输入处的松散比较、未启用严格模式的 in_array 用法、switch 语句的类型强制转换问题,以及可能的哈希比较绕过情形。

0 下载

介绍来源: third_party · dykyi-roman · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: check-type-juggling
description: Detects PHP type juggling vulnerabilities. Identifies loose comparison with user input, in_array without strict mode, switch statement type coercion, and hash comparison bypasses.

Type Juggling Security Check (A03:2021)

Analyze PHP code for type juggling vulnerabilities exploiting PHP's loose comparison behavior.

PHP version baseline: 8.4. PHP 8.0 changed number-to-string comparison (Saner string to number comparisons): an int compared with a non-numeric string is now compared as a string, so 0 == 'admin' is false. Do not report the pre-8.0 bypass. What survives is listed below — bools, numeric strings, null, and false-returning functions.

Detection Patterns

1. Loose Comparison with User Input

// CRITICAL: Loose == comparison with user input
if ($request->get('role') == 'admin') { } // true == 'admin' is true — a JSON body can send a bool
if ($id == '100') { }                     // '1e2' == '100' is true — both are numeric strings

// CRITICAL: Password comparison
if ($password == $storedHash) { }          // NEVER use == for security checks

// CORRECT: Strict comparison
if ($request->get('role') === 'admin') { }
if (hash_equals($expectedToken, $token)) { } // Timing-safe comparison

2. in_array Without Strict Mode

// CRITICAL: in_array defaults to loose comparison
$allowedRoles = ['admin', 'editor', 'viewer'];
if (in_array($request->get('role'), $allowedRoles)) { }
// in_array(true, ['admin', 'editor']) === true!  — bool vs non-empty string
// in_array(0, ['admin']) is false since PHP 8 — do not report that one

// VULNERABLE: numeric-string values still collide
$allowedIds = ['100', '200'];
if (in_array($input, $allowedIds)) { }
// '1e2' == '100' is true → '1e2' and ' 100' both pass

// CORRECT: Always use strict mode
if (in_array($request->get('role'), $allowedRoles, true)) { }

3. Switch Statement Type Coercion

// VULNERABLE: Switch uses loose comparison
switch ($request->get('id')) {
    case 100:   // '1e2' and ' 100' both match — numeric-string coercion
        $this->deleteAll();
        break;
    case 'view':
        $this->view();
        break;
}
// Since PHP 8 a non-numeric string no longer matches `case 0`,
// but numeric strings and bools still coerce into the wrong branch.

// CORRECT: Use match (strict comparison)
$result = match ($request->get('action')) {
    'view' => $this->view(),
    'edit' => $this->edit(),
    default => throw new InvalidActionException(),
};

4. Hash Comparison Bypass

// CRITICAL: comparing a false-returning function with == instead of ===
if (strpos($url, 'https://') == 0) { }
// strpos() returns false when not found, and false == 0 is true →
// every URL WITHOUT the prefix passes as "starts with https://"

// CORRECT
if (str_starts_with($url, 'https://')) { }

// NOTE: strcmp() with an array argument throws TypeError since PHP 8 —
// the old "strcmp() returns null" bypass no longer exists. Still use ===
// on its result, because == hides an int|false mix-up on sibling functions.

// CRITICAL: md5/sha1 magic hashes
if (md5($input) == '0') { }
// md5('240610708') = '0e462097431906509019562988736854'
// '0e...' == '0' is true (scientific notation: 0 * 10^... = 0)

// CRITICAL: Loose comparison of hashes
if (md5($a) == md5($b)) { }
// Two different inputs can have 0e... hashes → both equal 0

// CORRECT: hash_equals for hash comparison
if (hash_equals($expectedHash, md5($input))) { }

5. Null Coalescing with Loose Types

// VULNERABLE: isset + loose comparison
if (isset($data['admin']) && $data['admin'] == true) {
    $this->grantAdminAccess(); // 'yes', '1', 1, true all pass
}

// VULNERABLE: Empty check
if (!empty($request->get('verified'))) {
    // '0' is empty, but 'false' is not — inconsistent
}

// CORRECT: Explicit type check
if (($data['admin'] ?? false) === true) {
    $this->grantAdminAccess();
}

6. Array Key Type Juggling

// VULNERABLE: Numeric string keys become integers
$permissions = ['0' => 'none', '1' => 'read', '2' => 'write'];
$level = $request->get('level'); // String from request
$permission = $permissions[$level]; // '01' !== 1, but both exist in different contexts

// VULNERABLE: Boolean key
$config = [true => 'enabled', false => 'disabled'];
// true becomes 1, false becomes 0 as array keys

7. JSON Decode Type Juggling

// VULNERABLE: JSON sends a bool where a string is expected
$data = json_decode($request->getContent(), true);
if ($data['token'] == $validToken) { }
// JSON: {"token": true} → true == "any-non-empty-string" is true!
// (JSON: {"token": 0} no longer matches a non-numeric string in PHP 8)

// CORRECT: Validate type after decode
$data = json_decode($request->getContent(), true);
if (!is_string($data['token'] ?? null)) {
    throw new InvalidInputException('Token must be a string');
}
if (hash_equals($validToken, $data['token'])) { }

Grep Patterns

# Loose comparison with variables
Grep: "\\\$.*==\s*['\"]|['\"].*==\s*\\\$" --glob "**/*.php"
Grep: "==\s*true|==\s*false|==\s*null|==\s*0\b" --glob "**/*.php"

# in_array without strict — two-pass: ripgrep has no lookahead, so widen then confirm
# 1) list every call site, 2) Read each hit and check for a third `true` argument
Grep: "in_array\s*\(" --glob "**/*.php"

# switch instead of match (ripgrep uses `|`, never the BRE `\|`)
Grep: "switch\s*\(\\\$(request|input|data)" --glob "**/*.php"

# strcmp with loose comparison
Grep: "strcmp\(.*==\s*0|strcmp\(.*!=\s*0" --glob "**/*.php"

# Hash comparison with ==
Grep: "md5\(.*==|sha1\(.*==|hash\(.*==" --glob "**/*.php"

# array_search without strict — same two-pass approach as in_array
Grep: "array_search\s*\(" --glob "**/*.php"

Severity Classification

Pattern Severity
Token/hash comparison with == 🔴 Critical
Authentication check with == 🔴 Critical
false-returning function compared with == (e.g. strpos(...) == 0) 🔴 Critical
in_array without strict on security check 🟠 Major
JSON decode + loose comparison 🟠 Major
switch on user input (instead of match) 🟠 Major
in_array without strict (non-security) 🟡 Minor
General loose == usage 🟡 Minor

Do not report as Critical on PHP 8+: a bare in_array($string, $stringList) where every element is a non-numeric string. The pre-8.0 0 == 'admin' bypass is gone; the residual risk is a bool or numeric-string input, which is Major, not Critical. Confirm the input can actually be a bool or a numeric string before escalating.

PHP Type Juggling Reference

Verified against PHP 8.4. The first row is the one that changed in 8.0 — everything below it still holds.

Comparison Result Why
0 == 'admin' false PHP 8+: int is compared as a string against a non-numeric string
'1e2' == '100' true Both are numeric strings → compared numerically
' 1' == '1' true Leading whitespace is allowed in a numeric string
0 == null true null casts to 0
'' == null true null casts to ''
'0e1' == '0e2' true Both numeric strings evaluate to 0 (magic-hash bypass)
true == 'anything' true Non-empty string casts to true
[] == false true Empty array casts to false
false == 0 true The strpos()/strstr() false-return trap

Output Format

### Type Juggling: [Description]

**Severity:** 🔴/🟠/🟡
**Location:** `file.php:line`
**CWE:** CWE-1025 (Comparison Using Wrong Factors)
**OWASP:** A03:2021 — Injection

**Issue:**
[Description of the type juggling vulnerability]

**Exploit:**
Input `0` (integer) matches any non-numeric string via loose comparison.

**Code:**
```php
// Vulnerable code with ==

Fix:

// Fixed with === or hash_equals()

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值75/100

亮点

  • 针对 PHP 类型转换漏洞的专项检测,对开发者具有明确实用价值
  • 覆盖多种常见漏洞场景,包括松散比较、in_array、switch、哈希比较等
触发精准95/100

亮点

  • 触发词精准完整,覆盖多种变体,边界清晰
  • description 明确描述了检测 PHP 类型转换漏洞的功能
结构规范95/100

亮点

  • 文档结构清晰,包含检测模式、grep 模式、严重性分类、参考表格和输出格式
  • 步骤划分明确,有端到端示例,frontmatter 完整
执行可靠80/100

待提升项

  • 未处理 grep 结果误报 原报告严重程度:轻微

    grep 模式可能匹配到注释或字符串字面量中的代码,导致误报

    改进建议:建议增加对注释和字符串字面量的过滤逻辑,或提供后处理脚本

  • 未提及 PHP 版本自动检测 原报告严重程度:轻微

    Skill 依赖 PHP 8.4 的行为,但未提供自动检测目标代码 PHP 版本的机制

    改进建议:建议增加版本检测机制,或明确提示用户确认目标代码的 PHP 版本

亮点

  • 提供了具体的检测模式和 grep 命令,工具来源明确
  • 覆盖了主要异常场景,并给出了修复建议
证据可验 (前四项均值)86/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

文档引用的域名或地址

这些地址可能用于文档链接或示例,不是已批准的联网清单。使用前核对实际目的地及发送的数据。

  • wiki.php.net

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。