Skip to content

Commit

Permalink
Merge pull request #1226 from ladybirdweb/development
Browse files Browse the repository at this point in the history
Security Fixes
  • Loading branch information
Ashutosh pathak authored Jul 27, 2020
2 parents 9d5f75f + c5b5ef6 commit 4905665
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 243 deletions.
41 changes: 26 additions & 15 deletions app/Http/Controllers/Common/BaseSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ class BaseSettingsController extends PaymentSettingsController
{
use ApiKeySettings;

public function __construct()
{
$this->middleware('auth');
$this->middleware('admin');
}

/**
* Get the logged activity.
*/
Expand Down Expand Up @@ -333,20 +327,37 @@ public function captchaDetails(Request $request)
if ($status == 1) {
$nocaptcha_sitekey = $request->input('nocaptcha_sitekey');
$captcha_secretCheck = $request->input('nocaptcha_secret');
$path_to_file = base_path('.env');
$file_contents = file_get_contents($path_to_file);
$file_contents_sitekey = str_replace(env('NOCAPTCHA_SITEKEY'), $nocaptcha_sitekey, $file_contents);
file_put_contents($path_to_file, $file_contents_sitekey);
$file_contents_nocaptcha_secret = str_replace(env('NOCAPTCHA_SECRET'), $captcha_secretCheck, $file_contents);
file_put_contents($path_to_file, $file_contents_nocaptcha_secret);
$values = ['NOCAPTCHA_SITEKEY'=>$nocaptcha_sitekey, 'NOCAPTCHA_SECRET'=>$captcha_secretCheck];

$envFile = app()->environmentFilePath();
$str = file_get_contents($envFile);

if (count($values) > 0) {
foreach ($values as $envKey => $envValue) {
$str .= "\n"; // In case the searched variable is in the last line without \n
$keyPosition = strpos($str, "{$envKey}=");
$endOfLinePosition = strpos($str, "\n", $keyPosition);
$oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);

// If key does not exist, add it
if (! $keyPosition || ! $endOfLinePosition || ! $oldLine) {
$str .= "{$envKey}={$envValue}\n";
} else {
$str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
}
}
}

$str = substr($str, 0, -1);
if (! file_put_contents($envFile, $str)) {
return false;
}
} else {
$nocaptcha_sitekey = '00';
$captcha_secretCheck = '00';
$path_to_file = base_path('.env');
$file_contents = file_get_contents($path_to_file);
$file_contents_sitekey = str_replace(env('NOCAPTCHA_SITEKEY'), $nocaptcha_sitekey, $file_contents);
file_put_contents($path_to_file, $file_contents_sitekey);
$file_contents_secretchek = str_replace(env('NOCAPTCHA_SECRET'), $captcha_secretCheck, $file_contents);
$file_contents_secretchek = str_replace([env('NOCAPTCHA_SECRET'), env('NOCAPTCHA_SITEKEY')], [$captcha_secretCheck, $nocaptcha_sitekey], $file_contents);
file_put_contents($path_to_file, $file_contents_secretchek);
}

Expand Down
117 changes: 73 additions & 44 deletions app/Http/Controllers/Front/BaseCartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,14 @@ public function reduceAgentQty(Request $request)
{
try {
$id = $request->input('productid');
$cartValues = $this->getCartValues($id, true);
Cart::update($id, [
'price' => $cartValues['price'],
'attributes' => ['agents' => $cartValues['agtqty'], 'currency'=> ['currency'=>$cartValues['currency'], 'symbol'=>$cartValues['symbol']]],
]);
$hasPermissionToModifyAgent = Product::find($id)->can_modify_agent;
if ($hasPermissionToModifyAgent) {
$cartValues = $this->getCartValues($id, true);
Cart::update($id, [
'price' => $cartValues['price'],
'attributes' => ['agents' => $cartValues['agtqty'], 'currency'=> ['currency'=>$cartValues['currency'], 'symbol'=>$cartValues['symbol']]],
]);
}

return successResponse('Cart updated successfully');
} catch (\Exception $ex) {
Expand All @@ -212,22 +215,37 @@ public function updateAgentQty(Request $request)
{
try {
$id = $request->input('productid');
$cartValues = $this->getCartValues($id);
Cart::update($id, [
'price' => $cartValues['price'],
'attributes' => ['agents' => $cartValues['agtqty'], 'currency'=> ['currency'=>$cartValues['currency'], 'symbol'=>$cartValues['symbol']]],
]);
$hasPermissionToModifyAgent = Product::find($id)->can_modify_agent;
if ($hasPermissionToModifyAgent) {
$cartValues = $this->getCartValues($id);
Cart::update($id, [
'price' => $cartValues['price'],
'attributes' => ['agents' => $cartValues['agtqty'], 'currency'=> ['currency'=>$cartValues['currency'], 'symbol'=>$cartValues['symbol']]],
]);
}

return successResponse('Cart updated successfully');
} catch (\Exception $ex) {
return errorResponse($ex->getMessage());
}
}

/**
* The method returns the updated price, no of agents and currency of the product added to the cart
* Since this method is called when the the user has permission to modify agents(set from the admin panel), if the parameter $canReduceAgent is true the agent quantity and the cart total will be divided by two else they will be multiplied by two.
*
* Wehn the api for reducing the agent is called, agent gets divided by 2 and the price also gets divided by two, since we only increase and decrease agents/price by doubling them or making them half.
*
*
* @param int $productId The product to be added to cart
* @param bool $canReduceAgent Increase or decrease no of agents
*
* @return array
*/
private function getCartValues($productId, $canReduceAgent = false)
{
$cart = \Cart::get($productId);
$hasPermissionToModifyAgent = Product::find($productId)->can_modify_agent;

if ($cart) {
$agtqty = $cart->attributes->agents;
$price = \Cart::getTotal();
Expand All @@ -237,14 +255,12 @@ private function getCartValues($productId, $canReduceAgent = false)
throw new \Exception('Product not present in cart.');
}

if ($hasPermissionToModifyAgent) {
if ($canReduceAgent) {
$agtqty = $agtqty / 2;
$price = \Cart::getTotal() / 2;
} else {
$agtqty = $agtqty * 2;
$price = \Cart::getTotal() * 2;
}
if ($canReduceAgent) {
$agtqty = $agtqty / 2;
$price = \Cart::getTotal() / 2;
} else {
$agtqty = $agtqty * 2;
$price = \Cart::getTotal() * 2;
}

return ['agtqty'=>$agtqty, 'price'=>$price, 'currency'=>$currency, 'symbol'=>$symbol];
Expand All @@ -259,16 +275,23 @@ private function getCartValues($productId, $canReduceAgent = false)
*/
public function reduceProductQty(Request $request)
{
$id = $request->input('productid');
$cart = \Cart::get($id);
$qty = $cart->quantity - 1;
$price = $this->cost($id);
Cart::update($id, [
'quantity' => -1,
'price' => $price,
]);

return 'success';
try {
$id = $request->input('productid');
$hasPermissionToModifyQuantity = Product::find($id)->can_modify_quantity;
if ($hasPermissionToModifyQuantity) {
$cart = \Cart::get($id);
$qty = $cart->quantity - 1;
$price = $this->cost($id);
Cart::update($id, [
'quantity' => -1,
'price' => $price,
]);
} else {
throw new \Exception('Cannot Modify Quantity');
}
} catch (\Exception $ex) {
return redirect()->back()->with('fails', $ex->getMessage());
}
}

/**
Expand All @@ -280,19 +303,26 @@ public function reduceProductQty(Request $request)
*/
public function updateProductQty(Request $request)
{
$id = $request->input('productid');
$cart = \Cart::get($id);
$qty = $cart->quantity + 1;
$price = $this->cost($id);
Cart::update($id, [
'quantity' => [
'relative' => false,
'value' => $qty,
],
'price' => $price,
]);

return 'success';
try {
$id = $request->input('productid');
$hasPermissionToModifyQuantity = Product::find($id)->can_modify_quantity;
if ($hasPermissionToModifyQuantity) {
$cart = \Cart::get($id);
$qty = $cart->quantity + 1;
$price = $this->cost($id);
Cart::update($id, [
'quantity' => [
'relative' => false,
'value' => $qty,
],
'price' => $price,
]);
} else {
throw new \Exception('Cannot Modify Quantity');
}
} catch (\Exception $ex) {
return redirect()->back()->with('fails', $ex->getMessage());
}
}

/**
Expand Down Expand Up @@ -332,8 +362,7 @@ public function addProduct(int $id)
} catch (\Exception $e) {
app('log')->error($e->getMessage());
Bugsnag::notifyException($e);

return redirect()->back()->with('fails', $e->getMessage());
throw new \Exception($e->getMessage());
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Front/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,11 @@ public function cost($productid, $userid = '', $planid = '')
} catch (\Exception $ex) {
Bugsnag::notifyException($ex->getMessage());
app('log')->error($ex->getMessage());
throw new \Exception($ex->getMessage());
}
}

public function updateFinalPrice(Request $request)
public static function updateFinalPrice(Request $request)
{
$value = $request->input('processing_fee').'%';
$updateValue = new CartCondition([
Expand Down
43 changes: 24 additions & 19 deletions app/Http/Controllers/Front/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public function checkoutForm(Request $request)

return redirect('auth/login')->with('fails', 'Please login');
}
if (\Cart::isEmpty()) {//During renewal when payment fails due to some reason
$invoice = \Session::get('invoice');
if ($invoice && \Session::has('fails')) {
return redirect('paynow/'.$invoice->id)->with('fails', 'Payment cannot be processed. Please try the other gateway.');
}
}
if (\Session::has('items')) {
$content = \Session::get('items');
$attributes = $this->getAttributes($content);
Expand Down Expand Up @@ -190,22 +196,23 @@ public function payNow($invoiceid)

public function postCheckout(Request $request)
{
$invoice_controller = new \App\Http\Controllers\Order\InvoiceController();
$info_cont = new \App\Http\Controllers\Front\InfoController();
$payment_method = $request->input('payment_gateway');
\Session::put('payment_method', $payment_method);
$paynow = $this->checkregularPaymentOrRenewal($request->input('invoice_id'));
$cost = $request->input('cost');
$state = $this->getState();
if (Cart::getSubTotal() != 0 || $cost > 0) {
$this->validate($request, [
'payment_gateway'=> 'required',
], [
'payment_gateway.required'=> 'Please Select a Payment Gateway',
]);
}

try {
$invoice_controller = new \App\Http\Controllers\Order\InvoiceController();
$info_cont = new \App\Http\Controllers\Front\InfoController();
$payment_method = $request->input('payment_gateway');
\Session::put('payment_method', $payment_method);
$paynow = $this->checkregularPaymentOrRenewal($request->input('invoice_id'));
$cost = $request->input('cost');
$state = $this->getState();

if ($paynow === false) {
/*
* Do order, invoicing etc
Expand All @@ -219,7 +226,9 @@ public function postCheckout(Request $request)
$invoice_no = $invoice->number;
$date = $this->getDate($invoice);
$invoiceid = $invoice->id;
$amount = $invoice->grand_total;
$processingFee = $this->getProcessingFee($payment_method, $invoice->currency);
CartController::updateFinalPrice(new Request(['processing_fee'=>$processingFee]));
$amount = Cart::getTotal();
$url = '';
$cart = Cart::getContent();
$invoices = $this->invoice->find($invoiceid);
Expand Down Expand Up @@ -317,20 +326,16 @@ private function getProcessingFee($paymentMethod, $currency)

public function checkregularPaymentOrRenewal($invoiceid)
{
try {
$paynow = false;
$paynow = false;

if ($invoiceid) {
if (Invoice::find($invoiceid)->user_id != \Auth::user()->id) {
throw new \Exception('Invalid modification of data');
}
$paynow = true;
if ($invoiceid) {
if (Invoice::find($invoiceid)->user_id != \Auth::user()->id) {
throw new \Exception('Invalid modification of data');
}

return $paynow;
} catch (\Exception $ex) {
return redirect()->back()->with('fails', $ex->getMessage());
$paynow = true;
}

return $paynow;
}

public function checkoutAction($invoice)
Expand Down
9 changes: 4 additions & 5 deletions app/Http/Controllers/Front/ExtendedBaseCartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ public function planCost($productid, $userid, $planid = '')
$months = 0;
$cont = new CartController();
$currency = $cont->currency($userid);
if ($this->checkPlanSession() === true) {
$planid = Session::get('plan');
}
if (! $planid) {//When Product Is Added from Cart
$planid = Plan::where('product', $productid)->pluck('id')->first();
} elseif ($this->checkPlanSession() === true && ! $planid) {
$planid = Session::get('plan');
}
$plan = Plan::where('id', $planid)->where('product', $productid)->first();
if ($plan) { //Get the Total Plan Cost if the Plan Exists For a Product
Expand All @@ -151,12 +150,12 @@ public function planCost($productid, $userid, $planid = '')
}
$finalPrice = str_replace(',', '', $price);
$cost = round($months) * $finalPrice;
} else {
throw new \Exception('Product cannot be added to cart. No such plan exists.');
}

return $cost;
} catch (\Exception $ex) {
dd($ex);

throw new \Exception($ex->getMessage());
}
}
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/Google2FAController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,16 @@ public function postLoginValidateToken(ValidateSecretRequest $request)
$this->user = User::findorFail($userId);
$secret = Crypt::decrypt($this->user->google2fa_secret);
$checkValidPasscode = Google2FA::verifyKey($secret, $request->totp);

//login and redirect user
if ($checkValidPasscode) {
if (\Session::has('reset_token')) {
$token = \Session::get('reset_token');
\Session::put('2fa_verified', 1);
\Session::forget('2fa:user:id');

return redirect('password/reset/'.$token);
}
\Auth::loginUsingId($userId);

return redirect()->intended($this->redirectPath());
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Order/BaseOrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public function getMail($setting, $user, $downloadurl, $invoiceurl, $order, $pro

return $mail;
} catch (\Exception $ex) {
throw new Exception($ex->getMessage());
throw new \Exception($ex->getMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Model/User/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
class Password extends Model
{
protected $table = 'password_resets';
protected $fillable = ['email', 'token'];
protected $fillable = ['email', 'token', 'created_at'];
public $timestamps = false;
}
Loading

0 comments on commit 4905665

Please sign in to comment.