Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(core): reject vendor variant routes when the variant is not on the product (#1545)

Viktor Holik17774cfe8bee
diff --git a/integration-tests/http/product-edit/vendor/product-edit.spec.ts b/integration-tests/http/product-edit/vendor/product-edit.spec.ts
index 5c4834e67..5c45fd411 100644
--- a/integration-tests/http/product-edit/vendor/product-edit.spec.ts
+++ b/integration-tests/http/product-edit/vendor/product-edit.spec.ts
@@ -374,6 +374,49 @@ medusaIntegrationTestRunner({
return variant.id as string
}
+ it("rejects a variant that does not belong to the product in the path", async () => {
+ const otherProductId = await createVendorProduct(
+ "Other Product",
+ otherSellerHeaders,
+ )
+ await api.post(
+ `/vendor/products/${otherProductId}/variants`,
+ { title: "Other Variant" },
+ otherSellerHeaders,
+ )
+ const otherProduct = await api.get(
+ `/vendor/products/${otherProductId}`,
+ otherSellerHeaders,
+ )
+ const otherVariantId = (
+ otherProduct.data.product.variants as Array<any>
+ ).find((v) => v.title === "Other Variant").id as string
+
+ const productId = await createVendorProduct("Own Product")
+
+ const update = await api
+ .post(
+ `/vendor/products/${productId}/variants/${otherVariantId}`,
+ { title: "Hijacked", images: { remove: [] } },
+ sellerHeaders,
+ )
+ .catch((e) => e.response)
+ expect(update.status).toBe(404)
+
+ const remove = await api
+ .delete(
+ `/vendor/products/${productId}/variants/${otherVariantId}`,
+ sellerHeaders,
+ )
+ .catch((e) => e.response)
+ expect(remove.status).toBe(404)
+
+ const variantActions = (await listChanges(productId)).flatMap((c) =>
+ c.actions.filter((a) => a.action.startsWith("VARIANT_")),
+ )
+ expect(variantActions).toHaveLength(0)
+ })
+
it("stages a VARIANT_UPDATE carrying the variant_id and only the changed fields", async () => {
const productId = await createVendorProduct("Variant Update Product")
const variantId = await addVariant(productId, {
diff --git a/packages/core/src/api/vendor/products/middlewares.ts b/packages/core/src/api/vendor/products/middlewares.ts
index 8b90d56db..75f84b78f 100644
--- a/packages/core/src/api/vendor/products/middlewares.ts
+++ b/packages/core/src/api/vendor/products/middlewares.ts
@@ -33,7 +33,11 @@ import {
VendorUpdateProduct,
VendorUpdateProductVariant,
} from "./validators"
-import { promiseAll } from "@medusajs/framework/utils"
+import {
+ ContainerRegistrationKeys,
+ MedusaError,
+ promiseAll,
+} from "@medusajs/framework/utils"
const applySellerProductLinkFilter = async (
req: AuthenticatedMedusaRequest,
@@ -65,6 +69,31 @@ const applySellerProductLinkFilter = async (
return next()
}
+const ensureVariantBelongsToProduct = async (
+ req: AuthenticatedMedusaRequest,
+ _res: MedusaResponse,
+ next: MedusaNextFunction
+) => {
+ const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
+
+ const {
+ data: [variant],
+ } = await query.graph({
+ entity: "variant",
+ fields: ["id"],
+ filters: { id: req.params.variant_id, product_id: req.params.id },
+ })
+
+ if (!variant) {
+ throw new MedusaError(
+ MedusaError.Types.NOT_FOUND,
+ `Variant with id ${req.params.variant_id} was not found`
+ )
+ }
+
+ return next()
+}
+
export const vendorProductsMiddlewares: MiddlewareRoute[] = [
{
method: ["GET"],
@@ -213,6 +242,7 @@ export const vendorProductsMiddlewares: MiddlewareRoute[] = [
method: ["POST"],
matcher: "/vendor/products/:id/variants/:variant_id",
middlewares: [
+ ensureVariantBelongsToProduct,
validateAndTransformBody(VendorUpdateProductVariant),
validateAndTransformQuery(
VendorGetProductParams,
@@ -229,7 +259,7 @@ export const vendorProductsMiddlewares: MiddlewareRoute[] = [
{
method: ["DELETE"],
matcher: "/vendor/products/:id/variants/:variant_id",
- middlewares: [],
+ middlewares: [ensureVariantBelongsToProduct],
policies: [
{
resource: PolicyResource.product_variant,