Opened 3 weeks ago

Closed 2 weeks ago

#36768 closed Cleanup/optimization (fixed)

Repetitive string concatentation (in a loop) in File.__iter__

Reported by: wooseokdotkim Owned by: Varun Kasyap Pentamaraju
Component: File uploads/storage Version: dev
Severity: Normal Keywords: concatenation
Cc: wooseokdotkim Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

I thought the code below could generate DoS, so I made a bug report
However, File._iter__() was not recognized as a bug because only one line was buffered and only worked for chunks returned from File.chunks, but it was determined that verification code for input should be added, so it was created as an open ticket.

The code pattern is similar to CVE-2023-36053, which is already released, so I think it needs to be modified.

code: django/core/files/base.py:89

def __iter__(self):
    buffer_ = None
    for chunk in self.chunks():
        for line in chunk.splitlines(True):
            if buffer_:
                line = buffer_ + line  # < Code!

How should I patch it?

Change History (9)

comment:1 by wooseokdotkim, 3 weeks ago

Description: modified (diff)

comment:2 by Pravin, 3 weeks ago

Cc: Pravin added

comment:3 by Tim Graham, 3 weeks ago

Component: UncategorizedFile uploads/storage
Description: modified (diff)

comment:4 by Jacob Walls, 3 weeks ago

Keywords: concatenation added; DoS removed
Summary: File.__iter__() Quadratic-time DoSRepetitive string concatentation (in a loop) in File.__iter__
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Thanks for the follow-up.

How should I patch it?

You can just collect and join the strings instead of concatenating them during a loop.

In general, we won't audit the entire project for this pattern, but the Security Team's rationale for directing the reporter to Trac was that we did have a PoC of a degradation in hand, even if it was outside the bounds of what we consider a security issue.

If you'd like to submit a PR, please set yourself in the owner field. Thanks!

comment:5 by Pravin, 3 weeks ago

Can I work on this ?

comment:6 by Varun Kasyap Pentamaraju, 3 weeks ago

Has patch: set
Owner: set to Varun Kasyap Pentamaraju
Status: newassigned

comment:7 by Pravin, 3 weeks ago

Cc: Pravin removed

comment:8 by Jacob Walls, 3 weeks ago

Triage Stage: AcceptedReady for checkin
Version: dev

comment:9 by Jacob Walls <jacobtylerwalls@…>, 2 weeks ago

Resolution: fixed
Status: assignedclosed

In 3ccef1d:

Fixed #36768 -- Optimized string concatenation in File.iter().

Note: See TracTickets for help on using tickets.
Back to Top