fg-dynamic-component 2.0.0 → 3.0.0

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/README.md CHANGED
@@ -7,6 +7,7 @@ Create an Angular standalone signals based dynamic components tree through a JSO
7
7
 
8
8
  | Angular | fg-dynamic-component | NPM package |
9
9
  | ------- | -------------------- | ----------------------------- |
10
+ | 22.2.3 | 3.0.0 | `fg-dynamic-component@^3.0.0` |
10
11
  | 22.1.6 | 2.0.0 | `fg-dynamic-component@^2.0.0` |
11
12
  | 17.2.4 | 1.1.1 | `fg-dynamic-component@^1.1.1` |
12
13
  | 17.2.4 | 1.1.0 | `fg-dynamic-component@^1.1.0` |
@@ -121,6 +122,34 @@ class MyComponent {
121
122
 
122
123
  _NOTE:_ You can declare components to dynamically load in static and/or dynamic way using or not the FGDynamicService.
123
124
 
125
+ #### Scoped registry
126
+
127
+ `FGDynamicService` uses a global registry. When configurations are not fully trusted
128
+ (for example multi-tenant or plugin scenarios), create a scoped registry and pass it
129
+ to the component. Scoped registries are propagated to nested items as well:
130
+
131
+ ```ts
132
+ import { FGDynamicComponent, FGDynamicRegistry, FGDynamicService } from "fg-dynamic-component";
133
+
134
+ const registry: FGDynamicRegistry = FGDynamicService.createRegistry();
135
+ registry.register("MyDynamicComponent1", MyDynamicComponent1);
136
+
137
+ @Component({
138
+ selector: "my-component",
139
+ template: `<fg-dynamic [configuration]="configuration()" [registry]="registry" />`,
140
+ imports: [FGDynamicComponent],
141
+ standalone: true
142
+ })
143
+ class MyComponent {
144
+ registry = registry;
145
+ configuration = signal({ type: "MyDynamicComponent1" });
146
+ }
147
+ ```
148
+
149
+ A configuration can only resolve component names that were explicitly registered in
150
+ the registry it receives. Names such as `constructor`, `__proto__` or `toString` are
151
+ never resolved from the registry prototype chain.
152
+
124
153
  ### Inputs and Outputs
125
154
 
126
155
  You can pass `inputs` and `outputs` to your dynamic components:
@@ -176,10 +205,14 @@ class MyComponent {
176
205
  hello: '$vm().count % 2 === 0 ? $vm().message : \'Hello World!\''
177
206
  },
178
207
  outputs: {
179
- onSomething: '$vm.update(viewModel => { ...viewModel, message: $event, count: viewModel.count + 1 })'
208
+ onSomething: '$vm().handleSomething($event)'
180
209
  }
181
210
  } as IFGDynamicItem);
182
- viewModel = model({ message: 'Hello World!', count: 0 });
211
+ viewModel = model({
212
+ message: 'Hello World!',
213
+ count: 0,
214
+ handleSomething: (value: string) => { /* ... */ }
215
+ });
183
216
  }
184
217
 
185
218
  @Component({
@@ -209,7 +242,7 @@ class MyComponent {
209
242
  configuration = signal({
210
243
  type: MyDynamicComponent1,
211
244
  attributes: {
212
- "my-attribute": "attribute-value",
245
+ "data-my-attribute": "attribute-value",
213
246
  class: "some classes",
214
247
  disabled: "$vm().isDisabled"
215
248
  }
@@ -218,6 +251,18 @@ class MyComponent {
218
251
  }
219
252
  ```
220
253
 
254
+ > **Allowed attributes**
255
+ >
256
+ > Only a safe allowlist of attributes is applied: `id`, `class`, `name`, `role`,
257
+ > `title`, `tabindex`, `hidden`, `dir`, `lang`, `for`, `href`, `src`, the common
258
+ > form attributes (`disabled`, `readonly`, `required`, `placeholder`, …) and any
259
+ > `data-*` or `aria-*` attribute.
260
+ >
261
+ > Event handlers (`on*`), `style`, `srcdoc`, `formaction`, namespaced attributes
262
+ > and non-primitive values are ignored. `href` and `src` only accept `http`,
263
+ > `https`, `mailto`, `tel` and relative URLs, so `javascript:` URLs are blocked
264
+ > even when they come from an expression.
265
+
221
266
  ### Reactive Forms
222
267
 
223
268
  You can declaratively set attributes to activate model binding, form control status (enabled/disabled) and validations with your ControlValueAccessor components.
@@ -254,6 +299,56 @@ class MyComponent {
254
299
  }
255
300
  ```
256
301
 
302
+ > **Security note**
303
+ >
304
+ > `disabled` and the form validators are configured by the same configuration that
305
+ > renders the form. They are **UX-only**: a client can always bypass them. Never use
306
+ > them for authorization or as the only validation layer. Enforce permissions,
307
+ > required fields, ranges and formats again on the server.
308
+ >
309
+ > Control names are interpreted as dot/bracket paths into the view model. Paths that
310
+ > contain `__proto__`, `prototype` or `constructor` are rejected, so a configuration
311
+ > cannot pollute object prototypes or register such names in a `FormGroup`.
312
+
313
+ ## Security
314
+
315
+ ### Expressions are interpreted, never evaluated
316
+
317
+ `inputs`, `outputs` and `attributes` values that contain `$vm` or `$event` are
318
+ evaluated by a small, self-contained interpreter. No `eval`, `new Function` or
319
+ other string-to-code primitive is used. The supported subset is:
320
+
321
+ - literals (`'text'`, `42`, `true`, `false`, `null`, `undefined`), arrays and objects;
322
+ - member access `$vm().name`, `$vm()['name']` and optional chaining `$vm().a?.b`;
323
+ - calls such as `$vm().save($event)` and expression-bodied arrow functions
324
+ (for example `$vm().items.map(item => item.id)`);
325
+ - unary, arithmetic, comparison, logical, nullish and ternary operators;
326
+ - array/object spread and arrow functions whose body returns an expression or
327
+ contains a `return` statement.
328
+
329
+ Anything else is ignored at runtime (a warning is printed only in development mode).
330
+ In particular globals (`window`, `fetch`, …), assignments, `new`, `this`, template
331
+ literals, regular-expression literals and access to `__proto__`, `prototype`,
332
+ `constructor`, `caller`, `callee` or `arguments` are rejected. Expressions are also
333
+ limited in size, token count, nesting depth and evaluation steps.
334
+
335
+ A value that contains neither `$vm` nor `$event` is used as a plain literal.
336
+
337
+ > Expressions are configuration data, not sandboxed user code: a trusted
338
+ > configuration can still call methods that exist on the view model. Only load
339
+ > configurations from sources you trust.
340
+
341
+ ### Other hardening
342
+
343
+ - Attribute names/values are validated (see [Attributes](#attributes)) and URL
344
+ schemes are checked.
345
+ - The component registry uses a `Map` and never resolves inherited properties
346
+ (`constructor`, `__proto__`, …). Prefer a scoped registry for untrusted
347
+ configurations.
348
+ - Angular component classes are never invoked as loader functions.
349
+ - Dev-mode diagnostics never include expression sources, configuration values or
350
+ raw error objects.
351
+
257
352
  ## Contributing
258
353
 
259
354
  You are welcome to contribute to this project.