Skip to content

Commit b3eedb5

Browse files
authored
Merge pull request #32479 from nextcloud/backport/32428/stable19
[stable19] Add Email validation
2 parents df5f3b9 + 3859c3b commit b3eedb5

File tree

2 files changed

+94
-166
lines changed

2 files changed

+94
-166
lines changed

apps/dav/lib/CalDAV/Reminder/NotificationProvider/EmailProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ private function getAllEMailAddressesFromEvent(VEvent $vevent):array {
272272
$emailAddressesOfDelegates = $delegates->getParts();
273273
foreach ($emailAddressesOfDelegates as $addressesOfDelegate) {
274274
if (strcasecmp($addressesOfDelegate, 'mailto:') === 0) {
275-
$emailAddresses[substr($addressesOfDelegate, 7)] = [];
275+
$delegateEmail = substr($addressesOfDelegate, 7);
276+
if ($delegateEmail !== false && $this->mailer->validateMailAddress($delegateEmail)) {
277+
$emailAddresses[$delegateEmail] = [];
278+
}
276279
}
277280
}
278281

@@ -344,8 +347,12 @@ private function getEMailAddressOfAttendee(VObject\Property $attendee):?string {
344347
if (!$this->hasAttendeeMailURI($attendee)) {
345348
return null;
346349
}
350+
$attendeeEMail = substr($attendee->getValue(), 7);
351+
if ($attendeeEMail === false || !$this->mailer->validateMailAddress($attendeeEMail)) {
352+
return null;
353+
}
347354

348-
return substr($attendee->getValue(), 7);
355+
return $attendeeEMail;
349356
}
350357

351358
/**

apps/dav/tests/unit/CalDAV/Reminder/NotificationProvider/EmailProviderTest.php

Lines changed: 85 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* @author Christoph Wurst <[email protected]>
1010
* @author Georg Ehrke <[email protected]>
11+
* @author Joas Schilling <[email protected]>
1112
* @author Roeland Jago Douma <[email protected]>
1213
* @author Thomas Citharel <[email protected]>
1314
*
@@ -40,27 +41,28 @@
4041
use OCP\Mail\IEMailTemplate;
4142
use OCP\Mail\IMailer;
4243
use OCP\Mail\IMessage;
44+
use PHPUnit\Framework\MockObject\MockObject;
4345
use Sabre\VObject\Component\VCalendar;
4446

4547
class EmailProviderTest extends AbstractNotificationProviderTest {
4648
public const USER_EMAIL = '[email protected]';
4749

48-
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
50+
/** @var ILogger|MockObject */
4951
protected $logger;
5052

51-
/** @var L10NFactory|\PHPUnit\Framework\MockObject\MockObject */
53+
/** @var L10NFactory|MockObject */
5254
protected $l10nFactory;
5355

54-
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
56+
/** @var IL10N|MockObject */
5557
protected $l10n;
5658

57-
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
59+
/** @var IURLGenerator|MockObject */
5860
protected $urlGenerator;
5961

60-
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
62+
/** @var IConfig|MockObject */
6163
protected $config;
6264

63-
/** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
65+
/** @var IMailer|MockObject */
6466
private $mailer;
6567

6668
protected function setUp(): void {
@@ -78,22 +80,7 @@ protected function setUp(): void {
7880
}
7981

8082
public function testSendWithoutAttendees():void {
81-
$this->config->expects($this->at(0))
82-
->method('getUserValue')
83-
->with('uid1', 'core', 'lang', null)
84-
->willReturn(null);
85-
$this->config->expects($this->at(1))
86-
->method('getUserValue')
87-
->with('uid2', 'core', 'lang', null)
88-
->willReturn('de');
89-
$this->config->expects($this->at(2))
90-
->method('getUserValue')
91-
->with('uid3', 'core', 'lang', null)
92-
->willReturn('de');
93-
$this->config->expects($this->at(3))
94-
->method('getUserValue')
95-
->with('uid5', 'core', 'lang', null)
96-
->willReturn('de');
83+
list($user1, $user2, $user3, , $user5) = $users = $this->getUsers();
9784

9885
$enL10N = $this->createMock(IL10N::class);
9986
$enL10N->method('t')
@@ -107,30 +94,32 @@ public function testSendWithoutAttendees():void {
10794
$deL10N->method('l')
10895
->willReturnArgument(0);
10996

110-
$this->l10nFactory->expects($this->at(0))
97+
$this->l10nFactory
98+
->method('getUserLanguage')
99+
->willReturnMap([
100+
[$user1, 'en'],
101+
[$user2, 'de'],
102+
[$user3, 'de'],
103+
[$user5, 'de'],
104+
]);
105+
106+
$this->l10nFactory
111107
->method('findLanguage')
112-
->with()
113108
->willReturn('en');
114109

115-
$this->l10nFactory->expects($this->at(1))
110+
$this->l10nFactory
116111
->method('languageExists')
117-
->with('dav', 'en')
118-
->willReturn(true);
112+
->willReturnMap([
113+
['dav', 'en', true],
114+
['dav', 'de', true],
115+
]);
119116

120-
$this->l10nFactory->expects($this->at(2))
117+
$this->l10nFactory
121118
->method('get')
122-
->with('dav', 'en')
123-
->willReturn($enL10N);
124-
125-
$this->l10nFactory->expects($this->at(3))
126-
->method('languageExists')
127-
->with('dav', 'de')
128-
->willReturn(true);
129-
130-
$this->l10nFactory->expects($this->at(4))
131-
->method('get')
132-
->with('dav', 'de')
133-
->willReturn($deL10N);
119+
->willReturnMap([
120+
['dav', 'en', null, $enL10N],
121+
['dav', 'de', null, $deL10N],
122+
]);
134123

135124
$template1 = $this->getTemplateMock();
136125
$message11 = $this->getMessageMock('[email protected]', $template1);
@@ -197,26 +186,11 @@ public function testSendWithoutAttendees():void {
197186
$this->setupURLGeneratorMock(2);
198187

199188
$vcalendar = $this->getNoAttendeeVCalendar();
200-
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $this->getUsers());
189+
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
201190
}
202191

203192
public function testSendWithAttendees(): void {
204-
$this->config->expects($this->at(0))
205-
->method('getUserValue')
206-
->with('uid1', 'core', 'lang', null)
207-
->willReturn(null);
208-
$this->config->expects($this->at(1))
209-
->method('getUserValue')
210-
->with('uid2', 'core', 'lang', null)
211-
->willReturn('de');
212-
$this->config->expects($this->at(2))
213-
->method('getUserValue')
214-
->with('uid3', 'core', 'lang', null)
215-
->willReturn('de');
216-
$this->config->expects($this->at(3))
217-
->method('getUserValue')
218-
->with('uid5', 'core', 'lang', null)
219-
->willReturn('de');
193+
list($user1, $user2, $user3, , $user5) = $users = $this->getUsers();
220194

221195
$enL10N = $this->createMock(IL10N::class);
222196
$enL10N->method('t')
@@ -230,138 +204,85 @@ public function testSendWithAttendees(): void {
230204
$deL10N->method('l')
231205
->willReturnArgument(0);
232206

233-
$this->l10nFactory->expects($this->at(0))
207+
$this->l10nFactory
208+
->method('getUserLanguage')
209+
->willReturnMap([
210+
[$user1, 'en'],
211+
[$user2, 'de'],
212+
[$user3, 'de'],
213+
[$user5, 'de'],
214+
]);
215+
216+
$this->l10nFactory
234217
->method('findLanguage')
235-
->with()
236218
->willReturn('en');
237219

238-
$this->l10nFactory->expects($this->at(1))
220+
$this->l10nFactory
239221
->method('languageExists')
240-
->with('dav', 'de')
241-
->willReturn(true);
222+
->willReturnMap([
223+
['dav', 'en', true],
224+
['dav', 'de', true],
225+
]);
242226

243-
$this->l10nFactory->expects($this->at(2))
227+
$this->l10nFactory
244228
->method('get')
245-
->with('dav', 'de')
246-
->willReturn($enL10N);
247-
248-
$this->l10nFactory->expects($this->at(3))
249-
->method('languageExists')
250-
->with('dav', 'en')
251-
->willReturn(true);
229+
->willReturnMap([
230+
['dav', 'en', null, $enL10N],
231+
['dav', 'de', null, $deL10N],
232+
]);
252233

253-
$this->l10nFactory->expects($this->at(4))
254-
->method('get')
255-
->with('dav', 'en')
256-
->willReturn($deL10N);
257-
258-
// German
259234
$template1 = $this->getTemplateMock();
260235
$message11 = $this->getMessageMock('[email protected]', $template1);
261236
$message12 = $this->getMessageMock('[email protected]', $template1);
262237
$message13 = $this->getMessageMock('[email protected]', $template1);
263-
// English
264238
$template2 = $this->getTemplateMock();
265239
$message21 = $this->getMessageMock('[email protected]', $template2);
266240
$message22 = $this->getMessageMock('[email protected]', $template2);
267241
$message23 = $this->getMessageMock('[email protected]', $template2);
268242

269-
$this->mailer->expects($this->at(0))
270-
->method('createEMailTemplate')
271-
->with('dav.calendarReminder')
272-
->willReturn($template1);
273-
274-
$this->mailer->expects($this->at(1))
275-
->method('validateMailAddress')
276-
277-
->willReturn(true);
278-
279-
$this->mailer->expects($this->at(2))
280-
->method('createMessage')
281-
->with()
282-
->willReturn($message11);
283-
$this->mailer->expects($this->at(3))
284-
->method('send')
285-
->with($message11)
286-
->willReturn([]);
287-
$this->mailer->expects($this->at(4))
288-
->method('validateMailAddress')
289-
290-
->willReturn(true);
291-
292-
$this->mailer->expects($this->at(5))
293-
->method('createMessage')
294-
->with()
295-
->willReturn($message12);
296-
$this->mailer->expects($this->at(6))
297-
->method('send')
298-
->with($message12)
299-
->willReturn([]);
300-
$this->mailer->expects($this->at(7))
301-
->method('validateMailAddress')
302-
303-
->willReturn(true);
304-
$this->mailer->expects($this->at(8))
305-
->method('createMessage')
306-
->with()
307-
->willReturn($message13);
308-
$this->mailer->expects($this->at(9))
309-
->method('send')
310-
->with($message13)
311-
->willReturn([]);
312-
$this->mailer->expects($this->at(10))
313-
->method('validateMailAddress')
314-
->with('invalid')
315-
->willReturn(false);
316-
317-
$this->mailer->expects($this->at(11))
243+
$this->mailer->expects(self::exactly(2))
318244
->method('createEMailTemplate')
319245
->with('dav.calendarReminder')
320-
->willReturn($template2);
321-
322-
$this->mailer->expects($this->at(12))
246+
->willReturnOnConsecutiveCalls(
247+
$template1,
248+
$template2
249+
);
250+
$this->mailer->expects($this->atLeastOnce())
323251
->method('validateMailAddress')
324-
325-
->willReturn(true);
326-
$this->mailer->expects($this->at(13))
252+
->willReturnMap([
253+
['[email protected]', true],
254+
['[email protected]', true],
255+
['[email protected]', true],
256+
['[email protected]', true],
257+
['[email protected]', true],
258+
['[email protected]', true],
259+
['invalid', false]
260+
]);
261+
$this->mailer->expects($this->exactly(6))
327262
->method('createMessage')
328263
->with()
329-
->willReturn($message21);
330-
$this->mailer->expects($this->at(14))
264+
->willReturnOnConsecutiveCalls(
265+
$message11,
266+
$message12,
267+
$message13,
268+
$message21,
269+
$message22,
270+
$message23
271+
);
272+
$this->mailer->expects($this->exactly(6))
331273
->method('send')
332-
->with($message21)
333-
->willReturn([]);
334-
335-
$this->mailer->expects($this->at(15))
336-
->method('validateMailAddress')
337-
338-
->willReturn(true);
339-
$this->mailer->expects($this->at(16))
340-
->method('createMessage')
341-
->with()
342-
->willReturn($message22);
343-
$this->mailer->expects($this->at(17))
344-
->method('send')
345-
->with($message22)
346-
->willReturn([]);
347-
348-
$this->mailer->expects($this->at(18))
349-
->method('validateMailAddress')
350-
351-
->willReturn(true);
352-
$this->mailer->expects($this->at(19))
353-
->method('createMessage')
354-
->with()
355-
->willReturn($message23);
356-
$this->mailer->expects($this->at(20))
357-
->method('send')
358-
->with($message23)
359-
->willReturn([]);
360-
274+
->withConsecutive(
275+
[$message11],
276+
[$message12],
277+
[$message13],
278+
[$message21],
279+
[$message22],
280+
[$message23]
281+
)->willReturn([]);
361282
$this->setupURLGeneratorMock(2);
362283

363284
$vcalendar = $this->getAttendeeVCalendar();
364-
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $this->getUsers());
285+
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
365286
}
366287

367288
/**

0 commit comments

Comments
 (0)