pinokiod 8.3.14 → 8.3.16

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,214 @@
1
+ "use strict"
2
+
3
+ // The files an agent made for a session, as a page reads them: only that
4
+ // session's output folder, nothing outside it, and markdown made safe.
5
+
6
+ const test = require("node:test")
7
+ const assert = require("node:assert/strict")
8
+ const fs = require("fs")
9
+ const os = require("os")
10
+ const path = require("path")
11
+ const http = require("http")
12
+ const express = require("express")
13
+ const SessionFiles = require("../server/lib/session_files")
14
+
15
+ const ID = "20261118-211145-a46df201"
16
+
17
+ // A Pinokio home with one session in the brain and one in an app, and the
18
+ // addresses served from it.
19
+ const withHome = async (run) => {
20
+ const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "pinokio-session-files-")))
21
+ const brain = path.join(home, "brain")
22
+ const appDir = path.join(home, "api", "Speech")
23
+ const brainOut = path.join(brain, "sessions", ID, "output")
24
+ const appOut = path.join(appDir, ".pinokio-sessions", ID, "output")
25
+ fs.mkdirSync(brainOut, { recursive: true })
26
+ fs.mkdirSync(appOut, { recursive: true })
27
+ fs.writeFileSync(path.join(home, "secret.txt"), "not for a page")
28
+ fs.writeFileSync(path.join(brain, "sessions", ID, "request.md"), "the request")
29
+ const app = express()
30
+ SessionFiles.registerSessionFiles(app, { kernel: { homedir: home } })
31
+ const server = http.createServer(app)
32
+ await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve))
33
+ const base = `http://127.0.0.1:${server.address().port}`
34
+ const address = (tail, cwd, more = {}) => `${base}/pinokio/session-files${tail}?${new URLSearchParams({ cwd, session: ID, ...more })}`
35
+ try {
36
+ await run({ home, brain, appDir, brainOut, appOut, address })
37
+ } finally {
38
+ await new Promise((resolve) => server.close(resolve))
39
+ fs.rmSync(home, { recursive: true, force: true })
40
+ }
41
+ }
42
+ const write = (dir, relative, text) => {
43
+ const file = path.join(dir, ...relative.split("/"))
44
+ fs.mkdirSync(path.dirname(file), { recursive: true })
45
+ fs.writeFileSync(file, text)
46
+ }
47
+
48
+ test("a session's output folder is in the brain's sessions or in the app's own folder", () => {
49
+ const home = path.resolve("/home/pinokio")
50
+ assert.equal(SessionFiles.outputDir(home, path.join(home, "brain"), ID), path.join(home, "brain", "sessions", ID, "output"))
51
+ assert.equal(SessionFiles.outputDir(home, `${path.join(home, "brain")}${path.sep}`, ID), path.join(home, "brain", "sessions", ID, "output"), "a trailing separator is the same folder")
52
+ assert.equal(SessionFiles.outputDir(home, path.join(home, "api", "Speech"), ID), path.join(home, "api", "Speech", ".pinokio-sessions", ID, "output"))
53
+ })
54
+
55
+ test("no other folder has session files", () => {
56
+ const home = path.resolve("/home/pinokio")
57
+ const refused = (cwd, id, status) => assert.throws(() => SessionFiles.outputDir(home, cwd, id), (error) => error.status === status)
58
+ refused(home, ID, 404)
59
+ refused(path.join(home, "api"), ID, 404)
60
+ refused(path.join(home, "api", "Speech", "app"), ID, 404)
61
+ refused(path.join(home, "workspaces", "notes"), ID, 404)
62
+ refused(path.join(home, "brain", "..", ".."), ID, 404)
63
+ refused(path.resolve("/somewhere/else"), ID, 404)
64
+ refused("brain", ID, 400)
65
+ refused("", ID, 400)
66
+ refused(path.join(home, "brain"), "../../etc", 400)
67
+ refused(path.join(home, "brain"), "20261118-211145-a46df201/..", 400)
68
+ refused(path.join(home, "brain"), undefined, 400)
69
+ })
70
+
71
+ test("a session that made nothing has an empty list", async () => {
72
+ await withHome(async ({ brain, address }) => {
73
+ const body = await fetch(address("", brain)).then((response) => response.json())
74
+ assert.deepEqual(body, { ok: true, files: [], more: false })
75
+ // a session whose folder was never made (an empty request) is the same
76
+ const none = await fetch(address("", brain, { session: "20200101-000000-00000000" })).then((response) => response.json())
77
+ assert.deepEqual(none, { ok: true, files: [], more: false })
78
+ })
79
+ })
80
+
81
+ test("every file in the output folder is listed by its path, folders included", async () => {
82
+ await withHome(async ({ brain, brainOut, appDir, appOut, address }) => {
83
+ write(brainOut, "report/README.md", "# hi")
84
+ write(brainOut, "report/assets/b10.png", "png")
85
+ write(brainOut, "report/assets/b2.png", "png")
86
+ write(brainOut, "summary.txt", "12345")
87
+ write(appOut, "voice.wav", "wav")
88
+ const body = await fetch(address("", brain)).then((response) => response.json())
89
+ assert.deepEqual(body.files.map((file) => file.path), ["report/assets/b2.png", "report/assets/b10.png", "report/README.md", "summary.txt"])
90
+ assert.equal(body.files[3].size, 5)
91
+ assert.equal(typeof body.files[3].modified, "number")
92
+ const inApp = await fetch(address("", appDir)).then((response) => response.json())
93
+ assert.deepEqual(inApp.files.map((file) => file.path), ["voice.wav"], "an app's session lists its own folder only")
94
+ })
95
+ })
96
+
97
+ test("a link out of the output folder is neither listed nor served", async (t) => {
98
+ await withHome(async ({ home, brain, brainOut, address }) => {
99
+ write(brainOut, "real.txt", "real")
100
+ try {
101
+ fs.symlinkSync(path.join(home, "secret.txt"), path.join(brainOut, "link.txt"))
102
+ fs.symlinkSync(home, path.join(brainOut, "up"))
103
+ } catch (error) {
104
+ t.skip("links cannot be made here")
105
+ return
106
+ }
107
+ const body = await fetch(address("", brain)).then((response) => response.json())
108
+ assert.deepEqual(body.files.map((file) => file.path), ["real.txt"])
109
+ assert.equal((await fetch(address("/file/link.txt", brain))).status, 404)
110
+ assert.equal((await fetch(address("/file/up/secret.txt", brain))).status, 404)
111
+ })
112
+ })
113
+
114
+ test("a file is served as itself, can be saved, and nothing outside the folder is", async () => {
115
+ await withHome(async ({ brain, brainOut, address }) => {
116
+ write(brainOut, "report/notes one.txt", "hello there")
117
+ const shown = await fetch(address("/file/report/notes%20one.txt", brain))
118
+ assert.equal(shown.status, 200)
119
+ assert.equal(await shown.text(), "hello there")
120
+ assert.match(shown.headers.get("content-type"), /^text\/plain/)
121
+ assert.match(shown.headers.get("content-security-policy"), /^sandbox;/, "opened by itself, a file an agent made runs nothing")
122
+ assert.equal(shown.headers.get("x-content-type-options"), "nosniff")
123
+ assert.equal(shown.headers.get("content-disposition"), null, "shown in place")
124
+ const saved = await fetch(address("/file/report/notes%20one.txt", brain, { download: "1" }))
125
+ assert.match(saved.headers.get("content-disposition"), /^attachment; filename="notes one.txt"/)
126
+ const part = await fetch(address("/file/report/notes%20one.txt", brain), { headers: { Range: "bytes=0-4" } })
127
+ assert.equal(part.status, 206, "a video can be read in pieces")
128
+ assert.equal(await part.text(), "hello")
129
+
130
+ for (const escape of ["/file/..%2F..%2Frequest.md", "/file/%2E%2E/request.md", "/file/report/..%2F..%2F..%2Frequest.md", "/file/..%5C..%5Crequest.md"]) {
131
+ const response = await fetch(address(escape, brain))
132
+ assert.ok([400, 404].includes(response.status), `${escape} is refused (${response.status})`)
133
+ assert.notEqual(await response.text(), "the request")
134
+ }
135
+ assert.equal((await fetch(address("/file/missing.txt", brain))).status, 404)
136
+ assert.equal((await fetch(address("/file/report", brain))).status, 404, "a folder is not a file")
137
+ })
138
+ })
139
+
140
+ test("a file in a hidden folder of the session is served", async () => {
141
+ await withHome(async ({ appDir, appOut, address }) => {
142
+ write(appOut, ".cache/a.txt", "x")
143
+ const response = await fetch(address("/file/.cache/a.txt", appDir))
144
+ assert.equal(response.status, 200)
145
+ assert.equal(await response.text(), "x")
146
+ })
147
+ })
148
+
149
+ test("a link in a file leads to another file of the session, or nowhere", () => {
150
+ const at = (href) => SessionFiles.linkedPath("report/README.md", href)
151
+ assert.equal(at("assets/pic.png"), "report/assets/pic.png")
152
+ assert.equal(at("./assets/pic%20one.png?raw=1#top"), "report/assets/pic one.png")
153
+ assert.equal(at("../summary.wav"), "summary.wav")
154
+ assert.equal(at("/summary.wav"), "summary.wav", "from the top of the output folder")
155
+ assert.equal(at("../../outside.txt"), null, "it never climbs out")
156
+ assert.equal(at("#section"), null)
157
+ assert.equal(at("https://example.com/a.png"), null)
158
+ assert.equal(at("//example.com/a.png"), null)
159
+ assert.equal(at("javascript:alert(1)"), null)
160
+ assert.equal(at("data:text/html,x"), null)
161
+ assert.equal(at("%E0%A4%A"), null, "an address that cannot be read")
162
+ assert.equal(at(""), null)
163
+ })
164
+
165
+ test("markdown becomes a page with nothing of the file's own HTML in it", () => {
166
+ const cwd = "/home/pinokio/brain"
167
+ const html = SessionFiles.renderMarkdown([
168
+ "# Title",
169
+ "",
170
+ "<script>alert(1)</script>",
171
+ "",
172
+ "Text <img src=x onerror=alert(2)> and <b onclick=\"alert(3)\">bold</b>.",
173
+ "",
174
+ "![The picture](assets/pic%20one.png)",
175
+ "![Remote](https://example.com/track.png)",
176
+ "![Bad](javascript:alert(4))",
177
+ "",
178
+ "[other file](notes/prompt.txt) [site](https://example.com/?a=1&b=\"2\") [bad](javascript:alert(5)) [worse](JaVaScRiPt:alert(6)) [data](data:text/html,<script>alert(7)</script>) [climb](../../../etc/passwd)"
179
+ ].join("\n"), { from: "report/README.md", cwd, id: ID })
180
+ assert.match(html, /<h1>Title<\/h1>/)
181
+ assert.doesNotMatch(html, /<script/i)
182
+ assert.doesNotMatch(html, /onerror|onclick/i)
183
+ assert.doesNotMatch(html, /href="(?!#"|https?:\/\/)/i, "a link is a web address or a file of the session, nothing else")
184
+ assert.doesNotMatch(html, /src="(?!\/pinokio\/session-files\/file\/)/i, "a picture is a file of the session, nothing else")
185
+ assert.match(html, /<a href="#" data-session-file="report\/notes\/prompt.txt">other file<\/a>/)
186
+ assert.match(html, /<a href="https:\/\/example.com\/\?a=1&amp;b=%222%22" target="_blank" rel="noopener noreferrer">site<\/a>|<a href="https:\/\/example.com\/\?a=1&amp;b=&quot;2&quot;" target="_blank" rel="noopener noreferrer">site<\/a>/)
187
+ assert.match(html, /<a href="https:\/\/example.com\/track.png" target="_blank" rel="noopener noreferrer">Remote<\/a>/, "a picture from another site is a link, not loaded")
188
+ const picture = /<img src="([^"]+)" alt="The picture"/.exec(html)
189
+ assert.ok(picture, "the session's own picture is shown")
190
+ const address = new URL(picture[1].replace(/&amp;/g, "&"), "http://x")
191
+ assert.equal(decodeURIComponent(address.pathname), "/pinokio/session-files/file/report/assets/pic one.png")
192
+ assert.equal(address.searchParams.get("cwd"), cwd)
193
+ assert.equal(address.searchParams.get("session"), ID)
194
+ for (const word of ["bad", "worse", "data", "climb"]) {
195
+ assert.doesNotMatch(html, new RegExp(`<a[^>]*>${word}</a>`), `"${word}" is plain words`)
196
+ assert.match(html, new RegExp(word))
197
+ }
198
+ })
199
+
200
+ test("a markdown file is served as that page", async () => {
201
+ await withHome(async ({ brain, brainOut, address }) => {
202
+ write(brainOut, "report/README.md", "# Hello\n\n![p](assets/p.png)\n\n<script>bad()</script>\n")
203
+ write(brainOut, "report/assets/p.png", "png")
204
+ const body = await fetch(address("/markdown/report/README.md", brain)).then((response) => response.json())
205
+ assert.equal(body.ok, true)
206
+ assert.match(body.html, /<h1>Hello<\/h1>/)
207
+ assert.doesNotMatch(body.html, /<script/)
208
+ const src = /<img src="([^"]+)"/.exec(body.html)[1].replace(/&amp;/g, "&")
209
+ const picture = await fetch(new URL(src, address("", brain)))
210
+ assert.equal(picture.status, 200, "the page's picture is found at the address it was given")
211
+ assert.equal(await picture.text(), "png")
212
+ assert.equal((await fetch(address("/markdown/report/none.md", brain))).status, 404)
213
+ })
214
+ })
@@ -0,0 +1,47 @@
1
+ // The font size field of the terminal appearance menu
2
+ // (server/public/terminal-settings.js): a size is typed one digit at a time, so
3
+ // the field is not corrected under the fingers.
4
+ const assert = require('node:assert/strict')
5
+ const fs = require('node:fs')
6
+ const path = require('node:path')
7
+ const test = require('node:test')
8
+ const { JSDOM } = require('jsdom')
9
+
10
+ const script = fs.readFileSync(path.resolve(__dirname, '../server/public/terminal-settings.js'), 'utf8')
11
+
12
+ test('a font size can be typed digit by digit, and is settled on leaving the field', (t) => {
13
+ const dom = new JSDOM('<!doctype html><body><div id="runner"></div></body>', {
14
+ url: 'http://127.0.0.1:42000/',
15
+ runScripts: 'outside-only',
16
+ pretendToBeVisual: true,
17
+ })
18
+ t.after(() => dom.window.close())
19
+ const { window } = dom
20
+ window.eval(script)
21
+ const settings = window.PinokioTerminalSettings
22
+ const menu = settings.createMenu(window.document.getElementById('runner'))
23
+ // (as initRunnerMenus does for a terminal page's menu)
24
+ settings.menus.add(menu)
25
+ const { sizeInput } = menu
26
+ const type = (value) => {
27
+ sizeInput.value = value
28
+ sizeInput.dispatchEvent(new window.Event('input', { bubbles: true }))
29
+ }
30
+
31
+ type('1')
32
+ assert.equal(sizeInput.value, '1', 'the first digit of 14 stays as typed')
33
+ assert.equal(settings.preferences.fontSize, undefined)
34
+ type('14')
35
+ assert.equal(sizeInput.value, '14')
36
+ assert.equal(settings.preferences.fontSize, 14)
37
+
38
+ type('140')
39
+ assert.equal(sizeInput.value, '140')
40
+ assert.equal(settings.preferences.fontSize, 14, 'too large a number changes nothing while typing')
41
+ sizeInput.dispatchEvent(new window.Event('blur'))
42
+ assert.equal(sizeInput.value, '72', 'leaving the field brings it into range')
43
+ assert.equal(settings.preferences.fontSize, 72)
44
+
45
+ type('')
46
+ assert.equal(settings.preferences.fontSize, undefined, 'an emptied field goes back to the default size')
47
+ })
@@ -0,0 +1,58 @@
1
+ // What server/public/terminal-settings-sheet.js adds to the terminal appearance
2
+ // menu for a phone: Done, a − and a + for the font size, and the fold of the
3
+ // theme colors. Which screens show them is the stylesheet's part and is not
4
+ // covered: this page has no layout.
5
+ const assert = require('node:assert/strict')
6
+ const fs = require('node:fs')
7
+ const path = require('node:path')
8
+ const test = require('node:test')
9
+ const { JSDOM } = require('jsdom')
10
+
11
+ const script = (name) => fs.readFileSync(path.resolve(__dirname, '../server/public', name), 'utf8')
12
+
13
+ test('the terminal appearance menu gets its phone parts as it is opened', (t) => {
14
+ const dom = new JSDOM('<!doctype html><body><div id="runner"></div></body>', {
15
+ url: 'http://127.0.0.1:42000/',
16
+ runScripts: 'outside-only',
17
+ pretendToBeVisual: true,
18
+ })
19
+ t.after(() => dom.window.close())
20
+ const { window } = dom
21
+ window.eval(script('terminal-settings.js'))
22
+ window.eval(script('terminal-settings-sheet.js'))
23
+ const settings = window.PinokioTerminalSettings
24
+ const record = settings.createMenu(window.document.getElementById('runner'))
25
+ // (as initRunnerMenus does for a terminal page's menu)
26
+ settings.menus.add(record)
27
+ const { menu, button, sizeInput, themeSection } = record
28
+
29
+ button.click()
30
+ assert.equal(menu.hidden, false)
31
+ assert.equal(menu.firstChild.className, 'terminal-config-sheet-head')
32
+
33
+ // the steps start from the size in use
34
+ sizeInput.placeholder = '12'
35
+ const [smaller, larger] = menu.querySelectorAll('.terminal-config-step')
36
+ assert.equal(smaller.nextSibling, sizeInput)
37
+ larger.click()
38
+ assert.equal(settings.preferences.fontSize, 13)
39
+ assert.equal(sizeInput.value, '13')
40
+ smaller.click()
41
+ smaller.click()
42
+ assert.equal(settings.preferences.fontSize, 11)
43
+ assert.equal(menu.hidden, false, 'a step leaves the menu open')
44
+
45
+ const fold = themeSection.querySelector('.terminal-config-fold')
46
+ assert.equal(fold.getAttribute('aria-expanded'), 'false')
47
+ fold.click()
48
+ assert.equal(fold.getAttribute('aria-expanded'), 'true')
49
+ assert.equal(themeSection.classList.contains('terminal-config-theme--open'), true)
50
+
51
+ menu.querySelector('.terminal-config-sheet-done').click()
52
+ assert.equal(menu.hidden, true)
53
+
54
+ // opened again, it is not dressed twice
55
+ button.click()
56
+ assert.equal(menu.querySelectorAll('.terminal-config-sheet-head, .terminal-config-fold').length, 2)
57
+ assert.equal(menu.querySelectorAll('.terminal-config-step').length, 2)
58
+ })