diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2024-03-19 19:47:48 +0800 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2024-03-19 19:47:48 +0800 |
commit | 4b47032686c52de5bf2324c1b1d4151c5fd6c41b (patch) | |
tree | 85098e25c0a4ad383f267fd59d591346d5e09a17 /Utility/SafeOutput.hs | |
parent | 34f99f9355c275917ff91539bbdac60a56ad7e17 (diff) | |
parent | 4993eab4a4507d52037ff74c67f6ca04d2401b5e (diff) | |
download | git-repair-4b47032686c52de5bf2324c1b1d4151c5fd6c41b.tar.gz |
Merge tag '1.20230814'
tagging package git-repair version 1.20230814
Diffstat (limited to 'Utility/SafeOutput.hs')
-rw-r--r-- | Utility/SafeOutput.hs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Utility/SafeOutput.hs b/Utility/SafeOutput.hs new file mode 100644 index 0000000..d781386 --- /dev/null +++ b/Utility/SafeOutput.hs @@ -0,0 +1,36 @@ +{- Safe output to the terminal of possibly attacker-controlled strings, + - avoiding displaying control characters. + - + - Copyright 2023 Joey Hess <id@joeyh.name> + - + - License: BSD-2-clause + -} + +{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, CPP #-} +{-# OPTIONS_GHC -fno-warn-tabs #-} + +module Utility.SafeOutput ( + safeOutput, + safeOutputChar, +) where + +import Data.Char +import qualified Data.ByteString as S + +class SafeOutputtable t where + safeOutput :: t -> t + +instance SafeOutputtable String where + safeOutput = filter safeOutputChar + +instance SafeOutputtable S.ByteString where + safeOutput = S.filter (safeOutputChar . chr . fromIntegral) + +safeOutputChar :: Char -> Bool +safeOutputChar c + | not (isControl c) = True + | c == '\n' = True + | c == '\t' = True + | c == '\DEL' = False + | ord c > 31 = True + | otherwise = False |