Community Hub membersJoin more than 6500+ Members
All security advisories
ModerateCVSS 5.3– Published

Reading media entities by aggregating fields individually bypasses MediaVisibilityRestrictionSubscriber

GHSA-m895-2hj3-8cg9

Affected versions

PackageAffected versionsFixed in
shopware/platform>=6.7.0.0 <6.7.3.16.7.3.1
shopware/platform<6.6.10.76.6.10.7
shopware/core>=6.7.0.0 <6.7.3.16.7.3.1
shopware/core<6.6.10.76.6.10.7

Description

Summary

Extra filters for preventing access to private media via API can be bypassed by using aggregations. And now we all think:

<img width="488" height="367" alt="Dinkelberg-meme with AGGREGATIONS as caption" src="https://github.com/user-attachments/assets/7226059a-ef26-47ed-986a-94d9af815390" />

because we regularly have issues with aggregations

Details

Aggregations can have filters but no one is adding these filters to the aggregations. There is a subscriber MediaVisibilityRestrictionSubscriber but it can only work on entity reads. So every subscriber on that can be bypassed by using aggregations. It is annoying to use aggregations but works. This allows to query private media like invoices but you cannot read them as this is not possible via API. This is not that bad as there should be nothing, that exposes any media to be downloaded if you haven't already have access to orders or similar private media anyway ( SPOILER the other vuln is about being able to expose any media ).

The script was tested against a dockware/play:6.4.20 and dockware/play:6.7.1.0 so likely it will work for anything in between.

Impact

I only evaluated against the media visibility subscriber. If you have other scenarios you are free to raise the bar.

The attack scenario is to have a low-permission product editor in the administration, that can link other medias to products and also be able to buy them with a new customer with a payment method that gets marked as paid quickly. This can be any default set up shop with a payment like credit card, paypal, ... so the paid state change happens without manual review (although you won't see it in the administration what you are about to approve). If you have admin accounts, that can access everything anyway you are not prone to it because then your users can already download the files anyway. If you do not store invoices in the shop but serve them from mails from the ERP then you have nothing to lose in default scenarios. Other plugins could also make use of private medias ( like customization tools supporting customer uploads ) these could be affected as well . So any shop owner who distrusts certain persons to access orders and has likely a high throughput shop with fast registration and payment process is affected by this chain.

As soon as this happens you can expose about 10k documents per order and with that leak addresses, payment methods, order behaviour and maybe payment details depending on the content of the invoices / user uploads.

Solution

I suggest to have new event to intervene with aggregations. This way anyone can start to check things like this. This can also be served as a decorator for EntityAggregator so easily addable to the SwagPlatformSecurity.

This is a possible solution (tested locally in 6.6):

class BeforeEntityAggregateEvent extends Event implements ShopwareEvent
{
    public function __construct(
        private readonly Criteria $criteria,
        private Aggregation $aggregation,
        private readonly string $entityName,
        private readonly Context $context
    ) {
    }

    public function getAggregation(): Aggregation
    {
        return $this->aggregation;
    }

    public function setAggregation(Aggregation $aggregation): void
    {
        $this->aggregation = $aggregation;
    }

    public function getCriteria(): Criteria
    {
        return $this->criteria;
    }

    public function getEntityName(): string
    {
        return $this->entityName;
    }

    public function getContext(): Context
    {
        return $this->context;
    }
}

Integration in EntityRepository:

    private function _aggregate(Criteria $criteria, Context $context): AggregationResultCollection
    {
        $criteria = clone $criteria;
        $eventCriteria = clone $criteria;

        $aggregations = $criteria->getAggregations();
        $criteria->resetAggregations();
        $newAggregations = [];

        foreach ($aggregations as $aggregation) {
            $newAggregations[] = $this->_replaceAggregations($eventCriteria, $aggregation, $context);
        }

        $criteria->addAggregation(...$newAggregations);

        $result = $this->aggregator->aggregate($this->definition, $criteria, $context);

        $event = new EntityAggregationResultLoadedEvent($this->definition, $result, $context);
        $this->eventDispatcher->dispatch($event, $event->getName());

        return $result;
    }

    private function _replaceAggregations(Criteria $criteria, Aggregation $aggregation, Context $context): Aggregation
    {
        if ($aggregation instanceof BucketAggregation || $aggregation instanceof DateHistogramAggregation || $aggregation instanceof FilterAggregation || $aggregation instanceof TermsAggregation) {
            $innerAggregation = $aggregation->getAggregation();

            if ($innerAggregation !== null) {
                $innerAggregation = $this->_replaceAggregations($criteria, $innerAggregation, $context);
                $aggregation->assign([
                    'aggregation' => $innerAggregation,
                ]);
            }
        }
        $event = new BeforeEntityAggregateEvent($criteria, $aggregation, $this->definition->getEntityName(), $context);
        $this->eventDispatcher->dispatch($event);

        return $event->getAggregation();
    }

New event subscriber for the media visibility:

    public function securePrivateFolderAggregation(BeforeEntityAggregateEvent $event): void
    {
        if ($event->getContext()->getScope() === Context::SYSTEM_SCOPE) {
            return;
        }

        $aggregation = $event->getAggregation();

        if ($event->getEntityName() === MediaFolderDefinition::ENTITY_NAME) {
            $aggregation = new FilterAggregation($aggregation->getName() . '_filter', $aggregation, [
                new MultiFilter('OR', [
                    new EqualsFilter('media_folder.configuration.private', false),
                    new EqualsFilter('media_folder.configuration.private', null),
                ])
            ]);
        }

        if ($event->getEntityName() === MediaDefinition::ENTITY_NAME) {
            $aggregation = new FilterAggregation($aggregation->getName() . '_filter', $aggregation, [
                new MultiFilter('OR', [
                    new EqualsFilter('private', false),
                    new MultiFilter('AND', [
                        new EqualsFilter('private', true),
                        new EqualsFilter('mediaFolder.defaultFolder.entity', 'product_download'),
                    ]),
                ]),
            ]);
        }

        $event->setAggregation($aggregation);
    }

This approach is tested against this exact scenario but the entity name check of the event might not be sufficient for a general purpose solution. It likely can be bypassed by aggregations on these entities by using association paths. But the path + entity definition traversal is likely something for those ( YOU ) who already wrote ApiAware and permission checks on these before!