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

Adds masking to all IDs #154

Merged
merged 10 commits into from
Aug 11, 2023
Merged
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
Binary file modified .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ cat env-example > .env

Open the `.env` file and follow the [link](https://generate-random.org/api-token-generator) to create the `APPCHECK_TOKEN` env variable.

**Ensure you have the correct 16-byte `MASK_SECRET` in the `.env` file.**

An example is provided in the `env-example`, but obviously generate your own for prod.

**Add your AWS data to the `.env` file.**

Specifically what IAM roles are needed will be determined in the future. Currently, a general admin user suffices.
Expand Down
278 changes: 160 additions & 118 deletions db/models.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ services:
AWS_ACCESS_KEY_ID:
AWS_SECRET_ACCESS_KEY:
AWS_REGION:
MASK_SECRET:

redis:
image: redis:alpine
Expand Down
3 changes: 3 additions & 0 deletions env-example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ APPCHECK_TOKEN="kXfeSRgYTnoUztu6MO8FndqiRayoBaJqyDKQmoqvX3V9sZVlep/cm7cP!mgd-B9H
# either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256
HKDF_SECRET="some-secret-string"

# a 16-byte key
MASK_SECRET="your_16_byte_key"

# to generate a new nonce for production:
# go run ./scripts/main.go --new-nonce
CIPHER_NONCE="47363bcf91a1545668f0fd7a"
Expand Down
7 changes: 4 additions & 3 deletions features/admin/feedback_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package admin

import (
"confesi/db"
"confesi/lib/encryption"
"confesi/lib/response"
"errors"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"gorm.io/gorm"
Expand All @@ -14,13 +14,14 @@ import (
func (h *handler) handleFeedbackID(c *gin.Context) {
feedbackID := c.Param("feedbackID")

_, err := strconv.ParseInt(feedbackID, 10, 64)
unmaskedFeedbackId, err := encryption.Unmask(feedbackID)
if err != nil {
response.New(http.StatusBadRequest).Err("invalid feedback id").Send(c)
return
}

feedback := db.Feedback{}
err = h.db.Model(&db.Feedback{}).Where("id = ?", feedbackID).First(&feedback).Error
err = h.db.Model(&db.Feedback{}).Where("id = ?", unmaskedFeedbackId).First(&feedback).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
response.New(http.StatusInternalServerError).Err("server error").Send(c)
} else if errors.Is(err, gorm.ErrRecordNotFound) {
Expand Down
8 changes: 8 additions & 0 deletions features/admin/fetch_reports_for_comment_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package admin

import (
"confesi/config"
"confesi/lib/encryption"
"confesi/lib/response"
"confesi/lib/utils"
"confesi/lib/validation"
Expand All @@ -18,12 +19,19 @@ func (h *handler) handleFetchReportForCommentById(c *gin.Context) {
return
}

unmaskedId, err := encryption.Unmask(req.CommentID)
if err != nil {
response.New(http.StatusBadRequest).Err(invalidValue.Error()).Send(c)
return
}

fetchResults := fetchResults{}

err = h.db.
Preload("ReportType").
Where(req.Next.Cursor("created_at >")).
Where("comment_id IS NOT NULL").
Where("comment_id = ?", unmaskedId).
Order("created_at ASC").
Find(&fetchResults.Reports).
Limit(config.AdminViewAllReportsPerCommentId).
Expand Down
8 changes: 8 additions & 0 deletions features/admin/fetch_reports_for_post_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package admin

import (
"confesi/config"
"confesi/lib/encryption"
"confesi/lib/response"
"confesi/lib/utils"
"confesi/lib/validation"
Expand All @@ -18,12 +19,19 @@ func (h *handler) handleFetchReportForPostById(c *gin.Context) {
return
}

unmaskedId, err := encryption.Unmask(req.PostID)
if err != nil {
response.New(http.StatusBadRequest).Err(invalidValue.Error()).Send(c)
return
}

fetchResults := fetchResults{}

err = h.db.
Preload("ReportType").
Where(req.Next.Cursor("created_at >")).
Where("post_id IS NOT NULL").
Where("post_id = ?", unmaskedId).
Order("created_at ASC").
Find(&fetchResults.Reports).
Limit(config.AdminViewAllReportsPerPostId).
Expand Down
6 changes: 3 additions & 3 deletions features/admin/get_report_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package admin

import (
"confesi/db"
"confesi/lib/encryption"
"confesi/lib/response"
"confesi/lib/utils"
"errors"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"gorm.io/gorm"
Expand All @@ -16,7 +16,7 @@ func (h *handler) handleGetReportById(c *gin.Context) {
// get id from query param id
id := c.Query("id")

idNumeric, err := strconv.Atoi(id)
unmaskedId, err := encryption.Unmask(id)
if err != nil {
response.New(http.StatusBadRequest).Err("invalid id").Send(c)
return
Expand All @@ -25,7 +25,7 @@ func (h *handler) handleGetReportById(c *gin.Context) {
report := db.Report{}
err = h.db.
Preload("ReportType"). // preload the ReportType field of the Report
Where("id = ?", idNumeric).
Where("id = ?", unmaskedId).
First(&report).
Error
if err != nil {
Expand Down
13 changes: 10 additions & 3 deletions features/admin/hide.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package admin
import (
"confesi/config/builders"
"confesi/db"
"confesi/lib/encryption"
"confesi/lib/logger"
"confesi/lib/response"
"confesi/lib/utils"
Expand Down Expand Up @@ -34,17 +35,23 @@ func (h *handler) handleHideContent(c *gin.Context) {
return
}

unmaskedContentId, err := encryption.Unmask(req.ContentID)
if err != nil {
response.New(http.StatusBadRequest).Err(invalidValue.Error()).Send(c)
return
}

hideLogEntry := db.HideLog{}
var commentOrPostIdMatcher string

var table string
if req.ContentType == "comment" {
table = "comments"
hideLogEntry.CommentID = &req.ContentID
hideLogEntry.CommentID = &db.EncryptedID{Val: unmaskedContentId}
commentOrPostIdMatcher = "comment_id"
} else if req.ContentType == "post" {
table = "posts"
hideLogEntry.PostID = &req.ContentID
hideLogEntry.PostID = &db.EncryptedID{Val: unmaskedContentId}
commentOrPostIdMatcher = "post_id"
} else {
response.New(http.StatusBadRequest).Err(invalidValue.Error()).Send(c)
Expand Down Expand Up @@ -180,7 +187,7 @@ func (h *handler) handleHideContent(c *gin.Context) {
} else if len(offenders) > 0 {
for _, tokenWithOffenderID := range offenders {
go fcm.New(h.fb.MsgClient).
ToTokens([]string{"tokenWithOffenderID.Token"}).
ToTokens([]string{tokenWithOffenderID.Token}).
WithMsg(builders.HideOffendingUserNoti()).
WithData(builders.HideOffendingUserData(tokenWithOffenderID.HideLogID)).
Send(*h.db)
Expand Down
Loading
Loading