From e1767abfda77e939ccfde2aa5ff355c24bcde519 Mon Sep 17 00:00:00 2001 From: Maxime Besson Date: Thu, 26 Mar 2020 20:19:38 +0100 Subject: [PATCH] CORS: special handling for AJAX SSL (#2110) --- .../lib/Lemonldap/NG/Portal/Main/Run.pm | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm index 7384193d7..52216c5b7 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm @@ -1100,7 +1100,19 @@ sub corsPreflight { sub sendJSONresponse { my ( $self, $req, $j, %args ) = @_; my $res = Lemonldap::NG::Common::PSGI::sendJSONresponse(@_); - if ( $self->conf->{corsEnabled} ) { + + # If this is a cross-domain request from the portal itself + # (Ajax SSL to a different VHost) + # we allow CORS + if ( $req->origin and index( $self->conf->{portal}, $req->origin ) == 0 ) { + $self->logger->debug('AJAX request from portal, allowing CORS'); + push @{ $res->[1] }, + "Access-Control-Allow-Origin" => $req->origin, + "Access-Control-Allow-Methods" => "*", + "Access-Control-Allow-Credentials" => "true"; + + } + elsif ( $self->conf->{corsEnabled} ) { my @cors = split /;/, $self->cors; push @{ $res->[1] }, @cors; $self->logger->debug('Apply following CORS policy :');