`set_key` does not handle single quote `'` inside value properly
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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()
- Lingua principale
- Python
- Stelle
- 8.9k
- Fork
- 581
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di theskumar/python-dotenv
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
theskumar/python-dotenv#699 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
theskumar/python-dotenv#697 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 28/100
theskumar/python-dotenv#693 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
theskumar/python-dotenv#683 · 4 commenti ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
theskumar/python-dotenv#644 · 2 commenti ·
Tutte le issue di theskumar/python-dotenv
Issue simili
-
area: harness bug status: needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Human-Agent-Society/reef#625 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 80/100
learningequality/kolibri#15351 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Name consistency Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
eellak/triplestore#65 · 1 commento ·