Search by

qiick / laravel-settings

LucaGiardi20

Scoped, typed settings and encrypted secrets for qiick Laravel products.

Package info

github.com/qiick-io/laravel-settings

pkg:composer/qiick/laravel-settings

Statistics

Installs: 1 988

Dependents: 0

Suggesters: 1

Stars: 0

Open Issues: 8

v0.1.0-beta.2 2026-11-28 10:36 UTC

This package is auto-updated.

Last update: 2026-12-04 17:40:57 UTC


README

laravel-settings: Settings core for Laravel products

qiick/laravel-settings

OpenSSF Scorecard

PHPStan at max and the existing CI checks cover PHP. CodeQL covers GitHub Actions and JavaScript/TypeScript.

Scoped, typed settings and encrypted secrets for qiick Laravel products.

Pre-release. 0.1.0-beta.2 is on Packagist for qiick products. Public APIs and the storage schema can change before 1.0.0. See the upgrade guide before adopting it in a product with existing settings.

Requirements

PHP 8.3 or later in the 8.x series, and Laravel 12 or 13.

Installation

Install the beta from Packagist in your Laravel application.

composer require qiick/laravel-settings:^0.1@beta

Configuration

Publish the configuration if the application needs to override a default. Run the migrations before the deploy check.

php artisan vendor:publish --tag=settings-config
php artisan migrate
php artisan settings:check

Declaring settings

Implement Qiick\Settings\Contracts\SettingsSchemaContract and tag the implementation in your Laravel service provider with SettingsSchemaContract::class. Declare each setting key once across all tagged schemas.

return [
    Definition::bool('two_factor_required')->default(false),
    Definition::json('sidebar_modules')->default(fn () => ['overview' => true]),
    Definition::enum('password_policy', PasswordPolicy::class)->default(PasswordPolicy::Required),
    Definition::string('oidc_client_secret')->secret(),
];

Definitions use the app group and allow only the instance scope by default. Use in() and scopes() to opt into other groups and scopes. Closures supply defaults on demand.

json() accepts PHP lists and associative arrays. An enum definition accepts instances of the declared backed enum and stores their backing values. A stored backing value that fails validation falls back to the default and reports SettingTypeMismatchException.

Secret definitions use string()->secret() and have a null default. The package reads secrets from encrypted database values. Products that provide their own environment fallback handle it in application code.

Organisation scopes

ScopeRef::userInOrganisation($user, $organisationUuid) addresses a user's settings inside one organisation; ScopeRef::user($user) stays their global settings, and both can coexist. ScopeRef::organisation($uuid) is the organisation's own settings. The organisation is part of the natural key, so each is its own group with its own revision.

History

Every write passes each key to SettingsChangeRecorderContract inside its transaction, with old and new values (secrets as [redacted]), the revision and the actor. The package binds a no-op; qiick/laravel-audit records the changes in the activity log when it is installed, and a product can bind its own recorder. A recorder that throws rolls the write back.

Reading and editing settings

Resolve Qiick\Settings\Contracts\SettingsContract from the Laravel container. Typed reads use the stored value or the declared default. Ordinary group reads omit secrets, and forEdit() exposes a has_<key> flag. Call secret($key)?->reveal() when the caller needs plaintext.

Typed reads throw InvalidArgumentException for an undeclared key, a type or enum mismatch, or a scope the definition does not allow. When a stored non-secret value fails validation, the reader reports SettingTypeMismatchException and returns the declared default.

After a set() or forget() commits, SettingsUpdatedEvent carries the scope, group and changed key names.

Read the group's revision($group, $scope) before reading its edit values. Pass that revision as expectedRevision to set() when saving. A conditional write targets one group and throws StaleSettingException if another write changed its revision. The revision is monotonic across writes and clears. An empty group starts at zero.

forget() clears the stored value and advances the revision, so subsequent reads use the default. An unconditional set() can update several groups in one transaction.

Request and job memoization

Each request or job reads committed settings from the database and reuses the result for that group within the same scope. Reads inside a transaction go directly to the database. Writes invalidate the memo after the outer transaction commits. The memo holds only ciphertext for secret settings. A fresh request or job sees changes committed by other processes.

User erasure

Resolve Qiick\Settings\Erasure\SettingsEraser from Laravel in the product's erasure orchestrator. A full erasure deletes the user's settings (global and per organisation), clears the user as updated_by on rows they last wrote in other scopes, and clears their request or job memo after commit. With an organisation ID it does the same only for rows of that organisation, which serves removing a person from one organisation. See the public API for arguments and reporting.

Maintenance commands

settings:verify checks declared keys, scopes and stored encryption.

Encryption format and key rotation

A secret's encryption column names its format: v2:<cipher>:<key id>, where <cipher> is the app.cipher it was written with and <key id> a 16-hex one-way fingerprint of the APP_KEY that wrote it. Reads take that key from APP_KEY / APP_PREVIOUS_KEYS and that cipher, so a key rotation or an app.cipher change keeps every row readable without a table migration. Rows written before this format carry laravel-v1, a bare Laravel payload read with the current app.cipher, APP_KEY and APP_PREVIOUS_KEYS. Any other marker, an unsupported cipher or a key id no configured key matches fails closed (UndecryptableSettingException).

To rotate APP_KEY:

  1. Move the old key into APP_PREVIOUS_KEYS, set the new APP_KEY and deploy. New writes use the new key; old rows still read.
  2. php artisan settings:reencrypt --dry-run counts rows under a previous key, an old cipher or laravel-v1; php artisan settings:reencrypt moves them to the current marker under the group write lock. It exits non-zero, without printing a value, when a row cannot be read.
  3. Run settings:verify, and once a dry run reports 0 rows, remove the old key from APP_PREVIOUS_KEYS. Keep that key with any export or database backup taken before step 2: restoring it needs the key it was encrypted with.

To change app.cipher, run settings:reencrypt first so no laravel-v1 row is left (it names no cipher and is read with the current one), then change the cipher (with a key of the length it needs, keeping the old key in APP_PREVIOUS_KEYS if it changes) and run settings:reencrypt again. A later format version ships as a reader first and becomes the writer one release later, so a rollback still reads every row.

php artisan settings:export > settings.json
php artisan settings:import settings.json --dry-run
php artisan settings:import settings.json

The JSON export contains ciphertext bound to the current APP_KEY; export refuses a secret that only a previous key reads, so run settings:reencrypt first. Treat it as a database backup and protect it accordingly. Use the same key and declared schema when restoring. The import validates the entire file before writing and restores revision, metadata and timestamps in one transaction on an empty settings table. Dry run validates the file without changing storage.

Threat model

Assets. Product configuration that changes security behaviour (two-factor requirements, password policy, alert routing) and the secrets stored next to it (OIDC client secrets, webhook and API credentials).

Trust boundaries. Values written through SettingsContract::set() come from product code, often from an admin form: the package treats the key, scope and value as untrusted and validates them against the declared schema. The product decides who may edit which scope (its Policies). The application's APP_KEY and database credentials are trusted.

Threat Control Checklist
Duplicate instance rows through a NULL scope ID scope_id is never null; unique natural key (scope, scope ID, group, key) with case-sensitive identity on every engine S1
A secret leaks into a page, log, exception or memo forEdit() exposes has_<key> only; Secret redacts every representation; the memo holds ciphertext S2
A declared secret is stored in plaintext writes encrypt (encryption = v2:<cipher>:<key id>); settings:verify fails on a plaintext row S3
Mass assignment of arbitrary keys writes refuse undeclared keys and keys outside the definition's scopes S4
Scope confusion unregistered scopes and non-empty IDs for instance-like scopes are refused; authorization stays in the product Policy S5
Stale security settings in workers per-request or per-job memo, refreshed from committed data S6
A rolled-back write poisons the memo the memo is invalidated after the outer commit only S7
Lost update between two admins group revision with expectedRevision, and 64 ordered write-lock buckets (crc32 of scope, ID and group) that serialise writers even before a group's first row exists S8, SettingsWriteRaceTest
Key rotation breaks secrets APP_PREVIOUS_KEYS keeps them readable; settings:reencrypt moves rows under the group lock S9
An undecryptable secret enables a default (fail open) strict mode throws UndecryptableSettingException; non-strict reads it as absent S10
A partial write or restore multi-group writes, archive imports and the eraser each run in one transaction; an import validates the whole file before writing SettingsRuntimeTest, SettingsArchiveTest

Out of scope, owned by the product. Authorization of who edits which scope, environment-variable fallbacks for secrets, protection of settings:export files (they hold ciphertext bound to APP_KEY), database backups, and the APP_KEY itself: anyone holding it and the database can read every secret.

How each design standard is met: docs/standards-traceability.md.

License

MIT