The Open Source DocuSign Alternative.
TypeScript digital-signaturedocument-signingdocusign-alternativee-signatureesignesignaturenext-authnextjsopen-sourcepades-standardpdfpdf-signpdf-signaturepostgresqlprismaself-hostedsigningtypescript
fix: preserve base path in email asset URLs (#3327)
Christopher Ryan5603a9e59da2
diff --git a/packages/email/template-components/template-access-auth-2fa.tsx b/packages/email/template-components/template-access-auth-2fa.tsxindex 7873a1735..a7918ad4b 100644--- a/packages/email/template-components/template-access-auth-2fa.tsx+++ b/packages/email/template-components/template-access-auth-2fa.tsx@@ -1,6 +1,7 @@import { Plural, Trans } from '@lingui/react/macro';import { Heading, Img, Section, Text } from '../components';+import { getEmailAssetUrl } from '../utils/asset-url';export type TemplateAccessAuth2FAProps = {documentTitle: string;@@ -18,13 +19,9 @@ export const TemplateAccessAuth2FA = ({expiresInMinutes,assetBaseUrl = 'http://localhost:3002',}: TemplateAccessAuth2FAProps) => {- const getAssetUrl = (path: string) => {- return new URL(path, assetBaseUrl).toString();- };-return (<div>- <Img src={getAssetUrl('/static/document.png')} alt="Document" className="mx-auto h-12 w-12" />+ <Img src={getEmailAssetUrl(assetBaseUrl, 'static/document.png')} alt="Document" className="mx-auto h-12 w-12" /><Section className="mt-8"><Heading className="text-center font-semibold text-foreground text-lg">diff --git a/packages/email/template-components/template-branding-logo.tsx b/packages/email/template-components/template-branding-logo.tsxindex c7e99684e..6b560aed2 100644--- a/packages/email/template-components/template-branding-logo.tsx+++ b/packages/email/template-components/template-branding-logo.tsx@@ -1,5 +1,6 @@import { Img, Link } from '../components';import { useBranding } from '../providers/branding';+import { getEmailAssetUrl } from '../utils/asset-url';import { getSafeBrandingUrl } from '../utils/branding-url';export type TemplateBrandingLogoProps = {@@ -20,7 +21,7 @@ export const TemplateBrandingLogo = ({ assetBaseUrl, className = 'mb-4 h-6' }: Tconst hasCustomBrandingLogo = branding.brandingEnabled && Boolean(branding.brandingLogo);if (!hasCustomBrandingLogo) {- const documensoLogoUrl = new URL('/static/logo.png', assetBaseUrl).toString();+ const documensoLogoUrl = getEmailAssetUrl(assetBaseUrl, 'static/logo.png');return <Img src={documensoLogoUrl} alt="Documenso Logo" className={className} />;}diff --git a/packages/email/template-components/template-document-completed.tsx b/packages/email/template-components/template-document-completed.tsxindex ec453f315..20b7000e1 100644--- a/packages/email/template-components/template-document-completed.tsx+++ b/packages/email/template-components/template-document-completed.tsx@@ -1,6 +1,7 @@import { Trans } from '@lingui/react/macro';import { Button, Column, Img, Section, Text } from '../components';+import { getEmailAssetUrl } from '../utils/asset-url';import { TemplateDocumentImage } from './template-document-image';export interface TemplateDocumentCompletedProps {@@ -16,10 +17,6 @@ export const TemplateDocumentCompleted = ({assetBaseUrl,customBody,}: TemplateDocumentCompletedProps) => {- const getAssetUrl = (path: string) => {- return new URL(path, assetBaseUrl).toString();- };-return (<><TemplateDocumentImage className="mt-6" assetBaseUrl={assetBaseUrl} />@@ -29,7 +26,7 @@ export const TemplateDocumentCompleted = ({<Column align="center"><Text className="font-semibold text-base text-foreground"><Img- src={getAssetUrl('/static/completed.png')}+ src={getEmailAssetUrl(assetBaseUrl, 'static/completed.png')}className="-mt-0.5 mr-2 inline h-7 w-7 align-middle"alt=""/>@@ -51,7 +48,11 @@ export const TemplateDocumentCompleted = ({className="rounded-lg border border-border border-solid px-4 py-2 text-center font-medium text-foreground text-sm no-underline"href={downloadLink}>- <Img src={getAssetUrl('/static/download.png')} className="mr-2 mb-0.5 inline h-5 w-5 align-middle" alt="" />+ <Img+ src={getEmailAssetUrl(assetBaseUrl, 'static/download.png')}+ className="mr-2 mb-0.5 inline h-5 w-5 align-middle"+ alt=""+ /><Trans>Download</Trans></Button></Section>diff --git a/packages/email/template-components/template-document-image.tsx b/packages/email/template-components/template-document-image.tsxindex ed45512a5..88c06282c 100644--- a/packages/email/template-components/template-document-image.tsx+++ b/packages/email/template-components/template-document-image.tsx@@ -1,4 +1,5 @@import { Column, Img, Row, Section } from '../components';+import { getEmailAssetUrl } from '../utils/asset-url';export interface TemplateDocumentImageProps {assetBaseUrl: string;@@ -6,17 +7,13 @@ export interface TemplateDocumentImageProps {}export const TemplateDocumentImage = ({ assetBaseUrl, className }: TemplateDocumentImageProps) => {- const getAssetUrl = (path: string) => {- return new URL(path, assetBaseUrl).toString();- };-return (<Section className={className}><Row className="table-fixed"><Column /><Column>- <Img className="mx-auto h-42" src={getAssetUrl('/static/document.png')} alt="Documenso" />+ <Img className="mx-auto h-42" src={getEmailAssetUrl(assetBaseUrl, 'static/document.png')} alt="Documenso" /></Column><Column />diff --git a/packages/email/template-components/template-document-pending.tsx b/packages/email/template-components/template-document-pending.tsxindex c44ae5c81..5cc435e28 100644--- a/packages/email/template-components/template-document-pending.tsx+++ b/packages/email/template-components/template-document-pending.tsx@@ -1,6 +1,7 @@import { Trans } from '@lingui/react/macro';import { Column, Img, Section, Text } from '../components';+import { getEmailAssetUrl } from '../utils/asset-url';import { TemplateDocumentImage } from './template-document-image';export interface TemplateDocumentPendingProps {@@ -9,10 +10,6 @@ export interface TemplateDocumentPendingProps {}export const TemplateDocumentPending = ({ documentName, assetBaseUrl }: TemplateDocumentPendingProps) => {- const getAssetUrl = (path: string) => {- return new URL(path, assetBaseUrl).toString();- };-return (<><TemplateDocumentImage className="mt-6" assetBaseUrl={assetBaseUrl} />@@ -21,7 +18,11 @@ export const TemplateDocumentPending = ({ documentName, assetBaseUrl }: Template<Section className="mb-4"><Column align="center"><Text className="font-semibold text-base text-foreground">- <Img src={getAssetUrl('/static/clock.png')} className="-mt-0.5 mr-2 inline h-7 w-7 align-middle" alt="" />+ <Img+ src={getEmailAssetUrl(assetBaseUrl, 'static/clock.png')}+ className="-mt-0.5 mr-2 inline h-7 w-7 align-middle"+ alt=""+ /><Trans>Waiting for others</Trans></Text></Column>diff --git a/packages/email/template-components/template-document-recipient-signed.tsx b/packages/email/template-components/template-document-recipient-signed.tsxindex 42ebe0e10..9077091fb 100644--- a/packages/email/template-components/template-document-recipient-signed.tsx+++ b/packages/email/template-components/template-document-recipient-signed.tsx@@ -1,6 +1,7 @@import { Trans } from '@lingui/react/macro';import { Column, Img, Section, Text } from '../components';+import { getEmailAssetUrl } from '../utils/asset-url';import { TemplateDocumentImage } from './template-document-image';export interface TemplateDocumentRecipientSignedProps {@@ -16,10 +17,6 @@ export const TemplateDocumentRecipientSigned = ({recipientEmail,assetBaseUrl,}: TemplateDocumentRecipientSignedProps) => {- const getAssetUrl = (path: string) => {- return new URL(path, assetBaseUrl).toString();- };-const recipientReference = recipientName || recipientEmail;return (@@ -31,7 +28,7 @@ export const TemplateDocumentRecipientSigned = ({<Column align="center"><Text className="font-semibold text-base text-foreground"><Img- src={getAssetUrl('/static/completed.png')}+ src={getEmailAssetUrl(assetBaseUrl, 'static/completed.png')}className="-mt-0.5 mr-2 inline h-7 w-7 align-middle"alt=""/>diff --git a/packages/email/template-components/template-document-self-signed.tsx b/packages/email/template-components/template-document-self-signed.tsxindex 852843577..729a3b156 100644--- a/packages/email/template-components/template-document-self-signed.tsx+++ b/packages/email/template-components/template-document-self-signed.tsx@@ -2,6 +2,7 @@ import { env } from '@documenso/lib/utils/env';import { Trans } from '@lingui/react/macro';import { Button, Column, Img, Link, Section, Text } from '../components';+import { getEmailAssetUrl } from '../utils/asset-url';import { TemplateDocumentImage } from './template-document-image';export interface TemplateDocumentSelfSignedProps {@@ -14,10 +15,6 @@ export const TemplateDocumentSelfSigned = ({ documentName, assetBaseUrl }: Templconst signUpUrl = `${NEXT_PUBLIC_WEBAPP_URL ?? 'http://localhost:3000'}/signup`;- const getAssetUrl = (path: string) => {- return new URL(path, assetBaseUrl).toString();- };-return (<><TemplateDocumentImage className="mt-6" assetBaseUrl={assetBaseUrl} />@@ -27,7 +24,7 @@ export const TemplateDocumentSelfSigned = ({ documentName, assetBaseUrl }: Templ<Column align="center"><Text className="font-semibold text-base text-foreground"><Img- src={getAssetUrl('/static/completed.png')}+ src={getEmailAssetUrl(assetBaseUrl, 'static/completed.png')}className="-mt-0.5 mr-2 inline h-7 w-7 align-middle"alt=""/>@@ -56,7 +53,7 @@ export const TemplateDocumentSelfSigned = ({ documentName, assetBaseUrl }: TemplclassName="mr-4 rounded-lg border border-border border-solid px-4 py-2 text-center font-medium text-foreground text-sm no-underline"><Img- src={getAssetUrl('/static/user-plus.png')}+ src={getEmailAssetUrl(assetBaseUrl, 'static/user-plus.png')}className="mr-2 mb-0.5 inline h-5 w-5 align-middle"alt=""/>@@ -67,7 +64,11 @@ export const TemplateDocumentSelfSigned = ({ documentName, assetBaseUrl }: TemplclassName="rounded-lg border border-border border-solid px-4 py-2 text-center font-medium text-foreground text-sm no-underline"href="https://documenso.com/pricing">- <Img src={getAssetUrl('/static/review.png')} className="mr-2 mb-0.5 inline h-5 w-5 align-middle" alt="" />+ <Img+ src={getEmailAssetUrl(assetBaseUrl, 'static/review.png')}+ className="mr-2 mb-0.5 inline h-5 w-5 align-middle"+ alt=""+ /><Trans>View plans</Trans></Button></Section>diff --git a/packages/email/template-components/template-image.tsx b/packages/email/template-components/template-image.tsxindex b16bcfe60..198c55aec 100644--- a/packages/email/template-components/template-image.tsx+++ b/packages/email/template-components/template-image.tsx@@ -1,4 +1,5 @@import { Img } from '../components';+import { getEmailAssetUrl } from '../utils/asset-url';export interface TemplateImageProps {assetBaseUrl: string;@@ -7,11 +8,7 @@ export interface TemplateImageProps {}export const TemplateImage = ({ assetBaseUrl, className, staticAsset }: TemplateImageProps) => {- const getAssetUrl = (path: string) => {- return new URL(path, assetBaseUrl).toString();- };-- return <Img className={className} src={getAssetUrl(`/static/${staticAsset}`)} alt="" />;+ return <Img className={className} src={getEmailAssetUrl(assetBaseUrl, `static/${staticAsset}`)} alt="" />;};export default TemplateImage;diff --git a/packages/email/templates/admin-user-created.tsx b/packages/email/templates/admin-user-created.tsxindex ccc92020b..000b4d3cb 100644--- a/packages/email/templates/admin-user-created.tsx+++ b/packages/email/templates/admin-user-created.tsx@@ -5,6 +5,7 @@ import { Body, Container, Head, Html, Img, Preview, Section } from '../componentimport type { TemplateAdminUserCreatedProps } from '../template-components/template-admin-user-created';import { TemplateAdminUserCreated } from '../template-components/template-admin-user-created';import { TemplateFooter } from '../template-components/template-footer';+import { getEmailAssetUrl } from '../utils/asset-url';export const AdminUserCreatedTemplate = ({resetPasswordLink,@@ -14,10 +15,6 @@ export const AdminUserCreatedTemplate = ({const previewText = msg`Set your password for Documenso`;- const getAssetUrl = (path: string) => {- return new URL(path, assetBaseUrl).toString();- };-return (<Html><Head />@@ -27,7 +24,7 @@ export const AdminUserCreatedTemplate = ({<Section><Container className="mx-auto mt-8 mb-2 max-w-xl rounded-lg border border-border border-solid p-4 backdrop-blur-sm"><Section>- <Img src={getAssetUrl('/static/logo.png')} alt="Documenso Logo" className="mb-4 h-6" />+ <Img src={getEmailAssetUrl(assetBaseUrl, 'static/logo.png')} alt="Documenso Logo" className="mb-4 h-6" /><TemplateAdminUserCreated resetPasswordLink={resetPasswordLink} assetBaseUrl={assetBaseUrl} /></Section>diff --git a/packages/email/utils/asset-url.ts b/packages/email/utils/asset-url.tsnew file mode 100644index 000000000..f6143de19--- /dev/null+++ b/packages/email/utils/asset-url.ts@@ -0,0 +1,17 @@+/**+ * Resolve a static email asset path against the asset base URL.+ *+ * The base is normalised to end with a trailing slash and the path is+ * normalised to have no leading slash, so a sub-path in the base URL+ * (e.g. "/ESign") is preserved. Passing a root-absolute path straight to+ * `new URL()` would otherwise replace the base pathname entirely.+ *+ * `getEmailAssetUrl('https://host/ESign', 'static/logo.png')` -> `https://host/ESign/static/logo.png`+ * `getEmailAssetUrl('https://host/ESign/', '/static/logo.png')` -> `https://host/ESign/static/logo.png`+ */+export const getEmailAssetUrl = (assetBaseUrl: string, path: string): string => {+ const base = assetBaseUrl.endsWith('/') ? assetBaseUrl : `${assetBaseUrl}/`;+ const relativePath = path.startsWith('/') ? path.slice(1) : path;++ return new URL(relativePath, base).toString();+};