Skip to content

Commit 436245d

Browse files
committed
fix(confluence): skip commented records and bare base64 in Optics script data, retry on a missing storage body
1 parent ebdc014 commit 436245d

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

‎apps/sim/connectors/confluence/app-macros.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ const OBJECT_PROPERTY_PATTERN =
2525

2626
/** Values that are encoded payloads (inline images, fonts), not readable data. */
2727
const MAX_UNBROKEN_VALUE_LENGTH = 200
28+
const BASE64_PAYLOAD_PATTERN = /^[A-Za-z0-9+/]{20,}={0,2}$/
29+
30+
/**
31+
* A string literal (kept, so a `//` inside `'https://…'` survives) or a comment
32+
* (dropped, so commented-out records never read as page data).
33+
*/
34+
const STRING_OR_COMMENT_PATTERN =
35+
/("(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'|`(?:[^`\\]|\\.)*`)|\/\/[^\r\n]*|\/\*[\s\S]*?\*\//g
2836

2937
/**
3038
* Whether a rendered `view` body may hold an Optics HTML macro, whose text only
@@ -122,7 +130,8 @@ function markupToText(markup: string): string {
122130
/** Object-literal properties as `key: value` lines, one line per object. */
123131
function readScriptData(script: string): string {
124132
const records: string[][] = [[]]
125-
for (const match of script.matchAll(OBJECT_PROPERTY_PATTERN)) {
133+
const code = script.replace(STRING_OR_COMMENT_PATTERN, (_, literal?: string) => literal ?? ' ')
134+
for (const match of code.matchAll(OBJECT_PROPERTY_PATTERN)) {
126135
const [, opener, bareKey, doubleKey, singleKey, doubleValue, singleValue, numberValue] = match
127136
const key = bareKey ?? unescapeJsString(doubleKey ?? singleKey ?? '')
128137
const value = numberValue ?? unescapeJsString(doubleValue ?? singleValue ?? '').trim()
@@ -136,7 +145,7 @@ function readScriptData(script: string): string {
136145
}
137146

138147
function isReadableValue(value: string): boolean {
139-
if (!value || value.startsWith('data:')) return false
148+
if (!value || value.startsWith('data:') || BASE64_PAYLOAD_PATTERN.test(value)) return false
140149
return value.length <= MAX_UNBROKEN_VALUE_LENGTH || /\s/.test(value)
141150
}
142151

‎apps/sim/connectors/confluence/confluence.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,42 @@ describe('Confluence Caelor Optics HTML macros', () => {
730730
expectOpticsText(document?.content)
731731
})
732732

733+
it('leaves commented-out records and bare base64 payloads out of the script data', async () => {
734+
const markup =
735+
'<h2>Find your app</h2><script>' +
736+
"// const retired = {name: 'Retired app'};\n" +
737+
"/* {name: 'Draft app'} */ const apps = [{name: 'Zoom', url: 'https://zoom.example/a'}];" +
738+
" const icon = {png: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB'};</script>"
739+
stubPage({ view: opticsView, storage: opticsMacro(JSON.stringify({ html: markup })) })
740+
741+
const document = await confluenceConnector.getDocument('token', config, 'optics-page', {
742+
cloudId: 'cloud-1',
743+
})
744+
745+
expect(document?.content).toContain('name: Zoom, url: https://zoom.example/a')
746+
expect(document?.content).not.toContain('Retired app')
747+
expect(document?.content).not.toContain('Draft app')
748+
expect(document?.content).not.toContain('iVBORw0KGgo')
749+
})
750+
751+
it('fails hydration when the storage response omits its body so the page retries', async () => {
752+
stubFetchWithoutAttachments(
753+
vi.fn(async (input: string | URL | Request) => {
754+
const format = new URL(String(input)).searchParams.get('body-format')
755+
return Response.json({
756+
id: 'optics-page',
757+
status: 'current',
758+
version: { number: 4 },
759+
body: format === 'storage' ? {} : { view: { value: `<p>Prose</p>${opticsView}` } },
760+
})
761+
})
762+
)
763+
764+
await expect(
765+
confluenceConnector.getDocument('token', config, 'optics-page', { cloudId: 'cloud-1' })
766+
).rejects.toThrow('missing its storage body')
767+
})
768+
733769
it('reads a contentByMode that holds raw HTML instead of JSON', async () => {
734770
stubPage({ view: opticsView, storage: opticsMacro(opticsHtml) })
735771

‎apps/sim/connectors/confluence/confluence.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,10 @@ export const confluenceConnector: ConnectorConfig = {
940940
}
941941
const storagePage = (await response.json()) as { body?: { storage?: { value?: unknown } } }
942942
const storage = storagePage.body?.storage?.value
943-
if (typeof storage === 'string') {
944-
plainText = [plainText, extractOpticsHtmlMacroText(storage)].filter(Boolean).join(' ')
943+
if (typeof storage !== 'string') {
944+
throw new Error('Confluence content is missing its storage body')
945945
}
946+
plainText = [plainText, extractOpticsHtmlMacroText(storage)].filter(Boolean).join(' ')
946947
}
947948

948949
const links = page._links as Record<string, unknown> | undefined

0 commit comments

Comments
 (0)