Skip to content

Commit

Permalink
Filter request.spec to show only exposed schemas
Browse files Browse the repository at this point in the history
TODO also filter private junction tables
  • Loading branch information
steve-chavez committed Apr 17, 2021
1 parent 979018d commit ee13150
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/PostgREST/DbStructure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ These queries are executed once at startup or when PostgREST is reloaded.
module PostgREST.DbStructure
( DbStructure(..)
, getDbStructure
, removeInternalSchemas
, accessibleTables
, accessibleProcs
, schemaDescription
Expand Down Expand Up @@ -80,6 +81,19 @@ tableCols dbs tSchema tName = filter (\Column{colTable=Table{tableSchema=s, tabl
tablePKCols :: DbStructure -> Schema -> TableName -> [Text]
tablePKCols dbs tSchema tName = pkName <$> filter (\pk -> tSchema == (tableSchema . pkTable) pk && tName == (tableName . pkTable) pk) (dbPrimaryKeys dbs)

-- | Remove internal schemas(not exposed through the API) from the DbStructure
removeInternalSchemas :: DbStructure -> [Schema] -> DbStructure
removeInternalSchemas dbStruct schemas =
DbStructure {
dbTables = filter (\x -> tableSchema x `elem` schemas) $ dbTables dbStruct
, dbColumns = filter (\x -> tableSchema (colTable x) `elem` schemas) $ dbColumns dbStruct
, dbRelations = filter (\x -> tableSchema (relTable x) `elem` schemas &&
tableSchema (relFTable x) `elem` schemas) $ dbRelations dbStruct
, dbPrimaryKeys = filter (\x -> tableSchema (pkTable x) `elem` schemas) $ dbPrimaryKeys dbStruct
, dbProcs = M.filterWithKey (\(QualifiedIdentifier sch _) _ -> sch `elem` schemas) $ dbProcs dbStruct
, pgVersion = pgVersion dbStruct
}

-- | The source table column a view column refers to
type SourceColumn = (Column, ViewColumn)
type ViewColumn = Column
Expand Down
7 changes: 5 additions & 2 deletions src/PostgREST/Middleware.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import System.IO.Unsafe (unsafePerformIO)
import System.Log.FastLogger (toLogStr)

import PostgREST.Config (AppConfig (..), LogLevel (..))
import PostgREST.DbStructure (DbStructure)
import PostgREST.DbStructure (DbStructure,
removeInternalSchemas)
import PostgREST.Error (Error, errorResponseFor)
import PostgREST.GucHeader (addHeadersIfNotIncluded)
import PostgREST.Query.SqlFragment (fromQi, intercalateSnippet,
Expand Down Expand Up @@ -78,7 +79,8 @@ runPgLocals conf claims app req dbs = do
setConfigLocal mempty ("search_path", toS schemas)
preReqSql = (\f -> "select " <> fromQi f <> "();") <$> configDbPreRequest conf
specSql = case iTarget req of
TargetProc{tpIsRootSpec=True} -> [setConfigLocal mempty ("request.spec", toS $ JSON.encode dbs)]
TargetProc{tpIsRootSpec=True} -> [setConfigLocal mempty
("request.spec", toS $ JSON.encode $ removeInternalSchemas dbs $ toList $ configDbSchemas conf)]
_ -> mempty
-- | Do a pg set_config(setting, value, true) call. This is equivalent to a SET LOCAL.
setConfigLocal :: ByteString -> (ByteString, ByteString) -> H.Snippet
Expand Down Expand Up @@ -183,3 +185,4 @@ optionalRollback AppConfig{..} ApiRequest{..} transaction = do
[(HTTP.hPreferenceApplied, BS.pack (show Rollback))]
| otherwise =
identity

0 comments on commit ee13150

Please sign in to comment.