Skip to content

Commit 0a32b2f

Browse files
authored
fix(ftp,httpv1): keep the raw command case, HTTP/1 whitespace and field octets, and parse whole multi-line FTP replies (#1347)
- FTP requests keep the command token as received (`raw_cmmd`); `make` writes it whenever it differs from `cmmd` only in case (#1333). - A multi-line FTP reply arriving whole in one segment parses when it ends at its first `<code> SP` line; the lines after the first are kept verbatim in `lines` and rebuilt (#1303). - HTTP/1 keeps the start line (`receipt.raw_line`) and each field line, obs-folds included (`raw_header`); `make` writes them whenever they read back as the given values, matching field lines by content, so whitespace and non-UTF-8 field values rebuild byte-exactly (#1302, #1333). A raw line carrying a CR or LF its values lack raises `ProtocolError`. Parsed values are unchanged. Closes #1302 Closes #1303 Closes #1333
1 parent 36db422 commit 0a32b2f

7 files changed

Lines changed: 533 additions & 70 deletions

File tree

‎pcapkit/protocols/application/ftp.py‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from pcapkit.utilities.exceptions import ProtocolError, UnsupportedCall
3232

3333
if TYPE_CHECKING:
34-
from typing import Any, NoReturn, Optional
34+
from typing import Any, Iterable, NoReturn, Optional
3535

3636
from typing_extensions import Literal
3737

@@ -110,20 +110,34 @@ def read(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> 'Data_FTP':
110110
schema = self.__header__
111111

112112
data = schema.data
113-
if (match := FTP_REQUEST.match(data)) is not None:
114-
cmmd = match.group('cmmd').decode()
113+
# NOTE: A multi-line reply (:rfc:`959#section-4`) may arrive whole in one
114+
# segment. Its first line is parsed as any reply line, and the lines
115+
# after it are kept as received in ``lines``. A segment holding anything
116+
# else -- no closing line, or more after it -- is refused as before.
117+
line, lines = data, () # type: bytes, tuple[bytes, ...]
118+
if FTP_RESPONSE.match(data) is None and data.endswith(b'\r\n'):
119+
first, _, rest = data.partition(b'\r\n')
120+
if rest and (match := FTP_RESPONSE.match(first + b'\r\n')) is not None \
121+
and match.group('more'):
122+
rest_lines = tuple(rest[:-2].split(b'\r\n'))
123+
if self._test_lines(match.group('code'), rest_lines):
124+
line, lines = first + b'\r\n', rest_lines
125+
126+
if (match := FTP_REQUEST.match(line)) is not None:
127+
raw_cmmd = match.group('cmmd')
115128
args = match.group('args')
116129

117-
cmmd_val = Enum_Command.get(cmmd)
130+
cmmd_val = Enum_Command.get(raw_cmmd.decode())
118131
args_val, charset = self._decode_args(args)
119132

120133
ftp = Data_Request(
121134
type=Type.REQUEST,
122135
cmmd=cmmd_val,
123136
args=args_val,
124137
charset=charset,
138+
raw_cmmd=raw_cmmd,
125139
) # type: Data_FTP
126-
elif (match := FTP_RESPONSE.match(data)) is not None:
140+
elif (match := FTP_RESPONSE.match(line)) is not None:
127141
code = int(match.group('code'))
128142
more = bool(match.group('more'))
129143
args = match.group('text') if more else match.group('args')
@@ -137,6 +151,7 @@ def read(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> 'Data_FTP':
137151
more=more,
138152
args=args_val,
139153
charset=charset,
154+
lines=lines,
140155
)
141156
else:
142157
raise ProtocolError('FTP: invalid packet format')
@@ -148,6 +163,8 @@ def make(self,
148163
args: 'Optional[str | bytes]' = None,
149164
more: 'bool' = False,
150165
charset: 'Optional[str]' = None,
166+
raw_cmmd: 'Optional[bytes]' = None,
167+
lines: 'Iterable[bytes]' = (),
151168
**kwargs: 'Any') -> 'Schema_FTP':
152169
"""Make (construct) packet data.
153170
@@ -156,17 +173,30 @@ def make(self,
156173
the command or code, except after the hyphen of a multi-line reply,
157174
which the text follows immediately (:rfc:`959#section-4`).
158175
176+
Commands are case-insensitive (:rfc:`959#section-5`), so ``raw_cmmd``
177+
is written in place of ``cmmd`` whenever the two differ only in case,
178+
which keeps the case the command was read in.
179+
159180
Args:
160181
cmmd: FTP command.
161182
code: FTP status code.
162183
args: Optional FTP command arguments and/or status messages.
163184
more: More status messages to follow for response packets.
164185
charset: Encoding for :obj:`str` arguments, **UTF-8** if not given.
186+
raw_cmmd: FTP command as received, in its original case.
187+
lines: Lines of a multi-line reply after the first, each written
188+
verbatim and terminated with CRLF; the last, and only the last,
189+
is the closing line, ``<code> SP <text>``.
165190
**kwargs: Arbitrary keyword arguments.
166191
167192
Returns:
168193
Constructed packet data.
169194
195+
Raises:
196+
ProtocolError: If neither or both of ``cmmd`` and ``code`` are
197+
given, or if ``lines`` are given other than as the rest of a
198+
multi-line reply.
199+
170200
"""
171201
if cmmd is not None and code is None:
172202
if isinstance(cmmd, bytes):
@@ -175,6 +205,8 @@ def make(self,
175205
prefix = cmmd.encode()
176206
else:
177207
prefix = cmmd.value
208+
if raw_cmmd is not None and raw_cmmd.upper() == prefix.upper():
209+
prefix = raw_cmmd
178210

179211
mf = b''
180212
elif cmmd is None and code is not None:
@@ -185,6 +217,10 @@ def make(self,
185217
else:
186218
raise ProtocolError('FTP: invalid packet type')
187219

220+
lines = tuple(lines)
221+
if lines and not (mf and self._test_lines(prefix, lines)):
222+
raise ProtocolError('FTP: lines must close a multi-line reply')
223+
188224
if args is None:
189225
suffix = b''
190226
elif isinstance(args, bytes):
@@ -195,7 +231,7 @@ def make(self,
195231
# the separating SP, absent with no arguments and after a hyphen
196232
sep = b'' if args is None or mf else b' '
197233
return Schema_FTP(
198-
data=b'%s%s%s%s\r\n' % (prefix, mf, sep, suffix),
234+
data=b'%s%s%s%s\r\n' % (prefix, mf, sep, suffix) + b''.join(b'%s\r\n' % line for line in lines),
199235
)
200236

201237
##########################################################################
@@ -219,8 +255,32 @@ def _make_data(cls, data: 'Data_FTP') -> 'dict[str, Any]': # type: ignore[overr
219255
'args': getattr(data, 'args', None),
220256
'more': getattr(data, 'more', False),
221257
'charset': getattr(data, 'charset', None),
258+
'raw_cmmd': getattr(data, 'raw_cmmd', None),
259+
'lines': getattr(data, 'lines', ()),
222260
}
223261

262+
@staticmethod
263+
def _test_lines(code: 'bytes', lines: 'tuple[bytes, ...]') -> 'bool':
264+
"""Whether ``lines`` are the rest of a multi-line reply with ``code``.
265+
266+
A multi-line reply ends at the first line that starts with its code
267+
followed by SP (:rfc:`959#section-4`). The lines before that one are
268+
free text, which may begin with digits -- the code with a hyphen
269+
included -- but, as in any reply line, carry no LF.
270+
271+
Args:
272+
code: Reply code of the first line.
273+
lines: Lines after the first, CRLF excluded.
274+
275+
Returns:
276+
Whether the last line, and only the last, is the closing line.
277+
278+
"""
279+
closing = code + b' '
280+
if not lines or not all(isinstance(line, bytes) and b'\n' not in line for line in lines):
281+
return False
282+
return lines[-1].startswith(closing) and not any(line.startswith(closing) for line in lines[:-1])
283+
224284
def _decode_args(self, args: 'Optional[bytes]') -> 'tuple[Optional[str], Optional[str]]':
225285
"""Decode arguments, recording how to encode them back.
226286

0 commit comments

Comments
 (0)