-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (111 loc) · 3.44 KB
/
index.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
/* global module */
/* eslint no-undef: "error" */
// Plugin method that runs on plugin load
async function setupPlugin({ config }) {
}
async function makePostRequest(url, data, token) {
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Accept": "*/*"
},
body: JSON.stringify(data)
});
if (response.status === 200) {
const responseData = await response.json();
return responseData;
} else {
console.error("Request code " + response.status);
throw new Error("Request failed with status code " + response.status);
}
} catch (error) {
console.error("Error:", error);
throw error;
}
}
function isValidURL(url) {
try {
const urlObject = new URL(url);
// Check if the protocol is either http or https and contains double slash
const isHttpOrHttps = /^https?:\/\/.*/.test(url);
// Check if the hostname is an IP address or localhost
const isIPAddress = /^(\d{1,3}\.){3}\d{1,3}$/.test(urlObject.hostname);
const isLocalhost = urlObject.hostname === 'localhost';
// If the protocol is not http or https, return false
if (!isHttpOrHttps) {
return false;
}
// If the hostname is an IP address or localhost, the port must be present
if ((isIPAddress || isLocalhost) && !urlObject.port) {
return false;
}
// If the hostname is not a valid domain name, IP address, or localhost, return false
if (!urlObject.hostname && !isIPAddress && !isLocalhost) {
return false;
}
// Check if the hostname ends with a double quote
if (urlObject.hostname.endsWith('"')) {
return false;
}
// Check if the path contains a double slash
if (urlObject.pathname.includes('//')) {
return false;
}
return true;
} catch (e) {
// Return false if the URL is not valid
return false;
}
}
async function processEvent(event, { config, cache }) {
const path = 'conversation_prompt_attack_plugin';
let fullUrl = '';
let API_SERVER_URL = config.API_SERVER_URL;
if (isValidURL(API_SERVER_URL)) {
const server_url = API_SERVER_URL.endsWith('/')? API_SERVER_URL : API_SERVER_URL + '/';
fullUrl = server_url + path;
}
else
{
console.error('API_SERVER_URL is not a valid URL');
throw new Error('API_SERVER_URL is not a valid URL');
}
if (!event.properties) {
event.properties = {};
}
if (!event.properties['$llm_input'] || !event.properties['$llm_output']) {
return event;
}
if (Object.keys(event.properties).some(prop => prop.startsWith('user_threats') || prop.startsWith('agent_threats'))) {
return event;
}
let input = event.properties['$llm_input'];
let llm_output = event.properties['$llm_output'];
if (Array.isArray(input)) {
// read the last input and get content
llm_input = input.slice(-1)[0].content
}
else if (typeof input === 'string') {
llm_input = input;
}
var task = {
"llm_input": llm_input,
"llm_output": llm_output
};
const res = await makePostRequest(fullUrl, task);
for (const key in res) {
if (res[key] !== "") {
event.properties[key] = res[key];
}
}
return event;
}
// The plugin itself
module.exports = {
setupPlugin,
processEvent
}