Files
swift-nio-http2/Sources/NIOHTTP2/ConnectionStateMachine/HasRemoteSettings.swift
T
Cory Benfield 860622124b Correctly respect changes to HEADER_TABLE_SIZE (#374)
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.
2023-02-24 13:20:51 +00:00

81 lines
3.7 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 remote settings.
///
/// This protocol provides implementations that can apply changes to the remote settings.
protocol HasRemoteSettings {
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
var remoteSettings: HTTP2SettingsState { get set }
var streamState: ConnectionStreamState { get set }
var outboundFlowControlWindow: HTTP2FlowControlWindow { get set }
}
extension HasRemoteSettings {
mutating func receiveSettingsChange(_ settings: HTTP2Settings, frameEncoder: inout HTTP2FrameEncoder) -> (StateMachineResultWithEffect, PostFrameOperation) {
// 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.remoteSettings)
defer {
swap(&temporarySettings, &self.remoteSettings)
}
var effect = NIOHTTP2ConnectionStateChange.RemoteSettingsChanged()
do {
try temporarySettings.receiveSettings(settings) { (setting, originalValue, newValue) in
switch setting {
case .maxConcurrentStreams:
if self.role == .client {
self.streamState.maxClientInitiatedStreams = newValue
} else {
self.streamState.maxServerInitiatedStreams = newValue
}
effect.newMaxConcurrentStreams = newValue
case .headerTableSize:
try frameEncoder.headerEncoder.setDynamicTableSize(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.remoteInitialWindowSizeChanged(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
default:
// No operation required
return
}
}
return (.init(result: .succeed, effect: .remoteSettingsChanged(effect)), .sendAck)
} catch let err where err is NIOHTTP2Errors.InvalidFlowControlWindowSize {
return (.init(result: .connectionError(underlyingError: err, type: .flowControlError), effect: nil), .nothing)
} catch {
preconditionFailure("Unexpected error thrown: \(error)")
}
}
}