Skip to content

Commit fef1173

Browse files
committed
fix search index decryption
1 parent ab836ac commit fef1173

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

encryptcontent/decrypt-contents.tpl.js

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,33 +147,24 @@ function reload_js(src) {
147147

148148
{% if experimental -%}
149149
/* Decrypt part of the search index and refresh it for search engine */
150-
function decrypt_search(key, path_location) {
150+
function decrypt_search(keys) {
151151
sessionIndex = sessionStorage.getItem('encryptcontent-index');
152152
let could_decrypt = false;
153153
if (sessionIndex) {
154154
sessionIndex = JSON.parse(sessionIndex);
155155
for (var i=0; i < sessionIndex.docs.length; i++) {
156-
var doc = sessionIndex.docs[i];
157-
if (doc.location.indexOf(path_location.replace('{{ site_path }}', '')) !== -1) {
158-
// grab the ciphertext bundle and try to decrypt it
159-
let title = decrypt_content_from_bundle(key, doc.title);
160-
if (title !== false) {
161-
could_decrypt = true;
162-
doc.title = title;
163-
// any post processing on the decrypted search index should be done here
164-
let content = decrypt_content_from_bundle(key, doc.text);
165-
if (content !== false) {
166-
doc.text = content;
167-
let location_bundle = doc.location;
168-
let location_sep = location_bundle.indexOf(';')
169-
if (location_sep !== -1) {
170-
let toc_bundle = location_bundle.substring(location_sep+1)
171-
let location_doc = location_bundle.substring(0,location_sep)
172-
let toc_url = decrypt_content_from_bundle(key, toc_bundle);
173-
if (toc_url !== false) {
174-
doc.location = location_doc + toc_url;
175-
}
176-
}
156+
let doc = sessionIndex.docs[i];
157+
let location_sep = doc.location.indexOf(';');
158+
if (location_sep !== -1) {
159+
let location_id = doc.location.substring(0,location_sep);
160+
let location_bundle = doc.location.substring(location_sep+1);
161+
if (location_id in keys) {
162+
let key = keys[location_id];
163+
doc.location = decrypt_content_from_bundle(key, location_bundle);
164+
doc.text = decrypt_content_from_bundle(key, doc.text);
165+
doc.title = decrypt_content_from_bundle(key, doc.title);
166+
if (doc.title !== false) {
167+
could_decrypt = true;
177168
}
178169
}
179170
}
@@ -278,14 +269,14 @@ function decryptor_reaction(key_or_keys, password_input, fallback_used=false) {
278269
{% if remember_password -%}
279270
setKeys(key_or_keys);
280271
{%- endif %}
272+
{% if experimental -%}
273+
decrypt_search(key_or_keys);
274+
{%- endif %}
281275
} else {
282276
key = key_or_keys;
283277
}
284278

285279
// continue to decrypt others parts
286-
{% if experimental -%}
287-
//decrypt_search(key, location_path);
288-
{%- endif %}
289280
{% if encrypted_something -%}
290281
let encrypted_something = {{ encrypted_something }};
291282
decrypt_somethings(key, encrypted_something);

encryptcontent/plugin.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -626,30 +626,36 @@ def on_page_markdown(self, markdown, page, config, **kwargs):
626626
del page.meta['encryption_info_message']
627627

628628
if encryptcontent.get('password'):
629-
if encryptcontent['password'] not in self.setup['password_keys']:
629+
index = encryptcontent['password']
630+
if index not in self.setup['password_keys']:
630631
new_entry = {}
631632
self.keystore_id += 1
632633
new_entry['id'] = quote(self.config['remember_suffix'] + str(self.keystore_id), safe='~()*!\'')
633634
new_entry['key'] = get_random_bytes(32)
634-
self.__add_to_keystore__((KS_PASSWORD,password), new_entry['key'], new_entry['id'], encryptcontent['password'])
635-
self.setup['password_keys'][encryptcontent['password']] = new_entry
635+
self.__add_to_keystore__((KS_PASSWORD,index), new_entry['key'], new_entry['id'], index)
636+
self.setup['password_keys'][index] = new_entry
636637
encryptcontent['type'] = 'password'
637-
encryptcontent['key'] = self.setup['password_keys'][encryptcontent['password']]['key']
638+
encryptcontent['key'] = self.setup['password_keys'][index]['key']
639+
encryptcontent['id'] = self.setup['password_keys'][index]['id']
638640
setattr(page, 'encryptcontent', encryptcontent)
639641
elif encryptcontent.get('level'):
642+
index = encryptcontent['level']
640643
encryptcontent['type'] = 'level'
641-
encryptcontent['key'] = self.setup['level_keys'][encryptcontent['level']]['key']
644+
encryptcontent['key'] = self.setup['level_keys'][index]['key']
645+
encryptcontent['id'] = self.setup['level_keys'][index]['id']
642646
setattr(page, 'encryptcontent', encryptcontent)
643647
elif encryptcontent.get('obfuscate'):
644-
if encryptcontent['obfuscate'] not in self.setup['obfuscate_keys']:
648+
index = encryptcontent['obfuscate']
649+
if index not in self.setup['obfuscate_keys']:
645650
new_entry = {}
646651
self.keystore_id += 1
647652
new_entry['id'] = quote(self.config['remember_suffix'] + str(self.keystore_id), safe='~()*!\'')
648653
new_entry['key'] = get_random_bytes(32)
649-
self.__add_to_keystore__((KS_OBFUSCATE,encryptcontent['obfuscate']), new_entry['key'], new_entry['id'], encryptcontent['obfuscate'])
650-
self.setup['obfuscate_keys'][encryptcontent['obfuscate']] = new_entry
654+
self.__add_to_keystore__((KS_OBFUSCATE,index), new_entry['key'], new_entry['id'], index)
655+
self.setup['obfuscate_keys'][index] = new_entry
651656
encryptcontent['type'] = 'obfuscate'
652-
encryptcontent['key'] = self.setup['obfuscate_keys'][encryptcontent['obfuscate']]['key']
657+
encryptcontent['key'] = self.setup['obfuscate_keys'][index]['key']
658+
encryptcontent['id'] = self.setup['obfuscate_keys'][index]['id']
653659
setattr(page, 'encryptcontent', encryptcontent)
654660

655661
return markdown
@@ -830,7 +836,7 @@ def on_post_page(self, output_content, page, config, **kwargs):
830836

831837
if hasattr(page, 'encryptcontent'):
832838
location = page.url.lstrip('/')
833-
self.setup['locations'][location] = page.encryptcontent['key']
839+
self.setup['locations'][location] = (page.encryptcontent['key'], page.encryptcontent['id'])
834840
delattr(page, 'encryptcontent')
835841

836842
if self.config['sign_files']:
@@ -891,20 +897,21 @@ def on_post_build(self, config, **kwargs):
891897
for entry in search_entries['docs'].copy(): #iterate through all entries of search_index
892898
for location in self.setup['locations'].keys():
893899
if entry['location'] == location or entry['location'].startswith(location+"#"): #find the ones located at encrypted pages
894-
page_key = self.setup['locations'][location]
900+
page_key = self.setup['locations'][location][0]
901+
page_id = self.setup['locations'][location][1]
895902
if self.config['search_index'] == 'encrypted':
896903
search_entries['docs'].remove(entry)
897904
elif self.config['search_index'] == 'dynamically' and page_key is not None:
898-
#encrypt text/title/location(anchor only)
905+
#encrypt text/title/location
899906
text = entry['text']
900907
title = entry['title']
901-
toc_anchor = entry['location'].replace(location, '')
908+
location = entry['location']
909+
code = self.__encrypt_text__(location, page_key)
910+
entry['location'] = page_id + ';' + ';'.join(code) # add encryptcontent_id
902911
code = self.__encrypt_text__(text, page_key )
903912
entry['text'] = ';'.join(code)
904913
code = self.__encrypt_text__(title, page_key)
905914
entry['title'] = ';'.join(code)
906-
code = self.__encrypt_text__(toc_anchor, page_key)
907-
entry['location'] = location + ';' + ';'.join(code)
908915
break
909916
self.setup['locations'].clear()
910917

0 commit comments

Comments
 (0)