Need you opinion on a hack

This topic contains 3 replies, has 2 voices, and was last updated by  Mariusz Wojcik 3 years, 8 months ago.

  • Author
    Posts
  • #13526

    Mariusz Wojcik
    Participant

    Hi all,

    I am working on a module for administering RabbitMQ servers. All communication with the server is through REST API, so I'm using Invoke-RestMethod extensively. Unfortunately, some methods require passing forward slash (/) in the url in it's encoded form: %2f. As it turns out, in .NET Framework versions earlier than 4.5, the Invoke-RestMethod will automatically un-escape the forward slash back to /. After some research I found a way to hack that behaviour. It requires bit of reflection to modify internal flag on UriParser class, but the problem is that this will affect whole PowerShell session. I came with following way to tackle the problem:

    • on module registration – detect if hack is needed, print warning to the user
    • before invoking rest method which requires hack – modify internal flag
    • after invoking rest method, in finally block – restore state before applying hack
    • add verbose output whenever hack is applied
    • add extra information in module's help explaining the problem

    Can anyone think about what else I could do to minimise impact of the hack. What could be done in a better way, or perhaps there is better approach?

    I wrote an article explaining how to prevent unescaping in url's at http://mariuszwojcik.wordpress.com/2014/03/04/how-to-prevent-invoke-restmethod-from-un-escaping-forward-slashes/
    the modules code is on GitHub: http://mariuszwojcik.wordpress.com/2014/03/04/how-to-prevent-invoke-restmethod-from-un-escaping-forward-slashes/
    Files related to the problem are: PreventUnEscapeDotsAndSlashesOnUri.ps1 and AddExchange.ps1.

    I much appreciate any input.

    Thanks a lot, Mariusz

  • #13536

    Dave Wyatt
    Moderator

    Assuming there's no other way of getting around this (perhaps by double-escaping the slashes or something), I'd probably write a proxy function for Invoke-RestMethod which temporarily sets that flag, then sets it back when finished. That would limit the impact to the single pipeline where Invoke-RestMethod is called. Basically the same thing you've described, except the person calling Invoke-RestMethod doesn't have to do the work; they can just call the function the same way they would call the cmdlet.

  • #13546

    Mariusz Wojcik
    Participant

    Brilliant idea Dave. That will much simplify the code in functions. I am thinking about adding new parameter to Invoke-RestMethod cmdlet called -AllowEscapedDotsAndSlashes, which when set will apply the hack to make call possible.

    Thanks a lot for the suggestion, very much appreciated. And I have learned something new again 🙂

  • #13611

    Mariusz Wojcik
    Participant

    For anyone interested, the proxy function over Invoke-RestMethod which allows for escaped dots and slashes: http://mariuszwojcik.wordpress.com/2014/03/06/invoke-restmethod-cmdlet-proxy-more-on-un-escaping-forward-slashes-in-uris/

    Thanks again, Dave

You must be logged in to reply to this topic.