AI: address issue #483
このコミットが含まれているのは:
@@ -7,6 +7,10 @@ import type {
|
||||
MaterialFilter,
|
||||
WikiTagReference } from '@/types'
|
||||
|
||||
export const staticMirrorKeys = {
|
||||
definition: ['static_mirror', 'definition'] as const,
|
||||
job: (id: string | null) => ['static_mirror', 'jobs', id] as const }
|
||||
|
||||
export const localesKeys = {
|
||||
root: ['locales'] as const,
|
||||
index: ['locales', 'index'] as const }
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { URL as NodeURL } from 'node:url'
|
||||
import { fireEvent, waitFor } from '@testing-library/react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const script = readFileSync (new NodeURL (
|
||||
'../../../backend/app/views/static_mirror/archive.js', import.meta.url), 'utf8')
|
||||
const archiveWindow = window as typeof window & { archiveConfig?: { origin: string | null } }
|
||||
|
||||
beforeEach (() => {
|
||||
const element = document.createElement ('script')
|
||||
element.src = 'https://mirror.test/subdirectory/archive/assets/archive.js'
|
||||
vi.spyOn (document, 'currentScript', 'get').mockReturnValue (element)
|
||||
delete archiveWindow.archiveConfig
|
||||
document.body.innerHTML = `
|
||||
<nav><a href="ja/posts/index.html">Post</a>
|
||||
<a data-original="/ja/posts/1" href="https://source.test/ja/posts/1">Source</a></nav>
|
||||
<form id="archive-search"><input name="q" value="alias"></form>
|
||||
<ul id="search-results"></ul>`
|
||||
})
|
||||
afterEach (() => {
|
||||
delete archiveWindow.archiveConfig
|
||||
document.body.replaceChildren ()
|
||||
vi.unstubAllGlobals ()
|
||||
})
|
||||
|
||||
describe ('Portable static mirror assets', () => {
|
||||
it ('searches local JSON by aliases using safe text and relative links', async () => {
|
||||
const fetcher = vi.fn ().mockResolvedValue ({ ok: true, json: async () => [
|
||||
{ path: 'ja/tags/1/index.html', title: '<img src=x onerror=alert(1)>', names: ['alias'] },
|
||||
{ path: 'https://outside.test/', title: 'alias' },
|
||||
{ path: '../outside.html', title: 'alias' }] })
|
||||
vi.stubGlobal ('fetch', fetcher)
|
||||
new Function (script) ()
|
||||
fireEvent.submit (document.querySelector ('form')!)
|
||||
await waitFor (() => expect (document.querySelectorAll ('#search-results a')).toHaveLength (1))
|
||||
const link = document.querySelector ('#search-results a')!
|
||||
expect (link).toHaveAttribute ('href', 'ja/tags/1/index.html')
|
||||
expect (link.textContent).toBe ('<img src=x onerror=alert(1)>')
|
||||
expect (document.querySelector ('img')).toBeNull ()
|
||||
expect (fetcher).toHaveBeenCalledTimes (1)
|
||||
expect (fetcher.mock.calls[0][0].href)
|
||||
.toBe ('https://mirror.test/subdirectory/archive/search-index.json')
|
||||
})
|
||||
|
||||
it ('retains baked links without config and changes only source links with valid config', () => {
|
||||
new Function (script) ()
|
||||
const source = document.querySelector ('[data-original]')!
|
||||
expect (source).toHaveAttribute ('href', 'https://source.test/ja/posts/1')
|
||||
archiveWindow.archiveConfig = { origin: 'javascript:alert(1)' }
|
||||
new Function (script) ()
|
||||
expect (source).toHaveAttribute ('href', 'https://source.test/ja/posts/1')
|
||||
archiveWindow.archiveConfig = { origin: 'https://replacement.test/base/' }
|
||||
new Function (script) ()
|
||||
expect (source).toHaveAttribute ('href', 'https://replacement.test/base/ja/posts/1')
|
||||
expect (document.querySelector ('a:not([data-original])'))
|
||||
.toHaveAttribute ('href', 'ja/posts/index.html')
|
||||
})
|
||||
})
|
||||
新しいイシューから参照
ユーザーをブロックする