File: SafeOutput.hs

package info (click to toggle)
git-repair 1.20230814-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704 kB
  • sloc: haskell: 6,890; makefile: 39; sh: 9
file content (36 lines) | stat: -rw-r--r-- 831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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