1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/spdy/alps_decoder.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "net/base/features.h"
#include "net/base/hex_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::spdy::AcceptChOriginValuePair;
using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Pair;
namespace net {
namespace {
TEST(AlpsDecoderTest, EmptyInput) {
AlpsDecoder decoder;
EXPECT_THAT(decoder.GetAcceptCh(), IsEmpty());
EXPECT_THAT(decoder.GetSettings(), IsEmpty());
EXPECT_EQ(0, decoder.settings_frame_count());
AlpsDecoder::Error error = decoder.Decode({});
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_THAT(decoder.GetAcceptCh(), IsEmpty());
EXPECT_THAT(decoder.GetSettings(), IsEmpty());
EXPECT_EQ(0, decoder.settings_frame_count());
}
TEST(AlpsDecoderTest, EmptyAcceptChFrame) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("000000" // length
"89" // type ACCEPT_CH
"00" // flags
"00000000")); // stream ID
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_THAT(decoder.GetAcceptCh(), IsEmpty());
EXPECT_THAT(decoder.GetSettings(), IsEmpty());
EXPECT_EQ(0, decoder.settings_frame_count());
}
TEST(AlpsDecoderTest, EmptySettingsFrame) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("000000" // length
"04" // type SETTINGS
"00" // flags
"00000000")); // stream ID
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_THAT(decoder.GetAcceptCh(), IsEmpty());
EXPECT_THAT(decoder.GetSettings(), IsEmpty());
EXPECT_EQ(1, decoder.settings_frame_count());
}
TEST(AlpsDecoderTest, ParseSettingsAndAcceptChFrames) {
AlpsDecoder decoder;
AlpsDecoder::Error error = decoder.Decode(HexDecode(
// ACCEPT_CH frame
"00003d" // length
"89" // type ACCEPT_CH
"00" // flags
"00000000" // stream ID
"0017" // origin length
"68747470733a2f2f7777772e" //
"6578616d706c652e636f6d" // origin "https://www.example.com"
"0003" // value length
"666f6f" // value "foo"
"0018" // origin length
"68747470733a2f2f6d61696c" //
"2e6578616d706c652e636f6d" // origin "https://mail.example.com"
"0003" // value length
"626172" // value "bar"
// SETTINGS frame
"00000c" // length
"04" // type
"00" // flags
"00000000" // stream ID
"0dab" // identifier
"01020304" // value
"1234" // identifier
"fedcba98")); // value
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_THAT(
decoder.GetAcceptCh(),
ElementsAre(AcceptChOriginValuePair{"https://www.example.com", "foo"},
AcceptChOriginValuePair{"https://mail.example.com", "bar"}));
EXPECT_THAT(decoder.GetSettings(),
ElementsAre(Pair(0x0dab, 0x01020304), Pair(0x1234, 0xfedcba98)));
EXPECT_EQ(1, decoder.settings_frame_count());
}
TEST(AlpsDecoderTest, ParseLargeAcceptChFrame) {
std::string frame = HexDecode(
// ACCEPT_CH frame
"0001ab" // length: 427 total bytes
"89" // type ACCEPT_CH
"00" // flags
"00000000" // stream ID
"0017" // origin length
"68747470733a2f2f7777772e" //
"6578616d706c652e636f6d" // origin "https://www.example.com"
"0190" // value length (400 in hex)
);
// The Accept-CH tokens payload is a string of 400 'x' characters.
const std::string accept_ch_tokens(400, 'x');
// Append the value bytes to the frame.
frame += accept_ch_tokens;
AlpsDecoder decoder;
AlpsDecoder::Error error = decoder.Decode(frame);
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_THAT(decoder.GetAcceptCh(),
ElementsAre(AcceptChOriginValuePair{"https://www.example.com",
accept_ch_tokens}));
}
TEST(AlpsDecoderTest, DisableAlpsParsing) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(features::kAlpsParsing);
AlpsDecoder decoder;
AlpsDecoder::Error error = decoder.Decode(HexDecode(
// ACCEPT_CH frame
"00003d" // length
"89" // type ACCEPT_CH
"00" // flags
"00000000" // stream ID
"0017" // origin length
"68747470733a2f2f7777772e" //
"6578616d706c652e636f6d" // origin "https://www.example.com"
"0003" // value length
"666f6f" // value "foo"
"0018" // origin length
"68747470733a2f2f6d61696c" //
"2e6578616d706c652e636f6d" // origin "https://mail.example.com"
"0003" // value length
"626172" // value "bar"
));
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_THAT(decoder.GetAcceptCh(), IsEmpty());
}
TEST(AlpsDecoderTest, DisableAlpsClientHintParsing) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(features::kAlpsClientHintParsing);
AlpsDecoder decoder;
AlpsDecoder::Error error = decoder.Decode(HexDecode(
// ACCEPT_CH frame
"00003d" // length
"89" // type ACCEPT_CH
"00" // flags
"00000000" // stream ID
"0017" // origin length
"68747470733a2f2f7777772e" //
"6578616d706c652e636f6d" // origin "https://www.example.com"
"0003" // value length
"666f6f" // value "foo"
"0018" // origin length
"68747470733a2f2f6d61696c" //
"2e6578616d706c652e636f6d" // origin "https://mail.example.com"
"0003" // value length
"626172" // value "bar"
));
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_THAT(decoder.GetAcceptCh(), IsEmpty());
}
TEST(AlpsDecoderTest, IncompleteFrame) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("00000c" // length
"04" // type
"00" // flags
"00000000" // stream ID
"0dab" // identifier
"01")); // first byte of value
EXPECT_EQ(AlpsDecoder::Error::kNotOnFrameBoundary, error);
}
TEST(AlpsDecoderTest, TwoSettingsFrames) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("000006" // length
"04" // type SETTINGS
"00" // flags
"00000000" // stream ID
"0dab" // identifier
"01020304" // value
"000006" // length
"04" // type SETTINGS
"00" // flags
"00000000" // stream ID
"1234" // identifier
"fedcba98")); // value
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_EQ(2, decoder.settings_frame_count());
EXPECT_THAT(decoder.GetSettings(),
ElementsAre(Pair(0x0dab, 0x01020304), Pair(0x1234, 0xfedcba98)));
}
TEST(AlpsDecoderTest, AcceptChOnInvalidStream) {
AlpsDecoder decoder;
AlpsDecoder::Error error = decoder.Decode(
HexDecode("00001e" // length
"89" // type ACCEPT_CH
"00" // flags
"00000001" // invalid stream ID: should be zero
"0017" // origin length
"68747470733a2f2f7777772e" //
"6578616d706c652e636f6d" // origin "https://www.example.com"
"0003" // value length
"666f6f")); // value "foo"
EXPECT_EQ(AlpsDecoder::Error::kAcceptChInvalidStream, error);
}
// According to
// https://davidben.github.io/http-client-hint-reliability/ \
// draft-davidben-http-client-hint-reliability.html#name-http-2-accept_ch-frame
// "If a user agent receives an ACCEPT_CH frame whose stream [...] flags
// field is non-zero, it MUST respond with a connection error [...]."
TEST(AlpsDecoderTest, AcceptChWithInvalidFlags) {
AlpsDecoder decoder;
AlpsDecoder::Error error = decoder.Decode(
HexDecode("00001e" // length
"89" // type ACCEPT_CH
"02" // invalid flags: should be zero
"00000000" // stream ID
"0017" // origin length
"68747470733a2f2f7777772e" //
"6578616d706c652e636f6d" // origin "https://www.example.com"
"0003" // value length
"666f6f")); // value "foo"
EXPECT_EQ(AlpsDecoder::Error::kAcceptChWithFlags, error);
}
TEST(AlpsDecoderTest, SettingsOnInvalidStream) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("000006" // length
"04" // type SETTINGS
"00" // flags
"00000001" // invalid stream ID: should be zero
"1234" // identifier
"fedcba98")); // value
EXPECT_EQ(AlpsDecoder::Error::kFramingError, error);
}
TEST(AlpsDecoderTest, SettingsAck) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("000000" // length
"04" // type SETTINGS
"01" // ACK flag
"00000000")); // stream ID
EXPECT_EQ(AlpsDecoder::Error::kSettingsWithAck, error);
}
// According to https://httpwg.org/specs/rfc7540.html#FrameHeader:
// "Flags that have no defined semantics for a particular frame type MUST be
// ignored [...]"
TEST(AlpsDecoderTest, SettingsWithInvalidFlags) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("000006" // length
"04" // type SETTINGS
"02" // invalid flag
"00000000" // stream ID
"1234" // identifier
"fedcba98")); // value
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
}
TEST(AlpsDecoderTest, ForbiddenFrame) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("000003" // length
"00" // frame type DATA
"01" // flags END_STREAM
"00000001" // stream ID
"666f6f")); // payload "foo"
EXPECT_EQ(AlpsDecoder::Error::kForbiddenFrame, error);
}
TEST(AlpsDecoderTest, UnknownFrame) {
AlpsDecoder decoder;
AlpsDecoder::Error error =
decoder.Decode(HexDecode("000003" // length
"2a" // unknown frame type
"ff" // flags
"00000008" // stream ID
"666f6f")); // payload "foo"
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
EXPECT_THAT(decoder.GetAcceptCh(), IsEmpty());
EXPECT_THAT(decoder.GetSettings(), IsEmpty());
EXPECT_EQ(0, decoder.settings_frame_count());
}
class AlpsDecoderTestWithFeature : public ::testing::TestWithParam<bool> {
public:
bool ShouldKillSessionOnAcceptChMalformed() { return GetParam(); }
private:
void SetUp() override {
feature_list_.InitWithFeatureState(
features::kShouldKillSessionOnAcceptChMalformed,
ShouldKillSessionOnAcceptChMalformed());
}
base::test::ScopedFeatureList feature_list_;
};
INSTANTIATE_TEST_SUITE_P(All, AlpsDecoderTestWithFeature, testing::Bool());
TEST_P(AlpsDecoderTestWithFeature, MalformedAcceptChFrame) {
// Correct, complete payload.
std::string payload = HexDecode(
"0017" // origin length
"68747470733a2f2f7777772e"
"6578616d706c652e636f6d" // origin "https://www.example.com"
"0003" // value length
"666f6f"); // value "foo"
for (uint8_t payload_length = 1; payload_length < payload.length();
payload_length++) {
base::HistogramTester histogram_tester;
// First two bytes of length.
std::string frame = HexDecode("0000");
// Last byte of length.
frame.push_back(static_cast<char>(payload_length));
frame.append(
HexDecode("89" // type ACCEPT_CH
"00" // flags
"00000000")); // stream ID
// Incomplete, malformed payload.
frame.append(payload.data(), payload_length);
AlpsDecoder decoder;
AlpsDecoder::Error error = decoder.Decode(frame);
if (ShouldKillSessionOnAcceptChMalformed()) {
EXPECT_EQ(AlpsDecoder::Error::kAcceptChMalformed, error);
histogram_tester.ExpectUniqueSample(
"Net.SpdySession.AlpsDecoderStatus.Bypassed",
static_cast<int>(AlpsDecoder::Error::kNoError), 1);
} else {
EXPECT_EQ(AlpsDecoder::Error::kNoError, error);
histogram_tester.ExpectUniqueSample(
"Net.SpdySession.AlpsDecoderStatus.Bypassed",
static_cast<int>(AlpsDecoder::Error::kAcceptChMalformed), 1);
}
}
}
} // namespace
} // namespace net
|