hash user ip

This commit is contained in:
Umputun
2018-03-22 20:11:57 -05:00
parent a9aeb7e4f0
commit 9a5aa72b0d
7 changed files with 33 additions and 49 deletions
+4 -2
View File
@@ -54,12 +54,14 @@ func TestRemark_Import(t *testing.T) {
}
// makes new boltdb, put two records
func prep(t *testing.T) *store.BoltDB {
func prep(t *testing.T) *store.Service {
os.Remove(testDb)
b, err := store.NewBoltDB(store.BoltSite{SiteID: "radio-t", FileName: testDb})
boltStore, err := store.NewBoltDB(store.BoltSite{SiteID: "radio-t", FileName: testDb})
assert.Nil(t, err)
b := &store.Service{Interface: boltStore}
comment := store.Comment{
ID: "efbc17f177ee1a1c0ee6e1e025749966ec071adc",
Text: `some text, <a href="http://radio-t.com">link</a>`,
+1 -1
View File
@@ -96,7 +96,7 @@ func TestServer_CreateAndGet(t *testing.T) {
assert.Equal(t, "<p><strong>test</strong> <em>123</em> http://radio-t.com</p>", comment.Text)
assert.Equal(t, store.User{Name: "developer one", ID: "dev",
Picture: "/api/v1/avatar/remark.image",
Profile: "https://remark42.com", Admin: true, Blocked: false, IP: "127.0.0.1"},
Profile: "https://remark42.com", Admin: true, Blocked: false, IP: "4b84b15bff6ee5796152495a230e45e3d7e947d9"},
comment.User)
t.Logf("%+v", comment)
}
-15
View File
@@ -9,7 +9,6 @@ import (
"time"
"github.com/coreos/bbolt"
"github.com/google/uuid"
"github.com/pkg/errors"
)
@@ -81,20 +80,6 @@ func NewBoltDB(sites ...BoltSite) (*BoltDB, error) {
// Create saves new comment to store. Adds to posts bucket, reference to last and user bucket and increments count bucket
func (b *BoltDB) Create(comment Comment) (commentID string, err error) {
// fill ID and time if empty
if comment.ID == "" {
comment.ID = uuid.New().String()
}
if comment.Timestamp.IsZero() {
comment.Timestamp = time.Now()
}
// reset votes if nothing
if comment.Votes == nil {
comment.Votes = make(map[string]bool)
}
comment.sanitize() // clear potentially dangerous js from all parts of comment
bdb, err := b.db(comment.Locator.SiteID)
if err != nil {
return "", err
+4 -3
View File
@@ -11,7 +11,7 @@ import (
var testDb = "/tmp/test-remark.db"
func TestBoltDB_CreateAndFind(t *testing.T) {
var b Interface = prep(t)
var b = prep(t)
defer os.Remove(testDb)
res, err := b.Find(Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "time")
@@ -190,11 +190,12 @@ func TestBoltDB_GetForUser(t *testing.T) {
}
// makes new boltdb, put two records
func prep(t *testing.T) *BoltDB {
func prep(t *testing.T) *Service {
os.Remove(testDb)
b, err := NewBoltDB(BoltSite{FileName: "/tmp/test-remark.db", SiteID: "radio-t"})
boltStore, err := NewBoltDB(BoltSite{FileName: "/tmp/test-remark.db", SiteID: "radio-t"})
assert.Nil(t, err)
b := &Service{Interface: boltStore}
comment := Comment{
ID: "id-1",
+3 -4
View File
@@ -97,8 +97,8 @@ func (c *Comment) sanitize() {
c.Text = strings.Replace(c.Text, "\t", "", -1)
}
// HashUserFields replace sensitive fields with hashes
func (c *Comment) hashUserFields() {
// hashIP replace sensitive fields with hashes
func (u *User) hashIP() {
hashVal := func(val string) string {
if _, err := strconv.ParseUint(val, 16, 64); err == nil || val == "" {
@@ -112,6 +112,5 @@ func (c *Comment) hashUserFields() {
return fmt.Sprintf("%x", h.Sum(nil))
}
c.User.IP = hashVal(c.User.IP)
c.User.ID = hashVal(c.User.ID)
u.IP = hashVal(u.IP)
}
-24
View File
@@ -58,27 +58,3 @@ func TestComment_PrepareUntrusted(t *testing.T) {
assert.Equal(t, User{ID: "username"}, comment.User)
}
func TestComment_HashUserFields(t *testing.T) {
tbl := []struct {
inp Comment
out Comment
}{
{inp: Comment{}, out: Comment{}},
{
inp: Comment{
Text: "blah",
User: User{ID: "my id", IP: "127.0.0.1"},
},
out: Comment{
Text: "blah",
User: User{ID: "de58071dda71e1783b6deb931ddb48bb66966f79", IP: "4b84b15bff6ee5796152495a230e45e3d7e947d9"},
},
},
}
for n, tt := range tbl {
tt.inp.hashUserFields()
assert.Equal(t, tt.out, tt.inp, "check #%d", n)
}
}
+21
View File
@@ -3,6 +3,7 @@ package store
import (
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
)
@@ -12,6 +13,26 @@ type Service struct {
EditDuration time.Duration
}
// Create prepares comment and forward to Interface.Create
func (s *Service) Create(comment Comment) (commentID string, err error) {
// fill ID and time if empty
if comment.ID == "" {
comment.ID = uuid.New().String()
}
if comment.Timestamp.IsZero() {
comment.Timestamp = time.Now()
}
// reset votes if nothing
if comment.Votes == nil {
comment.Votes = make(map[string]bool)
}
comment.sanitize() // clear potentially dangerous js from all parts of comment
comment.User.hashIP() // replace ip by hash
return s.Interface.Create(comment)
}
// SetPin pin/un-pin comment as special
func (s *Service) SetPin(locator Locator, commentID string, status bool) error {
comment, err := s.Get(locator, commentID)