@postalsys/email-text-tools 1.2.7 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs').promises;
4
+ const { riskAnalysis } = require('..');
5
+ const simpleParser = require('mailparser').simpleParser;
6
+ const libmime = require('libmime');
7
+ const Path = require('path');
8
+
9
+ const BASE_PATH = process.argv[2] || '.';
10
+ const MAX_ENTRIES = Math.abs(Number(process.argv[3]) || 0);
11
+
12
+ async function analyzeEmail(path) {
13
+ const eml = await fs.readFile(path);
14
+
15
+ const parsed = await simpleParser(eml);
16
+
17
+ const result = await riskAnalysis(
18
+ {
19
+ headers: parsed.headerLines.map(header => libmime.decodeHeader(header.line)),
20
+ attachments: parsed.attachments,
21
+ html: parsed.html,
22
+ text: parsed.text
23
+ },
24
+ process.env.OPENAI_API_KEY
25
+ );
26
+
27
+ result.file = Path.basename(path);
28
+
29
+ console.log(JSON.stringify(result));
30
+ }
31
+
32
+ function shuffleArray(array) {
33
+ for (let i = array.length - 1; i > 0; i--) {
34
+ let j = Math.floor(Math.random() * (i + 1));
35
+ let temp = array[i];
36
+ array[i] = array[j];
37
+ array[j] = temp;
38
+ }
39
+ return array;
40
+ }
41
+
42
+ async function main() {
43
+ const stat = await fs.stat(BASE_PATH);
44
+ if (stat.isDirectory()) {
45
+ // process as directory
46
+
47
+ let processed = 0;
48
+ let list = await fs.readdir(BASE_PATH);
49
+
50
+ for (let file of shuffleArray(list)) {
51
+ if (Path.extname(file).toLowerCase() === '.eml') {
52
+ if (processed) {
53
+ process.stdout.write(',');
54
+ }
55
+ try {
56
+ await analyzeEmail(Path.join(BASE_PATH, file));
57
+ console.error(`Processed ${file} [${processed + 1}]`);
58
+ } catch (err) {
59
+ console.log(JSON.stringify({ file, error: err.message }));
60
+ console.error(`Failed processing ${file} [${processed + 1}]`);
61
+ console.error(err);
62
+ }
63
+ processed++;
64
+
65
+ if (MAX_ENTRIES && processed >= MAX_ENTRIES) {
66
+ break;
67
+ }
68
+ }
69
+ }
70
+ console.error(`Processed ${processed} files from ${BASE_PATH}`);
71
+ } else {
72
+ // process as file
73
+ await analyzeEmail(BASE_PATH);
74
+ }
75
+ }
76
+
77
+ console.log('[');
78
+ main().then(() => {
79
+ console.log(']');
80
+ });
package/index.js CHANGED
@@ -6,5 +6,14 @@ const inlineHtml = require('./lib/inline-html');
6
6
  const inlineText = require('./lib/inline-text');
7
7
  const mimeHtml = require('./lib/mime-html');
8
8
  const generateSummary = require('./lib/generate-summary');
9
+ const riskAnalysis = require('./lib/risk-analysis');
9
10
 
10
- module.exports = { textToHtml, htmlToText, inlineHtml, inlineText, mimeHtml, generateSummary };
11
+ module.exports = {
12
+ textToHtml,
13
+ htmlToText,
14
+ inlineHtml,
15
+ inlineText,
16
+ mimeHtml,
17
+ generateSummary,
18
+ riskAnalysis
19
+ };
@@ -0,0 +1,220 @@
1
+ 'use strict';
2
+
3
+ const packageData = require('../package.json');
4
+ const htmlToText = require('../lib/html-to-text');
5
+ const nodeFetch = require('node-fetch');
6
+ const GPT3Tokenizer = require('gpt3-tokenizer').default;
7
+ const fetchCmd = global.fetch || nodeFetch;
8
+
9
+ const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });
10
+
11
+ const MAX_ALLOWED_TEXT_LENGTH = 32 * 1024;
12
+ const MAX_ALLOWED_TOKENS = 4000;
13
+ const OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions';
14
+
15
+ const SYSTEM_PROMPT = `
16
+ I want you to act as are an IT security expert that monitors emails for suspicious and risky activity.
17
+ `
18
+ .replace(/\s+/g, ' ')
19
+ .trim();
20
+
21
+ const USER_PROMPT = `
22
+ Instructions:
23
+ - You are an IT security expert.
24
+ - Your task is to monitor and analyze incoming emails which consist of the message headers, a list of attachments, and text content
25
+ - Your analysis should contain (but is not limited to) the following:
26
+ - Does the email include links with domain names that contain typos or homoglyphs that might mislead the user about the actual target of the link
27
+ - Does the sender address of the email match the persona or organization the sender email claims to be
28
+ - Does the email promise the user extremely good financial outcome
29
+ - Does the email promise unclaimed money or goods
30
+ - Does the email offer a vague business opportunity with no specific details
31
+ - Does the email suggest there is a severe penalty if the user does not act as requested
32
+ - Does the email claim that there are technical issues with the user's email account
33
+ - Does the email offer services or activities that are not suitable for underage persons
34
+ - Does the sender email look like a throwaway address
35
+ - Does the sender email look like it might be spoofed
36
+ - Does the email contain attachments where the name of the attachment might be misleading and suggest a different file format than is actually used
37
+ - Does the email contain attachments that might be executable files
38
+ - Provide a risk score for the email using the following scale: 1 - 5 (where 1 is low rist, and 5 is high risk), take into account what may happen if a user acts by the instructions given in the email.
39
+ - Provide a short single-sentence assessment text that includes details about issues that increase the risk score, do not disclose details that decrease the risk score or do not affect it.
40
+ - Respond with a JSON formatted structure with numeric risk score as "risk" property, and the assessment as the "assessment" property. Do not write any other explanations.
41
+
42
+ Facts:
43
+ - An email consists of message headers, attachments list, and text content
44
+ - An email might contain an "authentication-results" result header that indicate if the message passed SPF, DKIM, ARC and DMARC checks
45
+ - Having a valid SPF, DKIM and DMARC increases the chances that the sender domain name is not spoofed
46
+ - For valid SPF the "authentication-results" header must include "spf=pass"
47
+ - For valid DKIM the "authentication-results" header must include "dkim=pass"
48
+ - For valid DMARC the "authentication-results" header must include "dmarc=pass"
49
+ - For valid ARC the "authentication-results" header must include "arc=pass"
50
+ - Throwaway email addresses might contain a word or a name and more than one number in the username of the email address
51
+ - Throwaway email addresses might use a randomly generated or hex text string as the username of the email address
52
+ - Throwaway email addresses might use free email services like gmail.com, outlook.com, hotmail.com, yahoo.com, aol.com etc
53
+ - The email to analyze is formatted in a JSON format
54
+ - The email structure includes a property "headers" that contains an array of header values.
55
+ - Each header contains of two properties, "key" as the header field key name, and "value" as the header value without the key prefix
56
+ - The email structure includes a property "attachments" that contains an array of attachments.
57
+ - Each attachment includes a "filename" property that describes the file name, "contentType" property that describes the Content-Type value of the attachment
58
+ - The email includes a "text" property for the text content
59
+
60
+ Analyze the following email:
61
+ `.trim();
62
+
63
+ async function riskAnalysis(message, apiToken) {
64
+ let text = message.text || '';
65
+ if (message.html && (!text || message.html.length > text.length * 2)) {
66
+ text = htmlToText(message.html);
67
+ }
68
+
69
+ let prompt;
70
+
71
+ const headerSeen = new Set();
72
+ const content = {
73
+ headers: []
74
+ .concat(message.headers || [])
75
+ .filter(
76
+ // use a whitelist
77
+ header =>
78
+ ['from', 'to', 'cc', 'bcc', 'subject', 'mime-version', 'authentication-results', 'date', 'content-type', 'list-id'].includes(header.key)
79
+ )
80
+ .filter(header => {
81
+ // only keept the latest
82
+ if (['authentication-results'].includes(header.key)) {
83
+ if (headerSeen.has(header.key)) {
84
+ return false;
85
+ }
86
+ headerSeen.add(header.key);
87
+ }
88
+ return true;
89
+ }),
90
+ attachments: [].concat(message.attachments || []).map(attachment => ({ filename: attachment.filename, contentType: attachment.contentType }))
91
+ };
92
+
93
+ let charactersRemoved = 0;
94
+ let promptText = text;
95
+
96
+ if (promptText.length > MAX_ALLOWED_TEXT_LENGTH) {
97
+ charactersRemoved += promptText.length - MAX_ALLOWED_TEXT_LENGTH;
98
+ promptText = promptText.substr(0, MAX_ALLOWED_TEXT_LENGTH);
99
+ }
100
+
101
+ while (promptText.length) {
102
+ content.text = promptText;
103
+ prompt = `${USER_PROMPT}
104
+
105
+ ${JSON.stringify(content)}`;
106
+
107
+ let tokens = tokenizer.encode(prompt);
108
+ if (tokens.text.length <= MAX_ALLOWED_TOKENS) {
109
+ break;
110
+ }
111
+ if (promptText.length > 2 * 1024 * 1024) {
112
+ promptText = promptText.substring(0, promptText.length - 1024 * 1024);
113
+ charactersRemoved += 1024 * 1024;
114
+ } else if (promptText.length > 2 * 1024) {
115
+ promptText = promptText.substring(0, promptText.length - 1024);
116
+ charactersRemoved += 1024;
117
+ } else if (promptText.length > 2 * 256) {
118
+ promptText = promptText.substring(0, promptText.length - 256);
119
+ charactersRemoved += 255;
120
+ } else if (promptText.length > 2 * 100) {
121
+ promptText = promptText.substring(0, promptText.length - 100);
122
+ charactersRemoved += 100;
123
+ } else if (promptText.length > 2 * 10) {
124
+ promptText = promptText.substring(0, promptText.length - 10);
125
+ charactersRemoved += 10;
126
+ } else if (promptText.length > 1) {
127
+ promptText = promptText.substring(0, promptText.length - 1);
128
+ charactersRemoved += 1;
129
+ } else {
130
+ throw new Error(`Prompt too long. Removed ${charactersRemoved} characters.`);
131
+ }
132
+ }
133
+
134
+ let headers = {
135
+ 'User-Agent': `${packageData.name}/${packageData.version}`,
136
+ Authorization: `Bearer ${apiToken}`,
137
+ 'Content-Type': 'application/json'
138
+ };
139
+
140
+ let payload = {
141
+ model: 'gpt-3.5-turbo',
142
+ messages: [
143
+ {
144
+ role: 'system',
145
+ content: `${SYSTEM_PROMPT}`
146
+ },
147
+ {
148
+ role: 'user',
149
+ content: prompt
150
+ }
151
+ ]
152
+ };
153
+
154
+ const reqStartTime = Date.now();
155
+
156
+ let res = await fetchCmd(OPENAI_API_URL, {
157
+ method: 'post',
158
+ headers,
159
+ body: JSON.stringify(payload)
160
+ });
161
+
162
+ const reqEndTime = Date.now();
163
+
164
+ let data = await res.json();
165
+
166
+ if (!res.ok) {
167
+ if (data && data.error) {
168
+ let error = new Error(data.error.message || data.error);
169
+ if (data.error.code) {
170
+ error.code = data.error.code;
171
+ }
172
+
173
+ error.statusCode = res.status;
174
+ throw error;
175
+ }
176
+
177
+ let error = new Error('Failed to run API request');
178
+ error.statusCode = res.status;
179
+ throw error;
180
+ }
181
+
182
+ if (!data) {
183
+ throw new Error(`Failed to POST API request`);
184
+ }
185
+
186
+ let values;
187
+ const output =
188
+ data &&
189
+ data.choices &&
190
+ data.choices
191
+ .filter(msg => msg && msg.message && msg.message.role === 'assistant' && msg.message.content)
192
+ .sort((a, b) => ((a && a.index) || 0) - ((b && b.index) || 0))
193
+ .map(msg => msg.message.content)
194
+ .join('\n')
195
+ .trim()
196
+ .replace(/^\s*"|"\s*$/g, '')
197
+ .trim();
198
+
199
+ try {
200
+ values = JSON.parse(output);
201
+ values.risk = Number(values.risk) || -1;
202
+ } catch (err) {
203
+ let error = new Error('Failed to parse output from OpenAI API');
204
+ error.textContent = output;
205
+ throw error;
206
+ }
207
+
208
+ const response = Object.assign({ id: null, tokens: null }, values, {
209
+ id: data && data.id,
210
+ tokens: data && data.usage && data.usage.total_tokens,
211
+ text: content.text,
212
+
213
+ _time: reqEndTime - reqStartTime,
214
+ _cr: charactersRemoved
215
+ });
216
+
217
+ return response;
218
+ }
219
+
220
+ module.exports = riskAnalysis;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postalsys/email-text-tools",
3
- "version": "1.2.7",
3
+ "version": "1.3.1",
4
4
  "description": "Email text tools",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "dompurify": "3.0.1",
27
+ "gpt3-tokenizer": "1.1.5",
27
28
  "handlebars": "4.7.7",
28
29
  "he": "1.2.0",
29
30
  "html-to-text": "9.0.5",