Skip to content

xml.etree.ElementTree accepts control chars. #63050

Closed

Description

@mmaker
mannequin
BPO 18850
Nosy @scoder, @tiran, @bitdancer, @mmaker, @serhiy-storchaka
Superseder
  • bpo-5166: ElementTree and minidom don't prevent creation of not well-formed XML
  • Files
  • test.py
  • inject.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-09-02.21:19:38.862>
    created_at = <Date 2013-08-27.09:50:51.437>
    labels = ['expert-XML', 'type-feature', 'library']
    title = 'xml.etree.ElementTree accepts control chars.'
    updated_at = <Date 2013-09-02.21:19:38.861>
    user = 'https://github.com/mmaker'

    bugs.python.org fields:

    activity = <Date 2013-09-02.21:19:38.861>
    actor = 'eli.bendersky'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-09-02.21:19:38.862>
    closer = 'eli.bendersky'
    components = ['Library (Lib)', 'XML']
    creation = <Date 2013-08-27.09:50:51.437>
    creator = 'maker'
    dependencies = []
    files = ['31482', '31484']
    hgrepos = []
    issue_num = 18850
    keywords = []
    message_count = 30.0
    messages = ['196271', '196277', '196278', '196280', '196284', '196286', '196288', '196294', '196296', '196298', '196313', '196315', '196328', '196360', '196361', '196362', '196364', '196365', '196366', '196367', '196368', '196369', '196373', '196379', '196441', '196488', '196510', '196756', '196797', '196808']
    nosy_count = 7.0
    nosy_names = ['mmokrejs', 'scoder', 'christian.heimes', 'r.david.murray', 'eli.bendersky', 'maker', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '5166'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue18850'
    versions = ['Python 3.4']

    Activity

    1. mmaker commented on Aug 27, 2013

      mmakermannequin
      MannequinAuthor

      Got from irc;
      <JerryKwan> python bug in xml.etree.ElementTree, from version 2.7 to 3.2 http://www.reddit.com/r/Python/comments/1l6cta/python_bug_in_xmletreeelementtree/

      I think we should keep consistency with lxml and forbid control chars in advance.
      <https://en.wikipedia.org/wiki/Valid_characters_in_XML#XML_1.1\>

      Attaching the test file proving the issue, slightly modified to work on 2.7 and 3.x

    2. added
      stdlib
      type-bug
      on Aug 27, 2013
    3. scoder commented on Aug 27, 2013

      @scoder
      Contributor

      This is a bit tricky in ET because it generally allows you to stick anything into the Element properties (and that's a feature). So catching this at tree building time (as lxml.etree does) isn't really possible.

      However, at least catching it in the serialiser should be possible and would help. Regexes for well-formed tag names and text could do the job.

    4. mmaker commented on Aug 27, 2013

      mmakermannequin
      MannequinAuthor

      you mind if I try by myself to provide patch and unittest in the next few days?

    5. scoder commented on Aug 27, 2013

      @scoder
      Contributor

      Go for it. That's usually the fastest way to get things done.

    6. bitdancer commented on Aug 27, 2013

      @bitdancer
      Member

      Unless it is a security issue, this seems like the kind of fix that shouldn't be applied to maintenance releases.

    7. mmaker commented on Aug 27, 2013

      mmakermannequin
      MannequinAuthor

      I suppose it is, David, if in 2 minutes flat I can change your terminal name.

    8. bitdancer commented on Aug 27, 2013

      @bitdancer
      Member

      In that case, the fix needs to be applied to 3.2 and 2.6 as well. Or at least considered for application. It could be that this will break working (though dangerous) programs. I'll leave it to folks more knowledgeable in this particular area than I to decide where the severity/backward compatibility line lies in this case.

    9. added
      type-security
      and removed
      type-bug
      on Aug 27, 2013
    10. serhiy-storchaka commented on Aug 27, 2013

      @serhiy-storchaka
      Member

      See also bpo-7727. Almost any other XML generation code (xml.sax.sautils.XMLGenerator, xml.dom.minidom.Element.writexml(), etc, but not plistlib.PlistWriter) has the same problem.

      The problem with filtering control characters is that it will significantly slowdown XML generation.

    11. scoder commented on Aug 27, 2013

      @scoder
      Contributor

      Michele, could you elaborate how you would exploit this issue as a security risk?

      I mean, I can easily create a (non-)XML-document with control characters manually, and the parser would reject it.

      What part of the create-to-serialise process exactly is a problem here?

    12. mmaker commented on Aug 27, 2013

      mmakermannequin
      MannequinAuthor

      Michele, could you elaborate how you would exploit this issue as a security risk?
      Sure. What I meant in my message is: assume you have a script that simply stores each message it receives (from stdin, from a tcp stream, whatever) inside an xml tree like
      '<text>{message1}</text><text>{message2}<text>',
      and prints the tree on SIGINT.

      What I would expect is the xml document not to allow control chars, as "restricted and discouraged", and consistent with lxml.
      What instead happens is that the control chars are not handled, and thus anybody can send control chars in my terminal. Changing the terminal title is a trivial example of those.

      For sure an echo server may have the same issue, but the premises are different, because I expect to print just a byte stream.
      Mentioning this fact in the documentation may be a possible solution, but I believe more that keeping consistency with lxml is the right way.

      I mean, I can easily create a (non-)XML-document with control characters manually, and the parser would reject it.
      False? The parser is *not* rejecting control chars.

      What part of the create-to-serialise process exactly is a problem here?
      ElementTree.tostring().

    13. 9 remaining items

    14. scoder commented on Aug 28, 2013

      @scoder
      Contributor

      > XML is *defined* as a stream of bytes.
      Can you *paste* the *source* proving what you are arguing, please?

      http://www.w3.org/TR/REC-xml/

      python3 works with ElementTree(bytes(unicode))

      What does this sentence mean?

    15. mmaker commented on Aug 28, 2013

      mmakermannequin
      MannequinAuthor
      >>> XML is *defined* as a stream of bytes.
      >> Can you *paste* the *source* proving what you are arguing, please?
      > http://www.w3.org/TR/REC-xml/
      """
      The first two suggestions are directly derived from the rules given for identifiers in Standard Annex #31 (UAX #31) of the Unicode Standard, version 5.0 [Unicode], and exclude all control characters, enclosing nonspacing marks, non-decimal numbers, private-use characters, punctuation characters (with the noted exceptions), symbol characters, unassigned codepoints, and white space characters. The other suggestions are mostly derived from Appendix B in previous editions of this specification.
      """
    16. mmaker commented on Aug 28, 2013

      mmakermannequin
      MannequinAuthor

      """
      Document authors are encouraged to avoid "compatibility characters", as defined in section 2.3 of [Unicode]. The characters defined in the following ranges are also discouraged. They are either control characters or permanently undefined Unicode characters:
      """

    17. mmaker commented on Aug 28, 2013

      mmakermannequin
      MannequinAuthor

      Does not seem to me just a "byte string" where you can put binary data.
      Hence, I expect the xml tree to escape/reject those.
      Hence, Is not an enhancement, but a bug. Unless we just want to document this.

      (not going to change the metadata, otherwise we'll end up changing on each message).

    18. scoder commented on Aug 28, 2013

      @scoder
      Contributor

      We are talking about two different things here.

      I said that (serialised) XML is defined as a sequence of bytes. Read the spec on that.

      What you are talking about is the Infoset, or the parsed/generated in-memory XML tree. That's obviously not bytes, it's defined based on Unicode. Parsing and serialising does the mapping here.

      The "attack" that you presented is based on serialised XML, thus on a sequence of bytes. What I am saying is that this "attack" can be done by any kind of binary data, so it's not XML specific, thus not a problem with ElementTree.

      The fact that ElementTree allows you to generate non well-formed 'XML' containing control characters when you tell it to do so is unfortunate, but it's neither a security risk (you already had the non well-formed content in your hands *before* you passed it into ElementTree), nor clearly a bug, because the user specifically requested the serialisation of an in-memory tree that contained these control characters.

      But, once again, it would be nice if ElementTree rejected this input in one way or another, and that's a feature request.

    19. mmaker commented on Aug 28, 2013

      mmakermannequin
      MannequinAuthor

      I said that (serialised) XML is defined as a sequence of bytes.
      Read the spec on that.
      And I'm saying that's inexact. I have expectations that control chars are escaped in the serialized xml, because the spec I'm reading says so, and because the documentation does not warn me about this.

    20. mmaker commented on Aug 28, 2013

      mmakermannequin
      MannequinAuthor

      Just pointed by a friend - <http://www.w3.org/TR/REC-xml/#sec-cdata-sect\>
      I suppose this is insanely used to put binary blobs inside xml until "only the CDEnd string is recognized as markup".
      That's what I needed.

      Amen.

    21. scoder commented on Aug 29, 2013

      @scoder
      Contributor

      Is that you actual use case? That you *want* to store binary data in XML, instead of getting it properly rejected as non well-formed content?

      Then I suggest going the canonical route of passing it through base64 first, or any of the other binary-to-characters encoding.

    22. mmaker commented on Aug 29, 2013

      mmakermannequin
      MannequinAuthor

      Is that you actual use case? That you *want* to store binary data in XML, instead of getting it properly rejected as non well-formed content?
      No, Stefan.

      What I was saying in my last message was just "you're right, the user shall always use repr() when printing an xml tree" (msg196313) because "xml does *not* guarantee to have only printable chars by itself" (msg196368, msg196379).

      As an advice I hope you do not take as insult, saying
      "in section {section} the spec says {argument}"
      is much more constructive than
      "read the spec on that", "{extremely_obvious_link}",
      at least to people not familiar with the spec and asking for the source of your arguments (msg196360). Can shorten threads, too.

    23. scoder commented on Aug 30, 2013

      @scoder
      Contributor

      As an advice I hope you do not take as insult, saying
      "in section {section} the spec says {argument}"
      is much more constructive than
      "read the spec on that", "{extremely_obvious_link}",
      at least to people not familiar with the spec and asking for the source > of your arguments (msg196360). Can shorten threads, too.

      No harm done. The reason why I just posted the spec URL is that it's actually the entire spec that backs the argument. XML is (essentially) specified as a mapping from a sequence of bytes to a hierarchical structure (and back again). That's why there is an XML declaration header that names the encoding, for example. It wouldn't be needed if XML was defined as Unicode data.

    24. elibendersky commented on Sep 1, 2013

      elibenderskymannequin
      Mannequin

      Can this be transformed into a new issue that succinctly summarizes what the new requested feature is, and why it's useful?

    25. serhiy-storchaka commented on Sep 2, 2013

      @serhiy-storchaka
      Member

      Isn't this a duplicate of bpo-5166?

    26. elibendersky commented on Sep 2, 2013

      elibenderskymannequin
      Mannequin

      As Serhiy points out, this is a duplicate of bpo-5166

    27. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibtopic-XMLtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions