$30 off During Our Annual Pro Sale. View Details »

Go 1.19.1 security fix net/url JoinPath

aboy
September 07, 2022

Go 1.19.1 security fix net/url JoinPath

Let's Go Talk #3 で LT した資料です
https://connehito.connpass.com/event/257000/

aboy

September 07, 2022
Tweet

More Decks by aboy

Other Decks in Programming

Transcript

  1. Go 1.19.1 security fix
    net/url JoinPath
    2022/09/07 Let’s Go Talk #3 5min LT
    aboy 

    View Slide

  2. 🎉

    View Slide

  3. security fixes 🤔

    View Slide

  4. 今日は net/url についての話!

    View Slide

  5. net/url JoinPath (Go 1.19)
    - elem を base と結合し ./ や ../ が取り除かれた URL 文字列を返す
    - 内部では url.Parse したあとに URL.JoinPath (Go 1.19) を呼んでいる
    // JoinPath returns a URL string with the provided path elements joined to
    // the existing path of base and the resulting path cleaned of any ./ or ../
    elements.
    func JoinPath(base string, elem ...string) (result string, err error) {
    url, err := Parse(base)
    if err != nil {
    return
    }
    result = url.JoinPath(elem...).String()
    return
    }

    View Slide

  6. どういう問題があった?
    - net/url JoinPath(base string, elem ...string) が相対パスを取り除か
    ないケースがあった
    - base に末尾スラッシュがない場合の ../ パス
    - elem に何も指定しない場合の base パス内
    - ディレクトリトラバーサルにつながる可能性
    - Go 1.19.1 では URL.JoinPath に2点変更が加わった
    fmt.Println(url.JoinPath("https://go.dev", "../go")) // https://go.dev/../go
    fmt.Println(url.JoinPath("https://go.dev/", "../go")) // https://go.dev/go
    fmt.Println(url.JoinPath("https://go.dev/../go")) // https://go.dev/../go

    View Slide

  7. 必ず先頭に / をつけて path.Join を呼ぶようになった
    - path.Join(“”, “../”) は “..”、path.Join(“/”, “../”) は “/” を返す
    ※参考:path.Join のテストケース https://github.com/golang/go/blob/master/src/path/path_test.go#L36

    View Slide

  8. 必ず base のパス含めて path.Join を呼ぶようになった
    - url.JoinPath(“https://go.dev/../go”) のとき /../go をパスとして処理

    View Slide

  9. 参考
    - https://twitter.com/golang/status/1567189780873486337?s=20&t=1NSLbARJbP_DS
    CsVPjJDPw
    - Go 1.19.1 and Go 1.18.6 are released
    - net/url: JoinPath doesn’t strip relative path components in all
    circumstances
    - https://go-review.googlesource.com/c/go/+/423514/
    - https://github.com/golang/go/commit/28335508913a46e05ef0c
    04a18e8a1a6beb775ec
    - https://pkg.go.dev/net/url#JoinPath
    - https://github.com/golang/go/blob/master/src/path/path_test.go

    View Slide