| 1 | // Copyright 2016 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package httpresponse defines an Analyzer that checks for mistakes |
| 6 | // using HTTP responses. |
| 7 | package httpresponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/ |
| 8 | |
| 9 | import ( |
| 10 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 11 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 12 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 13 | |
| 14 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 15 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 16 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 17 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 18 | ) |
| 19 | |
| 20 | const Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/Doc. = `check for mistakes using HTTP responses |
| 21 | |
| 22 | A common mistake when using the net/http package is to defer a function |
| 23 | call to close the http.Response Body before checking the error that |
| 24 | determines whether the response is valid: |
| 25 | |
| 26 | resp, err := http.Head(url) |
| 27 | defer resp.Body.Close() |
| 28 | if err != nil { |
| 29 | log.Fatal(err) |
| 30 | } |
| 31 | // (defer statement belongs here) |
| 32 | |
| 33 | This checker helps uncover latent nil dereference bugs by reporting a |
| 34 | diagnostic for such mistakes.` |
| 35 | |
| 36 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/Analyzer. = &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 37 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "httpresponse", |
| 38 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/Doc., |
| 39 | URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#URL.: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/httpresponse", |
| 40 | Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.}, |
| 41 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/run()., |
| 42 | } |
| 43 | |
| 44 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/run().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 45 | inspect := pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.].(*inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#) |
| 46 | |
| 47 | // Fast path: if the package doesn't import net/http, |
| 48 | // skip the traversal. |
| 49 | if !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.Importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/Imports().(pass.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Pkg., "net/http") { |
| 50 | return nil, nil |
| 51 | } |
| 52 | |
| 53 | nodeFilter := []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#{ |
| 54 | (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#)(nil), |
| 55 | } |
| 56 | inspect.WithStackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#WithStack().(nodeFilter, func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, push bool, stack []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) bool { |
| 57 | if !push { |
| 58 | return true |
| 59 | } |
| 60 | call := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) |
| 61 | if !isHTTPFuncOrMethodOnClientscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/isHTTPFuncOrMethodOnClient().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., call) { |
| 62 | return true // the function call is not related to this check. |
| 63 | } |
| 64 | |
| 65 | // Find the innermost containing block, and get the list |
| 66 | // of statements starting with the one containing call. |
| 67 | stmts, ncalls := restOfBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/restOfBlock().(stack) |
| 68 | if len(stmts) < 2 { |
| 69 | // The call to the http function is the last statement of the block. |
| 70 | return true |
| 71 | } |
| 72 | |
| 73 | // Skip cases in which the call is wrapped by another (#52661). |
| 74 | // Example: resp, err := checkError(http.Get(url)) |
| 75 | if ncalls > 1 { |
| 76 | return true |
| 77 | } |
| 78 | |
| 79 | asg, ok := stmts[0].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#) |
| 80 | if !ok { |
| 81 | return true // the first statement is not assignment. |
| 82 | } |
| 83 | |
| 84 | resp := rootIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/rootIdent().(asg.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[0]) |
| 85 | if resp == nil { |
| 86 | return true // could not find the http.Response in the assignment. |
| 87 | } |
| 88 | |
| 89 | def, ok := stmts[1].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.DeferStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/DeferStmt#) |
| 90 | if !ok { |
| 91 | return true // the following statement is not a defer. |
| 92 | } |
| 93 | root := rootIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/rootIdent().(def.Callscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/DeferStmt#Call..Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun.) |
| 94 | if root == nil { |
| 95 | return true // could not find the receiver of the defer call. |
| 96 | } |
| 97 | |
| 98 | if resp.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Obj. == root.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Obj. { |
| 99 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(root, "using %s before checking for errors", resp.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.) |
| 100 | } |
| 101 | return true |
| 102 | }) |
| 103 | return nil, nil |
| 104 | } |
| 105 | |
| 106 | // isHTTPFuncOrMethodOnClient checks whether the given call expression is on |
| 107 | // either a function of the net/http package or a method of http.Client that |
| 108 | // returns (*http.Response, error). |
| 109 | func isHTTPFuncOrMethodOnClientscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/isHTTPFuncOrMethodOnClient().(info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, expr *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) bool { |
| 110 | fun, _ := expr.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#) |
| 111 | sig, _ := info.Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[fun].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type..(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#) |
| 112 | if sig == nil { |
| 113 | return false // the call is not of the form x.f() |
| 114 | } |
| 115 | |
| 116 | res := sig.Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Results().() |
| 117 | if res.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#Len().() != 2 { |
| 118 | return false // the function called does not return two values. |
| 119 | } |
| 120 | isPtr, named := typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.ReceiverNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/ReceiverNamed().(res.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(0)) |
| 121 | if !isPtr || named == nil || !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsTypeNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsTypeNamed().(named, "net/http", "Response") { |
| 122 | return false // the first return type is not *http.Response. |
| 123 | } |
| 124 | |
| 125 | errorType := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Universescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Universe..Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().("error").Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().() |
| 126 | if !typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Identicalscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Identical().(res.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(1).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().(), errorType) { |
| 127 | return false // the second return type is not error |
| 128 | } |
| 129 | |
| 130 | typ := info.Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[fun.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X.].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type. |
| 131 | if typ == nil { |
| 132 | id, ok := fun.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 133 | return ok && id.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == "http" // function in net/http package. |
| 134 | } |
| 135 | |
| 136 | if typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsTypeNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsTypeNamed().(typ, "net/http", "Client") { |
| 137 | return true // method on http.Client. |
| 138 | } |
| 139 | ptr, ok := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(typ).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#) |
| 140 | return ok && typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsTypeNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsTypeNamed().(ptr.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().(), "net/http", "Client") // method on *http.Client. |
| 141 | } |
| 142 | |
| 143 | // restOfBlock, given a traversal stack, finds the innermost containing |
| 144 | // block and returns the suffix of its statements starting with the current |
| 145 | // node, along with the number of call expressions encountered. |
| 146 | func restOfBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/restOfBlock().(stack []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) ([]astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#, int) { |
| 147 | var ncalls int |
| 148 | for i, n := range slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Backwardscip-go gomod github.com/golang/go/src go1.25.0 slices/Backward().(stack) { |
| 149 | if b, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#); ok { |
| 150 | for j, v := range b.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. { |
| 151 | if v == stack[i+1] { |
| 152 | return b.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.[j:], ncalls |
| 153 | } |
| 154 | } |
| 155 | break |
| 156 | } |
| 157 | |
| 158 | if _, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#); ok { |
| 159 | ncalls++ |
| 160 | } |
| 161 | } |
| 162 | return nil, 0 |
| 163 | } |
| 164 | |
| 165 | // rootIdent finds the root identifier x in a chain of selections x.y.z, or nil if not found. |
| 166 | func rootIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/rootIdent().(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident# { |
| 167 | switch n := n.(type) { |
| 168 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#: |
| 169 | return rootIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/httpresponse`/rootIdent().(n.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X.) |
| 170 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#: |
| 171 | return n |
| 172 | default: |
| 173 | return nil |
| 174 | } |
| 175 | } |