import { wrt } from '../browser/wrt';
+const Module = require('module');
export class DeviceAPIRouter {
currentApplication: any;
packageId: string;
callerAppId: string;
permissions: string[];
+ sharedPaths: string[];
+ validPaths: string[];
constructor(id: string, isGlobal: boolean) {
this.id = id;
this.callerAppId = ids[1] ?? '';
this.packageId = this.serviceId.split('.')[0];
this.permissions = [];
+ this.sharedPaths = [
+ '/opt/usr/apps/shared/res/',
+ '/opt/usr/globalapps/shared/res/'
+ ];
+ this.validPaths = [
+ '/bin/emps/empPepperPlugins/',
+ `/home/owner/apps_rw/${this.packageId}/`,
+ '/home/owner/content/',
+ '/home/owner/share/',
+ '/media/',
+ '/opt/media/',
+ '/opt/share/',
+ `/opt/usr/apps/${this.packageId}/`,
+ '/opt/usr/apps/pepper/',
+ `/opt/usr/globalapps/${this.packageId}/`,
+ `/opt/usr/home/owner/apps_rw/${this.packageId}/`,
+ '/opt/usr/home/owner/content/',
+ '/opt/usr/home/owner/share/',
+ '/tmp/',
+ '/usr/bin/emps/empPepperPlugins/',
+ '/usr/share/wrt/'
+ ];
this.initWebapis();
+ this.refineResolveFilename();
if (isGlobal) {
this.permissions = wrt.getPrivileges(this.id);
this.refineApplicationApis();
}
initWebapis() {
+ let app_info = global.tizen.application.getAppInfo(this.serviceId);
+ if (app_info) {
+ this.packageId = app_info.packageId;
+ }
global.webapis = global.webapis ?? {};
-
global.webapis.getCallerAppId = () => {
return this.callerAppId;
}
+ global.webapis.getPackageId = () => {
+ return this.packageId;
+ }
+ global.webapis.getPermissions = () => {
+ return this.permissions;
+ }
global.webapis.getServiceId = () => {
return this.serviceId;
}
- let app_info = global.tizen.application.getAppInfo(this.serviceId);
- if (app_info) {
- this.packageId = app_info.packageId;
- }
- global.webapis.getPackageId = () => {
- return this.packageId;
+ global.webapis.isValidPath = (path: string) => {
+ let ret = false;
+ for (const validPath of this.validPaths) {
+ if (path.startsWith(validPath))
+ return true;
+ }
+ for (const sharedPath of this.sharedPaths) {
+ if (path.replace(`${path.split('/')[4]}/`, '').includes(sharedPath))
+ return true;
+ }
+ return false;
}
+ Object.defineProperties(global.webapis, {
+ getCallerAppId: { writable: false, enumerable: true },
+ getPackageId: { writable: false, enumerable: true },
+ getPermissions: { writable: false, enumerable: true },
+ getServiceId: { writable: false, enumerable: true },
+ isValidPath: { writable: false, enumerable: true },
+ });
this.initEdgeWebapis();
this.initProductWebapis();
}
+ refineResolveFilename() {
+ const originalResolveFilename = Module._resolveFilename;
+ Module._resolveFilename = function(...args: any[]) {
+ let path = '';
+ if (args[0] === 'fs') {
+ path = originalResolveFilename('fs_tizen', args[1], args[2]);
+ } else {
+ path = originalResolveFilename(...args);
+ }
+ if (path.startsWith('/') && !global.webapis.isValidPath(path))
+ throw new Error(`Invalid access to ${path}`);
+ return path;
+ }
+ Object.defineProperty(Module, '_resolveFilename', { writable: false });
+ }
+
initEdgeWebapis() {
if (wrt['edge'] && !global.webapis.edge) {
let edge = wrt.edge as NativeWRTjs.EdgeExtension;