Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Saferish strings in Haskell APIs

Saferish strings in Haskell APIs

An introduction safer-ish strings in Haskell library APIs using OverloadedStrings and rewrite rules.

This was presented at the Open Programming Miniconf at linux.conf.au 2013.

Thomas Sutton

January 22, 2013
Tweet

More Decks by Thomas Sutton

Other Decks in Programming

Transcript

  1. So what? • Use custom datatypes instead of String at

    API boundaries. • If SQL commands aren’t strings, no string concatenation and less risk of injection vulnerabilities!
  2. mysql-simple • Database access library for MySQL written by Bryan

    O'Sullivan. • Uses this pattern to pass query text to the API.
  3. BUT! • All of the code which makes this work

    is public and you can import it. • So you (and any other developer) can circumvent this safety pretty trivially.
  4. How does this magic work? • Through the magic of

    compiler rules! • Allow library authors to specify rewrite rules to be applied during compilation. • Used for fusion and other optimisation techniques for data structure libraries. (text is very, very fast).
  5. String “Literals” • GHC generates a packed C-style string in

    the executable. • Code like this: ! • compiles to something like this:
  6. By default, it’s unsafe... It turns out that RULES do

    type inference. So we can write a RULE which replaces our fromStrings with out fromStringUnsafe.
  7. Identify and replace safe calls • Recall RULES use type

    inference. • And that the only safe calls are ones with static values. • And that static values have a specific type. • And are processed (before our code) with runtime support functions.
  8. What’s next? • Make it less fragile. (Needs NOINLINE pragmas

    and is sensitive to optimisation). • Make the error compile time. (But it needs to be typesafe to work. #lolwut) • Probably much better and easier to write a GHC plugin.