Search by

hryvinskyi / magento2-client-address

hryvinskyi

Client address helpers for Magento 2: an anonymiser that keeps only the network part of an IPv4 or IPv6 address.

Package info

github.com/hryvinskyi/magento2-client-address

Type:magento2-module

pkg:composer/hryvinskyi/magento2-client-address

Statistics

Installs: 1

Dependents: 3

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-30 18:52 UTC

This package is auto-updated.

Last update: 2026-11-30 20:09:26 UTC


README

One anonymiser for the address of the person who sent a request. A module that keeps a note of where a request came from — a consent record, a journal row — asks for IpAnonymiserInterface and stores the network part of the address only, never the host part that identifies one connection.

What it does for a store

  • IPv4 keeps 24 bits. 203.0.113.77 is kept as 203.0.113.0.
  • IPv6 keeps 48 bits. 2001:db8:1:2:3::4 is kept as 2001:db8:1::.
  • An IPv4 address carried in IPv6 is the IPv4 address. ::ffff:203.0.113.77 is kept as 203.0.113.0.
  • One answer for every spelling. The address is read as bytes, so 2001:0DB8:ABCD:0012::0001 and 2001:db8:abcd:12::1 give the same answer, written the one way PHP writes it (2001:db8:abcd::).
  • Only one address is an address. A host name, a list of addresses, an address with a port, a zone or a network length, surrounding spaces and an empty text have no answer: anonymise() returns null.
  • Telling a kept address from a full one. isAnonymised() is true only for the anonymiser's own answer, so a store can refuse to keep a value that is a full address or an anonymised address spelled another way.

Requirements

  • Magento Open Source or Adobe Commerce, PHP 8.1 to 8.5 (the range composer.json allows).
  • magento/framework.

Installation

Run from the Magento root.

composer require hryvinskyi/magento2-client-address
bin/magento module:enable Hryvinskyi_ClientAddress
bin/magento setup:upgrade
bin/magento setup:di:compile  # production mode only
bin/magento cache:flush

The module has no settings and creates no table.

Using the anonymiser

use Hryvinskyi\ClientAddress\Api\IpAnonymiserInterface;

public function __construct(private readonly IpAnonymiserInterface $anonymiser)
{
}

// '203.0.113.0'; null when the text is not exactly one address.
$kept = $this->anonymiser->anonymise('203.0.113.77');

// true: '203.0.113.0' is what the anonymiser answers for itself.
$this->anonymiser->isAnonymised('203.0.113.0');

Limits

  • The anonymiser decides how much of an address is kept, not whether an address is kept at all. Whether a store keeps the anonymised address, the full one or none is the setting of the module that keeps it.
  • The address is taken as it is given. The module reads no request and trusts no forwarding header; the caller decides which address it passes.

Registrations

Where What
preference Hryvinskyi\ClientAddress\Api\IpAnonymiserInterface Hryvinskyi\ClientAddress\Model\IpAnonymiser

The module registers no route, command, cron job, event or plugin.

Extension point

IpAnonymiserInterface is @api and replaceable by a preference. Every module that asks for it then keeps addresses by the new rule.

<preference for="Hryvinskyi\ClientAddress\Api\IpAnonymiserInterface" type="Vendor\Module\Model\IpAnonymiser"/>

Tests

  • Unit: vendor/bin/phpunit -c app/code/Hryvinskyi/ClientAddress/phpunit.xml.dist, run from the Magento root.
  • Static analysis: PHPStan 2.x at level 10 with the package's phpstan.neon.dist (phpstan analyse -c app/code/Hryvinskyi/ClientAddress/phpstan.neon.dist --level=10), and vendor/bin/phpcs --standard=app/code/Hryvinskyi/ClientAddress/phpcs.xml.dist app/code/Hryvinskyi/ClientAddress.