tuzelko/yii2-pwned-passwords 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tuzelko/yii2-pwned-passwords

Composer 安装命令:

composer require tuzelko/yii2-pwned-passwords

包简介

Have I Been Pwned password breach check (k-anonymity API) for Yii2 framework

README 文档

README

Project Status: Active Tests Latest Version PHP Version Total Downloads License

Password breach check for the Yii2 framework, backed by the Have I Been Pwned Pwned Passwords API.

Checks whether a password appears in known data breaches without ever sending the password (or its full hash) anywhere — only the first 5 characters of the SHA-1 hash are transmitted (k-anonymity model), and the match is performed locally.

Features

  • k-anonymity — only a 5-character hash prefix leaves your application
  • Yii component — configure once, inject anywhere (DI container friendly)
  • Shared HTTP client — reuses the GuzzleHttp\ClientInterface from your DI container when one is registered; falls back to a default Guzzle client otherwise
  • Response padding — optional Add-Padding mode so even the response size leaks nothing
  • Breach countgetHits() returns how many times the password was seen in breaches
  • Zero configuration — sensible defaults, override only what you need

Requirements

  • PHP >= 8.0
  • yiisoft/yii2 ~2.0
  • guzzlehttp/guzzle ^7.4

Installation

composer require tuzelko/yii2-pwned-passwords

Quick start

use tuzelko\yii\pwnedpasswords\PwnedPasswords;

$pwned = new PwnedPasswords();

$pwned->isPwned('password');  // true — seen in millions of breaches
$pwned->getHits('password');  // e.g. 9659365 — number of breach occurrences

Or register it as an application component:

// config/web.php
'components' => [
    'pwnedPasswords' => [
        'class'   => \tuzelko\yii\pwnedpasswords\PwnedPasswords::class,
        'padding' => true,
    ],
],
if (Yii::$app->pwnedPasswords->isPwned($form->password)) {
    $form->addError('password', 'This password has been found in a data breach.');
}

Usage in a validator

A typical place for a breach check is a password policy validator on a form:

use tuzelko\yii\pwnedpasswords\PwnedPasswords;
use yii\validators\Validator;

class PasswordBreachValidator extends Validator
{
    public function validateAttribute($model, $attribute): void
    {
        try {
            $pwned = Yii::$container->get(PwnedPasswords::class);
            if ($pwned->isPwned($model->$attribute)) {
                $this->addError($model, $attribute, 'This password has been found in a data breach. Please choose a different one.');
            }
        } catch (\Throwable) {
            // Decide your fail-open / fail-closed policy here
            $this->addError($model, $attribute, 'Unable to verify password against breach database. Please try again later.');
        }
    }
}

Configuration

Property Type Default Description
apiUrl string https://api.pwnedpasswords.com API base URL (override for a proxy or a self-hosted mirror)
padding bool false Send Add-Padding: true so responses are padded with fake zero-hit entries and their size leaks nothing about the requested range
requestOptions array [] Extra Guzzle request options merged into every API call (timeouts, proxy, headers, ...)
$pwned = new PwnedPasswords([
    'apiUrl'         => 'https://hibp-mirror.internal',
    'padding'        => true,
    'requestOptions' => ['timeout' => 5, 'connect_timeout' => 2],
]);

HTTP client resolution

On init() the component looks for a GuzzleHttp\ClientInterface definition in Yii::$container:

  • registered — your application's client is reused (with its base timeouts, middleware, etc.);
  • not registered — a plain GuzzleHttp\Client is created.
// config/main.php — share one configured client across the application
'container' => [
    'singletons' => [
        \GuzzleHttp\ClientInterface::class => static fn () => new \GuzzleHttp\Client([
            'timeout'         => 30,
            'connect_timeout' => 10,
        ]),
    ],
],

Error handling

Condition Result
Empty password InvalidArgumentException
Transport / HTTP error (4xx, 5xx, timeout) GuzzleHttp\Exception\GuzzleException
Unexpected non-200 success status RuntimeException

The component never fails silently — decide at the call site whether a check failure should block the user (fail-closed) or be ignored (fail-open).

How k-anonymity works

  1. The password is hashed with SHA-1 locally.
  2. Only the first 5 characters of the hash are sent: GET /range/5BAA6.
  3. The API returns every known hash suffix in that range (~800–1000 entries) with breach counts.
  4. The full hash is matched against the list locally.

The API operator never learns the password, its hash, or even whether a match occurred.

Running tests

make test

Tests run inside Docker (PHP 8.3) with no local setup required and no real HTTP calls (Guzzle mock handler).

License

MIT — see LICENSE.

tuzelko/yii2-pwned-passwords 适用场景与选型建议

tuzelko/yii2-pwned-passwords 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 80 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「security」 「extension」 「password」 「yii2」 「hibp」 「pwned-passwords」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tuzelko/yii2-pwned-passwords 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tuzelko/yii2-pwned-passwords 我们能提供哪些服务?
定制开发 / 二次开发

基于 tuzelko/yii2-pwned-passwords 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 80
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 38
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-10