2015-07-20 13:59:04 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Phil Davis <phil.davis@inf.org>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-07-20 13:59:04 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-07-20 13:59:04 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Session;
|
|
|
|
|
|
|
|
use OCP\IConfig;
|
2015-08-21 19:27:52 +03:00
|
|
|
use OCP\IRequest;
|
2015-07-20 13:59:04 +03:00
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\Security\ICrypto;
|
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
|
2015-08-21 19:27:52 +03:00
|
|
|
/**
|
|
|
|
* Class CryptoWrapper provides some rough basic level of additional security by
|
|
|
|
* storing the session data in an encrypted form.
|
|
|
|
*
|
|
|
|
* The content of the session is encrypted using another cookie sent by the browser.
|
|
|
|
* One should note that an adversary with access to the source code or the system
|
|
|
|
* memory is still able to read the original session ID from the users' request.
|
|
|
|
* This thus can not be considered a strong security measure one should consider
|
|
|
|
* it as an additional small security obfuscation layer to comply with compliance
|
|
|
|
* guidelines.
|
|
|
|
*
|
2015-09-29 09:32:47 +03:00
|
|
|
* TODO: Remove this in a future release with an approach such as
|
2015-08-21 19:27:52 +03:00
|
|
|
* https://github.com/owncloud/core/pull/17866
|
|
|
|
*
|
|
|
|
* @package OC\Session
|
|
|
|
*/
|
2015-07-20 13:59:04 +03:00
|
|
|
class CryptoWrapper {
|
|
|
|
const COOKIE_NAME = 'oc_sessionPassphrase';
|
|
|
|
|
2017-07-19 19:46:01 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
2015-07-20 13:59:04 +03:00
|
|
|
/** @var ISession */
|
|
|
|
protected $session;
|
2017-07-19 19:46:01 +03:00
|
|
|
/** @var ICrypto */
|
2015-07-20 13:59:04 +03:00
|
|
|
protected $crypto;
|
|
|
|
/** @var ISecureRandom */
|
|
|
|
protected $random;
|
2017-07-19 19:46:01 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $passphrase;
|
2015-07-20 13:59:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param ICrypto $crypto
|
|
|
|
* @param ISecureRandom $random
|
2015-08-21 19:27:52 +03:00
|
|
|
* @param IRequest $request
|
2015-07-20 13:59:04 +03:00
|
|
|
*/
|
2015-08-21 19:27:52 +03:00
|
|
|
public function __construct(IConfig $config,
|
|
|
|
ICrypto $crypto,
|
|
|
|
ISecureRandom $random,
|
|
|
|
IRequest $request) {
|
2015-07-20 13:59:04 +03:00
|
|
|
$this->crypto = $crypto;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->random = $random;
|
|
|
|
|
2015-08-21 19:27:52 +03:00
|
|
|
if (!is_null($request->getCookie(self::COOKIE_NAME))) {
|
|
|
|
$this->passphrase = $request->getCookie(self::COOKIE_NAME);
|
2015-07-20 13:59:04 +03:00
|
|
|
} else {
|
2016-01-11 22:05:30 +03:00
|
|
|
$this->passphrase = $this->random->generate(128);
|
2015-08-21 19:27:52 +03:00
|
|
|
$secureCookie = $request->getServerProtocol() === 'https';
|
|
|
|
// FIXME: Required for CI
|
2015-07-20 13:59:04 +03:00
|
|
|
if (!defined('PHPUNIT_RUN')) {
|
2015-09-14 12:22:34 +03:00
|
|
|
$webRoot = \OC::$WEBROOT;
|
|
|
|
if($webRoot === '') {
|
|
|
|
$webRoot = '/';
|
|
|
|
}
|
2019-09-09 22:29:58 +03:00
|
|
|
|
|
|
|
if (PHP_VERSION_ID < 70300) {
|
|
|
|
setcookie(self::COOKIE_NAME, $this->passphrase, 0, $webRoot, '', $secureCookie, true);
|
|
|
|
} else {
|
|
|
|
setcookie(
|
|
|
|
self::COOKIE_NAME,
|
|
|
|
$this->passphrase,
|
|
|
|
[
|
|
|
|
'expires' => 0,
|
|
|
|
'path' => $webRoot,
|
|
|
|
'domain' => '',
|
|
|
|
'secure' => $secureCookie,
|
|
|
|
'httponly' => true,
|
|
|
|
'samesite' => 'Lax',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2015-07-20 13:59:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ISession $session
|
|
|
|
* @return ISession
|
|
|
|
*/
|
|
|
|
public function wrapSession(ISession $session) {
|
2015-08-21 19:27:52 +03:00
|
|
|
if (!($session instanceof CryptoSessionData)) {
|
|
|
|
return new CryptoSessionData($session, $this->crypto, $this->passphrase);
|
2015-07-20 13:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $session;
|
|
|
|
}
|
|
|
|
}
|