Skip to content

Commit c3ffd75

Browse files
committed
test: tests for reader
1 parent 0374a0c commit c3ffd75

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

src/parser.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ try {
1515
var { defaultImportReader } = require("./reader");
1616
} catch (e) {}
1717

18+
const defaultTrustedHosts = [
19+
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master"
20+
];
21+
1822
function wy2tokens(
1923
txt,
2024
assert = (msg, pos, b) => {
@@ -666,6 +670,8 @@ async function compile(arg1, arg2, arg3) {
666670
requestTimeout = 2000
667671
} = options;
668672

673+
trustedHosts.push(...defaultTrustedHosts);
674+
669675
const requestOptions = {
670676
allowHttp,
671677
trustedHosts,

src/reader.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
const axios = require("axios");
2+
13
function isHostTrusted(url, trustedHosts) {
2-
// TODO:
4+
for (const host of trustedHosts) {
5+
// FIXME: it can be bypassed by relative path resolving,
6+
// for examples: https://trusted.com/a/../../hijack.com/a/
7+
if (url.startsWith(host)) return true;
8+
}
39
return false;
410
}
511

@@ -24,8 +30,8 @@ async function defaultImportReader(
2430
const uri = dir + "/" + moduleName + ".wy";
2531
if (isHttpURL(uri)) {
2632
if (!allowHttp && !isHostTrusted(uri, trustedHosts)) {
27-
throw new Error(
28-
`URL request "${uri}" is blocked by default for security purpose.` +
33+
throw new URIError(
34+
`URL request "${uri}" is blocked by default for security purpose. ` +
2935
`You can turn it on by specify the "allowHttp" option.`
3036
);
3137
}
@@ -39,12 +45,12 @@ async function defaultImportReader(
3945
} catch (e) {}
4046
} else {
4147
try {
42-
return await eval("require")("fs").promises.readFile(uri, "utf-8");
48+
return await eval("require")("fs").readFileSync(uri, "utf-8");
4349
} catch (e) {}
4450
}
4551
}
4652

47-
throw new Error(
53+
throw new ReferenceError(
4854
`Module "${moduleName}" is not found. Searched in ${importPaths}`
4955
);
5056
}

test/reader.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var { expect } = require("chai");
2+
const { defaultImportReader: reader } = require("../src/reader");
3+
4+
const helloworldContent = "吾有一言。曰「「問天地好在。」」。書之。";
5+
6+
describe("reader", () => {
7+
describe("local", () => {
8+
it("import local files", async () => {
9+
const content = await reader("helloworld", "./examples");
10+
11+
expect(content).eq(helloworldContent);
12+
});
13+
14+
it("search for files", async () => {
15+
const content = await reader("helloworld", [
16+
"./some/invalid/dir",
17+
"./lib",
18+
"./examples"
19+
]);
20+
21+
expect(content).eq(helloworldContent);
22+
});
23+
24+
it("not found", async () => {
25+
try {
26+
await reader("not_exists", "./examples");
27+
} catch (e) {
28+
expect(e).to.be.an.instanceof(ReferenceError);
29+
}
30+
});
31+
});
32+
33+
describe("http", () => {
34+
it("block http imports by default", async () => {
35+
try {
36+
await reader(
37+
"helloworld",
38+
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/"
39+
);
40+
} catch (e) {
41+
expect(e).to.be.an.instanceof(URIError);
42+
}
43+
});
44+
45+
it("load http contents", async () => {
46+
const content = await reader(
47+
"helloworld",
48+
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/",
49+
{
50+
allowHttp: true
51+
}
52+
);
53+
54+
expect(content).eq(helloworldContent);
55+
});
56+
57+
it("load http contents in trusted hosts", async () => {
58+
const content = await reader(
59+
"helloworld",
60+
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/",
61+
{
62+
trustedHosts: [
63+
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples"
64+
]
65+
}
66+
);
67+
68+
expect(content).eq(helloworldContent);
69+
});
70+
71+
it("search for http contents", async () => {
72+
const content = await reader(
73+
"helloworld",
74+
[
75+
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/lib/",
76+
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master/examples/"
77+
],
78+
{
79+
trustedHosts: [
80+
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master"
81+
]
82+
}
83+
);
84+
85+
expect(content).eq(helloworldContent);
86+
});
87+
});
88+
});

0 commit comments

Comments
 (0)