Skip to content
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

Strip markdown comments from stringified commits #528

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions src/lib/github/v3/getPullRequest/getPullRequestBody.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ text to append"
);
});

it('strips markdown comments', () => {
const commits = [
{
sourcePullRequest: {
url: 'https://github.com/backport-org/different-merge-strategies/pull/55',
},
sourceCommit: {
sha: 'acbcdef',
message:
'My commit message (#55) <!-- markdown-comment --> And then some more text',
},
sourceBranch: 'main',
},
] as Commit[];

const options = {
prDescription: 'Just output the commits: {commits}',
} as ValidConfigOptions;

const res = getPullRequestBody({
options,
commits,
targetBranch: '7.x',
});

expect(res).not.toContain('markdown-comment');
expect(res).not.toContain('<!--');
expect(res).not.toContain('-->');
expect(res).toContain(
'My commit message (#55) And then some more text',
);
});

it('handles curly brackets in commit messages without error', () => {
const commits = [
{
Expand Down
8 changes: 7 additions & 1 deletion src/lib/github/v3/getPullRequest/getPullRequestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
'### Questions ?\n' +
'Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport)';

const commitsStringified = `{{{{raw}}}}${JSON.stringify(commits)}{{{{/raw}}}}`;
const commitsStringified = stripMarkdownComments(
`{{{{raw}}}}${JSON.stringify(commits)}{{{{/raw}}}}`,
);

const prDescription = (options.prDescription ?? defaultPrDescription)

Expand Down Expand Up @@ -79,3 +81,7 @@
.replaceAll('{{{{/raw}}}}', '');
}
}

function stripMarkdownComments(str: string): string {
return str.replace(/<!--[\s\S]*?-->/g, '');

Check failure

Code scanning / CodeQL

Incomplete multi-character sanitization High

This string may still contain
<!--
, which may cause an HTML element injection vulnerability.

Copilot Autofix AI 14 days ago

To fix the problem, we need to ensure that all instances of the targeted pattern are removed, even if they appear consecutively or are nested. One effective way to achieve this is to apply the regular expression replacement repeatedly until no more replacements can be performed. This ensures that the unsafe text does not reappear in the sanitized input.

We will modify the stripMarkdownComments function to repeatedly apply the regular expression replacement until the input string no longer changes. This will ensure that all HTML comments are fully removed.

Suggested changeset 1
src/lib/github/v3/getPullRequest/getPullRequestBody.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/lib/github/v3/getPullRequest/getPullRequestBody.ts b/src/lib/github/v3/getPullRequest/getPullRequestBody.ts
--- a/src/lib/github/v3/getPullRequest/getPullRequestBody.ts
+++ b/src/lib/github/v3/getPullRequest/getPullRequestBody.ts
@@ -85,3 +85,8 @@
 function stripMarkdownComments(str: string): string {
-  return str.replace(/<!--[\s\S]*?-->/g, '');
+  let previous;
+  do {
+    previous = str;
+    str = str.replace(/<!--[\s\S]*?-->/g, '');
+  } while (str !== previous);
+  return str;
 }
EOF
@@ -85,3 +85,8 @@
function stripMarkdownComments(str: string): string {
return str.replace(/<!--[\s\S]*?-->/g, '');
let previous;
do {
previous = str;
str = str.replace(/<!--[\s\S]*?-->/g, '');
} while (str !== previous);
return str;
}
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
}
Loading