Skip to content

fix: skip nil RequestOptions to prevent nil pointer dereferences on option.apply(r) #578

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions authentication/authentication_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (a *Authentication) NewRequest(
request.Header.Add("Content-Type", "application/json")

for _, option := range opts {
if option == nil {
continue
}

option.apply(request, nil)
}

Expand All @@ -70,6 +74,10 @@ func (a *Authentication) NewFormRequest(
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")

for _, option := range opts {
if option == nil {
continue
}

option.apply(request, payload)
}

Expand Down
4 changes: 4 additions & 0 deletions management/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ func applyActionsListDefaults(options []RequestOption) RequestOption {
PerPage(50).apply(r)

for _, option := range options {
if option == nil {
continue
}

option.apply(r)
}
})
Expand Down
4 changes: 4 additions & 0 deletions management/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func (m *JobManager) ImportUsers(ctx context.Context, j *Job, opts ...RequestOpt
request.Header.Add("Content-Type", mp.FormDataContentType())

for _, option := range opts {
if option == nil {
continue
}

option.apply(request)
}

Expand Down
12 changes: 12 additions & 0 deletions management/management_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func (m *Management) NewRequest(
m.applyCustomDomainHeader(request, uri)

for _, option := range options {
if option == nil {
continue
}

option.apply(request)
}

Expand Down Expand Up @@ -251,6 +255,10 @@ func applyListDefaults(options []RequestOption) RequestOption {
IncludeTotals(true).apply(r)

for _, option := range options {
if option == nil {
continue
}

option.apply(r)
}
})
Expand All @@ -261,6 +269,10 @@ func applyListCheckpointDefaults(options []RequestOption) RequestOption {
Take(50).apply(r)

for _, option := range options {
if option == nil {
continue
}

option.apply(r)
}
})
Expand Down
67 changes: 65 additions & 2 deletions management/management_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,41 @@ func TestNewRequest(t *testing.T) {
expectedError: "",
expectedBody: `{"name":"TestClient","description":"Test description"}` + "\n",
},
{
name: "Request with nil options mixed with valid options",
method: http.MethodPost,
endpoint: api.URI("clients"),
payload: nil,
options: []RequestOption{
nil,
Body([]byte(`{"test":"value"}`)),
nil,
},
expectedError: "",
expectedBody: `{"test":"value"}`,
},
{
name: "Request with all nil options",
method: http.MethodPost,
endpoint: api.URI("clients"),
payload: &Client{Name: auth0.String("TestClient")},
options: []RequestOption{
nil,
nil,
},
expectedError: "",
expectedBody: `{"name":"TestClient"}` + "\n",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
request, err := api.NewRequest(context.Background(), testCase.method, testCase.endpoint, testCase.payload, testCase.options...)
request, err := api.NewRequest(
context.Background(),
testCase.method,
testCase.endpoint,
testCase.payload,
testCase.options...)

if testCase.expectedError != "" {
assert.EqualError(t, err, testCase.expectedError)
Expand All @@ -145,10 +175,43 @@ func TestNewRequest(t *testing.T) {
assert.Equal(t, testCase.expectedBody, string(requestBody))

if testCase.expectedBody != "" {
assert.Equal(t, "application/json", request.Header.Get("Content-Type"))
assert.Equal(
t,
"application/json",
request.Header.Get("Content-Type"),
)
} else {
assert.Empty(t, request.Header.Get("Content-Type"))
}
})
}
}

// TestNilOptionsInHelperFunctions tests that helper functions handle nil options correctly.
func TestNilOptionsInHelperFunctions(t *testing.T) {
t.Run("applyListDefaults with nil options", func(t *testing.T) {
req, err := http.NewRequest("GET", "https://example.com", nil)
require.NoError(t, err)

// Test with nil options mixed with valid ones
listDefaults := applyListDefaults([]RequestOption{nil, Page(2), nil})
listDefaults.apply(req)

query := req.URL.Query()
assert.Equal(t, "2", query.Get("page"))
assert.Equal(t, "50", query.Get("per_page")) // default
assert.Equal(t, "true", query.Get("include_totals")) // default
})

t.Run("applyListCheckpointDefaults with nil options", func(t *testing.T) {
req, err := http.NewRequest("GET", "https://example.com", nil)
require.NoError(t, err)

// Test with nil options mixed with valid ones
checkpointDefaults := applyListCheckpointDefaults([]RequestOption{nil, Take(25), nil})
checkpointDefaults.apply(req)

query := req.URL.Query()
assert.Equal(t, "25", query.Get("take"))
})
}