Page MenuHomePhorge

D5502.1775234852.diff
No OneTemporary

Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None

D5502.1775234852.diff

diff --git a/src/app/Policy/Mailfilter.php b/src/app/Policy/Mailfilter.php
--- a/src/app/Policy/Mailfilter.php
+++ b/src/app/Policy/Mailfilter.php
@@ -182,6 +182,7 @@
protected static function getModulesConfig(User $user): array
{
$modules = [
+ Modules\TestModule::class => [],
Modules\ItipModule::class => [],
Modules\ExternalSenderModule::class => [],
];
@@ -194,8 +195,9 @@
// Check if the module is enabled
if (
- (isset($config["{$module}_config"]) && $config["{$module}_config"] === false)
- || (!isset($config["{$module}_config"]) && empty($config["{$module}_policy"]))
+ $module != 'test' // Always enable the test module
+ && ((isset($config["{$module}_config"]) && $config["{$module}_config"] === false)
+ || (!isset($config["{$module}_config"]) && empty($config["{$module}_policy"])))
) {
unset($modules[$class]);
continue;
@@ -209,9 +211,6 @@
}
}
- // Always enable the test module
- $modules[Modules\TestModule::class] = [];
-
return $modules;
}
}
diff --git a/src/app/Policy/Mailfilter/MailParser.php b/src/app/Policy/Mailfilter/MailParser.php
--- a/src/app/Policy/Mailfilter/MailParser.php
+++ b/src/app/Policy/Mailfilter/MailParser.php
@@ -433,6 +433,11 @@
}
}
+ // Throw if the content is empty
+ if ($this->start == 0 && $position == 0) {
+ throw new \Exception('MailParser: Empty content');
+ }
+
$this->addHeader($header);
$this->bodyPosition = $position;
}
diff --git a/src/app/Policy/Mailfilter/Modules/TestModule.php b/src/app/Policy/Mailfilter/Modules/TestModule.php
--- a/src/app/Policy/Mailfilter/Modules/TestModule.php
+++ b/src/app/Policy/Mailfilter/Modules/TestModule.php
@@ -14,17 +14,27 @@
public function handle(MailParser $parser): ?Result
{
$subject = $parser->getHeader('subject');
- if (str_starts_with($subject, "KOLABv4TestMessage")) {
+
+ if (str_starts_with($subject, 'KOLABv4TestMessage')) {
$parser->debug("Received a test message: {$subject}");
- if (str_contains($subject, "DUMP")) {
+
+ if (str_contains($subject, 'DUMP')) {
$str = $parser->dumpStream();
$str = str_replace("\r", "CR", $str);
$str = str_replace("\n", "LF\n", $str);
$parser->debug($str);
}
- if (str_contains($subject, "MODIFYSUBJECT")) {
- $parser->setHeader('Subject', $subject . " MODIFIED");
+ if (str_contains($subject, 'REJECT')) {
+ return new Result(Result::STATUS_REJECT);
+ }
+
+ if (str_contains($subject, 'DISCARD')) {
+ return new Result(Result::STATUS_DISCARD);
+ }
+
+ if (str_contains($subject, 'MODIFYSUBJECT')) {
+ $parser->setHeader('Subject', $subject . ' MODIFIED');
}
}
diff --git a/src/tests/Feature/Policy/MailfilterTest.php b/src/tests/Feature/Policy/MailfilterTest.php
--- a/src/tests/Feature/Policy/MailfilterTest.php
+++ b/src/tests/Feature/Policy/MailfilterTest.php
@@ -48,28 +48,24 @@
$mail = file_get_contents(self::BASE_DIR . '/data/mail/1.eml');
$mail = str_replace("\n", "\r\n", $mail);
- // Test unknown recipient
- $get = ['recipient' => 'unknown@domain.tld', 'sender' => 'jack@kolab.org'];
- $request = new Request($get, [], [], [], [], [], $mail);
- $response = Mailfilter::handle($request);
-
- $this->assertSame(200, $response->status());
- $this->assertSame(Mailfilter::HEADER_ACTION_ACCEPT_EMPTY, $response->headers->get(Mailfilter::HEADER));
- $this->assertSame('', $response->content());
-
$john = $this->getTestUser('john@kolab.org');
- // No modules enabled, no changes to the mail content
- $get = ['recipient' => $john->email, 'sender' => 'jack@kolab.org'];
- $request = new Request($get, [], [], [], [], [], $mail);
- $response = Mailfilter::handle($request);
+ // Note: We use the HTTP controller here for easier use of Laravel request/response assertions
+ $this->useServicesUrl();
- $this->assertSame(200, $response->status());
- $this->assertSame(Mailfilter::HEADER_ACTION_ACCEPT_EMPTY, $response->headers->get(Mailfilter::HEADER));
- $this->assertSame('', $response->content());
+ // Test unknown recipient
+ $url = '/api/webhooks/policy/mail/filter?recipient=unknown@domain.tld&sender=jack@kolab.org';
+ $this->call('POST', $url, [], [], [], [], $mail)
+ ->assertStatus(200)
+ ->assertHeader(Mailfilter::HEADER, Mailfilter::HEADER_ACTION_ACCEPT_EMPTY)
+ ->assertContent('');
- // Note: We using HTTP controller here for easier use of Laravel request/response
- $this->useServicesUrl();
+ // No modules enabled, no changes to the mail content
+ $url = '/api/webhooks/policy/mail/filter?recipient=john@kolab.org&sender=jack@kolab.org';
+ $this->call('POST', $url, [], [], [], [], $mail)
+ ->assertStatus(200)
+ ->assertHeader(Mailfilter::HEADER, Mailfilter::HEADER_ACTION_ACCEPT_EMPTY)
+ ->assertContent('');
// Test returning (modified) mail content
$john->setConfig(['externalsender_policy' => true]);
@@ -77,6 +73,7 @@
$content = $this->call('POST', $url, [], [], [], [], $mail)
->assertStatus(200)
->assertHeader('Content-Type', 'message/rfc822')
+ ->assertHeader(Mailfilter::HEADER, Mailfilter::HEADER_ACTION_ACCEPT)
->streamedContent();
$this->assertStringContainsString('Subject: [EXTERNAL] test sync', $content);
@@ -87,14 +84,39 @@
$content = $this->call('POST', $url, ['file' => $file], [], [], [])
->assertStatus(200)
->assertHeader('Content-Type', 'message/rfc822')
+ ->assertHeader(Mailfilter::HEADER, Mailfilter::HEADER_ACTION_ACCEPT)
->streamedContent();
$this->assertStringContainsString('Subject: [EXTERNAL] test sync', $content);
$this->assertStringContainsString('ZWVlYQ==', $content);
- // TODO: Test rejecting mail
- // TODO: Test two modules that both modify the mail content
- $this->markTestIncomplete();
+ // Test request with no file attached and no content
+ $this->call('POST', $url, [], [], [], [])->assertStatus(500);
+
+ // Test two modules that both modify the mail content
+ $mail = str_replace('test sync', 'KOLABv4TestMessage MODIFYSUBJECT', $mail);
+ $content = $this->call('POST', $url, [], [], [], [], $mail)
+ ->assertStatus(200)
+ ->assertHeader('Content-Type', 'message/rfc822')
+ ->assertHeader(Mailfilter::HEADER, Mailfilter::HEADER_ACTION_ACCEPT)
+ ->streamedContent();
+
+ $this->assertStringContainsString('Subject: [EXTERNAL] KOLABv4TestMessage MODIFYSUBJECT MODIFIED', $content);
+ $this->assertStringContainsString('ZWVlYQ==', $content);
+
+ // Test rejecting mail
+ $mail = str_replace('KOLABv4TestMessage MODIFYSUBJECT', 'KOLABv4TestMessage REJECT', $mail);
+ $this->call('POST', $url, [], [], [], [], $mail)
+ ->assertStatus(200)
+ ->assertHeader(Mailfilter::HEADER, Mailfilter::HEADER_ACTION_REJECT)
+ ->assertContent('');
+
+ // Test discarding mail
+ $mail = str_replace('REJECT', 'DISCARD', $mail);
+ $this->call('POST', $url, [], [], [], [], $mail)
+ ->assertStatus(200)
+ ->assertHeader(Mailfilter::HEADER, Mailfilter::HEADER_ACTION_DISCARD)
+ ->assertContent('');
}
/**
@@ -117,6 +139,7 @@
// Enable account policies
$john->setConfig(['externalsender_policy' => true, 'itip_policy' => true]);
$expected = [
+ TestModule::class => [],
ItipModule::class => [
'itip_config' => null,
'itip_policy' => true,
@@ -126,7 +149,6 @@
'externalsender_policy' => true,
'externalsender_policy_domains' => [],
],
- TestModule::class => [],
];
$this->assertSame($expected, $this->invokeMethod($filter, 'getModulesConfig', [$john]));

File Metadata

Mime Type
text/plain
Expires
Fri, Apr 3, 4:47 PM (13 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18824875
Default Alt Text
D5502.1775234852.diff (8 KB)

Event Timeline