From 5622f2f43a4fefe4cbac76edd7e0b7e36050f72e Mon Sep 17 00:00:00 2001 From: Giuseppe Guerra Date: Tue, 20 Jun 2023 14:12:17 +0200 Subject: [PATCH] Plugins: Angular: Update hardcoded Angular detection patterns (#70053) * Plugins: Angular: add ConfigCtrl to Angular patterns * Update angular detection patterns * Add tests --- .../angulardetector/angulardetector_test.go | 28 ++++++++++++------- .../manager/loader/angulardetector/service.go | 6 ++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pkg/plugins/manager/loader/angulardetector/angulardetector_test.go b/pkg/plugins/manager/loader/angulardetector/angulardetector_test.go index f77cae7f118..c50dca73e98 100644 --- a/pkg/plugins/manager/loader/angulardetector/angulardetector_test.go +++ b/pkg/plugins/manager/loader/angulardetector/angulardetector_test.go @@ -22,6 +22,8 @@ func TestAngularDetector_Inspect(t *testing.T) { []byte(`define(["app/plugins/sdk"],(function(n){return function(n){var t={};function e(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return n[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=n,e.c=t,e.d=function(n,t,r){e.o(n,t)||Object.defineProperty(n,t,{enumerable:!0,get:r})},e.r=function(n){"undefined"!=typeof`), []byte(`define(["app/plugins/sdk"],(function(n){return function(n){var t={};function e(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return n[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=n,e.c=t,e.d=function(n,t,r){e.o(n,t)||Object.defineProperty(n,t,{enumerable:!0,get:r})},e.r=function(n){"undefined"!=typeof Symbol&&Symbol.toSt`), []byte(`define(["react","lodash","@grafana/data","@grafana/ui","@emotion/css","@grafana/runtime","moment","app/core/utils/datemath","jquery","app/plugins/sdk","app/core/core_module","app/core/core","app/core/table_model","app/core/utils/kbn","app/core/config","angular"],(function(e,t,r,n,i,a,o,s,u,l,c,p,f,h,d,m){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};retur`), + []byte(`exports_1("QueryCtrl", query_ctrl_1.PluginQueryCtrl);`), + []byte(`exports_1('QueryCtrl', query_ctrl_1.PluginQueryCtrl);`), } { tcs = append(tcs, tc{ name: "angular " + strconv.Itoa(i), @@ -34,16 +36,22 @@ func TestAngularDetector_Inspect(t *testing.T) { }) } - // Not angular - tcs = append(tcs, tc{ - name: "not angular", - plugin: &plugins.Plugin{ - FS: plugins.NewInMemoryFS(map[string][]byte{ - "module.js": []byte(`import { PanelPlugin } from '@grafana/data'`), - }), - }, - exp: false, - }) + // Not angular (test against possible false detections) + for i, content := range [][]byte{ + []byte(`import { PanelPlugin } from '@grafana/data'`), + // React ML app + []byte(`==(null===(t=e.components)||void 0===t?void 0:t.QueryCtrl)};function`), + } { + tcs = append(tcs, tc{ + name: "not angular " + strconv.Itoa(i), + plugin: &plugins.Plugin{ + FS: plugins.NewInMemoryFS(map[string][]byte{ + "module.js": content, + }), + }, + exp: false, + }) + } inspector := NewDefaultPatternsListInspector() for _, tc := range tcs { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/plugins/manager/loader/angulardetector/service.go b/pkg/plugins/manager/loader/angulardetector/service.go index 6b0867d82de..c8633a483c8 100644 --- a/pkg/plugins/manager/loader/angulardetector/service.go +++ b/pkg/plugins/manager/loader/angulardetector/service.go @@ -12,16 +12,14 @@ import ( // They are executed in the specified order. var defaultDetectors = []detector{ &containsBytesDetector{pattern: []byte("PanelCtrl")}, - &containsBytesDetector{pattern: []byte("QueryCtrl")}, + &containsBytesDetector{pattern: []byte("ConfigCtrl")}, &containsBytesDetector{pattern: []byte("app/plugins/sdk")}, &containsBytesDetector{pattern: []byte("angular.isNumber(")}, &containsBytesDetector{pattern: []byte("editor.html")}, &containsBytesDetector{pattern: []byte("ctrl.annotation")}, &containsBytesDetector{pattern: []byte("getLegacyAngularInjector")}, - ®exDetector{regex: regexp.MustCompile(`['"](app/core/utils/promiseToDigest)|(app/plugins/.*?)|(app/core/core_module)['"]`)}, - ®exDetector{regex: regexp.MustCompile(`from\s+['"]grafana\/app\/`)}, - ®exDetector{regex: regexp.MustCompile(`System\.register\(`)}, + ®exDetector{regex: regexp.MustCompile(`["']QueryCtrl["']`)}, } // PatternsListInspector matches module.js against all the specified patterns, in sequence.