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

fix: wrong uri format in location header #144

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion internal/sbi/processor/pdu_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package processor
import (
"encoding/hex"
"errors"
"fmt"
"net"
"net/http"
"reflect"
"strings"

"github.com/gin-gonic/gin"

Expand Down Expand Up @@ -253,7 +255,22 @@ func (p *Processor) HandlePDUSessionSMContextCreate(

doSubscribe = true
response.JsonData = smContext.BuildCreatedData()
c.Header("Location", smContext.Ref)

// default location value will only be used in test environment
// in real environment, location value will be formatted as a full URI
location := smContext.Ref // this is the default location value
if c.Request != nil {
protocol := "http"
if c.Request.TLS != nil {
protocol += "s"
}
location = fmt.Sprintf("%s://%s%s/%s",
protocol,
c.Request.Host,
strings.TrimSuffix(c.Request.URL.Path, "/"),
strings.Split(smContext.Ref, ":")[2])
Copy link
Contributor

Choose a reason for hiding this comment

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

Should add the length check to avoid the invalid memory access.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this check appears to be redundant for two reasons:

  1. During UUID creation, the Must() function already ensures its validity by panicking if the creation fails.
  2. In the formatting part, the code:
copy(buf[:], "urn:uuid:")
encodeHex(buf[9:], uuid)

guarantees that the format will always be urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

Therefore, an additional length check at here is unnecessary as the format is enforced by the implementation itself.

By the way, I still can add a double check at here. Do you think we need it?😆

}
c.Header("Location", location)
c.JSON(http.StatusCreated, response)
}

Expand Down
Loading