Skip to content

Commit

Permalink
feat: include UI meta in comments/ and strings/ (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubfaouzi authored Oct 13, 2024
1 parent 129de38 commit 03a8e30
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions db/meta-ui.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* N1QL query to retrieve UI metadata. */
SELECT
pe.meta as pe,
default_behavior_report.id as default_behavior_id,
ARRAY_CONCAT(
ARRAY_INTERSECT(
OBJECT_NAMES(d),
[
"pe",
"elf",
"strings",
"multiav",
"behavior_scans"
]
),
["summary", "comments"]
["summary", "comments", "antivirus"]
) AS tabs
FROM
`bucket_name` AS d
Expand Down
4 changes: 2 additions & 2 deletions internal/file/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func RegisterHandlers(g *echo.Group, service Service, logger log.Logger,
g.PUT("/files/:sha256/", res.update, verifyHash, requireLogin)
g.PATCH("/files/:sha256/", res.patch, verifyHash, requireLogin)
g.DELETE("/files/:sha256/", res.delete, verifyHash, requireLogin)
g.GET("/files/:sha256/strings/", res.strings, verifyHash)
g.GET("/files/:sha256/strings/", res.strings, modifyResponse, verifyHash)
g.GET("/files/:sha256/summary/", res.summary, modifyResponse, verifyHash)
g.GET("/files/:sha256/comments/", res.comments, verifyHash, optionalLogin)
g.GET("/files/:sha256/comments/", res.comments, modifyResponse, verifyHash, optionalLogin)
g.POST("/files/:sha256/like/", res.like, verifyHash, requireLogin)
g.POST("/files/:sha256/unlike/", res.unlike, verifyHash, requireLogin)
g.POST("/files/:sha256/rescan/", res.rescan, verifyHash, requireLogin)
Expand Down
20 changes: 16 additions & 4 deletions internal/file/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,28 @@ func (m middleware) VerifyHash(next echo.HandlerFunc) echo.HandlerFunc {
// ModifyResponse modifies the JSON response to include some metadata for the UI.
func (m middleware) ModifyResponse(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {

var err error
var isUI int

writer := &toolBodyWriter{body: &bytes.Buffer{}, ResponseWriter: c.Response().Writer}
c.Response().Writer = writer

if err := next(c); err != nil {
if err = next(c); err != nil {
return err
}

// Determines the source of the API request, if it originates from a
// browser, we want to attach some more UI metadata.
if !isBrowser(c.Request().UserAgent()) {
isUIStr := c.Request().Header.Get("X-Get-Ui")
if len(isUIStr) > 0 {
isUI, err = strconv.Atoi(isUIStr)
if err != nil {
return err
}
}

// Determines the source of the API request, if it originates from the UI,
// we want to attach some more UI metadata.
if isUI != 1 {
_, err := writer.ResponseWriter.Write(writer.body.Bytes())
return err
}
Expand Down
15 changes: 0 additions & 15 deletions internal/file/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"crypto/sha256"
"encoding/hex"
"regexp"
"strings"
)

var (
Expand All @@ -31,17 +30,3 @@ func hash(b []byte) string {
h.Write(b)
return hex.EncodeToString(h.Sum(nil))
}

// isBrowser returns true when the HTTP request is coming from a known user agent.
func isBrowser(userAgent string) bool {
browserList := []string{
"Chrome", "Chromium", "Mozilla", "Opera", "Safari", "Edge", "MSIE",
}

for _, browserName := range browserList {
if strings.Contains(userAgent, browserName) {
return true
}
}
return false
}

0 comments on commit 03a8e30

Please sign in to comment.