Skip to content

Commit 7bc5f7c

Browse files
committed
fix(confluence): keep regex literals and long names in Optics script data
1 parent 436245d commit 7bc5f7c

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ 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}$/
28+
const BASE64_ALPHABET_PATTERN = /^[A-Za-z0-9+/]{24,}={0,2}$/
2929

3030
/**
31-
* A string literal (kept, so a `//` inside `'https://…'` survives) or a comment
32-
* (dropped, so commented-out records never read as page data).
31+
* A string literal (kept, so a `//` inside `'https://…'` survives), a regex
32+
* literal (kept, so the `//` ending `/https?:\/\//` survives; recognized only
33+
* where an expression can start, so division is not mistaken for one), or a
34+
* comment (dropped, so commented-out records never read as page data).
3335
*/
3436
const STRING_OR_COMMENT_PATTERN =
35-
/("(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'|`(?:[^`\\]|\\.)*`)|\/\/[^\r\n]*|\/\*[\s\S]*?\*\//g
37+
/("(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'|`(?:[^`\\]|\\.)*`|\/(?<=(?:^|[=(,:;!&|?{}[\]+\-*%~^<>]|\breturn)\s*\/)(?![*/])(?:[^/\\\r\n[]|\\.|\[(?:[^\]\\\r\n]|\\.)*\])+\/[a-z]*)|\/\/[^\r\n]*|\/\*[\s\S]*?\*\//g
3638

3739
/**
3840
* Whether a rendered `view` body may hold an Optics HTML macro, whose text only
@@ -145,10 +147,20 @@ function readScriptData(script: string): string {
145147
}
146148

147149
function isReadableValue(value: string): boolean {
148-
if (!value || value.startsWith('data:') || BASE64_PAYLOAD_PATTERN.test(value)) return false
150+
if (!value || value.startsWith('data:') || isBase64Payload(value)) return false
149151
return value.length <= MAX_UNBROKEN_VALUE_LENGTH || /\s/.test(value)
150152
}
151153

154+
/**
155+
* Base64-alphabet runs that also carry the evidence of encoding: padding or
156+
* `+`/`/`, or the upper, lower, and digit mix random bytes produce. A long name
157+
* (`MicrosoftAuthenticationLibrary`) or numeric id has the alphabet but not this.
158+
*/
159+
function isBase64Payload(value: string): boolean {
160+
if (!BASE64_ALPHABET_PATTERN.test(value)) return false
161+
return /[+/=]/.test(value) || (/[a-z]/.test(value) && /[A-Z]/.test(value) && /\d/.test(value))
162+
}
163+
152164
const JS_ESCAPES: Record<string, string> = { n: ' ', r: ' ', t: ' ', b: '', f: '', v: '' }
153165

154166
function unescapeJsString(value: string): string {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,11 +730,12 @@ 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 () => {
733+
it('keeps script data around regex literals and long names, without comments or base64', async () => {
734734
const markup =
735735
'<h2>Find your app</h2><script>' +
736736
"// const retired = {name: 'Retired app'};\n" +
737-
"/* {name: 'Draft app'} */ const apps = [{name: 'Zoom', url: 'https://zoom.example/a'}];" +
737+
"/* {name: 'Draft app'} */ const re = /https?:\\/\\//; const apps = [{name: 'Zoom', url: 'https://zoom.example/a'}," +
738+
" {name: 'MicrosoftAuthenticationLibrary', id: '100200300400500600700'}];" +
738739
" const icon = {png: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB'};</script>"
739740
stubPage({ view: opticsView, storage: opticsMacro(JSON.stringify({ html: markup })) })
740741

@@ -743,6 +744,9 @@ describe('Confluence Caelor Optics HTML macros', () => {
743744
})
744745

745746
expect(document?.content).toContain('name: Zoom, url: https://zoom.example/a')
747+
expect(document?.content).toContain(
748+
'name: MicrosoftAuthenticationLibrary, id: 100200300400500600700'
749+
)
746750
expect(document?.content).not.toContain('Retired app')
747751
expect(document?.content).not.toContain('Draft app')
748752
expect(document?.content).not.toContain('iVBORw0KGgo')

0 commit comments

Comments
 (0)