Skip to content

fix(sbi): enforce inbound OAuth2 on NEF service routes#23

Closed
solar224 wants to merge 13 commits into
free5gc:mainfrom
solar224:fix/nef-oauth2-inbound-auth
Closed

fix(sbi): enforce inbound OAuth2 on NEF service routes#23
solar224 wants to merge 13 commits into
free5gc:mainfrom
solar224:fix/nef-oauth2-inbound-auth

Conversation

@solar224

@solar224 solar224 commented Mar 11, 2026

Copy link
Copy Markdown
Contributor
  • Add NFContext interface and AuthorizationCheck method to NefContext, calling oauth.VerifyOAuth for inbound token validation
  • Add RouterAuthorizationCheck utility (util/util.go) and UtilLog
  • Refactor server.go: mount routes conditionally per ServiceList entry and attach per-group OAuth2 middleware
  • Extend ServiceList validation in config.go to include 3gpp-traffic-influence
  • Add 3gpp-traffic-influence to default serviceList in nefcfg.yaml
  • Remove four stale '// TODO: Authorize the AF' comments from pfd.go now that middleware handles authorization

Fixes: free5gc/free5gc#858free5gc/free5gc#859free5gc/free5gc#860free5gc/free5gc#861free5gc/free5gc#862

- Add NFContext interface and AuthorizationCheck method to NefContext,
  calling oauth.VerifyOAuth for inbound token validation
- Add RouterAuthorizationCheck utility (util/util.go) and UtilLog
- Refactor server.go: mount routes conditionally per ServiceList entry
  and attach per-group OAuth2 middleware; use correct ServiceName constant
  for each route group (NNEF_PFDMANAGEMENT, NNEF_OAM, 3GPP_TRAFFIC_INFLUENCE)
- Extend ServiceList validation in config.go to include
  3gpp-traffic-influence
- Add 3gpp-traffic-influence to default serviceList in nefcfg.yaml
- Remove four stale '// TODO: Authorize the AF' comments from pfd.go
  now that middleware handles authorization
@solar224 solar224 force-pushed the fix/nef-oauth2-inbound-auth branch from bbca77b to b16537c Compare March 12, 2026 07:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens NEF’s SBI surface by enforcing inbound OAuth2 token validation on NEF service routes, while also improving Traffic Influence request validation and implementing authenticated forwarding of SMF notifications to the AF callback URI.

Changes:

  • Add middleware-oriented authorization plumbing (NFContext interface, per-route-group OAuth2 checks, RouterAuthorizationCheck helper).
  • Refactor route mounting to be serviceList-driven (plus protected callback group) and extend config defaults/validation for 3gpp-traffic-influence and nnef-callback.
  • Validate notificationDestination for Traffic Influence subscriptions and add callback forwarding logic with unit tests.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pkg/factory/config.go Extends serviceList validation to include traffic influence + callback services.
internal/util/util.go Introduces RouterAuthorizationCheck helper used as OAuth2 middleware.
internal/sbi/server.go Refactors route mounting per serviceList and attaches OAuth2 middleware per group.
internal/sbi/processor/ti.go Adds notificationDestination presence + URL validation.
internal/sbi/processor/ti_test.go Updates TI tests to include notificationDestination and adds missing-field cases.
internal/sbi/processor/pfd.go Removes stale authorization TODO comments now handled by middleware.
internal/sbi/processor/callback.go Implements forwarding SMF notifications to AF with outbound OAuth2 token binding.
internal/sbi/processor/callback_test.go Adds unit tests for token binding and callback POST behavior.
internal/logger/logger.go Adds a UtilLog logger category for util-layer logging.
internal/context/nef_context.go Adds NFContext + AuthorizationCheck that calls oauth.VerifyOAuth.
config/nefcfg.yaml Updates default serviceList to include traffic influence + callback services.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/sbi/server.go
Comment on lines +52 to +56
callbackAuthCheck := nef_util.NewRouterAuthorizationCheck(models.ServiceName("nnef-callback"))
callbackGroup := s.router.Group(factory.NefCallbackResUriPrefix)
callbackGroup.Use(func(c *gin.Context) {
callbackAuthCheck.Check(c, s.Context())
})

Copilot AI Apr 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Gin middleware functions added via Group.Use don’t call c.Next() when authorization succeeds. In Gin, handlers must call Next() to continue the chain; otherwise the request will stop at the middleware and the route handlers won’t run (likely returning an empty 200). After Check passes (and c.IsAborted() is false), call c.Next() (or refactor Check to do so).

Copilot uses AI. Check for mistakes.
Comment thread internal/context/nef_context.go Outdated
"golang.org/x/oauth2"
)

var afCallbackHTTPClient = &http.Client{}

Copilot AI Apr 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afCallbackHTTPClient is a package-level http.Client with no timeout configured. For outbound callbacks this can hang indefinitely on network stalls and tie up goroutines. Consider setting a reasonable Timeout (or using a custom Transport with timeouts) and/or enforcing a deadline on requestCtx before issuing the request.

Copilot uses AI. Check for mistakes.
Comment thread internal/util/util.go
Comment on lines +27 to +33
func (rac *RouterAuthorizationCheck) Check(c *gin.Context, nefCtx nef_context.NFContext) {
token := c.Request.Header.Get("Authorization")
if err := nefCtx.AuthorizationCheck(token, rac.serviceName); err != nil {
logger.UtilLog.Debugf("RouterAuthorizationCheck::Check Unauthorized: %s", err.Error())
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
c.Abort()
return

Copilot AI Apr 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OAuth2 middleware returns 401 with {"error": ...} but doesn’t set the metrics/sbi context key (sbi.IN_PB_DETAILS_CTX_STR) like the rest of the SBI handlers do for ProblemDetails responses. If you rely on that key for metrics/observability, consider setting it here as well (even if you keep the simplified 401 body).

Copilot uses AI. Check for mistakes.
@solar224 solar224 closed this May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bugs] NEF 3gpp-pfd-management is unauthenticated: forged-token requests can create, read, and delete PFD transactions

2 participants