Skip to content

Potential DoS attack when reading large containers #332

Open

Description

@rubensworks

I've setup the server over a directory with 309.978 very small N-Quads files (~10 triples per file).
The server seems to be unable to reply to requests to this container (files within it work fine).
The server also doesn't appear to give an error, or result in a timeout.

I've done some profiling in FileDataAccessor, and these are my findings:

  • fsPromises.readdir takes 541 ms. Given the number of files, this is a reasonable duration.
  • The for loop for generating metadata takes 28.045 s. This can probably be optimized. Maybe some parallelization can be done even using Promise.all.
  • Quad generation takes 1.187 s. Relatively fast, but maybe also optimizable if done in a streaming manner.
  • After this, I see warn: This writer can only write errors appearing in the console, and nothing happens anymore after that. Possibly something crashes silently, as I'm unable to see any further output (and my request is still hanging).

One could argue that this container is simply too large, so perhaps the server should have a default max limit when listing containers. Alternatively, container listing can be done in a streaming manner, which would probably require refactoring a bunch of components.

Activity

  1. jvherweg commented on Nov 18, 2020

    @jvherweg
    Member
    • After this, I see warn: This writer can only write errors appearing in the console, and nothing happens anymore after that. Possibly something crashes silently, as I'm unable to see any further output (and my request is still hanging).

    That probably means the BasicResponseWriter is crashing (that warning is from the canHandle function of the other response writer).

  2. RubenVerborgh commented on Nov 18, 2020

    @RubenVerborgh
    Member

    First of all, the logging:

    That probably means the BasicResponseWriter is crashing

    And if it does, we can no longer write the error to the HTTP stream.
    So any error at this point MUST be logged as an error, since there is no other way to see it.
    We do have an issue abut this: #6, so it needs to take that into consideration.

    One could argue that this container is simply too large, so perhaps the server should have a default max limit when listing containers.

    Agreed, we could bail on > 10,000 for now; later, it could be multi-paged.

    • fsPromises.readdir takes 541 ms. Given the number of files, this is a reasonable duration.

    Fair enough.

    • The for loop for generating metadata takes 28.045 s. This can probably be optimized. Maybe some parallelization can be done even using Promise.all.

    • Quad generation takes 1.187 s. Relatively fast, but maybe also optimizable if done in a streaming manner.

    The combination of the above is the problem: there should be no such thing as a generation phase.

    Rather, we should:

    • before (or in parallel with) readdir:
      • write :c a ldp:BasicContainer, ldp:Container; etc.
      • the rest of the container metadata as it arrives
    • immediately after readdir
      • for each file, write :c contains :f
    • asynchronously after readdir
      • for each file, get and write quad metadata

    That way, the response will be streaming. We should also listen for response stream close to stop generation.

    So yes, might still take 28s, but we will be streaming the response during all of that time, and the client can back out at any point.

    I would not parallelize, that could be an even bigger DDOS; with a linear loop, at least only the one request will be blocked.

  3. jvherweg commented on Nov 18, 2020

    @jvherweg
    Member

    First of all, the logging:

    Since the last update there should be loggin though if the stream crashes. The fact that this doesn't happen means something unexpected is breaking
    https://github.com/solid/community-server/blob/82f3aa0cd849417b25d56dc288999a4acc914036/src/ldp/http/BasicResponseWriter.ts#L37-L40

    That way, the response will be streaming. We should also listen for response stream close to stop generation.

    That will require some architectural rethinking since everything that is metadata is currently not stream and put in a RepresentationMetadata object. Some other functions also depend on this (e.g., the delete function using that metadata to check if a container is empty before deleting).

  4. RubenVerborgh commented on Nov 18, 2020

    @RubenVerborgh
    Member

    In fact, the response stream should likely be lazy here, such that we only read from disk when asked to write data.
    So container metadata and containment triples can be non-lazy (they are 2 calls for us), but reading the other files' metadata should be on demand.

    So we after we have piped our "header" (container metadata + containment triples), we should only write until false and wait for drain events on the response before continuing: https://nodejs.org/api/stream.html#stream_event_drain

  5. rubensworks commented on Nov 18, 2020

    @rubensworks
    ContributorAuthor

    Since the last update there should be loggin though if the stream crashes. The fact that this doesn't happen means something unexpected is breaking

    FYI, I am in sync with master, so something unexpected must indeed be breaking.

  6. RubenVerborgh commented on Nov 18, 2020

    @RubenVerborgh
    Member

    That will require some architectural rethinking since everything that is metadata is currently not stream and put in a RepresentationMetadata object.

    Maybe the problem is that we have put it in metadata in the first place. Maybe this should have been data with internal/quads (or internal/dir+quads) to begin with.

    Or even a stream internal/dirobjects of custom objects { type: 'container', …}, {type: 'filename', …}, {type: 'filedetails'}, …

  7. jvherweg commented on Nov 18, 2020

    @jvherweg
    Member

    We are also returning metadata of every resource in the container because that's what NSS does. Question is if we really need to do this or if the containment triples are sufficient? Or maybe we could have this as a setting?

  8. RubenVerborgh commented on Nov 18, 2020

    @RubenVerborgh
    Member

    Let's check the LDP spec on this indeed. I do know cases where all metadata is used (file browsers).

  9. RubenVerborgh commented on Nov 18, 2020

    @RubenVerborgh
    Member
  10. rubensworks commented on Jan 19, 2021

    @rubensworks
    ContributorAuthor

    FYI, I just ran into this again, and it looks like the BasicResponseWriter is not crashing anymore, so that's a good thing :-)
    Things also seem a bit faster, but still quite slow (and non-streaming).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions