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

Are the user commands properly XML encoded/escaped? #95

Open
rgl opened this issue Oct 22, 2019 · 4 comments
Open

Are the user commands properly XML encoded/escaped? #95

rgl opened this issue Oct 22, 2019 · 4 comments

Comments

@rgl
Copy link

rgl commented Oct 22, 2019

Looking at

command = "<![CDATA[" + command + "]]>"
leaves me doubtful of whether that is properly encoding/escaping the user supplied command.

Why is that just putting the user supplied command in a CDATA section without further encoding/escaping? Why is this even trying to encode/escape the data using a string concatenation? I mean, shouldn't the XML API have a proper way to encode a literal text (as-in the XmlNode.InnerText property of an .NET XML element)?

For example, to properly use a CDATA section you must make sure there is no nested CDATA sections nor the user command has the ]]> string in it, etc.

@masterzen
Copy link
Owner

Hi,

Thanks for your comment. While that's true there's no escaping of the CDATA, I doubt it would form a valid command anyway :)

I find it particularly difficult to answer your (I suppose rhetorical) question with something else than: because I probably didn't thought about it at that time or that I didn't care enough for my use case at that particular time :)

Now, if you look deeper you'll notice that this project isn't using the Go standard XML parser/encoder but a naive homegrown DOM system (for a lot of reason including that there wasn't any DOM stuff available without native dependencies at the time of writing the library) to implement the SOAP messages building. This DOM thing doesn't implement escaping and assume things are properly escaped (the name of the library tells it all).

The fun fact is that Go doesn't expose a CDATA escaping function, so we have to add our own which might look like the the private golang one.

Would you want to submit a PR with a fix !
That would be terrific :)

Thanks,

@rgl
Copy link
Author

rgl commented Oct 23, 2019

I should have warned you, but this kind of stuff is like a pet peeve of mine because I've been hurt by broken implementations so many times.

I was curious to known if there was a reason of why it is like it is (e.g. whether the WinRM/PSRemoting SOAP stack was broken and only worked with this kind of XML or because of any other reason), so if you have any more reasons, please let me known ;-)

I can only imagine the pain you went trough to implement this library and interact with this windows subsystem, and only have to thank you, so thank you! :-)

What do you think of instead of using:

commandElement := message.CreateElement(body, "Command", soap.DOM_NS_WIN_SHELL)
commandElement.SetContent(command)

We change this to:

message.CreateTextElement(body, "Command", soap.DOM_NS_WIN_SHELL, command)

Or:

commandElement := message.CreateElement(body, "Command", soap.DOM_NS_WIN_SHELL)
commandElement.SetContent(emitCDATA(command))

And borrow the go implementation that you've mention to implement it in the MessageBuilder class?

Or even better, just use the regular/public xml.EscapeText?

Because the dom library does not do any escaping, this responsibility is supposed to be handled by winrm library? the soap library?

@masterzen
Copy link
Owner

@rgl,

Indeed we can consider the responsibility of escaping to be in the winrm library. It's simpler.

xml.EscapeText would definitely work, it would be interesting to test.

The problem with emitCDATA is that we need to check if the license is compatible with the project and keep the copyright attribution (or reimplement).

@rgl
Copy link
Author

rgl commented Oct 25, 2019

Go uses a 3 clause BSD-style license and both are compatible. But I will try with the public xml.EscapeText first.

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

2 participants