effective-input.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Build the complete input that may execute after a permission decision.
  3. *
  4. * A checker may return only the fields it rewrites, while approval can keep
  5. * the decision pending long enough for either the original args or the
  6. * rewrite object to be mutated by another owner. Merge and deep-freeze a
  7. * detached JSON-like snapshot immediately after the decision so the eventual
  8. * executor input cannot drift while approval is pending.
  9. */
  10. export function snapshotPermissionEffectiveInput(
  11. originalInput: Readonly<Record<string, unknown>>,
  12. rewrittenInput: Readonly<Record<string, unknown>> | undefined,
  13. ): Readonly<Record<string, unknown>> | undefined {
  14. if (!rewrittenInput) return undefined;
  15. const merged = createPlainRecord();
  16. copyEnumerableDataProperties(originalInput, merged);
  17. copyEnumerableDataProperties(rewrittenInput, merged);
  18. return cloneAndFreeze(merged, new WeakSet()) as Readonly<Record<string, unknown>>;
  19. }
  20. /** Detach and deep-freeze a complete permission input without applying a rewrite. */
  21. export function snapshotPermissionInput(
  22. input: Readonly<Record<string, unknown>>,
  23. ): Readonly<Record<string, unknown>> {
  24. const copied = createPlainRecord();
  25. copyEnumerableDataProperties(input, copied);
  26. return cloneAndFreeze(copied, new WeakSet()) as Readonly<Record<string, unknown>>;
  27. }
  28. function cloneAndFreeze(value: unknown, ancestors: WeakSet<object>): unknown {
  29. if (value === null || typeof value !== 'object') return value;
  30. if (ancestors.has(value)) {
  31. throw new TypeError('Permission effective input must not contain cyclic values.');
  32. }
  33. ancestors.add(value);
  34. let cloned: unknown;
  35. if (Array.isArray(value)) {
  36. cloned = value.map((item) => cloneAndFreeze(item, ancestors));
  37. } else {
  38. const prototype = Object.getPrototypeOf(value);
  39. if (prototype !== Object.prototype && prototype !== null) {
  40. throw new TypeError('Permission effective input must contain only JSON-like values.');
  41. }
  42. const record = createPlainRecord();
  43. for (const key of Reflect.ownKeys(value)) {
  44. const descriptor = Object.getOwnPropertyDescriptor(value, key);
  45. if (!descriptor?.enumerable) continue;
  46. if (!('value' in descriptor)) {
  47. throw new TypeError('Permission effective input must not contain accessors.');
  48. }
  49. defineEnumerableValue(record, key, cloneAndFreeze(descriptor.value, ancestors));
  50. }
  51. cloned = record;
  52. }
  53. ancestors.delete(value);
  54. return Object.freeze(cloned);
  55. }
  56. function copyEnumerableDataProperties(
  57. source: Readonly<Record<string, unknown>>,
  58. target: Record<PropertyKey, unknown>,
  59. ): void {
  60. for (const key of Reflect.ownKeys(source)) {
  61. const descriptor = Object.getOwnPropertyDescriptor(source, key);
  62. if (!descriptor?.enumerable) continue;
  63. if (!('value' in descriptor)) {
  64. throw new TypeError('Permission effective input must not contain accessors.');
  65. }
  66. defineEnumerableValue(target, key, descriptor.value);
  67. }
  68. }
  69. function createPlainRecord(): Record<PropertyKey, unknown> {
  70. return {} as Record<PropertyKey, unknown>;
  71. }
  72. function defineEnumerableValue(
  73. target: Record<PropertyKey, unknown>,
  74. key: PropertyKey,
  75. value: unknown,
  76. ): void {
  77. Object.defineProperty(target, key, {
  78. value,
  79. enumerable: true,
  80. configurable: true,
  81. writable: true,
  82. });
  83. }