@zenstackhq/plugin-policy 3.8.3 → 3.9.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/dist/index.mjs CHANGED
@@ -771,6 +771,7 @@ var PolicyHandler = class extends OperationNodeTransformer {
771
771
  this.tryRejectNonexistentModel(mutationModel);
772
772
  if (InsertQueryNode.is(node)) await this.preCreateCheck(mutationModel, node, proceed);
773
773
  if (UpdateQueryNode.is(node)) await this.preUpdateCheck(mutationModel, node, proceed);
774
+ if (DeleteQueryNode.is(node)) await this.preDeleteCheck(mutationModel, node, proceed);
774
775
  const needsPostUpdateCheck = UpdateQueryNode.is(node) && this.hasPostUpdatePolicies(mutationModel);
775
776
  let beforeUpdateInfo;
776
777
  if (needsPostUpdateCheck) beforeUpdateInfo = await this.loadBeforeUpdateEntities(mutationModel, node.where, proceed, !this.dialect.supportsReturning);
@@ -805,6 +806,52 @@ var PolicyHandler = class extends OperationNodeTransformer {
805
806
  selections: [SelectionNode.create(AliasNode.create(this.eb.exists(violatingRowsQuery).toOperationNode(), IdentifierNode.create("$condition")))]
806
807
  })).rows[0].$condition) throw createRejectedByPolicyError(mutationModel, RejectedByPolicyReason.NO_ACCESS, "some rows cannot be updated due to field policies");
807
808
  }
809
+ /**
810
+ * Deleting rows from an implicit many-to-many join table requires both participant sides to be
811
+ * updatable. Unlike inserts (which are explicitly rejected by {@link enforcePreCreatePolicyForManyToManyJoinTable}),
812
+ * a delete would otherwise just be silently filtered down to zero rows and report success. See issue #2752.
813
+ */
814
+ async preDeleteCheck(mutationModel, node, proceed) {
815
+ const m2m = this.resolveManyToManyJoinTable(mutationModel);
816
+ if (!m2m) return;
817
+ const sides = [{
818
+ column: "A",
819
+ model: m2m.firstModel,
820
+ idField: m2m.firstIdField
821
+ }, {
822
+ column: "B",
823
+ model: m2m.secondModel,
824
+ idField: m2m.secondIdField
825
+ }].map((side) => ({
826
+ ...side,
827
+ value: this.extractEqualityValue(node.where?.where, side.column)
828
+ })).filter((side) => side.value !== void 0);
829
+ if (sides.length === 0) return;
830
+ const result = await proceed({
831
+ kind: "SelectQueryNode",
832
+ selections: sides.map((side, index) => SelectionNode.create(AliasNode.create(this.eb.selectFrom(side.model).where(this.eb(this.eb.ref(`${side.model}.${side.idField}`), "=", side.value)).select(() => new ExpressionWrapper(this.buildPolicyFilter(side.model, void 0, "update")).as("_")).toOperationNode(), IdentifierNode.create(`$condition${index}`))))
833
+ });
834
+ for (const [index, side] of sides.entries()) if (!result.rows[0]?.[`$condition${index}`]) throw createRejectedByPolicyError(side.model, RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${side.model}" not updatable`);
835
+ }
836
+ /**
837
+ * Finds the literal value that `column` is constrained to by a top-level conjunction of the given
838
+ * where clause, or `undefined` if there's no such constraint.
839
+ *
840
+ * E.g., given the where clause `("A" = 1 AND "B" IN (2, 3))`, extracting column "A" returns `1`,
841
+ * while extracting column "B" returns `undefined` since it's not an equality constraint.
842
+ */
843
+ extractEqualityValue(node, column) {
844
+ if (!node) return;
845
+ if (ParensNode.is(node)) return this.extractEqualityValue(node.node, column);
846
+ if (AndNode.is(node)) return this.extractEqualityValue(node.left, column) ?? this.extractEqualityValue(node.right, column);
847
+ if (!BinaryOperationNode.is(node)) return;
848
+ if (!OperatorNode.is(node.operator) || node.operator.operator !== "=") return;
849
+ const { leftOperand, rightOperand } = node;
850
+ if (!ReferenceNode.is(leftOperand) || !ColumnNode.is(leftOperand.column)) return;
851
+ if (leftOperand.column.column.name !== column) return;
852
+ if (!ValueNode.is(rightOperand) || rightOperand.value === null || rightOperand.value === void 0) return;
853
+ return rightOperand.value;
854
+ }
808
855
  async postUpdateCheck(model, beforeUpdateInfo, updateResult, proceed) {
809
856
  let postUpdateRows;
810
857
  if (this.dialect.supportsReturning) postUpdateRows = updateResult.rows;
@@ -1109,7 +1156,7 @@ var PolicyHandler = class extends OperationNodeTransformer {
1109
1156
  kind: "SelectQueryNode",
1110
1157
  selections: [SelectionNode.create(AliasNode.create(queryA.toOperationNode(), IdentifierNode.create("$conditionA"))), SelectionNode.create(AliasNode.create(queryB.toOperationNode(), IdentifierNode.create("$conditionB")))]
1111
1158
  });
1112
- if (!result.rows[0]?.$conditionA) throw createRejectedByPolicyError(m2m.firstModel, RejectedByPolicyReason.CANNOT_READ_BACK, `many-to-many relation participant model "${m2m.firstModel}" not updatable`);
1159
+ if (!result.rows[0]?.$conditionA) throw createRejectedByPolicyError(m2m.firstModel, RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.firstModel}" not updatable`);
1113
1160
  if (!result.rows[0]?.$conditionB) throw createRejectedByPolicyError(m2m.secondModel, RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.secondModel}" not updatable`);
1114
1161
  }
1115
1162
  async enforcePreCreatePolicyForOne(model, fields, values, proceed) {