Hide sensitive info from log

This commit is contained in:
DariusIII
2026-06-30 19:05:15 +02:00
parent 5b927b4581
commit 0790d2290c
4 changed files with 684 additions and 3 deletions
@@ -0,0 +1,241 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Facades\Search;
use App\Models\Category;
use App\Models\Release;
use App\Services\BookService;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PDO;
use Tests\TestCase;
class BookServiceObfuscatedNormalizationTest extends TestCase
{
private string $databasePath;
/**
* @var array<string, string|false>
*/
private array $originalEnvironment = [];
public function createApplication()
{
$this->databasePath = sys_get_temp_dir().'/nntmux-book-obfuscated-normalization.sqlite';
$this->originalEnvironment = [
'APP_ENV' => getenv('APP_ENV'),
'DB_CONNECTION' => getenv('DB_CONNECTION'),
'DB_DATABASE' => getenv('DB_DATABASE'),
];
if (file_exists($this->databasePath)) {
unlink($this->databasePath);
}
$pdo = new PDO('sqlite:'.$this->databasePath);
$pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)');
$pdo->exec("INSERT INTO settings (name, value) VALUES ('maxbooksprocessed', '50'), ('amazonsleep', '0'), ('lookupbooks', '1')");
$this->setEnvironmentValue('APP_ENV', 'testing');
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
protected function setUp(): void
{
parent::setUp();
DB::table('settings')->upsert([
['name' => 'maxbooksprocessed', 'value' => '50'],
['name' => 'amazonsleep', 'value' => '0'],
['name' => 'lookupbooks', 'value' => '1'],
], ['name'], ['value']);
config([
'database.default' => 'sqlite',
'database.connections.sqlite.database' => $this->databasePath,
]);
DB::purge();
DB::reconnect();
$this->createSchema();
}
protected function tearDown(): void
{
if ($this->databasePath !== '' && file_exists($this->databasePath)) {
unlink($this->databasePath);
}
parent::tearDown();
foreach ($this->originalEnvironment as $key => $value) {
$this->setEnvironmentValue($key, $value === false ? null : $value);
}
}
public function test_process_book_releases_normalizes_obfuscated_searchnames_for_existing_book_rows(): void
{
Search::shouldReceive('updateRelease')->once();
DB::table('releases')->insert([
'id' => 1,
'name' => "N_NZB_[1_6]_-_Woman's_Day_New_Zealand_-_Issue_45_April_27_2026.par2",
'searchname' => "N_NZB_[1_6]_-_Woman's_Day_New_Zealand_-_Issue_45_April_27_2026.par2",
'groups_id' => 1,
'size' => 1,
'postdate' => now(),
'adddate' => now(),
'guid' => str_repeat('a', 40),
'leftguid' => 'a',
'fromname' => 'poster@example.com',
'categories_id' => Category::BOOKS_MAGAZINES,
'videos_id' => 0,
'tv_episodes_id' => 0,
'imdbid' => null,
'musicinfo_id' => null,
'consoleinfo_id' => null,
'bookinfo_id' => 123,
'anidbid' => null,
'predb_id' => 0,
'iscategorized' => 1,
'isrenamed' => 0,
'proc_nfo' => 0,
'proc_files' => 0,
'proc_par2' => 0,
'proc_uid' => 0,
'proc_hash16k' => 0,
'proc_srr' => 0,
'proc_crc32' => 0,
'passwordstatus' => 0,
'nzbstatus' => 1,
]);
$service = app(BookService::class);
$service->processBookReleases();
$release = Release::query()->findOrFail(1);
$this->assertSame("Woman's Day New Zealand - Issue 45 April 27 2026", $release->searchname);
$this->assertSame(123, (int) $release->bookinfo_id);
$this->assertSame(1, (int) $release->isrenamed);
}
public function test_process_book_releases_normalizes_existing_mcn_magazine_searchname(): void
{
Search::shouldReceive('updateRelease')->once();
DB::table('releases')->insert([
'id' => 2,
'name' => 'MCN.April.22.2026.HYBRID.MAGAZINE.eBook-21A1',
'searchname' => 'MCN.April.22.2026.HYBRID.MAGAZINE.eBook-21A1',
'groups_id' => 1,
'size' => 1,
'postdate' => now(),
'adddate' => now(),
'guid' => str_repeat('b', 40),
'leftguid' => 'b',
'fromname' => 'poster@example.com',
'categories_id' => Category::BOOKS_MAGAZINES,
'videos_id' => 0,
'tv_episodes_id' => 0,
'imdbid' => null,
'musicinfo_id' => null,
'consoleinfo_id' => null,
'bookinfo_id' => -2,
'anidbid' => null,
'predb_id' => 0,
'iscategorized' => 1,
'isrenamed' => 0,
'proc_nfo' => 0,
'proc_files' => 0,
'proc_par2' => 0,
'proc_uid' => 0,
'proc_hash16k' => 0,
'proc_srr' => 0,
'proc_crc32' => 0,
'passwordstatus' => 0,
'nzbstatus' => 1,
]);
$service = app(BookService::class);
$service->processBookReleases();
$release = Release::query()->findOrFail(2);
$this->assertSame('MCN - April 22, 2026', $release->searchname);
$this->assertSame(-2, (int) $release->bookinfo_id);
$this->assertSame(1, (int) $release->isrenamed);
}
private function setEnvironmentValue(string $key, ?string $value): void
{
if ($value === null) {
putenv($key);
unset($_ENV[$key], $_SERVER[$key]);
return;
}
putenv($key.'='.$value);
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
private function createSchema(): void
{
if (! Schema::hasTable('settings')) {
Schema::create('settings', function (Blueprint $table): void {
$table->string('name')->primary();
$table->text('value')->nullable();
});
}
if (! Schema::hasTable('releases')) {
Schema::create('releases', function (Blueprint $table): void {
$table->increments('id');
$table->string('name')->default('');
$table->string('searchname')->default('');
$table->unsignedInteger('groups_id')->default(0);
$table->unsignedBigInteger('size')->default(0);
$table->dateTime('postdate')->nullable();
$table->dateTime('adddate')->nullable();
$table->string('guid', 40);
$table->char('leftguid', 1);
$table->string('fromname')->nullable();
$table->integer('categories_id')->default(Category::OTHER_MISC);
$table->unsignedInteger('videos_id')->default(0);
$table->integer('tv_episodes_id')->default(0);
$table->string('imdbid')->nullable();
$table->integer('musicinfo_id')->nullable();
$table->integer('consoleinfo_id')->nullable();
$table->integer('bookinfo_id')->nullable();
$table->integer('anidbid')->nullable();
$table->unsignedInteger('predb_id')->default(0);
$table->tinyInteger('iscategorized')->default(0);
$table->tinyInteger('isrenamed')->default(0);
$table->tinyInteger('proc_nfo')->default(0);
$table->tinyInteger('proc_files')->default(0);
$table->tinyInteger('proc_par2')->default(0);
$table->tinyInteger('proc_uid')->default(0);
$table->tinyInteger('proc_hash16k')->default(0);
$table->tinyInteger('proc_srr')->default(0);
$table->tinyInteger('proc_crc32')->default(0);
$table->tinyInteger('passwordstatus')->default(0);
$table->tinyInteger('nzbstatus')->default(0);
});
}
}
}
@@ -0,0 +1,165 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Category;
use App\Services\Runners\PostProcessRunner;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PDO;
use Tests\TestCase;
class PostProcessRunnerBooksGateTest extends TestCase
{
private string $databasePath;
/**
* @var array<string, string|false>
*/
private array $originalEnvironment = [];
public function createApplication()
{
$this->databasePath = sys_get_temp_dir().'/nntmux-postprocess-books-gate.sqlite';
$this->originalEnvironment = [
'APP_ENV' => getenv('APP_ENV'),
'DB_CONNECTION' => getenv('DB_CONNECTION'),
'DB_DATABASE' => getenv('DB_DATABASE'),
];
if (file_exists($this->databasePath)) {
unlink($this->databasePath);
}
$pdo = new PDO('sqlite:'.$this->databasePath);
$pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)');
$pdo->exec("INSERT INTO settings (name, value) VALUES ('lookupbooks', '1'), ('postthreadsnon', '1')");
$this->setEnvironmentValue('APP_ENV', 'testing');
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
protected function setUp(): void
{
parent::setUp();
DB::table('settings')->upsert([
['name' => 'lookupbooks', 'value' => '1'],
['name' => 'postthreadsnon', 'value' => '1'],
], ['name'], ['value']);
config([
'database.default' => 'sqlite',
'database.connections.sqlite.database' => $this->databasePath,
]);
DB::purge();
DB::reconnect();
$this->createSchema();
}
protected function tearDown(): void
{
if ($this->databasePath !== '' && file_exists($this->databasePath)) {
unlink($this->databasePath);
}
parent::tearDown();
foreach ($this->originalEnvironment as $key => $value) {
$this->setEnvironmentValue($key, $value === false ? null : $value);
}
}
public function test_process_books_treats_obfuscated_book_rows_as_pending_work(): void
{
DB::table('releases')->insert([
'id' => 1,
'name' => 'N_NZB_[2_5]_-_History_of_War_-_Issue_158_2026.rar',
'searchname' => 'N_NZB_[2_5]_-_History_of_War_-_Issue_158_2026.rar',
'groups_id' => 1,
'size' => 1,
'postdate' => now(),
'adddate' => now(),
'guid' => str_repeat('a', 40),
'leftguid' => 'a',
'fromname' => 'poster@example.com',
'categories_id' => Category::BOOKS_MAGAZINES,
'bookinfo_id' => -2,
]);
$runner = new class extends PostProcessRunner
{
public array $captured = [];
public function headerNone(): void {}
protected function headerStart(string $workType, int $count, int $maxProcesses): void {}
protected function executeCommand(string $command): string
{
$this->captured[] = $command;
return '';
}
};
$runner->processBooks();
$this->assertCount(1, $runner->captured);
$this->assertStringContainsString('artisan postprocess:guid books a', $runner->captured[0]);
}
private function setEnvironmentValue(string $key, ?string $value): void
{
if ($value === null) {
putenv($key);
unset($_ENV[$key], $_SERVER[$key]);
return;
}
putenv($key.'='.$value);
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
private function createSchema(): void
{
if (! Schema::hasTable('settings')) {
Schema::create('settings', function (Blueprint $table): void {
$table->string('name')->primary();
$table->text('value')->nullable();
});
}
if (! Schema::hasTable('releases')) {
Schema::create('releases', function (Blueprint $table): void {
$table->increments('id');
$table->string('name')->default('');
$table->string('searchname')->default('');
$table->unsignedInteger('groups_id')->default(0);
$table->unsignedBigInteger('size')->default(0);
$table->dateTime('postdate')->nullable();
$table->dateTime('adddate')->nullable();
$table->string('guid', 40);
$table->char('leftguid', 1);
$table->string('fromname')->nullable();
$table->integer('categories_id')->default(Category::OTHER_MISC);
$table->integer('bookinfo_id')->nullable();
});
}
}
}
+220
View File
@@ -0,0 +1,220 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Http\Middleware\BlockAbusiveServices;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use PDO;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;
class BlockAbusiveServicesTest extends TestCase
{
private string $databasePath;
/**
* @var array<string, string|false>
*/
private array $originalEnvironment = [];
public function createApplication()
{
$this->databasePath = sys_get_temp_dir().'/nntmux-block-abusive-services-test.sqlite';
$this->originalEnvironment = [
'APP_ENV' => getenv('APP_ENV'),
'DB_CONNECTION' => getenv('DB_CONNECTION'),
'DB_DATABASE' => getenv('DB_DATABASE'),
];
if (file_exists($this->databasePath)) {
unlink($this->databasePath);
}
$pdo = new PDO('sqlite:'.$this->databasePath);
$pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)');
$pdo->exec("INSERT INTO settings (name, value) VALUES
('categorizeforeign', '0'),
('catwebdl', '0'),
('innerfileblacklist', '')");
$this->setEnvironmentValue('APP_ENV', 'testing');
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
protected function tearDown(): void
{
if ($this->databasePath !== '' && file_exists($this->databasePath)) {
unlink($this->databasePath);
}
parent::tearDown();
foreach ($this->originalEnvironment as $key => $value) {
$this->setEnvironmentValue($key, $value === false ? null : $value);
}
}
public function test_disabled_proxy_indexer_app_block_allows_configured_user_agent_on_indexer_endpoint(): void
{
config()->set('nntmux.block_proxy_indexer_apps', false);
config()->set('nntmux.block_proxy_indexer_app_user_agents', 'Prowlarr/,NZBHydra2');
$response = $this->handleRequest('/api/v1/api?t=search&q=linux', 'Prowlarr/2.0.0');
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
}
public function test_enabled_proxy_indexer_app_block_denies_configured_user_agent_on_newznab_endpoints(): void
{
config()->set('nntmux.block_proxy_indexer_apps', true);
config()->set('nntmux.block_proxy_indexer_app_user_agents', 'Prowlarr/,NZBHydra2');
foreach ([
'/api/v1/api?t=caps',
'/api/v1/api?t=search&q=linux',
'/api/v1/api?t=get&id=release-guid',
] as $uri) {
$response = $this->handleRequest($uri, 'Prowlarr/2.0.0');
$this->assertSame(Response::HTTP_FORBIDDEN, $response->getStatusCode(), $uri);
$this->assertStringContainsString('Proxy indexer app access is not allowed', (string) $response->getContent());
}
}
public function test_enabled_proxy_indexer_app_block_denies_configured_user_agent_on_v2_endpoints(): void
{
config()->set('nntmux.block_proxy_indexer_apps', true);
config()->set('nntmux.block_proxy_indexer_app_user_agents', 'Prowlarr/,NZBHydra2');
foreach ([
'/api/v2/capabilities',
'/api/v2/search?q=linux',
'/api/v2/getnzb?id=release-guid',
] as $uri) {
$response = $this->handleRequest($uri, 'NZBHydra2 8.3.0');
$this->assertSame(Response::HTTP_FORBIDDEN, $response->getStatusCode(), $uri);
}
}
public function test_enabled_proxy_indexer_app_block_denies_configured_user_agent_on_rss_feed_endpoints(): void
{
config()->set('nntmux.block_proxy_indexer_apps', true);
config()->set('nntmux.block_proxy_indexer_app_user_agents', 'Prowlarr/,NZBHydra2');
foreach ([
'/rss/full-feed?api_token=token',
'/rss/category?api_token=token&t=2000',
] as $uri) {
$response = $this->handleRequest($uri, 'Prowlarr/2.0.0');
$this->assertSame(Response::HTTP_FORBIDDEN, $response->getStatusCode(), $uri);
}
}
public function test_enabled_proxy_indexer_app_block_allows_redirected_downloader_user_agent_on_same_download_url(): void
{
config()->set('nntmux.block_proxy_indexer_apps', true);
config()->set('nntmux.block_proxy_indexer_app_user_agents', 'Prowlarr/,NZBHydra2');
$response = $this->handleRequest('/api/v1/api?t=get&id=release-guid', 'SABnzbd/4.3.3');
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
}
public function test_enabled_proxy_indexer_app_block_allows_configured_user_agent_on_unrelated_routes(): void
{
config()->set('nntmux.block_proxy_indexer_apps', true);
config()->set('nntmux.block_proxy_indexer_app_user_agents', 'Prowlarr/,NZBHydra2');
foreach ([
'/api/inform/release',
'/api/release/123/mediainfo',
'/rss/health',
] as $uri) {
$response = $this->handleRequest($uri, 'Prowlarr/2.0.0');
$this->assertSame(Response::HTTP_OK, $response->getStatusCode(), $uri);
}
}
public function test_existing_abusive_user_agent_block_still_applies(): void
{
config()->set('nntmux.block_proxy_indexer_apps', false);
$response = $this->handleRequest('/api/v1/api?t=caps', 'AIOStreams/1.0');
$this->assertSame(Response::HTTP_FORBIDDEN, $response->getStatusCode());
$this->assertStringContainsString('Streaming services are not allowed', (string) $response->getContent());
}
public function test_block_logs_redact_sensitive_query_parameters(): void
{
Log::spy();
config()->set('nntmux.block_proxy_indexer_apps', true);
config()->set('nntmux.block_proxy_indexer_app_user_agents', 'Prowlarr/,NZBHydra2');
$response = $this->handleRequest(
'/api/v1/api?t=search&apikey=secret-api-key&api_token=secret-token&passkey=secret-passkey&q=linux',
'Prowlarr/2.0.0'
);
$this->assertSame(Response::HTTP_FORBIDDEN, $response->getStatusCode());
Log::shouldHaveReceived('warning')
->with('Blocked proxy indexer app request', \Mockery::on(function (mixed $context): bool {
if (! is_array($context)) {
return false;
}
$uri = (string) ($context['uri'] ?? '');
return str_contains($uri, 'apikey=%5Bredacted%5D')
&& str_contains($uri, 'api_token=%5Bredacted%5D')
&& str_contains($uri, 'passkey=%5Bredacted%5D')
&& str_contains($uri, 'q=linux')
&& ! str_contains($uri, 'secret-api-key')
&& ! str_contains($uri, 'secret-token')
&& ! str_contains($uri, 'secret-passkey');
}))
->once();
}
private function setEnvironmentValue(string $key, ?string $value): void
{
if ($value === null) {
putenv($key);
unset($_ENV[$key], $_SERVER[$key]);
return;
}
putenv($key.'='.$value);
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
private function handleRequest(string $uri, string $userAgent): Response
{
$request = Request::create($uri, 'GET', server: [
'HTTP_USER_AGENT' => $userAgent,
'REMOTE_ADDR' => '127.0.0.1',
]);
return app(BlockAbusiveServices::class)->handle(
$request,
static fn (): Response => response()->json(['ok' => true])
);
}
}