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 vulnerabilities CWE-113 (issue #4208 issue #4209) #4210

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
27 changes: 21 additions & 6 deletions agreementbot/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package agreementbot
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"sort"
"sync"
"time"

"github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/open-horizon/anax/abstractprotocol"
Expand All @@ -14,11 +21,6 @@ import (
"github.com/open-horizon/anax/exchange"
"github.com/open-horizon/anax/policy"
"github.com/open-horizon/anax/worker"
"io/ioutil"
"net/http"
"sort"
"sync"
"time"
)

type API struct {
Expand Down Expand Up @@ -318,11 +320,24 @@ func (a *API) listen(apiListen string) {
return
}

isValidInput := func(input string) bool {
// Check for CR or LF characters in input
re := regexp.MustCompile(`[\r\n]`)
return !re.MatchString(input)
}

nocache := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Add("Pragma", "no-cache, no-store")
w.Header().Add("Access-Control-Allow-Origin", r.Header.Get("Origin"))

input := r.Header.Get("Origin")
if !isValidInput(input) {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}

w.Header().Add("Access-Control-Allow-Origin", input)
w.Header().Add("Access-Control-Allow-Headers", "X-Requested-With, content-type, Authorization")
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
h.ServeHTTP(w, r)
Expand Down
21 changes: 18 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package api
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"sync"

"github.com/boltdb/bolt"
"github.com/golang/glog"
"github.com/gorilla/mux"
Expand All @@ -12,8 +16,6 @@ import (
"github.com/open-horizon/anax/persistence"
"github.com/open-horizon/anax/policy"
"github.com/open-horizon/anax/worker"
"net/http"
"sync"
)

type API struct {
Expand Down Expand Up @@ -133,11 +135,24 @@ func (a *API) router(includeStaticRedirects bool) *mux.Router {
func (a *API) listen(cfg *config.HorizonConfig) {
glog.Info(apiLogString(fmt.Sprintf("Starting Anax API server")))

isValidInput := func(input string) bool {
// Check for CR or LF characters in input
re := regexp.MustCompile(`[\r\n]`)
return !re.MatchString(input)
}

nocache := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Add("Pragma", "no-cache, no-store")
w.Header().Add("Access-Control-Allow-Origin", r.Header.Get("Origin"))

input := r.Header.Get("Origin")
if !isValidInput(input) {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}

w.Header().Add("Access-Control-Allow-Origin", input)
w.Header().Add("Access-Control-Allow-Headers", "X-Requested-With, content-type, Authorization")
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
h.ServeHTTP(w, r)
Expand Down