Skip to content

Commit e7e9893

Browse files
committed
[IMP] add manifest-external-assets check
This new check verifies that no external sources are referenced on modules' assets. Related to: OCA/odoo-pre-commit-hooks#124.
1 parent e0ca599 commit e7e9893

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ manifest-author-string | The author key in the manifest file must be a string (w
3636
manifest-behind-migrations | Manifest version (%s) is lower than migration scripts (%s) | E8145
3737
manifest-data-duplicated | The file "%s" is duplicated in lines %s from manifest key "%s" | W8125
3838
manifest-deprecated-key | Deprecated key "%s" in manifest file | C8103
39+
manifest-external-assets | Assets should be distributed with module's source code. More info at https://httptoolkit.com/blog/public-cdn-risks/ | W8162
3940
manifest-maintainers-list | The maintainers key in the manifest file must be a list of strings | E8104
4041
manifest-required-author | One of the following authors must be present in manifest: %s | C8101
4142
manifest-required-key | Missing required key "%s" in manifest file | C8102
@@ -224,6 +225,11 @@ Checks valid only for odoo <= 13.0
224225

225226
- https://github.com/OCA/pylint-odoo/blob/v9.3.2/testing/resources/test_repo/broken_module/__openerp__.py#L7 Deprecated key "description" in manifest file
226227

228+
* manifest-external-assets
229+
230+
- https://github.com/OCA/pylint-odoo/blob/v9.3.2/testing/resources/test_repo/twelve_module/__manifest__.py#L15 Assets should be distributed with module's source code. More info at https://httptoolkit.com/blog/public-cdn-risks/
231+
- https://github.com/OCA/pylint-odoo/blob/v9.3.2/testing/resources/test_repo/twelve_module/__manifest__.py#L19 Assets should be distributed with module's source code. More info at https://httptoolkit.com/blog/public-cdn-risks/
232+
227233
* manifest-maintainers-list
228234

229235
- https://github.com/OCA/pylint-odoo/blob/v9.3.2/testing/resources/test_repo/broken_module3/__openerp__.py#L6 The maintainers key in the manifest file must be a list of strings

src/pylint_odoo/checkers/odoo_addons.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
import string
105105
import warnings
106106
from collections import Counter, defaultdict
107+
from urllib.parse import urlparse
107108

108109
from astroid import ClassDef, FunctionDef, NodeNG, nodes
109110
from pylint.checkers import BaseChecker, utils
@@ -269,6 +270,11 @@
269270
"prefer-env-translation",
270271
CHECK_DESCRIPTION,
271272
),
273+
"W8162": (
274+
"Asset %s should be distributed with module's source code. More info at https://httptoolkit.com/blog/public-cdn-risks/",
275+
"manifest-external-assets",
276+
CHECK_DESCRIPTION,
277+
),
272278
}
273279

274280
DFTL_MANIFEST_REQUIRED_KEYS = ["license"]
@@ -1042,6 +1048,7 @@ def visit_call(self, node):
10421048
"resource-not-exist",
10431049
"website-manifest-key-not-valid-uri",
10441050
"manifest-behind-migrations",
1051+
"manifest-external-assets",
10451052
)
10461053
def visit_dict(self, node):
10471054
if not os.path.basename(self.linter.current_file) in misc.MANIFEST_FILES or not isinstance(
@@ -1197,6 +1204,32 @@ def visit_dict(self, node):
11971204
):
11981205
self.add_message("manifest-maintainers-list", node=manifest_keys_nodes.get("maintainers") or node)
11991206

1207+
# Check there are no external assets
1208+
if self.linter.is_message_enabled("manifest-external-assets"):
1209+
assets_node = None
1210+
for item in node.items:
1211+
if item[0].value == "assets":
1212+
assets_node = item[1]
1213+
1214+
# it is important to use the actual astroid.node instead of manifest_dict, otherwise the
1215+
# errors are not attributed to the proper node.
1216+
if assets_node:
1217+
self._check_manifest_external_assets(assets_node)
1218+
1219+
def _check_manifest_external_assets(self, node):
1220+
def is_external_url(url):
1221+
return urlparse(url).scheme
1222+
1223+
for _, item in node.items:
1224+
for element in item.elts:
1225+
if isinstance(element, nodes.Const):
1226+
if is_external_url(element.value):
1227+
self.add_message("manifest-external-assets", node=element, args=(element.value,))
1228+
elif isinstance(element, (nodes.Tuple, nodes.List)):
1229+
for entry in element.elts:
1230+
if isinstance(entry, nodes.Const) and is_external_url(entry.value):
1231+
self.add_message("manifest-external-assets", node=element, args=(entry.value,))
1232+
12001233
def check_deprecated_odoo_method(self, node: NodeNG) -> bool:
12011234
"""Verify the given method is not marked as deprecated under the set Odoo versions.
12021235
:param node: Function definition to be checked

testing/resources/test_repo/twelve_module/__manifest__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,15 @@
99
'data': [
1010
'security/ir.model.access.csv',
1111
],
12+
"assets": {
13+
"web.assets_common": [
14+
"twelve_module/static/nonexistent.js",
15+
"https://shady.cdn.com/somefile.js"
16+
],
17+
"web.assets_frontend": [
18+
"/twelve_module/hypothetically/good/file.css",
19+
("before", "/web/static/src/css/random.css", "https://bad.idea.com/cool.css"),
20+
["prepend", "/web/static/src/js/hello.js", "http://insecure.and.bad.idea.com/kiwi.js"]
21+
]
22+
}
1223
}

tests/test_main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"manifest-required-author": 1,
3737
"manifest-required-key": 1,
3838
"manifest-version-format": 3,
39+
"manifest-external-assets": 3,
3940
"method-compute": 1,
4041
"method-inverse": 1,
4142
"method-required-super": 8,

0 commit comments

Comments
 (0)