Skip to content

Support rendering OpenAPI and Swagger documents #25824

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

Closed
wants to merge 0 commits into from

Conversation

lunny
Copy link
Member

@lunny lunny commented Jul 11, 2023

Fix #20852

At the beginning, I want to use iframe but finally I found it'd difficult to implement it safely. So the new idea is open a new page to render the file.

There will be a placeholder button

图片

And click the button will redirect to the real render page.

图片

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jul 11, 2023
@silverwind
Copy link
Member

silverwind commented Jul 11, 2023

No, the current approach is not right. We should load swagger-ui-dist from JS code, with the backend only signalling via HTML attribute that the content is to be rendered as openapi, very similar to the PDF rendering.

To achieve this, put the same dynamic import in both web_src/js/standalone/swagger.js and the new web_src/js/render/swagger.js:

const [{default: SwaggerUI}] = await Promise.all([
  import(/* webpackChunkName: "swagger-ui" */'swagger-ui-dist/swagger-ui-es-bundle.js'),
  import(/* webpackChunkName: "swagger-ui" */'swagger-ui-dist/swagger-ui.css')
]);

Having the exact same block will allow webpack to dedupe the chunks.

Also, I'm pretty sure we want to contain swagger-ui's CSS into an <iframe> because otherwise it will wreak havoc on our styles, but let's take it one step at a time.

@silverwind silverwind changed the title WIP: Support render opanapi v2.0 WIP: Support render OpenAPI and Swagger documents Jul 11, 2023
@silverwind
Copy link
Member

Changed the title. This should support OpenAPI up to 3.1, and all Swagger versions.

@silverwind silverwind changed the title WIP: Support render OpenAPI and Swagger documents WIP: Support rendering OpenAPI and Swagger documents Jul 11, 2023
@lunny lunny added the type/feature Completely new functionality. Can only be merged if feature freeze is not active. label Jul 23, 2023
@lunny lunny added this to the 1.21.0 milestone Jul 23, 2023
@lunny lunny marked this pull request as ready for review July 23, 2023 14:02
@lunny lunny changed the title WIP: Support rendering OpenAPI and Swagger documents Support rendering OpenAPI and Swagger documents Jul 23, 2023
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jul 26, 2023
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jul 26, 2023
@@ -62,12 +62,14 @@ func RenderFile(ctx *context.Context) {
treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
}

ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'; sandbox allow-scripts")
ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'; sandbox allow-scripts allow-same-origin")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the rendered content contains user's javascript, would allow-same-origin cause XSS?

Copy link
Member

@silverwind silverwind Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iframe with sandbox attributes allow-scripts allow-same-origin allows the embedee to remove the sandbox attribute. From MDN:

When the embedded document has the same origin as the embedding page, it is strongly discouraged to use both allow-scripts and allow-same-origin, as that lets the embedded document remove the sandbox attribute — making it no more secure than not using the sandbox attribute at all.

Not that I see a big issue here because openapi specs can not contain executable code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this code is for all renders, not only for OpenAPI.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For PDF renderer, it can definitely be an issue as those could maybe contain JS.

Copy link
Member

@silverwind silverwind Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if the iframe really needs allow-same-origin, we should specifically allow it only for the openapi renderer, otherwise remove the attribute. In fact, I strongly prefer removal to avoid future security issues.

@puni9869 puni9869 self-requested a review July 26, 2023 09:04
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. labels Jul 26, 2023
@lunny
Copy link
Member Author

lunny commented Jul 26, 2023

The current swagger.js just supports 2.0 specification and just support JSON file. It needs to be changed to support 3.0 specification and YAML file.

@silverwind
Copy link
Member

The current swagger.js just supports 2.0 specification and just support JSON file. It needs to be changed to support 3.0 specification and YAML file.

We use the latest swagger-ui available (from npm), so it should support everything up to openapi 3.1, json or yaml.

@silverwind
Copy link
Member

silverwind commented Aug 10, 2023

I'll give this a review/test soon.

@silverwind
Copy link
Member

silverwind commented Aug 10, 2023

Hmm I do get Uncaught (in promise) TypeError: can't access property "sort", spec.schemes is undefined when I try to view this openapi 3.1 document. Also, the "open in new page" needs to be removed, we can embed the iframe.

@silverwind
Copy link
Member

Hmm I do get Uncaught (in promise) TypeError: can't access property "sort", spec.schemes is undefined when I try to view this openapi 3.1 document. Also, the "open in new page" needs to be removed, we can embed the iframe.

Fixed it with 897be68, now openapi 3.1 renders fine.

@silverwind
Copy link
Member

silverwind commented Aug 10, 2023

d866b87 makes it work for yaml specs as well. Had to add the yaml parser dependency as swagger-ui can only process js objects.

@silverwind
Copy link
Member

TODOs:

  • Embed in iframe
  • Inherit dark/light theme from parent page

@silverwind silverwind closed this Aug 29, 2023
@silverwind silverwind force-pushed the lunny/support_render_openapi branch from 1041279 to 3c91b09 Compare August 29, 2023 20:03
@GiteaBot GiteaBot removed this from the 1.21.0 milestone Aug 29, 2023
@silverwind
Copy link
Member

silverwind commented Aug 29, 2023

Oops, I think I destroyed this PR with some force-push action (it wouldn't let me push my latest commit even after ensuring I was on tip multiple times via reset and branch re-creation).

New PR to continue: #26802

@lunny lunny deleted the lunny/support_render_openapi branch August 30, 2023 01:09
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Nov 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPI Viewer
7 participants