Skip to content

Commit

Permalink
fix(dialogkeywords.ts): injected security context inside DialogKeywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan committed Jan 25, 2023
1 parent dea972f commit 81953d7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 44 deletions.
16 changes: 8 additions & 8 deletions packages/azuredeployer.gbapp/services/AzureDeployerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,17 @@ export class AzureDeployerService implements IGBInstallationDeployer {
*/
public async internalDeployBot (
instance,
accessToken,
botId,
name,
accessToken: string,
botId: string,
name: string,
group,
description,
description: string ,
endpoint,
location,
nlpAppId,
nlpKey,
appId,
appPassword,
nlpKey: string,
appId: string,
appPassword: string,
subscriptionId
): Promise<IGBInstance> {
return new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -578,7 +578,7 @@ export class AzureDeployerService implements IGBInstallationDeployer {
reject(error);
}
});
}
}

public async syncBotServerRepository (group: string, name: string) {
await this.webSiteClient.webApps.syncRepository(group, name);
Expand Down
55 changes: 33 additions & 22 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,11 @@ export class DialogKeywords {
* @example TALK TOLIST (array,member)
*
*/
public getToLst(array, member) {
public async getToLst(pid,array, member) {
const {
min, user
} = await DialogKeywords.getProcessInfo(pid);

if (!array) {
return '<Empty>';
}
Expand All @@ -447,7 +451,11 @@ export class DialogKeywords {
* @example hour = HOUR (date)
*
*/
public getHourFromDate(pid, date) {
public async getHourFromDate(pid, date) {
const {
min, user
} = await DialogKeywords.getProcessInfo(pid);

function addZero(i) {
if (i < 10) {
i = '0' + i;
Expand All @@ -456,7 +464,7 @@ export class DialogKeywords {
}

const contentLocale = this.min.core.getParam<string>(
this.min.instance,
min.instance,
'Default Content Language',
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
);
Expand Down Expand Up @@ -577,7 +585,7 @@ export class DialogKeywords {
*/
public async setIdGeneration({ mode }) {
this['idGeneration'] = mode;
this['id'] = await this.sys().getRandomId();
this['id'] = this.sys().getRandomId();
}

/**
Expand Down Expand Up @@ -793,14 +801,14 @@ export class DialogKeywords {
setTimeout(resolve, ms);
});
};
this.min.cbMap[userId] = {};
this.min.cbMap[userId]['promise'] = '!GBHEAR';
min.cbMap[userId] = {};
min.cbMap[userId]['promise'] = '!GBHEAR';

while (this.min.cbMap[userId].promise === '!GBHEAR') {
while (min.cbMap[userId].promise === '!GBHEAR') {
await sleep(500);
}

const text = this.min.cbMap[userId].promise;
const text = min.cbMap[userId].promise;

if (kind === 'file') {
// TODO: https://github.com/GeneralBots/BotServer/issues/227
Expand Down Expand Up @@ -887,7 +895,7 @@ export class DialogKeywords {

result = value;
} else if (kind === 'hour') {
const extractEntity = text => {
const extractEntity = (text: string) => {
return text.match(/^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$/gi);
};

Expand All @@ -900,7 +908,7 @@ export class DialogKeywords {

result = value;
} else if (kind === 'money') {
const extractEntity = text => {
const extractEntity = (text: string) => {
// https://github.com/GeneralBots/BotServer/issues/307
if (user.locale === 'en') {
return text.match(/(?:\d{1,3},)*\d{1,3}(?:\.\d+)?/gi);
Expand All @@ -926,7 +934,7 @@ export class DialogKeywords {
phoneNumber = phone(text, { country: 'BRA' })[0];
phoneNumber = phoneUtil.parse(phoneNumber);
} catch (error) {
await this.talk({pid, text: Messages[locale].validation_enter_valid_mobile});
await this.talk({ pid, text: Messages[locale].validation_enter_valid_mobile });

return await this.getHear({ pid, kind, arg });
}
Expand All @@ -937,7 +945,7 @@ export class DialogKeywords {

result = phoneNumber;
} else if (kind === 'zipcode') {
const extractEntity = text => {
const extractEntity = (text: string) => {
text = text.replace(/\-/gi, '');

if (user.locale === 'en') {
Expand Down Expand Up @@ -1077,45 +1085,48 @@ export class DialogKeywords {
private async internalSendFile({ pid, mobile, filename, caption }) {
// Handles SEND FILE TO mobile,element in Web Automation.

const {
min, user
} = await DialogKeywords.getProcessInfo(pid);
const element = filename._page ? filename._page : filename.screenshot ? filename : null;

if (element) {
const gbaiName = `${this.min.botId}.gbai`;
const gbaiName = `${min.botId}.gbai`;
const localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
await element.screenshot({ path: localName, fullPage: true });

const url = urlJoin(GBServer.globals.publicAddress, this.min.botId, 'cache', Path.basename(localName));
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', Path.basename(localName));

GBLog.info(`BASIC: WebAutomation: Sending the file ${url} to mobile ${mobile}.`);
await this.min.conversationalService.sendFile(this.min, null, mobile, url, caption);
await min.conversationalService.sendFile(min, null, mobile, url, caption);
}

// Handles Markdown.
else if (filename.indexOf('.md') > -1) {
GBLog.info(`BASIC: Sending the contents of ${filename} markdown to mobile ${mobile}.`);
const md = await this.min.kbService.getAnswerTextByMediaName(this.min.instance.instanceId, filename);
const md = await min.kbService.getAnswerTextByMediaName(min.instance.instanceId, filename);
if (!md) {
GBLog.info(`BASIC: Markdown file ${filename} not found on database for ${this.min.instance.botId}.`);
GBLog.info(`BASIC: Markdown file ${filename} not found on database for ${min.instance.botId}.`);
}

await this.min.conversationalService['playMarkdown'](this.min, md, DialogKeywords.getChannel(), mobile);
await min.conversationalService['playMarkdown'](min, md, DialogKeywords.getChannel(), mobile);
} else {
GBLog.info(`BASIC: Sending the file ${filename} to mobile ${mobile}.`);
let url;
let url: string;
if (!filename.startsWith('https://')) {
url = urlJoin(
GBServer.globals.publicAddress,
'kb',
`${this.min.botId}.gbai`,
`${this.min.botId}.gbkb`,
`${min.botId}.gbai`,
`${min.botId}.gbkb`,
'assets',
filename
);
} else {
url = filename;
}

await this.min.conversationalService.sendFile(this.min, null, mobile, url, caption);
await min.conversationalService.sendFile(min, null, mobile, url, caption);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/basic.gblib/services/SystemKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export class SystemKeywords {
});

if (!documents || documents.length === 0) {
throw `File '${file}' specified on GBasic command not found. Check the .gbdata or the .gb'dialog' associated.`;
throw `File '${file}' specified on GBasic command not found. Check the .gbdata or the .gbdialog associated.`;
}

return documents[0];
Expand Down
22 changes: 11 additions & 11 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export class WhatsappDirectLine extends GBService {
private locale: string = 'pt-BR';
provider: any;
INSTANCE_URL = 'https://api.maytapi.com/api';
private customClient;
private browserWSEndpoint;
private customClient: any;
private browserWSEndpoint: any;
private groupId;

constructor(
Expand Down Expand Up @@ -111,20 +111,20 @@ export class WhatsappDirectLine extends GBService {
}
}

public async setup(setUrl) {
public async setup(setUrl: boolean) {
this.directLineClient = new Swagger({
spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')),
usePromise: true
});
const client = await this.directLineClient;
let url;
let body;
let url: string;
let body: any;

client.clientAuthorizations.add(
'AuthorizationBotConnector',
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
);
let options;
let options: any;

switch (this.provider) {
case 'GeneralBots':
Expand Down Expand Up @@ -166,7 +166,7 @@ export class WhatsappDirectLine extends GBService {

client.on(
'message',
(async message => {
(async (message: string) => {
await this.WhatsAppCallback(message, null);
}).bind(this)
);
Expand Down Expand Up @@ -227,7 +227,7 @@ export class WhatsappDirectLine extends GBService {

const chats = await client.getChats();
await CollectionUtil.asyncForEach(chats, async chat => {
const sleep = ms => {
const sleep = (ms: number) => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
Expand Down Expand Up @@ -303,7 +303,7 @@ export class WhatsappDirectLine extends GBService {
}
}

public async resetConversationId(botId, number, group = '') {
public async resetConversationId(botId: string, number: number, group = '') {
WhatsappDirectLine.conversationIds[botId + number + group] = undefined;
}

Expand All @@ -326,14 +326,14 @@ export class WhatsappDirectLine extends GBService {
}
}

public static providerFromRequest(req) {
public static providerFromRequest(req: any) {
return req.body.messages ? 'chatapi' : req.body.message ? 'maytapi' : 'GeneralBots';
}

public async received(req, res) {
const provider = WhatsappDirectLine.providerFromRequest(req);

let message, from, fromName, text;
let message, from, fromName, text: string;
let group = '';
let answerText = null;
let attachments = null;
Expand Down
4 changes: 2 additions & 2 deletions packages/whatsapp.gblib/strings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const Messages = {
'en-US': {
notify_end_transfer: botName => `Now talking to ${botName} again.`
notify_end_transfer: (botName: any) => `Now talking to ${botName} again.`
},
'pt-BR': {
notify_end_transfer: botName => `Falando com o bot ${botName} novamente.`
notify_end_transfer: (botName: any) => `Falando com o bot ${botName} novamente.`
}
};

0 comments on commit 81953d7

Please sign in to comment.