From 117dcc9b160b7997d1b7a97fdfb5e829151abfe4 Mon Sep 17 00:00:00 2001 From: Wesley Schwengle Date: Wed, 10 Jun 2020 18:31:35 -0400 Subject: [PATCH] Disable the validation badge for those who do not want it (#5994) * disabled on string values: "127.0.0.1", "localhost", "none" --- docs/usage/configuration.md | 2 +- .../components/online-validator-badge.jsx | 6 +-- src/core/utils.js | 9 ++++ test/mocha/core/utils.js | 48 ++++++++++++++++++- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index 1ddf8924..38d80df3 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -70,7 +70,7 @@ Parameter name | Docker variable | Description `responseInterceptor` | _Unavailable_ | `Function=(a => a)`. MUST be a function. Function to intercept remote definition, "Try it out", and OAuth 2.0 responses. Accepts one argument responseInterceptor(response) and must return the modified response, or a Promise that resolves to the modified response. `showMutatedRequest` | `SHOW_MUTATED_REQUEST` | `Boolean=true`. If set to `true`, uses the mutated request returned from a requestInterceptor to produce the curl command in the UI, otherwise the request before the requestInterceptor was applied is used. `supportedSubmitMethods` | `SUPPORTED_SUBMIT_METHODS` | `Array=["get", "put", "post", "delete", "options", "head", "patch", "trace"]`. List of HTTP methods that have the "Try it out" feature enabled. An empty array disables "Try it out" for all operations. This does not filter the operations from the display. -`validatorUrl` | `VALIDATOR_URL` | `String="https://validator.swagger.io/validator" OR null`. By default, Swagger UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to `null` will disable validation. +`validatorUrl` | `VALIDATOR_URL` | `String="https://validator.swagger.io/validator" OR null`. By default, Swagger UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to either `none`, `127.0.0.1` or `localhost` will disable validation. `withCredentials` | `WITH_CREDENTIALS` | `Boolean=false` If set to `true`, enables passing credentials, [as defined in the Fetch standard](https://fetch.spec.whatwg.org/#credentials), in CORS requests that are sent by the browser. Note that Swagger UI cannot currently set cookies cross-domain (see [swagger-js#1163](https://github.com/swagger-api/swagger-js/issues/1163)) - as a result, you will have to rely on browser-supplied cookies (which this setting enables sending) that Swagger UI cannot control. ##### Macros diff --git a/src/core/components/online-validator-badge.jsx b/src/core/components/online-validator-badge.jsx index 8998edba..a40428b1 100644 --- a/src/core/components/online-validator-badge.jsx +++ b/src/core/components/online-validator-badge.jsx @@ -2,7 +2,7 @@ import React from "react" import URL from "url-parse" import PropTypes from "prop-types" -import { sanitizeUrl } from "core/utils" +import { sanitizeUrl, requiresValidationURL } from "core/utils" import win from "core/window" export default class OnlineValidatorBadge extends React.Component { @@ -48,8 +48,8 @@ export default class OnlineValidatorBadge extends React.Component { if ( typeof spec === "object" && Object.keys(spec).length) return null - if (!this.state.url || !this.state.validatorUrl || this.state.url.indexOf("localhost") >= 0 - || this.state.url.indexOf("127.0.0.1") >= 0) { + if (!this.state.url || !requiresValidationURL(this.state.validatorUrl) + || !requiresValidationURL(this.state.url)) { return null } diff --git a/src/core/utils.js b/src/core/utils.js index a1ee531b..042a0b25 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -800,6 +800,15 @@ export function sanitizeUrl(url) { return braintreeSanitizeUrl(url) } + +export function requiresValidationURL(uri) { + if (!uri || uri.indexOf("localhost") >= 0 || uri.indexOf("127.0.0.1") >= 0 || uri === "none") { + return false + } + return true +} + + export function getAcceptControllingResponse(responses) { if(!Im.OrderedMap.isOrderedMap(responses)) { // wrong type! diff --git a/test/mocha/core/utils.js b/test/mocha/core/utils.js index 8ac322dc..df7b9a9d 100644 --- a/test/mocha/core/utils.js +++ b/test/mocha/core/utils.js @@ -23,6 +23,7 @@ import { getExtensions, getCommonExtensions, sanitizeUrl, + requiresValidationURL, extractFileNameFromContentDispositionHeader, deeplyStripKey, getSampleSchema, @@ -1339,7 +1340,52 @@ describe("utils", function() { expect(sanitizeUrl({})).toEqual("") }) }) - + + describe("requiresValidationURL", function() { + it("Should tell us if we require a ValidationURL", function() { + const res = requiresValidationURL("https://example.com") + + expect(res).toBe(true) + }) + + it(".. and localhost is not", function() { + const res = requiresValidationURL("http://localhost") + + expect(res).toBe(false) + }) + + it(".. and neither does 127.0.0.1", function() { + const res = requiresValidationURL("http://127.0.0.1") + + expect(res).toBe(false) + }) + + it(".. even without the proto", function() { + const res = requiresValidationURL("127.0.0.1") + + expect(res).toBe(false) + }) + + it(".. and also not with 'none'", function() { + const res = requiresValidationURL("none") + + expect(res).toBe(false) + }) + + it(".. and also not with 'none'", function() { + const res = requiresValidationURL("none") + + expect(res).toBe(false) + }) + + it(".. and also not with ''", function() { + const res = requiresValidationURL("") + + expect(res).toBe(false) + }) + + }) + describe("getSampleSchema", function() { const oriDate = Date