|
| 1 | +""" |
| 2 | +Helpers for URI and method injection tests. |
| 3 | +
|
| 4 | +@see: U{CVE-2019-12387} |
| 5 | +""" |
| 6 | + |
| 7 | +import string |
| 8 | + |
| 9 | + |
| 10 | +UNPRINTABLE_ASCII = ( |
| 11 | + frozenset(range(0, 128)) - |
| 12 | + frozenset(bytearray(string.printable, 'ascii')) |
| 13 | +) |
| 14 | + |
| 15 | +NONASCII = frozenset(range(128, 256)) |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +class MethodInjectionTestsMixin(object): |
| 20 | + """ |
| 21 | + A mixin that runs HTTP method injection tests. Define |
| 22 | + L{MethodInjectionTestsMixin.attemptRequestWithMaliciousMethod} in |
| 23 | + a L{twisted.trial.unittest.SynchronousTestCase} subclass to test |
| 24 | + how HTTP client code behaves when presented with malicious HTTP |
| 25 | + methods. |
| 26 | +
|
| 27 | + @see: U{CVE-2019-12387} |
| 28 | + """ |
| 29 | + |
| 30 | + def attemptRequestWithMaliciousMethod(self, method): |
| 31 | + """ |
| 32 | + Attempt to send a request with the given method. This should |
| 33 | + synchronously raise a L{ValueError} if either is invalid. |
| 34 | +
|
| 35 | + @param method: the method (e.g. C{GET\x00}) |
| 36 | +
|
| 37 | + @param uri: the URI |
| 38 | +
|
| 39 | + @type method: |
| 40 | + """ |
| 41 | + raise NotImplementedError() |
| 42 | + |
| 43 | + |
| 44 | + def test_methodWithCLRFRejected(self): |
| 45 | + """ |
| 46 | + Issuing a request with a method that contains a carriage |
| 47 | + return and line feed fails with a L{ValueError}. |
| 48 | + """ |
| 49 | + with self.assertRaises(ValueError) as cm: |
| 50 | + method = b"GET\r\nX-Injected-Header: value" |
| 51 | + self.attemptRequestWithMaliciousMethod(method) |
| 52 | + self.assertRegex(str(cm.exception), "^Invalid method") |
| 53 | + |
| 54 | + |
| 55 | + def test_methodWithUnprintableASCIIRejected(self): |
| 56 | + """ |
| 57 | + Issuing a request with a method that contains unprintable |
| 58 | + ASCII characters fails with a L{ValueError}. |
| 59 | + """ |
| 60 | + for c in UNPRINTABLE_ASCII: |
| 61 | + method = b"GET%s" % (bytearray([c]),) |
| 62 | + with self.assertRaises(ValueError) as cm: |
| 63 | + self.attemptRequestWithMaliciousMethod(method) |
| 64 | + self.assertRegex(str(cm.exception), "^Invalid method") |
| 65 | + |
| 66 | + |
| 67 | + def test_methodWithNonASCIIRejected(self): |
| 68 | + """ |
| 69 | + Issuing a request with a method that contains non-ASCII |
| 70 | + characters fails with a L{ValueError}. |
| 71 | + """ |
| 72 | + for c in NONASCII: |
| 73 | + method = b"GET%s" % (bytearray([c]),) |
| 74 | + with self.assertRaises(ValueError) as cm: |
| 75 | + self.attemptRequestWithMaliciousMethod(method) |
| 76 | + self.assertRegex(str(cm.exception), "^Invalid method") |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +class URIInjectionTestsMixin(object): |
| 81 | + """ |
| 82 | + A mixin that runs HTTP URI injection tests. Define |
| 83 | + L{MethodInjectionTestsMixin.attemptRequestWithMaliciousURI} in a |
| 84 | + L{twisted.trial.unittest.SynchronousTestCase} subclass to test how |
| 85 | + HTTP client code behaves when presented with malicious HTTP |
| 86 | + URIs. |
| 87 | + """ |
| 88 | + |
| 89 | + def attemptRequestWithMaliciousURI(self, method): |
| 90 | + """ |
| 91 | + Attempt to send a request with the given URI. This should |
| 92 | + synchronously raise a L{ValueError} if either is invalid. |
| 93 | +
|
| 94 | + @param uri: the URI. |
| 95 | +
|
| 96 | + @type method: |
| 97 | + """ |
| 98 | + raise NotImplementedError() |
| 99 | + |
| 100 | + |
| 101 | + def test_hostWithCRLFRejected(self): |
| 102 | + """ |
| 103 | + Issuing a request with a URI whose host contains a carriage |
| 104 | + return and line feed fails with a L{ValueError}. |
| 105 | + """ |
| 106 | + with self.assertRaises(ValueError) as cm: |
| 107 | + uri = b"http://twisted\r\n.invalid/path" |
| 108 | + self.attemptRequestWithMaliciousURI(uri) |
| 109 | + self.assertRegex(str(cm.exception), "^Invalid URI") |
| 110 | + |
| 111 | + |
| 112 | + def test_hostWithWithUnprintableASCIIRejected(self): |
| 113 | + """ |
| 114 | + Issuing a request with a URI whose host contains unprintable |
| 115 | + ASCII characters fails with a L{ValueError}. |
| 116 | + """ |
| 117 | + for c in UNPRINTABLE_ASCII: |
| 118 | + uri = b"http://twisted%s.invalid/OK" % (bytearray([c]),) |
| 119 | + with self.assertRaises(ValueError) as cm: |
| 120 | + self.attemptRequestWithMaliciousURI(uri) |
| 121 | + self.assertRegex(str(cm.exception), "^Invalid URI") |
| 122 | + |
| 123 | + |
| 124 | + def test_hostWithNonASCIIRejected(self): |
| 125 | + """ |
| 126 | + Issuing a request with a URI whose host contains non-ASCII |
| 127 | + characters fails with a L{ValueError}. |
| 128 | + """ |
| 129 | + for c in NONASCII: |
| 130 | + uri = b"http://twisted%s.invalid/OK" % (bytearray([c]),) |
| 131 | + with self.assertRaises(ValueError) as cm: |
| 132 | + self.attemptRequestWithMaliciousURI(uri) |
| 133 | + self.assertRegex(str(cm.exception), "^Invalid URI") |
| 134 | + |
| 135 | + |
| 136 | + def test_pathWithCRLFRejected(self): |
| 137 | + """ |
| 138 | + Issuing a request with a URI whose path contains a carriage |
| 139 | + return and line feed fails with a L{ValueError}. |
| 140 | + """ |
| 141 | + with self.assertRaises(ValueError) as cm: |
| 142 | + uri = b"http://twisted.invalid/\r\npath" |
| 143 | + self.attemptRequestWithMaliciousURI(uri) |
| 144 | + self.assertRegex(str(cm.exception), "^Invalid URI") |
| 145 | + |
| 146 | + |
| 147 | + def test_pathWithWithUnprintableASCIIRejected(self): |
| 148 | + """ |
| 149 | + Issuing a request with a URI whose path contains unprintable |
| 150 | + ASCII characters fails with a L{ValueError}. |
| 151 | + """ |
| 152 | + for c in UNPRINTABLE_ASCII: |
| 153 | + uri = b"http://twisted.invalid/OK%s" % (bytearray([c]),) |
| 154 | + with self.assertRaises(ValueError) as cm: |
| 155 | + self.attemptRequestWithMaliciousURI(uri) |
| 156 | + self.assertRegex(str(cm.exception), "^Invalid URI") |
| 157 | + |
| 158 | + |
| 159 | + def test_pathWithNonASCIIRejected(self): |
| 160 | + """ |
| 161 | + Issuing a request with a URI whose path contains non-ASCII |
| 162 | + characters fails with a L{ValueError}. |
| 163 | + """ |
| 164 | + for c in NONASCII: |
| 165 | + uri = b"http://twisted.invalid/OK%s" % (bytearray([c]),) |
| 166 | + with self.assertRaises(ValueError) as cm: |
| 167 | + self.attemptRequestWithMaliciousURI(uri) |
| 168 | + self.assertRegex(str(cm.exception), "^Invalid URI") |
0 commit comments