DigitalTrustCenter / sectxt

security.txt parser and validator
European Union Public License 1.2
17 stars 6 forks source link

Improve on missing newline in a file by adding line number #55

Closed bwbroersma closed 1 year ago

bwbroersma commented 1 year ago

Although #21 was fixed in #35 (by these 5 lines in 4dc8b2b), the current error does not list the line number, nor indicate that it's about the last line of the file:

[
  {
    'code': 'no_line_separators',
    'message': 'Every line must end with either a carriage return and line feed characters or just a line feed character',
    'line': None
  }
]

Either:

  1. the line number must be specified; and/or
  2. the text message should be made explicit it's about the last line.

So the problem in the code is here: https://github.com/DigitalTrustCenter/sectxt/blob/79bb38634f2d3c5bed28cce4b47edb98bcd44948/sectxt/__init__.py#L92-L93 It set's the _line_no to None, and then in validate_contents the check is done: https://github.com/DigitalTrustCenter/sectxt/blob/79bb38634f2d3c5bed28cce4b47edb98bcd44948/sectxt/__init__.py#L284-L290 In the _add_error the _line_no is used: https://github.com/DigitalTrustCenter/sectxt/blob/79bb38634f2d3c5bed28cce4b47edb98bcd44948/sectxt/__init__.py#L95-L101

I would propose doing both:

  1. Add explicit_line_no=None to the _add_error definition, plus self._line_no if explicit_line_no = None else explicit_line_no and a self.lines[-1]._line_no to the _add_error call in the 'empty content after \r?\n' check in validate_contents.
  2. Change the text to 'Every line, including the last one, must end with either a carriage return and line feed characters or just a line feed character'
DigitalTrustCenter commented 1 year ago

With the new release the line is added with the missing newline error and the error message has been clarified.