-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[PE-219] chore: new live server endpoint to convert description_html to all other formats #6310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // plane editor | ||
| import { | ||
| getAllDocumentFormatsFromDocumentEditorBinaryData, | ||
| getAllDocumentFormatsFromRichTextEditorBinaryData, | ||
| getBinaryDataFromDocumentEditorHTMLString, | ||
| getBinaryDataFromRichTextEditorHTMLString, | ||
| } from "@plane/editor"; | ||
| // plane types | ||
| import { TDocumentPayload } from "@plane/types"; | ||
|
|
||
| type TArgs = { | ||
| document_html: string; | ||
| variant: "rich" | "document"; | ||
| }; | ||
|
|
||
| export const convertHTMLDocumentToAllFormats = (args: TArgs): TDocumentPayload => { | ||
| const { document_html, variant } = args; | ||
|
|
||
| let allFormats: TDocumentPayload; | ||
|
|
||
| if (variant === "rich") { | ||
| const contentBinary = getBinaryDataFromRichTextEditorHTMLString(document_html); | ||
| const { contentBinaryEncoded, contentHTML, contentJSON } = | ||
| getAllDocumentFormatsFromRichTextEditorBinaryData(contentBinary); | ||
| allFormats = { | ||
| description: contentJSON, | ||
| description_html: contentHTML, | ||
| description_binary: contentBinaryEncoded, | ||
| }; | ||
| } else if (variant === "document") { | ||
| const contentBinary = getBinaryDataFromDocumentEditorHTMLString(document_html); | ||
| const { contentBinaryEncoded, contentHTML, contentJSON } = | ||
| getAllDocumentFormatsFromDocumentEditorBinaryData(contentBinary); | ||
| allFormats = { | ||
| description: contentJSON, | ||
| description_html: contentHTML, | ||
| description_binary: contentBinaryEncoded, | ||
| }; | ||
| } else { | ||
| throw new Error(`Invalid variant provided: ${variant}`); | ||
| } | ||
|
|
||
| return allFormats; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { getSchema } from "@tiptap/core"; | ||
| import { generateHTML, generateJSON } from "@tiptap/html"; | ||
| import { prosemirrorJSONToYDoc, yXmlFragmentToProseMirrorRootNode } from "y-prosemirror"; | ||
| import * as Y from "yjs"; | ||
| // extensions | ||
| import { | ||
| CoreEditorExtensionsWithoutProps, | ||
| DocumentEditorExtensionsWithoutProps, | ||
| } from "@/extensions/core-without-props"; | ||
|
|
||
| // editor extension configs | ||
| const RICH_TEXT_EDITOR_EXTENSIONS = CoreEditorExtensionsWithoutProps; | ||
| const DOCUMENT_EDITOR_EXTENSIONS = [...CoreEditorExtensionsWithoutProps, ...DocumentEditorExtensionsWithoutProps]; | ||
| // editor schemas | ||
| const richTextEditorSchema = getSchema(RICH_TEXT_EDITOR_EXTENSIONS); | ||
| const documentEditorSchema = getSchema(DOCUMENT_EDITOR_EXTENSIONS); | ||
|
|
||
| /** | ||
| * @description apply updates to a doc and return the updated doc in binary format | ||
| * @param {Uint8Array} document | ||
| * @param {Uint8Array} updates | ||
| * @returns {Uint8Array} | ||
| */ | ||
| export const applyUpdates = (document: Uint8Array, updates?: Uint8Array): Uint8Array => { | ||
| const yDoc = new Y.Doc(); | ||
| Y.applyUpdate(yDoc, document); | ||
| if (updates) { | ||
| Y.applyUpdate(yDoc, updates); | ||
| } | ||
|
|
||
| const encodedDoc = Y.encodeStateAsUpdate(yDoc); | ||
| return encodedDoc; | ||
| }; | ||
|
|
||
| /** | ||
| * @description this function encodes binary data to base64 string | ||
| * @param {Uint8Array} document | ||
| * @returns {string} | ||
| */ | ||
| export const convertBinaryDataToBase64String = (document: Uint8Array): string => | ||
| Buffer.from(document).toString("base64"); | ||
|
|
||
| /** | ||
| * @description this function decodes base64 string to binary data | ||
| * @param {string} document | ||
| * @returns {ArrayBuffer} | ||
| */ | ||
| export const convertBase64StringToBinaryData = (document: string): ArrayBuffer => Buffer.from(document, "base64"); | ||
|
|
||
| /** | ||
| * @description this function generates the binary equivalent of html content for the rich text editor | ||
| * @param {string} descriptionHTML | ||
| * @returns {Uint8Array} | ||
| */ | ||
| export const getBinaryDataFromRichTextEditorHTMLString = (descriptionHTML: string): Uint8Array => { | ||
| // convert HTML to JSON | ||
| const contentJSON = generateJSON(descriptionHTML ?? "<p></p>", RICH_TEXT_EDITOR_EXTENSIONS); | ||
| // convert JSON to Y.Doc format | ||
| const transformedData = prosemirrorJSONToYDoc(richTextEditorSchema, contentJSON, "default"); | ||
| // convert Y.Doc to Uint8Array format | ||
| const encodedData = Y.encodeStateAsUpdate(transformedData); | ||
| return encodedData; | ||
| }; | ||
|
|
||
| /** | ||
| * @description this function generates the binary equivalent of html content for the document editor | ||
| * @param {string} descriptionHTML | ||
| * @returns {Uint8Array} | ||
| */ | ||
| export const getBinaryDataFromDocumentEditorHTMLString = (descriptionHTML: string): Uint8Array => { | ||
| // convert HTML to JSON | ||
| const contentJSON = generateJSON(descriptionHTML ?? "<p></p>", DOCUMENT_EDITOR_EXTENSIONS); | ||
| // convert JSON to Y.Doc format | ||
| const transformedData = prosemirrorJSONToYDoc(documentEditorSchema, contentJSON, "default"); | ||
| // convert Y.Doc to Uint8Array format | ||
| const encodedData = Y.encodeStateAsUpdate(transformedData); | ||
| return encodedData; | ||
| }; | ||
|
|
||
| /** | ||
| * @description this function generates all document formats for the provided binary data for the rich text editor | ||
| * @param {Uint8Array} description | ||
| * @returns | ||
| */ | ||
| export const getAllDocumentFormatsFromRichTextEditorBinaryData = ( | ||
| description: Uint8Array | ||
| ): { | ||
| contentBinaryEncoded: string; | ||
| contentJSON: object; | ||
| contentHTML: string; | ||
| } => { | ||
| // encode binary description data | ||
| const base64Data = convertBinaryDataToBase64String(description); | ||
| const yDoc = new Y.Doc(); | ||
| Y.applyUpdate(yDoc, description); | ||
| // convert to JSON | ||
| const type = yDoc.getXmlFragment("default"); | ||
| const contentJSON = yXmlFragmentToProseMirrorRootNode(type, richTextEditorSchema).toJSON(); | ||
| // convert to HTML | ||
| const contentHTML = generateHTML(contentJSON, RICH_TEXT_EDITOR_EXTENSIONS); | ||
|
|
||
| return { | ||
| contentBinaryEncoded: base64Data, | ||
| contentJSON, | ||
| contentHTML, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * @description this function generates all document formats for the provided binary data for the document editor | ||
| * @param {Uint8Array} description | ||
| * @returns | ||
| */ | ||
| export const getAllDocumentFormatsFromDocumentEditorBinaryData = ( | ||
| description: Uint8Array | ||
| ): { | ||
| contentBinaryEncoded: string; | ||
| contentJSON: object; | ||
| contentHTML: string; | ||
| } => { | ||
| // encode binary description data | ||
| const base64Data = convertBinaryDataToBase64String(description); | ||
| const yDoc = new Y.Doc(); | ||
| Y.applyUpdate(yDoc, description); | ||
| // convert to JSON | ||
| const type = yDoc.getXmlFragment("default"); | ||
| const contentJSON = yXmlFragmentToProseMirrorRootNode(type, documentEditorSchema).toJSON(); | ||
| // convert to HTML | ||
| const contentHTML = generateHTML(contentJSON, DOCUMENT_EDITOR_EXTENSIONS); | ||
|
|
||
| return { | ||
| contentBinaryEncoded: base64Data, | ||
| contentJSON, | ||
| contentHTML, | ||
| }; | ||
| }; |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export * from "@/extensions/core-without-props"; | ||
| export * from "@/constants/document-collaborative-events"; | ||
| export * from "@/helpers/get-document-server-event"; | ||
| export * from "@/helpers/yjs-utils"; | ||
| export * from "@/types/document-collaborative-events"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Based on the analysis of the codebase, I can now provide the final response:
HTML input requires additional security measures
The current implementation lacks proper HTML sanitization for the
description_htmlinput, which could lead to XSS vulnerabilities. While the codebase includes DOMPurify for sanitization in other areas (via@plane/utils), it's not being used in this endpoint. Additionally, thevariantparameter needs type validation.Required changes:
sanitizeHTMLutility from@plane/utilsto sanitize thedescription_htmlinput before processingvariantparameter using TypeScript's union type"rich" | "document"(as seen in theconvertHTMLDocumentToAllFormatsimplementation)Example implementation:
🔗 Analysis chain
Ensure robust sanitization and validation for HTML input.
While you verify that
description_htmlandvariantare defined, consider:description_htmlto prevent XSS vulnerabilities and potential malicious injection.variantvalues to “rich” or “document” to avoid unexpected behavior.Here’s a sample snippet (no direct diff as it spans multiple lines) illustrating a possible sanitization approach using DOMPurify:
Would you like a more in-depth solution that includes advanced sanitization and variant validation?
Let me verify the concerns by checking the codebase for existing sanitization, validation mechanisms, and the allowed variant values.
Let me check the implementation details of the conversion function and the type definitions to better understand the validation needs.
Let me check the type definition for TConvertDocumentRequestBody and examine the editor package's HTML processing functions since they handle the actual conversion.
Let me check the editor extensions and components that might be handling HTML processing and security.
Let me check the core editor extensions to understand how HTML content is processed and secured.
🏁 Scripts executed
The following scripts were executed for the analysis:
Error: No shell scripts found
Script:
Length of output: 4675
Script:
Length of output: 4335
Script:
Length of output: 2374
Script:
Length of output: 15585
Script:
Length of output: 12207