Skip to content

Commit 99e35ca

Browse files
committed
always try to encrypt search index if experimental search index encryption is selected.
1 parent e531b87 commit 99e35ca

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
dist/
12
build/
23
mkdocs_encryptcontent_plugin.egg-info/
34
install.cmd

encryptcontent/decrypt-contents.tpl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ function reload_js(src) {
133133
$('<script>').attr('src', src).appendTo('head');
134134
};
135135

136+
{% if experimental -%}
136137
/* Decrypt part of the search index and refresh it for search engine */
137138
function decrypt_search(password_value, path_location) {
138139
sessionIndex = sessionStorage.getItem('encryptcontent-index');
@@ -178,6 +179,7 @@ function decrypt_search(password_value, path_location) {
178179
}
179180
}
180181
};
182+
{%- endif %}
181183

182184
/* Decrypt speficique html entry from mkdocs configuration */
183185
function decrypt_somethings(password_value, encrypted_something) {

encryptcontent/plugin.py

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ class encryptContentPlugin(BasePlugin):
7676
('encrypted_something', config_options.Type(dict, default={})),
7777
('search_index', config_options.Choice(('clear', 'dynamically', 'encrypted'), default='encrypted')),
7878
('reload_scripts', config_options.Type(list, default=[])),
79-
('experimental', config_options.Type(bool, default=False)),
8079
('inject', config_options.Type(dict, default={})),
8180
('selfhost', config_options.Type(bool, default=False)),
8281
('selfhost_download', config_options.Type(bool, default=True)),
8382
# legacy features, doesn't exist anymore
8483
('disable_cookie_protection', config_options.Type(bool, default=False)),
85-
('decrypt_search', config_options.Type(bool, default=False))
84+
('decrypt_search', config_options.Type(bool, default=False)),
85+
('experimental', config_options.Type(bool, default=False)),
8686
)
8787

8888
def __hash_md5__(self, text):
@@ -153,7 +153,7 @@ def __generate_decrypt_js__(self):
153153
'default_expire_delay': int(self.config['default_expire_delay']),
154154
'encrypted_something': self.config['encrypted_something'],
155155
'reload_scripts': self.config['reload_scripts'],
156-
'experimental': self.config['experimental'],
156+
'experimental': self.config['search_index'] == 'dynamically',
157157
'site_path': self.config['site_path'],
158158
# add extra vars
159159
'extra': self.config['js_extra_vars']
@@ -232,11 +232,10 @@ def on_config(self, config, **kwargs):
232232
config['plugins'].move_to_end('search')
233233
config['plugins'].move_to_end('encryptcontent')
234234
except:
235-
logger.warning('Please check that "search" and "encryptcontent" are at the end of plugins')
235+
logger.warning('Could not reorder plugins. Please check that "search" and "encryptcontent" are at the end of plugins')
236236
# Enable experimental code .. :popcorn:
237-
if self.config['search_index'] == 'dynamically':
238-
logger.info('EXPERIMENTAL MODE ENABLE. Only work with default SearchPlugin, not Material.')
239-
self.config['experimental'] = True
237+
elif self.config['search_index'] == 'dynamically':
238+
logger.info("EXPERIMENTAL search index encryption enabled.")
240239
self.config['encrypted_something'] = self.config['inject'] | self.config['encrypted_something'] #add inject to encrypted_something
241240
# Get path to site in case of subdir in site_url
242241
self.config['site_path'] = urlsplit(config.data["site_url"] or '/').path[1::]
@@ -255,19 +254,19 @@ def on_pre_build(self, config, **kwargs):
255254
# ref: https://github.com/mkdocs/mkdocs/tree/master/mkdocs/contrib/search
256255
try:
257256
#search_index encryption was moved to on_post_page
258-
if self.config['experimental'] is True:
257+
if self.config['search_index'] == 'dynamically':
259258
if config['theme'].name == 'material':
260-
logger.error("UNSUPPORTED Material theme with experimantal feature search_index=dynamically !")
261-
exit("UNSUPPORTED Material theme: use search_index: [clear|encrypted] instead.")
262-
# Overwrite search/*.js files from templates/search with encryptcontent contrib search assets
263-
for dir in config['theme'].dirs.copy():
264-
if re.compile(r".*[/\\]contrib[/\\]search[/\\]templates$").match(dir):
265-
config['theme'].dirs.remove(dir)
266-
path = os.path.join(base_path, 'contrib/templates')
267-
config['theme'].dirs.append(path)
268-
if 'search/main.js' not in config['extra_javascript']:
269-
config['extra_javascript'].append('search/main.js')
270-
break
259+
logger.warning("To enable EXPERIMENTAL search index decryption mkdocs-material needs to be customized (patched)!")
260+
else:
261+
# Overwrite search/*.js files from templates/search with encryptcontent contrib search assets
262+
for dir in config['theme'].dirs.copy():
263+
if re.compile(r".*[/\\]contrib[/\\]search[/\\]templates$").match(dir):
264+
config['theme'].dirs.remove(dir)
265+
path = os.path.join(base_path, 'contrib/templates')
266+
config['theme'].dirs.append(path)
267+
if 'search/main.js' not in config['extra_javascript']:
268+
config['extra_javascript'].append('search/main.js')
269+
break
271270
except Exception as exp:
272271
logger.exception(exp)
273272

@@ -407,25 +406,29 @@ def on_post_page(self, output_content, page, config, **kwargs):
407406

408407
if hasattr(page, 'encrypted'):
409408
#encrypt or exclude encrypted pages from search_index.json
410-
if 'search' in config['plugins']:
411-
search_entries = config['plugins']['search'].search_index._entries
412-
for entry in search_entries.copy(): #iterate through all entries of search_index
413-
location = page.url.lstrip('/')
414-
if entry['location'] == location or entry['location'].startswith(location+"#"): #find the ones located at encrypted pages
415-
if self.config['search_index'] == 'encrypted':
416-
search_entries.remove(entry)
417-
elif self.config['search_index'] == 'dynamically' and page.password is not None:
418-
#encrypt text/title/location(anchor only)
419-
text = entry['text']
420-
title = entry['title']
421-
toc_anchor = entry['location'].replace(location, '')
422-
code = self.__encrypt_text_aes__(text, page.password)
423-
entry['text'] = b';'.join(code).decode('ascii')
424-
code = self.__encrypt_text_aes__(title, page.password)
425-
entry['title'] = b';'.join(code).decode('ascii')
426-
code = self.__encrypt_text_aes__(toc_anchor, page.password)
427-
entry['location'] = location + ';' + b';'.join(code).decode('ascii')
428-
config['plugins']['search'].search_index._entries = search_entries
409+
for plugin in config['plugins']:
410+
if plugin.endswith('search'):
411+
try:
412+
search_entries = config['plugins'][plugin].search_index._entries
413+
for entry in search_entries.copy(): #iterate through all entries of search_index
414+
location = page.url.lstrip('/')
415+
if entry['location'] == location or entry['location'].startswith(location+"#"): #find the ones located at encrypted pages
416+
if self.config['search_index'] == 'encrypted':
417+
search_entries.remove(entry)
418+
elif self.config['search_index'] == 'dynamically' and page.password is not None:
419+
#encrypt text/title/location(anchor only)
420+
text = entry['text']
421+
title = entry['title']
422+
toc_anchor = entry['location'].replace(location, '')
423+
code = self.__encrypt_text_aes__(text, page.password)
424+
entry['text'] = b';'.join(code).decode('ascii')
425+
code = self.__encrypt_text_aes__(title, page.password)
426+
entry['title'] = b';'.join(code).decode('ascii')
427+
code = self.__encrypt_text_aes__(toc_anchor, page.password)
428+
entry['location'] = location + ';' + b';'.join(code).decode('ascii')
429+
config['plugins'][plugin].search_index._entries = search_entries
430+
except:
431+
logger.error('Could not encrypt search index of "' + page.title + '" for "' + plugin+ '" plugin!')
429432

430433
return output_content
431434

File renamed without changes.

0 commit comments

Comments
 (0)