Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 3c73ee0

Browse files
authored
Merge pull request #8 from philips-software/feature/replace-scary-characters
💀 Replace scary characters
2 parents c62f39d + ca9f171 commit 3c73ee0

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

iam/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
550550
if c.debugFile != nil {
551551
c.debugFile.WriteString(out)
552552
} else {
553-
fmt.Printf(out)
553+
fmt.Println(out)
554554
}
555555
}
556556
if err != nil {

logging/bundle.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ type LogData struct {
4646

4747
// Valid returns true if a resource is valid according to HSDP rules, false otherwise
4848
func (r *Resource) Valid() bool {
49-
if r.EventID != "" && r.TransactionID != "" && r.LogTime != "" && r.LogData.Message != "" {
50-
return true
49+
var u map[string]interface{}
50+
51+
if r.EventID == "" || r.TransactionID == "" || r.LogTime == "" || r.LogData.Message == "" {
52+
return false
53+
}
54+
if len(r.Custom) > 0 && json.Unmarshal(r.Custom, &u) != nil {
55+
return false
5156
}
52-
return false
57+
return true
5358
}

logging/client.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http/httputil"
1313
"net/url"
1414
"os"
15+
"strings"
1516

1617
"github.com/philips-software/go-hsdp-api/fhir"
1718
signer "github.com/philips-software/go-hsdp-signer"
@@ -32,6 +33,22 @@ var (
3233
ErrMissingSharedSecret = errors.New("missing shared secret")
3334
ErrMissingBaseURL = errors.New("missing base URL")
3435
ErrMissingProductKey = errors.New("missing ProductKey")
36+
37+
scaryMap = map[string]string{
38+
";": "💀[semicolon]",
39+
"\\\\": "🎃[backslash]",
40+
"&": "👻[amp]",
41+
">": "👿[gt]",
42+
"<": "👾[lt]",
43+
"=": "👾[equal]",
44+
"\\u": "🎃[utf]",
45+
"\\f": "🎃[ff]",
46+
"\\b": "🎃[bs]",
47+
"\\r": "🎃[cr]",
48+
"\\n": "🎃[lf]",
49+
"\\t": "🎃[tab]",
50+
"\\\"": "🎃[quote]",
51+
}
3552
)
3653

3754
// Storer defines the store operations for logging
@@ -71,7 +88,6 @@ type Client struct {
7188
url *url.URL
7289
httpClient *http.Client
7390
httpSigner *signer.Signer
74-
debugFile *os.File
7591
}
7692

7793
// Response holds a LogEvent response
@@ -215,6 +231,7 @@ func (c *Client) StoreResources(msgs []Resource, count int) (*Response, error) {
215231
j := 0
216232
for i := 0; i < count; i++ {
217233
msg := msgs[i]
234+
replaceScaryCharacters(&msg)
218235
if !msg.Valid() {
219236
invalid = append(invalid, msg)
220237
continue
@@ -268,3 +285,15 @@ func (c *Client) StoreResources(msgs []Resource, count int) (*Response, error) {
268285

269286
return resp, err
270287
}
288+
289+
func replaceScaryCharacters(msg *Resource) {
290+
if len(msg.Custom) == 0 {
291+
return
292+
}
293+
stringCustom := strings.Replace(string(msg.Custom), "\\\\", "🎃[backslash]", -1)
294+
295+
for s, r := range scaryMap {
296+
stringCustom = strings.Replace(stringCustom, s, r, -1)
297+
}
298+
msg.Custom = []byte(stringCustom)
299+
}

logging/client_test.go

+33-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package logging
22

33
import (
4+
"encoding/json"
45
"io"
56
"io/ioutil"
67
"net/http"
78
"net/http/httptest"
89
"os"
910
"testing"
1011

12+
"github.com/stretchr/testify/assert"
13+
1114
"github.com/Jeffail/gabs"
1215
signer "github.com/philips-software/go-hsdp-signer"
1316
)
@@ -187,17 +190,10 @@ func TestStoreResourcesWithInvalidKeypair(t *testing.T) {
187190
}
188191

189192
resp, err := client.StoreResources(resource, len(resource))
190-
if resp == nil {
191-
t.Errorf("Unexpected nil value for response")
192-
return
193-
}
193+
assert.NotNil(t, resp)
194194
_ = err.Error() // Just to up coverage
195-
if resp.StatusCode != http.StatusForbidden {
196-
t.Errorf("Expected HTTP 403, Got: %d", resp.StatusCode)
197-
}
198-
if err == nil {
199-
t.Errorf("Expected error response")
200-
}
195+
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
196+
assert.NotNil(t, err)
201197
}
202198

203199
func TestConfig(t *testing.T) {
@@ -219,3 +215,30 @@ func TestConfig(t *testing.T) {
219215
}
220216
}
221217
}
218+
219+
func TestReplaceScaryCharacters(t *testing.T) {
220+
var invalidResource = Resource{
221+
ResourceType: "LogEvent",
222+
Custom: []byte(`{
223+
"foo": "bar",
224+
"bad1": ";",
225+
"bad2": "<key/>",
226+
"bad3": "&amp;",
227+
"bad4": "a\\b",
228+
"bad5": "a\b"
229+
}`),
230+
}
231+
replaceScaryCharacters(&invalidResource)
232+
233+
var custom map[string]interface{}
234+
err := json.Unmarshal(invalidResource.Custom, &custom)
235+
if !assert.Nil(t, err) {
236+
return
237+
}
238+
assert.Equal(t, "bar", custom["foo"].(string))
239+
assert.Equal(t, "💀[semicolon]", custom["bad1"].(string))
240+
assert.Equal(t, "👾[lt]key/👿[gt]", custom["bad2"].(string))
241+
assert.Equal(t, "👻[amp]amp💀[semicolon]", custom["bad3"].(string))
242+
assert.Equal(t, "a🎃[backslash]b", custom["bad4"].(string))
243+
assert.Equal(t, "a🎃[bs]", custom["bad5"].(string))
244+
}

0 commit comments

Comments
 (0)