Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
1e4c6be7
Commit
1e4c6be7
authored
Jun 18, 2018
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fully validate p2p addresses when decoding from bytes
Otherwise, .String() can panic.
parent
5416c663
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
10 deletions
+31
-10
codec.go
codec.go
+5
-0
transcoders.go
transcoders.go
+26
-10
No files found.
codec.go
View file @
1e4c6be7
...
...
@@ -88,6 +88,11 @@ func validateBytes(b []byte) (err error) {
return
fmt
.
Errorf
(
"invalid value for size"
)
}
err
=
p
.
Transcoder
.
ValidateBytes
(
b
[
:
size
])
if
err
!=
nil
{
return
err
}
b
=
b
[
size
:
]
}
...
...
transcoders.go
View file @
1e4c6be7
...
...
@@ -14,17 +14,21 @@ import (
type
Transcoder
interface
{
StringToBytes
(
string
)
([]
byte
,
error
)
BytesToString
([]
byte
)
(
string
,
error
)
ValidateBytes
([]
byte
)
error
}
func
NewTranscoderFromFunctions
(
s2b
func
(
string
)
([]
byte
,
error
),
b2s
func
([]
byte
)
(
string
,
error
))
Transcoder
{
return
twrp
{
s2b
,
b2s
}
func
NewTranscoderFromFunctions
(
s2b
func
(
string
)
([]
byte
,
error
),
b2s
func
([]
byte
)
(
string
,
error
),
val
func
([]
byte
)
error
,
)
Transcoder
{
return
twrp
{
s2b
,
b2s
,
val
}
}
type
twrp
struct
{
strtobyte
func
(
string
)
([]
byte
,
error
)
bytetostr
func
([]
byte
)
(
string
,
error
)
validbyte
func
([]
byte
)
error
}
func
(
t
twrp
)
StringToBytes
(
s
string
)
([]
byte
,
error
)
{
...
...
@@ -34,8 +38,15 @@ func (t twrp) BytesToString(b []byte) (string, error) {
return
t
.
bytetostr
(
b
)
}
var
TranscoderIP4
=
NewTranscoderFromFunctions
(
ip4StB
,
ipBtS
)
var
TranscoderIP6
=
NewTranscoderFromFunctions
(
ip6StB
,
ipBtS
)
func
(
t
twrp
)
ValidateBytes
(
b
[]
byte
)
error
{
if
t
.
validbyte
==
nil
{
return
nil
}
return
t
.
validbyte
(
b
)
}
var
TranscoderIP4
=
NewTranscoderFromFunctions
(
ip4StB
,
ipBtS
,
nil
)
var
TranscoderIP6
=
NewTranscoderFromFunctions
(
ip6StB
,
ipBtS
,
nil
)
func
ip4StB
(
s
string
)
([]
byte
,
error
)
{
i
:=
net
.
ParseIP
(
s
)
.
To4
()
...
...
@@ -57,7 +68,7 @@ func ipBtS(b []byte) (string, error) {
return
net
.
IP
(
b
)
.
String
(),
nil
}
var
TranscoderPort
=
NewTranscoderFromFunctions
(
portStB
,
portBtS
)
var
TranscoderPort
=
NewTranscoderFromFunctions
(
portStB
,
portBtS
,
nil
)
func
portStB
(
s
string
)
([]
byte
,
error
)
{
i
,
err
:=
strconv
.
Atoi
(
s
)
...
...
@@ -77,7 +88,7 @@ func portBtS(b []byte) (string, error) {
return
strconv
.
Itoa
(
int
(
i
)),
nil
}
var
TranscoderOnion
=
NewTranscoderFromFunctions
(
onionStB
,
onionBtS
)
var
TranscoderOnion
=
NewTranscoderFromFunctions
(
onionStB
,
onionBtS
,
nil
)
func
onionStB
(
s
string
)
([]
byte
,
error
)
{
addr
:=
strings
.
Split
(
s
,
":"
)
...
...
@@ -120,7 +131,7 @@ func onionBtS(b []byte) (string, error) {
return
addr
+
":"
+
strconv
.
Itoa
(
int
(
port
)),
nil
}
var
TranscoderP2P
=
NewTranscoderFromFunctions
(
p2pStB
,
p2pBtS
)
var
TranscoderP2P
=
NewTranscoderFromFunctions
(
p2pStB
,
p2pBtS
,
p2pVal
)
func
p2pStB
(
s
string
)
([]
byte
,
error
)
{
// the address is a varint prefixed multihash string representation
...
...
@@ -131,6 +142,11 @@ func p2pStB(s string) ([]byte, error) {
return
m
,
nil
}
func
p2pVal
(
b
[]
byte
)
error
{
_
,
err
:=
mh
.
Cast
(
b
)
return
err
}
func
p2pBtS
(
b
[]
byte
)
(
string
,
error
)
{
m
,
err
:=
mh
.
Cast
(
b
)
if
err
!=
nil
{
...
...
@@ -139,7 +155,7 @@ func p2pBtS(b []byte) (string, error) {
return
m
.
B58String
(),
nil
}
var
TranscoderUnix
=
NewTranscoderFromFunctions
(
unixStB
,
unixBtS
)
var
TranscoderUnix
=
NewTranscoderFromFunctions
(
unixStB
,
unixBtS
,
nil
)
func
unixStB
(
s
string
)
([]
byte
,
error
)
{
return
[]
byte
(
s
),
nil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment