Security\Feed
Netson\Platform\Security\Feed · All classes
The site's security feed: report security events, such as failed logins, so the server can block an address that keeps failing.
Fails open: when the feature is off or the platform is not ready, every call returns
an unavailable Result and writes nothing; a failed append returns failed. Nothing
throws. Never pass a username or an email address, not even hashed.
Event types the platform acts on: repeated auth.failed (a wrong password),
auth.2fa_failed (a wrong second-factor code) and auth.lockout (your app locked
the account) from one address within a short time get that address blocked for a
while. auth.succeeded and auth.reset_requested (a password-reset link was
asked for) are recorded for our analysis and never block on their own. In Laravel
the provider already reports failed logins, lockouts, logins and failed two-factor
codes; you only add what Laravel has no event for, such as reset requests.
Example
/**
* Call this wherever a reset link is requested. Safe without the library, without a
* request address (console, queue) and when the platform is in trouble.
*/
function recordResetRequested(Request $request): void
{
$ip = $request->server('REMOTE_ADDR');
if (!class_exists(NetsonCloud::class) || !is_string($ip) || $ip === '') {
return; // no library, or no TCP peer: nothing to record
}
// REMOTE_ADDR, never $request->ip(): behind a proxy ip() follows X-Forwarded-For,
// which the client controls. Never record the email address, not even hashed.
app(NetsonCloud::class)->security()->record('auth.reset_requested', $ip);
}
Methods
record()
public function record(string $type, string $ip, array<string,mixed> $data = [], string $ipSource = 'connection'): Result
Record one security event for a visitor's address (e.g. 'auth.failed').
Call it at the moment the event happens, from the request of the visitor who caused it. One call is one event: report each failed attempt, not a count.
$type(string): what happened,<area>.<event>in lower case: one of the types in the class description, e.g.'auth.failed'; other types are recorded but never lead to a block$ip(string): the visitor's address as the server saw it:$_SERVER['REMOTE_ADDR'](Laravel:$request->server('REMOTE_ADDR')). Never$request->ip()or an X-Forwarded-For header: a visitor can forge those and get someone else blocked. An empty address is not recorded$data(array<string,mixed>): optional details as simple values, e.g.['known_user' => false]or['user_id' => 42]; never a username or email address, not even hashed$ipSource(string): for platform use; apps leave the default ('connection': $ip is the TCP peer of the request)
Returns Result: ok: recorded. unavailable: the feed is off here (or this is not a platform server); nothing was recorded. failed: it could not be written, e.g. an empty address
See the example at the top of this page.
recordUnattributed()
public function recordUnattributed(string $type, array<string,mixed> $data = []): Result
Record one security event without an address (it cannot lead to a block).
For events with no visitor behind them, such as an administrator added by a console command, a queued job or a cron run. We use these to spot unexpected changes, such as an administrator nobody created on purpose.
$type(string): what happened,<area>.<event>in lower case, e.g.'user.admin_granted'or'user.admin_revoked'(the types the WordPress plugin uses)$data(array<string,mixed>): optional details as simple values, e.g.['user_id' => 42, 'via' => 'console']; never a username or email address, not even hashed
Returns Result: ok: recorded. unavailable: the feed is off here; nothing was recorded. failed: it could not be written
Example
/**
* Report that a user became an administrator outside a web request, e.g. in an
* artisan command. Never stops the command: a failure is logged and that is all.
*/
function reportAdminGranted(int $userId, string $via): void
{
if (!class_exists(NetsonCloud::class)) {
return; // no library on this machine
}
$result = app(NetsonCloud::class)->security()->recordUnattributed(
'user.admin_granted',
['user_id' => $userId, 'via' => $via], // an id, never a name or email address
);
if ($result->isOk() || $result->status === Result::UNAVAILABLE) {
return; // recorded, or there is no feed here: both are fine
}
error_log('security feed: ' . $result->reason); // failed: note it and carry on
}