@biglogic/rgs 3.9.6 β 3.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/ssr.d.ts +73 -0
- package/core/thunk.d.ts +75 -0
- package/docs/README.md +479 -0
- package/docs/SUMMARY.md +55 -0
- package/docs/_config.yml +1 -0
- package/docs/markdown/api.md +381 -0
- package/docs/markdown/case-studies.md +69 -0
- package/docs/markdown/faq.md +53 -0
- package/docs/markdown/getting-started.md +68 -0
- package/docs/markdown/local-first-sync.md +146 -0
- package/docs/markdown/migration-guide.md +284 -0
- package/docs/markdown/persistence-and-safety.md +125 -0
- package/docs/markdown/philosophy.md +54 -0
- package/docs/markdown/plugin-sdk.md +161 -0
- package/docs/markdown/plugins-and-extensibility.md +82 -0
- package/docs/markdown/security-architecture.md +50 -0
- package/docs/markdown/the-magnetar-way.md +69 -0
- package/index.cjs +1004 -276
- package/index.d.ts +4 -0
- package/index.js +1206 -485
- package/package.json +2 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Security Architecture & Hardening
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Reactive Global State (RGS) is designed with a "Security-First" philosophy. Our architecture ensures that global state is not only reactive but protected against common web vulnerabilities and unauthorized access.
|
|
5
|
+
|
|
6
|
+
## 1. Data Sanitization (XSS Defense)
|
|
7
|
+
The `sanitizeValue` utility provides a robust baseline defense by stripping malicious content from strings and objects before they enter the store.
|
|
8
|
+
|
|
9
|
+
- **Scheme Blocking**: Specifically blocks `javascript:`, `vbscript:`, and `data:text/html` schemes.
|
|
10
|
+
- **Tag Removal**: Automatically removes dangerous HTML tags such as `<script>`, `<iframe>`, `<form>`, and `<meta>`.
|
|
11
|
+
- **Entity Removal**: Strips HTML entities (`&#...;`) to prevent obfuscation-based bypasses.
|
|
12
|
+
|
|
13
|
+
## 2. Advanced Deep Cloning
|
|
14
|
+
To ensure state immutability and prevent unintended side effects, RGS uses an intelligent cloning engine:
|
|
15
|
+
- **Native structuredClone**: Leverages the browser's native API for maximum performance.
|
|
16
|
+
- **Support for Collections**: Extends cloning capabilities to `Map` and `Set` objects.
|
|
17
|
+
- **Circular Reference Protection**: Uses `WeakMap` to handle complex nested structures safely.
|
|
18
|
+
|
|
19
|
+
## 3. Cryptography (AES-256-GCM)
|
|
20
|
+
The security module uses the Web Crypto API to provide high-performance, authenticated encryption:
|
|
21
|
+
- **AES-GCM**: Provides both confidentiality and integrity verification.
|
|
22
|
+
- **GCM (Galois/Counter Mode)**: Ensures that data has not been tampered with during storage.
|
|
23
|
+
- **Safe Random UUID**: Fallback generation for environments without Web Crypto API.
|
|
24
|
+
|
|
25
|
+
## 4. RBAC (Role-Based Access Control)
|
|
26
|
+
RGS supports fine-grained access rules:
|
|
27
|
+
- **Fail-Closed Design**: Access is denied by default if any rules are defined.
|
|
28
|
+
- **Regex Caching**: Store instances cache compiled regular expressions for ultra-fast permission checks.
|
|
29
|
+
- **ReDoS Protection**: Regex patterns have a 100ms timeout to prevent denial-of-service attacks.
|
|
30
|
+
- **Storage Key Validation**: All persisted keys are validated against a strict pattern before storage.
|
|
31
|
+
|
|
32
|
+
## 5. Security Best Practices
|
|
33
|
+
For real-world implementations, refer to the `examples/security-best-practices` directory, which covers:
|
|
34
|
+
- **Encryption Key Management**: Using `generateEncryptionKey()` for secure key generation.
|
|
35
|
+
- **Audit Logging**: Tracking all store modifications for compliance.
|
|
36
|
+
- **GDPR Compliance**: Managing user consent and data export/deletion.
|
|
37
|
+
|
|
38
|
+
## Summary of 2.9.5 Enhancements
|
|
39
|
+
- Robust regex patterns for `sanitizeValue`.
|
|
40
|
+
- Recursive sanitization for plain objects.
|
|
41
|
+
- `Map` and `Set` support in `deepClone`.
|
|
42
|
+
- **Exposed Metadata**: Store instances now expose read-only `namespace` and `userId`.
|
|
43
|
+
- **Direct Store Access**: Added `getStore()` utility for non-React contexts.
|
|
44
|
+
|
|
45
|
+
## Summary of 3.7.0 Enhancements
|
|
46
|
+
- **ReDoS Protection**: Regex patterns timeout after 100ms to prevent malicious patterns.
|
|
47
|
+
- **Safe UUID**: Fallback UUID generation for environments without Web Crypto API.
|
|
48
|
+
- **Storage Key Validation**: Keys validated before persistence to prevent injection.
|
|
49
|
+
- **Production Safety**: Global window access only enabled in development mode (`NODE_ENV !== 'production'`).
|
|
50
|
+
- **PBKDF2 Key Derivation**: New `deriveKeyFromPassword()` and `generateSalt()` functions for secure password-based encryption (NIST SP 800-132 compliant - 600k iterations, 32-byte salt).
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# π Chapter 3: The Magnetar Way - Peak Simplicity
|
|
2
|
+
|
|
3
|
+
If you are a fan of "Clean Code" or just hate writing imports, the **Magnetar** method is your new best friend.
|
|
4
|
+
|
|
5
|
+
It's a single function that creates the store and the hook simultaneously. **Zero config, 100% typed.**
|
|
6
|
+
|
|
7
|
+
## π οΈ The "Master" Example
|
|
8
|
+
|
|
9
|
+
Let's create a User Profile module.
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { gstate } from '@biglogic/rgs';
|
|
13
|
+
|
|
14
|
+
// 1. Define the state and create everything in ONE shot
|
|
15
|
+
// Pass a string namespace to enable automatic persistence to localStorage
|
|
16
|
+
export const useUser = gstate({
|
|
17
|
+
name: 'Guest',
|
|
18
|
+
isLogged: false,
|
|
19
|
+
preferences: { theme: 'dark', lang: 'en' }
|
|
20
|
+
}, 'user_module'); // Auto-persists to localStorage (excludes sensitive fields)
|
|
21
|
+
|
|
22
|
+
// Or use an object config for more control:
|
|
23
|
+
export const useUserNoPersist = gstate({
|
|
24
|
+
name: 'Guest',
|
|
25
|
+
isLogged: false
|
|
26
|
+
}, { namespace: 'user_module', autoPersist: false }); // No persistence
|
|
27
|
+
|
|
28
|
+
// 2. Use it anywhere
|
|
29
|
+
function ProfileHeader() {
|
|
30
|
+
const [name, setName] = useUser('name');
|
|
31
|
+
// Note: 'setName' is already typed! It only accepts strings.
|
|
32
|
+
|
|
33
|
+
return <h1>Hello, {name}!</h1>;
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## π Why Magnetar Rocks
|
|
38
|
+
|
|
39
|
+
1. **Inferred Types**: No need to define `<string>`. TypeScript looks at the initial value you passed to `gstate` and figures it out.
|
|
40
|
+
2. **Auto-Persistence**: When you pass a string namespace, RGS automatically persists to localStorage (excluding sensitive fields like tokens, passwords, secrets).
|
|
41
|
+
3. **Security**: Sensitive fields (containing 'token', 'password', 'secret', 'key') are automatically excluded from persistence.
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// You can access the store OUTSIDE components!
|
|
45
|
+
const currentUser = useUser.get('name');
|
|
46
|
+
useUser.set('isLogged', true);
|
|
47
|
+
|
|
48
|
+
// You can even compute data on the fly (Computed State)
|
|
49
|
+
useUser.compute('fullName', ['firstName', 'lastName'], (s) => `${s.firstName} ${s.lastName}`);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## π Production Strategy: "The Store Folder"
|
|
53
|
+
|
|
54
|
+
Don't scatter stores everywhere. Create a `src/stores` folder and put your Magnetar files there:
|
|
55
|
+
|
|
56
|
+
- `auth.ts`
|
|
57
|
+
- `cart.ts`
|
|
58
|
+
- `settings.ts`
|
|
59
|
+
|
|
60
|
+
Then import them whenever needed. Itβs clean, fast, and professional.
|
|
61
|
+
|
|
62
|
+
## π§ Reflection for the Senior Dev
|
|
63
|
+
|
|
64
|
+
*"Wait, does Magnetar create a new store every time?"*
|
|
65
|
+
Yes, and that's the point! You can have isolated micro-stores for every domain of your app, while still keeping the ability to make them talk to each other. Itβs the evolution of the "Atomic State" concept.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
**Next step:** [Persistence and Safety: Data Never Dies](persistence-and-safety.md)
|