From 6deb6f1f14b72cad6dacd1fdeb7b73974acd773e Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Wed, 17 Mar 2021 16:32:11 +0100 Subject: [PATCH 1/2] Properly manage folder/file permissions --- scripts/install | 44 +++++++++++++++++++++++++------------------- scripts/restore | 32 ++++++++++++++++++-------------- scripts/upgrade | 33 +++++++++++++++++++++++++-------- 3 files changed, 68 insertions(+), 41 deletions(-) diff --git a/scripts/install b/scripts/install index 9ae52c7..55e3e8c 100755 --- a/scripts/install +++ b/scripts/install @@ -113,6 +113,14 @@ ynh_script_progression --message="Installing dependencies..." --time --weight=1 ynh_install_app_dependencies $pkg_dependencies +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Configuring system user..." --time --weight=1 + +# Create a system user +ynh_system_user_create --username=$app --home_dir="$final_path" + #================================================= # CREATE A MYSQL DATABASE #================================================= @@ -145,6 +153,17 @@ ynh_app_setting_set --app=$app --key=final_path --value=$final_path # Download, check integrity, uncompress and patch the source from app.src ynh_setup_source --dest_dir="$final_path" +# FIXME: this should be managed by the core in the future +# Here, as a packager, you may have to tweak the ownerhsip/permissions +# such that the appropriate users (e.g. maybe www-data) can access +# files in some cases. +# But FOR THE LOVE OF GOD, do not allow r/x for "others" on the entire folder - +# this will be treated as a security issue. +chmod 750 "$final_path" +chmod -R o-rwx "$final_path" +chown -R root: "$final_path" +chown root:$app "$final_path" + #================================================= # NGINX CONFIGURATION #================================================= @@ -155,14 +174,6 @@ ynh_script_progression --message="Configuring NGINX web server..." --time --weig # Create a dedicated NGINX config ynh_add_nginx_config -#================================================= -# CREATE DEDICATED USER -#================================================= -ynh_script_progression --message="Configuring system user..." --time --weight=1 - -# Create a system user -ynh_system_user_create --username=$app - #================================================= # PHP-FPM CONFIGURATION #================================================= @@ -246,6 +257,12 @@ ynh_permission_update --permission="main" --remove="visitors" ynh_add_config --template="some_config_file" --destination="$final_path/some_config_file" +# FIXME: this should be handled by the core in the future +# You may need to use chmod 600 instead of 400, +# for example if the app is expected to be able to modify its own config +chmod 400 +chown $app:$app "$final_path/some_config_file" + ### For more complex cases where you want to replace stuff using regexes, ### you shoud rely on ynh_replace_string (which is basically a wrapper for sed) ### When doing so, you also need to manually call ynh_store_file_checksum @@ -255,17 +272,6 @@ ynh_add_config --template="some_config_file" --destination="$final_path/some_con #================================================= # GENERIC FINALIZATION -#================================================= -# SECURE FILES AND DIRECTORIES -#================================================= - -### For security reason, any app should set the permissions to root: before anything else. -### Then, if write authorization is needed, any access should be given only to directories -### that really need such authorization. - -# Set permissions to app files -chown -R root: $final_path - #================================================= # SETUP LOGROTATE #================================================= diff --git a/scripts/restore b/scripts/restore index c334a77..7e635ce 100755 --- a/scripts/restore +++ b/scripts/restore @@ -53,6 +53,14 @@ test ! -d $final_path \ ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" +#================================================= +# RECREATE THE DEDICATED USER +#================================================= +ynh_script_progression --message="Recreating the dedicated system user..." --time --weight=1 + +# Create the dedicated user (if not existing) +ynh_system_user_create --username=$app --home_dir="$final_path" + #================================================= # RESTORE THE APP MAIN DIR #================================================= @@ -60,20 +68,16 @@ ynh_script_progression --message="Restoring the app main directory..." --time -- ynh_restore_file --origin_path="$final_path" -#================================================= -# RECREATE THE DEDICATED USER -#================================================= -ynh_script_progression --message="Recreating the dedicated system user..." --time --weight=1 - -# Create the dedicated user (if not existing) -ynh_system_user_create --username=$app - -#================================================= -# RESTORE USER RIGHTS -#================================================= - -# Restore permissions on app files -chown -R root: $final_path +# FIXME: this should be managed by the core in the future +# Here, as a packager, you may have to tweak the ownerhsip/permissions +# such that the appropriate users (e.g. maybe www-data) can access +# files in some cases. +# But FOR THE LOVE OF GOD, do not allow r/x for "others" on the entire folder - +# this will be treated as a security issue. +chmod 750 "$final_path" +chmod -R o-rwx "$final_path" +chown -R root: "$final_path" +chown root:$app "$final_path" #================================================= # RESTORE THE PHP-FPM CONFIGURATION diff --git a/scripts/upgrade b/scripts/upgrade index a749b70..b2cf9ef 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -101,6 +101,14 @@ ynh_script_progression --message="Stopping a systemd service..." --time --weight ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Making sure dedicated system user exists..." --time --weight=1 + +# Create a dedicated user (if not existing) +ynh_system_user_create --username=$app --home_dir="$final_path" + #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= @@ -113,6 +121,17 @@ then ynh_setup_source --dest_dir="$final_path" fi +# FIXME: this should be managed by the core in the future +# Here, as a packager, you may have to tweak the ownerhsip/permissions +# such that the appropriate users (e.g. maybe www-data) can access +# files in some cases. +# But FOR THE LOVE OF GOD, do not allow r/x for "others" on the entire folder - +# this will be treated as a security issue. +chmod 750 "$final_path" +chmod -R o-rwx "$final_path" +chown -R root: "$final_path" +chown root:$app "$final_path" + #================================================= # NGINX CONFIGURATION #================================================= @@ -128,14 +147,6 @@ ynh_script_progression --message="Upgrading dependencies..." --time --weight=1 ynh_install_app_dependencies $pkg_dependencies -#================================================= -# CREATE DEDICATED USER -#================================================= -ynh_script_progression --message="Making sure dedicated system user exists..." --time --weight=1 - -# Create a dedicated user (if not existing) -ynh_system_user_create --username=$app - #================================================= # PHP-FPM CONFIGURATION #================================================= @@ -169,6 +180,12 @@ ynh_add_systemd_config ynh_add_config --template="some_config_file" --destination="$final_path/some_config_file" +# FIXME: this should be handled by the core in the future +# You may need to use chmod 600 instead of 400, +# for example if the app is expected to be able to modify its own config +chmod 400 +chown $app:$app "$final_path/some_config_file" + ### For more complex cases where you want to replace stuff using regexes, ### you shoud rely on ynh_replace_string (which is basically a wrapper for sed) ### When doing so, you also need to manually call ynh_store_file_checksum From b1a9e7846607e788dc0ff1949d20795904ca8c52 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Wed, 17 Mar 2021 21:37:29 +0100 Subject: [PATCH 2/2] Typo: forgot target in chmod Co-authored-by: yalh76 --- scripts/install | 2 +- scripts/upgrade | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/install b/scripts/install index 55e3e8c..632de21 100755 --- a/scripts/install +++ b/scripts/install @@ -260,7 +260,7 @@ ynh_add_config --template="some_config_file" --destination="$final_path/some_con # FIXME: this should be handled by the core in the future # You may need to use chmod 600 instead of 400, # for example if the app is expected to be able to modify its own config -chmod 400 +chmod 400 "$final_path/some_config_file" chown $app:$app "$final_path/some_config_file" ### For more complex cases where you want to replace stuff using regexes, diff --git a/scripts/upgrade b/scripts/upgrade index b2cf9ef..fd11950 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -183,7 +183,7 @@ ynh_add_config --template="some_config_file" --destination="$final_path/some_con # FIXME: this should be handled by the core in the future # You may need to use chmod 600 instead of 400, # for example if the app is expected to be able to modify its own config -chmod 400 +chmod 400 "$final_path/some_config_file" chown $app:$app "$final_path/some_config_file" ### For more complex cases where you want to replace stuff using regexes,