ikiwiki: Validate a path when deleting wiki or blog

I tested that ikiwiki functional tests pass and running the command
`sudo ./actions/ikiwiki delete  --name '../'`
returns an error and does not delete any directory.

Signed-off-by: Veiko Aasa <veiko17@disroot.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Veiko Aasa authored and Sunil Mohan Adapa committed 2020-07-25 14:03:25 -07:00
1 parent f0000c4ba3
commit e2e3768fe1
1 file changed
+10
+10
View File
@@ -46,6 +46,11 @@ def parse_arguments():
return parser.parse_args()
def _is_safe_path(basedir, path):
"""Return whether a path is safe."""
return os.path.realpath(path).startswith(basedir)
def subcommand_setup(_):
"""Perform first time setup operations."""
setup()
@@ -106,6 +111,11 @@ def subcommand_delete(arguments):
html_folder = os.path.join(SITE_PATH, arguments.name)
wiki_folder = os.path.join(WIKI_PATH, arguments.name)
if not (_is_safe_path(SITE_PATH, html_folder)
and _is_safe_path(WIKI_PATH, wiki_folder)):
print('Error: {0} is not a correct name.'.format(arguments.name))
exit(1)
try:
shutil.rmtree(html_folder)
shutil.rmtree(wiki_folder)