magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.51k stars 9.31k forks source link

Despite "Allow Guest Checkout" set to "No" it's possible to place a guest order with the guest-carts REST API #36691

Closed C4rter closed 1 year ago

C4rter commented 1 year ago

Preconditions and environment

We just had a hacker that tried to use the vulnerability "APSB22-12" to hack our store.

Thankfully we had the patches applied on 2.4.4 (and now upgraded to 2.4.5-p1) and he was not successful. But he was able to try out the vulnerability by placing a guest order via the Rest API despite the guest-checkout being disabled in the backend.

Why is this still possible via API? It's a big risk because it allows unwanted guest orders being placed and in my case letting a hacker try out vulnerabilities.

How do I know that he did it via API? There is no way to place a guest order via the frontend and you can see it in the access log how he did it:

"POST /checkout/cart/add/uenc/aHR0cHM6Ly93d3cucGxhdGV3cml0ZS5kZS9jdHAtZHJ1Y2twbGF0dGVuYmVsaWNodHVuZy9kcnVja3BsYXR0ZS0yNTB4MzcwLmh0bWw%2C/product/500/ HTTP/1.1" 200 1025 "-" "GET /checkout HTTP/1.1" 302 1388 "-" "GET /checkout/cart/ HTTP/1.1" 200 34064 "-" "POST /rest/default/V1/customers/isEmailAvailable HTTP/1.1" 400 354 "-" "POST /rest/italian/V1/customers/isEmailAvailable HTTP/1.1" 400 4619 "-" "POST /rest/de/V1/customers/isEmailAvailable HTTP/1.1" 200 4840 "-" "POST /rest/de/V1/guest-carts/Qgfa1jFEf8m430Vlo0xnFLz2uIyqAUPI/estimate-shipping-methods HTTP/1.1" 200 1021 "-" "POST /rest/de/V1/guest-carts/Qgfa1jFEf8m430Vlo0xnFLz2uIyqAUPI/shipping-information HTTP/1.1" 200 3374 "-" "POST /rest/de/V1/guest-carts/Qgfa1jFEf8m430Vlo0xnFLz2uIyqAUPI/payment-information HTTP/1.1" 200 4843 "-" "POST /b2b.php HTTP/1.1" 302 5380 "-"

At the end he even tries to call the b2b.php file that he tried to create with his injection.

Steps to reproduce

  1. Make sure "Allow Guest Checkout" is set to "No" in Backend Config (Stores->Configuration->Sales->Checkout->Allow Guest Checkout)
  2. Check if guest checkout is possible when going through the frontend (should not and is not possible)
  3. Do a guest-checkout with the use of the Rest API.

Get a new Cart: POST https://[YOUR-SHOP-URL]/rest/[YOUR-STORE-VIEW-CODE]/V1/guest-carts You receive a basket ID here that you need in every step.

Add a product to the Cart: POST https://[YOUR-SHOP-URL]/rest/[YOUR-STORE-VIEW-CODE]/V1/guest-carts/[YOUR-BASKET-ID]/items

Body:

{
  "cartItem": {
    "sku": "[SOME-EXISTING-SKU]",
    "qty": 1
  }
}

Set shipping and billing address: POST https://[YOUR-SHOP-URL]/rest/[YOUR-STORE-VIEW-CODE]/V1/guest-carts/[YOUR-BASKET-ID]/shipping-information

Body: (Make sure you use a shipping method that exists. Maybe adjust the country to one that's allowed in your setup.)

{
  "addressInformation": {
    "shipping_address": {
      "region": "New York",
      "region_id": 0,
      "region_code": "",
      "country_id": "DE",
      "street": [
        "123 Oak Ave"
      ],
      "postcode": "10577",
      "city": "Purchase",
      "firstname": "Jane",
      "lastname": "Doe",
      "email": "jdoe@example.com",
      "telephone": "512-555-1111"
    },
    "billing_address": {
      "region": "New York",
      "region_id": 0,
      "region_code": "",
      "country_id": "DE",
      "street": [
        "123 Oak Ave"
      ],
      "postcode": "10577",
      "city": "Purchase",
      "firstname": "Jane",
      "lastname": "Doe",
      "email": "jdoe@example.com",
      "telephone": "512-555-1111"
    },
    "shipping_carrier_code": "freeshipping",
    "shipping_method_code": "freeshipping"
  }
}

Place Order: POST https://[YOUR-SHOP-URL]/rest/[YOUR-STORE-VIEW-CODE]/V1/guest-carts/[YOUR-BASKET-ID]/payment-information

Body: (You will have to adjust the agreement IDs or leave them out entirely and make sure you use a payment method that exists)

{
  "email": "jdoe@example.com",
  "paymentMethod": {
    "method": "banktransfer",
    "extension_attributes": {"agreement_ids": ["1","2","5"]}
  },
  "billing_address": {
    "email": "jdoe@example.com",
    "region": "New York",
    "region_id": 0,
    "region_code": "",
    "country_id": "DE",
    "street": [
      "123 Oak Ave"
    ],
    "postcode": "10577",
    "city": "Purchase",
    "telephone": "512-555-1111",
    "firstname": "Jane",
    "lastname": "Doe",
    "same_as_billing": 0
  }
}
  1. Guest Order will be placed.

Expected result

Guest checkout should not be possible via the API when the guest checkout is disabled in the configuration.

Actual result

Guest checkout can be used and an order can be placed as a guest.

Additional information

Maybe somewhere in vendor/magento/module-checkout/Model/GuestPaymentInformationManagement.php in the savePaymentInformationAndPlaceOrder function there can be a condition, that checks if guest checkout is even allowed.

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @C4rter. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] commented 1 year ago

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Bravo commented 1 year ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 1 year ago

Hi @engcom-Bravo. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 1 year ago

Hi @engcom-Bravo, here is your Magento Instance: https://bb644a2717558fc68d3647adf2faf0d4.instances.magento-community.engineering Admin access: https://bb644a2717558fc68d3647adf2faf0d4.instances.magento-community.engineering/admin_8dd6 Login: 50a1d619 Password: 6cba8bad502b

engcom-Bravo commented 1 year ago

Hi @C4rter,

Thank you for reporting and collaboration. Verified the issue on Magento 2.4-develop instance and the issue is not reproducible.Kindly refer the screenshots.

Using frontend not able to place the order as a Guest.

Screenshot 2023-01-03 at 2 50 43 PM

Using API not able to place the order as a Guest.

Screenshot 2023-01-03 at 2 52 55 PM

Kindly let us know if we miss anything.

Thanks.

viktorlantos commented 1 year ago

The same thing happened to us a few days ago on 2.4.5-p1. Guest checkout was set as NO. Pretty worrying.

C4rter commented 1 year ago

Hi @engcom-Bravo

thank you for testing. As I see from your screenshots you used the GraphQL endpoint. I mentioned that the hacker used the REST API endpoint and my "Steps to reproduce" also show the Rest API.

It might be that it's not possible via the GraphQL endpoint but I'm sure it's possible via the Rest API.

The error that's visible in your screenshot is a GQL specific error, thrown in a file only used when using the GQL API: vendor/magento/module-quote-graph-ql/Model/Cart/CheckCartCheckoutAllowance.php

    public function execute(Quote $quote): void
    {
        if (!$quote->getCustomerIsGuest()) {
            return;
        }

        $isAllowedGuestCheckout = (bool)$this->checkoutHelper->isAllowedGuestCheckout($quote);
        if (false === $isAllowedGuestCheckout) {
            throw new GraphQlAuthorizationException(
                __(
                    'Guest checkout is not allowed. ' .
                    'Register a customer account or login with existing one.'
                )
            );
        }
    }

So it makes sense, that it's not possible when using GQL Api. But it's unfortunately possible when using REST API.

Can you please verify it via the REST API endpoint? Thanks

viktorlantos commented 1 year ago

To confirm, we had Rest API guest order as well. Just finished the Sansec scan, and with M2.4.5-p1 only the order was placed. No other harm was detected. Sansec Scrutinize mode found it in the db:quote 10% MALWARE m2_template_attack_payload_cc6e3

engcom-Bravo commented 1 year ago

Hi @C4rter,

Thanks for your update.

@viktorlantos Thanks for your contribution & collaboration over here.

Verified the issue on Magento 2.4-develop instance and the issue is reproducible.Kindly refer the screenshots.

Steps to reproduce 1.Make sure "Allow Guest Checkout" is set to "No" in Backend Config (Stores->Configuration->Sales->Checkout->Allow Guest Checkout) 2.Check if guest checkout is possible when going through the frontend (should not and is not possible) 3.Do a guest-checkout with the use of the Rest API.

Using frontend we are not able to place the order as a guest.

Screenshot 2023-01-04 at 12 55 44 PM

Using Rest Api we are able to place the order as a guest.

Screenshot 2023-01-04 at 12 57 07 PM Screenshot 2023-01-04 at 12 55 53 PM

Hence confirming this issue.

Thanks.

github-jira-sync-bot commented 1 year ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-7577 is successfully created for this GitHub issue.

m2-assistant[bot] commented 1 year ago

:white_check_mark: Confirmed by @engcom-Bravo. Thank you for verifying the issue.
Issue Available: @engcom-Bravo, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

anthonyvdg-se commented 1 year ago

Hi, we have seen this (at least partially automated, few pages were visited but there were 6 checkout attempts in 2 minutes) attack happening on an older installation of Magento as well, we were wondering if this was fixed in a newer version, but apparently it is not (yet). The attack was used in combination with a code injection attempt.

The access logs look very similar as in the original report.

CptCharlesG commented 1 year ago

Same here on 2.4.5-1 Guest Checkout disabled

BUT Also: WEB API - Allow Anonymus Guest Access: No

I do not know what the later means, but it looks like both checks failed!

Complete access.log that might helps to understand a bit more (going trough all the pages makes it not anonymous guest anymore?):

"GET / HTTP/1.1" 200 29438 "-" 
"GET / HTTP/1.1" 200 29438 "-" 
"POST /pr-cookie-consent/consent_guest/update/ HTTP/1.1" 302 31 "-" 
"GET / HTTP/1.1" 200 29438 "-" 
"GET /someproducturl.html HTTP/1.1" 200 20423 "-" 
"POST /checkout/cart/add/uenc/aHR0cHM6Ly93d3cudHJhZGVzZXJ2aWNlLmh1L3R6NGstbXQ4L2ZwbTgwMy5odG1s/product/9108/ HTTP/1.1" 200 89 "-" 
"GET /checkout HTTP/1.1" 302 31 "-" 
"GET /checkout/cart/ HTTP/1.1" 200 64515 "-" 
"POST /rest/default/V1/customers/isEmailAvailable HTTP/1.1" 200 35 "-" 
"POST /rest/default/V1/guest-carts/SEJoFR1LPXT1VpcBXqP8q1suSllJMvoG/estimate-shipping-methods HTTP/1.1" 200 219 "-" 
"POST /rest/default/V1/guest-carts/SEJoFR1LPXT1VpcBXqP8q1suSllJMvoG/shipping-information HTTP/1.1" 200 547 "-" 
"POST /b2b.php HTTP/1.1" 404 368 "-"
C4rter commented 1 year ago

BUT Also: WEB API - Allow Anonymus Guest Access: No I do not know what the later means, but it looks like both checks failed!

As it says in the backend right under the configuration, this has nothing to do with the checkout API, it only applies to CMS, Catalog and Store API: "This feature applies only to CMS, Catalog and Store APIs. Please consult your developers for details on potential security risks."

(going trough all the pages makes it not anonymous guest anymore?)

As far as I know the check if guest checkout is allowed is just missing entirely on the guest-checkout rest api.

github-jira-sync-bot commented 1 year ago

:x: Cannot export the issue. This GitHub issue is already linked to Jira issue(s): https://jira.corp.adobe.com/browse/AC-7577

mabt commented 1 year ago

Same here on 2.4.5-1 Guest Checkout disabled

BUT Also: WEB API - Allow Anonymus Guest Access: No

I do not know what the later means, but it looks like both checks failed!

Complete access.log that might helps to understand a bit more (going trough all the pages makes it not anonymous guest anymore?):

"GET / HTTP/1.1" 200 29438 "-" 
"GET / HTTP/1.1" 200 29438 "-" 
"POST /pr-cookie-consent/consent_guest/update/ HTTP/1.1" 302 31 "-" 
"GET / HTTP/1.1" 200 29438 "-" 
"GET /someproducturl.html HTTP/1.1" 200 20423 "-" 
"POST /checkout/cart/add/uenc/aHR0cHM6Ly93d3cudHJhZGVzZXJ2aWNlLmh1L3R6NGstbXQ4L2ZwbTgwMy5odG1s/product/9108/ HTTP/1.1" 200 89 "-" 
"GET /checkout HTTP/1.1" 302 31 "-" 
"GET /checkout/cart/ HTTP/1.1" 200 64515 "-" 
"POST /rest/default/V1/customers/isEmailAvailable HTTP/1.1" 200 35 "-" 
"POST /rest/default/V1/guest-carts/SEJoFR1LPXT1VpcBXqP8q1suSllJMvoG/estimate-shipping-methods HTTP/1.1" 200 219 "-" 
"POST /rest/default/V1/guest-carts/SEJoFR1LPXT1VpcBXqP8q1suSllJMvoG/shipping-information HTTP/1.1" 200 547 "-" 
"POST /b2b.php HTTP/1.1" 404 368 "-"

We are facing everyday the same hack (same requests) on multiple magento2, does someone have a fix?

anthonyvdg-se commented 1 year ago

@mabt If you don't have guest orders, you can block requests to the guest endpoints. See https://developer.adobe.com/commerce/webapi/rest/use-rest/anonymous-api-security/

(Search on /V1/guest-carts)

C4rter commented 1 year ago

@mabt If you don't have guest orders, you can block requests to the guest endpoints. See https://developer.adobe.com/commerce/webapi/rest/use-rest/anonymous-api-security/

(Search on /V1/guest-carts)

If you mean the "Allow Anonymous Guest Access" config mentioned there, that does not block access to the guest-carts API.

It says so on the page: "The following APIs remain accessible to anonymous users" and then lists all guest-cart endpoints.

Only way would be to block them manually via a custom plugin.

anthonyvdg-se commented 1 year ago

@mabt If you don't have guest orders, you can block requests to the guest endpoints. See https://developer.adobe.com/commerce/webapi/rest/use-rest/anonymous-api-security/ (Search on /V1/guest-carts)

If you mean the "Allow Anonymous Guest Access" config mentioned there, that does not block access to the guest-carts API.

It says so on the page: "The following APIs remain accessible to anonymous users" and then lists all guest-cart endpoints.

Only way would be to block them manually via a custom plugin.

The mostly easiest solution is to block on server level (nginx, apache, etc)

yogesh-valiya commented 1 year ago

Not sure if I'm missing anything but not able to reproduce it image

Steps I followed is like below,

  1. POST http://magento-github.local/rest/V1/guest-carts => GOT cart ID h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01
  2. GET http://magento-github.local/rest/V1/guest-carts/h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01 => Got quote ID
  3. POST http://magento-github.local/rest/default/V1/guest-carts/h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01/items
  4. POST http://magento-github.local/rest/default/V1/guest-carts/h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01/estimate-shipping-methods
  5. POST http://magento-github.local/rest/default/V1/guest-carts/h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01/shipping-information
  6. POST http://magento-github.local/rest/default/V1/guest-carts/2FHsrl9MF58IKc8AIxk70Psvl4EYyoLW/payment-information

It's stopping me at step 5 with proper message

glo05363 commented 1 year ago

@C4rter thank you for addressing the issue: we have reproduced it with following steps:

We have disabled guest-cart-checkout in admin which disables storefront access.

image

API:

  1. POST [domain]/rest/default/V1/guest-carts : response: cartid-> 8Sa8iH70go696ng0TqWZVyyNSWTSnzMc
  2. POST [domain]/rest/default/V1/guest-carts/{cartid}/items with payload { "cartItem": { "sku": "simple", "qty": 1 } }
  3. POST [domain]/rest/default/V1/guest-carts/{cartid}/shipping-information with appropriate payload { "addressInformation": { "shipping_address": { "region": "New York", "region_id": 0, "region_code": "", "country_id": "DE", "street": [ "123 Oak Ave" ], "postcode": "10577", "city": "Purchase", "firstname": "Jane", "lastname": "Doe", "email": "jdoe@example.com", "telephone": "512-555-1111" }, "billing_address": { "region": "New York", "region_id": 0, "region_code": "", "country_id": "DE", "street": [ "123 Oak Ave" ], "postcode": "10577", "city": "Purchase", "firstname": "Jane", "lastname": "Doe", "email": "jdoe@example.com", "telephone": "512-555-1111" }, "shipping_carrier_code": "flatrate", "shipping_method_code": "flatrate" } }
  4. POST [domain]/rest/default/V1/guest-carts/{cartid}/payment-information with payload { "email": "jdoe@example.com", "paymentMethod": { "method": "checkmo" }, "billing_address": { "email": "jdoe@example.com", "region": "New York", "region_id": 0, "region_code": "", "country_id": "DE", "street": [ "123 Oak Ave" ], "postcode": "10577", "city": "Purchase", "telephone": "512-555-1111", "firstname": "Jane", "lastname": "Doe", "same_as_billing": 0 } } Response: image image

And we see that every guest-cart API is functioning .

We have also disabled WEB API Anonymous guest access though it works only for CMS, Catalog and store APIs.

image

To add to this please check https://developer.adobe.com/commerce/webapi/rest/use-rest/anonymous-api-security/ Here it is mentioned that:

image

we conclude it to be working as it is suppose to.

anthonyvdg-se commented 1 year ago

I thought the payment-information endpoint created the order, did you check that despite the shipping endpoint failing? (maybe there are some extra checks preventing the shipping flow from continuing, every store can be configured differently)

Not sure if I'm missing anything but not able to reproduce it image

Steps I followed is like below,

  1. POST http://magento-github.local/rest/V1/guest-carts => GOT cart ID h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01
  2. GET http://magento-github.local/rest/V1/guest-carts/h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01 => Got quote ID
  3. POST http://magento-github.local/rest/default/V1/guest-carts/h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01/items
  4. POST http://magento-github.local/rest/default/V1/guest-carts/h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01/estimate-shipping-methods
  5. POST http://magento-github.local/rest/default/V1/guest-carts/h8YLEHwbzZ9ewbXjhr29B6iBO2ksNs01/shipping-information
  6. POST http://magento-github.local/rest/default/V1/guest-carts/2FHsrl9MF58IKc8AIxk70Psvl4EYyoLW/payment-information

It's stopping me at step 5 with proper message

yogesh-valiya commented 1 year ago

I'm not able to reproduce it on 2.4-develop branch as it seems to be get fixed by this commit - https://github.com/magento/magento2/commit/47c388a093fd5621b19eb2089877db80254a937a

glo05363 commented 1 year ago

@C4rter @yogesh-valiya Yes The issue is no longer there on 2.4-develop as the commit #47c388a

engcom-Hotel commented 1 year ago

Hello,

As I can see this issue got fixed in the scope of the internal Jira ticket ACP2E-1339 by the internal team Related commits: https://github.com/magento/magento2/search?q=ACP2E-1339&type=commits

Thanks

LucScu commented 5 months ago

@yogesh-valiya @glo05363 @engcom-Bravo It isn't merged with 2.4.6-p3 right? What is the best way to merge this commit into own environment?

s-renz commented 2 months ago

@yogesh-valiya @glo05363 @engcom-Bravo It isn't merged with 2.4.6-p3 right? What is the best way to merge this commit into own environment?

There's aparently quality patch with the ID ACSD-47920 that you can manually apply (see KB article linked below). However, it's not marked as compatible with 2.4.6-p*, so not sure if that's actually working.

But actually I think the better question would actually be, why is a security fix not merged in 2.4.6 including p6 even though it should be?

In my opinion this isn't just a "quality" patch, this is a potential security issue, as any attacker can still send orders without authentication. Which might include payloads (like stated in the original post regarding APSB22-12). So next time a similar exploit is found it can be executed even though guest checkout is disabled already.

The KB article even states that this should be fixed in 2.4.6: https://experienceleague.adobe.com/en/docs/commerce-knowledge-base/kb/support-tools/patches/v1-1-24/acsd-47920-guest-order-allow-guest-checkout-off

Please note that the issue is scheduled to be fixed in Adobe Commerce 2.4.6

rintoug commented 2 months ago

There are some of the endpoints still used by magento even if you are only using the web UI. For example, rest/storecode/V1/guest-carts/quote-id/estimate-shipping-methods which used in the /checkout/cartpage to retrieve the shipping charges. In case you completely blocked the guest APIs, this feature will not work.

frostitution commented 2 months ago

This is still an issue, I received a "guest" order just fine despite having guest checkout disabled. 2.4.6-p5

76ludwig commented 2 months ago

I received a hacker's guest order via API in my Magento shop yesterday. The shop was updated to 2.4.6-p6 two days ago...

rintoug commented 2 months ago

I received a hacker's guest order via API in my Magento shop yesterday. The shop was updated to 2.4.6-p6 two days ago...

We are running the same version and received a couple of orders last week. However, we are not using the APIs at the moment, so went ahead in Cloudflare and blocked the guest checkout APIs.

You can try executing the guest apis yourself in Postman or any other clients, and see it yourself(Quick endpoint ref to make quick guest order).

dphilipps commented 2 months ago

We've blocked access to guest cart APIs as we don't use them by adding the following rules to .htaccess after RewriteEngine on. This results in a 404 (before Magento's htaccess converts 403s to 404s)

RewriteCond %{REQUEST_METHOD} POST RewriteRule ^rest/V1/guest-carts - [F,NC,L] RewriteRule ^rest/default/V1/guest-carts - [F,NC,L]

bafmaamy commented 2 months ago

I managed to deal with this, creating a custom module and block order execution if firstname or lastname contain characters like "}","{", <.. etc. and log the attempt too.

Added also validation of the shpping address, billing address, post code, city, order comments and disallow more than 30 characters in firstname and lastname.

https://github.com/magento/magento2/issues/39002#issuecomment-2291143582