xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / tests / http_login.php (2d74e60f0b17f5b4e3b808c6c6285fc0dea6cc5b) (2,968B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/util.inc.php");
require_once("helpers.inc.php");
require_once("http.inc.php");

rg_log_set_file("http_login.log");

$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");

$_testns = 'http_login';
$rg_cache_enable = TRUE;

rg_test_create_user($db, $rg_ui);

rg_log("Test login operation");
// First we need to load the form so we can get the token
// We provide an old cookie to test if we generate a new pre-login one
$data = array();
$headers = array("Cookie: sid=d978671c2cd12fba05be218bb1653c1ce7bfb947");
$r = do_req($test_url . "/op/login", $data, $headers);
if ($r === FALSE) {
	rg_log("Cannot load login form.");
	exit(1);
}
$good_sid = $r['sid'];
$good_token = $r['tokens']['login'];
rg_log("good: sid=$good_sid token=$good_token");
if (strncmp($good_sid, "X", 1) != 0) {
	rg_log("Seems we did not get a pre-login session!");
	exit(1);
}


rg_log("Sending wrong token, because bad guy cannot control it");
// we need a valid token, so we do a new request
$data = array();
$headers = array();
$r = do_req($test_url . "/op/login", $data, $headers);
if ($r === FALSE) {
	rg_log("Cannot load login form.");
	exit(1);
}
//rg_log_ml("After loading attacker login form, r: " . print_r($r, TRUE));
$bad_sid = $r['sid'];
$bad_token = $r['tokens']['login'];
rg_log("bad: sid=$bad_sid token=$bad_token");

rg_log("Now we can attack, we use attacker token but user cookie...");
$data = array(
	"doit" => 1,
	"token" => $bad_token,
	"user" => $rg_ui['username'],
	"pass" => $rg_ui['pass'],
	"lock_ip" => 1
	);
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . "/op/login?t=bad_token_good_sid", $data, $headers);
if ($r === FALSE) {
	rg_log_ml(print_r($r, TRUE));
	rg_log_ml("Cannot do the request. Check above.");
	exit(1);
}
if (!strstr($r['body'], "invalid token")) {
	rg_log_ml(print_r($r, TRUE));
	rg_log("We sould get an invalid token error message. Check above!");
	exit(1);
}

rg_log("Do the real login post request");
$data = array(
	"doit" => 1,
	"token" => $good_token,
	"user" => $rg_ui['username'],
	"pass" => $rg_ui['pass'],
	"lock_ip" => 1
	);
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . "/op/login?t=good_sid_good_token", $data, $headers);
if ($r === FALSE) {
	rg_log_ml("Cannot login: " . print_r($r, TRUE));
	exit(1);
}
if (strstr($r['body'], "invalid user")) {
	rg_log_ml(print_r($r, TRUE));
	rg_log("Login invalid. Check above!");
	exit(1);
}

rg_log("Check what happens if we are logged in and want to login again");
$r = test_login($test_url, $rg_ui, $good_sid);
if ($r === FALSE) {
	rg_log_ml(print_r($r, TRUE));
	rg_log("Seems we cannot login again if we are already logged in!");
	exit(1);
}


rg_log("OK!");
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master