dsh-inline-link-preview 0.0.0-stage → 0.5.0-beta.2
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/LICENSE +21 -0
- package/README.md +77 -2
- package/assets/icon-180.png +0 -0
- package/assets/inline-preview-flow.png +0 -0
- package/assets/supported-sources.png +0 -0
- package/build-client.js +13 -0
- package/client.js +208 -0
- package/composer.js +141 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +6 -0
- package/index.js +214 -0
- package/link-policy.js +61 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/npm-adapter.js +90 -0
- package/package.json +67 -4
- package/public-fetch.js +266 -0
- package/test/host.test.js +168 -0
- package/test/inline.test.js +256 -0
- package/test/npm-adapter.test.js +163 -0
- package/test/public-fetch.test.js +432 -0
- package/test/title.test.js +127 -0
- package/title.js +190 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { readFileSync } from 'node:fs'
|
|
3
|
+
import test from 'node:test'
|
|
4
|
+
import vm from 'node:vm'
|
|
5
|
+
|
|
6
|
+
// Load the actual Client entry, but never render an editor, touch the clipboard or send a message.
|
|
7
|
+
let plugin, source
|
|
8
|
+
const loader = { load(module) { plugin = module.factory(() => ({})) } }
|
|
9
|
+
vm.runInNewContext(readFileSync(new URL('../client.js', import.meta.url), 'utf8'), {
|
|
10
|
+
window: { __ModuleLoader__: loader }, URL, AbortController, AbortSignal,
|
|
11
|
+
})
|
|
12
|
+
plugin.apply({
|
|
13
|
+
get: name => name === 'inputTriggers' ? { registerSource(value) { source = value } } : {},
|
|
14
|
+
effect: fn => fn(), locale: { register() {}, bind: () => key => key }, slots: { inject() {} },
|
|
15
|
+
})
|
|
16
|
+
const token = 'ExampleToken00001'
|
|
17
|
+
const doc = `https://team.feishu.cn/docx/${token}`
|
|
18
|
+
const wiki = `https://team.feishu.cn/wiki/${token}`
|
|
19
|
+
const signal = () => new AbortController().signal
|
|
20
|
+
|
|
21
|
+
// A test double for the published InputActions contract. Offsets are UTF-16 units in the
|
|
22
|
+
// detect projection, in which each earlier chip/quote occupies exactly one unit, not its URL.
|
|
23
|
+
function actionsFor(text, start, end = start) {
|
|
24
|
+
const s = { text, start, end, draftRev: 1, current: true, requests: [], notices: 0, chips: [] }
|
|
25
|
+
const api = {
|
|
26
|
+
current: () => s.current,
|
|
27
|
+
actions: {
|
|
28
|
+
captureInsertion: () => ({ start: s.start, end: s.end, draftRev: s.draftRev }),
|
|
29
|
+
insertText(value, at) {
|
|
30
|
+
if (at.draftRev !== s.draftRev) return false
|
|
31
|
+
s.text = s.text.slice(0, at.start) + value + s.text.slice(at.end)
|
|
32
|
+
s.start = s.end = at.start + value.length
|
|
33
|
+
s.draftRev++
|
|
34
|
+
return true
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
async readTitle(lookup) { s.requests.push(lookup); return '文档标题' },
|
|
38
|
+
unavailable() { s.notices++ },
|
|
39
|
+
session: {
|
|
40
|
+
bail(scope, eventName, { span, reference }) {
|
|
41
|
+
assert.equal(scope, this, 'explicit Cordis origin required to avoid affecting another retained session')
|
|
42
|
+
assert.equal(eventName, 'slash/input-insert-reference')
|
|
43
|
+
if (span.draftRev !== s.draftRev) return false
|
|
44
|
+
assert.equal(s.text.slice(span.start, span.end), reference.clipboardText)
|
|
45
|
+
s.chips.push({ span, reference })
|
|
46
|
+
const next = s.text.slice(span.end), inserted = next[0] === ' ' ? '\ufffc' : '\ufffc '
|
|
47
|
+
s.text = s.text.slice(0, span.start) + inserted + next
|
|
48
|
+
s.start = s.end = span.start + inserted.length
|
|
49
|
+
s.draftRev++
|
|
50
|
+
return true
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
return { s, api }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const kind of ['docx', 'wiki']) {
|
|
58
|
+
for (const host of ['feishu.cn', 'tenant.larksuite.com', 'a.b.larkoffice.com']) {
|
|
59
|
+
test(`one ${kind} link on ${host}: render only the title, keep original path for copy/serialize`, async () => {
|
|
60
|
+
const clean = `https://${host}/${kind}/${token}`
|
|
61
|
+
const url = `${clean}?from=copy&disposable_login_token=not-a-credential#section`
|
|
62
|
+
const { api, s } = actionsFor('', 0)
|
|
63
|
+
const pending = plugin.pasteLink(url, api, signal())
|
|
64
|
+
// Even sending before +inspect finishes must not send a disposable login token.
|
|
65
|
+
assert.equal(s.text, `${clean}?from=copy#section`)
|
|
66
|
+
assert.equal(await pending, true)
|
|
67
|
+
assert.deepEqual(s.requests, [clean])
|
|
68
|
+
assert.equal(s.chips.length, 1)
|
|
69
|
+
const { reference } = s.chips[0]
|
|
70
|
+
assert.equal(reference.source, source.name)
|
|
71
|
+
assert.equal(reference.appearance, 'file')
|
|
72
|
+
assert.equal(reference.label, '文档标题')
|
|
73
|
+
assert.equal(reference.clipboardText, `${clean}?from=copy#section`)
|
|
74
|
+
assert.equal(source.codec.clipboardText(reference.ref), reference.clipboardText)
|
|
75
|
+
assert.equal(await source.codec.serialize(reference.ref, signal()), reference.clipboardText)
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (const [kind, ref, expectedLookup] of [
|
|
81
|
+
['github', 'https://github.com/octocat/Hello-World/issues/1#issuecomment-1', 'https://github.com/octocat/Hello-World/issues/1'],
|
|
82
|
+
['wechat', 'https://mp.weixin.qq.com/s/ArticleSlug#rd', 'https://mp.weixin.qq.com/s/ArticleSlug'],
|
|
83
|
+
['web', 'https://example.org/articles?q=word#section', 'https://example.org/articles?q=word'],
|
|
84
|
+
['web', 'http://www.example.com/', 'http://www.example.com/'],
|
|
85
|
+
['web', `https://feishu.cn.example.com/wiki/${token}`, `https://feishu.cn.example.com/wiki/${token}`],
|
|
86
|
+
]) {
|
|
87
|
+
test(`${kind}: native icon + title in the pasted range, copy/send the original link`, async () => {
|
|
88
|
+
const { api, s } = actionsFor('参考 谢谢', 3)
|
|
89
|
+
assert.equal(plugin.parseLink(ref).kind, kind)
|
|
90
|
+
const result = plugin.pasteLink(ref, api, signal())
|
|
91
|
+
assert.equal(s.text, `参考 ${ref} 谢谢`)
|
|
92
|
+
assert.equal(await result, true)
|
|
93
|
+
assert.deepEqual(s.requests, [expectedLookup])
|
|
94
|
+
assert.equal(s.text, '参考 \ufffc 谢谢')
|
|
95
|
+
const { reference } = s.chips[0]
|
|
96
|
+
assert.equal(reference.appearance, 'file')
|
|
97
|
+
assert.equal(reference.label, '文档标题')
|
|
98
|
+
assert.equal(source.codec.clipboardText(reference.ref), ref)
|
|
99
|
+
assert.equal(await source.codec.serialize(reference.ref, signal()), ref)
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
test('WeChat long article: signed/tracking parameters never go into the metadata request; retain article identity', () => {
|
|
104
|
+
const base = 'https://mp.weixin.qq.com/s?__biz=abc%3D&mid=10&idx=1&sn=test'
|
|
105
|
+
const url = base + '&scene=1&pass_ticket=fixture&exportkey=fixture#wechat_redirect'
|
|
106
|
+
const result = plugin.parseLink(url)
|
|
107
|
+
assert.equal(result.kind, 'wechat')
|
|
108
|
+
assert.equal(result.lookup, base)
|
|
109
|
+
assert.equal(result.ref, base + '&scene=1#wechat_redirect')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('private, sensitive, non-article and non-web addresses use normal paste without lookup', () => {
|
|
113
|
+
for (const url of ['http://127.0.0.1/a', 'https://team.local/a', 'https://localhost/', 'https://host/',
|
|
114
|
+
'http://[::1]/', 'ftp://example.org/file', 'https://example.org:8080/', 'https://u:p@example.org/',
|
|
115
|
+
'https://example.org/page?access_token=test', 'https://example.org/page?X-Amz-Signature=test',
|
|
116
|
+
'https://example.org/page#access_token=test', 'https://example.org/oauth/authorize',
|
|
117
|
+
'https://mp.weixin.qq.com/cgi-bin/login', 'https://mp.weixin.qq.com/s?scene=1']) {
|
|
118
|
+
const { api, s } = actionsFor('原文', 0)
|
|
119
|
+
assert.equal(plugin.pasteLink(url, api, signal()), null, url)
|
|
120
|
+
assert.equal(s.text, '原文'); assert.equal(s.requests.length, 0)
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('public failure/challenge preserves a working copyable URL and uses public-page feedback', async () => {
|
|
125
|
+
const { api, s } = actionsFor('看 ', 2)
|
|
126
|
+
const url = 'https://mp.weixin.qq.com/s/ArticleSlug#rd'
|
|
127
|
+
let notice
|
|
128
|
+
api.readTitle = async () => ''
|
|
129
|
+
api.unavailable = kind => { notice = kind }
|
|
130
|
+
assert.equal(await plugin.pasteLink(url, api, signal()), false)
|
|
131
|
+
assert.equal(notice, 'wechat'); assert.equal(s.chips.length, 0); assert.equal(s.text, `看 ${url}`)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test('replace the selection in a sentence after an earlier chip and an identical literal link', async () => {
|
|
135
|
+
const prefix = '看📄 \ufffc ' + wiki + '\n这里 ', suffix = ' 再写摘要'
|
|
136
|
+
const { api, s } = actionsFor(prefix + '占位' + suffix, prefix.length, prefix.length + 2)
|
|
137
|
+
assert.equal(await plugin.pasteLink(wiki, api, signal()), true)
|
|
138
|
+
assert.equal(s.chips[0].span.start, prefix.length)
|
|
139
|
+
assert.equal(s.text, prefix + '\ufffc' + suffix)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
test('normal text, multiple links and nonmatching URLs are not intercepted', () => {
|
|
143
|
+
const { s, api } = actionsFor('原文', 0)
|
|
144
|
+
for (const text of [`说明 ${doc}`, `${doc}\n${wiki}`, `${doc} ${wiki}`, 'file:///tmp/index.html',
|
|
145
|
+
`http://team.feishu.cn/docx/${token}`, `https://team.feishu.cn/sheets/${token}`,
|
|
146
|
+
`https://localhost/wiki/${token}`, doc.replace('team.feishu.cn', 'u@team.feishu.cn'),
|
|
147
|
+
doc.replace('team.feishu.cn', 'team.feishu.cn:3000'), `${wiki}/extra`]) {
|
|
148
|
+
assert.equal(plugin.pasteLink(text, api, signal()), null, text)
|
|
149
|
+
}
|
|
150
|
+
assert.equal(s.text, '原文')
|
|
151
|
+
assert.equal(s.requests.length, 0)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('lookup drops all parameters, reference drops repeated/case-insensitive credential keys only', () => {
|
|
155
|
+
const info = plugin.parseLink(` ${wiki}/?Access_TOKEN=first&from=search&access_token=second&disposable_login_token=third&disposable_login_token=fourth#block `)
|
|
156
|
+
assert.equal(info.lookup, wiki)
|
|
157
|
+
assert.equal(info.ref, `${wiki}/?from=search#block`)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
test('strip known credential fields in fragments without altering normal paragraph anchors', async () => {
|
|
161
|
+
for (const [hash, keep] of [
|
|
162
|
+
['#access_token=sample', ''], ['#?%61ccess_token=sample&from=copy', '#?from=copy'],
|
|
163
|
+
['#block42?disposable_login_token=one&refresh_token=two', '#block42'],
|
|
164
|
+
['#block42&ID_TOKEN=sample', '#block42'], ['#scroll=%e4%b8%ad&access_token=sample', '#scroll=%e4%b8%ad'],
|
|
165
|
+
['#paragraph%2F42', '#paragraph%2F42'],
|
|
166
|
+
]) {
|
|
167
|
+
const { api, s } = actionsFor('', 0)
|
|
168
|
+
const pending = plugin.pasteLink(wiki + hash, api, signal())
|
|
169
|
+
assert.equal(s.text, wiki + keep)
|
|
170
|
+
assert.equal(await pending, true)
|
|
171
|
+
assert.deepEqual(s.requests, [wiki])
|
|
172
|
+
assert.equal(await source.codec.serialize(s.chips[0].reference.ref, signal()), wiki + keep)
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
test('two retained sessions: only the explicitly addressed session consumes the reference', async () => {
|
|
177
|
+
const a = actionsFor(wiki, wiki.length), b = actionsFor('', 0)
|
|
178
|
+
a.s.draftRev = b.s.draftRev + 1 // same revision and replaceable URL after B pastes
|
|
179
|
+
const handlers = new Map([a, b].map(({ api }) => [api.session, api.session.bail.bind(api.session)]))
|
|
180
|
+
for (const context of handlers.keys()) context.bail = (origin, event, data) => {
|
|
181
|
+
if (typeof origin === 'string') return handlers.get(a.api.session)(a.api.session, origin, event)
|
|
182
|
+
return handlers.get(origin)(origin, event, data)
|
|
183
|
+
}
|
|
184
|
+
assert.equal(await plugin.pasteLink(wiki, b.api, signal()), true)
|
|
185
|
+
assert.equal(a.s.text, wiki)
|
|
186
|
+
assert.equal(a.s.chips.length, 0)
|
|
187
|
+
assert.equal(b.s.chips.length, 1)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('no-op navigation, copy and modifiers do not cancel; submit, edit, IME and blur do', () => {
|
|
191
|
+
const card = new EventTarget(), view = new EventTarget()
|
|
192
|
+
let count = 0
|
|
193
|
+
const off = plugin.watchInterruptions(card, view, () => { count++ })
|
|
194
|
+
const key = (name, metaKey = false) => card.dispatchEvent(Object.assign(new Event('keydown'), { key: name, metaKey }))
|
|
195
|
+
key('Shift'); key('Meta'); key('ArrowRight'); key('c', true); assert.equal(count, 0)
|
|
196
|
+
key('Enter'); card.dispatchEvent(new Event('beforeinput')); card.dispatchEvent(new Event('compositionstart')); view.dispatchEvent(new Event('blur'))
|
|
197
|
+
assert.equal(count, 4)
|
|
198
|
+
off(); key('Escape'); view.dispatchEvent(new Event('blur'))
|
|
199
|
+
assert.equal(count, 4)
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
test('padded clipboard: preserve plain whitespace around a normalized link, including a newline, and the typing position', async () => {
|
|
203
|
+
for (const suffix of ['\n', ' ', '\t', ' \n ']) {
|
|
204
|
+
const { api, s } = actionsFor('参考:下一段', 3)
|
|
205
|
+
const pending = plugin.pasteLink(' ' + wiki + '?disposable_login_token=sample' + suffix, api, signal())
|
|
206
|
+
assert.equal(s.text, '参考: ' + wiki + suffix + '下一段')
|
|
207
|
+
assert.equal(await pending, true)
|
|
208
|
+
const expected = '参考: \ufffc' + suffix + '下一段'
|
|
209
|
+
assert.equal(s.text, expected)
|
|
210
|
+
assert.equal(s.start, expected.indexOf('下一段')); assert.equal(s.end, s.start)
|
|
211
|
+
assert.equal(expected.replace('\ufffc', await source.codec.serialize(s.chips[0].reference.ref, signal())), '参考: ' + wiki + suffix + '下一段')
|
|
212
|
+
}
|
|
213
|
+
const { api, s } = actionsFor('请看:下一段', 3)
|
|
214
|
+
api.readTitle = async () => ''
|
|
215
|
+
assert.equal(await plugin.pasteLink('\t' + doc + '\n', api, signal()), false)
|
|
216
|
+
assert.equal(s.text, '请看:\t' + doc + '\n下一段')
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
for (const interrupted of ['move caret', 'edit draft', 'switch session', 'cancel lookup']) {
|
|
220
|
+
test(`late result after ${interrupted} cannot replace text or raise a notice`, async () => {
|
|
221
|
+
const { s, api } = actionsFor('请看 ', 3)
|
|
222
|
+
const controller = new AbortController(), lookup = Promise.withResolvers()
|
|
223
|
+
api.readTitle = () => lookup.promise
|
|
224
|
+
const pending = plugin.pasteLink(wiki, api, controller.signal)
|
|
225
|
+
if (interrupted === 'move caret') s.start = s.end = 0
|
|
226
|
+
if (interrupted === 'edit draft') { s.draftRev++; s.text += '另写' }
|
|
227
|
+
if (interrupted === 'switch session') s.current = false
|
|
228
|
+
if (interrupted === 'cancel lookup') controller.abort()
|
|
229
|
+
const text = s.text
|
|
230
|
+
lookup.resolve('迟到的标题')
|
|
231
|
+
assert.equal(await pending, false)
|
|
232
|
+
assert.equal(s.text, text)
|
|
233
|
+
assert.equal(s.chips.length + s.notices, 0)
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
for (const failure of ['no access/title', 'network failure']) {
|
|
238
|
+
test(`${failure}: retain a usable clean URL and notify only the originating input`, async () => {
|
|
239
|
+
const { s, api } = actionsFor('请看 谢谢', 3)
|
|
240
|
+
api.readTitle = async () => { if (failure === 'network failure') throw Error('offline'); return '' }
|
|
241
|
+
assert.equal(await plugin.pasteLink(doc + '?disposable_login_token=sample', api, signal()), false)
|
|
242
|
+
assert.equal(s.text, '请看 ' + doc + ' 谢谢')
|
|
243
|
+
assert.equal(s.chips.length, 0)
|
|
244
|
+
assert.equal(s.notices, 1)
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
test('do not consume paste if its session is unavailable or its initial insertion refused', () => {
|
|
249
|
+
const { api, s } = actionsFor('', 0)
|
|
250
|
+
s.current = false
|
|
251
|
+
assert.equal(plugin.pasteLink(doc, api, signal()), null)
|
|
252
|
+
s.current = true
|
|
253
|
+
api.actions.insertText = () => false
|
|
254
|
+
assert.equal(plugin.pasteLink(doc, api, signal()), null)
|
|
255
|
+
assert.equal(s.requests.length, 0)
|
|
256
|
+
})
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import test from 'node:test'
|
|
3
|
+
import { extractNpmTitle, parseNpmPage } from '../npm-adapter.js'
|
|
4
|
+
|
|
5
|
+
const page = path => `https://www.npmjs.com/package/${path}`
|
|
6
|
+
const result = (packageName, version = null) => ({
|
|
7
|
+
packageName,
|
|
8
|
+
version,
|
|
9
|
+
registryUrl: `https://registry.npmjs.org/${encodeURIComponent(packageName)}/${encodeURIComponent(version ?? 'latest')}`,
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
const validPages = [
|
|
13
|
+
['reported prerelease page', page('dsh-inline-link-preview/v/0.5.0-beta.1'), result('dsh-inline-link-preview', '0.5.0-beta.1')],
|
|
14
|
+
['unversioned page uses latest', page('lodash'), result('lodash')],
|
|
15
|
+
['bare official host', 'https://npmjs.com/package/react/v/18.3.1', result('react', '18.3.1')],
|
|
16
|
+
['case-insensitive host and default port', 'HTTPS://WWW.NPMJS.COM:443/package/Foo_Bar/v/1.2.3/', result('Foo_Bar', '1.2.3')],
|
|
17
|
+
['literal scoped path', page('@types/node/v/20.12.7'), result('@types/node', '20.12.7')],
|
|
18
|
+
['encoded @ in scoped path', page('%40types/node'), result('@types/node')],
|
|
19
|
+
['encoded slash in scoped path', page('@types%2fnode'), result('@types/node')],
|
|
20
|
+
['fully encoded scoped path', page('%40types%2Fnode/v/20.12.7'), result('@types/node', '20.12.7')],
|
|
21
|
+
['encoded ordinary characters and build metadata', page('some%2Epackage/v/1.2.3-beta.1%2Bbuild.7'), result('some.package', '1.2.3-beta.1+build.7')],
|
|
22
|
+
['raw build metadata, ignored query and fragment', page('react/v/1.2.3+build.1?next=https://evil.invalid/path#readme'), result('react', '1.2.3+build.1')],
|
|
23
|
+
['query and fragment do not reach registry', page('react?access_token=secret#foo?signature=secret'), result('react')],
|
|
24
|
+
['supported legacy name characters', page('Some_pkg.name~legacy'), result('Some_pkg.name~legacy')],
|
|
25
|
+
['hyphen-leading package names', page('-package-name'), result('-package-name')],
|
|
26
|
+
['tilde-leading legacy package names', page('~legacy'), result('~legacy')],
|
|
27
|
+
['SemVer maximum safe core integer', page('x/v/9007199254740991.0.0-0'), result('x', '9007199254740991.0.0-0')],
|
|
28
|
+
]
|
|
29
|
+
for (const [name, input, expected] of validPages) test(name, () => assert.deepEqual(parseNpmPage(input), expected))
|
|
30
|
+
|
|
31
|
+
test('reject non-strings and arbitrary hosts, credentials or ports', () => {
|
|
32
|
+
const bad = [
|
|
33
|
+
undefined, null, 1, {}, new URL(page('react')), '',
|
|
34
|
+
'http://www.npmjs.com/package/react',
|
|
35
|
+
'https://npmjs.com.evil.invalid/package/react',
|
|
36
|
+
'https://evil-npmjs.com/package/react',
|
|
37
|
+
'https://registry.npmjs.org/package/react',
|
|
38
|
+
'https://npmjs.com./package/react',
|
|
39
|
+
'https://www.%6Epmjs.com/package/react',
|
|
40
|
+
'https://user@www.npmjs.com/package/react',
|
|
41
|
+
'https://@www.npmjs.com/package/react',
|
|
42
|
+
'https://user:password@www.npmjs.com/package/react',
|
|
43
|
+
'https://npmjs.com@evil.invalid/package/react',
|
|
44
|
+
'https://www.npmjs.com:444/package/react',
|
|
45
|
+
'https://www.npmjs.com:/package/react',
|
|
46
|
+
'https://www.npmjs.com:0443/package/react',
|
|
47
|
+
'https://www.npmjs.com\\package\\react',
|
|
48
|
+
` ${page('react')}`,
|
|
49
|
+
page('re\nact'),
|
|
50
|
+
page('react?white space'),
|
|
51
|
+
]
|
|
52
|
+
for (const input of bad) assert.equal(parseNpmPage(input), null, String(input))
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('reject non-package routes and raw or encoded path tricks', () => {
|
|
56
|
+
const bad = [
|
|
57
|
+
'https://www.npmjs.com/',
|
|
58
|
+
'https://www.npmjs.com/package',
|
|
59
|
+
page(''), page('react//'), page('react//v/1.0.0'),
|
|
60
|
+
'https://www.npmjs.com/%70ackage/react',
|
|
61
|
+
'https://www.npmjs.com/Package/react',
|
|
62
|
+
page('react/extra'), page('react/v/1.0.0/extra'), page('react/%76/1.0.0'),
|
|
63
|
+
page('react/../other'), page('react/%2e%2e/other'), page('%2E%2E'),
|
|
64
|
+
page('react%2Fv%2F1.0.0'), page('react%252Fv%252F1.0.0'),
|
|
65
|
+
page('@scope%252Fpkg'), page('%2540scope%252Fpkg'),
|
|
66
|
+
page('@scope%2Fpkg%2Fother'), page('@scope/pkg%2Fother'),
|
|
67
|
+
page('@scope%2F..'), page('@scope%2F%2Fpkg'),
|
|
68
|
+
page('@scope%5Cpkg'), page('react%5Cother'),
|
|
69
|
+
page('react%3Fother'), page('react%23other'), page('react%00'), page('react%0A'), page('react%0D'),
|
|
70
|
+
page('%'), page('%2'), page('%GG'), page('%C0%AF'), page('%ED%A0%80'),
|
|
71
|
+
page('react/v/1.0.0%2Fother'), page('react/v/1.0.0%252Fother'),
|
|
72
|
+
]
|
|
73
|
+
for (const input of bad) assert.equal(parseNpmPage(input), null, input)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('reject ambiguous or unbounded package names and non-exact versions', () => {
|
|
77
|
+
const names = [
|
|
78
|
+
'.', '..', '.hidden', '_hidden', '@', '@scope', '@scope/', '@/pkg', '@.scope/pkg', '@scope/.pkg',
|
|
79
|
+
'@scope/_pkg', '@scope/pkg/other', '@scope%2Fpkg%2Fextra', 'a b', 'a%20b', 'a%00b',
|
|
80
|
+
'a%3Cb', 'a%26b', 'café', '%E2%80%AEabc', 'a'.repeat(215), '@scope/' + 'x'.repeat(208),
|
|
81
|
+
]
|
|
82
|
+
for (const name of names) assert.equal(parseNpmPage(page(name)), null, name)
|
|
83
|
+
const versions = [
|
|
84
|
+
'', 'latest', 'beta', '*', '1', '1.2', '1.2.x', '^1.2.3', '~1.2.3', 'v1.2.3', '=1.2.3',
|
|
85
|
+
'01.2.3', '1.02.3', '1.2.03', '1.2.3-01', '1.2.3-a.01', '1.2.3-', '1.2.3+',
|
|
86
|
+
'1.2.3-foo..bar', '1.2.3+foo..bar', '1.2.3-alpha_beta', '1.2.3%00',
|
|
87
|
+
'9007199254740992.0.0', '1.2.3-' + 'a'.repeat(251),
|
|
88
|
+
]
|
|
89
|
+
for (const version of versions) assert.equal(parseNpmPage(page('pkg/v/' + version)), null, version)
|
|
90
|
+
assert.ok(parseNpmPage(page('x'.repeat(214))))
|
|
91
|
+
assert.ok(parseNpmPage(page('x/v/1.2.3-' + 'a'.repeat(250)))) // 256 chars
|
|
92
|
+
assert.equal(parseNpmPage(page('x') + '?q=' + 'a'.repeat(16384)), null)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('derive a plain title only from matching version JSON', () => {
|
|
96
|
+
const requested = parseNpmPage(page('dsh-inline-link-preview/v/0.5.0-beta.1'))
|
|
97
|
+
assert.equal(extractNpmTitle(requested, { name: 'dsh-inline-link-preview', version: '0.5.0-beta.1', description: '<b>ignored</b>' }), 'dsh-inline-link-preview@0.5.0-beta.1 - npm')
|
|
98
|
+
const scoped = parseNpmPage(page('%40scope%2Fpkg'))
|
|
99
|
+
assert.equal(extractNpmTitle(scoped, { name: '@scope/pkg', version: '1.2.3-beta.1+build.42' }), '@scope/pkg@1.2.3-beta.1+build.42 - npm')
|
|
100
|
+
assert.equal(extractNpmTitle(scoped, { name: '@scope/pkg', version: '0.0.0' }), '@scope/pkg@0.0.0 - npm')
|
|
101
|
+
assert.equal(extractNpmTitle(parseNpmPage(page('pkg/v/1.2.3+build.1')), { name: 'pkg', version: '1.2.3+build.1' }), 'pkg@1.2.3+build.1 - npm')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('registry labels never truncate package version or npm identity', () => {
|
|
105
|
+
const shortName = 'a'.repeat(188), longName = 'a'.repeat(189), version = '1.2.3'
|
|
106
|
+
const short = extractNpmTitle(parseNpmPage(page(shortName)), { name: shortName, version })
|
|
107
|
+
assert.equal(short.length, 200)
|
|
108
|
+
assert.ok(short.endsWith('@1.2.3 - npm'))
|
|
109
|
+
assert.equal(extractNpmTitle(parseNpmPage(page(longName)), { name: longName, version }), null)
|
|
110
|
+
const longVersion = '1.2.3-' + 'b'.repeat(190)
|
|
111
|
+
assert.equal(extractNpmTitle(parseNpmPage(page('pkg')), { name: 'pkg', version: longVersion }), null)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('no label for mismatched, malformed or attacker-controlled registry JSON', () => {
|
|
115
|
+
const requested = parseNpmPage(page('pkg/v/1.2.3'))
|
|
116
|
+
const latest = parseNpmPage(page('pkg'))
|
|
117
|
+
const invalid = [
|
|
118
|
+
undefined, null, '', '1.2.3', 0, [],
|
|
119
|
+
{}, { name: 'other', version: '1.2.3' }, { name: 'PKG', version: '1.2.3' },
|
|
120
|
+
{ name: 'pkg', version: '1.2.4' }, { name: 'pkg', version: '1.2.3+other' },
|
|
121
|
+
{ name: 'pkg', version: 'latest' }, { name: 'pkg', version: '1.2' },
|
|
122
|
+
{ name: 'pkg', version: '01.2.3' }, { name: 'pkg', version: '1.2.3-01' },
|
|
123
|
+
{ name: 'pkg', version: '1.2.3\n' }, { name: 'pkg', version: '1.2.3\u202e' },
|
|
124
|
+
{ name: 'pkg', version: '1.2.3</b>' }, { name: 'pkg\u202e', version: '1.2.3' },
|
|
125
|
+
{ name: ['pkg'], version: '1.2.3' }, { name: 'pkg', version: 123 },
|
|
126
|
+
{ name: 'pkg', version: '1.2.3-' + 'x'.repeat(251) },
|
|
127
|
+
Object.create({ name: 'pkg', version: '1.2.3' }),
|
|
128
|
+
]
|
|
129
|
+
for (const data of invalid) assert.equal(extractNpmTitle(requested, data), null, String(data))
|
|
130
|
+
for (const data of invalid.filter(data => !data || typeof data !== 'object' || !['1.2.4', '1.2.3+other'].includes(data.version))) {
|
|
131
|
+
// Another valid version is appropriate for latest, but malformed fixtures are not.
|
|
132
|
+
assert.equal(extractNpmTitle(latest, data), null, String(data))
|
|
133
|
+
}
|
|
134
|
+
assert.equal(extractNpmTitle(latest, { name: 'pkg', version: '1.2.4' }), 'pkg@1.2.4 - npm')
|
|
135
|
+
assert.equal(extractNpmTitle(latest, { name: 'pkg', version: '1.2.3+other' }), 'pkg@1.2.3+other - npm')
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('reject malformed page descriptors rather than synthesizing unverified labels', () => {
|
|
139
|
+
const data = { name: 'pkg', version: '1.2.3' }
|
|
140
|
+
const valid = parseNpmPage(page('pkg/v/1.2.3'))
|
|
141
|
+
const bad = [
|
|
142
|
+
undefined, null, [], {},
|
|
143
|
+
{ ...valid, packageName: 'other' }, { ...valid, version: 'latest' },
|
|
144
|
+
{ ...valid, version: undefined }, { ...valid, registryUrl: 'https://evil.invalid/pkg/1.2.3' },
|
|
145
|
+
{ ...valid, registryUrl: valid.registryUrl + '?url=evil' },
|
|
146
|
+
{ packageName: 'pkg', version: '1.2.3' },
|
|
147
|
+
Object.create(valid),
|
|
148
|
+
]
|
|
149
|
+
for (const input of bad) assert.equal(extractNpmTitle(input, data), null, String(input))
|
|
150
|
+
assert.equal(extractNpmTitle(valid, new Proxy({}, { getOwnPropertyDescriptor() { throw Error('bad data') } })), null)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
test('validated labels cannot be swapped through changing getters', () => {
|
|
154
|
+
const requested = parseNpmPage(page('pkg/v/1.2.3'))
|
|
155
|
+
let names = 0, versions = 0
|
|
156
|
+
const data = {
|
|
157
|
+
get name() { return ++names === 1 ? 'pkg' : '<unsafe>' },
|
|
158
|
+
get version() { return ++versions === 1 ? '1.2.3' : '\u202e<script>' },
|
|
159
|
+
}
|
|
160
|
+
assert.equal(extractNpmTitle(requested, data), 'pkg@1.2.3 - npm')
|
|
161
|
+
assert.equal(names, 1)
|
|
162
|
+
assert.equal(versions, 1)
|
|
163
|
+
})
|