`set_key` does not handle single quote `'` inside value properly
还没有人认领这个 Issue。
评估
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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()
- 主要语言
- Python
- 星标
- 8.9k
- 派生
- 581
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
theskumar/python-dotenv 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
theskumar/python-dotenv#699 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 35/100
theskumar/python-dotenv#697 ·
-
难度 5/5 一周以上 新手友好度 28/100
theskumar/python-dotenv#693 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 55/100
theskumar/python-dotenv#683 · 4 条评论 ·
-
难度 3/5 1-2 天 新手友好度 55/100
theskumar/python-dotenv#644 · 2 条评论 ·
查看 theskumar/python-dotenv 的全部 Issue
相似的 Issue
-
area: harness bug status: needs-triage
难度 2/5 1-3 小时 新手友好度 75/100
Human-Agent-Society/reef#625 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 1/5 1 小时以内 新手友好度 80/100
learningequality/kolibri#15351 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
Name consistency 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
eellak/triplestore#65 · 1 条评论 ·