-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add session command data and enforce-session
builtin
#1171
Open
imalsogreg
wants to merge
26
commits into
master
Choose a base branch
from
greg/upstream-enforce-session
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
2dee0a2
Add enforceKeysetSession to Eval.hs
imalsogreg a74509f
Add enforce-session builtin
imalsogreg 625a50a
add repl test
imalsogreg 9d8e9b5
add sessionsigner to setupevalenv
imalsogreg 31a425b
fixup
imalsogreg 1e29182
export
imalsogreg 05ae94e
Thread session-signer through payload and update tests
imalsogreg bfe2ad1
Add enforce-session to gas tests and gas golden
imalsogreg 6fb10da
Update hashed payloads in continuation tests
imalsogreg 6b228f6
Remove sessionSigner from the pact payload
imalsogreg bb25cd3
wip move sessionSigner into CommandEnv
imalsogreg 452198e
Pass sessionPubkey into the environment through the Command
imalsogreg 911298a
Remove sessionPubkey from ApiReq
imalsogreg 6434715
tests fixup
imalsogreg 024173e
Revert "Update hashed payloads in continuation tests"
imalsogreg a1926d2
Remove sessionPubkey from command too
imalsogreg 3a5cc70
fix cap filtering in sessionPubKey check
imalsogreg 1ee475e
cleanup
imalsogreg a810a2a
more cleanup
imalsogreg 125ae0c
fixup
imalsogreg 6184201
Update src/Pact/Native/Session.hs
imalsogreg a33ab26
Update src-ghc/Pact/GasModel/GasTests.hs
imalsogreg 6173b6e
Update src/Pact/Native/Session.hs
imalsogreg b55bd9f
Update src-ghc/Pact/GasModel/GasTests.hs
imalsogreg 35d7f4e
cleanup enforceKeysetSession
imalsogreg 40d27ff
Update src/Pact/Native/Session.hs
imalsogreg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ module Pact.Eval | |
,acquireModuleAdmin | ||
,computeUserAppGas,prepareUserAppArgs,evalUserAppBody | ||
,evalByName | ||
,enforceKeySetSession | ||
,resumePact | ||
,enforcePactValue,enforcePactValue' | ||
,toPersistDirect | ||
|
@@ -146,6 +147,46 @@ enforceKeySet i ksn KeySet{..} = do | |
| otherwise = failed | ||
{-# INLINE enforceKeySet #-} | ||
|
||
-- | Enforce keyset against session key from the environment. | ||
-- This is very similar to `enforceKeyset` (and both could be implemented | ||
-- in terms of a common function), but since `enforceKeyset` is such a central | ||
-- piece of code, we define `enforceKeySetSession` separately for now, and | ||
-- don't modify `enforceKeyset`. | ||
enforceKeySetSession :: PureSysOnly e => Info -> Maybe KeySetName -> KeySet -> Eval e () | ||
enforceKeySetSession i ksn KeySet{..} = do | ||
sessionPubKey <- view eeSessionSig | ||
case sessionPubKey of | ||
Nothing -> error "enforce-session called while there is no session pubkey in the environment" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to error this hard instead of calling |
||
Just (publicKeyText, caps) -> do | ||
let matchingKeys = | ||
if publicKeyText `elem` _ksKeys | ||
then M.singleton publicKeyText caps | ||
else mempty | ||
sigs' <- checkSigCaps matchingKeys | ||
runPred (M.size sigs') | ||
where | ||
failed = failTx i $ "Keyset failure " <> parens (pretty _ksPredFun) <> ": " <> | ||
maybe (pretty $ map (elide . asString) $ toList _ksKeys) pretty ksn | ||
atLeast t m = m >= t | ||
elide pk | T.length pk < 8 = pk | ||
| otherwise = T.take 8 pk <> "..." | ||
count = length _ksKeys | ||
runPred matched = | ||
case M.lookup _ksPredFun keyPredBuiltins of | ||
Just KeysAll -> runBuiltIn (\c m -> atLeast c m) | ||
Just KeysAny -> runBuiltIn (\_ m -> atLeast 1 m) | ||
Just Keys2 -> runBuiltIn (\_ m -> atLeast 2 m) | ||
Nothing -> do | ||
r <- evalByName _ksPredFun [toTerm count,toTerm matched] i | ||
case r of | ||
(TLiteral (LBool b) _) | b -> return () | ||
| otherwise -> failed | ||
_ -> evalError i $ "Invalid response from keyset predicate: " <> pretty r | ||
where | ||
runBuiltIn p | p count matched = return () | ||
| otherwise = failed | ||
{-# INLINE enforceKeySetSession #-} | ||
|
||
enforceGuard :: HasInfo i => i -> Guard (Term Name) -> Eval e () | ||
enforceGuard i g = case g of | ||
GKeySet k -> runSysOnly $ enforceKeySet (getInfo i) Nothing k | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
-- | | ||
-- Module : Pact.Native.Session | ||
-- Copyright : (C) 2016 Stuart Popejoy | ||
-- License : BSD-style (see the file LICENSE) | ||
-- Maintainer : Stuart Popejoy <[email protected]> | ||
-- | ||
-- Builtins for working with sessions. | ||
-- | ||
|
||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE PatternSynonyms #-} | ||
|
||
module Pact.Native.Session | ||
( sessionDefs | ||
, enforceSessionDef | ||
) where | ||
|
||
import Pact.Eval (enforceKeySetSession) | ||
import Pact.Native.Internal(NativeDef, NativeModule, defRNative, funType, tTyBool, tTyGuard, tTyString) | ||
import Pact.Types.KeySet (KeySetName(..), parseAnyKeysetName) | ||
import Pact.Types.Native (RNativeFun) | ||
import Pact.Types.Pretty (pretty) | ||
import Pact.Types.Purity (PureSysOnly, runSysOnly) | ||
import Pact.Types.Runtime (getInfo, evalError, evalError', ifExecutionFlagSet, ExecutionFlag(FlagDisablePact44), readRow, Domain(KeySets), argsError) | ||
import Pact.Types.Term (Example(LitExample), Guard(GKeySet, GKeySetRef), pattern TLitString, Term(TGuard), _tGuard, toTerm) | ||
import Pact.Types.Type (GuardType(GTyKeySet)) | ||
|
||
sessionDefs :: NativeModule | ||
sessionDefs = | ||
("Session",[enforceSessionDef]) | ||
|
||
enforceSessionDef :: NativeDef | ||
enforceSessionDef = | ||
defRNative "enforce-session" (\i as -> runSysOnly $ enforceSession' i as) | ||
(funType tTyBool [("keyset", tTyGuard (Just GTyKeySet))] | ||
<> funType tTyBool [("keysetname",tTyString)] | ||
) | ||
[LitExample "(enforce-session keyset)"] | ||
"Enforce that the current environment contains a session pubkey \ | ||
\that satisfies the keyset parameter. The execution environment is \ | ||
\responsible for setting the session pubkey, usually in response to an \ | ||
\authorization flow." | ||
where | ||
|
||
lookupEnvironmentKeyset i keySetName = do | ||
readRow (getInfo i) KeySets keySetName >>= \case | ||
Nothing -> evalError (getInfo i) $ "No such keyset: " <> pretty keySetName | ||
Just keySet -> pure keySet | ||
|
||
enforceSession' :: PureSysOnly e => RNativeFun e | ||
enforceSession' i [TGuard{_tGuard}] = case _tGuard of | ||
GKeySetRef (ksr) -> do | ||
ks <- lookupEnvironmentKeyset i ksr | ||
enforceKeySetSession (getInfo i) Nothing ks | ||
return (toTerm True) | ||
GKeySet ks -> do | ||
enforceKeySetSession (getInfo i) Nothing ks | ||
return (toTerm True) | ||
_ -> evalError' i "incorrect guard type, must be keyset ref or keyset" | ||
enforceSession' i [TLitString k] = do | ||
keySetName <- ifExecutionFlagSet FlagDisablePact44 | ||
(pure $ KeySetName k Nothing) | ||
(case parseAnyKeysetName k of | ||
Left{} -> evalError' i "incorrect keyset name format" | ||
Right ksn -> return ksn | ||
) | ||
ks <- readRow (getInfo i) KeySets keySetName >>= \case | ||
Nothing -> evalError (getInfo i) $ "No such keyset: " <> pretty keySetName | ||
Just ks -> pure ks | ||
enforceKeySetSession (getInfo i) (Just keySetName) ks | ||
return (toTerm True) | ||
|
||
enforceSession' i as = argsError i as |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the syntax for a cap here? Can we get a more complex example?