Skip to content

fix: prevent html injection in cockpit#1381

Merged
andremedeiros merged 1 commit intomasterfrom
fix/html-injection-in-cockpit
Mar 5, 2019
Merged

fix: prevent html injection in cockpit#1381
andremedeiros merged 1 commit intomasterfrom
fix/html-injection-in-cockpit

Conversation

@andremedeiros
Copy link
Collaborator

No description provided.

@andremedeiros andremedeiros force-pushed the fix/html-injection-in-cockpit branch from 96b6e4e to 5301c9b Compare February 27, 2019 21:11
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\"/g, "&quot;")
.replace(/\'/g, "&#39;");
Copy link
Collaborator

Choose a reason for hiding this comment

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

There are npm packages that do that, but if we prefer our version of it, it's better to put it in a utils, because you use it at least twice.

(ws, _req) => {
this.events.on('process-log-' + this.processName, function (log) {
log.msg = self.escapeMessage(log.msg);
log.msg_clear = self.escapeMessage(log.msg_clear);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of introducing self , just replace function (log) { with (log) => {

@andremedeiros andremedeiros force-pushed the fix/html-injection-in-cockpit branch from 5301c9b to e2b3a11 Compare February 27, 2019 21:23
Copy link
Collaborator

@emizzle emizzle left a comment

Choose a reason for hiding this comment

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

Awesome work! I would like to see a description for the PR, but I understand that the title does a lot of justice. Something like "prevent API responses from XSS attacks" would be helpful.

let response = result;
if (typeof result !== "string") {
response = stringify(result, utils.jsonFunctionReplacer, 2);
this.logger.info(response);
Copy link
Collaborator

@emizzle emizzle Feb 27, 2019

Choose a reason for hiding this comment

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

Does stringify handle escaping HTML that could be produced from stringifying an object?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does not, but I'm pretty sure that this is the JSON printer, which we still want to be parsed as HTML.

The alternative is introducing plaintext / JSON response types that the cockpit could respond to, but that's a whole other bag of worms.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, so when we stringify in this case, we return it in the response at the end. So I think maybe we should still escape it, no? For example,

        if (typeof result !== "string") {
          response = stringify(result, utils.jsonFunctionReplacer, 2);
          this.logger.info(response);
        } else {
          // Avoid HTML injection in the Cockpit
          this.logger.info(response);
        }
        response = escapeHtml(response);
        return res.send({ result: response });

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Isn't the JSON tree explorer rendered in the backend? Or is it the frontend that renders it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that's a frontend thing, but could be wrong.

if(typeof message !== "string") return message;

return message
.replace(/&/g, "&amp;")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a library that would handle this for us and possibly handle additional use cases that we haven't thought of?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably tons of them, but this is a simple enough fix that I think we can handle it ourselves without introducing yet another dependency.

@emizzle
Copy link
Collaborator

emizzle commented Mar 1, 2019

Could we also apply this to packages/embark-ui/src/components/Console.js line 94 and line 100 to prevent console command usage containing < and > from injecting elements in to the cockpit?

@andremedeiros
Copy link
Collaborator Author

This might not be necessary, in favour of #1385. @emizzle WDYT?

@andremedeiros andremedeiros changed the title prevent html injection in cockpit fix: prevent html injection in cockpit Mar 4, 2019
@emizzle
Copy link
Collaborator

emizzle commented Mar 5, 2019

Yep, looks good!

@andremedeiros andremedeiros merged commit 78201ce into master Mar 5, 2019
@andremedeiros andremedeiros deleted the fix/html-injection-in-cockpit branch March 5, 2019 19:15
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.

4 participants