Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

koa2-swagger-ui does not render the swagger.json file and it shows error messages #218

Open
rmmariano opened this issue Feb 28, 2022 · 3 comments

Comments

@rmmariano
Copy link

Hello, I've just installed koa2-swagger-ui package via npm and I've updated my code with the settings on the documentation, however, for some reason, it does not load the swagger.json file.

The /docs route opens a blank page. When I inspect the console log, it shows some error messages:

Refused to load the script 'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.0/swagger-ui-bundle.js' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

docs:1 Refused to load the script 'https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.0/swagger-ui-standalone-preset.js' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

docs:71 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-CUXEv2EIcoCxm5IYzJhXO4B2nBMWti4GA5+EGW7dpG4='), or a nonce ('nonce-...') is required to enable inline execution.

image

I've tried to add the package to my project on both ways the documentation describes, directly to koa app and through koa-router, but both options show the same messages above.

I send below a sample of my code:

import Koa from 'koa';
import { koaSwagger } from 'koa2-swagger-ui';
import Router from 'koa-router';

let app = new Koa();
let router = new Router();

app.use(koaSwagger({
    routePrefix: '/docs', 
    swaggerOptions: {
        url: 'http://petstore.swagger.io/v2/swagger.json',
    }
}));

// or

router.use(koaSwagger({
    routePrefix: '/docs',
    swaggerOptions: {
      url: 'http://petstore.swagger.io/v2/swagger.json',
    }
}));

My dependencies are:

"devDependencies": {
    "@babel/core": "^7.16.12",
    "@babel/eslint-parser": "^7.16.5",
    "chance": "^1.1.8",
    "eslint": "^8.8.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.24.2",
    "eslint-plugin-import-helpers": "^1.1.0",
    "eslint-plugin-jest": "^26.0.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-prettier": "^4.0.0",
    "husky": "^7.0.4",
    "jest": "^27.0.6",
    "lint-staged": "^12.3.2",
    "nock": "^13.1.3",
    "nodemon": "^2.0.15",
    "prettier": "^2.0.5",
    "supertest": "^6.1.3"
  },
  "dependencies": {
    "@koa/cors": "^3.1.0",
    "@newrelic/winston-enricher": "^2.1.0",
    "axios": "^0.25.0",
    "dotenv": "^14.3.2",
    "ip": "^1.1.5",
    "jsonwebtoken": "^8.5.1",
    "koa": "^2.13.1",
    "koa-bodyparser": "^4.3.0",
    "koa-helmet": "^6.1.0",
    "koa-jwt": "^4.0.1",
    "koa-logger": "^3.2.1",
    "koa-logger-winston": "^0.0.2",
    "koa-router": "^10.0.0",
    "koa2-swagger-ui": "^5.3.0",
    "moment": "^2.29.1",
    "mssql": "^8.0.2",
    "newrelic": "^8.7.0",
    "sha256": "^0.2.0",
    "tedious": "^14.2.0",
    "tv4": "^1.3.0",
    "uuid": "^8.3.2",
    "winston": "^3.3.3"
  }

Thank you in advance.

@ashishbyteridge
Copy link

I am also facing this issue.

@xulsup
Copy link

xulsup commented Jun 19, 2022

you can do this in a local environment。
image

@Qubaish
Copy link

Qubaish commented Aug 16, 2023

If you are using Helmet in your node project then either remove this or add below property.

app.use( helmet.contentSecurityPolicy({ reportOnly: true, directives: { /* ... */ }, }) );

Its fixed my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants