Skip to content

Commit ae99267

Browse files
committed
Add feature to disable search encryption
1 parent 5306828 commit ae99267

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Install the package from source with pip:
3232
```bash
3333
cd mkdocs-encryptcontent-plugin/
3434
python3 setup.py sdist bdist_wheel
35-
pip3 install dist/mkdocs_encryptcontent_plugin-1.0.0-py3-none-any.whl
35+
pip3 install dist/mkdocs_encryptcontent_plugin-1.1.0-py3-none-any.whl
3636
```
3737

3838
Enable the plugin in your `mkdocs.yml`:
@@ -282,16 +282,39 @@ Your configuration like this :
282282
```
283283

284284

285+
### Do not encrypt search index
286+
287+
Related to [issue #13](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues/13)
288+
289+
> :warning: **This feature is NOT SECURE and CAUSE DATA LEAK**
290+
>
291+
> The unencrypted content of each page is accessible through the search index.
292+
> Not encrypting the search index means completely removing the protection provided by this plugin.
293+
> You have been warned
294+
295+
You can set `decrypt_search: True` in your `mkdocs.yml` to disable the search index encryption process.
296+
297+
```yaml
298+
plugins:
299+
- encryptcontent:
300+
decrypt_search: True
301+
```
302+
303+
It becomes possible again to make searches on all the pages, even if the content of the page is encrypted.
304+
305+
If you still want to protect some pages, even though the search index is not encrypted, you can use [mkdocs-exclude-search](https://github.com/chrieke/mkdocs-exclude-search) to exclude parts or complete articles from the search index.
306+
307+
285308
## Contributing
286309

287310
From reporting a bug to submitting a pull request: every contribution is appreciated and welcome.
311+
288312
Report bugs, ask questions and request features using [Github issues][github-issues].
313+
289314
If you want to contribute to the code of this project, please read the [Contribution Guidelines][contributing].
290315

291-
[mkdocs-plugins]: http://www.mkdocs.org/user-guide/plugins/
316+
[mkdocs-plugins]: https://www.mkdocs.org/dev-guide/plugins/
292317
[github-issues]: https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues
293318
[contributing]: CONTRIBUTING.md
294319

295-
### Contributors
296-
297-
- [anthonyeden](https://github.com/anthonyeden)
320+
### [Contributors](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/graphs/contributors)

encryptcontent/decrypt-form.tpl.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ <h1>{{ summary }}</h1>
138138
decrypted_content.innerHTML = content;
139139
encrypted_content.parentNode.removeChild(encrypted_content);
140140
// any post processing on the decrypted content should be done here
141-
{% if arithmatex %}MathJax.typesetPromise(){% endif %}
141+
{% if arithmatex %}
142+
MathJax.typesetPromise()
143+
{% endif %}
142144
{% if hljs %}
143145
document.getElementById("mkdocs-decrypted-content").querySelectorAll('pre code').forEach((block) => {
144146
hljs.highlightBlock(block);

encryptcontent/plugin.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import mkdocs
55
import base64
66
import hashlib
7+
import logging
78
from Crypto import Random
89
from jinja2 import Template
910
from Crypto.Cipher import AES
@@ -39,6 +40,7 @@
3940
'encryption_info_message': 'Contact your administrator for access to this page.'
4041
}
4142

43+
logger = logging.getLogger("mkdocs.plugins.encryptcontent")
4244

4345
class encryptContentPlugin(BasePlugin):
4446
""" Plugin that encrypt markdown content with AES and inject decrypt form. """
@@ -59,6 +61,7 @@ class encryptContentPlugin(BasePlugin):
5961
('password_button', mkdocs.config.config_options.Type(bool, default=False)),
6062
('password_button_text', mkdocs.config.config_options.Type(string_types, default=str(settings['password_button_text']))),
6163
('encrypted_something', mkdocs.config.config_options.Type(dict, default={})),
64+
('decrypt_search', mkdocs.config.config_options.Type(bool, default=False)),
6265
)
6366

6467
def __hash_md5__(self, text):
@@ -178,6 +181,11 @@ def on_pre_build(self, config):
178181
if 'encrypted_something' in plugin_config.keys():
179182
encrypted_something = self.config.get('encrypted_something')
180183
setattr(self, 'encrypted_something', encrypted_something)
184+
# Check if decrypt_search is enable: generate search_index.json on clear text (Data leak)
185+
setattr(self, 'decrypt_search', False)
186+
if 'decrypt_search' in plugin_config.keys():
187+
decrypt_search = self.config.get('decrypt_search')
188+
setattr(self, 'decrypt_search', decrypt_search)
181189

182190
def on_page_markdown(self, markdown, page, config, **kwargs):
183191
"""
@@ -226,27 +234,50 @@ def on_page_content(self, html, page, config, **kwargs):
226234
if self.tag_encrypted_page:
227235
# Set attribute on page to identify encrypted page on template rendering
228236
setattr(page, 'encrypted', True)
237+
if self.decrypt_search:
238+
# Keep encrypted html as temporary variable on page ... :(
239+
setattr(page, 'html_encrypted', self.__encrypt_content__(html))
240+
else:
241+
# Overwrite html with encrypted html, cause search it's encrypted too
242+
# Process encryption here, speed up mkdocs-search bultin plugin
243+
html = self.__encrypt_content__(html)
229244
if self.encrypted_something:
230245
# Set attributes on page to retrieve password on POST context
231246
setattr(page, 'password', self.password)
232-
html = self.__encrypt_content__(html)
233247
return html
234248

249+
def on_page_context(self, context, page, config, **kwargs):
250+
"""
251+
The page_context event is called after the context for a page is created and
252+
can be used to alter the context for that specific page only.
253+
254+
:param context: dict of template context variables
255+
:param page: mkdocs.nav.Page instance
256+
:param config: global configuration object
257+
:param nav: global navigation object
258+
:return: dict of template context variables
259+
"""
260+
if self.decrypt_search and page.content and hasattr(page, 'html_encrypted'):
261+
page.content = page.html_encrypted
262+
delattr(page, 'html_encrypted')
263+
return context
264+
235265
def on_post_page(self, output_content, page, config, **kwargs):
236266
"""
237267
The post_page event is called after the template is rendered,
238268
but before it is written to disc and can be used to alter the output of the page.
239269
If an empty string is returned, the page is skipped and nothing is written to disc.
270+
240271
:param output_content: output of rendered template as string
241272
:param page: mkdocs.nav.Page instance
242-
:param config: global configuration object
273+
:param config: global configuration object
243274
:return: output of rendered template as string
244275
"""
245276
# Limit this process only if encrypted_something feature is enable *(speedup)*
246277
if self.encrypted_something and hasattr(page, 'encrypted') and len(self.encrypted_something) > 0:
247278
soup = BeautifulSoup(output_content, 'html.parser')
248279
for name, tag in self.encrypted_something.items():
249-
#print({'name': name, 'html tag': tag[0], 'type': tag[1]})
280+
#logger.debug({'name': name, 'html tag': tag[0], 'type': tag[1]})
250281
something_search = soup.findAll(tag[0], { tag[1]: name })
251282
if something_search is not None and len(something_search) > 0:
252283
# Loop for multi child tags on target element
@@ -275,3 +306,13 @@ def on_post_page(self, output_content, page, config, **kwargs):
275306
item['style'] = "display:none"
276307
output_content = str(soup)
277308
return output_content
309+
310+
def on_post_build(self, config):
311+
"""
312+
The post_build event does not alter any variables.
313+
Use this event to call post-build scripts.
314+
315+
:param config: global configuration object
316+
"""
317+
318+

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def read(fname):
1111

1212
setup(
1313
name='mkdocs-encryptcontent-plugin',
14-
version='1.0.0',
14+
version='1.1.0',
1515
author='CoinK0in',
1616
author_email='[email protected]',
1717
description='A MkDocs plugin that encrypt/decrypt markdown content with AES',

0 commit comments

Comments
 (0)