Skip to content

Commit 84cbff1

Browse files
Sanitizing invalid literal text string
- modified tag11 and tag13 packets - modified sub06, sub20, sub24, sub26, and sub28 sub-packets
1 parent 1d689fb commit 84cbff1

File tree

11 files changed

+62
-73
lines changed

11 files changed

+62
-73
lines changed

packet/parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ const (
285285
Literal Data Packet (tag 11) (19 bytes)
286286
62 00 5a 19 0d e4 48 65 6c 6c 6f 20 77 6f 72 6c 64 0d 0a
287287
Literal data format: b (binary)
288-
File name: <null>
288+
File name (0 byte)
289289
Modification time of a file: 2017-11-25T06:29:56Z
290290
5a 19 0d e4
291291
Literal data (13 bytes)

packet/tags/sub06.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ func newSub06(cxt *context.Context, subID values.SuboacketID, body []byte) Subs
1919

2020
// Parse parsing Regular Expression Sub-packet
2121
func (s *sub06) Parse() (*info.Item, error) {
22-
rootInfo := s.ToItem()
23-
rootInfo.Value = string(s.reader.GetBody())
24-
return rootInfo, nil
22+
return values.NewText(s.reader.GetBody(), s.ToItem().Name).ToItem(s.cxt.Debug()), nil
2523
}
2624

2725
/* Copyright 2016-2019 Spiegel

packet/tags/sub20.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,21 @@ func (s *sub20) Parse() (*info.Item, error) {
4747
if err != nil {
4848
return rootInfo, errs.Wrapf(err, "illegal name in parsing sub packet %d (length: %d bytes)", int(s.subID), nameLength)
4949
}
50-
rootInfo.Add(info.NewItem(
51-
info.Name("Name"),
52-
info.Value(string(name)),
53-
))
50+
rootInfo.Add(values.NewText(name, "Name").ToItem(s.cxt.Debug()))
5451
value, err := s.reader.ReadBytes(int64(binary.BigEndian.Uint16(valueLength)))
5552
if err != nil {
5653
return rootInfo, errs.Wrapf(err, "illegal value in parsing sub packet %d (length: %d bytes)", int(s.subID), valueLength)
5754
}
58-
itm := info.NewItem(
59-
info.Name("Value"),
60-
)
6155
if human != 0x00 {
62-
itm.Value = string(value)
56+
//human readable data (text)
57+
rootInfo.Add(values.NewText(value, "Value").ToItem(s.cxt.Debug()))
6358
} else {
64-
itm.Dump = values.DumpBytes(value, true).String()
59+
//binary data
60+
rootInfo.Add(info.NewItem(
61+
info.Name("Value"),
62+
info.DumpStr(values.DumpBytes(value, true).String()),
63+
))
6564
}
66-
rootInfo.Add(itm)
67-
6865
return rootInfo, nil
6966
}
7067

packet/tags/sub24.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ func newSub24(cxt *context.Context, subID values.SuboacketID, body []byte) Subs
1919

2020
// Parse parsing Preferred Key Server Sub-packet
2121
func (s *sub24) Parse() (*info.Item, error) {
22-
rootInfo := s.ToItem()
23-
rootInfo.Value = string(s.reader.GetBody())
24-
return rootInfo, nil
22+
return values.NewText(s.reader.GetBody(), s.ToItem().Name).ToItem(s.cxt.Debug()), nil
2523
}
2624

2725
/* Copyright 2016-2019 Spiegel

packet/tags/sub26.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ func newSub26(cxt *context.Context, subID values.SuboacketID, body []byte) Subs
1919

2020
// Parse parsing Policy URI Sub-packet
2121
func (s *sub26) Parse() (*info.Item, error) {
22-
rootInfo := s.ToItem()
23-
rootInfo.Value = string(s.reader.GetBody())
24-
return rootInfo, nil
22+
return values.NewText(s.reader.GetBody(), s.ToItem().Name).ToItem(s.cxt.Debug()), nil
2523
}
2624

2725
/* Copyright 2016-2019 Spiegel

packet/tags/sub28.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ func newSub28(cxt *context.Context, subID values.SuboacketID, body []byte) Subs
1919

2020
// Parse parsing Signer's User ID Sub-packet
2121
func (s *sub28) Parse() (*info.Item, error) {
22-
rootInfo := s.ToItem()
23-
rootInfo.Value = string(s.reader.GetBody())
24-
return rootInfo, nil
22+
return values.NewText(s.reader.GetBody(), s.ToItem().Name).ToItem(s.cxt.Debug()), nil
2523
}
2624

2725
/* Copyright 2016-2019 Spiegel

packet/tags/tag11.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (t *tag11) Parse() (*info.Item, error) {
3434
if err != nil {
3535
return rootInfo, errs.Wrapf(err, "illegal length of file name in parsing tag %d (length: %d bytes)", int(t.tag), int64(flen))
3636
}
37-
rootInfo.Add(fname.ToItem(t.cxt.Debug()))
37+
rootInfo.Add(fname.ToItem(t.cxt.Literal()))
3838
ftime, err := values.NewDateTime(t.reader, t.cxt.UTC())
3939
if err != nil {
4040
return rootInfo, errs.Wrapf(err, "illegal timestump of file in parsing tag %d", int(t.tag))

packet/tags/tag11_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
tag11Result1 = `Literal Data Packet (tag 11) (19 bytes)
2121
62 00 5a 19 0d e4 48 65 6c 6c 6f 20 77 6f 72 6c 64 0d 0a
2222
Literal data format: b (binary)
23-
File name: <null>
23+
File name (0 byte)
2424
Modification time of a file: 2017-11-25T06:29:56Z
2525
5a 19 0d e4
2626
Literal data (13 bytes)
@@ -79,7 +79,7 @@ func TestTag11(t *testing.T) {
7979
}
8080
}
8181

82-
/* Copyright 2017 Spiegel
82+
/* Copyright 2017-2019 Spiegel
8383
*
8484
* Licensed under the Apache License, Version 2.0 (the "License");
8585
* you may not use this file except in compliance with the License.

packet/tags/tag13.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ func newTag13(cxt *context.Context, tag values.TagID, body []byte) Tags {
2020
// Parse parsing User ID Packet
2121
func (t *tag13) Parse() (*info.Item, error) {
2222
rootInfo := t.ToItem()
23-
body := t.reader.GetBody()
24-
rootInfo.Add(info.NewItem(
25-
info.Name("User ID"),
26-
info.Value(string(body)),
27-
info.DumpStr(values.DumpBytes(body, t.cxt.Debug()).String()),
28-
))
23+
rootInfo.Add(values.NewText(t.reader.GetBody(), "User ID").ToItem(t.cxt.Debug()))
2924
return rootInfo, nil
3025
}
3126

packet/values/literal.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,67 +34,66 @@ func (l LiteralFormat) String() string {
3434
return string(l)
3535
}
3636

37-
//LiteralFname is file name of literal data
38-
type LiteralFname struct {
39-
name []byte
37+
//Text is literal text
38+
type Text struct {
39+
name string
40+
body []byte
4041
}
4142

42-
//NewLiteralFname returns new LiteralFname instance
43-
func NewLiteralFname(r *reader.Reader, l int64) (*LiteralFname, error) {
43+
//NewText returns new Text instance
44+
func NewText(body []byte, name string) *Text {
45+
return &Text{name: name, body: body}
46+
}
47+
48+
//NewLiteralFname returns new Text instance for file name of literal data
49+
func NewLiteralFname(r *reader.Reader, l int64) (*Text, error) {
50+
name := "File name"
4451
if r == nil {
45-
return &LiteralFname{name: nil}, nil
52+
return NewText(nil, name), nil
4653
}
4754
if l < 1 {
48-
return &LiteralFname{name: nil}, nil
55+
return NewText(nil, name), nil
4956
}
5057
data, err := r.ReadBytes(l)
5158
if err != nil {
5259
return nil, errs.Wrapf(err, "illegal file name of literal packet (length: %d bytes)", l)
5360
}
54-
return &LiteralFname{name: data}, err
61+
return NewText(data, name), nil
5562
}
5663

57-
//ToItem returns Item instance
58-
func (l *LiteralFname) ToItem(dumpFlag bool) *info.Item {
59-
if l == nil {
60-
return nil
61-
}
62-
name := "File name"
63-
if l.name == nil {
64+
func (t *Text) ToItem(dumpFlag bool) *info.Item {
65+
if t == nil {
6466
return info.NewItem(
65-
info.Name(name),
66-
info.Value("<null>"),
67+
info.Note("null"),
6768
)
6869
}
69-
fname := stripString(l.name)
70-
if len(fname) == 0 {
70+
if t.body == nil || len(t.body) == 0 {
7171
return info.NewItem(
72-
info.Name(name),
73-
info.Value("<invalid text string>"),
74-
info.DumpStr(DumpBytes(l.name, dumpFlag).String()),
72+
info.Name(t.name),
73+
info.Note("0 byte"),
7574
)
7675
}
77-
return info.NewItem(
78-
info.Name(name),
79-
info.Value(fname),
80-
info.DumpStr(DumpBytes(l.name, dumpFlag).String()),
81-
)
82-
}
83-
84-
func stripString(b []byte) string {
85-
if !utf8.Valid(b) {
86-
return ""
76+
if !utf8.Valid(t.body) {
77+
return info.NewItem(
78+
info.Name(t.name),
79+
info.Note("invalid text string"),
80+
info.DumpStr(DumpBytes(t.body, true).String()),
81+
)
8782
}
8883
rs := []rune{}
89-
for _, r := range string(b) {
84+
for _, r := range string(t.body) {
9085
if unicode.IsControl(r) {
9186
c := fmt.Sprintf("(%U)", r)
9287
rs = append(rs, []rune(c)...)
9388
} else {
9489
rs = append(rs, r)
9590
}
9691
}
97-
return string(rs)
92+
return info.NewItem(
93+
info.Name(t.name),
94+
info.Value(string(rs)),
95+
info.DumpStr(DumpBytes(t.body, dumpFlag).String()),
96+
)
9897
}
9998

10099
//RawData returns info.Item instance for raw data

0 commit comments

Comments
 (0)