@smartsoft001/crud-shell-nestjs
v2.211.0
Published
Utils to crud
Maintainers
Readme
📦 @smartsoft001/crud-shell-nestjs
🚀 Usage
npm i @smartsoft001/crud-shell-nestjs
🛠️ Modules
CrudShellNestjsModule
- Provides the main integration module for CRUD features in a NestJS app.
- Static method:
forRoot(options)— Registers controllers, providers, and imports required modules with the given configuration.
CrudShellNestjsCoreModule
- Provides a core integration module for CRUD features in a NestJS app.
- Static method:
forRoot(options)— Registers providers and imports required modules with the given configuration.
🛠️ Controllers & Methods
CrudController
🔒 Attachment access
The attachment routes are denied unless SharedConfig.attachmentPolicy returns exactly true. The
policy is called with { operation, id, user } (operation is create, read or delete) before
storage is read or changed. A valid JWT is not ownership: resolve owner, tenant and roles from data
the server trusts, and never return an unconditional true for private files.
- Linking a file to its uploader. For
create, the controller generates the id first and passes it to the policy before reading the body. Recorduseragainst thatidthere and returntrue; laterreadanddeletechecks look the id up. A rejected upload can leave a record for an id with no file. - The stock Angular UI sends no token for files. The PDF and attachment displays open files
with
FileService.download(window.open), and the image, video andsmartFileUrldisplays loadFileService.getUrlas plain URLs, so these are anonymous reads. With the stock UI, allow anonymousreadfor files meant to be viewable. - Status codes. A refusal is a
DomainForbiddenError; it becomes403only whenAppExceptionFilterfrom@smartsoft001/nestjsis registered (otherwise500). A missing token on upload or delete is401. - Limits. One file of at most
attachmentMaxBytes(default 10 MiB) and at mostattachmentMaxFieldsextra form fields (default0). The file is buffered before storage; oversized, multi-file or extra-field requests answer413and nothing is stored. The upload answers only after the storage write finished.
Migration
The attachment routes used to have no guard. After upgrading: configure attachmentPolicy, register
AppExceptionFilter, send a Bearer token on uploads and deletes, and raise attachmentMaxBytes or
attachmentMaxFields if clients send larger files or extra form fields. Existing files have no
ownership records; the policy decides how to treat them.
🛠️ Gateways & Methods
CrudGateway
🛡️ Query bounds and exports
GET / checks the query string before it reaches MongoDB and answers anything outside these bounds with 400 Bad Request:
- Operators:
=,!=,>,>=,<,<=,~=and repeated keys (joined into one$in, at most 100 values). Raw Mongo operators ($where,$-keys other than$search,field:op=value) and/regex/values are refused: they reach MongoDB as a$matchstage, where an unbounded operator lets one request keep the database busy (denial of service). - Field names: letters, digits and underscores, dotted for nested fields, at most 128 characters;
__proto__,prototypeandconstructorare refused. ~=and$searchare matched literally (the text is escaped before it becomes a regular expression) and are at most 256 characters.$searchis always treated as text, so48,007andtrueare searched as written. Other values are at most 1024 characters, the encoded query at most 4096.limitis a positive integer, lowered tomaxQueryLimit(default100), which is also the page size when nolimitis sent.offsetis an integer from0to10000;nextandlastlinks beyond it are left out.- CSV and XLSX exports use
maxExportLimit(default10000) instead. An export withoutlimitthat matches more rows is refused with400rather than truncated, and every export reports the match count inX-Total-Count. - CSV text cells starting with
=,+,-or@(also after spaces) or with a tab or line break get a leading apostrophe (OWASP CSV-injection guidance), so+48 600 000 000is exported as'+48 600 000 000. Numbers are unchanged.
CrudShellNestjsModule.forRoot({
// ...
restApi: true,
socket: false,
maxQueryLimit: 500,
maxExportLimit: 50000,
});The bounds cap what one request asks for, not what it costs: index the fields you filter and sort on, scope data per customer, and set database timeouts and rate limits in the application.
Upgrading
- A list request without
limitnow returns at mostmaxQueryLimitrows and a largerlimitis lowered to it: page through the list or raisemaxQueryLimit. - An
offsetabove10000is refused. - Raw operators and
/regex/values are refused; use~=for a "contains" match. - Field names containing
__proto__,prototypeorconstructorare refused. - A
CrudControllersubclass that callssuper(service)gets the default limits; pass the injectedSharedConfigas the second argument to use the configured ones.
Change subscriptions
A subscription is refused unless the socket handshake carries a valid JWT in auth.token
(io(url, { auth: { token } })), the message names one entity with { id }, and
SharedConfig.changePolicy returns exactly true. The policy receives { id, user, type }. It runs
once on subscribe without type, and again before every event, with a fresh token check, with the
change type (create, update or delete). A refusal on an event ends the subscription. Decide
from server-side ownership or tenant data, never from the id alone.
- Events carry no document fields. Clients receive
{ id, type }and refetch the entity through an authorized endpoint. - Deletes. Mongo emits a
deleteafter the document is gone, so a policy that loads the document would deny it and the client would never learn about the delete. Branch ontype === 'delete'and decide without loading the document. - Cost. The policy runs per event and per subscriber; keep it cheap or cache it.
- Token expiry. The token from the handshake is rechecked on every event. After it expires the
next event ends the subscription; the client must reconnect with a fresh
auth.token. - Core module.
CrudShellNestjsCoreModulealways registers the gateway, so every subscription is refused untilchangePolicyis set.
Migration
Subscriptions used to be open to anyone, accepted an empty filter and carried the full change,
including the inserted document or the update delta. After upgrading: configure changePolicy,
connect with the JWT in auth.token (and reconnect after it expires), always subscribe with an
id, and refetch the entity on each event instead of applying a payload.
