Skip to content

Commit

Permalink
Fail HTTP request reading if unexpected end of data reached.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Nov 24, 2023
1 parent 0d36372 commit 511483e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tdnet/td/net/HttpReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ Result<size_t> HttpReader::read_next(HttpQuery *query, bool can_be_slow) {
CHECK(query_ == nullptr);
query_ = query;
}

auto r_size = do_read_next(can_be_slow);
if (state_ != State::ReadHeaders && flow_sink_.is_ready() && r_size.is_ok() && r_size.ok() > 0) {
CHECK(flow_sink_.status().is_ok());
return Status::Error(400, "Bad Request: unexpected end of request content");
}
return r_size;
}

Result<size_t> HttpReader::do_read_next(bool can_be_slow) {
size_t need_size = input_->size() + 1;
while (true) {
if (state_ != State::ReadHeaders) {
Expand Down
2 changes: 2 additions & 0 deletions tdnet/td/net/HttpReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class HttpReader {
string temp_file_name_;
int64 file_size_ = 0;

Result<size_t> do_read_next(bool can_be_slow);

Result<size_t> split_header() TD_WARN_UNUSED_RESULT;
void process_header(MutableSlice header_name, MutableSlice header_value);
Result<bool> parse_multipart_form_data(bool can_be_slow) TD_WARN_UNUSED_RESULT;
Expand Down
2 changes: 2 additions & 0 deletions tdutils/td/utils/ByteFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ByteFlowInterface {
virtual size_t get_write_size() = 0;
virtual void reset_need_size() {
}

ByteFlowInterface() = default;
ByteFlowInterface(const ByteFlowInterface &) = delete;
ByteFlowInterface &operator=(const ByteFlowInterface &) = delete;
Expand Down Expand Up @@ -139,6 +140,7 @@ class ByteFlowBaseCommon : public ByteFlowInterface {
bool can_read{true};
bool can_write{true};
Options options_;

void finish(Status status) {
stop_flag_ = true;
need_size_ = 0;
Expand Down

0 comments on commit 511483e

Please sign in to comment.