File: MaliciousXmlSuite.cpp

package info (click to toggle)
libspiff 1.0.0-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,240 kB
  • ctags: 1,556
  • sloc: cpp: 10,398; sh: 9,149; makefile: 379; ansic: 83
file content (285 lines) | stat: -rw-r--r-- 8,831 bytes parent folder | download
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
/*
 * libSpiff - XSPF playlist handling library
 *
 * Copyright (C) 2006-2008, Sebastian Pipping / Xiph.Org Foundation
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Sebastian Pipping, sping@xiph.org
 */

#include "MaliciousXmlSuite.h"
#include <spiff/SpiffReader.h>
#include <cstring>
using namespace Spiff;


MaliciousXmlSuite::MaliciousXmlSuite() {
	TEST_ADD(MaliciousXmlSuite::testDisabled)
	TEST_ADD(MaliciousXmlSuite::testLengthPerEntityValue)
	TEST_ADD(MaliciousXmlSuite::testLookupSumPerEntityValue)
	TEST_ADD(MaliciousXmlSuite::testLookupDepthPerEntityValue)
	TEST_ADD(MaliciousXmlSuite::testBillionLaughs1)
	TEST_ADD(MaliciousXmlSuite::testBillionLaughs2)
}


void
MaliciousXmlSuite::testDisabled() {
	char const * const document =
	"<!DOCTYPE d [\n"
	"\t<!ENTITY ld0 \"foo\">\n"
	"\t<!ENTITY ld1 \"&ld0;\">\n"
	"\t<!ENTITY ld2 \"&ld1;\">\n"
	"\t<!ENTITY ld3 \"&ld2;\">\n"
	"\t<!ENTITY ld4 \"&ld3;\">\n"
	"\t<!ENTITY ld5 \"&ld4;\">\n"
	"\t<!ENTITY ld6 \"&ld5;\">\n"
	"]>\n"
	"<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
	"\t<title>&ld6;</title>\n"
	"\t<trackList />\n"
	"</playlist>\n"
	;

	SpiffReader reader;
	int res; // no init needed

	// Disabled by default
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
		NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_SUCCESS);

	// Enabled manually
	reader.limitLookupDepthPerEntityValue(true);
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
		NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_ERROR_MALICIOUS_LOOKUP_DEPTH);

	// Disabled manually
	reader.limitLookupDepthPerEntityValue(false);
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
		NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_SUCCESS);
}


void
MaliciousXmlSuite::testLengthPerEntityValue() {
	char const * const document =
	"<!DOCTYPE d [\n"
	"\t<!ENTITY b10 \"xxxxxxxxxx\">\n"
	"\t<!ENTITY b20 \"&b10;&b10;\">\n"
	"]>\n"
	"<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
	"\t<title>&b20;</title>\n"
	"\t<trackList />\n"
	"</playlist>\n"
	;

	SpiffReader reader;
	reader.limitLengthPerEntityValue(true);
	int res; // no init needed

	// Limit too small
	reader.setMaxLengthPerEntityValue(19);
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
			NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_ERROR_MALICIOUS_SPACE);

	// Limit at minimum
	reader.setMaxLengthPerEntityValue(20);
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
			NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_SUCCESS);
}


void
MaliciousXmlSuite::testLookupSumPerEntityValue() {
	char const * const document =
	"<!DOCTYPE d [\n"
	"\t<!ENTITY ls0 \"foo\">\n"
	"\t<!ENTITY ls4 \"&ls0;&ls0;&ls0;&ls0;\">\n"
	"\t<!ENTITY ls10 \"&ls4;&ls4;\">\n"
	"]>\n"
	"<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
	"\t<title>&ls10;</title>\n"
	"\t<trackList />\n"
	"</playlist>\n"
	;

	SpiffReader reader;
	reader.limitLookupSumPerEntityValue(true);
	int res; // no init needed

	// Limit too small
	reader.setMaxLookupSumPerEntityValue(9);
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
		NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_ERROR_MALICIOUS_LOOKUP_SUM);

	// Limit at minimum
	reader.setMaxLookupSumPerEntityValue(10);
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
		NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_SUCCESS);
}


void
MaliciousXmlSuite::testLookupDepthPerEntityValue() {
	char const * const document =
	"<!DOCTYPE d [\n"
	"\t<!ENTITY ld0 \"foo\">\n"
	"\t<!ENTITY ld1 \"&ld0;\">\n"
	"\t<!ENTITY ld2 \"&ld1;\">\n"
	"\t<!ENTITY ld3 \"&ld2;\">\n"
	"]>\n"
	"<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
	"\t<title>&ld3;</title>\n"
	"\t<trackList />\n"
	"</playlist>\n"
	;

	SpiffReader reader;
	reader.limitLookupDepthPerEntityValue(true);
	int res; // no init needed

	// Limit too small
	reader.setMaxLookupDepthPerEntityValue(2);
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
		NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_ERROR_MALICIOUS_LOOKUP_DEPTH);

	// Limit at minimum
	reader.setMaxLookupDepthPerEntityValue(3);
	res = reader.parseMemory(document, static_cast<int>(::strlen(document)),
		NULL, _PT("http://example.org/"));
	TEST_ASSERT(res == SPIFF_READER_SUCCESS);
}


void
MaliciousXmlSuite::runMaliciousXml(char const * document) {
	SpiffReader reader;
	for (int i = 1; i <= 3; i++) {
		int expectedRes; // no init needed
		switch (i) {
		case 1:
			reader.limitLengthPerEntityValue(true);
			reader.limitLookupSumPerEntityValue(false);
			reader.limitLookupDepthPerEntityValue(false);
			expectedRes = SPIFF_READER_ERROR_MALICIOUS_SPACE;
			break;

		case 2:
			reader.limitLengthPerEntityValue(false);
			reader.limitLookupSumPerEntityValue(true);
			reader.limitLookupDepthPerEntityValue(false);
			expectedRes = SPIFF_READER_ERROR_MALICIOUS_LOOKUP_SUM;
			break;

		case 3:
			reader.limitLengthPerEntityValue(false);
			reader.limitLookupSumPerEntityValue(false);
			reader.limitLookupDepthPerEntityValue(true);
			expectedRes = SPIFF_READER_ERROR_MALICIOUS_LOOKUP_DEPTH;
			break;

		default:
			TEST_ASSERT(false);
		}

		int const res = reader.parseMemory(document,
				static_cast<int>(::strlen(document)),
				NULL, _PT("http://example.org/"));
		TEST_ASSERT(res == expectedRes);
	}
}


void
MaliciousXmlSuite::testBillionLaughs1() {
	// From http://www.cogsci.ed.ac.uk/~richard/billion-laughs.xml
	char const * const document =
	"<?xml version=\"1.0\"?>\n"
	"<!DOCTYPE billion [\n"
	"<!ELEMENT billion (#PCDATA)>\n"
	"<!ENTITY laugh0 \"ha\">\n"
	"<!ENTITY laugh1 \"&laugh0;&laugh0;\">\n"
	"<!ENTITY laugh2 \"&laugh1;&laugh1;\">\n"
	"<!ENTITY laugh3 \"&laugh2;&laugh2;\">\n"
	"<!ENTITY laugh4 \"&laugh3;&laugh3;\">\n"
	"<!ENTITY laugh5 \"&laugh4;&laugh4;\">\n"
	"<!ENTITY laugh6 \"&laugh5;&laugh5;\">\n"
	"<!ENTITY laugh7 \"&laugh6;&laugh6;\">\n"
	"<!ENTITY laugh8 \"&laugh7;&laugh7;\">\n"
	"<!ENTITY laugh9 \"&laugh8;&laugh8;\">\n"
	"<!ENTITY laugh10 \"&laugh9;&laugh9;\">\n"
	"<!ENTITY laugh11 \"&laugh10;&laugh10;\">\n"
	"<!ENTITY laugh12 \"&laugh11;&laugh11;\">\n"
	"<!ENTITY laugh13 \"&laugh12;&laugh12;\">\n"
	"<!ENTITY laugh14 \"&laugh13;&laugh13;\">\n"
	"<!ENTITY laugh15 \"&laugh14;&laugh14;\">\n"
	"<!ENTITY laugh16 \"&laugh15;&laugh15;\">\n"
	"<!ENTITY laugh17 \"&laugh16;&laugh16;\">\n"
	"<!ENTITY laugh18 \"&laugh17;&laugh17;\">\n"
	"<!ENTITY laugh19 \"&laugh18;&laugh18;\">\n"
	"<!ENTITY laugh20 \"&laugh19;&laugh19;\">\n"
	"<!ENTITY laugh21 \"&laugh20;&laugh20;\">\n"
	"<!ENTITY laugh22 \"&laugh21;&laugh21;\">\n"
	"<!ENTITY laugh23 \"&laugh22;&laugh22;\">\n"
	"<!ENTITY laugh24 \"&laugh23;&laugh23;\">\n"
	"<!ENTITY laugh25 \"&laugh24;&laugh24;\">\n"
	"<!ENTITY laugh26 \"&laugh25;&laugh25;\">\n"
	"<!ENTITY laugh27 \"&laugh26;&laugh26;\">\n"
	"<!ENTITY laugh28 \"&laugh27;&laugh27;\">\n"
	"<!ENTITY laugh29 \"&laugh28;&laugh28;\">\n"
	"<!ENTITY laugh30 \"&laugh29;&laugh29;\">\n"
	"]>\n"
	"<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
	"\t<title>&laugh30;</title>\n"
	"\t<trackList />\n"
	"</playlist>\n"
	;

	runMaliciousXml(document);
}


void
MaliciousXmlSuite::testBillionLaughs2() {
	// From http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/
	char const * const document =
	"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
	"<!DOCTYPE member [\n"
	"  <!ENTITY g \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\">\n"
	"  <!ENTITY f \"&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;\">\n"
	"  <!ENTITY e \"&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;\">\n"
	"  <!ENTITY d \"&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;\">\n"
	"  <!ENTITY c \"&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;\">\n"
	"  <!ENTITY b \"&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;\">\n"
	"  <!ENTITY a \"&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;\">\n"
	"]>\n"
	"<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
	"\t<title>&a;</title>\n"
	"\t<trackList />\n"
	"</playlist>\n"
	;

	runMaliciousXml(document);
}