Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/say/lib/comment-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ function renderComments(id, result, config) {
module.exports = {renderComments};

function renderWalineComments(item, {options, public: publicConfig}) {
if (typeof item.pathname !== 'string' || !item.pathname.trim()) {
throw new TypeError('A stored Say pathname is required for native comments');
}
// Waline 3.16 decodes path once before sending it. Protect query-value escapes
// so core receives the stored pathname, not an ID or a partially decoded value.
const path = `/says/${item.id}?pathname=${encodeURIComponent(item.pathname)}&type=3`;
const settings = options.comment || {};
const config = {
serverURL: '/api/comment',
path: encodeURI(`/says/${item.id}?pathname=${encodeURIComponent(item.pathname)}&type=3`),
path: encodeURI(path),
login: publicConfig.forceLogin ? 'force' : 'enable',
lang: settings.locale && settings.locale !== 'auto' ? settings.locale : 'zh-CN',
pageSize: Number(settings.pageSize) || 10,
Expand All @@ -77,15 +82,16 @@ function renderWalineComments(item, {options, public: publicConfig}) {
<script type="module">
const container = document.getElementById('say-comments');
try {
const config = JSON.parse(container.dataset.config);
config.serverURL = new URL('/api/comment', window.location.origin).href;
const [client, response] = await Promise.all([
import('https://cdn.jsdelivr.net/npm/@waline/client@3.16.0/dist/waline.js'),
fetch('/api/comment/api/token', {credentials: 'same-origin'})
fetch(config.serverURL + '/api/token', {credentials: 'same-origin'})
]);
const result = await response.json();
if (!response.ok || result.errno !== 0) throw new Error('session');
if (result.data?.objectId) localStorage.setItem('WALINE_USER', JSON.stringify({...result.data, remember: true}));
else localStorage.removeItem('WALINE_USER');
const config = JSON.parse(container.dataset.config);
container.textContent = '';
client.init({...config, el: container});
} catch {
Expand Down
55 changes: 52 additions & 3 deletions packages/say/test/waline-render.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,70 @@
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const vm = require('node:vm');
const {renderWalineComments} = require('../lib/comment-render');

test('detail page initializes Waline with a generic explicit target and only public settings', () => {
async function initialize(html, origin) {
const container = {dataset: {config: html.match(/data-config="([^"]+)"/)[1].replace(/&quot;/g, '"').replace(/&amp;/g, '&')}};
const requests = [];
let initialized;
const script = html.match(/<script type="module">([\s\S]*?)<\/script>/)[1]
.replace("import('https://cdn.jsdelivr.net/npm/@waline/client@3.16.0/dist/waline.js')", 'Promise.resolve(mockClient)');
await vm.runInNewContext('(async () => {' + script + '})()', {
URL, window: {location: {origin}}, document: {getElementById: () => container},
localStorage: {removeItem() {}, setItem() {}},
mockClient: {init: config => { initialized = config }},
fetch: async url => { requests.push(url); return {ok: true, json: async() => ({errno: 0, data: null})} }
});
assert.ok(initialized, 'Inline initialization must succeed');
assert.deepEqual(requests, [origin + '/api/comment/api/token']);
return initialized;
}

test('detail page initializes Waline with a generic explicit target and only public settings', async() => {
const html = renderWalineComments({id: 7, pathname: 'a&b%2F中文'}, {
options: {jwt_secret: 'PRIVATE', comment: {forceLogin: true, requiredMeta: ['nick']}},
public: {forceLogin: true, captcha: {provider: 'turnstile', siteKey: 'PUBLIC'}}
});
const config = JSON.parse(html.match(/data-config="([^"]+)"/)[1].replace(/&quot;/g, '"').replace(/&amp;/g, '&'));
const config = await initialize(html, 'https://example.com');
const target = new URL(decodeURI(config.path), 'https://example.com');
assert.equal(target.pathname, '/says/7');
assert.equal(target.searchParams.get('pathname'), 'a&b%2F中文');
assert.equal(target.searchParams.get('type'), '3');
assert.equal(config.serverURL, '/api/comment');
assert.equal(config.serverURL, 'https://example.com/api/comment');
assert.equal(config.login, 'force');
assert.equal(config.turnstileKey, 'PUBLIC');
assert.doesNotMatch(html, /PRIVATE|comment-client\.js|comments\/7\/config/);
assert.match(html, /client.init/);
});


test('Waline GET and POST paths retain the explicit database target after client decoding', async() => {
for (const pathname of ['say-1795060504709-8d4a4cffbf2e', '中文/路径', 'a&b?c#d', 'literal%2F+ space']) {
const html = renderWalineComments({id: 150017, pathname}, {
options: {}, public: {}
});
const config = await initialize(html, 'http://localhost:8360');
assert.equal(config.serverURL, 'http://localhost:8360/api/comment');
// Match the pinned client's path normalization, then its HTTP serialization.
const normalized = decodeURI(config.path);
const request = new URL(config.serverURL + '/api/comment');
request.searchParams.set('path', normalized);
assert.equal(request.host, 'localhost:8360');
for (const value of [request.searchParams.get('path'), JSON.parse(JSON.stringify({url: normalized})).url]) {
const target = new URL(value, 'http://localhost:8360');
assert.equal(target.pathname, '/says/150017');
assert.equal(target.searchParams.get('pathname'), pathname);
assert.equal(target.searchParams.get('type'), '3');
}
assert.match(html, /fetch\(config.serverURL \+ '\/api\/token'/);
}
});

test('missing stored pathname cannot silently become an undefined or numeric comment target', () => {
for (const pathname of [undefined, '', ' ']) {
assert.throws(() => renderWalineComments({id: 7, pathname}, {
options: {}, public: {}
}), /stored Say pathname/);
}
});
Loading