Skip to content

nis2shield/react-guard

⚠️ DEPRECATED / MOVED

This repository has been merged into the NIS2 Shield Frontend Monorepo.

Please use @nis2shield/react-guard from the monorepo (packages/react) for the latest updates, security patches, and features. This standalone repository is now ARCHIVED and will no longer receive updates.


React NIS2 Guard (@nis2shield/react-guard)

npm version License: MIT React CI TypeScript

Client-Side Security Telemetry & Session Protection for NIS2 Compliance.

Why this package?

Companies subject to NIS2 Directive require strict session management, audit logs, and client-side security controls. This library provides drop-in React components to handle:

  1. Automatic session termination (Idle Timer) - Art. 21.2.h
  2. Visual protection against shoulder surfing (Tab Napping)
  3. Security event hooks for SIEM integration
  4. Encrypted local storage for sensitive data (GDPR-compliant)

@nis2shield/react-guard acts as the "sentinel" for your frontend applications, integrating with any NIS2 Shield Backend Adapter (Django, Express, Spring) to provide end-to-end compliance coverage.

Part of the NIS2 Shield Ecosystem: Use with infrastructure for Demonstrable Compliance (audited via tfsec).

✨ Features

  • πŸ›‘οΈ Session Watchdog: Detects user inactivity and "Tab Napping" (background tab hijacking risks)
  • πŸ“‘ Telemetry Engine: Automatically captures React component crashes (AuditBoundary) and sends sanitized reports to your SIEM
  • πŸ” Secure Storage: Drop-in replacement for localStorage/sessionStorage with AES-GCM encryption
  • ⌨️ Secure Input: Pre-configured props to harden input fields against caching and clipboard
  • πŸ” Device Fingerprinting (v0.2.0+): Passive device fingerprint collection for session hijacking detection
  • ⚠️ Security Banner (v0.2.0+): Warns users about insecure connections (HTTP) and outdated browsers

πŸ“¦ Installation

npm install @nis2shield/react-guard
# or
yarn add @nis2shield/react-guard

πŸš€ Quick Start

1. Wrap your App

import { Nis2Provider, SessionWatchdog, AuditBoundary } from '@nis2shield/react-guard';

function App() {
  return (
    <Nis2Provider 
      config={{
        auditEndpoint: '/api/nis2/telemetry/',
        idleTimeoutMinutes: 15,
        debug: process.env.NODE_ENV === 'development'
      }}
    >
      <AuditBoundary fallback={<h1>Security Alert</h1>}>
        <SessionWatchdog onIdle={() => window.location.href = '/logout'} />
        <YourMainApp />
      </AuditBoundary>
    </Nis2Provider>
  );
}

2. Protect Sensitive Data

import { useSecureStorage } from '@nis2shield/react-guard';

const UserProfile = () => {
  const { value: iban, setValue: setIban } = useSecureStorage('user_iban', '');

  return (
    <input 
      value={iban} 
      onChange={(e) => setIban(e.target.value)} 
      placeholder="IBAN (Encrypted locally)"
    />
  );
};

3. Harden Input Fields

import { useSecureInput } from '@nis2shield/react-guard';

const PasswordField = () => {
  const secureProps = useSecureInput({ type: 'password' });
  return <input {...secureProps} placeholder="Enter Password" />;
};

4. Report Custom Incidents

import { useNis2Log } from '@nis2shield/react-guard';

const TransferMoney = () => {
  const { logWarning } = useNis2Log();

  const handleTransfer = (amount: number) => {
    if (amount > 10000) {
      logWarning('HIGH_VALUE_TRANSACTION_ATTEMPT', { amount });
    }
  };
};

5. Device Fingerprinting (v0.2.0+)

Collect passive device fingerprints to detect session hijacking:

import { useDeviceFingerprint } from '@nis2shield/react-guard';

const LoginPage = () => {
  const { fingerprint, isLoading, sendToBackend } = useDeviceFingerprint();

  const handleLogin = async () => {
    // Send fingerprint with login for backend validation
    sendToBackend();
    // ... rest of login logic
  };

  return <button onClick={handleLogin} disabled={isLoading}>Login</button>;
};

Collected data:

  • Screen resolution, color depth
  • Timezone, language, platform
  • Hardware concurrency, device memory
  • Canvas fingerprint (SHA-256 hash)
  • WebGL renderer/vendor

πŸ”— NIS2 Shield Ecosystem

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Frontend                              β”‚
β”‚  @nis2shield/react-guard                                    β”‚
β”‚  β”œβ”€β”€ SessionWatchdog (idle detection)                       β”‚
β”‚  β”œβ”€β”€ AuditBoundary (crash reports)                         β”‚
β”‚  β”œβ”€β”€ useDeviceFingerprint (session validation)             β”‚
β”‚  └── β†’ POST /api/nis2/telemetry/                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Backend (NIS2 Adapter)                      β”‚
β”‚  Supported: Django, Express, Spring Boot, .NET            β”‚
β”‚  β”œβ”€β”€ ForensicLogger (HMAC signed logs)                     β”‚
β”‚  β”œβ”€β”€ RateLimiter, SessionGuard, TorBlocker                 β”‚
β”‚  └── β†’ SIEM (Elasticsearch, Splunk, QRadar, etc.)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Infrastructure                            β”‚
β”‚  nis2shield/infrastructure                                  β”‚
β”‚  β”œβ”€β”€ Centralized Logging (ELK/Splunk)                       β”‚
β”‚  β”œβ”€β”€ Compliance Reporting (Automatic PDF generation)        β”‚
β”‚  └── Audited Deployment (Terraform/Helm)                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“– Recipes

Banking Dashboard with Auto-Logout

import { Nis2Provider, SessionWatchdog, AuditBoundary, useSecureStorage } from '@nis2shield/react-guard';

function BankingApp() {
  return (
    <Nis2Provider config={{ idleTimeoutMinutes: 5, auditEndpoint: '/api/audit/' }}>
      <AuditBoundary fallback={<SecurityLockScreen />}>
        <SessionWatchdog 
          onIdle={() => { 
            window.location.href = '/logout?reason=idle';
          }} 
        />
        <Dashboard />
      </AuditBoundary>
    </Nis2Provider>
  );
}

Protected Form with Encrypted Fields

import { useSecureStorage, useSecureInput } from '@nis2shield/react-guard';

const PaymentForm = () => {
  const { value: cardNumber, setValue: setCardNumber } = useSecureStorage('card', '');
  const secureProps = useSecureInput({ type: 'password' });
  
  return (
    <form>
      <input 
        value={cardNumber} 
        onChange={(e) => setCardNumber(e.target.value)}
        placeholder="Card Number (encrypted locally)"
      />
      <input {...secureProps} placeholder="CVV" />
    </form>
  );
};

Login with Device Fingerprinting

import { useDeviceFingerprint, useNis2Log } from '@nis2shield/react-guard';

const LoginPage = () => {
  const { fingerprint, sendToBackend } = useDeviceFingerprint();
  const { logWarning } = useNis2Log();

  const handleLogin = async (credentials) => {
    // Send fingerprint with login for session hijacking detection
    await sendToBackend();
    
    // Log high-risk attempts
    if (credentials.failedAttempts > 3) {
      logWarning('BRUTE_FORCE_ATTEMPT', { attempts: credentials.failedAttempts });
    }
  };
};

πŸ§ͺ Development

npm install      # Install dependencies
npm test         # Run test suite (35 tests)
npm run build    # Build for production

πŸ“„ License

MIT License - see LICENSE for details.

🀝 Contributing

See CONTRIBUTING.md for guidelines.


Documentation Β· npm Β· Changelog

About

πŸ›‘οΈ Client-Side Security Telemetry & Session Protection for React - NIS2 Compliance

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published