Skip to content

Commit d963118

Browse files
authored
Merge pull request #22 from jattento/feature/add-security-check-unmarshal-bitmap
add remaining bytes check of bitmap unmarshal method
2 parents 35b29de + 6b7cdb2 commit d963118

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pkg/iso8583/bitmap.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package iso8583
22

33
import (
44
"errors"
5+
"fmt"
56
"math"
67

78
"github.com/jattento/go-iso8583/pkg/bitmap"
@@ -22,6 +23,11 @@ func (b *BITMAP) UnmarshalISO8583(byt []byte, length int, encoding string) (int,
2223
}
2324

2425
bcap := int(math.Ceil(float64(length) / float64(bitsInByte)))
26+
27+
if len(byt) < bcap {
28+
return 0, fmt.Errorf("bitmap should be %v bytes long but only %v bytes are avaiable", bcap, len(byt))
29+
}
30+
2531
b.Bitmap = bitmap.FromBytes(byt[:bcap])
2632
return bcap, nil
2733
}

pkg/iso8583/bitmap_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ func TestBITMAP_MarshalISO8583(t *testing.T) {
2727
func TestBITMAP_UnmarshalISO8583_nil_input(t *testing.T) {
2828
var bmap iso8583.BITMAP
2929

30+
n, bmapErr := bmap.UnmarshalISO8583([]byte{1, 1, 1}, 64, "ascii")
31+
32+
assert.Equal(t, 0, n)
33+
if assert.NotNil(t, bmapErr) {
34+
assert.Equal(t, bmapErr.Error(), "bitmap should be 8 bytes long but only 3 bytes are avaiable")
35+
}
36+
}
37+
38+
func TestBITMAP_UnmarshalISO8583_too_short_input(t *testing.T) {
39+
var bmap iso8583.BITMAP
40+
3041
n, bmapErr := bmap.UnmarshalISO8583(nil, 64, "ascii")
3142

3243
assert.Equal(t, 0, n)

0 commit comments

Comments
 (0)