CVE-2022-24838 (published 2022-04-11T21:15Z)

Nextcloud Calendar is a calendar application for the nextcloud framework. SMTP Command Injection in Appointment Emails via Newlines: as newlines and special characters are not sanitized in the email value in the JSON request, a malicious attacker can inject newlines to break out of the `RCPT TO:<BOOKING USER'S EMAIL> ` SMTP command and begin injecting arbitrary SMTP commands. It is recommended that Calendar is upgraded to 3.2.2. There are no workaround available.

Severity: HIGH
CVSS v2 base score: 7.5 (AV:N/AC:L/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-74
cwe_name: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
description: The product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component.
extended_description: Software or other automated logic has certain assumptions about what constitutes data and control respectively. It is the lack of verification of these assumptions for user-controlled input that leads to injection problems. Injection problems encompass a wide variety of issues -- all mitigated in very different ways and usually attempted in order to alter the control flow of the process. For this reason, the most effective way to discuss these weaknesses is to note the distinct features that classify them as injection weaknesses. The most important issue to note is that all injection problems share one thing in common -- i.e., they allow for the injection of control plane data into the user-controlled data plane. This means that the execution of the process may be altered by sending code in through legitimate data channels, using no other mechanism. While buffer overflows, and many other flaws, involve the use of some further issue to gain execution, injection problems need only for the data to be parsed.
         

Fix information:
repo_url: https://github.com/nextcloud/calendar
commit_details: repo_url: https://github.com/nextcloud/calendar
author: Anna Larch
author_date: 2022-03-15 16:44:14+01:00
msg: Add email validation and testing for booking

Signed-off-by: Anna Larch <anna@nextcloud.com>
repository: repo_url: https://github.com/nextcloud/calendar
repo_name: nextcloud/calendar
description: 📆 Calendar app for Nextcloud
repo_language: JavaScript
owner: nextcloud
file_changes: filename: BookingController.php
change_type: ModificationType.MODIFY
diff: @@ -43,6 +43,7 @@ use OCP\AppFramework\Services\IInitialState;
 use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\DB\Exception;
 use OCP\IRequest;
+use OCP\Mail\IMailer;
 use Psr\Log\LoggerInterface;
 
 class BookingController extends Controller {
@@ -65,14 +66,18 @@ class BookingController extends Controller {
 	/** @var LoggerInterface */
 	private $logger;
 
-	public function __construct(string                   $appName,
-								IRequest                 $request,
-								ITimeFactory             $timeFactory,
-								IInitialState            $initialState,
-								BookingService           $bookingService,
+	/** @var IMailer */
+	private $mailer;
+
+	public function __construct(string $appName,
+								IRequest $request,
+								ITimeFactory $timeFactory,
+								IInitialState $initialState,
+								BookingService $bookingService,
 								AppointmentConfigService $appointmentConfigService,
-								URLGenerator             $urlGenerator,
-								LoggerInterface $logger) {
+								URLGenerator $urlGenerator,
+								LoggerInterface $logger,
+								IMailer $mailer) {
 		parent::__construct($appName, $request);
 
 		$this->bookingService = $bookingService;
@@ -81,6 +86,7 @@ class BookingController extends Controller {
 		$this->initialState = $initialState;
 		$this->urlGenerator = $urlGenerator;
 		$this->logger = $logger;
+		$this->mailer = $mailer;
 	}
 
 	/**
@@ -154,13 +160,17 @@ class BookingController extends Controller {
 	 * @param string $timeZone
 	 * @return JsonResponse
 	 */
-	public function bookSlot(int    $appointmentConfigId,
-							 int    $start,
-							 int    $end,
+	public function bookSlot(int $appointmentConfigId,
+							 int $start,
+							 int $end,
 							 string $displayName,
 							 string $email,
 							 string $description,
 							 string $timeZone): JsonResponse {
+		if (!$this->mailer->validateMailAddress($email)) {
+			return JsonResponse::fail('Invalid email address', Http::STATUS_UNPROCESSABLE_ENTITY);
+		}
+
 		if ($start > $end) {
 			return JsonResponse::fail('Invalid time range', Http::STATUS_UNPROCESSABLE_ENTITY);
 		}

programming_language: PHP
filename: BookingControllerTest.php
change_type: ModificationType.MODIFY
diff: @@ -27,8 +27,12 @@ namespace OCA\Calendar\Controller;
 use ChristophWurst\Nextcloud\Testing\TestCase;
 use DateTimeZone;
 use Exception;
+use InvalidArgumentException;
 use OC\URLGenerator;
 use OCA\Calendar\Db\AppointmentConfig;
+use OCA\Calendar\Db\Booking;
+use OCA\Calendar\Exception\NoSlotFoundException;
+use OCA\Calendar\Exception\ServiceException;
 use OCA\Calendar\Service\Appointments\AppointmentConfigService;
 use OCA\Calendar\Service\Appointments\BookingService;
 use OCP\AppFramework\Services\IInitialState;
@@ -38,6 +42,7 @@ use OCP\Contacts\IManager;
 use OCP\IInitialStateService;
 use OCP\IRequest;
 use OCP\IUser;
+use OCP\Mail\IMailer;
 use PHPUnit\Framework\MockObject\MockObject;
 use Psr\Log\LoggerInterface;
 use Safe\DateTimeImmutable;
@@ -80,6 +85,9 @@ class BookingControllerTest extends TestCase {
 	/** @var mixed|MockObject|LoggerInterface */
 	private $logger;
 
+	/** @var IMailer|MockObject */
+	private $mailer;
+
 	protected function setUp():void {
 		parent::setUp();
 
@@ -95,6 +103,7 @@ class BookingControllerTest extends TestCase {
 		$this->apptService = $this->createMock(AppointmentConfigService::class);
 		$this->urlGenerator = $this->createMock(URLGenerator::class);
 		$this->logger = $this->createMock(LoggerInterface::class);
+		$this->mailer = $this->createMock(IMailer::class);
 		$this->controller = new BookingController(
 			$this->appName,
 			$this->request,
@@ -103,7 +112,8 @@ class BookingControllerTest extends TestCase {
 			$this->bookingService,
 			$this->apptService,
 			$this->urlGenerator,
-			$this->logger
+			$this->logger,
+			$this->mailer
 		);
 	}
 
@@ -171,4 +181,112 @@ class BookingControllerTest extends TestCase {
 
 		$this->controller->getBookableSlots($apptConfg->getId(), $start,'Europe/Berlin');
 	}
+
+	public function testBook(): void {
+		$email = 'penny@stardewvalley.edu';
+		$config = new AppointmentConfig();
+
+		$this->mailer->expects(self::once())
+			->method('validateMailAddress')
+			->with($email)
+			->willReturn(true);
+		$this->apptService->expects(self::once())
+			->method('findById')
+			->willReturn($config);
+		$this->bookingService->expects(self::once())
+			->method('book')
+			->with($config, 1, 1, 'Hook/Neverland', 'Test', $email, 'Test')
+			->willReturn(new Booking());
+
+		$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Hook/Neverland');
+	}
+
+
+	public function testBookInvalidTimeZone(): void {
+		$email = 'penny@stardewvalley.edu';
+		$config = new AppointmentConfig();
+
+		$this->mailer->expects(self::once())
+			->method('validateMailAddress')
+			->with($email)
+			->willReturn(true);
+		$this->apptService->expects(self::once())
+			->method('findById')
+			->willReturn($config);
+		$this->bookingService->expects(self::once())
+			->method('book')
+			->with($config, 1, 1, 'Hook/Neverland', 'Test', $email, 'Test')
+			->willThrowException(new InvalidArgumentException());
+
+		$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Hook/Neverland');
+	}
+
+	public function testBookInvalidSlot(): void {
+		$email = 'penny@stardewvalley.edu';
+		$config = new AppointmentConfig();
+
+		$this->mailer->expects(self::once())
+			->method('validateMailAddress')
+			->with($email)
+			->willReturn(true);
+		$this->apptService->expects(self::once())
+			->method('findById')
+			->willReturn($config);
+		$this->bookingService->expects(self::once())
+			->method('book')
+			->with($config, 1, 1, 'Europe/Berlin', 'Test', $email, 'Test')
+			->willThrowException(new NoSlotFoundException());
+
+		$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
+	}
+
+	public function testBookInvalidBooking(): void {
+		$email = 'penny@stardewvalley.edu';
+		$config = new AppointmentConfig();
+
+		$this->mailer->expects(self::once())
+			->method('validateMailAddress')
+			->with($email)
+			->willReturn(true);
+		$this->apptService->expects(self::once())
+			->method('findById')
+			->willReturn($config);
+		$this->bookingService->expects(self::once())
+			->method('book')
+			->with($config, 1, 1, 'Europe/Berlin', 'Test', $email, 'Test')
+			->willThrowException(new ServiceException());
+
+		$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
+	}
+
+	public function testBookInvalidId(): void {
+		$email = 'penny@stardewvalley.edu';
+		$this->mailer->expects(self::once())
+			->method('validateMailAddress')
+			->with($email)
+			->willReturn(true);
+		$this->apptService->expects(self::once())
+			->method('findById')
+			->willThrowException(new ServiceException());
+		$this->bookingService->expects(self::never())
+			->method('book');
+
+		$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
+	}
+
+
+	public function testBookInvalidEmail(): void {
+		$email = 'testing-abcdef';
+
+		$this->mailer->expects(self::once())
+			->method('validateMailAddress')
+			->with($email)
+			->willReturn(false);
+		$this->apptService->expects(self::never())
+			->method('findById');
+		$this->bookingService->expects(self::never())
+			->method('book');
+
+		$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
+	}
 }

programming_language: PHP