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

fix: code-canning char sanitization #60

Merged
merged 1 commit into from
Nov 13, 2024
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
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@
"devDependencies": {
"@next/eslint-plugin-next": "^15.0.3",
"@tailwindcss/typography": "^0.5.15",
"@types/node": "^22",
"@types/node": "^22.9.0",
"@types/react": "npm:[email protected]",
"@types/react-dom": "npm:[email protected]",
"@zl-asica/prettier-config": "^1",
"eslint": "^9",
"eslint-config-zl-asica": "^1",
"@zl-asica/prettier-config": "^1.0.7",
"autoprefixer": "^10.4.20",
"eslint": "^9.14.0",
"eslint-config-zl-asica": "^1.0.17",
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"postcss": "^8.4.49",
"prettier": "^3",
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.8",
"tailwindcss": "^3.4.1",
"typescript": "^5.6.0"
Expand Down
41 changes: 36 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions postcss.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const config = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};

Expand Down
40 changes: 26 additions & 14 deletions src/services/content/getPostFromFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@
showComments: defaultTo(data.showComments, true),
};

// Clean up HTML comments
let contentSanitized = markdownContent.replaceAll(/<!--[^>]*-->/g, (match) =>
match === '<!--more-->' ? match : ''
);

// Clean up HTML comments and render friend links
let contentSanitized = removeHtmlComments(markdownContent);
if (contentSanitized.includes('{% links %}')) {
contentSanitized = contentSanitized.replaceAll(
/{% links %}([\S\s]*?){% endlinks %}/g,
Expand All @@ -87,6 +84,17 @@
};
}

function removeHtmlComments(input) {
let previous;
do {
previous = input;
input = input.replaceAll(/<!--[^>]*-->/g, (match) =>
match === '<!--more-->' ? match : ''
);
} while (input !== previous);
return input;
}

// Helper function to resolve thumbnail
async function resolveThumbnail(thumbnail?: string): Promise<string> {
if (thumbnail && !thumbnail.includes('://')) {
Expand Down Expand Up @@ -126,7 +134,7 @@
.join('');
return `<div class="friends-links"><ul class="friends-links-list" role="list">${linksHtml}</ul></div>`;
} catch {
return '<>Invalid JSON in links block</>';
return '<div>Invalid JSON in links block</div>';
}
}

Expand All @@ -152,15 +160,19 @@

// Helper function to create post abstract
function processPostAbstract(contentHtml: string): string {
const plainText = contentHtml
// Use negative lookahead to match all comments except <!--more-->
const sanitizedContent = contentHtml.replaceAll(/<!--(?!more\b)[^>]*-->/g, '');
Fixed Show fixed Hide fixed

// Clean all but keep <!--more--> to split content
const plainText = sanitizedContent
.replaceAll('<!--more-->', '[[MORE_PLACEHOLDER]]')
.replaceAll(/<[^>]*>/g, '');

Check failure

Code scanning / CodeQL

Incomplete multi-character sanitization High

This string may still contain
<script
, which may cause an HTML element injection vulnerability.
const moreIndex = plainText.indexOf('[[MORE_PLACEHOLDER]]');
return (
moreIndex > 0 ? plainText.slice(0, moreIndex) : plainText.slice(0, 150)
)
.replaceAll(/\s+/g, ' ')
.trim();

// Find the index of <!--more--> and slice the content
const moreIndex = plainText.indexOf('[[MORE_PLACEHOLDER]]') || 0;

// Remove extra spaces and return the abstract
return plainText.slice(0, moreIndex).replaceAll(/\s+/g, ' ').trim();
}

export { getPostFromFile };
export default getPostFromFile;
2 changes: 1 addition & 1 deletion src/services/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'node:path';

import { filter } from 'es-toolkit/compat';

import { getPostFromFile } from '@/services/content/getPostFromFile';
import getPostFromFile from '@/services/content/getPostFromFile';

const postsDirectory = path.join(process.cwd(), 'posts');

Expand Down
Loading