fix: ensure proper slashes in the email url

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-04-30 13:24:20 +03:00
parent e538b10043
commit 79bb100f05
2 changed files with 33 additions and 1 deletions
@@ -383,6 +383,32 @@ describe('NotificationsEmailProcessor', () => {
text: 'https://example.org/catalog/user/default/john.doe',
to: 'mock@backstage.io',
});
await processor.postProcess(
{
origin: 'plugin',
id: '1234',
user: 'user:default/mock',
created: new Date(),
payload: {
title: 'notification',
link: '/catalog/user/default/jane.doe',
},
},
{
recipients: { type: 'entity', entityRef: 'user:default/mock' },
payload: { title: 'notification' },
},
);
expect(sendmailMock).toHaveBeenCalledWith({
from: 'backstage@backstage.io',
html: '<p><a href="https://example.org/catalog/user/default/jane.doe">https://example.org/catalog/user/default/jane.doe</a></p>',
replyTo: undefined,
subject: 'notification',
text: 'https://example.org/catalog/user/default/jane.doe',
to: 'mock@backstage.io',
});
});
it('should send email with absolute link to given address', async () => {
@@ -219,8 +219,14 @@ export class NotificationsEmailProcessor implements NotificationProcessor {
private getNotificationLink(notification: Notification) {
if (notification.payload.link) {
const stripLeadingSlash = (s: string) => s.replace(/^\//, '');
const ensureTrailingSlash = (s: string) => s.replace(/\/?$/, '/');
try {
const url = new URL(notification.payload.link, this.frontendBaseUrl);
const url = new URL(
stripLeadingSlash(notification.payload.link),
ensureTrailingSlash(this.frontendBaseUrl),
);
return url.toString();
} catch (_e) {
// noop: fallback to relative URL