Skip to content

fix: escape HTML in search keywords #2586

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/core/event/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isMobile, mobileBreakpoint } from '../util/env.js';
import * as dom from '../util/dom.js';
import { stripUrlExceptId } from '../router/util.js';

/** @typedef {import('../Docsify.js').Constructor} Constructor */

Expand Down Expand Up @@ -474,6 +475,8 @@ export function Events(Base) {
return;
}

href = stripUrlExceptId(href);

const oldActive = dom.find(sidebar, 'li.active');
const newActive = dom
.find(
Expand Down
3 changes: 2 additions & 1 deletion src/core/render/compiler/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getAndRemoveDocsifyIgnoreConfig,
} from '../utils.js';
import { slugify } from '../slugify.js';
import { stripUrlExceptId } from '../../router/util.js';

export const headingCompiler = ({ renderer, router, compiler }) =>
(renderer.heading = function ({ tokens, depth }) {
Expand All @@ -20,7 +21,7 @@ export const headingCompiler = ({ renderer, router, compiler }) =>
nextToc.ignoreSubHeading = ignoreSubHeading;
const slug = slugify(config.id || str);
const url = router.toURL(router.getCurrentPath(), { id: slug });
nextToc.slug = url;
nextToc.slug = stripUrlExceptId(url);
compiler.toc.push(nextToc);

// Note: tabindex="-1" allows programmatically focusing on heading
Expand Down
16 changes: 16 additions & 0 deletions src/core/router/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ export function stringifyQuery(obj, ignores = []) {
return qs.length ? `?${qs.join('&')}` : '';
}

export function stripUrlExceptId(str) {
const [path, queryString] = str.split('?');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this function stands for?

if (!queryString) {
return str;
}

const params = new URLSearchParams(queryString);
const id = params.get('id');

if (id !== null) {
return `${path}?id=${id}`;
}

return path;
}

export const isAbsolutePath = cached(path => {
return /(:|(\/{2}))/g.test(path);
});
Expand Down
16 changes: 8 additions & 8 deletions src/plugins/search/component.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { search } from './search.js';
import { escapeHtml, search } from './search.js';
import cssText from './style.css';

let NO_DATA_TEXT = '';
Expand Down Expand Up @@ -75,11 +75,11 @@ function bindEvents() {
let timeId;

/**
Prevent to Fold sidebar.

When searching on the mobile end,
the sidebar is collapsed when you click the INPUT box,
making it impossible to search.
* Prevent to Fold sidebar.
*
* When searching on the mobile end,
* the sidebar is collapsed when you click the INPUT box,
* making it impossible to search.
*/
Docsify.dom.on(
$search,
Expand Down Expand Up @@ -129,10 +129,10 @@ export function init(opts, vm) {
return;
}

const keywords = vm.router.parse().query.s;
const keywords = vm.router.parse().query.s || '';

Docsify.dom.style(cssText);
tpl(vm, keywords);
tpl(vm, escapeHtml(keywords));
bindEvents();
keywords && setTimeout(_ => doSearch(keywords), 500);
}
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { markdownToTxt } from './markdown-to-txt.js';
import Dexie from 'dexie';

let INDEXES = {};
let INDEXES = [];

const db = new Dexie('docsify');
db.version(1).stores({
Expand Down Expand Up @@ -48,7 +48,7 @@ function resolveIndexKey(namespace) {
: LOCAL_STORAGE.INDEX_KEY;
}

function escapeHtml(string) {
export function escapeHtml(string) {
const entityMap = {
'&': '&',
'<': '&lt;',
Expand Down Expand Up @@ -102,7 +102,7 @@ function getListData(token) {
export function genIndex(path, content = '', router, depth, indexKey) {
const tokens = window.marked.lexer(content);
const slugify = window.Docsify.slugify;
const index = {};
const index = [];
let slug;
let title = '';

Expand Down Expand Up @@ -299,7 +299,7 @@ export async function init(config, vm) {
INDEXES = await getData(indexKey);

if (isExpired) {
INDEXES = {};
INDEXES = [];
} else if (!isAuto) {
return;
}
Expand Down
Loading