mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-24 04:38:00 +00:00
Motivation: When remote peers set the value of HEADER_TABLE_SIZE to any non-default value, or when we update it, we should respect that value when we're emitting headers. Unfortunately, right now we don't. In particular, we manage to update the wrong setting: when the peer sets HEADER_TABLE_SIZE we bound the maximum size of the dynamic table _they_ can use, not the maximum size _we_ can use. That's incorrect by the spec, which says: > This setting allows the sender to inform the remote endpoint of the > maximum size of the compression table used to decode field blocks, > in units of octets. The encoder can select any size equal to or less > than this value by using signaling specific to the compression > format inside a field block (see [COMPRESSION]). If a peer sent us SETTINGS_HEADER_TABLE_SIZE with a value of 0, we'd restrict _them_ to only using a maximum table size of 0, instead of us. This is wrong. Additionally, when we updated the encoder we used the wrong method, so the encoder would never send dynamic table size changes. This was also incorrect! In general this managed to get missed because, as RFC 9113 notes: > Implementers are advised that reducing the value of > SETTINGS_HEADER_TABLE_SIZE is not widely interoperable. Use of the > connection preface to reduce the value below the initial value of > 4,096 is somewhat better supported, but this might fail with some > implementations. It turns out one of those implementations this might fail with is us! The result, however, is that very few implementations actually try to reduce the value of the header table size, so we rarely ran into this issue. nginx does actually try this in some cases, however, which is how we found this problem. Modifications: - Correctly invert the application of the value of SETTINGS_HEADER_TABLE_SIZE - Correctly use `setDynamicTableSize` on HPACKEncoder instead of just setting the field manually. - Deprecate the invalid method of setting the field on HPACKEncoder. - Add some HTTP/2-level tests that ensure we correctly handle these changes. Result: Vastly better handling of peers reducing the value of SETTINGS_HEADER_TABLE_SIZE.
80 lines
3.5 KiB
Swift
80 lines
3.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2018 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// A protocol implemented by HTTP/2 connection state machine states with local settings.
|
|
///
|
|
/// This protocol provides implementations that can apply changes to the local settings.
|
|
protocol HasLocalSettings {
|
|
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
|
|
|
|
var localSettings: HTTP2SettingsState { get set }
|
|
|
|
var streamState: ConnectionStreamState { get set }
|
|
|
|
var inboundFlowControlWindow: HTTP2FlowControlWindow { get set }
|
|
}
|
|
|
|
extension HasLocalSettings {
|
|
mutating func receiveSettingsAck(frameDecoder: inout HTTP2FrameDecoder) -> StateMachineResultWithEffect {
|
|
// We do a little switcheroo here to avoid problems with overlapping accesses to
|
|
// self. It's a little more complex than normal because HTTP2SettingsState has
|
|
// two CoWable objects, and we don't want to CoW either of them, so we shove a dummy
|
|
// value in `self` to avoid that.
|
|
var temporarySettings = HTTP2SettingsState.dummyValue()
|
|
swap(&temporarySettings, &self.localSettings)
|
|
defer {
|
|
swap(&temporarySettings, &self.localSettings)
|
|
}
|
|
|
|
var effect = NIOHTTP2ConnectionStateChange.LocalSettingsChanged()
|
|
|
|
do {
|
|
try temporarySettings.receiveSettingsAck { (setting, originalValue, newValue) in
|
|
switch setting {
|
|
case .maxConcurrentStreams:
|
|
if self.role == .client {
|
|
self.streamState.maxServerInitiatedStreams = newValue
|
|
} else {
|
|
self.streamState.maxClientInitiatedStreams = newValue
|
|
}
|
|
case .headerTableSize:
|
|
frameDecoder.headerDecoder.maxDynamicTableLength = Int(newValue)
|
|
case .initialWindowSize:
|
|
// We default the value of SETTINGS_INITIAL_WINDOW_SIZE, so originalValue mustn't be nil.
|
|
// The max value of SETTINGS_INITIAL_WINDOW_SIZE is Int32.max, so we can safely fit it into that here.
|
|
let delta = Int32(newValue) - Int32(originalValue!)
|
|
|
|
try self.streamState.forAllStreams {
|
|
try $0.localInitialWindowSizeChanged(by: delta)
|
|
}
|
|
|
|
// We do a += here because the value may change multiple times in one settings block. This way, we correctly
|
|
// respect that possibility.
|
|
effect.streamWindowSizeChange += Int(delta)
|
|
case .maxFrameSize:
|
|
effect.newMaxFrameSize = newValue
|
|
case .maxHeaderListSize:
|
|
effect.newMaxHeaderListSize = newValue
|
|
default:
|
|
// No operation required
|
|
return
|
|
}
|
|
}
|
|
return .init(result: .succeed, effect: .localSettingsChanged(effect))
|
|
} catch {
|
|
return .init(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
|
|
}
|
|
}
|
|
}
|