-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9de70cb
commit 6fa04fd
Showing
6 changed files
with
155 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
web/app/Http/Controllers/Api/Request/HostingSubscriptionCreateRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\Request; | ||
|
||
class HostingSubscriptionCreateRequest extends AuthorizedApiRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'customer_id' => 'required|exists:customers,id', | ||
'hosting_plan_id' => 'required|exists:hosting_plans,id', | ||
'domain' => 'required|regex:/^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace tests\Unit; | ||
|
||
use Faker\Factory; | ||
use Tests\Feature\Api\ActionTestCase; | ||
|
||
class SecurityTest extends ActionTestCase | ||
{ | ||
public function testSecurity() | ||
{ | ||
$callHostingSubscriptionStoreResponse = $this->callApiAuthorizedRouteAction( | ||
'api.hosting-subscriptions.store', | ||
[ | ||
'customer_id' => '34232432', | ||
'hosting_plan_id'=> '4443232', | ||
'domain' => 'broken-domain-name', | ||
] | ||
)->json(); | ||
|
||
$this->assertArrayHasKey('error', $callHostingSubscriptionStoreResponse); | ||
$this->assertArrayHasKey('message', $callHostingSubscriptionStoreResponse); | ||
$this->assertArrayHasKey('data', $callHostingSubscriptionStoreResponse); | ||
$this->assertArrayHasKey('domain', $callHostingSubscriptionStoreResponse['data']); | ||
$this->assertSame('The selected customer id is invalid.', $callHostingSubscriptionStoreResponse['message']); | ||
|
||
$this->assertSame('The selected hosting plan id is invalid.', $callHostingSubscriptionStoreResponse['data']['hosting_plan_id'][0]); | ||
$this->assertSame('The domain field format is invalid.', $callHostingSubscriptionStoreResponse['data']['domain'][0]); | ||
|
||
|
||
// Create a customer | ||
$faker = Factory::create(); | ||
$randomName = $faker->firstName() . ' ' . $faker->lastName(); | ||
$randomEmail = $faker->email(); | ||
$callCustomerStoreResponse = $this->callApiAuthorizedRouteAction( | ||
'api.customers.store', | ||
[ | ||
'name' => $randomName, | ||
'email' => $randomEmail, | ||
] | ||
)->json(); | ||
$this->assertArrayHasKey('status', $callCustomerStoreResponse); | ||
$this->assertTrue($callCustomerStoreResponse['status'] == 'ok'); | ||
|
||
$this->assertArrayHasKey('message', $callCustomerStoreResponse); | ||
$this->assertArrayHasKey('data', $callCustomerStoreResponse); | ||
$this->assertArrayHasKey('customer', $callCustomerStoreResponse['data']); | ||
$this->assertArrayHasKey('id', $callCustomerStoreResponse['data']['customer']); | ||
$this->assertIsInt($callCustomerStoreResponse['data']['customer']['id']); | ||
$customerId = $callCustomerStoreResponse['data']['customer']['id']; | ||
|
||
// Create a hosting subscription | ||
$randId = rand(1000, 9999); | ||
$callHostingPlansResponse = $this->callApiAuthorizedRouteAction('api.hosting-plans.index')->json(); | ||
$this->assertArrayHasKey('status', $callHostingPlansResponse); | ||
$this->assertTrue($callHostingPlansResponse['status'] == 'ok'); | ||
$this->assertArrayHasKey('data', $callHostingPlansResponse); | ||
$this->assertArrayHasKey('hostingPlans', $callHostingPlansResponse['data']); | ||
$this->assertIsArray($callHostingPlansResponse['data']['hostingPlans']); | ||
$this->assertNotEmpty($callHostingPlansResponse['data']['hostingPlans']); | ||
$hostingPlanId = $callHostingPlansResponse['data']['hostingPlans'][0]['id']; | ||
$this->assertIsInt($hostingPlanId); | ||
|
||
$hostingSubscriptionDomain = 'phyre-unit-test-'.$randId.'.com'; | ||
$callHostingSubscriptionStoreResponse = $this->callApiAuthorizedRouteAction( | ||
'api.hosting-subscriptions.store', | ||
[ | ||
'customer_id' => $customerId, | ||
'hosting_plan_id'=> $hostingPlanId, | ||
'domain' => $hostingSubscriptionDomain, | ||
] | ||
)->json(); | ||
|
||
$this->assertArrayHasKey('status', $callHostingSubscriptionStoreResponse); | ||
$this->assertTrue($callHostingSubscriptionStoreResponse['status'] == 'ok'); | ||
|
||
$hostingSubscription = $callHostingSubscriptionStoreResponse['data']['hostingSubscription']; | ||
|
||
// Check user home dir permissions | ||
$userHomeDir = '/home/' . $hostingSubscription['system_username']; | ||
$this->assertDirectoryExists($userHomeDir); | ||
$getUserHomeDirPermission = substr(sprintf('%o', fileperms($userHomeDir)), -4); | ||
$this->assertSame('0775', $getUserHomeDirPermission); | ||
|
||
// Check domain dir permissions | ||
$domainDir = '/home/' . $hostingSubscription['system_username'] . '/public_html'; | ||
$this->assertDirectoryExists($domainDir); | ||
$getDomainDirPermission = substr(sprintf('%o', fileperms($domainDir)), -4); | ||
$this->assertSame('0775', $getDomainDirPermission); | ||
|
||
// Check domain dir file permissions | ||
$domainDirFile = '/home/' . $hostingSubscription['system_username'] . '/public_html/index.php'; | ||
$this->assertFileExists($domainDirFile); | ||
$getDomainDirFilePermission = substr(sprintf('%o', fileperms($domainDirFile)), -4); | ||
$this->assertSame('0775', $getDomainDirFilePermission); | ||
|
||
// Create second hosting subscription | ||
$randId = rand(1000, 9999); | ||
$callHostingSubscriptionStoreResponse = $this->callApiAuthorizedRouteAction( | ||
'api.hosting-subscriptions.store', | ||
[ | ||
'customer_id' => $customerId, | ||
'hosting_plan_id'=> $hostingPlanId, | ||
'domain' => 'phyre-unit-test-'.$randId.'.com', | ||
] | ||
)->json(); | ||
$this->assertArrayHasKey('status', $callHostingSubscriptionStoreResponse); | ||
$this->assertTrue($callHostingSubscriptionStoreResponse['status'] == 'ok'); | ||
$secondHostingSubscription = $callHostingSubscriptionStoreResponse['data']['hostingSubscription']; | ||
|
||
// Try to open /home directory with linux user | ||
$output = shell_exec("sudo -H -u ".$hostingSubscription['system_username']." bash -c 'ls -la /home'"); | ||
$this->assertSame($output, null); | ||
|
||
// Try to open /home/$user with linux user | ||
$output = shell_exec("sudo -H -u ".$hostingSubscription['system_username']." bash -c 'ls -la /home/".$hostingSubscription['system_username']."'"); | ||
$this->assertTrue(str_contains($output, 'public_html')); | ||
$this->assertTrue(str_contains($output, $hostingSubscription['system_username'])); | ||
|
||
|
||
|
||
} | ||
} |