-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparser_dtd_test.go
More file actions
902 lines (749 loc) · 34.5 KB
/
Copy pathparser_dtd_test.go
File metadata and controls
902 lines (749 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
package helium_test
import (
"bytes"
"context"
"errors"
"io"
"io/fs"
"strings"
"sync"
"testing"
"testing/fstest"
"time"
"github.com/lestrrat-go/helium"
"github.com/stretchr/testify/require"
)
// underReportingFS serves a single DTD file whose Stat under-reports the
// size while Read produces far more than MaxExternalDTDSize. It also counts
// the bytes pulled from the underlying reader so the test can assert the
// read is bounded.
type underReportingFS struct {
read *int64
}
type underReportingFile struct {
read *int64
}
// Stat lies: it claims a tiny size so the early precheck does not catch the
// oversized content.
func (f *underReportingFile) Stat() (fs.FileInfo, error) {
return underReportingInfo{}, nil
}
func (f *underReportingFile) Read(p []byte) (int, error) {
// Endless stream of spaces; the bounded read must stop it.
for i := range p {
p[i] = ' '
}
*f.read += int64(len(p))
return len(p), nil
}
func (f *underReportingFile) Close() error { return nil }
type underReportingInfo struct{}
func (underReportingInfo) Name() string { return "huge.dtd" }
func (underReportingInfo) Size() int64 { return 1 }
func (underReportingInfo) Mode() fs.FileMode { return 0 }
func (underReportingInfo) ModTime() time.Time { return time.Time{} }
func (underReportingInfo) IsDir() bool { return false }
func (underReportingInfo) Sys() any { return nil }
// errReadingFS serves a DTD whose Read returns a full buffer of bytes (taking
// the running total past the configured cap) together with a NON-EOF error on
// the cap-crossing read. The cap must be enforced against the bytes that were
// returned, before the read error is inspected, so the size-cap error still
// fires. A small cap is used so the bounded read does not pull megabytes; the
// fake records whether the simulated read error was actually returned so the
// test can prove the error path was exercised.
type errReadingFS struct {
cap int
hitReadErr *bool
}
type errReadingFile struct {
cap int
read int64
hitReadErr *bool
}
func (f *errReadingFile) Stat() (fs.FileInfo, error) { return underReportingInfo{}, nil }
func (f *errReadingFile) Read(p []byte) (int, error) {
for i := range p {
p[i] = ' '
}
f.read += int64(len(p))
// Once enough bytes have been handed out to reach the cap, return the
// filled buffer alongside a non-EOF error. A reader that handled the
// error before checking the byte count would escape the cap.
if f.read >= int64(f.cap) {
*f.hitReadErr = true
return len(p), errors.New("simulated transport failure")
}
return len(p), nil
}
func (f *errReadingFile) Close() error { return nil }
// overReportingFS serves a single small, valid DTD whose Stat over-reports the
// size as MaxExternalDTDSize+1. The actual content is well under the cap, so
// the parse must succeed: this proves Stat is advisory and never used to
// reject.
type overReportingFS struct {
data []byte
}
type overReportingFile struct {
data []byte
off int
}
// Stat lies the other way: it claims a size above the cap even though Read
// yields only a few valid bytes.
func (f *overReportingFile) Stat() (fs.FileInfo, error) {
return overReportingInfo{}, nil
}
func (f *overReportingFile) Read(p []byte) (int, error) {
if f.off >= len(f.data) {
return 0, io.EOF
}
n := copy(p, f.data[f.off:])
f.off += n
return n, nil
}
func (f *overReportingFile) Close() error { return nil }
type overReportingInfo struct{}
func (overReportingInfo) Name() string { return "small.dtd" }
func (overReportingInfo) Size() int64 { return helium.MaxExternalDTDSize + 1 }
func (overReportingInfo) Mode() fs.FileMode { return 0 }
func (overReportingInfo) ModTime() time.Time { return time.Time{} }
func (overReportingInfo) IsDir() bool { return false }
func (overReportingInfo) Sys() any { return nil }
// partialReadFS serves a small DTD whose Read hands back a few valid bytes and
// then a NON-EOF error (a truncated/partial read) well under the size cap. A
// truncated external subset must surface the read error, and must never be
// silently treated as an absent DTD.
type partialReadFS struct {
prefix []byte
}
type partialReadFile struct {
prefix []byte
done bool
}
func (f *partialReadFile) Stat() (fs.FileInfo, error) { return underReportingInfo{}, nil }
func (f *partialReadFile) Read(p []byte) (int, error) {
if f.done {
return 0, io.ErrUnexpectedEOF
}
n := copy(p, f.prefix)
f.done = true
return n, io.ErrUnexpectedEOF
}
func (f *partialReadFile) Close() error { return nil }
// recordingFS wraps an fs.FS and records every path passed to Open, so a test
// can assert which resources a parse attempted to load.
type recordingFS struct {
inner fs.FS
mu sync.Mutex
opened []string
}
// dtdLoadDoc references an external subset by SYSTEM id. A valid content model
// for <doc> is declared in the external subset so a validating parse succeeds
// once the subset is actually loaded.
const dtdLoadDocName = "sub.dtd"
func dtdLoadDoc() string {
return `<?xml version="1.0"?>` + "\n" +
`<!DOCTYPE doc SYSTEM "` + dtdLoadDocName + `">` + "\n" +
`<doc>hello</doc>`
}
func dtdLoadFS() fstest.MapFS {
return fstest.MapFS{
dtdLoadDocName: &fstest.MapFile{Data: []byte("<!ELEMENT doc (#PCDATA)>\n")},
}
}
// warnCaptureSAX embeds the default TreeBuilder (so the full DOM is still
// built) and records every SAX Warning delivered during the parse. A wrapping
// SAX handler takes the generic SAX-dispatch path (pctx.treeBuilder stays nil),
// which is a supported configuration.
type warnCaptureSAX struct {
*helium.TreeBuilder
warnings []error
}
func (w *warnCaptureSAX) Warning(_ context.Context, err error) error {
w.warnings = append(w.warnings, err)
return nil
}
func TestParseExternalDTDLimits(t *testing.T) {
t.Parallel()
t.Run("size limit", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "huge.dtd">
<r/>`
// An oversized external DTD must be rejected with a parse error rather
// than being read whole into memory (potential OOM/hang).
oversized := bytes.Repeat([]byte(" "), helium.MaxExternalDTDSize+1)
fsys := fstest.MapFS{"huge.dtd": &fstest.MapFile{Data: oversized}}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
_, err := p.Parse(t.Context(), []byte(input))
require.Error(t, err, "oversized external DTD must produce a parse error")
})
t.Run("bounded read", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "huge.dtd">
<r/>`
// A source whose Stat under-reports its size must still be rejected by
// the bounded read, and that read must not consume more than
// MaxExternalDTDSize+1 bytes from the underlying reader.
var read int64
fsys := underReportingFS{read: &read}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
_, err := p.Parse(t.Context(), []byte(input))
require.Error(t, err, "oversized external DTD must produce a parse error even when Stat under-reports size")
require.ErrorIs(t, err, helium.ErrExternalDTDTooLarge, "rejection must come from the byte-count cap")
// The bounded read must consume exactly MaxExternalDTDSize+1 bytes: enough
// to prove the cap was exceeded, but no more. An implementation that
// rejected before reading (e.g. trusting an advisory Stat) would leave
// read==0 and fail the lower bound; one without a cap would overrun it.
require.Equal(t, int64(helium.MaxExternalDTDSize)+1, read, "bounded read must consume exactly MaxExternalDTDSize+1 bytes")
})
t.Run("a read error is still capped", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "huge.dtd">
<r/>`
// Use a small cap so the bounded read does not pull a 10 MiB stream. The
// cap-crossing read returns n>0 plus a non-EOF error. The size cap must
// still fire: the returned bytes already exceed the configured cap.
const smallCap = 4096
var hitReadErr bool
fsys := errReadingFS{cap: smallCap, hitReadErr: &hitReadErr}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).
MaxExternalDTDBytes(smallCap).FS(fsys)
_, err := p.Parse(t.Context(), []byte(input))
require.True(t, hitReadErr, "the simulated non-EOF read error must actually be returned by the fake")
require.Error(t, err, "oversized external DTD must produce a parse error even when the read also errors")
require.ErrorIs(t, err, helium.ErrExternalDTDTooLarge, "size cap must be enforced before the read error is handled")
})
t.Run("the stat size is advisory", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "small.dtd">
<r/>`
// A small, valid DTD whose Stat over-reports its size must still load:
// the cap is enforced against actual bytes read, not the advisory Stat.
// The DTD defaults an attribute so the test observes that the external
// subset was actually loaded and applied, not silently skipped.
fsys := overReportingFS{data: []byte("<!ELEMENT r EMPTY>\n<!ATTLIST r x CDATA \"default\">")}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "small valid DTD must load even when Stat over-reports its size")
root := doc.DocumentElement()
require.NotNil(t, root, "root element should exist")
x, ok := root.GetAttribute("x")
require.True(t, ok, "external DTD ATTLIST default must be applied, proving the DTD was loaded")
require.Equal(t, "default", x, "defaulted attribute value must come from the external DTD")
})
t.Run("a configurable limit", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "ext.dtd">
<r/>`
t.Run("custom small limit rejects larger DTD", func(t *testing.T) {
t.Parallel()
// A 2 KiB DTD must be rejected when the configured cap is 1 KiB.
oversized := bytes.Repeat([]byte(" "), 2<<10)
fsys := fstest.MapFS{extDTDName: &fstest.MapFile{Data: oversized}}
p := helium.NewParser().BlockXXE(false).
LoadExternalDTD(true).
DefaultDTDAttributes(true).
MaxExternalDTDBytes(1 << 10).
FS(fsys)
_, err := p.Parse(t.Context(), []byte(input))
require.Error(t, err, "DTD larger than the configured cap must be rejected")
require.ErrorIs(t, err, helium.ErrExternalDTDTooLarge, "rejection must come from the byte-count cap")
})
t.Run("custom small limit allows smaller DTD", func(t *testing.T) {
t.Parallel()
// A DTD well under the 1 KiB cap must still load. It defaults an
// attribute so the test observes that the DTD was actually applied,
// not silently skipped.
fsys := fstest.MapFS{extDTDName: &fstest.MapFile{Data: []byte("<!ELEMENT r EMPTY>\n<!ATTLIST r x CDATA \"default\">")}}
p := helium.NewParser().BlockXXE(false).
LoadExternalDTD(true).
DefaultDTDAttributes(true).
MaxExternalDTDBytes(1 << 10).
FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "DTD under the configured cap must load")
root := doc.DocumentElement()
require.NotNil(t, root, "root element should exist")
x, ok := root.GetAttribute("x")
require.True(t, ok, "external DTD ATTLIST default must be applied, proving the DTD was loaded")
require.Equal(t, "default", x, "defaulted attribute value must come from the external DTD")
})
t.Run("default cap allows a normal DTD over a small custom cap", func(t *testing.T) {
t.Parallel()
// Without configuring a custom cap, a DTD larger than 1 KiB (but well
// under the 10 MiB default) must still load. It defaults an attribute
// so the test observes that the DTD was actually applied.
large := append([]byte("<!ELEMENT r EMPTY>\n<!ATTLIST r x CDATA \"default\">"), bytes.Repeat([]byte(" "), 4<<10)...)
fsys := fstest.MapFS{extDTDName: &fstest.MapFile{Data: large}}
p := helium.NewParser().BlockXXE(false).
LoadExternalDTD(true).
DefaultDTDAttributes(true).
FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "DTD under the default cap must load")
root := doc.DocumentElement()
require.NotNil(t, root, "root element should exist")
x, ok := root.GetAttribute("x")
require.True(t, ok, "external DTD ATTLIST default must be applied, proving the DTD was loaded")
require.Equal(t, "default", x, "defaulted attribute value must come from the external DTD")
})
// The sentinel convention matches every other Parser byte cap
// (MaxNodeContentSize, MaxEntityAmplification, ...): 0 selects the built-in
// default, and a negative value removes the cap. Build a DTD larger than the
// 10 MiB default cap, but with no single whitespace run over the
// node-content cap, so only the external-DTD byte cap decides the outcome.
overDefault := []byte("<!ELEMENT r EMPTY>\n<!ATTLIST r x CDATA \"default\">\n")
for range 12 {
overDefault = append(overDefault, bytes.Repeat([]byte(" "), 1<<20)...)
overDefault = append(overDefault, []byte("<!-- pad -->\n")...)
}
t.Run("zero selects the default cap and rejects a DTD over it", func(t *testing.T) {
t.Parallel()
fsys := fstest.MapFS{extDTDName: &fstest.MapFile{Data: overDefault}}
p := helium.NewParser().BlockXXE(false).
LoadExternalDTD(true).
DefaultDTDAttributes(true).
MaxExternalDTDBytes(0).
FS(fsys)
_, err := p.Parse(t.Context(), []byte(input))
require.Error(t, err, "a DTD over the default cap must be rejected when the cap is the default (0)")
require.ErrorIs(t, err, helium.ErrExternalDTDTooLarge, "rejection must come from the byte-count cap")
})
t.Run("negative removes the cap and loads a DTD over the default", func(t *testing.T) {
t.Parallel()
fsys := fstest.MapFS{extDTDName: &fstest.MapFile{Data: overDefault}}
p := helium.NewParser().BlockXXE(false).
LoadExternalDTD(true).
DefaultDTDAttributes(true).
MaxExternalDTDBytes(-1).
FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "a negative cap must disable the limit, loading a DTD larger than the default")
root := doc.DocumentElement()
require.NotNil(t, root, "root element should exist")
x, ok := root.GetAttribute("x")
require.True(t, ok, "external DTD ATTLIST default must be applied, proving the oversized DTD was loaded")
require.Equal(t, "default", x, "defaulted attribute value must come from the external DTD")
})
})
t.Run("a partial-read error surfaces", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "trunc.dtd">
<r/>`
// The DTD content is well under the cap but Read returns a non-EOF error,
// modelling a truncated transport. The parse must fail, and must never silently
// accept the document as if no external subset existed.
fsys := partialReadFS{prefix: []byte("<!ELEMENT r EMPTY>")}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
_, err := p.Parse(t.Context(), []byte(input))
require.Error(t, err, "a truncated external DTD read must surface as a parse error")
})
}
func TestParseExternalDTDMalformed(t *testing.T) {
t.Parallel()
t.Run("a malformed declaration terminates", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "bogus.dtd">
<r/>`
// "<!BOGUS" is not a valid markup declaration and may neither advance the
// cursor nor return an error. The progress guard must turn that into a
// terminating error instead of an infinite loop.
fsys := fstest.MapFS{"bogus.dtd": &fstest.MapFile{Data: []byte("<!BOGUS")}}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
done := make(chan struct{})
var err error
go func() {
defer close(done)
_, err = p.Parse(t.Context(), []byte(input))
}()
select {
case <-done:
require.Error(t, err, "a malformed external DTD declaration must produce a parse error")
case <-time.After(10 * time.Second):
t.Fatal("parsing a malformed external DTD did not terminate (no cursor-progress guard)")
}
})
t.Run("a malformed declaration reports its location", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "bogus.dtd">
<r/>`
// A malformed declaration in the external subset must report the external
// DTD's location, not the main document's doctype line. The progress-guard
// error must be raised while the external DTD cursor and baseURI are still
// active so the reported File carries the DTD path.
fsys := fstest.MapFS{"bogus.dtd": &fstest.MapFile{Data: []byte("<!BOGUS")}}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
_, err := p.Parse(t.Context(), []byte(input))
require.Error(t, err, "a malformed external DTD declaration must produce a parse error")
var pe helium.ErrParseError
require.ErrorAs(t, err, &pe, "error must be a structured parse error")
require.Equal(t, "bogus.dtd", pe.File, "error must reference the external DTD, not the main document")
})
t.Run("a malformed declaration inside INCLUDE surfaces", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "inc.dtd">
<r/>`
// A malformed declaration ("<!BOGUS") inside a WELL-FORMED, properly
// terminated top-level <![INCLUDE[ ... ]]> section must surface as a parse
// error. Previously the top-level external-subset loop swallowed EVERY error
// from parseConditionalSections, silently accepting the bogus declaration.
// Now conditional-section errors propagate: a missing/malformed keyword and
// an unterminated "]]>" section are both fatal, and an actual declaration
// parse error inside the INCLUDE body propagates.
const dtd = `<![INCLUDE[ <!BOGUS ]]>`
fsys := fstest.MapFS{"inc.dtd": &fstest.MapFile{Data: []byte(dtd)}}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
_, err := p.Parse(t.Context(), []byte(input))
require.Error(t, err, "a malformed declaration inside a top-level INCLUDE section must surface as a parse error")
})
t.Run("an unterminated INCLUDE does not hang", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "inc.dtd">
<r/>`
// An external DTD whose <![INCLUDE[ ... ]]> section reaches EOF before its
// "]]>" terminator must report an error PROMPTLY. The INCLUDE body loop reads
// the section through the shared declaration step; when that step signals stop
// (the section's own cursor is exhausted), getCursor() would auto-pop the
// spent section cursor up to the main document cursor (which is not Done),
// defeating the EOF check. Honoring the stop signal — and inspecting the floor
// cursor directly — turns the former infinite loop into a prompt error.
const dtd = `<![INCLUDE[
<!ELEMENT r EMPTY>`
fsys := fstest.MapFS{"inc.dtd": &fstest.MapFile{Data: []byte(dtd)}}
// Guard against a regression manifesting as a hang: run the parse on a
// goroutine with a deadline so a re-introduced infinite loop fails the test
// instead of hanging the whole suite. The requirement here is PROMPT
// completion (whether or not the parse surfaces a conditional-section error),
// not a hang.
ctx := t.Context()
done := make(chan struct{})
go func() {
defer close(done)
p := helium.NewParser().LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
_, _ = p.Parse(ctx, []byte(input))
}()
select {
case <-done:
// Completed promptly: the unterminated INCLUDE section did not loop.
case <-time.After(5 * time.Second):
t.Fatal("parsing an unterminated INCLUDE section hung (infinite loop regression)")
}
})
}
func TestParseExternalDTDParameterEntity(t *testing.T) {
t.Parallel()
t.Run("expands", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "pe.dtd">
<r/>`
// The external subset declares a parameter entity whose replacement text is
// a markup declaration, then references it. The reference must be expanded
// (not merely validated and skipped) so the <!ATTLIST> takes effect and the
// default attribute is applied to <r/>. A trailing newline after the
// reference exercises the progress guard: it must not misfire on valid
// PE-expanding input.
const dtd = `<!ELEMENT r EMPTY>
<!ENTITY % defaults "<!ATTLIST r x CDATA 'default'>">
%defaults;
`
fsys := fstest.MapFS{"pe.dtd": &fstest.MapFile{Data: []byte(dtd)}}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "valid external-subset parameter-entity reference must parse")
require.NotNil(t, doc, "document must be returned")
root := doc.DocumentElement()
require.NotNil(t, root, "root element must be available")
val, ok := root.GetAttribute("x")
require.True(t, ok, "default attribute from expanded PE must be present")
require.Equal(t, "default", val, "expanded PE must apply the default attribute value")
})
t.Run("a conditional section followed by a declaration", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "cs.dtd">
<r/>`
// The external subset declares a parameter entity whose replacement text is
// a conditional section, references it, and THEN declares another markup
// declaration. After the PE expands to the conditional section and is
// exhausted, the loop must resume in the parent DTD and apply the trailing
// <!ATTLIST>. Before the fix, the conditional-section path "continue"-d past
// the cursor-cleanup/progress guard, leaving the spent PE cursor on the
// stack so the next iteration broke the loop and the trailing declaration
// was silently skipped.
const dtd = `<!ELEMENT r EMPTY>
<!ENTITY % cs "<![INCLUDE[ <!ELEMENT a EMPTY> ]]>">
%cs;
<!ATTLIST r x CDATA 'd'>
`
fsys := fstest.MapFS{"cs.dtd": &fstest.MapFile{Data: []byte(dtd)}}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "PE expanding to a conditional section must parse")
require.NotNil(t, doc, "document must be returned")
root := doc.DocumentElement()
require.NotNil(t, root, "root element must be available")
val, ok := root.GetAttribute("x")
require.True(t, ok, "declaration following the PE conditional section must be applied")
require.Equal(t, "d", val, "trailing <!ATTLIST> must not be silently skipped")
})
t.Run("whitespace followed by a declaration", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "ws.dtd">
<r/>`
// The external subset declares a parameter entity whose replacement text is
// ONLY whitespace, references it, and THEN declares another markup
// declaration. The blank skip consumes the PE's entire replacement text, so
// the PE cursor is exhausted by the skip itself. The loop must pop the spent
// PE cursor and resume in the parent DTD to apply the trailing <!ATTLIST>.
// Before the fix, the blank-skip's Done()-cursor break exited the loop and
// the deferred cleanup popped the parent DTD cursor too, silently skipping
// the trailing declaration.
const dtd = `<!ELEMENT r EMPTY>
<!ENTITY % ws " ">
%ws;
<!ATTLIST r x CDATA 'd'>
`
fsys := fstest.MapFS{"ws.dtd": &fstest.MapFile{Data: []byte(dtd)}}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "PE expanding to only whitespace must parse")
require.NotNil(t, doc, "document must be returned")
root := doc.DocumentElement()
require.NotNil(t, root, "root element must be available")
val, ok := root.GetAttribute("x")
require.True(t, ok, "declaration following the whitespace-only PE must be applied")
require.Equal(t, "d", val, "trailing <!ATTLIST> must not be silently skipped")
})
t.Run("expands inside an INCLUDE section", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "inc.dtd">
<r/>`
// The external subset wraps its declarations in an <![INCLUDE[ ... ]]>
// conditional section. Inside that section it declares a parameter entity
// whose replacement text is an <!ATTLIST> and then references it. The
// reference must be expanded (not merely validated and skipped by the
// blank-skip's handlePEReference) so the default attribute is applied to
// <r/>. Before the fix, the INCLUDE loop's skipBlanks consumed "%attrs;"
// without pushing its replacement text, silently dropping the declaration.
const dtd = `<![INCLUDE[
<!ELEMENT r EMPTY>
<!ENTITY % attrs "<!ATTLIST r x CDATA 'inc'>">
%attrs;
]]>`
fsys := fstest.MapFS{"inc.dtd": &fstest.MapFile{Data: []byte(dtd)}}
p := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "PE reference inside an INCLUDE section must parse")
require.NotNil(t, doc, "document must be returned")
root := doc.DocumentElement()
require.NotNil(t, root, "root element must be available")
val, ok := root.GetAttribute("x")
require.True(t, ok, "default attribute from PE expanded inside INCLUDE must be present")
require.Equal(t, "inc", val, "PE inside INCLUDE must apply the default attribute value")
})
// the property
// the blank-run cap MUST NOT break: the external-subset declaration step uses a
// blank-ONLY skip (skipBlankRun) precisely so a "%pe;" reference that follows
// whitespace is left for parsePEReference to expand. Consuming it
// through skipBlanks/handlePEReference would push no replacement text. With a
// non-trivial (but under-cap) whitespace run before "%pe;", the PE must still
// expand and its declarations apply — here a general entity declared inside the
// PE is registered.
t.Run("expands after whitespace in the external subset", func(t *testing.T) {
ws := strings.Repeat(" ", 2048)
fsys := fstest.MapFS{
dtdSystemID: {Data: []byte(
`<!ENTITY ctrl "control">` + "\n" +
`<!ENTITY % pe SYSTEM "pe.ent">` + ws + `%pe;`)},
peSystemID: {Data: []byte(`<!ENTITY fromPE "loaded-from-external-pe">`)},
}
const input = `<?xml version="1.0"?>` + "\n" +
`<!DOCTYPE r SYSTEM "d.dtd"><r/>`
doc, err := helium.NewParser().
BlockXXE(false).
LoadExternalDTD(true).
MaxNodeContentSize(4096).
FS(fsys).
Parse(t.Context(), []byte(input))
require.NoError(t, err, "under-cap whitespace before %%pe; must not break parsing")
require.NotNil(t, doc)
ent, ok := doc.GetEntity("fromPE")
require.True(t, ok, "the PE following whitespace must still expand and register its declarations")
require.Equal(t, "loaded-from-external-pe", string(ent.Content()))
})
t.Run("trailing whitespace preserves post-DOCTYPE misc", func(t *testing.T) {
const input = `<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "d.dtd"><!--after--><?pi go?><r/>`
// The external DTD ends with trailing whitespace AFTER its last declaration.
// The shared declaration step's blank-only skip consumes that whitespace and
// reaches EOF on the pushed external-DTD (floor) cursor. getCursor() would
// then auto-pop the exhausted floor cursor and return the cursor BELOW it —
// the MAIN DOCUMENT cursor positioned right after the DOCTYPE — which is not
// Done. The step would parse the document's post-DOCTYPE "<!--after-->"
// comment and "<?pi go?>" PI as if they were external-subset markup, dropping
// them from the parsed document. Inspecting the floor cursor directly (rather
// than via getCursor) stops at the floor instead, so the misc nodes survive.
const dtd = "<!ELEMENT r EMPTY>\n"
fsys := fstest.MapFS{dtdSystemID: &fstest.MapFile{Data: []byte(dtd)}}
p := helium.NewParser().LoadExternalDTD(true).DefaultDTDAttributes(true).FS(fsys)
doc, err := p.Parse(t.Context(), []byte(input))
require.NoError(t, err, "external DTD with trailing whitespace must parse")
require.NotNil(t, doc, "document must be returned")
root := doc.DocumentElement()
require.NotNil(t, root, "root element must be available")
var sawComment, sawPI bool
for n := doc.FirstChild(); n != nil; n = n.NextSibling() {
switch n.Type() {
case helium.CommentNode:
require.Equal(t, "after", string(n.Content()), "post-DOCTYPE comment content must be preserved")
sawComment = true
case helium.ProcessingInstructionNode:
sawPI = true
}
}
require.True(t, sawComment, "post-DOCTYPE comment must not be consumed as external-subset markup")
require.True(t, sawPI, "post-DOCTYPE PI must not be consumed as external-subset markup")
})
}
func TestExternalDTDLoad(t *testing.T) {
t.Parallel()
// The external subset is loaded when load, default-attributes, or validation is
// requested; the three intents are independent and the load decision does not
// depend on the order the setters were called. ValidateDTD(true) must cause the
// external subset to load even when LoadExternalDTD(false) is also set, and the
// result must be identical regardless of call order.
t.Run("load order is independent", func(t *testing.T) {
validateThenNoLoad := helium.NewParser().
BlockXXE(false).
FS(dtdLoadFS()).
ValidateDTD(true).
LoadExternalDTD(false)
noLoadThenValidate := helium.NewParser().
BlockXXE(false).
FS(dtdLoadFS()).
LoadExternalDTD(false).
ValidateDTD(true)
doc1, err1 := validateThenNoLoad.Parse(t.Context(), []byte(dtdLoadDoc()))
require.NoError(t, err1, "validation should succeed once the external subset is loaded")
require.NotNil(t, doc1)
doc2, err2 := noLoadThenValidate.Parse(t.Context(), []byte(dtdLoadDoc()))
require.NoError(t, err2)
require.NotNil(t, doc2)
// Both orders resolve the load decision the same way: validation causes the
// external subset to load, LoadExternalDTD(false) notwithstanding.
require.NotNil(t, doc1.ExtSubset(), "validation must load the external subset regardless of LoadExternalDTD(false)")
require.NotNil(t, doc2.ExtSubset(), "call order must not change the load decision")
})
// A requested external-subset load that fails to open must surface a non-fatal
// warning instead of being silently swallowed. The parse stays lenient (no
// fatal error, document still returned), matching libxml2.
t.Run("a failed load warns", func(t *testing.T) {
capture := &warnCaptureSAX{TreeBuilder: helium.NewTreeBuilder()}
// Empty filesystem: the SYSTEM "sub.dtd" open fails.
doc, err := helium.NewParser().
BlockXXE(false).
FS(fstest.MapFS{}).
SAXHandler(capture).
LoadExternalDTD(true).
Parse(t.Context(), []byte(dtdLoadDoc()))
require.NoError(t, err, "a failed external-subset load stays non-fatal")
require.NotNil(t, doc)
require.NotEmpty(t, capture.warnings, "a requested-but-failed external-subset load must emit a warning")
var lvl helium.ErrorLeveler
require.ErrorAs(t, capture.warnings[0], &lvl)
require.Equal(t, helium.ErrorLevelWarning, lvl.ErrorLevel())
require.Contains(t, capture.warnings[0].Error(), dtdLoadDocName,
"the warning must name the external DTD that failed to load")
})
// Turning DefaultDTDAttributes back off must clear the load intent it set. A
// DefaultDTDAttributes(true) followed by DefaultDTDAttributes(false) must leave
// the parser NOT loading the external subset — the load bit does not get stuck
// on. DefaultDTDAttributes(true) on its own does load.
t.Run("toggling default attributes clears the load", func(t *testing.T) {
toggledOff := helium.NewParser().
BlockXXE(false).
FS(dtdLoadFS()).
DefaultDTDAttributes(true).
DefaultDTDAttributes(false)
doc, err := toggledOff.Parse(t.Context(), []byte(dtdLoadDoc()))
require.NoError(t, err)
require.NotNil(t, doc)
require.Nil(t, doc.ExtSubset(), "toggling DefaultDTDAttributes off must not leave the load bit stuck on")
on := helium.NewParser().
BlockXXE(false).
FS(dtdLoadFS()).
DefaultDTDAttributes(true)
doc2, err2 := on.Parse(t.Context(), []byte(dtdLoadDoc()))
require.NoError(t, err2)
require.NotNil(t, doc2)
require.NotNil(t, doc2.ExtSubset(), "DefaultDTDAttributes(true) must load the external subset")
})
// the not-found branch of external DTD
// resolution: a SYSTEM id pointing at a non-existent file must not crash.
t.Run("a missing file", func(t *testing.T) {
xml := `<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "/nonexistent/path/to.dtd">
<root>content</root>`
// The document body is still well-formed; the external DTD simply cannot be
// loaded. Parsing should not panic.
doc, _ := helium.NewParser().LoadExternalDTD(true).Parse(t.Context(), []byte(xml))
if doc != nil {
require.NotNil(t, doc.DocumentElement())
}
})
// a DOCTYPE that declares a PUBLIC
// external identifier (with both public and system IDs).
t.Run("a public identifier", func(t *testing.T) {
const dtd = `<!ELEMENT root (#PCDATA)>
<!ENTITY who "world">`
path := writeDTD(t, dtd)
xml := `<?xml version="1.0"?>
<!DOCTYPE root PUBLIC "-//Example//DTD root//EN" "` + path + `">
<root/>`
doc, err := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).FS(helium.PermissiveFS()).Parse(t.Context(), []byte(xml))
require.NoError(t, err)
_, found := doc.GetEntity("who")
require.True(t, found)
})
// notation and external entity
// declarations resolved from the external subset.
t.Run("notations and entities", func(t *testing.T) {
const dtd = `<!ELEMENT root (#PCDATA)>
<!NOTATION gif SYSTEM "viewer.exe">
<!NOTATION png PUBLIC "-//N//EN" "png.exe">
<!ENTITY img SYSTEM "img.gif" NDATA gif>
<!ENTITY ext SYSTEM "data.xml">
<!ENTITY pub PUBLIC "-//P//EN" "pub.xml">`
path := writeDTD(t, dtd)
xml := `<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "` + path + `">
<root/>`
doc, err := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).FS(helium.PermissiveFS()).Parse(t.Context(), []byte(xml))
require.NoError(t, err)
require.NotNil(t, doc.ExtSubset(), "external subset must be present")
// The external general entities are resolvable from the document.
_, ok := doc.GetEntity("ext")
require.True(t, ok, "external SYSTEM entity declared in ext subset")
_, ok = doc.GetEntity("pub")
require.True(t, ok, "external PUBLIC entity declared in ext subset")
})
// INCLUDE/IGNORE conditional
// sections, which only appear in the external subset.
t.Run("conditional sections", func(t *testing.T) {
const dtd = `<!ELEMENT root (#PCDATA)>
<![INCLUDE[
<!ENTITY included "in">
]]>
<![IGNORE[
<!ENTITY ignored "out">
]]>`
path := writeDTD(t, dtd)
xml := `<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "` + path + `">
<root/>`
doc, err := helium.NewParser().BlockXXE(false).LoadExternalDTD(true).FS(helium.PermissiveFS()).Parse(t.Context(), []byte(xml))
require.NoError(t, err)
_, found := doc.GetEntity("included")
require.True(t, found, "entity inside INCLUDE section must be declared")
_, found = doc.GetEntity("ignored")
require.False(t, found, "entity inside IGNORE section must be skipped")
})
}