theskumar / theskumar/python-dotenv
`set_key` does not handle single quote `'` inside value properly
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 8.9k
- Forks
- 581
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
Problem
set_key function does not properly handle single quote ' inside value.
python-dotenv version: 1.0.1
Related PR
- https://github.com/theskumar/python-dotenv/pull/330 (https://github.com/theskumar/python-dotenv/pull/330#discussion_r669439596)
How to reproduce
- Run python script below
- Run
source .env
Python Script
from dotenv import set_key
def main():
var_value = "I'm a bug :("
set_key(".env", "VAR", var_value)
if __name__ == "__main__":
main()
Error from source .env
bash: .env: line 1: syntax error near unexpected token `('
bash: .env: line 1: `VAR='I\'m a bug :(''
Example of fix
⚠️ this fix produces values that dotenv cannot parse (see this issue) ⚠️
from dotenv import set_key
def fixed_set_key(file_path, key, value):
# properly escape ' if present
# https://stackoverflow.com/questions/8254120/how-can-i-escape-a-single-quote-in-a-single-quote-string-in-bash/26165123#26165123
value = value.replace("'", "'\"'\"'")
set_key(file_path, key, f"'{value}'", quote_mode="never")
def main():
var_value = "I'm a fixed :)"
fixed_set_key(".env", "VAR", var_value)
if __name__ == "__main__":
main()
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Start at the set_key entry point and reproduce the issue with the Python script in this report, then source the generated .env file in Bash. Trace how values are quoted and escaped, including the related PR discussion and warning about dotenv parsing. Done means a value containing a single quote is written in a form that both Bash can source and dotenv can parse correctly.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100