hryvinskyi / magento2-client-address
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
Requires
- php: ~8.1.0||~8.2.0||~8.3.0||~8.4.0||~8.5.0
- magento/framework: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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.77is kept as203.0.113.0. - IPv6 keeps 48 bits.
2001:db8:1:2:3::4is kept as2001:db8:1::. - An IPv4 address carried in IPv6 is the IPv4 address.
::ffff:203.0.113.77is kept as203.0.113.0. - One answer for every spelling. The address is read as bytes, so
2001:0DB8:ABCD:0012::0001and2001:db8:abcd:12::1give 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()returnsnull. - 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.jsonallows). 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), andvendor/bin/phpcs --standard=app/code/Hryvinskyi/ClientAddress/phpcs.xml.dist app/code/Hryvinskyi/ClientAddress.