Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions examples/echo_bot/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func main() {
channelSecret := os.Getenv("LINE_CHANNEL_SECRET")
channelSecret := "DummyChannelSecret" // Dummy value is fine to skip webhook signature verification
bot, err := messaging_api.NewMessagingApiAPI(
os.Getenv("LINE_CHANNEL_TOKEN"),
)
Expand All @@ -38,7 +38,9 @@ func main() {
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
log.Println("/callback called...")

cb, err := webhook.ParseRequest(channelSecret, req)
cb, err := webhook.ParseRequestWithOption(channelSecret, req, &webhook.ParseOption{
SkipSignatureValidation: func() bool { return true },
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Normally, webhook signature verification "should" be done. Providing examples that skip it might lead to uninformed users using this, so please do not offer it as an example.

if err != nil {
log.Printf("Cannot parse request: %+v\n", err)
if errors.Is(err, webhook.ErrInvalidSignature) {
Expand Down
13 changes: 11 additions & 2 deletions linebot/webhook/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ var (
ErrInvalidSignature = errors.New("invalid signature")
)

type ParseOption struct {
SkipSignatureValidation func() bool
}

// ParseRequest func
func ParseRequest(channelSecret string, r *http.Request) (*CallbackRequest, error) {
func ParseRequestWithOption(channelSecret string, r *http.Request, opt *ParseOption) (*CallbackRequest, error) {
defer func() { _ = r.Body.Close() }()
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
if !ValidateSignature(channelSecret, r.Header.Get("x-line-signature"), body) {
skip := opt != nil && opt.SkipSignatureValidation != nil && opt.SkipSignatureValidation()
if !skip && !ValidateSignature(channelSecret, r.Header.Get("x-line-signature"), body) {
return nil, ErrInvalidSignature
}

Expand All @@ -33,6 +38,10 @@ func ParseRequest(channelSecret string, r *http.Request) (*CallbackRequest, erro
return &cb, nil
}

func ParseRequest(channelSecret string, r *http.Request) (*CallbackRequest, error) {
return ParseRequestWithOption(channelSecret, r, nil)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you do these like the Java SDK?

  • Write comments explaining why users need this option in ParseRequestWithOption /when it should be used.
  • Indicate to ParseRequest users that there is a version where the option can be utilized.
  • Write sufficient tests.

func ValidateSignature(channelSecret, signature string, body []byte) bool {
decoded, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
Expand Down