Skip to content

Commit fcc3f79

Browse files
authored
Merge pull request #5 from ZaparooProject/comprehensive-ndef-fix
fix: comprehensive NDEF parsing - replace ParseNDEFData with ParseNDEFMessage for all tag types
2 parents da6431f + 2b33a3a commit fcc3f79

File tree

5 files changed

+10
-78
lines changed

5 files changed

+10
-78
lines changed

felica.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (f *FeliCaTag) ReadNDEF() (*NDEFMessage, error) {
359359
actualNdefData := ndefData[:ndefLength]
360360

361361
// Step 8: Parse NDEF data using existing parser
362-
return ParseNDEFData(actualNdefData)
362+
return ParseNDEFMessage(actualNdefData)
363363
}
364364

365365
// WriteNDEF writes NDEF data to the FeliCa tag

ndef.go

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -75,75 +75,6 @@ type NDEFRecord struct {
7575
Payload []byte
7676
}
7777

78-
// ParseNDEFData parses raw NDEF data into an NDEFMessage
79-
func ParseNDEFData(data []byte) (*NDEFMessage, error) {
80-
// SECURITY: Validate total data size before processing
81-
if len(data) > MaxNDEFMessageSize {
82-
return nil, fmt.Errorf("%w: data size %d exceeds maximum %d",
83-
ErrSecurityViolation, len(data), MaxNDEFMessageSize)
84-
}
85-
86-
if len(data) < 4 {
87-
return nil, fmt.Errorf("%w: data too short for NDEF", ErrInvalidNDEF)
88-
}
89-
90-
// Find NDEF start marker
91-
startIndex := bytes.LastIndex(data, ndefStart)
92-
if startIndex == -1 {
93-
return nil, ErrNoNDEF
94-
}
95-
96-
// Check for corrupted data with multiple NDEF starts
97-
if len(data) > startIndex+8 {
98-
nextStart := bytes.Index(data[startIndex+4:], ndefStart)
99-
if nextStart != -1 {
100-
startIndex += nextStart + 4
101-
}
102-
}
103-
104-
// Find NDEF end marker
105-
endIndex := bytes.Index(data[startIndex:], ndefEnd)
106-
if endIndex == -1 {
107-
return nil, fmt.Errorf("%w: end marker not found", ErrInvalidNDEF)
108-
}
109-
endIndex += startIndex
110-
111-
// Validate indices
112-
if startIndex >= endIndex {
113-
return nil, fmt.Errorf("%w: invalid start index", ErrInvalidNDEF)
114-
}
115-
116-
// SECURITY: Comprehensive bounds checking
117-
if startIndex+4 > len(data) {
118-
return nil, fmt.Errorf("%w: insufficient data for NDEF header", ErrInvalidNDEF)
119-
}
120-
121-
if endIndex > len(data) {
122-
return nil, fmt.Errorf("%w: invalid start index", ErrInvalidNDEF)
123-
}
124-
125-
// SECURITY: Validate text length before extraction
126-
textLength := endIndex - (startIndex + 4)
127-
if textLength > MaxNDEFPayloadSize {
128-
return nil, fmt.Errorf("%w: text payload %d exceeds maximum %d",
129-
ErrSecurityViolation, textLength, MaxNDEFPayloadSize)
130-
}
131-
132-
// Extract text from simple text record
133-
// TODO: Properly parse full NDEF structure
134-
text := string(data[startIndex+4 : endIndex])
135-
136-
return &NDEFMessage{
137-
Records: []NDEFRecord{
138-
{
139-
Type: NDEFTypeText,
140-
Text: text,
141-
Payload: data[startIndex:endIndex],
142-
},
143-
},
144-
}, nil
145-
}
146-
14778
// BuildNDEFMessage creates NDEF data from text
14879
// Deprecated: Use BuildTextMessage or BuildNDEFMessageEx for more flexibility
14980
func BuildNDEFMessage(text string) ([]byte, error) {

ndef_construction_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func TestCalculateNDEFHeaderErrors(t *testing.T) {
253253
}
254254
}
255255

256-
func TestParseNDEFDataSecurity(t *testing.T) {
256+
func TestParseNDEFMessageSecurity(t *testing.T) {
257257
t.Parallel()
258258
tests := []struct {
259259
errorType error
@@ -285,10 +285,10 @@ func TestParseNDEFDataSecurity(t *testing.T) {
285285
t.Run(tt.name, func(t *testing.T) {
286286
t.Parallel()
287287
data := make([]byte, tt.dataSize)
288-
_, err := ParseNDEFData(data)
288+
_, err := ParseNDEFMessage(data)
289289

290290
if (err != nil) != tt.wantError {
291-
t.Errorf("ParseNDEFData() error = %v, wantError %v", err, tt.wantError)
291+
t.Errorf("ParseNDEFMessage() error = %v, wantError %v", err, tt.wantError)
292292
return
293293
}
294294

@@ -300,7 +300,8 @@ func TestParseNDEFDataSecurity(t *testing.T) {
300300
!containsString(errStr, "security violation") &&
301301
!containsString(errStr, "invalid NDEF") &&
302302
!containsString(errStr, "no NDEF") {
303-
t.Errorf("ParseNDEFData() error type mismatch: got %q, expected to contain %q", errStr, expectedStr)
303+
t.Errorf("ParseNDEFMessage() error type mismatch: got %q, expected to contain %q",
304+
errStr, expectedStr)
304305
}
305306
}
306307
})

ntag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (t *NTAGTag) ReadNDEF() (*NDEFMessage, error) {
224224
data = data[:totalBytes]
225225
}
226226

227-
return ParseNDEFData(data)
227+
return ParseNDEFMessage(data)
228228
}
229229

230230
type ndefHeader struct {
@@ -392,7 +392,7 @@ func (t *NTAGTag) readNDEFBlockByBlock() (*NDEFMessage, error) {
392392
}
393393
}
394394

395-
return ParseNDEFData(data)
395+
return ParseNDEFMessage(data)
396396
}
397397

398398
// WriteNDEF writes NDEF data to the NTAG tag

parameter_validation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ func TestNDEFParameterValidation(t *testing.T) {
320320
for _, tt := range tests {
321321
t.Run(tt.name, func(t *testing.T) {
322322
t.Parallel()
323-
_, err := ParseNDEFData(tt.data)
323+
_, err := ParseNDEFMessage(tt.data)
324324
gotError := err != nil
325325

326326
if gotError != tt.wantError {
327-
t.Errorf("ParseNDEFData() error = %v, wantError %v (%s)", err, tt.wantError, tt.desc)
327+
t.Errorf("ParseNDEFMessage() error = %v, wantError %v (%s)", err, tt.wantError, tt.desc)
328328
}
329329
})
330330
}

0 commit comments

Comments
 (0)