Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing the use of the ioutil package And Fix CVE-2021-42576 #1954

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bindings/alicloud/dingtalk/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package webhook

import (
"context"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestPublishMsg(t *testing.T) { //nolint:paralleltest
t.Errorf("Expected request to '/test', got '%s'", r.URL.EscapedPath())
}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.Nil(t, err)
assert.Equal(t, msg, string(body))
}))
Expand Down
4 changes: 2 additions & 2 deletions bindings/apns/apns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package apns
import (
"bytes"
"context"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestInvoke(t *testing.T) {

return &http.Response{
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(body)),
Body: io.NopCloser(strings.NewReader(body)),
}
})
_, err := testBinding.Invoke(context.TODO(), successRequest)
Expand Down
4 changes: 2 additions & 2 deletions bindings/azure/eventgrid/eventgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"time"

"github.com/Azure/azure-sdk-for-go/services/eventgrid/mgmt/2021-12-01/eventgrid"
Expand Down Expand Up @@ -279,7 +279,7 @@ func (a *AzureEventGrid) createSubscription(ctx context.Context) error {
res := result.FutureAPI.Response()

if res.StatusCode != fasthttp.StatusCreated {
bodyBytes, err := ioutil.ReadAll(res.Body)
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
a.logger.Debugf("Failed reading error body when creating or updating Event Grid subscription: %v", err)

Expand Down
4 changes: 2 additions & 2 deletions bindings/azure/signalr/signalr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -213,7 +213,7 @@ func (s *SignalR) sendMessageToSignalR(ctx context.Context, url string, token st
defer resp.Body.Close()

// Read the body regardless to drain it and ensure the connection can be reused
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions bindings/azure/signalr/signalr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -284,7 +284,7 @@ func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {

func TestWriteShouldFail(t *testing.T) {
httpTransport := &mockTransport{
response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(""))},
response: &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(""))},
}

s := NewSignalR(logger.NewLogger("test"))
Expand Down Expand Up @@ -335,7 +335,7 @@ func TestWriteShouldFail(t *testing.T) {

func TestWriteShouldSucceed(t *testing.T) {
httpTransport := &mockTransport{
response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(""))},
response: &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(""))},
}

s := NewSignalR(logger.NewLogger("test"))
Expand Down
5 changes: 2 additions & 3 deletions bindings/gcp/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"strconv"

Expand Down Expand Up @@ -214,9 +213,9 @@ func (g *GCPStorage) get(ctx context.Context, req *bindings.InvokeRequest) (*bin
}
defer rc.Close()

data, err := ioutil.ReadAll(rc)
data, err := io.ReadAll(rc)
if err != nil {
return nil, fmt.Errorf("gcp bucketgcp bucket binding error: ioutil.ReadAll: %v", err)
return nil, fmt.Errorf("gcp bucketgcp bucket binding error: io.ReadAll: %v", err)
}

if metadata.EncodeBase64 {
Expand Down
3 changes: 1 addition & 2 deletions bindings/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -148,7 +147,7 @@ func (h *HTTPSource) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*

// Read the response body. For empty responses (e.g. 204 No Content)
// `b` will be an empty slice.
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package http_test

import (
"context"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestInit(t *testing.T) {
input := req.Method
if req.Body != nil {
defer req.Body.Close()
b, _ := ioutil.ReadAll(req.Body)
b, _ := io.ReadAll(req.Body)
if len(b) > 0 {
input = string(b)
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/huawei/obs/obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"strconv"

"github.com/google/uuid"
Expand Down Expand Up @@ -228,7 +228,7 @@ func (o *HuaweiOBS) get(ctx context.Context, req *bindings.InvokeRequest) (*bind
}
}()

data, err := ioutil.ReadAll(out.Body)
data, err := io.ReadAll(out.Body)
if err != nil {
return nil, fmt.Errorf("obs binding error. error reading obs object content: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions bindings/huawei/obs/obs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"strings"
"testing"
"testing/iotest"
Expand Down Expand Up @@ -374,7 +374,7 @@ func TestGetOperation(t *testing.T) {
},
Metadata: map[string]string{},
},
Body: ioutil.NopCloser(strings.NewReader("Hello Dapr")),
Body: io.NopCloser(strings.NewReader("Hello Dapr")),
}, nil
},
},
Expand Down Expand Up @@ -447,7 +447,7 @@ func TestGetOperation(t *testing.T) {
},
Metadata: map[string]string{},
},
Body: ioutil.NopCloser(iotest.ErrReader(errors.New("unexpected data reading error"))),
Body: io.NopCloser(iotest.ErrReader(errors.New("unexpected data reading error"))),
}, nil
},
},
Expand Down Expand Up @@ -667,7 +667,7 @@ func TestInvoke(t *testing.T) {
},
Metadata: map[string]string{},
},
Body: ioutil.NopCloser(strings.NewReader("Hello Dapr")),
Body: io.NopCloser(strings.NewReader("Hello Dapr")),
}, nil
},
},
Expand Down
4 changes: 2 additions & 2 deletions bindings/localstorage/localstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -153,7 +153,7 @@ func (ls *LocalStorage) get(filename string, req *bindings.InvokeRequest) (*bind
return nil, err
}

b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
ls.logger.Debugf("%s", err)

Expand Down
4 changes: 2 additions & 2 deletions bindings/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"time"
Expand Down Expand Up @@ -263,7 +263,7 @@ func initDB(url, pemPath string) (*sql.DB, error) {

if pemPath != "" {
rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile(pemPath)
pem, err := os.ReadFile(pemPath)
if err != nil {
return nil, errors.Wrapf(err, "Error reading PEM file from %s", pemPath)
}
Expand Down
6 changes: 3 additions & 3 deletions bindings/twilio/sms/sms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package sms
import (
"context"
"errors"
"io/ioutil"
"io"
"net/http"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestParseDuration(t *testing.T) {

func TestWriteShouldSucceed(t *testing.T) {
httpTransport := &mockTransport{
response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(""))},
response: &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(""))},
}
m := bindings.Metadata{}
m.Properties = map[string]string{
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestWriteShouldSucceed(t *testing.T) {

func TestWriteShouldFail(t *testing.T) {
httpTransport := &mockTransport{
response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(""))},
response: &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(""))},
}
m := bindings.Metadata{}
m.Properties = map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ require (
github.com/matoous/go-nanoid/v2 v2.0.0
github.com/matryer/is v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/microcosm-cc/bluemonday v1.0.7 // indirect
github.com/microcosm-cc/bluemonday v1.0.17 // indirect
github.com/miekg/dns v1.1.48 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4
github.com/moul/http2curl v1.0.0 // indirect
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2184,8 +2184,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182aff
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
github.com/microcosm-cc/bluemonday v1.0.7 h1:6yAQfk4XT+PI/dk1ZeBp1gr3Q2Hd1DR0O3aEyPUJVTE=
github.com/microcosm-cc/bluemonday v1.0.7/go.mod h1:HOT/6NaBlR0f9XlxD3zolN6Z3N8Lp4pvhp+jLS5ihnI=
github.com/microcosm-cc/bluemonday v1.0.17 h1:Z1a//hgsQ4yjC+8zEkV8IWySkXnsxmdSY642CTFQb5Y=
github.com/microcosm-cc/bluemonday v1.0.17/go.mod h1:Z0r70sCuXHig8YpBzCc5eGHAap2K7e/u082ZUpDRRqM=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
Expand Down Expand Up @@ -3238,7 +3238,6 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
Expand All @@ -3247,6 +3246,7 @@ golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand Down
4 changes: 2 additions & 2 deletions internal/authentication/azure/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
Expand Down Expand Up @@ -331,7 +331,7 @@ func (c CertConfig) GetTokenCredential() (token azcore.TokenCredential, err erro
// If we have a certificate path, load it
if c.ClientCertificateConfig.CertificatePath != "" {
var errB error
data, errB = ioutil.ReadFile(ccc.CertificatePath)
data, errB = os.ReadFile(ccc.CertificatePath)
if errB != nil {
return nil, fmt.Errorf("failed to read the certificate file (%s): %v", ccc.CertificatePath, errB)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/authentication/azure/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package azure

import (
"encoding/base64"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -60,7 +59,7 @@ func TestGetClientCert(t *testing.T) {
func TestAuthorizorWithCertFile(t *testing.T) {
testCertFileName := "./.cert.pfx"
certBytes := getTestCert()
err := ioutil.WriteFile(testCertFileName, certBytes, 0o644)
err := os.WriteFile(testCertFileName, certBytes, 0o644)
assert.NoError(t, err)

settings, err := NewEnvironmentSettings(
Expand Down
6 changes: 3 additions & 3 deletions middleware/http/nethttpadaptor/nethttpadaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ limitations under the License.
package nethttpadaptor

import (
"io/ioutil"
"io"
"net"
"net/http"
"strconv"
Expand All @@ -29,11 +29,11 @@ func NewNetHTTPHandlerFunc(logger logger.Logger, h fasthttp.RequestHandler) http
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := fasthttp.RequestCtx{}
remoteIP := net.ParseIP(r.RemoteAddr)
remoteAddr := net.IPAddr{remoteIP, ""} //nolint
remoteAddr := net.IPAddr{remoteIP, ""} // nolint
berndverst marked this conversation as resolved.
Show resolved Hide resolved
c.Init(&fasthttp.Request{}, &remoteAddr, nil)

if r.Body != nil {
reqBody, err := ioutil.ReadAll(r.Body)
reqBody, err := io.ReadAll(r.Body)
if err != nil {
logger.Errorf("error reading request body, %+v", err)

Expand Down
4 changes: 2 additions & 2 deletions middleware/http/nethttpadaptor/nethttpadaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package nethttpadaptor

import (
"context"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestNewNetHTTPHandlerFuncResponses(t *testing.T) {
return httptest.NewRequest("GET", "http://localhost:8080/test", nil)
},
func(t *testing.T, res *http.Response) {
body, _ := ioutil.ReadAll(res.Body)
body, _ := io.ReadAll(res.Body)
assert.Equal(t, "test body!", string(body))
},
},
Expand Down
Loading