@metamask/snaps-utils 7.2.0 → 7.3.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/CHANGELOG.md +10 -1
- package/dist/{chunk-I77AVJKV.mjs → chunk-L4DKSTK6.mjs} +5 -5
- package/dist/chunk-L4DKSTK6.mjs.map +1 -0
- package/dist/chunk-YACE3IOJ.mjs +262 -0
- package/dist/chunk-YACE3IOJ.mjs.map +1 -0
- package/dist/chunk-YAKDZ5LP.js +262 -0
- package/dist/chunk-YAKDZ5LP.js.map +1 -0
- package/dist/{chunk-3SOYDY4W.js → chunk-ZUSNAQJU.js} +4 -4
- package/dist/chunk-ZUSNAQJU.js.map +1 -0
- package/dist/handlers.js +2 -2
- package/dist/handlers.mjs +1 -1
- package/dist/index.executionenv.js +2 -2
- package/dist/index.executionenv.mjs +1 -1
- package/dist/index.js +13 -5
- package/dist/index.mjs +17 -9
- package/dist/node.js +13 -5
- package/dist/node.mjs +16 -8
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/handlers.d.ts +89 -89
- package/dist/types/ui.d.ts +51 -11
- package/dist/ui.js +10 -2
- package/dist/ui.mjs +13 -5
- package/package.json +3 -3
- package/dist/chunk-3SOYDY4W.js.map +0 -1
- package/dist/chunk-I77AVJKV.mjs.map +0 -1
- package/dist/chunk-KP4ZAWMP.mjs +0 -80
- package/dist/chunk-KP4ZAWMP.mjs.map +0 -1
- package/dist/chunk-NDIITWO4.js +0 -80
- package/dist/chunk-NDIITWO4.js.map +0 -1
package/dist/chunk-NDIITWO4.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/ui.ts
|
|
2
|
-
var _snapssdk = require('@metamask/snaps-sdk');
|
|
3
|
-
var _utils = require('@metamask/utils');
|
|
4
|
-
var _marked = require('marked');
|
|
5
|
-
var ALLOWED_PROTOCOLS = ["https:", "mailto:"];
|
|
6
|
-
function getMarkdownLinks(text) {
|
|
7
|
-
const tokens = _marked.lexer.call(void 0, text, { gfm: false });
|
|
8
|
-
const links = [];
|
|
9
|
-
_marked.walkTokens.call(void 0, tokens, (token) => {
|
|
10
|
-
if (token.type === "link") {
|
|
11
|
-
links.push(token);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
return links.map((link) => link?.href).filter(Boolean);
|
|
15
|
-
}
|
|
16
|
-
function validateTextLinks(text, isOnPhishingList) {
|
|
17
|
-
const links = getMarkdownLinks(text);
|
|
18
|
-
for (const link of links) {
|
|
19
|
-
try {
|
|
20
|
-
const url = new URL(link);
|
|
21
|
-
_utils.assert.call(void 0,
|
|
22
|
-
ALLOWED_PROTOCOLS.includes(url.protocol),
|
|
23
|
-
`Protocol must be one of: ${ALLOWED_PROTOCOLS.join(", ")}.`
|
|
24
|
-
);
|
|
25
|
-
const hostname = url.protocol === "mailto:" ? url.pathname.split("@")[1] : url.hostname;
|
|
26
|
-
_utils.assert.call(void 0, !isOnPhishingList(hostname), "The specified URL is not allowed.");
|
|
27
|
-
} catch (error) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
`Invalid URL: ${error instanceof _utils.AssertionError ? error.message : "Unable to parse URL."}`
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function validateComponentLinks(component, isOnPhishingList) {
|
|
35
|
-
const { type } = component;
|
|
36
|
-
switch (type) {
|
|
37
|
-
case _snapssdk.NodeType.Panel:
|
|
38
|
-
component.children.forEach(
|
|
39
|
-
(node) => validateComponentLinks(node, isOnPhishingList)
|
|
40
|
-
);
|
|
41
|
-
break;
|
|
42
|
-
case _snapssdk.NodeType.Row:
|
|
43
|
-
validateComponentLinks(component.value, isOnPhishingList);
|
|
44
|
-
break;
|
|
45
|
-
case _snapssdk.NodeType.Text:
|
|
46
|
-
validateTextLinks(component.value, isOnPhishingList);
|
|
47
|
-
break;
|
|
48
|
-
default:
|
|
49
|
-
break;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function getTotalTextLength(component) {
|
|
53
|
-
const { type } = component;
|
|
54
|
-
switch (type) {
|
|
55
|
-
case _snapssdk.NodeType.Panel:
|
|
56
|
-
return component.children.reduce(
|
|
57
|
-
// This is a bug in TypeScript: https://github.com/microsoft/TypeScript/issues/48313
|
|
58
|
-
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
59
|
-
(sum, node) => sum + getTotalTextLength(node),
|
|
60
|
-
0
|
|
61
|
-
);
|
|
62
|
-
case _snapssdk.NodeType.Row:
|
|
63
|
-
return getTotalTextLength(component.value);
|
|
64
|
-
case _snapssdk.NodeType.Text:
|
|
65
|
-
return component.value.length;
|
|
66
|
-
default:
|
|
67
|
-
return 0;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
function hasChildren(component) {
|
|
71
|
-
return _utils.hasProperty.call(void 0, component, "children");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
exports.validateTextLinks = validateTextLinks; exports.validateComponentLinks = validateComponentLinks; exports.getTotalTextLength = getTotalTextLength; exports.hasChildren = hasChildren;
|
|
80
|
-
//# sourceMappingURL=chunk-NDIITWO4.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ui.ts"],"names":[],"mappings":";AACA,SAAS,gBAAgB;AACzB,SAAS,QAAQ,gBAAgB,mBAAmB;AAEpD,SAAS,OAAO,kBAAkB;AAElC,IAAM,oBAAoB,CAAC,UAAU,SAAS;AAQ9C,SAAS,iBAAiB,MAAc;AACtC,QAAM,SAAS,MAAM,MAAM,EAAE,KAAK,MAAM,CAAC;AACzC,QAAM,QAA0C,CAAC;AAGjD,aAAW,QAAQ,CAAC,UAAU;AAC5B,QAAI,MAAM,SAAS,QAAQ;AACzB,YAAM,KAAK,KAAK;AAAA,IAClB;AAAA,EACF,CAAC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,MAAM;AAAA,IACzB;AACE,aAAO;AAAA,EACX;AACF;AAQO,SAAS,YACd,WAC+B;AAC/B,SAAO,YAAY,WAAW,UAAU;AAC1C","sourcesContent":["import type { Component, NodeWithChildren } from '@metamask/snaps-sdk';\nimport { NodeType } from '@metamask/snaps-sdk';\nimport { assert, AssertionError, hasProperty } from '@metamask/utils';\nimport type { Tokens } from 'marked';\nimport { lexer, walkTokens } from 'marked';\n\nconst ALLOWED_PROTOCOLS = ['https:', 'mailto:'];\n\n/**\n * Extract all links from a Markdown text string using the `marked` lexer.\n *\n * @param text - The markdown text string.\n * @returns A list of URLs linked to in the string.\n */\nfunction getMarkdownLinks(text: string) {\n const tokens = lexer(text, { gfm: false });\n const links: (Tokens.Link | Tokens.Generic)[] = [];\n\n // Walk the lexed tokens and collect all link tokens\n walkTokens(tokens, (token) => {\n if (token.type === 'link') {\n links.push(token);\n }\n });\n\n return links.map((link) => link?.href).filter(Boolean);\n}\n\n/**\n * Searches for markdown links in a string and checks them against the phishing list.\n *\n * @param text - The text to verify.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n * @throws If the text contains a link that is not allowed.\n */\nexport function validateTextLinks(\n text: string,\n isOnPhishingList: (url: string) => boolean,\n) {\n const links = getMarkdownLinks(text);\n\n for (const link of links) {\n try {\n const url = new URL(link);\n assert(\n ALLOWED_PROTOCOLS.includes(url.protocol),\n `Protocol must be one of: ${ALLOWED_PROTOCOLS.join(', ')}.`,\n );\n\n const hostname =\n url.protocol === 'mailto:' ? url.pathname.split('@')[1] : url.hostname;\n\n assert(!isOnPhishingList(hostname), 'The specified URL is not allowed.');\n } catch (error) {\n throw new Error(\n `Invalid URL: ${\n error instanceof AssertionError\n ? error.message\n : 'Unable to parse URL.'\n }`,\n );\n }\n }\n}\n\n/**\n * Search for links in UI components and check that the URL they are trying to\n * pass in is not in the phishing list.\n *\n * @param component - The custom UI component.\n * @param isOnPhishingList - The function that checks the link against the\n * phishing list.\n * @throws If the component contains a link that is not allowed.\n */\nexport function validateComponentLinks(\n component: Component,\n isOnPhishingList: (url: string) => boolean,\n) {\n const { type } = component;\n switch (type) {\n case NodeType.Panel:\n component.children.forEach((node) =>\n validateComponentLinks(node, isOnPhishingList),\n );\n break;\n case NodeType.Row:\n validateComponentLinks(component.value, isOnPhishingList);\n break;\n case NodeType.Text:\n validateTextLinks(component.value, isOnPhishingList);\n break;\n default:\n break;\n }\n}\n\n/**\n * Calculate the total length of all text in the component.\n *\n * @param component - A custom UI component.\n * @returns The total length of all text components in the component.\n */\nexport function getTotalTextLength(component: Component): number {\n const { type } = component;\n\n switch (type) {\n case NodeType.Panel:\n return component.children.reduce<number>(\n // This is a bug in TypeScript: https://github.com/microsoft/TypeScript/issues/48313\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n (sum, node) => sum + getTotalTextLength(node),\n 0,\n );\n case NodeType.Row:\n return getTotalTextLength(component.value);\n case NodeType.Text:\n return component.value.length;\n default:\n return 0;\n }\n}\n\n/**\n * Check if a component has children.\n *\n * @param component - A custom UI component.\n * @returns `true` if the component has children, `false` otherwise.\n */\nexport function hasChildren(\n component: Component,\n): component is NodeWithChildren {\n return hasProperty(component, 'children');\n}\n"]}
|