Files
swagger-ui/test/unit/xss/anchor-target-rel/info.jsx
kyy f464ba2d31
Some checks failed
Node.js CI / build (push) Failing after 2s
Node.js CI / e2e-tests (+(a11y|security|bugs)/**/*cy.js) (push) Failing after 2s
Node.js CI / e2e-tests (features/**/!(o|d|m)*.cy.js) (push) Failing after 2s
Node.js CI / e2e-tests (features/**/+(o|d)*.cy.js) (push) Failing after 2s
Node.js CI / e2e-tests (features/**/m*.cy.js) (push) Failing after 2s
CodeQL / Analyze (javascript) (push) Failing after 2m49s
Security scan for docker image / build (push) Failing after 54s
Update swagger-ui
2025-06-24 13:40:26 +09:00

111 lines
3.1 KiB
JavaScript
Executable File

import React from "react"
import { render } from "enzyme"
import { fromJS } from "immutable"
import Info, { InfoUrl } from "core/components/info"
import Contact from "core/components/contact"
import License from "core/components/license"
import { Link } from "core/components/layout-utils"
import Markdown from "core/components/providers/markdown"
describe("<Info/> Anchor Target Safety", function(){
const dummyComponent = () => null
const components = {
Markdown,
InfoUrl,
License,
Contact,
Link,
}
const baseProps = {
getComponent: c => components[c] || dummyComponent,
host: "example.test",
basePath: "/api",
info: fromJS({
title: "Hello World"
})
}
it("renders externalDocs links with safe `rel` attributes", function () {
const props = {
...baseProps,
externalDocs: fromJS({
url: "http://google.com/"
})
}
let wrapper = render(<Info {...props} />)
const anchor = wrapper.find("a")
expect(anchor.html()).toEqual("http://google.com/")
expect(anchor.attr("target")).toEqual("_blank")
expect(anchor.attr("rel") || "").toMatch("noopener")
expect(anchor.attr("rel") || "").toMatch("noreferrer")
})
it("renders Contact links with safe `rel` attributes", function () {
const props = {
...baseProps,
info: fromJS({
contact: {
url: "http://google.com/",
name: "My Site"
}
})
}
let wrapper = render(<Info {...props} />)
const anchor = wrapper.find("a")
expect(anchor.attr("href")).toEqual("http://google.com/")
expect(anchor.attr("target")).toEqual("_blank")
expect(anchor.attr("rel") || "").toMatch("noopener")
expect(anchor.attr("rel") || "").toMatch("noreferrer")
})
it("renders License links with safe `rel` attributes", function () {
const props = {
...baseProps,
info: fromJS({
license: {
url: "http://mit.edu/"
}
})
}
let wrapper = render(<Info {...props} />)
const anchor = wrapper.find("a")
expect(anchor.attr("href")).toEqual("http://mit.edu/")
expect(anchor.attr("target")).toEqual("_blank")
expect(anchor.attr("rel") || "").toMatch("noopener")
expect(anchor.attr("rel") || "").toMatch("noreferrer")
})
it("renders termsOfService links with safe `rel` attributes", function () {
const props = {
...baseProps,
info: fromJS({
termsOfService: "http://smartbear.com/"
})
}
let wrapper = render(<Info {...props} />)
const anchor = wrapper.find("a")
expect(anchor.attr("href")).toEqual("http://smartbear.com/")
expect(anchor.attr("target")).toEqual("_blank")
expect(anchor.attr("rel") || "").toMatch("noopener")
expect(anchor.attr("rel") || "").toMatch("noreferrer")
})
it("renders definition URL links with safe `rel` attributes", function () {
const props = {
...baseProps,
url: "http://petstore.swagger.io/v2/petstore.json"
}
let wrapper = render(<Info {...props} />)
const anchor = wrapper.find("a")
expect(anchor.attr("href")).toEqual("http://petstore.swagger.io/v2/petstore.json")
expect(anchor.attr("target")).toEqual("_blank")
expect(anchor.attr("rel") || "").toMatch("noopener")
expect(anchor.attr("rel") || "").toMatch("noreferrer")
})
})