mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-25 04:47:50 +00:00
Motivation: Without knowing where an error was thrown it can be unnecessarily difficult to work out the reasons an application ended up in an error state. Modifications: - Add a 'file' string to each of the 'NIOHTTP2Errors' denoting the file and line where an error was created - Add static methods to create errors to 'NIOHTTP2Errors' defaulting file and line, this is unfortunately necessary as defaulting file and line in the `init` makes the defaulted and non-defaulted errors indistinguishable, and the compiler will pick the one with fewest defaulted values. - Add equatable conformance manually so as to ignore 'file' - Add backing storage to errors which no longer be stored in an existential container - Add 'CustomStringConvertible' conformance to the errors using backing storage so that the 'file' is actually visible when converting the error - Deprecate the old `init`s - Fix all deprecation warnings Result: - NIOHTTP2Errors carry a 'file' with them indicating where they were created
61 lines
2.3 KiB
Swift
61 lines
2.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
/// Implements some simple denial of service heuristics on inbound frames.
|
|
struct DOSHeuristics {
|
|
/// The number of "empty" (zero bytes of useful payload) DATA frames we've received since the
|
|
/// last useful frame.
|
|
///
|
|
/// We reset this count each time we see END_STREAM, or a HEADERS frame, both of which we count
|
|
/// as doing useful work. We have a small budget for these because we want to tolerate buggy
|
|
/// implementations that occasionally emit empty DATA frames, but don't want to drown in them.
|
|
private var receivedEmptyDataFrames: Int
|
|
|
|
/// The maximum number of "empty" data frames we're willing to tolerate.
|
|
private let maximumSequentialEmptyDataFrames: Int
|
|
|
|
internal init(maximumSequentialEmptyDataFrames: Int) {
|
|
precondition(maximumSequentialEmptyDataFrames >= 0,
|
|
"maximum sequential empty data frames must be positive, got \(maximumSequentialEmptyDataFrames)")
|
|
self.maximumSequentialEmptyDataFrames = maximumSequentialEmptyDataFrames
|
|
self.receivedEmptyDataFrames = 0
|
|
}
|
|
}
|
|
|
|
|
|
extension DOSHeuristics {
|
|
mutating func process(_ frame: HTTP2Frame) throws {
|
|
switch frame.payload {
|
|
case .data(let payload):
|
|
if payload.data.readableBytes == 0 {
|
|
self.receivedEmptyDataFrames += 1
|
|
}
|
|
|
|
if payload.endStream {
|
|
self.receivedEmptyDataFrames = 0
|
|
}
|
|
case .headers:
|
|
self.receivedEmptyDataFrames = 0
|
|
case .alternativeService, .goAway, .origin, .ping, .priority, .pushPromise, .rstStream, .settings, .windowUpdate:
|
|
// Currently we don't assess these for DoS risk.
|
|
()
|
|
}
|
|
|
|
if self.receivedEmptyDataFrames > self.maximumSequentialEmptyDataFrames {
|
|
throw NIOHTTP2Errors.excessiveEmptyDataFrames()
|
|
}
|
|
}
|
|
}
|