Skip to content

Commit

Permalink
Sig check only considers acquired (and pending) caps
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuart Popejoy committed Oct 25, 2019
1 parent df45f1c commit a0a7a83
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Pact/Repl/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ expectFail i as = case as of
TLitString msg -> do
r <- catch (Right <$> reduce expr) (\(e :: SomeException) -> return $ Left (show e))
case r of
Right v -> tfailure msg $ "expected failure, got result = " <> pack (show v)
Right v -> tfailure msg $ "expected failure, got result = " <> pack (showPretty v)
Left e -> case errM of
Nothing -> tsuccess msg
Just err | err `isInfixOf` e -> tsuccess msg
Expand Down Expand Up @@ -590,7 +590,7 @@ setGasModel _ as = do
testCapability :: ZNativeFun ReplState
testCapability i [ (TApp app _) ] = do
(_,d,_) <- appToCap app
let (scope,verb) = maybe (CapCallStack,"aquired") (const (CapManaged,"installed")) (_dDefMeta d)
let (scope,verb) = maybe (CapCallStack,"acquired") (const (CapManaged,"installed")) (_dDefMeta d)
r <- evalCap i scope False $ app
return . tStr $ case r of
AlreadyAcquired -> "Capability already " <> verb
Expand Down
8 changes: 1 addition & 7 deletions src/Pact/Runtime/Capabilities.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ type ApplyMgrFun e = Def Ref -> PactValue -> PactValue -> Eval e PactValue
noopApplyMgrFun :: ApplyMgrFun e
noopApplyMgrFun _ mgd _ = return mgd

-- | Get any cap that is currently granted, of any scope.
grantedCaps :: Eval e (S.Set UserCapability)
grantedCaps = S.union <$> getAllStackCaps <*> getAllManaged
where
getAllManaged = S.fromList . concatMap toList <$> use (evalCapabilities . capManaged)

-- | Check for acquired/stack (or composed therein) capability.
capabilityAcquired :: UserCapability -> Eval e Bool
capabilityAcquired cap = elem cap <$> getAllStackCaps
Expand Down Expand Up @@ -197,7 +191,7 @@ checkSigCaps
checkSigCaps sigs = go
where
go = do
granted <- grantedCaps
granted <- getAllStackCaps
return $ M.filter (match granted) sigs

match granted sigCaps =
Expand Down
28 changes: 28 additions & 0 deletions tests/pact/keysets.repl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@

(defun run-AB ()
(with-capability (AB) true))

(defcap M (a:integer) @managed a M_mgr
(enforce-keyset KEYSET_A))

(defun M_mgr (m r) m)

(defcap O ()
(enforce-keyset KEYSET_A))

)

(env-keys ["a"])
Expand All @@ -173,3 +182,22 @@
(run-A "bar" true))
(expect "AB succeeds with a in scope" true
(run-AB))

(env-sigs [{ 'key: "a", 'caps: [(M 1)] }])
;; env-sigs does NOT install managed caps like Intepreter does
(expect
"Managed install works with scoped keyset"
"Capability installed"
(test-capability (M 1)))

(expect-failure
"O not in scope, managed M shouldn't allow it to pass"
"Keyset failure"
(test-capability (O)))

(env-sigs [{ 'key: "a", 'caps: [(M 1),(O)] }])

(expect
"Scoped acquire of O succeeds"
"Capability acquired"
(test-capability (O)))

0 comments on commit a0a7a83

Please sign in to comment.