Skip to content

Commit 14f8d2b

Browse files
committed
feat: improve security and code
1 parent ccb857f commit 14f8d2b

File tree

6 files changed

+29
-28
lines changed

6 files changed

+29
-28
lines changed

src/Core/Facades/App.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class App
1515
*
1616
* @var Application $app
1717
*/
18-
public static $app;
18+
private static $app;
1919

2020
/**
2121
* Bikin objek untuk pertama kalinya.

src/Core/Facades/Service.php

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ protected function registerProvider(): void
8888
{
8989
foreach ($this->kernel->services() as $service) {
9090
$this->app->invoke($service, Provider::REGISTRASI);
91-
$this->app->clean($service);
9291
}
9392
}
9493

src/Core/Facades/Web.php

+14-18
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
use Core\Http\Exception\NotAllowedException;
99
use Core\Http\Exception\NotFoundException;
1010
use Core\Http\Exception\StreamTerminate;
11-
use Core\Http\Respond;
1211
use Core\Http\Session;
13-
use Core\Http\Stream;
1412
use Core\Middleware\Middleware;
1513
use Core\Middleware\MiddlewareInterface;
1614
use Core\Routing\Controller;
@@ -78,33 +76,31 @@ private function process(array $route): mixed
7876

7977
if ($controller) {
8078
$controller = $this->app->singleton($controller);
81-
if (!($controller instanceof Controller)) {
82-
throw new Exception(sprintf('Class "%s" is not extends BaseController.', get_class($controller)));
79+
if (!($controller instanceof Controller) && !($controller instanceof Provider)) {
80+
throw new Exception(sprintf('Class "%s" is not extends Controller or Provider.', get_class($controller)));
8381
}
8482
}
8583

86-
$attributeMiddleware = [];
84+
$middlewares = [
85+
...$this->kernel->middlewares(),
86+
...$route['middleware'],
87+
];
88+
8789
if ($controller && $function) {
8890
foreach ($this->app->getAttribute($controller, $function) as $value) {
89-
$name = $value->getName();
90-
$object = new $name();
91+
$object = $this->app->singleton($value->getName());
9192

9293
if ($object instanceof MiddlewareInterface) {
93-
$attributeMiddleware[] = $object;
94+
array_push($middlewares, $object);
9495
}
9596
}
9697
}
9798

98-
$middleware = new Middleware([
99-
...$this->kernel->middlewares(),
100-
...$route['middleware'],
101-
...$attributeMiddleware
102-
]);
103-
104-
$result = $middleware->handle(
105-
$this->request,
106-
$this->coreMiddleware($controller, $function)
107-
);
99+
$result = $this->app->make(Middleware::class, [$middlewares])
100+
->handle(
101+
$this->request,
102+
$this->coreMiddleware($controller, $function)
103+
);
108104

109105
$error = error_get_last();
110106
if ($error !== null) {

src/Core/Middleware/Middleware.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Core\Middleware;
44

55
use Closure;
6+
use Core\Facades\App;
67
use Core\Http\Request;
78

89
/**
@@ -30,7 +31,7 @@ class Middleware
3031
public function __construct(array $layers = [])
3132
{
3233
for ($i = (count($layers) - 1); $i >= 0; $i--) {
33-
$this->layers[] = is_object($layers[$i]) ? $layers[$i] : new $layers[$i];
34+
$this->layers[] = is_object($layers[$i]) ? $layers[$i] : App::get()->singleton($layers[$i]);
3435
}
3536
}
3637

src/Core/Valid/Hash.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public static function encrypt(string $str): string
4141
{
4242
$key = explode(static::SPTR, env('APP_KEY', static::SPTR), 2);
4343
$iv = openssl_random_pseudo_bytes(intval(openssl_cipher_iv_length(static::CIPHERING)));
44-
$encrypted = openssl_encrypt($str, static::CIPHERING, base64_decode($key[1]), OPENSSL_RAW_DATA, $iv);
44+
$encrypted = openssl_encrypt($str, static::CIPHERING, strval(base64_decode($key[1], true)), OPENSSL_RAW_DATA, $iv);
4545

46-
return base64_encode($iv . hash_hmac(static::HASH, $encrypted, base64_decode($key[0]), true) . $encrypted);
46+
return base64_encode($iv . hash_hmac(static::HASH, $encrypted, strval(base64_decode($key[0], true)), true) . $encrypted);
4747
}
4848

4949
/**
@@ -66,12 +66,12 @@ public static function decrypt(string $str): string|null
6666

6767
if (!hash_equals(
6868
substr($raw, $iv, 64),
69-
hash_hmac(static::HASH, $encrypted, base64_decode($key[0]), true)
69+
hash_hmac(static::HASH, $encrypted, strval(base64_decode($key[0], true)), true)
7070
)) {
7171
return null;
7272
}
7373

74-
$result = openssl_decrypt($encrypted, static::CIPHERING, base64_decode($key[1]), OPENSSL_RAW_DATA, substr($raw, 0, $iv));
74+
$result = openssl_decrypt($encrypted, static::CIPHERING, strval(base64_decode($key[1], true)), OPENSSL_RAW_DATA, substr($raw, 0, $iv));
7575
if ($result === false) {
7676
return null;
7777
}

src/helpers/helpers.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,12 @@ function format_bytes(float $size, int $precision = 2): string
590590
$base = log($size, 1024);
591591
$suffixes = ['Byte', 'Kb', 'Mb', 'Gb', 'Tb'];
592592

593-
return strval(round(pow(1024, $base - floor($base)), $precision)) . $suffixes[intval(floor($base))];
593+
$index = intval(floor($base));
594+
if ($index === -1) {
595+
return 'NaN' . $suffixes[0];
596+
}
597+
598+
return strval(round(pow(1024, $base - floor($base)), $precision)) . $suffixes[$index];
594599
}
595600
}
596601

@@ -672,9 +677,9 @@ function fake(string $locale = 'id_ID'): \Faker\Generator
672677
/**
673678
* Panggil secara satu kali saja.
674679
*
675-
* @template TReturnType
680+
* @template TReturnType
676681
*
677-
* @param callable(): TReturnType $callback
682+
* @param callable(): TReturnType $callback
678683
* @return TReturnType
679684
*/
680685
function &once(callable $callback): mixed

0 commit comments

Comments
 (0)