@@ -84,6 +84,7 @@ class encryptContentPlugin(BasePlugin):
8484 ('html_extra_vars' , config_options .Type (dict , default = {})),
8585 ('js_template_path' , config_options .Type (string_types , default = str (os .path .join (PLUGIN_DIR , 'decrypt-contents.tpl.js' )))),
8686 ('js_extra_vars' , config_options .Type (dict , default = {})),
87+ ('canary_template_path' , config_options .Type (string_types , default = str (os .path .join (PLUGIN_DIR , 'canary.tpl.py' )))),
8788 # others features
8889 ('encrypted_something' , config_options .Type (dict , default = {})),
8990 ('search_index' , config_options .Choice (('clear' , 'dynamically' , 'encrypted' ), default = 'encrypted' )),
@@ -315,6 +316,9 @@ def on_config(self, config, **kwargs):
315316 logger .debug ('Load JS template from file: "{file}".' .format (file = str (self .config ['js_template_path' ])))
316317 with open (self .config ['js_template_path' ], 'r' ) as template_js :
317318 self .setup ['js_template' ] = template_js .read ()
319+ logger .debug ('Load canary template from file: "{file}".' .format (file = str (self .config ['canary_template_path' ])))
320+ with open (self .config ['canary_template_path' ], 'r' ) as template_html :
321+ self .setup ['canary_template' ] = template_html .read ()
318322
319323 # Check if hljs feature need to be enabled, based on theme configuration
320324 if ('highlightjs' in config ['theme' ]._vars
@@ -361,6 +365,8 @@ def on_config(self, config, **kwargs):
361365 # Get path to site in case of subdir in site_url
362366 self .setup ['site_path' ] = urlsplit (config .data ["site_url" ] or '/' ).path [1 ::]
363367
368+ self .setup ['config_path' ] = Path (config ['config_file_path' ]).parents [0 ]
369+
364370 self .setup ['search_plugin_found' ] = False
365371 encryptcontent_plugin_found = False
366372 for plugin in config ['plugins' ]:
@@ -421,8 +427,7 @@ def on_config(self, config, **kwargs):
421427 self .setup ['level_keystore' ][level ] = new_entry
422428
423429 if self .config ['sign_files' ]:
424- configpath = Path (config ['config_file_path' ]).parents [0 ]
425- sign_key_path = configpath .joinpath (self .config ['sign_key' ])
430+ sign_key_path = self .setup ['config_path' ].joinpath (self .config ['sign_key' ])
426431 if not exists (sign_key_path ):
427432 logger .info ('Generating signing key and saving to "{file}".' .format (file = str (self .config ['sign_key' ])))
428433 key = ECC .generate (curve = 'Ed25519' )
@@ -470,8 +475,7 @@ def on_pre_build(self, config, **kwargs):
470475 if self .config ['selfhost' ] and self .config ['selfhost_download' ]:
471476 logger .info ('Downloading cryptojs for self-hosting (if needed)...' )
472477 if self .config ['selfhost_dir' ]:
473- configpath = Path (config ['config_file_path' ]).parents [0 ]
474- dlpath = configpath .joinpath (self .config ['selfhost_dir' ] + '/assets/javascripts/cryptojs/' )
478+ dlpath = self .setup ['config_path' ].joinpath (self .config ['selfhost_dir' ] + '/assets/javascripts/cryptojs/' )
475479 else :
476480 dlpath = Path (config .data ['docs_dir' ] + '/assets/javascripts/cryptojs/' )
477481 dlpath .mkdir (parents = True , exist_ok = True )
@@ -774,7 +778,7 @@ def on_post_build(self, config, **kwargs):
774778 new_entry = {}
775779 if self .config ['selfhost' ]:
776780 new_entry ['file' ] = Path (config .data ["site_dir" ] + '/assets/javascripts/cryptojs/' + jsurl [0 ].rsplit ('/' ,1 )[1 ])
777- new_entry ['url' ] = config .data ["site_url" ] + '/ assets/javascripts/cryptojs/' + jsurl [0 ].rsplit ('/' ,1 )[1 ]
781+ new_entry ['url' ] = config .data ["site_url" ] + 'assets/javascripts/cryptojs/' + jsurl [0 ].rsplit ('/' ,1 )[1 ]
778782 else :
779783 new_entry ['file' ] = ""
780784 new_entry ['url' ] = "https:" + jsurl [0 ]
@@ -838,3 +842,12 @@ def on_post_build(self, config, **kwargs):
838842 sign_file_path = Path (config .data ["site_dir" ] + '/' + self .config ['sign_files' ])
839843 with open (sign_file_path , "w" ) as file :
840844 file .write (json .dumps (signatures ))
845+
846+ canary_file = self .setup ['config_path' ].joinpath ('canary.py' )
847+ if not exists (canary_file ):
848+ canary_py = Template (self .setup ['canary_template' ]).render ({
849+ 'public_key' : self .setup ['sign_key' ].public_key ().export_key (format = 'PEM' ),
850+ 'signature_url' : config .data ["site_url" ] + self .config ['sign_files' ]
851+ })
852+ with open (canary_file , 'w' ) as file :
853+ file .write (canary_py )
0 commit comments