Skip to content
Open
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
22 changes: 19 additions & 3 deletions internal/sbi/api_subscriberdatamanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,22 @@ func (s *Server) HandleUpuAck(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{})
}

// respondResourceURIStructureNotFound answers a request that reached the API
// but names no resource this NF defines, as TS 29.500 table 5.2.7.2-1
// requires: 404 with a ProblemDetails whose cause is
// RESOURCE_URI_STRUCTURE_NOT_FOUND.
func respondResourceURIStructureNotFound(c *gin.Context) {
problemDetails := models.ProblemDetails{
Title: "Resource URI structure not found",
Status: http.StatusNotFound,
Detail: c.Request.Method + " " + c.Request.URL.Path + " does not name a resource of this API",
Cause: "RESOURCE_URI_STRUCTURE_NOT_FOUND",
}
c.Set(sbi.IN_PB_DETAILS_CTX_STR, problemDetails.Cause)
c.Header("Content-Type", "application/problem+json")
c.JSON(int(problemDetails.Status), problemDetails)
}

func (s *Server) OneLayerPathHandlerFunc(c *gin.Context) {
supi := c.Param("supi")
oneLayerPathRouter := s.getOneLayerRoutes()
Expand All @@ -756,7 +772,7 @@ func (s *Server) OneLayerPathHandlerFunc(c *gin.Context) {
return
}

c.String(http.StatusNotFound, "404 page not found")
respondResourceURIStructureNotFound(c)
}

func (s *Server) TwoLayerPathHandlerFunc(c *gin.Context) {
Expand Down Expand Up @@ -799,7 +815,7 @@ func (s *Server) TwoLayerPathHandlerFunc(c *gin.Context) {
}
}

c.String(http.StatusNotFound, "404 page not found")
respondResourceURIStructureNotFound(c)
}

func pathPatternMatches(pattern, path string) bool {
Expand Down Expand Up @@ -881,7 +897,7 @@ func (s *Server) ThreeLayerPathHandlerFunc(c *gin.Context) {
return
}

c.String(http.StatusNotFound, "404 page not found")
respondResourceURIStructureNotFound(c)
}

func (s *Server) getOneLayerRoutes() []Route {
Expand Down
82 changes: 82 additions & 0 deletions internal/sbi/api_subscriberdatamanagement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/free5gc/udm/internal/sbi/consumer"
"github.com/free5gc/udm/internal/sbi/processor"
"github.com/free5gc/udm/pkg/app"
"github.com/free5gc/util/metrics/sbi"
"github.com/free5gc/util/validator"
)

Expand Down Expand Up @@ -161,6 +162,87 @@ func TestTwoLayerPathHandlerMatchesPathAndMethod(t *testing.T) {
}
}

func TestPathHandlersRejectUnmatchedResourceURIs(t *testing.T) {
server := &Server{}
const supi = "imsi-208930000000003"
tests := []struct {
name string
method string
path string
params gin.Params
handler func(*gin.Context)
}{
{
name: "sdm one layer wrong method",
method: http.MethodDelete,
path: "/nudm-sdm/v2/" + supi,
params: gin.Params{{Key: "supi", Value: supi}},
handler: server.OneLayerPathHandlerFunc,
},
{
name: "sdm two layer unknown resource",
method: http.MethodGet,
path: "/nudm-sdm/v2/" + supi + "/no-such-resource",
params: gin.Params{{Key: "supi", Value: supi}, {Key: "subscriptionId", Value: "no-such-resource"}},
handler: server.TwoLayerPathHandlerFunc,
},
{
name: "sdm two layer wrong method",
method: http.MethodPost,
path: "/nudm-sdm/v2/" + supi + "/am-data",
params: gin.Params{{Key: "supi", Value: supi}, {Key: "subscriptionId", Value: "am-data"}},
handler: server.TwoLayerPathHandlerFunc,
},
{
name: "sdm three layer unknown resource",
method: http.MethodGet,
path: "/nudm-sdm/v2/" + supi + "/am-data/no-such-resource",
params: gin.Params{
{Key: "supi", Value: supi},
{Key: "subscriptionId", Value: "am-data"},
{Key: "thirdLayer", Value: "no-such-resource"},
},
handler: server.ThreeLayerPathHandlerFunc,
},
{
name: "ueau two layer unknown resource",
method: http.MethodGet,
path: "/nudm-ueau/v1/" + supi + "/no-such-resource",
params: gin.Params{{Key: "supi", Value: supi}, {Key: "twoLayer", Value: "no-such-resource"}},
handler: server.UEAUTwoLayerPathHandlerFunc,
},
{
name: "ueau three layer unknown resource",
method: http.MethodPost,
path: "/nudm-ueau/v1/" + supi + "/no-such-resource/generate-av",
params: gin.Params{
{Key: "supi", Value: supi},
{Key: "twoLayer", Value: "no-such-resource"},
{Key: "thirdLayer", Value: "generate-av"},
},
handler: server.UEAUThreeLayerPathHandlerFunc,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
recorder, c := newSDMTestContext(t, tt.method, tt.path, "")
c.Params = tt.params

require.NotPanics(t, func() { tt.handler(c) })
require.Equal(t, http.StatusNotFound, recorder.Code)
require.Equal(t, "application/problem+json", recorder.Header().Get("Content-Type"))

var problem models.ProblemDetails
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &problem))
require.Equal(t, int32(http.StatusNotFound), problem.Status)
require.Equal(t, "RESOURCE_URI_STRUCTURE_NOT_FOUND", problem.Cause)
require.Contains(t, problem.Detail, tt.path)
require.Equal(t, problem.Cause, c.GetString(sbi.IN_PB_DETAILS_CTX_STR))
})
}
}

func TestIsValidSDMSubscriptionID(t *testing.T) {
tests := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions internal/sbi/api_ueauthentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (s *Server) UEAUTwoLayerPathHandlerFunc(c *gin.Context) {
return
}

c.String(http.StatusNotFound, "404 page not found")
respondResourceURIStructureNotFound(c)
}

func (s *Server) UEAUThreeLayerPathHandlerFunc(c *gin.Context) {
Expand Down Expand Up @@ -254,5 +254,5 @@ func (s *Server) UEAUThreeLayerPathHandlerFunc(c *gin.Context) {
return
}

c.String(http.StatusNotFound, "404 page not found")
respondResourceURIStructureNotFound(c)
}
Loading