Join the sub-path to base path. This function always ensure the result path is a subpath of base path. :param str base: MUST a absolute path :param str subpath: sub-path under the `base` path :return: normalized result path. None returned if the sub path is not valid
(base, subpath)
| 51 | |
| 52 | |
| 53 | def valid_and_norm_path(base, subpath): |
| 54 | """Join the sub-path to base path. This function always ensure the result path is a subpath of base path. |
| 55 | |
| 56 | :param str base: MUST a absolute path |
| 57 | :param str subpath: sub-path under the `base` path |
| 58 | :return: normalized result path. None returned if the sub path is not valid |
| 59 | """ |
| 60 | subpath = subpath.lstrip('/') |
| 61 | full_path = os.path.normpath(os.path.join(base, subpath)) |
| 62 | if not full_path.startswith(base): |
| 63 | return None |
| 64 | |
| 65 | parts = subpath.split('/') |
| 66 | for i in parts: |
| 67 | if not filename_ok(i): |
| 68 | return None |
| 69 | |
| 70 | return full_path |
| 71 | |
| 72 | |
| 73 | _cached_modules = {} |
no test coverage detected
searching dependent graphs…