* [ADD] Application URL for handling requests through reverse proxy. Thanks to @rob42 for the feedback. Closes #1218

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2019-02-24 20:48:34 +01:00
parent 5c9c9147bf
commit 1ec8d26e3a
11 changed files with 105 additions and 27 deletions

View File

@@ -241,7 +241,9 @@ final class AccountController extends ControllerBase implements CrudControllerIn
$clientAddress = $this->configData->isDemoEnabled() ? '***' : $this->request->getClientAddress(true);
$deepLink = new Uri(Bootstrap::$WEBURI . Bootstrap::$SUBURI);
$baseUrl = ($this->configData->getApplicationUrl() ?: Bootstrap::$WEBURI) . Bootstrap::$SUBURI;
$deepLink = new Uri($baseUrl);
$deepLink->addParam('r', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW) . '/' . $accountData->getId());
$this->eventDispatcher->notifyEvent('show.account.link',
@@ -1055,7 +1057,9 @@ final class AccountController extends ControllerBase implements CrudControllerIn
$accountDetails = $this->accountService->getById($id)->getAccountVData();
$deepLink = new Uri(Bootstrap::$WEBURI . Bootstrap::$SUBURI);
$baseUrl = ($this->configData->getApplicationUrl() ?: Bootstrap::$WEBURI) . Bootstrap::$SUBURI;
$deepLink = new Uri($baseUrl);
$deepLink->addParam('r', Acl::getActionRoute(ActionsInterface::ACCOUNT_VIEW) . '/' . $id);
$usersId = [$accountDetails->userId, $accountDetails->userEditId];

View File

@@ -61,6 +61,7 @@ final class ConfigGeneralController extends SimpleControllerBase
$siteLang = $this->request->analyzeString('sitelang');
$siteTheme = $this->request->analyzeString('sitetheme', 'material-blue');
$sessionTimeout = $this->request->analyzeInt('session_timeout', 300);
$applicationUrl = $this->request->analyzeString('app_url');
$httpsEnabled = $this->request->analyzeBool('https_enabled', false);
$debugEnabled = $this->request->analyzeBool('debug_enabled', false);
$maintenanceEnabled = $this->request->analyzeBool('maintenance_enabled', false);
@@ -71,6 +72,7 @@ final class ConfigGeneralController extends SimpleControllerBase
$configData->setSiteLang($siteLang);
$configData->setSiteTheme($siteTheme);
$configData->setSessionTimeout($sessionTimeout);
$configData->setApplicationUrl($applicationUrl);
$configData->setHttpsEnabled($httpsEnabled);
$configData->setDebug($debugEnabled);
$configData->setMaintenance($maintenanceEnabled);

View File

@@ -174,7 +174,9 @@ final class AccountHelper extends HelperBase
$accountActionsDto->setPublicLinkId($publicLinkData->getId());
$accountActionsDto->setPublicLinkCreatorId($publicLinkData->getUserId());
$this->view->assign('publicLinkUrl', PublicLinkService::getLinkForHash($publicLinkData->getHash()));
$baseUrl = ($this->configData->getApplicationUrl() ?: Bootstrap::$WEBURI) . Bootstrap::$SUBURI;
$this->view->assign('publicLinkUrl', PublicLinkService::getLinkForHash($baseUrl, $publicLinkData->getHash()));
$this->view->assign('publicLinkId', $publicLinkData->getId());
} catch (NoSuchItemException $e) {
$this->view->assign('publicLinkId', 0);
@@ -323,7 +325,9 @@ final class AccountHelper extends HelperBase
{
$route = Acl::getActionRoute($this->actionId) . ($this->accountId ? '/' . $this->accountId : '');
$uri = new Uri(Bootstrap::$WEBROOT . Bootstrap::$SUBURI);
$baseUrl = ($this->configData->getApplicationUrl() ?: Bootstrap::$WEBURI) . Bootstrap::$SUBURI;
$uri = new Uri($baseUrl);
$uri->addParam('r', $route);
return $uri->getUriSigned($this->configData->getPasswordSalt());

View File

@@ -133,9 +133,9 @@ final class LayoutHelper extends HelperBase
protected function getResourcesLinks()
{
$version = VersionUtil::getVersionStringNormalized();
$uri = Bootstrap::$WEBROOT . Bootstrap::$SUBURI;
$baseUrl = ($this->configData->getApplicationUrl() ?: Bootstrap::$WEBURI) . Bootstrap::$SUBURI;
$jsUri = new Uri($uri);
$jsUri = new Uri($baseUrl);
$jsUri->addParam('_r', 'resource/js');
$jsUri->addParam('_v', md5($version));
@@ -164,7 +164,7 @@ final class LayoutHelper extends HelperBase
$resultsAsCards = $this->configData->isResultsAsCards();
}
$cssUri = new Uri($uri);
$cssUri = new Uri($baseUrl);
$cssUri->addParam('_r', 'resource/css');
$cssUri->addParam('_v', md5($version . $resultsAsCards));

View File

@@ -24,6 +24,7 @@
namespace SP\Modules\Web\Controllers;
use SP\Bootstrap;
use SP\Core\Acl\Acl;
use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
@@ -150,7 +151,9 @@ final class PublicLinkController extends ControllerBase implements CrudControlle
$this->view->assign('nextAction', Acl::getActionRoute(Acl::ACCESS_MANAGE));
if ($this->view->isView === true) {
$this->view->assign('publicLinkURL', PublicLinkService::getLinkForHash($publicLink->getHash()));
$baseUrl = ($this->configData->getApplicationUrl() ?: Bootstrap::$WEBURI) . Bootstrap::$SUBURI;
$this->view->assign('publicLinkURL', PublicLinkService::getLinkForHash($baseUrl, $publicLink->getHash()));
$this->view->assign('disabled', 'disabled');
$this->view->assign('readonly', 'readonly');
} else {

View File

@@ -10,7 +10,8 @@
<?php if (!$_getvar('curlIsAvailable')): ?>
<div class="msg-option-unvailable">
<?php printf(__('The \'%s\' extension is unavailable'), 'curl'); ?>
<i id="help-curl" class="material-icons"><?php echo $icons->getIconHelp()->getIcon(); ?></i>
<i id="help-curl"
class="material-icons"><?php echo $icons->getIconHelp()->getIcon(); ?></i>
<div class="mdl-tooltip mdl-tooltip--large" for="help-curl">
<p>
<?php echo __('This extension is needed to check for sysPass updates and notices'); ?>
@@ -43,7 +44,8 @@
<td class="valField">
<div class="lowres-title"><?php echo __('Language'); ?></div>
<select name="sitelang" id="sel-sitelang" size="1" class="select-box sel-chosen-ns">
<select name="sitelang" id="sel-sitelang" size="1"
class="select-box sel-chosen-ns">
<?php /** @var \SP\Mvc\View\Components\SelectItem $lang */
foreach ($_getvar('langs') as $lang): ?>
<option
@@ -82,21 +84,48 @@
</td>
<td class="valField">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input id="session_timeout" name="session_timeout" type="number" step="300"
pattern="[0-9]{2,4}" class="mdl-textfield__input mdl-color-text--indigo-400" maxlength="5"
value="<?php echo $configData->getSessionTimeout(); ?>" required/>
<input id="session_timeout" name="session_timeout" type="number"
min="0" step="300"
pattern="[0-9]{2,4}"
class="mdl-textfield__input mdl-color-text--indigo-400"
maxlength="5"
value="<?php echo $configData->getSessionTimeout(); ?>"
required/>
<label class="mdl-textfield__label"
for="session_timeout"><?php echo __('Session timeout (s)'); ?></label>
</div>
</td>
</tr>
<tr>
<td class="descField">
<?php echo __('Application URL'); ?>
<div id="help-app_url"
class="icon material-icons <?php echo $icons->getIconHelp()->getClass(); ?>"><?php echo $icons->getIconHelp()->getIcon(); ?></div>
<div class="mdl-tooltip mdl-tooltip--large" for="help-app_url">
<p>
<?php echo __('Sets the application URL when accessing through a reverse proxy or load balancer.'); ?>
</p>
</div>
</td>
<td class="valField">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input id="app_url" name="app_url" type="url"
pattern="^https?://.*"
class="mdl-textfield__input mdl-color-text--indigo-400"
value="<?php echo $configData->getApplicationUrl(); ?>"/>
<label class="mdl-textfield__label"
for="app_url"><?php echo __('Application URL'); ?></label>
</div>
</td>
</tr>
<tr>
<td class="descField"></td>
<td class="valField">
<ul class="config-site-list-action mdl-list">
<li class="mdl-list__item mdl-list__item--two-line">
<div class="mdl-switch__box">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="https_enabled">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
for="https_enabled">
<input type="checkbox" id="https_enabled"
class="mdl-switch__input"
name="https_enabled" <?php echo $configData->isHttpsEnabled() ? 'checked' : ''; ?>/>
@@ -111,7 +140,8 @@
<li class="mdl-list__item mdl-list__item--two-line">
<div class="mdl-switch__box">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="debug_enabled">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
for="debug_enabled">
<input type="checkbox" id="debug_enabled"
class="mdl-switch__input"
name="debug_enabled" <?php echo $configData->isDebug() ? 'checked' : ''; ?>/>
@@ -126,7 +156,8 @@
<li class="mdl-list__item mdl-list__item--two-line">
<div class="mdl-switch__box">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="maintenance_enabled">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
for="maintenance_enabled">
<input type="checkbox" id="maintenance_enabled"
class="mdl-switch__input"
name="maintenance_enabled" <?php echo $configData->isMaintenance() ? 'checked' : ''; ?>/>
@@ -141,7 +172,8 @@
<li class="mdl-list__item mdl-list__item--two-line">
<div class="mdl-switch__box">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="check_updates_enabled">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
for="check_updates_enabled">
<input type="checkbox" id="check_updates_enabled"
class="mdl-switch__input"
name="check_updates_enabled" <?php echo $configData->isCheckUpdates() ? 'checked' : ''; ?>/>
@@ -156,7 +188,8 @@
<li class="mdl-list__item mdl-list__item--two-line">
<div class="mdl-switch__box">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="check_notices_enabled">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
for="check_notices_enabled">
<input type="checkbox" id="check_notices_enabled"
class="mdl-switch__input"
name="check_notices_enabled" <?php echo $configData->isChecknotices() ? 'checked' : ''; ?>/>
@@ -171,7 +204,8 @@
<li class="mdl-list__item mdl-list__item--two-line">
<div class="mdl-switch__box">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="encrypt_session_enabled">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
for="encrypt_session_enabled">
<input type="checkbox" id="encrypt_session_enabled"
class="mdl-switch__input"
name="encrypt_session_enabled" <?php echo $configData->isEncryptSession() ? 'checked' : ''; ?>/>

View File

@@ -401,6 +401,10 @@ final class ConfigData implements JsonSerializable
* @var bool
*/
private $ldapTlsEnabled = false;
/**
* @var string
*/
private $applicationUrl;
/**
* @return array
@@ -2189,4 +2193,20 @@ final class ConfigData implements JsonSerializable
{
$this->appVersion = $appVersion;
}
/**
* @return string
*/
public function getApplicationUrl()
{
return $this->applicationUrl;
}
/**
* @param string $applicationUrl
*/
public function setApplicationUrl(string $applicationUrl = null)
{
$this->applicationUrl = $applicationUrl ? rtrim($applicationUrl, '/') : null;
}
}

View File

@@ -46,7 +46,7 @@ final class Request
/**
* @var \Klein\DataCollection\HeaderDataCollection
*/
protected $headers;
private $headers;
/**
* @var \Klein\Request
*/

View File

@@ -372,8 +372,10 @@ final class Template
return $this->vars->get($key, $default);
};
$_getRoute = function ($path) use ($sk) {
$uri = new Uri(Bootstrap::$WEBROOT . Bootstrap::$SUBURI);
$_getRoute = function ($path) use ($sk, $configData) {
$baseUrl = ($configData->getApplicationUrl() ?: Bootstrap::$WEBURI) . Bootstrap::$SUBURI;
$uri = new Uri($baseUrl);
$uri->addParam('r', $path);
$uri->addParam('sk', $sk);

View File

@@ -26,6 +26,7 @@ namespace SP\Services\Account;
defined('APP_ROOT') || die();
use SP\Bootstrap;
use SP\Config\ConfigData;
use SP\DataModel\AccountSearchVData;
use SP\DataModel\ItemData;
@@ -241,8 +242,15 @@ final class AccountSearchItem
*/
public function getPublicLink()
{
return self::$publicLinkEnabled
&& $this->accountSearchVData->getPublicLinkHash() !== null ? PublicLinkService::getLinkForHash($this->accountSearchVData->getPublicLinkHash()) : null;
if (self::$publicLinkEnabled
&& $this->accountSearchVData->getPublicLinkHash() !== null
) {
$baseUrl = ($this->configData->getApplicationUrl() ?: Bootstrap::$WEBURI) . Bootstrap::$SUBURI;
return PublicLinkService::getLinkForHash($baseUrl, $this->accountSearchVData->getPublicLinkHash());
}
return null;
}
/**

View File

@@ -24,7 +24,6 @@
namespace SP\Services\PublicLink;
use SP\Bootstrap;
use SP\Config\Config;
use SP\Core\Crypt\Crypt;
use SP\Core\Crypt\Vault;
@@ -33,6 +32,7 @@ use SP\DataModel\ItemSearchData;
use SP\DataModel\PublicLinkData;
use SP\DataModel\PublicLinkListData;
use SP\Http\Request;
use SP\Http\Uri;
use SP\Repositories\NoSuchItemException;
use SP\Repositories\PublicLink\PublicLinkRepository;
use SP\Services\Account\AccountService;
@@ -66,13 +66,14 @@ final class PublicLinkService extends Service
/**
* Returns an HTTP URL for given hash
*
* @param $baseUrl
* @param $hash
*
* @return string
*/
public static function getLinkForHash($hash)
public static function getLinkForHash($baseUrl, $hash)
{
return Bootstrap::$WEBURI . '/index.php?r=account/viewLink/' . $hash;
return (new Uri($baseUrl))->addParam('r', 'account/viewLink/' . $hash)->getUri();
}
/**