-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAppsEngineValidator.ts
More file actions
125 lines (101 loc) · 4.92 KB
/
AppsEngineValidator.ts
File metadata and controls
125 lines (101 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as vm from 'vm';
import path from 'path';
import { ICompilerResult } from '../definition';
import { IPermission } from '../definition/IPermission';
import { getAvailablePermissions } from '../misc/getAvailablePermissions';
import { Utilities } from '../misc/Utilities';
export class AppsEngineValidator {
constructor(private readonly appSourceRequire: NodeRequire) { }
public validateAppPermissionsSchema(permissions: Array<IPermission>): void {
if (!permissions) {
return;
}
if (!Array.isArray(permissions)) {
throw new Error('Invalid permission definition. Check your manifest file.');
}
const permissionsRequire = this.appSourceRequire('@rocket.chat/apps-engine/server/permissions/AppPermissions');
if (!permissionsRequire || !permissionsRequire.AppPermissions) {
return;
}
const availablePermissions = getAvailablePermissions(permissionsRequire.AppPermissions);
permissions.forEach((permission) => {
if (permission && !availablePermissions.includes(permission.name)) {
throw new Error(`Invalid permission "${ String(permission.name) }" defined. Check your manifest file`);
}
});
}
public isValidAppInterface(interfaceName: string): boolean {
let { AppInterface } = this.appSourceRequire('@rocket.chat/apps-engine/definition/metadata');
if (!AppInterface) {
AppInterface = this.appSourceRequire('@rocket.chat/apps-engine/server/compiler/AppImplements');
}
return interfaceName in AppInterface;
}
public resolveAppDependencyPath(module: string): string | undefined {
try {
return this.appSourceRequire.resolve(module);
} catch (e) {
console.warn(e);
}
}
public checkInheritance(mainClassFile: string, compilationResult: ICompilerResult): void {
const { App: EngineBaseApp } = this.appSourceRequire('@rocket.chat/apps-engine/definition/App');
const mainClassModule = this.compiledRequire(mainClassFile, compilationResult);
// if the main class is default export use that,
// else take it from the object { [mainClassFile]: Class }
const RealApp = typeof mainClassModule === 'function'
? mainClassModule
: mainClassModule.default
? mainClassModule.default
: mainClassModule[mainClassFile];
if (!RealApp) {
throw new Error(`There must be an exported class "${ mainClassFile }" or a default export in the main class file.`);
}
const mockInfo = { name: '', requiredApiVersion: '', author: { name: '' } };
const mockLogger = { debug: () => { } };
const realApp = new RealApp(mockInfo, mockLogger);
if (!(realApp instanceof EngineBaseApp)) {
throw new Error('App must extend apps-engine\'s "App" abstract class.'
+ ' Maybe you forgot to install dependencies? Try running `npm install`'
+ ' in your app folder to fix it.',
);
}
}
/**
* Require a module from the app compiled source files
*/
private compiledRequire(filename: string, compilationResult: ICompilerResult): any {
const exports = {};
const context = vm.createContext({
require: (filepath: string) => {
// Handles Apps-Engine import
if (filepath.startsWith('@rocket.chat/apps-engine/definition/')) {
return this.appSourceRequire(filepath);
}
// Handles native node modules import
if (Utilities.allowedInternalModuleRequire(filepath)) {
return require(filepath);
}
// At this point, if the app is trying to require anything that
// is not a relative path, we don't want to let it through
if (!filepath.startsWith('.')) {
return undefined;
}
filepath = path.normalize(path.join(path.dirname(filename), filepath));
// Handles import of other files in app's source
if (compilationResult.files[filepath.endsWith('.js') ? filepath : `${ filepath }.js`]) {
return this.compiledRequire(filepath, compilationResult);
}
},
exports,
});
vm.runInContext(compilationResult.files[`${ filename }.js` as keyof ICompilerResult['files']].compiled, context);
/**
* exports will always contain everything
* for result = vm.runInContext(...), result == Object.values(exports)[0] or exports[filename]
* since compiledRequired is not classFile only method (called recursively), better to return all exports
* and extract the main class constructor in the checkInheritance method.
*/
return exports;
}
}