Skip to content
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

Hash sign inside string value that includes quotes interpreted as comment #426

Open
bonderado opened this issue Aug 10, 2023 · 0 comments
Open
Labels
component: decoder Related to parsing in `toml.load` syntax: comments Related to comments syntax: strings Related to string literals type: bug A confirmed bug or unintended behavior

Comments

@bonderado
Copy link

When a string value is contains parts enclosed in quotes (e.g.: a XML tag with attributes), if the quoted part contains a '#', it can be considered as a comment. since the rest of the line is ignored, including other quotes, the value may be incomplete in multi line strings. if all is on one line the whole decoding may fail.

import toml
import tomllib

print(   toml.loads("""key = "'''before hash # after hash'''" """))
print(tomllib.loads("""key = "'''before hash # after hash'''" """))
print(   toml.loads("""key = '''"before hash # after hash"'''"""))
print(tomllib.loads("""key = '''"before hash # after hash"'''"""))
print(   toml.loads("""key = ''''before hash # after hash''''"""))
print(tomllib.loads("""key = ''''before hash # after hash''''"""))

output:

{'key': "'''before hash # after hash'''"}
{'key': "'''before hash # after hash'''"}
{'key': '"before hash # after hash"'}
{'key': '"before hash # after hash"'}
{}
{'key': "'before hash # after hash'"}

The case that let me discover this was storing a svg fragment:

import toml
src='''key = """
<mask id="b">
       <g filter="url(#a)">
           <rect fill-opacity="0.104" height="128" width="128"/>
       </g>
   </mask>
"""
'''
print(toml.loads(src))
print(tomllib.loads(src))

output:

{'key': '<mask id="b">\n       <g filter="url(     \n           <rect fill-opacity="0.104" height="128" width="128"/>\n       </g>\n   </mask>\n'}
{'key': '<mask id="b">\n       <g filter="url(#a)">\n           <rect fill-opacity="0.104" height="128" width="128"/>\n       </g>\n   </mask>\n'}
@pradyunsg pradyunsg added type: bug A confirmed bug or unintended behavior component: decoder Related to parsing in `toml.load` syntax: strings Related to string literals syntax: comments Related to comments labels Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: decoder Related to parsing in `toml.load` syntax: comments Related to comments syntax: strings Related to string literals type: bug A confirmed bug or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants