这是indexloc提供的服务,不要输入任何密码
Skip to content

TOCTOU (time of check vs. time of use) issue in body stream consumption  #3271

@weissi

Description

@weissi

switch self.request.bodyStorage.withLockedValue({ $0 }) {
case .stream(let stream):
return stream.consume(max: max, on: self.request.eventLoop).map { buffer in
self.request.bodyStorage.withLockedValue({ $0 = .collected(buffer) })

The linked code has an issue. It's something like

    switch self.request.bodyStorage.withLockedValue({ $0 }) { // LOCK ACQUSITION
    case .stream(let stream): // ACT UPON value
        return stream.consume(max: max, on: self.request.eventLoop).map { buffer in
            self.request.bodyStorage.withLockedValue({ $0 = .collected(buffer) }) // LOCK RE-ACQUISITON & OVERWRITE

Any code that re-acquires locks and assumes that the state underneath hasn't changed isn't quite right.

If collect() is intended to only be run once, this should be documented. And to catch users doing a bad thing, it should probably be changed to something like

switch self.request.bodyStorage.withLockedValue({ value in let oldValue = value; value = .currentlyStreaming;  return oldValue })

and then you can throw an error if the user attempts to consume the body twice whilst it's streaming.

Most importantly, we should document exactly how collect() is supposed to be used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions