Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/app/client-preview/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Handlebars from 'handlebars'
import { IClient, ICustomField, ISettings } from '@/types/interfaces'
import ClientPreview from '../components/ClientPreview'
import { apiUrl } from '@/config'
import { IClient, ICustomField, ISettings } from '@/types/interfaces'
import { defaultBannerImagePath, defaultBgColor } from '@/utils/constants'
import { CopilotAPI } from '@/utils/copilotApiUtils'
import { prepareCustomLabel } from '@/utils/customLabels'
import { getPreviewMode } from '@/utils/previewMode'
import { safeCompile } from '@/utils/safeCompile'
import { preprocessTemplate } from '@/utils/string'
import Image from 'next/image'
import { z } from 'zod'
import { CopilotAPI } from '@/utils/copilotApiUtils'
import InvalidToken from '../components/InvalidToken'
import { defaultState } from '../../../defaultState'
import { defaultBannerImagePath, defaultBgColor } from '@/utils/constants'
import { getPreviewMode } from '@/utils/previewMode'
import ClientPreview from '../components/ClientPreview'
import InvalidToken from '../components/InvalidToken'
import { NoPreviewSupport } from './NoPreviewSupport'
import { preprocessTemplate } from '@/utils/string'
import { prepareCustomLabel } from '@/utils/customLabels'

export const revalidate = 0

Expand Down Expand Up @@ -128,7 +128,7 @@ export default async function ClientPreviewPage({
}
}

const template = Handlebars?.compile(
const template = safeCompile(
preprocessTemplate(
prepareCustomLabel(settings?.content, workspace.labels, {
isClientMode: true,
Expand Down
3 changes: 2 additions & 1 deletion src/app/components/EditorInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import TableRow from '@tiptap/extension-table-row'
import Text from '@tiptap/extension-text'
import Underline from '@tiptap/extension-underline'
import Handlebars from 'handlebars'

Check warning on line 35 in src/app/components/EditorInterface.tsx

View workflow job for this annotation

GitHub Actions / Run linters

'Handlebars' is defined but never used

Check warning on line 35 in src/app/components/EditorInterface.tsx

View workflow job for this annotation

GitHub Actions / Run linters

'Handlebars' is defined but never used
import { Scrollbars } from 'react-custom-scrollbars'
import { Toaster } from 'react-hot-toast'

Expand Down Expand Up @@ -63,6 +63,7 @@
import Image from 'next/image'
import { defaultState } from '../../../defaultState'
import { preprocessTemplate } from '@/utils/string'
import { safeCompile } from '@/utils/safeCompile'

interface IEditorInterface {
settings: ISettings | null
Expand Down Expand Up @@ -185,7 +186,7 @@

useEffect(() => {
if (appState?.appState.readOnly) {
const template = Handlebars?.compile(
const template = safeCompile(
preprocessTemplate(
prepareCustomLabel(
replaceCustomLabelsWithPlaceholders(
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useAppData.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createContext, PropsWithChildren, useContext, useMemo } from 'react'
import { useAppState } from '@/hooks/useAppState'
import { IClient, INotification } from '@/types/interfaces'
import Handlebars from 'handlebars'
import { CustomField, CustomFieldsSchema } from '@/types/common'
import { CustomField } from '@/types/common'
import { safeCompile } from '@/utils/safeCompile'

const AppDataContext = createContext<null | Record<string, unknown>>({})

Expand All @@ -17,7 +17,7 @@ export const useAppData = (template: string) => {
return template
}

return Handlebars?.compile(template || '')(appData)
return safeCompile(template || '')(appData)
}

export const AppDataProvider = ({ children }: PropsWithChildren) => {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/safeCompile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Handlebars, { TemplateDelegate } from 'handlebars'

/**
* Safely compiles a Handlebars template.
* Invalid/malformed placeholders like {{client.}} are removed.
* Missing properties resolve to empty string.
*/
export const safeCompile = (templateSource: string): TemplateDelegate => {
const sanitizedTemplate = templateSource.replace(/{{\s*[\w]+\.\s*}}/g, '') // Remove placeholders ending with a dot

Handlebars.registerHelper('helperMissing', () => '')

return Handlebars?.compile(sanitizedTemplate, { strict: false })
}
Loading