|
| 1 | +# Ginkelsoft Laravel Encrypted Search Index |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Modern applications that handle sensitive user data—such as healthcare, financial, or membership systems—must ensure that all personally identifiable information (PII) is properly encrypted at rest. However, standard encryption creates a practical challenge: **once data is encrypted, it can no longer be searched efficiently.** |
| 6 | + |
| 7 | +Laravel's built-in `Crypt` system offers strong encryption (AES-256-CBC) but provides no mechanism for searching encrypted values. Some systems attempt to address this by storing partial plaintext or using blind indexes, which can leak statistical patterns and increase the risk of correlation attacks. |
| 8 | + |
| 9 | +The **Laravel Encrypted Search Index** package provides a clean, secure, and scalable alternative. It allows encrypted model fields to be **searched using deterministic hashed tokens**, without ever exposing plaintext data. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Statement |
| 14 | + |
| 15 | +### The traditional trade-off |
| 16 | + |
| 17 | +When data is fully encrypted, you lose the ability to perform meaningful queries. Developers must choose between: |
| 18 | + |
| 19 | +1. **Strong security (no search):** Encrypt every value with a random IV; searches become impossible. |
| 20 | +2. **Weak security (searchable):** Store hashed or partially-encrypted values that can be compared, leaking patterns. |
| 21 | + |
| 22 | +This package removes that trade-off by introducing a **detached searchable index** that maps encrypted records to deterministic tokens. |
| 23 | + |
| 24 | +---\n |
| 25 | + |
| 26 | +## Key Features |
| 27 | + |
| 28 | +* **Searchable encryption**: Enables exact and prefix-based searches over encrypted data. |
| 29 | +* **Detached search index**: Tokens are stored separately from the main data, reducing exposure risk. |
| 30 | +* **Deterministic hashing with peppering**: Each token is derived from normalized text combined with a secret pepper, preventing reverse-engineering. |
| 31 | +* **No blind indexes in primary tables**: Encrypted fields remain opaque—only hashed references are stored elsewhere. |
| 32 | +* **High scalability**: Indexes can handle millions of records efficiently using native database indexes. |
| 33 | +* **Laravel-native integration**: Fully compatible with Eloquent models, query scopes, and events. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## How It Works |
| 38 | + |
| 39 | +Each model can declare specific fields as searchable. When the model is saved, a background process normalizes the field value, generates one or more hashed tokens, and stores them in a separate database table named `encrypted_search_index`. |
| 40 | + |
| 41 | +When you search, the package hashes your input using the same process and retrieves matching model IDs from the index. |
| 42 | + |
| 43 | +### 1. Token Generation |
| 44 | + |
| 45 | +For each configured field: |
| 46 | + |
| 47 | +* **Exact match token:** A SHA-256 hash of the normalized value plus a secret pepper. |
| 48 | +* **Prefix tokens:** Multiple SHA-256 hashes representing progressive prefixes of the normalized text (e.g., `w`, `wi`, `wie`). |
| 49 | + |
| 50 | +### 2. Token Storage |
| 51 | + |
| 52 | +All tokens are stored in `encrypted_search_index` with the following structure: |
| 53 | + |
| 54 | +| model_type | model_id | field | type | token | |
| 55 | +| ----------------- | -------- | ---------- | ------ | ------ | |
| 56 | +| App\Models\Client | 42 | last_names | exact | [hash] | |
| 57 | +| App\Models\Client | 42 | last_names | prefix | [hash] | |
| 58 | + |
| 59 | +### 3. Querying |
| 60 | + |
| 61 | +The package provides two Eloquent scopes: |
| 62 | + |
| 63 | +```php |
| 64 | +Client::encryptedExact('last_names', 'Vermeer')->get(); |
| 65 | +Client::encryptedPrefix('first_names', 'Wie')->get(); |
| 66 | +``` |
| 67 | + |
| 68 | +These queries use database-level indexes for efficient lookups even on large datasets. |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## Security Model |
| 73 | + |
| 74 | +| Threat | Mitigation | |
| 75 | +| ----------------------- | --------------------------------------------------------------------------- | |
| 76 | +| Database dump or breach | Tokens cannot be reversed to plaintext (salted and peppered SHA-256). | |
| 77 | +| Statistical analysis | Tokens are fully detached; frequency analysis yields no useful correlation. | |
| 78 | +| Insider access | No sensitive data in the index table; encrypted fields remain opaque. | |
| 79 | +| Leaked `APP_KEY` | Does not affect token security; the pepper is stored separately in `.env`. | |
| 80 | + |
| 81 | +The system follows a **defense-in-depth** approach: encrypted data remains fully protected, while token search provides limited, controlled visibility for queries. |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +## Installation |
| 86 | + |
| 87 | +```bash |
| 88 | +composer require ginkelsoft/laravel-encrypted-search-index |
| 89 | +php artisan vendor:publish --tag=config |
| 90 | +php artisan migrate |
| 91 | +``` |
| 92 | + |
| 93 | +Update your `.env` file with a unique pepper: |
| 94 | + |
| 95 | +``` |
| 96 | +SEARCH_PEPPER=your-random-secret-string |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## Configuration |
| 102 | + |
| 103 | +`config/encrypted-search.php` |
| 104 | + |
| 105 | +```php |
| 106 | +return [ |
| 107 | + 'search_pepper' => env('SEARCH_PEPPER', ''), |
| 108 | + 'max_prefix_depth' => 6, |
| 109 | +]; |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Usage |
| 115 | + |
| 116 | +### Model Setup |
| 117 | + |
| 118 | +```php |
| 119 | +use Illuminate\Database\Eloquent\Model; |
| 120 | +use Ginkelsoft\EncryptedSearch\Traits\HasEncryptedSearchIndex; |
| 121 | + |
| 122 | +class Client extends Model |
| 123 | +{ |
| 124 | + use HasEncryptedSearchIndex; |
| 125 | + |
| 126 | + protected array $encryptedSearch = [ |
| 127 | + 'first_names' => ['exact' => true, 'prefix' => true], |
| 128 | + 'last_names' => ['exact' => true, 'prefix' => true], |
| 129 | + 'bsn' => ['exact' => true], |
| 130 | + ]; |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +When a `Client` record is saved, its searchable tokens are automatically created or updated in the `encrypted_search_index` table. |
| 135 | + |
| 136 | +### Searching |
| 137 | + |
| 138 | +```php |
| 139 | +// Exact match search |
| 140 | +$clients = Client::encryptedExact('last_names', 'Vermeer')->get(); |
| 141 | + |
| 142 | +// Prefix match search |
| 143 | +$clients = Client::encryptedPrefix('first_names', 'Wie')->get(); |
| 144 | +``` |
| 145 | + |
| 146 | +### Rebuilding the Index |
| 147 | + |
| 148 | +You can rebuild the entire search index using an Artisan command: |
| 149 | + |
| 150 | +```bash |
| 151 | +php artisan encryption:index-rebuild "App\\Models\\Client" |
| 152 | +``` |
| 153 | + |
| 154 | +This will reprocess all searchable fields for the specified model. |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## Scalability and Performance |
| 159 | + |
| 160 | +* **Optimized database lookups**: The `encrypted_search_index` table uses compound indexes for fast token-based lookups. |
| 161 | +* **Chunked rebuilds**: The `index-rebuild` command supports chunked processing to handle large datasets efficiently. |
| 162 | +* **Asynchronous rebuilds**: Can be safely run in queues or background jobs. |
| 163 | + |
| 164 | +Unlike in-memory search systems, this index-based approach scales linearly with the size of your dataset and can efficiently handle millions of records. |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +## Compliance |
| 169 | + |
| 170 | +This approach aligns with major privacy and compliance frameworks: |
| 171 | + |
| 172 | +* GDPR: Minimal data exposure; encrypted and hashed data separation. |
| 173 | +* HIPAA: Ensures ePHI remains protected even in breach scenarios. |
| 174 | +* ISO 27001: Supports layered security controls for data confidentiality. |
| 175 | + |
| 176 | +--- |
| 177 | + |
| 178 | +## License |
| 179 | + |
| 180 | +MIT License |
| 181 | +(c) 2025 Ginkelsoft |
0 commit comments