@caido/sdk-backend 0.1.6 → 0.40.0

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.
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@caido/sdk-backend",
3
- "version": "0.1.6",
3
+ "version": "0.40.0",
4
4
  "description": "Typing for the Caido Backend SDK",
5
5
  "author": "Caido Labs Inc. <dev@caido.io>",
6
6
  "license": "MIT",
7
7
  "types": "./src/index.d.ts",
8
8
  "files": [
9
9
  "src/*"
10
- ]
10
+ ],
11
+ "dependencies": {
12
+ "@caido/quickjs-types": "0.2.1"
13
+ }
11
14
  }
package/src/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  ///<reference path="types/runtime.d.ts" />
2
2
  ///<reference path="types/common.d.ts" />
3
3
  ///<reference path="types/typing.d.ts" />
4
+ ///<reference types="@caido/quickjs-types" />
@@ -40,6 +40,7 @@ declare module "caido:utils" {
40
40
  getHeaders(): Record<string, Array<string>>;
41
41
  getHeader(name: string): Array<string> | undefined;
42
42
  getBody(): Body | undefined;
43
+ getCreatedAt(): Date;
43
44
  toSpec(): RequestSpec;
44
45
  toSpecRaw(): RequestSpecRaw;
45
46
  };
@@ -103,6 +104,8 @@ declare module "caido:utils" {
103
104
  getHeaders(): Record<string, Array<string>>;
104
105
  getHeader(name: string): Array<string> | undefined;
105
106
  getBody(): Body | undefined;
107
+ getRoundtripTime(): number;
108
+ getCreatedAt(): Date;
106
109
  };
107
110
 
108
111
  /**
@@ -113,10 +116,108 @@ declare module "caido:utils" {
113
116
  response: Response;
114
117
  };
115
118
 
119
+ /**
120
+ * Information on the current page of paginated data.
121
+ */
122
+ export type PageInfo = {
123
+ hasNextPage: boolean;
124
+ hasPreviousPage: boolean;
125
+ startCursor: string;
126
+ endCursor: string;
127
+ };
128
+
129
+ export type RequestOrderField =
130
+ | "ext"
131
+ | "host"
132
+ | "id"
133
+ | "method"
134
+ | "path"
135
+ | "query"
136
+ | "created_at"
137
+ | "source";
138
+ export type ResponseOrderField = "length" | "roundtrip" | "code";
139
+
140
+ export type RequestsConnectionItem = {
141
+ cursor: string;
142
+ request: Request;
143
+ response?: Response;
144
+ };
145
+
146
+ export type RequestsConnection = {
147
+ pageInfo: PageInfo;
148
+ items: Array<RequestsConnectionItem>;
149
+ };
150
+
151
+ /**
152
+ * Query builder to fetch requests.
153
+ */
154
+ export type RequestsQuery = {
155
+ /**
156
+ * Requests after a given cursor.
157
+ * @param cursor Cursor of the request
158
+ */
159
+ after(cursor: string): RequestsQuery;
160
+
161
+ /**
162
+ * Requests before a given cursor.
163
+ * @param cursor Cursor of the request
164
+ */
165
+ before(cursor: string): RequestsQuery;
166
+
167
+ /**
168
+ * First n requests.
169
+ * @param n Number of requests to return
170
+ */
171
+ first(n: number): RequestsQuery;
172
+
173
+ /**
174
+ * Last n requests.
175
+ * @param n Number of requests to return
176
+ */
177
+ last(n: number): RequestsQuery;
178
+
179
+ /**
180
+ * Filter requests.
181
+ * @param filter HTTPQL filter
182
+ */
183
+ filter(filter: string): RequestsQuery;
184
+
185
+ /**
186
+ * Ascending ordering.
187
+ * @param target Target of the ordering: req or resp.
188
+ * @param field Field to order by.
189
+ */
190
+ ascending(target: "req", field: RequestOrderField): RequestsQuery;
191
+ ascending(target: "resp", field: ResponseOrderField): RequestsQuery;
192
+
193
+ /**
194
+ * Descending ordering.
195
+ * @param target Target of the ordering: req or resp.
196
+ * @param field Field to order by.
197
+ */
198
+ descending(target: "req", field: RequestOrderField): RequestsQuery;
199
+ descending(target: "resp", field: ResponseOrderField): RequestsQuery;
200
+
201
+ /**
202
+ * Execute the query.
203
+ *
204
+ * @throws {Error} If a query parameter is invalid or the query cannot be executed.
205
+ */
206
+ execute(): Promise<RequestsConnection>;
207
+ };
208
+
116
209
  /**
117
210
  * The SDK for the Requests service.
118
211
  */
119
212
  export type RequestsSDK = {
213
+ /**
214
+ * Query requests of the current project.
215
+ *
216
+ * @example
217
+ * const page = await sqk.requests.query().first(2).execute();
218
+ * sdk.console.log(`ID: ${page.items[1].request.getId()}`);
219
+ */
220
+ query(): RequestsQuery;
120
221
  /**
121
222
  * Sends a request.
122
223
  *
@@ -126,14 +227,13 @@ declare module "caido:utils" {
126
227
  *
127
228
  * @example
128
229
  * const spec = new RequestSpec("https://example.com");
129
- * sdk.requests.send(request)
130
- * .then((res) => {
131
- * console.log(res.request.getId());
132
- * console.log(res.response.getCode());
133
- * })
134
- * .catch((err) => {
135
- * console.error(err);
136
- * });
230
+ * try {
231
+ * const res = await sdk.requests.send(request)
232
+ * sdk.console.log(res.request.getId());
233
+ * sdk.console.log(res.response.getCode());
234
+ * } catch (err) {
235
+ * sdk.console.error(err);
236
+ * }
137
237
  */
138
238
  send(request: RequestSpec | RequestSpecRaw): Promise<RequestResponse>;
139
239
 
@@ -142,7 +242,7 @@ declare module "caido:utils" {
142
242
  *
143
243
  * @example
144
244
  * if (sdk.requests.inScope(request)) {
145
- * console.log("In scope");
245
+ * sdk.console.log("In scope");
146
246
  * }
147
247
  */
148
248
  inScope(request: Request | RequestSpec): boolean;
@@ -150,13 +250,23 @@ declare module "caido:utils" {
150
250
 
151
251
  /**
152
252
  * A saved immutable Finding.
153
- *
154
- * To modify, use `toSpec` to get a `FindingSpec` object.
155
253
  */
156
254
  export type Finding = {
255
+ /**
256
+ * The unique Caido {@link ID} of the finding.
257
+ */
157
258
  getId(): ID;
259
+ /**
260
+ * The title of the finding.
261
+ */
158
262
  getTitle(): string;
263
+ /**
264
+ * The description of the finding.
265
+ */
159
266
  getDescription(): string | undefined;
267
+ /**
268
+ * The name of the reporter.
269
+ */
160
270
  getReporter(): string;
161
271
  };
162
272
 
@@ -164,9 +274,27 @@ declare module "caido:utils" {
164
274
  * A mutable Finding not yet created.
165
275
  */
166
276
  export type FindingSpec = {
277
+ /**
278
+ * The title of the finding.
279
+ */
167
280
  title: string;
281
+ /**
282
+ * The description of the finding.
283
+ */
168
284
  description?: string | undefined;
285
+ /**
286
+ * The name of the reporter.
287
+ * It will be used to group findings.
288
+ */
169
289
  reporter: string;
290
+ /**
291
+ * Deduplication key for findings.
292
+ * If a finding with the same dedupe key already exists, it will not be created.
293
+ */
294
+ dedupeKey?: string | undefined;
295
+ /**
296
+ * The associated {@link Request}.
297
+ */
170
298
  request: Request;
171
299
  };
172
300
 
@@ -180,10 +308,11 @@ declare module "caido:utils" {
180
308
  * @throws {Error} If the request cannot be saved.
181
309
  *
182
310
  * @example
183
- * sdk.findings.create({
311
+ * await sdk.findings.create({
184
312
  * title: "Title",
185
313
  * description: "Description",
186
314
  * reporter: "Reporter",
315
+ * dedupe: `${request.getHost()}-${request.getPath()}`,
187
316
  * request,
188
317
  * });
189
318
  */
@@ -1,148 +1,3 @@
1
- declare global {
2
- /**
3
- * Console interface for logging.
4
- *
5
- * Currently logs are only available in the backend logs.
6
- * See https://docs.caido.io/report_bug.html#1-backend-logs
7
- */
8
- type Console = {
9
- debug(message: any): void;
10
- log(message: any): void;
11
- warn(message: any): void;
12
- error(message: any): void;
13
- };
14
- var console: Console;
15
-
16
- /**
17
- * The URLSearchParams interface defines utility methods to work with the query string of a URL.
18
- */
19
- class URLSearchParams implements Iterable<[string, string]> {
20
- constructor(
21
- init?:
22
- | URLSearchParams
23
- | string
24
- | { readonly [name: string]: string }
25
- | Iterable<readonly [name: string, value: string]>
26
- | ReadonlyArray<readonly [name: string, value: string]>,
27
- );
28
- /**
29
- * Append a new name-value pair to the query string.
30
- */
31
- append(name: string, value: string): void;
32
- /**
33
- * If `value` is provided, removes all name-value pairs
34
- * where name is `name` and value is `value`.
35
- *
36
- * If `value` is not provided, removes all name-value pairs whose name is `name`.
37
- */
38
- delete(name: string, value?: string): void;
39
- /**
40
- * Returns an ES6 `Iterator` over each of the name-value pairs in the query.
41
- * Each item of the iterator is a JavaScript `Array`. The first item of the `Array` is the `name`, the second item of the `Array` is the `value`.
42
- *
43
- * Alias for `urlSearchParams[@@iterator]()`.
44
- */
45
- entries(): IterableIterator<[string, string]>;
46
- /**
47
- * Iterates over each name-value pair in the query and invokes the given function.
48
- *
49
- * ```js
50
- * const myURL = new URL('https://example.org/?a=b&#x26;c=d');
51
- * myURL.searchParams.forEach((value, name) => {
52
- * console.log(name, value);
53
- * });
54
- * // Prints:
55
- * // a b
56
- * // c d
57
- * ```
58
- * @param fn Invoked for each name-value pair in the query
59
- */
60
- forEach(fn: (value: string, name: string) => void): void;
61
- /**
62
- * Returns the value of the first name-value pair whose name is `name`. If there
63
- * are no such pairs, `null` is returned.
64
- * @return or `null` if there is no name-value pair with the given `name`.
65
- */
66
- get(name: string): string | null;
67
- /**
68
- * Returns the values of all name-value pairs whose name is `name`. If there are
69
- * no such pairs, an empty array is returned.
70
- */
71
- getAll(name: string): string[];
72
- /**
73
- * Checks if the `URLSearchParams` object contains key-value pair(s) based on `name` and an optional `value` argument.
74
- *
75
- * If `value` is provided, returns `true` when name-value pair with
76
- * same `name` and `value` exists.
77
- *
78
- * If `value` is not provided, returns `true` if there is at least one name-value
79
- * pair whose name is `name`.
80
- */
81
- has(name: string, value?: string): boolean;
82
- /**
83
- * Returns an ES6 `Iterator` over the names of each name-value pair.
84
- *
85
- * ```js
86
- * const params = new URLSearchParams('foo=bar&#x26;foo=baz');
87
- * for (const name of params.keys()) {
88
- * console.log(name);
89
- * }
90
- * // Prints:
91
- * // foo
92
- * // foo
93
- * ```
94
- */
95
- keys(): IterableIterator<string>;
96
- /**
97
- * Sets the value in the `URLSearchParams` object associated with `name` to `value`. If there are any pre-existing name-value pairs whose names are `name`,
98
- * set the first such pair's value to `value` and remove all others. If not,
99
- * append the name-value pair to the query string.
100
- *
101
- * ```js
102
- * const params = new URLSearchParams();
103
- * params.append('foo', 'bar');
104
- * params.append('foo', 'baz');
105
- * params.append('abc', 'def');
106
- * console.log(params.toString());
107
- * // Prints foo=bar&#x26;foo=baz&#x26;abc=def
108
- *
109
- * params.set('foo', 'def');
110
- * params.set('xyz', 'opq');
111
- * console.log(params.toString());
112
- * // Prints foo=def&#x26;abc=def&#x26;xyz=opq
113
- * ```
114
- */
115
- set(name: string, value: string): void;
116
- /**
117
- * The total number of parameter entries.
118
- */
119
- readonly size: number;
120
- /**
121
- * Sort all existing name-value pairs in-place by their names. Sorting is done
122
- * with a [stable sorting algorithm](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability), so relative order between name-value pairs
123
- * with the same name is preserved.
124
- *
125
- * This method can be used, in particular, to increase cache hits.
126
- *
127
- * ```js
128
- * const params = new URLSearchParams('query[]=abc&#x26;type=search&#x26;query[]=123');
129
- * params.sort();
130
- * console.log(params.toString());
131
- * // Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search
132
- * ```
133
- */
134
- sort(): void;
135
- /**
136
- * Returns the search parameters serialized as a string, with characters
137
- * percent-encoded where necessary.
138
- */
139
- toString(): string;
140
- /**
141
- * Returns an ES6 `Iterator` over the values of each name-value pair.
142
- */
143
- values(): IterableIterator<string>;
144
- [Symbol.iterator](): IterableIterator<[string, string]>;
145
- }
146
- }
147
-
148
1
  export {};
2
+
3
+ declare global {}
@@ -1,5 +1,11 @@
1
1
  declare module "caido:plugin" {
2
- import { MaybePromise, FindingsSDK, RequestsSDK } from "caido:utils";
2
+ import {
3
+ MaybePromise,
4
+ FindingsSDK,
5
+ RequestsSDK,
6
+ Request,
7
+ Response,
8
+ } from "caido:utils";
3
9
 
4
10
  export type DefineAPI<
5
11
  API extends Record<string, (...args: any[]) => MaybePromise<any>>,
@@ -22,7 +28,7 @@ declare module "caido:plugin" {
22
28
  * Registers a new backend function for the RPC.
23
29
  *
24
30
  * @example
25
- * sdk.api.register("multiply", (a: number, b: number) => {
31
+ * sdk.api.register("multiply", (sdk: SDK, a: number, b: number) => {
26
32
  * return a * b;
27
33
  * });
28
34
  */
@@ -32,6 +38,43 @@ declare module "caido:plugin" {
32
38
  ): void;
33
39
  };
34
40
 
41
+ /**
42
+ * The SDK for the API RPC service.
43
+ */
44
+ export type EventsSDK = {
45
+ /**
46
+ * Registers an callback on new intercepted requests.
47
+ *
48
+ * This callback is called asynchronously and cannot modify requests.
49
+ *
50
+ * @example
51
+ * sdk.events.onInterceptRequest((sdk: SDK, request: Request) => {
52
+ * // Do something with the request
53
+ * });
54
+ */
55
+ onInterceptRequest(
56
+ callback: (sdk: SDK, request: Request) => MaybePromise<void>,
57
+ ): void;
58
+
59
+ /**
60
+ * Registers an callback on new intercepted responses.
61
+ *
62
+ * This callback is called asynchronously and cannot modify responses.
63
+ *
64
+ * @example
65
+ * sdk.events.onInterceptResponse((sdk: SDK, request: Request, response: Response) => {
66
+ * // Do something with the request/response
67
+ * });
68
+ */
69
+ onInterceptResponse(
70
+ callback: (
71
+ sdk: SDK,
72
+ request: Request,
73
+ response: Response,
74
+ ) => MaybePromise<void>,
75
+ ): void;
76
+ };
77
+
35
78
  /**
36
79
  * The SDK object available to all scripts.
37
80
  */
@@ -54,5 +97,9 @@ declare module "caido:plugin" {
54
97
  * The SDK for the API RPC service.
55
98
  */
56
99
  api: APISDK;
100
+ /**
101
+ * The SDK for the Events service.
102
+ */
103
+ events: EventsSDK;
57
104
  };
58
105
  }