First of all a big thanks for all the great work in this project, thank you very much! It is one of the best documented and supported Drupal modules I know.
But I have an issue, you can reproduce it like this:
1. Create a paid promotion campaign in Instagram or Twitter
2. Someone clicks in an app on his mobile phone on that link and the page opens in the in-app browser of Twitter or Instagram
3. The user does an order (Hooray!) with an anonymous checkout
4. The banking app of the users takes over the payment
5. The payment is successful! (Hooray!)
6. The banking app redirects the user to the payment provider In the regular browser
7. The payment provider redirects the user back to our checkout page /checkout/26896/payment/return?payment_id=9848
8. Because we are in another browser; Drupal does not recognize the user and gives a 403 (Bummer...)
Off course we can fix this with a control-hash in the return URL, but I was thinking maybe Drupal Commerce has already something available to solve this issue?
Issue fork commerce-3051241
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
sheijtel commentedComment #3
zaporylieSimilar issue (403 after returning from offsite payment gateway) was reported to me as a bug in the payment gateway module. After restudying the module's logic I haven't found the case when the payment would return 403.
Comment #4
twiik commentedCan confirm that this is easily reproducible in the payment gateway module @zaporylie mentions above (https://www.drupal.org/project/commerce_vipps).
1) Open website in an app with a built-in browser, I used Facebook.
2) Checkout with Vipps.
3) This opens the Vipps app prompting you to complete the payment.
4) Completing the payment opens the default browser rather than return you to the Facebook built-in browser.
5) Which results in 403 because you have no session in that browser.
This does not happen if you're logged in with the same user in both browsers though because then it seems to just make sure the uid is the same for the logged in user and the order being completed. I assume checkAccess() in CheckoutController.php is what's doing the access checking here.
This seems sort of major actually. It happens at least once a day for us. Apps with built-in browsers are very common and payment gateways providing their own apps for payment is starting to become common as well. I've seen other payment gateways with their own apps as well which I assume would lead to the same issues if used. Klarna Checkout seems to have their own app, which is a payment gateway we also use. We've had similar issues with Klarna Checkout which I assumed were due to other issues, but now it seems likely they could be caused by this issue.
Comment #5
twiik commentedChanging the category and priority as I feel this needs to be fixed in Commerce.
What do you mean by this? Isn't Commerce denying access to the checkout step before any access check you add would fire? Have you solved this in praxis? If so how?
Comment #6
twiik commentedQuick question: Does this access check need to be there?
And by that I mean the one verifying ownership of the order as that is the one which is causing us headaches in this situation.
I've debugged this a bit now and applied a quick fix for commerce_vipps which is the payment gateway we're using and experiencing this problem with. My quick-fix is just to override the CheckoutController::checkAccess method if we're on the payment return page and skip the order ownership check if we find ourselves in this situation, ie. anonymous user with no session and thus unable to verify ownership of the order. The way I see it the missing session information is only an order id used to verify that we have some form of ownership to the order passed in the url, but why do I need to prove I own the order in this step? I get that I shouldn't be able to go to someone else's order when it is still in the checkout process, but do we need it for the payment return step? Especially if it can cause us problems as is the case here?
I'm asking mainly to try and understand more about this problem, but also for anyone to tell me I'm an idiot for removing this check so that I can undo my quick-fix if that's the case. :p
Comment #7
fkatsukawa@TwiiK did you found the proper solution for this? I'm having the same scenario with commerce_datatrans module when I try to pay through paypal or twint.
The problem also occurs in the checkAccess of the CheckoutController.php of commerce module.
Comment #8
fkatsukawaComment #9
fkatsukawaComment #10
fkatsukawaI'm uploading a patch for this. Probably is not the better solution, but I already spent a lot of time on this and I only find this solution.
This is critical, because users don't are redirected to the complete order page after payment, and make the users to create another order because they think the previous order had a payment problem, but they don't have because the payment was done successfully.
If anyone find a better solution, please share with us.
Comment #11
eleo3113 commented@fkatsukawa we have the exact same problem. I tried contacting DataTrans and ...nothing. Since it has been 8 days...how has your patch been...did you experience further issues
Comment #12
ktch_my commentedHaving same issue and I was developing my own payment gateway module.
It seems like beside the access checking.
The user was logged out(or sessions doesn't match) when returned to /checkout/[order_id]/payment/return with 403.
Tried @fkatsukawa patch but was getting http500 error.
Comment #13
twiik commented@fkatsukawa No, I haven't done anything else regarding this. I also have the same custom access checking for the payment step as shown in your patch here.
Comment #14
RamonOliveira commentedI could replicate a similar issue (user logged out automatically) on Chromium 85.0.4183.102, but not on Firefox and older Chromium versions. It seems browser-related. Probably related to a new Chrome "feature": https://support.google.com/chrome/thread/62111771?hl=en, I've tried some solutions but none worked so far, does anyone have a solution for Drupal sites?
Comment #15
golubovicm commentedI also have problem with Datrans as @fkatsukawa mentioned and I implemented his patch from #10 but it still happens time to time. Any other solution?
Comment #16
fkatsukawaHello guys,
@ktch_my
@golubovicm
I checked that for some reason, every time, in return of the user to the complete order page, it came as an anonymous user. My patch is still checking if the user account has the "access checkout" permission. In my case, anonymous users have the permission to "access checkout", so, in my case, it works. Maybe you need to check if the anonymous user has the "access checkout" too on your websites, this is my guess.
It's very important to say that my website allows anonymous users to complete orders. So, in my case that permission is necessary.
If in your case you don't allow an anonymous user to complete order, maybe we need to create a new version of the patch without the "access checkout" permission check.
Comment #17
ktch_my commentedAs @RamonOliveira mentioned, it seems to be related to the samesite cookies policy.
For now, I managed to get my checkout page works by adding the line below to .htaccess.
Header always edit Set-Cookie (.*) "$1; SameSite=None; Secure"HOWEVER, please take note that the line above DIDN'T actually solve the issue.
I still get "AccessDenied" logged in watchdog. However, the checkout went through and redirect users to /checkout/id/complete without showing the 403 page.
Comment #18
fkatsukawaFirst, thanks @ktch_my for sharing the solution with .htaccess
But, I tried and in my case not worked. So, I updated the patch because the problem was affecting the complete page, not only the return of payment. So maybe this patch can solve the situations where the previous patch don't worked.
Comment #19
twiik commentedI think the only reason the above patched fails is due to coding standards and I'm not able to use the patch due to coding standards anyway due to us having automatic tests on pull requests so here is the same patch, just with the coding standards fixed.
I also added a comment about why we do this. Not sure if that's necessary or not.
We just launched a new site using a new offsite payment gateway and immediately ran into this issue again. The quick-fix I've used on previous sites was less elegant than this patch so I'm switching to this patch now.
This is still a major issue in my opinion.
Comment #20
mrupsidownHaving the same issue with Commerce Datatrans. None of the above patches did work for me. I also set cookies "samesite" to none, secure to true. I always hit a 403 Forbidden on the /checkout/order_number/payment/return path. I tried everything I could think of so far and was never able to complete an order due to this (these) issue(s).
Any idea will be more than welcome as this is simply preventing any sales on my side :/
PS. I don't currently understand all aspects of this issue tbh. I am using Nginx and was wondering if it is more "restrictive" than Apache and might be playing a role in the issue.
I have tried using
proxy_cookie_path / "/; SameSite=none; httponly; secure";in my nginx conf as well as settingin my php.ini together with the above patches but nothing helped.
PHP Version: 7.4.11
Web server: nginx/1.15.5
Drupal: 8.9.6
Commerce: 8.x-2.20
Commerce Datatrans: 8.x-1.0-beta1
Comment #21
mrupsidownStill banging my head on this
I confirm that the session cookie shows as HTTPOnly, Secure and SameSite=None (that's what is reported in Chrome dev tools).
I still hit the 403 error on payment return, with or without the above patches. The above is true too on the /payment/return path.
I really don't know how to work around this issue :(
Comment #22
agoradesign commentedSimiliar case: your payment gateway sends an IPN callback to inform the website about successful payment (usually arrives before user request). In that scenario, I'm seeing both "Warning: session_id(): Cannot change session id" (see #3135550: "Warning: session_id(): Cannot change session id" when cron runs to delete abandoned carts) warnings, as well as sometimes the user sees a 403 page. The problem here is that the the order gets placed by an anonymous request by a 3rd party, and the cart provider is reacting on order placement and tries to manipulate the cart session. On the hand, the cart ids won't be found on the IPN callback's non-existing session, on the other side I don't know if it might be a problem, that the user's session does not get updated then
Comment #23
khiminrm commentedHad similar issue with PayFlowLink gateway in commerce_paypal. Was able to fix on local Docker environment by using php 7.3 and ini_set('session.cookie_samesite', 'None'); in settings.php.
Comment #24
megadesk3000 commentedI came across this issue while having the same problem. Anonymous and logged in users get an 403 while getting redirected from the payment provider (datatrans) to drupal after a successful or canceled payment.
While not providing a patch i want to present how we solved that with a service decorator, that decorates the session_configuration service. Maybe that can help others.
In a custom module we create a new service decorator.
custom_module.services.yml
src/Session/CookieSameSiteSessionConfiguration.php
After that, if you log out and in and inspect the session cookie, you will see the cookie's SameSite option set to "None" and the Payment works as expected by redirection back to drupal without a 403.
Comment #25
cobenashI have a similar result on the offsite payment gateway.
When the people have been redirected to the payment provider and finish the payment process, the user will be redirected back and will get a 403 error.
I am sure the user is logged in and can't get the post data from the onReturn() though.
However, I used the #19 patch. It works for me.
Comment #32
didebruThanks all for working on this issue.
While yes the same site cookie is an issue and patch #19 would solve this issue but addresses another issue that basically all anon user can access the complete step pane if they know the order number (or guess it)
I tried another approach to check if the session is idle or not.
I set the idle time to 10 minutes that should be enough for most customers to proceed the checkout and return to the page from an external payment provider.
Hope this will help someone.
Comment #33
didebruComment #34
grahlMR breaks general cart handling.
Comment #35
didebruComment #36
twiik commentedI agree with the sentiment that my patch introduces a security issue in that you can access all checkout complete pages, but I don't see how your MR fixes the actual issue. Your MR and the proposed changes in #24 both rely on the session data being there, but the problem is that if you're sent from one browser to another as part of the payment process then you'll lose all session data.
The proper way to fix this imo is for the payment method to pass some hash or something to the payment gateway when you leave Drupal and then for the payment gateway to pass that back as a way to verify ownership of the order and to gain access to the payment and checkout steps.
Comment #37
arefen commentedAdd this line to settings.php for PHP < 7.3
And add this for PHP >= 7.3
Make sure SSL is active on your website
Comment #38
s.messaris commented#37 worked for me.
Comment #39
pstewart commentedComment #40
mmaranao commented#19 fixed the issue for me, but I needed the review step to added for the E-xact offsite payment gateway module or possible others, otherwise it will just end up in a loop.
Comment #42
renrhafI had this issue during the development of a custom Payment Gateway plugin.
In fact, on my local environment, the return/cancel URLs where generated using HTTP instead of HTTPS and causing the cookies not to be transfered correctly when going and coming back from an offsite payment page.
Changing my settings and adding my Traeffik/Docker installation as a trusted reverse proxy helped Drupal/Symfony to consider my request in HTTPS instead of HTTP and generating the correct return/cancel URLs. Problem was now gone.
If this information can help anyone facing that issue on their local environment...
Comment #43
nicolasgraphI faced to the same issue using the commerce_payzen module because of the return mode set to POST. It does work using GET.
Comment #44
janehollander commentedAdding
cookie_samesite: nonetoservices.ymlundersession.storage.options:worked for me.Comment #45
rszrama commentedI finally experienced this issue myself today thanks to Drupal 10 setting the
SameSitecookie attribute to Lax as of Drupal 10.1.0. Setting the value toNonein my site'sservices.ymldid the trick. I'm open to the idea that we should change our own access checker, but the issue is that it does leave the possibility open for an anonymous user to spoof a return for an authenticated order, where just adjusting the cookie attribute solves it more fundamentally.I just don't know what other implications changing this to None might have ... it was obviously set to Lax for the sake of hardening the Drupal application against cross-site request forgery attacks, but I can't imagine we'd document how to set it back to None if setting it back actually left a site insecure.
In the meantime, I'll figure out how to document this so folks can find it. I'll at least be including instructions in the CyberSource module, which is where I've been working.
Comment #46
agoradesign commentedThanks Ryan! please keep us updated
Comment #47
zaporylieSeems like we're discussing the same problem but with 2 different origins here:
- redirect from the offsite gateway via POST - this is where SameSite cookie will help
- redirect from offsite gateway to default browser with no user-session as the checkout was initiated in the non-default browser, most commonly - in-app browser (facebook, Instagram, etc) - in that case, SameSite cookie won't help.
Comment #48
agoradesign commentedExactly... I believe, finding a solution for the different browser scenario would automatically fix the first issue, regardless of SameSite cookie settings
Comment #49
zaporylieI am the category to Task - it is not a bug as this functionality is neither implemented nor supported. Still, it is not a support request either because sometimes this cannot be "fixed" using the SameSite cookie.
I also changed the version to 3.0.x which is where this would have to go.
Comment #50
mhawwari commentedFor our specific use case, the issue happens with the following steps:
1. On an android device, open a commerce site in a non-default browser.
2. Reach the payment step for an off-site payment gateway that redirects to a mobile app to complete the payment.
3. After completion the app redirects back to the site on the mobile's default browser.
4. 403 page opens because the session is not established on that browser.
My proposed solution is a new off-browser payment gateway with a new route that passes a token which is then verified onReturn.
I created a draft in a separate module here https://github.com/mhawwari/off_browser_payment_gateway and I can open a PR for it if the approach sounds appropriate.
Comment #51
danwonac commentedPatch #40 worked for me. The issue just started out of the blue on a live site.
Comment #52
dawitti commentedHaving the same issue, similar to @mhawwari's workaround I created a custom route with a hash with an overridden checkAccess method and using commerce's original controller method.
As there are multiple scenarios that are not edge cases in which checkout breaks with relevant consequences, and as multiple people already consider this a bug, this deserves either prominent documentation or a fix.
For me personally this goes into my drupal gotcha's handbook, as I'm sure I'll come across this again :)
Comment #53
stijndSame issue here. On my end this results in unprocessed payments. Applied patch in #40 to see what's the outcome.
Comment #54
anybodyWe're starting to see at a lot of Access denied (403) errors on checkout complete paths:
/checkout/12345/completein our logs recently.
Even more important we're seeing orders not being completed because there are even Access denied (403) errors on payment return URLs like:
checkout/12345/review/return?payment_intent=pi_1234566&payment_intent_client_secret=pi_fasdfasdfasdfasdfasdf&redirect_status=succeededDoes anyone know if this might be the same issue and why this might come up that much now?
We're using commerce_stripe for payments. As I think the latter needs to be investigated and resolved in the payment method module, I've opened a dedicated issue over there: #3593425: Access denied (403) and order not completed on POST: /checkout/X/review/return
Comment #59
grevil commentedI hid the branches, on which no further work is done on.
8.x-2.x is still still visible, as @zaporylie wanted to provide his own approach there.
Comment #60
grevil commentedI discussed this problem internally with @anybody, and we came to the conclusion, that additionally to the access checks in "CheckoutController::checkAccess()" (specifically how "$customer_check" is calculated), the payment gateway should provide its own alternative access check through a dedicated "checkAccess" method inside the payment provider.
A payment provider doesn't need to implement this method, we'll provide a base implementation in "PaymentGatewayBase" which simply returns NULL. When NULL is returned, the payment provider access check won't be taken into account.
Otherwise the method should return TRUE or FALSE. E.g. for the "StripePaymentElement", we could provide a "checkAccess" implementation, which compares the payment intent id with the id stored in the return url and allow access if they are identical.
Comment #62
grevil commentedComment #63
grevil commentedCreated a first draft how it could look like. Only problem would be, that when the access is granted on the `/review/return` step, the next step `/complete` won't have the cart session either and will most likely throw an error there then.
As a (stripe specific) workaround we could simply pass all query parameters to the next step as well.
Comment #64
grevil commentedHere is an example implementation of the method: #3593425: Access denied (403) and order not completed on POST: /checkout/X/review/return.
Comment #65
anybodyComment #66
grevil commentedAdjusted accordingly.
Comment #67
agoradesign commentedregarding the NULL check: allowing NULL as return value would make sense, if there's a possible use case, where the gateway should be able to overrule a positive check from CheckoutController with a FALSE -> so that a FALSE is always a veto, and a NULL would be a neutral position.
I'm not aware of a possible use case for this, however that would allow ultimative freedom. though I'm also only aware of these 403 problems, where we can now overrule the negative $customer_check value
Comment #68
grevil commentedAlright, this fixes the MAJOR Problem, that the order can't be placed because of the missing cart session. Unfortunately the "/completed" afterwards will still show the access denied error.
But this is definitly a step forward. For us the problem is most likely connected to Klarna (via commerce_stripe):
Once a user buys a product on our site with their phone and checkout through klarna, the klarna app opens up, on which the user is asked to finish their payment. When getting redirected to our site, he gets a 403, because the cart session expired.
The current MR together with the MR in #3593425: Access denied (403) and order not completed on POST: /checkout/X/review/return fixes the issue and provides a template for other offsite payment gateways.
Setting this to MAJOR, as we have several customers each week trying and failing to buy our customers products.
Comment #69
grevil commented@agoradesign yes, that would make sense, but I am also unaware off a case, where this would matter.
Comment #70
grevil commentedStatic patch for the time being.
Comment #71
anybodyI added some more details in #3593425: Access denied (403) and order not completed on POST: /checkout/X/review/return (#12). TL;DR: This is happening frequently on iOS devices. So I agree this is definitely still a major issue.
Comment #72
yang_yi_cn commented#70 doesn't work for me, because my payment gateway doesn't have a field in the order
I have multiple payment gateway instances and I use custom resolver logic to decide which payment gateway I use. So this field check always fails.
I'm making a patch using #40 and reroll against current 3.x instead.
Comment #73
grevil commented@yang_yi_cn that sounds like quite the workaround on your site. The payment_gateway field on the order is where commerce core stores the selected gateway. For example, the PaymentProcess pane reads the gateway from it. A setup where the gateway never ends up on the order would also have trouble with the standard offsite return flow, not only with this patch.
I'll put this back to needs review as I don't think we should support this specific use case. @yang_yi_cn could your gateway resolver not simply set the gateway on the order once it is resolved instead?