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

Add Sanitize API #349

Merged
merged 11 commits into from
Jul 7, 2022
Merged

Add Sanitize API #349

merged 11 commits into from
Jul 7, 2022

Conversation

ArturWierzbicki
Copy link
Contributor

@ArturWierzbicki ArturWierzbicki commented Jun 16, 2022

grafana/grafana#50936 for context

See https://github.com/grafana/grafana/pull/50936/files#diff-7d6b47ef00ca0a64668dd9913f6b4279b06c3d2a0d975c70daad20691abab72a for an exemplary HTTP request

The HTTP request requires two files:

  1. file to be sanitized under key file
  2. JSON config under key config, example
{
  "config": {
    "USE_PROFILES": {
      "svg": true,
      "svgFilters": true
    },
    "ADD_TAGS": [
      "use"
    ],
    "allowAllLinksInSvgUseTags": false
  },
  "configType": "DOMPurify"
}

image

@CLAassistant
Copy link

CLAassistant commented Jun 16, 2022

CLA assistant check
All committers have signed the CLA.

Comment on lines 9 to 10
bytes domPurifyConfig = 3;
bool allowAllLinksInSvgUseTags = 4;
Copy link
Member

Choose a reason for hiding this comment

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

maybe we have this proto definition be more "open" to non svg thigns in the future?

Perhaps

Suggested change
bytes domPurifyConfig = 3;
bool allowAllLinksInSvgUseTags = 4;
string sanitizer = 3; // 'svg' | ...
bytes sanitizerConfig = 4; // json blob that depends on the sanitier type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah - thats better. For string sanitizer I would probably use the library name or "sanitization logic type" instead of the file type so we avoid having an abstraction over the library config. DOMPurify has a pretty extensive API and it can support html, svg or mathml out of the box. Soo, something like this:

sanitizer: 'dompurify'
config: { dompurifyConfig: { ... }, domPurifyExtraFunctionalitiesBuiltByUsConfig: { ... } }

and

sanitizer: 'some-other-image-sanitizer'
config: { some-other-image-sanitizerConfig': { ... }}

what do you think?


message SanitizeResponse {
string error = 1;
bytes sanitized = 2;
Copy link
Member

Choose a reason for hiding this comment

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

is there relevant info we may also return? I guess we can add that later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure, I could add a generic string info; field and for now populate it with "sanitized" / "not sanitized" depending on whether the hash changed. We could use that field in the future if we choose to migrate to some other library that returns some info on itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it would be difficult to differentiate between sanitized vs unsanitized files even right now, as dompurify modifies all svgs. I think we can do it later if we'd like

src/types.ts Outdated Show resolved Hide resolved
@ArturWierzbicki ArturWierzbicki marked this pull request as ready for review June 20, 2022 17:30
src/types.ts Outdated
Comment on lines 31 to 37
export type SanitizeRequest = {
filename: string;
content: string;
contentType?: string;
domPurifyConfig?: DOMPurify.Config;
allowAllLinksInSvgUseTags?: boolean;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this is never used, is there a reason to not remove it and remove the V2 of SanitizeRequestV2?

@@ -49,6 +61,14 @@ export class HttpServer {

this.app.get('/render', asyncMiddleware(this.render));
this.app.get('/render/csv', asyncMiddleware(this.renderCSV));
this.app.post(
'/render/sanitize',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'/render/sanitize',
'/sanitize',

This would be better if we could get rid of the render prefix and do something in Grafana to replace /render in the renderer URL with /sanitize for example.
Else, it will mess up the metrics we collect about image renderer (set up here: https://github.com/grafana/grafana-image-renderer/blob/master/src/service/metrics_middleware.ts#L49). It could be interesting to have metrics for this endpoint as well but we should keep them separate.

src/types.ts Outdated
@@ -1,3 +1,5 @@
import * as DOMPurify from 'dompurify';
Copy link
Contributor

@AgnesToulet AgnesToulet Jun 29, 2022

Choose a reason for hiding this comment

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

What about putting all these new types in a types.ts file in sanitizer folder? I think it would help keep things separated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, extracted all of them to sanitizer/types.ts

Copy link
Contributor

@AgnesToulet AgnesToulet left a comment

Choose a reason for hiding this comment

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

LGTM! Tested this with an older version of Grafana and it's working fine as well.

@ArturWierzbicki ArturWierzbicki merged commit 1e14736 into master Jul 7, 2022
@ArturWierzbicki ArturWierzbicki deleted the #50597-sanitizer branch July 7, 2022 12:05
@Bujupah
Copy link
Contributor

Bujupah commented Jan 3, 2023

hey... wondering what is the use of this feature? would you mind brief explain this @ArturWierzbicki

@ArturWierzbicki
Copy link
Contributor Author

hey @Bujupah! The use of this feature is server side sanitization of SVGs hosted in Grafana. There is some more context here if you are interested: grafana/grafana#50597.
Right now, this feature is unused - we still haven't added the capability of uploading media files to Grafana.

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

Successfully merging this pull request may close these issues.

5 participants