Skip to content

Escape special characters in DB_PASS environment variable when substituting with sed #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2021

Conversation

Kangie
Copy link
Contributor

@Kangie Kangie commented Jun 29, 2021

linuxserver.io


  • I have read the contributing guideline and understand that I have made the correct modifications

Description:

Escape special characters in DB_PASSWORD environment variable before they're used in a sed substitution.

Benefits of this PR and context:

A DB_PASS variable containing any of the following characters will not be output verbatim in a sed replacement string:

  • /
  • \
  • &
  • $

Causing Bookstack initialisations to fail as the DB and Web App are provided with different passwords.

How Has This Been Tested?

Tested in Bash/Zsh:

Old Behaviour:

matt@matt-pc ~/ $ echo 'DB_PASSWORD=database_user_password' > regex_test.env
matt@matt-pc ~/ $ cat regex_test.env 
DB_PASSWORD=database_user_password
matt@matt-pc ~/ $ sed -i "s/DB_PASSWORD=database_user_password/DB_PASSWORD=${DB_PASS}/g" regex_test.env
matt@matt-pc ~/ $ cat regex_test.env 
DB_PASSWORD=!UX$vzT!2!w6r3sEb2XXFWDB_PASSWORD=database_user_passwordY2byHc

New Behaviour:

matt@laptop ~/ $ DB_PASS='!UX$vzT!2!w6r3sEb2XXFW&Y2byHc'
matt@laptop ~/ $ ESCAPED_PASSWORD=$(sed -e 's/[$\/&]/\\&/g' <<< $DB_PASS)
matt@laptop ~/ $ echo $ESCAPED_PASSWORD                   
!UX\$vzT!2!w6r3sEb2XXFW\&Y2byHc
matt@laptop ~/ $ echo 'DB_PASSWORD=database_user_password' > regex_test.env 
matt@laptop ~/ $ cat regex_test.env                  
DB_PASSWORD=database_user_password
matt@laptop ~/ $ sed -i "s/DB_PASSWORD=database_user_password/DB_PASSWORD=${ESCAPED_PASSWORD}/g" regex_test.env 
matt@laptop ~/ $ cat regex_test.env 
DB_PASSWORD=!UX$vzT!2!w6r3sEb2XXFW&Y2byHc

Source / References:

Fixes #100

substituting with sed

Characters:
  - /
  - \
  - $
  - &
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this pull request! Be sure to follow the pull request template!

@LinuxServer-CI
Copy link
Collaborator

Copy link
Member

@thespad thespad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thespad thespad merged commit f6fb87f into linuxserver:master Jul 9, 2021
@Kangie Kangie deleted the sed-substitution branch October 16, 2021 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generation of .env file from environment variables fails to replace password properly
3 participants