Mercurial > p > roundup > code
annotate roundup/cgi/TAL/markupbase.py @ 8411:ef1ea918b07a reauth-confirm_id
feat(security): Add user confirmation/reauth for sensitive changes
Auditors can raise Reauth(reason) exception to require the user to
enter a token (e.g. account password) to verify the user is performing
the change.
Naming is subject to change.
actions.py: New ReauthAction class handler and verifyPassword() method
for overriding if needed.
client.py: Handle Reauth exception by calling Client:reauth() method.
Default client:reauth method. Add 'reauth' action declaration.
exceptions.py: Define and document Reauth exception as a subclass of
RoundupCGIException.
templating.py: Define method utils.embed_form_fields().
The original form making a change to the database has a lot of form
fields. These need to be resubmitted to Roundup as part of the form
submission that verifies the user's password.
This method turns all non file form fields into type=hidden inputs.
It escapes the names and values to prevent XSS.
For file form fields, it base64 encodes the contents and puts them
in hidden pre blocks. The pre blocks have data attributes for the
filename, filetype and the original field name. (Note the original
field name is not used.)
This stops the file content data (maybe binary e.g. jpegs) from
breaking the html page. The reauth template runs JavaScript that
turns the encoded data inside the pre tags back into a file. Then
it adds a multiple file input control to the page and attaches all
the files to it. This file input is submitted with the rest of the
fields.
_generic.reauth.html (multiple tracker templates): Generates a form
with id=reauth_form to:
display any message from the Reauth exception to the user (e.g. why
user is asked to auth).
get the user's password
submit the form
embed all the form data that triggered the reauth
recreate any file data that was submitted as part of the form and
generate a new file input to push the data to the back end
It has the JavaScript routine (as an IIFE) that regenerates a file
input without user intervention.
All the TAL based tracker templates use the same form. There is also
one for the jinja2 template. The JavaScript for both is the same.
reference.txt: document embed_form_fields utility method.
upgrading.txt: initial upgrading docs.
TODO:
Finalize naming. I am leaning toward ConfirmID rather than Reauth.
Still looking for a standard name for this workflow.
Externalize the javascript in _generic.reauth.html to a seperate file
and use utils.readfile() to embed it or change the script to load it
from a @@file url.
Clean up upgrading.txt with just steps to implement and less feature
detail/internals.
Document internals/troubleshooting in reference.txt.
Add tests using live server.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 11 Aug 2025 14:01:12 -0400 |
| parents | 12fe83f90f0d |
| children |
| rev | line source |
|---|---|
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
1 """Shared support for scanning document type declarations in HTML and XHTML.""" |
| 1049 | 2 |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
3 import re, string |
| 1049 | 4 |
| 5 _declname_match = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match | |
| 6 _declstringlit_match = re.compile(r'(\'[^\']*\'|"[^"]*")\s*').match | |
| 7 | |
| 8 del re | |
| 9 | |
| 10 | |
| 11 class ParserBase: | |
| 12 """Parser base class which provides some common support methods used | |
| 13 by the SGML/HTML and XHTML parsers.""" | |
| 14 | |
| 15 def reset(self): | |
| 16 self.lineno = 1 | |
| 17 self.offset = 0 | |
| 18 | |
| 19 def getpos(self): | |
| 20 """Return current line number and offset.""" | |
| 21 return self.lineno, self.offset | |
| 22 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
23 def error(self, message): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
24 """Return an error, showing current line number and offset. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
25 |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
26 Concrete subclasses *must* override this method. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
27 """ |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
28 raise NotImplementedError |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
29 |
| 1049 | 30 # Internal -- update line number and offset. This should be |
| 31 # called for each piece of data exactly once, in order -- in other | |
| 32 # words the concatenation of all the input strings to this | |
| 33 # function should be exactly the entire input. | |
| 34 def updatepos(self, i, j): | |
| 35 if i >= j: | |
| 36 return j | |
| 37 rawdata = self.rawdata | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
38 nlines = rawdata.count("\n", i, j) |
| 1049 | 39 if nlines: |
| 40 self.lineno = self.lineno + nlines | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
41 pos = rawdata.rindex("\n", i, j) # Should not fail |
| 1049 | 42 self.offset = j-(pos+1) |
| 43 else: | |
| 44 self.offset = self.offset + j-i | |
| 45 return j | |
| 46 | |
| 47 _decl_otherchars = '' | |
| 48 | |
| 49 # Internal -- parse declaration (for use by subclasses). | |
| 50 def parse_declaration(self, i): | |
| 51 # This is some sort of declaration; in "HTML as | |
| 52 # deployed," this should only be the document type | |
| 53 # declaration ("<!DOCTYPE html...>"). | |
| 54 rawdata = self.rawdata | |
| 55 import sys | |
| 56 j = i + 2 | |
| 57 assert rawdata[i:j] == "<!", "unexpected call to parse_declaration" | |
| 58 if rawdata[j:j+1] in ("-", ""): | |
| 59 # Start of comment followed by buffer boundary, | |
| 60 # or just a buffer boundary. | |
| 61 return -1 | |
| 62 # in practice, this should look like: ((name|stringlit) S*)+ '>' | |
| 63 n = len(rawdata) | |
| 64 decltype, j = self._scan_name(j, i) | |
| 65 if j < 0: | |
| 66 return j | |
| 67 if decltype == "doctype": | |
| 68 self._decl_otherchars = '' | |
| 69 while j < n: | |
| 70 c = rawdata[j] | |
| 71 if c == ">": | |
| 72 # end of declaration syntax | |
| 73 data = rawdata[i+2:j] | |
| 74 if decltype == "doctype": | |
| 75 self.handle_decl(data) | |
| 76 else: | |
| 77 self.unknown_decl(data) | |
| 78 return j + 1 | |
| 79 if c in "\"'": | |
| 80 m = _declstringlit_match(rawdata, j) | |
| 81 if not m: | |
| 82 return -1 # incomplete | |
| 83 j = m.end() | |
| 84 elif c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ": | |
| 85 name, j = self._scan_name(j, i) | |
| 86 elif c in self._decl_otherchars: | |
| 87 j = j + 1 | |
| 88 elif c == "[": | |
| 89 if decltype == "doctype": | |
| 90 j = self._parse_doctype_subset(j + 1, i) | |
| 91 else: | |
| 92 self.error("unexpected '[' char in declaration") | |
| 93 else: | |
| 94 self.error( | |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
95 "unexpected %s char in declaration" % repr(rawdata[j])) |
| 1049 | 96 if j < 0: |
| 97 return j | |
| 98 return -1 # incomplete | |
| 99 | |
| 100 # Internal -- scan past the internal subset in a <!DOCTYPE declaration, | |
| 101 # returning the index just past any whitespace following the trailing ']'. | |
| 102 def _parse_doctype_subset(self, i, declstartpos): | |
| 103 rawdata = self.rawdata | |
| 104 n = len(rawdata) | |
| 105 j = i | |
| 106 while j < n: | |
| 107 c = rawdata[j] | |
| 108 if c == "<": | |
| 109 s = rawdata[j:j+2] | |
| 110 if s == "<": | |
| 111 # end of buffer; incomplete | |
| 112 return -1 | |
| 113 if s != "<!": | |
| 114 self.updatepos(declstartpos, j + 1) | |
| 115 self.error("unexpected char in internal subset (in %s)" | |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
116 % repr(s)) |
| 1049 | 117 if (j + 2) == n: |
| 118 # end of buffer; incomplete | |
| 119 return -1 | |
| 120 if (j + 4) > n: | |
| 121 # end of buffer; incomplete | |
| 122 return -1 | |
| 123 if rawdata[j:j+4] == "<!--": | |
| 124 j = self.parse_comment(j, report=0) | |
| 125 if j < 0: | |
| 126 return j | |
| 127 continue | |
| 128 name, j = self._scan_name(j + 2, declstartpos) | |
| 129 if j == -1: | |
| 130 return -1 | |
| 131 if name not in ("attlist", "element", "entity", "notation"): | |
| 132 self.updatepos(declstartpos, j + 2) | |
| 133 self.error( | |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
134 "unknown declaration %s in internal subset" % repr(name)) |
| 1049 | 135 # handle the individual names |
| 136 meth = getattr(self, "_parse_doctype_" + name) | |
| 137 j = meth(j, declstartpos) | |
| 138 if j < 0: | |
| 139 return j | |
| 140 elif c == "%": | |
| 141 # parameter entity reference | |
| 142 if (j + 1) == n: | |
| 143 # end of buffer; incomplete | |
| 144 return -1 | |
| 145 s, j = self._scan_name(j + 1, declstartpos) | |
| 146 if j < 0: | |
| 147 return j | |
| 148 if rawdata[j] == ";": | |
| 149 j = j + 1 | |
| 150 elif c == "]": | |
| 151 j = j + 1 | |
| 152 while j < n and rawdata[j] in string.whitespace: | |
| 153 j = j + 1 | |
| 154 if j < n: | |
| 155 if rawdata[j] == ">": | |
| 156 return j | |
| 157 self.updatepos(declstartpos, j) | |
| 158 self.error("unexpected char after internal subset") | |
| 159 else: | |
| 160 return -1 | |
| 161 elif c in string.whitespace: | |
| 162 j = j + 1 | |
| 163 else: | |
| 164 self.updatepos(declstartpos, j) | |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
165 self.error("unexpected char %s in internal subset" % repr(c)) |
| 1049 | 166 # end of buffer reached |
| 167 return -1 | |
| 168 | |
| 169 # Internal -- scan past <!ELEMENT declarations | |
| 170 def _parse_doctype_element(self, i, declstartpos): | |
| 171 rawdata = self.rawdata | |
| 172 n = len(rawdata) | |
| 173 name, j = self._scan_name(i, declstartpos) | |
| 174 if j == -1: | |
| 175 return -1 | |
| 176 # style content model; just skip until '>' | |
| 177 if '>' in rawdata[j:]: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
178 return rawdata.find(">", j) + 1 |
| 1049 | 179 return -1 |
| 180 | |
| 181 # Internal -- scan past <!ATTLIST declarations | |
| 182 def _parse_doctype_attlist(self, i, declstartpos): | |
| 183 rawdata = self.rawdata | |
| 184 name, j = self._scan_name(i, declstartpos) | |
| 185 c = rawdata[j:j+1] | |
| 186 if c == "": | |
| 187 return -1 | |
| 188 if c == ">": | |
| 189 return j + 1 | |
| 190 while 1: | |
| 191 # scan a series of attribute descriptions; simplified: | |
| 192 # name type [value] [#constraint] | |
| 193 name, j = self._scan_name(j, declstartpos) | |
| 194 if j < 0: | |
| 195 return j | |
| 196 c = rawdata[j:j+1] | |
| 197 if c == "": | |
| 198 return -1 | |
| 199 if c == "(": | |
| 200 # an enumerated type; look for ')' | |
| 201 if ")" in rawdata[j:]: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
202 j = rawdata.find(")", j) + 1 |
| 1049 | 203 else: |
| 204 return -1 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
205 while rawdata[j:j+1].isspace(): |
| 1049 | 206 j = j + 1 |
| 207 if not rawdata[j:]: | |
| 208 # end of buffer, incomplete | |
| 209 return -1 | |
| 210 else: | |
| 211 name, j = self._scan_name(j, declstartpos) | |
| 212 c = rawdata[j:j+1] | |
| 213 if not c: | |
| 214 return -1 | |
| 215 if c in "'\"": | |
| 216 m = _declstringlit_match(rawdata, j) | |
| 217 if m: | |
| 218 j = m.end() | |
| 219 else: | |
| 220 return -1 | |
| 221 c = rawdata[j:j+1] | |
| 222 if not c: | |
| 223 return -1 | |
| 224 if c == "#": | |
| 225 if rawdata[j:] == "#": | |
| 226 # end of buffer | |
| 227 return -1 | |
| 228 name, j = self._scan_name(j + 1, declstartpos) | |
| 229 if j < 0: | |
| 230 return j | |
| 231 c = rawdata[j:j+1] | |
| 232 if not c: | |
| 233 return -1 | |
| 234 if c == '>': | |
| 235 # all done | |
| 236 return j + 1 | |
| 237 | |
| 238 # Internal -- scan past <!NOTATION declarations | |
| 239 def _parse_doctype_notation(self, i, declstartpos): | |
| 240 name, j = self._scan_name(i, declstartpos) | |
| 241 if j < 0: | |
| 242 return j | |
| 243 rawdata = self.rawdata | |
| 244 while 1: | |
| 245 c = rawdata[j:j+1] | |
| 246 if not c: | |
| 247 # end of buffer; incomplete | |
| 248 return -1 | |
| 249 if c == '>': | |
| 250 return j + 1 | |
| 251 if c in "'\"": | |
| 252 m = _declstringlit_match(rawdata, j) | |
| 253 if not m: | |
| 254 return -1 | |
| 255 j = m.end() | |
| 256 else: | |
| 257 name, j = self._scan_name(j, declstartpos) | |
| 258 if j < 0: | |
| 259 return j | |
| 260 | |
| 261 # Internal -- scan past <!ENTITY declarations | |
| 262 def _parse_doctype_entity(self, i, declstartpos): | |
| 263 rawdata = self.rawdata | |
| 264 if rawdata[i:i+1] == "%": | |
| 265 j = i + 1 | |
| 266 while 1: | |
| 267 c = rawdata[j:j+1] | |
| 268 if not c: | |
| 269 return -1 | |
| 270 if c in string.whitespace: | |
| 271 j = j + 1 | |
| 272 else: | |
| 273 break | |
| 274 else: | |
| 275 j = i | |
| 276 name, j = self._scan_name(j, declstartpos) | |
| 277 if j < 0: | |
| 278 return j | |
| 279 while 1: | |
| 280 c = self.rawdata[j:j+1] | |
| 281 if not c: | |
| 282 return -1 | |
| 283 if c in "'\"": | |
| 284 m = _declstringlit_match(rawdata, j) | |
| 285 if m: | |
| 286 j = m.end() | |
| 287 else: | |
| 288 return -1 # incomplete | |
| 289 elif c == ">": | |
| 290 return j + 1 | |
| 291 else: | |
| 292 name, j = self._scan_name(j, declstartpos) | |
| 293 if j < 0: | |
| 294 return j | |
| 295 | |
| 296 # Internal -- scan a name token and the new position and the token, or | |
| 297 # return -1 if we've reached the end of the buffer. | |
| 298 def _scan_name(self, i, declstartpos): | |
| 299 rawdata = self.rawdata | |
| 300 n = len(rawdata) | |
| 301 if i == n: | |
| 302 return None, -1 | |
| 303 m = _declname_match(rawdata, i) | |
| 304 if m: | |
| 305 s = m.group() | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
306 name = s.strip() |
| 1049 | 307 if (i + len(s)) == n: |
| 308 return None, -1 # end of buffer | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
309 return name.lower(), m.end() |
| 1049 | 310 else: |
| 311 self.updatepos(declstartpos, i) | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
312 self.error("expected name token") |
