release: v1.2.4 — 修复未配置防盗链时外链图裂
GitHub Release / release (push) Canceled after 0s
Publish Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Canceled after 0s
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Canceled after 0s
Publish Docker image / merge (push) Canceled after 0s
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
GitHub Release / release (push) Canceled after 0s
Publish Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Canceled after 0s
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Canceled after 0s
Publish Docker image / merge (push) Canceled after 0s
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
This commit is contained in:
1 parent
20afda5869
commit
81dcc2c15e
7 files changed
+96
-7
No files matched your search
@@ -1,5 +1,30 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { isRefererAllowed } from './serve-image'
|
||||
import { describe, expect, it, vi, afterEach } from 'vitest'
|
||||
import type { H3Event } from 'h3'
|
||||
import { isHotlinkBlocked, isRefererAllowed } from './serve-image'
|
||||
import { getSetting, SETTINGS_ALLOWED_REFERER_HOSTS } from './db'
|
||||
|
||||
vi.mock('./db', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('./db')>()
|
||||
return {
|
||||
...actual,
|
||||
getSetting: vi.fn()
|
||||
}
|
||||
})
|
||||
|
||||
vi.stubGlobal('getRequestURL', () => new URL('https://pic.example.com'))
|
||||
|
||||
vi.stubGlobal('useRuntimeConfig', () => ({
|
||||
apiUploadToken: '',
|
||||
siteBaseUrl: '',
|
||||
imageBaseUrl: ''
|
||||
}))
|
||||
|
||||
function mockEvent(): H3Event {
|
||||
return {
|
||||
node: { req: {}, res: {} },
|
||||
context: {}
|
||||
} as H3Event
|
||||
}
|
||||
|
||||
describe('isRefererAllowed', () => {
|
||||
const allowedHosts = new Set(['pic.example.com', 'blog.example.com'])
|
||||
@@ -21,3 +46,42 @@ describe('isRefererAllowed', () => {
|
||||
expect(isRefererAllowed('not-a-url', allowedHosts)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isHotlinkBlocked', () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
delete process.env.ALLOWED_REFERER_HOSTS
|
||||
})
|
||||
|
||||
it('does not block external referers when protection is not configured', () => {
|
||||
vi.mocked(getSetting).mockReturnValue(null)
|
||||
const event = mockEvent()
|
||||
expect(isHotlinkBlocked(event, 'https://blog.example.com/post')).toBe(false)
|
||||
})
|
||||
|
||||
it('does not block external referers when db explicitly cleared', () => {
|
||||
process.env.ALLOWED_REFERER_HOSTS = 'blog.example.com'
|
||||
vi.mocked(getSetting).mockImplementation(key =>
|
||||
key === SETTINGS_ALLOWED_REFERER_HOSTS ? '' : null
|
||||
)
|
||||
const event = mockEvent()
|
||||
expect(isHotlinkBlocked(event, 'https://forum.example.com/thread')).toBe(false)
|
||||
})
|
||||
|
||||
it('blocks external referers when protection is configured', () => {
|
||||
vi.mocked(getSetting).mockImplementation(key =>
|
||||
key === SETTINGS_ALLOWED_REFERER_HOSTS ? 'blog.example.com' : null
|
||||
)
|
||||
const event = mockEvent()
|
||||
expect(isHotlinkBlocked(event, 'https://evil.example.com/hotlink')).toBe(true)
|
||||
expect(isHotlinkBlocked(event, 'https://blog.example.com/post')).toBe(false)
|
||||
})
|
||||
|
||||
it('still allows missing referer when protection is configured', () => {
|
||||
vi.mocked(getSetting).mockImplementation(key =>
|
||||
key === SETTINGS_ALLOWED_REFERER_HOSTS ? 'blog.example.com' : null
|
||||
)
|
||||
const event = mockEvent()
|
||||
expect(isHotlinkBlocked(event, null)).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user