SlideShare a Scribd company logo
Designing with capabilities
for fun and profit
@ScottWlaschin
fsharpforfunandprofit.com/cap
Designing with Capabilities
Good security => Complicated
I won’t like doing this
Good security => Complicated
X
Security for free!
...which is good,
because I’m lazy
Good security == Good design
I like doing this
The topic of this talk
Not about OAuth, JWT etc
Good security == Good design
Transparent
Opaque
It’s all about
security, right?
Sed ut perspiciatis unde omnis iste natus error sit voluptatem
accusantium doloremque laudantium, totam rem aperiam,
eaque ipsa quae ab illo inventore veritatis et quasi architecto
beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem
quia voluptas sit aspernatur aut odit aut fugit, sed quia
consequuntur magni dolores eos qui ratione voluptatem sequi
nesciunt. Neque porro quisquam est, qui dolorem ipsum quia
dolor sit amet, consectetur, adipisci velit, sed quia non
numquam eius modi tempora incidunt ut labore et dolore
magnam aliquam quaerat voluptatem. Ut enim ad minima
veniam, quis nostrum exercitationem ullam corporis suscipit
laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis
autem vel eum iure reprehenderit qui in ea voluptate velit esse
quam nihil molestiae consequatur, Temporibus autem quibus
Dacei Megasystems Tech Inc necessitatibust aut officiis debitis
auteo 2799 E Dragam Suite 7 quisquam saepe Itaque
enieti Los Angeles CA 90002 ut et voluptates repudiandae sint
et molestiae non recusandae. Itaque earum rerum hic tenetur a
sapiente delectus, ut aut reiciendis voluptatibus maiores alias
consequatur aut perferendis doloribus asperiores repellat.
Neque porro quisquam est, qui dolorem ipsum quia dolor sit
amet, consectetur, adipisci velit, sed quia non numquam eius
modi tempora incidunt ut labore et dolore magnam aliquam
quaerat voluptatem. Ut enim ad minima veniam, quis nostrum
exercitationem ullam corporis suscipit laboriosam, nisi ut
aliquid ex ea commodi consequatur?
Please deliver
this letter
A counterexample
Sed ut perspiciatis unde omnis iste natus error sit voluptatem
accusantium doloremque laudantium, totam rem aperiam,
eaque ipsa quae ab illo inventore veritatis et quasi architecto
beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem
quia voluptas sit aspernatur aut odit aut fugit, sed quia
consequuntur magni dolores eos qui ratione voluptatem sequi
nesciunt. Neque porro quisquam est, qui dolorem ipsum quia
dolor sit amet, consectetur, adipisci velit, sed quia non
numquam eius modi tempora incidunt ut labore et dolore
magnam aliquam quaerat voluptatem. Ut enim ad minima
veniam, quis nostrum exercitationem ullam corporis suscipit
laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis
autem vel eum iure reprehenderit qui in ea voluptate velit esse
quam nihil molestiae consequatur, Temporibus autem quibus
Dacei Megasystems Tech Inc necessitatibust aut officiis debitis
auteo 2799 E Dragam Suite 7 quisquam saepe Itaque
enieti Los Angeles CA 90002 ut et voluptates repudiandae sint
et molestiae non recusandae. Itaque earum rerum hic tenetur a
sapiente delectus, ut aut reiciendis voluptatibus maiores alias
consequatur aut perferendis doloribus asperiores repellat.
Neque porro quisquam est, qui dolorem ipsum quia dolor sit
amet, consectetur, adipisci velit, sed quia non numquam eius
modi tempora incidunt ut labore et dolore magnam aliquam
quaerat voluptatem. Ut enim ad minima veniam, quis nostrum
exercitationem ullam corporis suscipit laboriosam, nisi ut
aliquid ex ea commodi consequatur?
Please deliver
this letter
It’s not just
about security...
...hiding irrelevant
information is
good design!
EVOLUTION OF AN API
Evolution of an API
(from the security point of view)
Say that the UI needs to set a
configuration option
(e.g. DontShowThisMessageAgain)
How can we stop a malicious caller doing bad things?
Version1
Give the caller the configuration file name
interface IConfiguration
{
string GetConfigFilename();
}
var filename = config.GetConfigFilename();
// open file
// write new config
// close file
API
Caller
 A malicious caller has the
ability to open and write to
any file on the filesystem
Version 2
Give the caller aTextWriter
interface IConfiguration
{
TextWriter GetConfigWriter();
}
var writer = config.GetConfigWriter();
// write new config
API
Caller
 A malicious caller can
corrupt the config file
We control which file is opened
Version 3
Give the caller a key/value interface
interface IConfiguration
{
void SetConfig(string key, string value);
}
config.SetConfig(
"DontShowThisMessageAgain", "True");
API
Caller
 A malicious caller can set
the value to a non-boolean
Version 4
Give the caller a domain-centric interface
enum MessageFlag {
ShowThisMessageAgain,
DontShowThisMessageAgain
}
interface IConfiguration
{
void SetMessageFlag(MessageFlag value);
void SetConnectionString(ConnectionString value);
void SetBackgroundColor(Color value);
}
API
 What's to stop a malicious caller
changing the connection string when they
were only supposed to set the flag?
Version 5
Give the caller only the interface they need
interface IWarningMessageConfiguration
{
void SetMessageFlag(MessageFlag value);
}
API
 The caller can *only* do the thing we
allow them to do.
Can’t get your
work done
Too much information passed in
Just right Potential for
abuse
Principle of Least Authority
(POLA)
Too little information passed in
Security spectrum
Evolution of the same API
(from the design point of view)
DesignVersion1
Give the caller the configuration file name
interface IConfiguration
{
string GetConfigFilename();
}
API
Bad design: Using a filename means we limit ourselves
to file-based config files.
Better design: A TextWriter would make the design
more mockable.
DesignVersion 2
Give the caller aTextWriter
interface IConfiguration
{
TextWriter GetConfigWriter();
}
API
Better design: A generic KeyValue store would make
implementation choices more flexible.
Bad design: Using a TextWriter means exposing a
specific storage format
DesignVersion 3
Give the caller a key/value interface
interface IConfiguration
{
void SetConfig(string key, string value);
}
API
Bad design: A KeyValue store using strings means
possible bugs. So we need to write validation and
tests for that 
Better design: A statically typed interface
means no corruption checking code. 
DesignVersion 4
Give the caller a domain-centric interface
enum MessageFlag {
ShowThisMessageAgain,
DontShowThisMessageAgain
}
interface IConfiguration
{
void SetMessageFlag(MessageFlag value);
void SetConnectionString(ConnectionString value);
void SetBackgroundColor(Color value);
}
API
Bad design: An interface with too many methods violates the ISP.
Better design: Reduce the number of available methods to one!
DesignVersion 5
Give the caller only the interface they need
interface IWarningMessageConfiguration
{
void SetMessageFlag(MessageFlag value);
}
API
Good design: The caller has no dependencies
on anything else. Bonus: easy to mock! 
Can’t do
anything
Too many dependencies
Just right Unnecessary
coupling
Interface Segregation Principle
Too few dependencies
Design spectrum
Ak.a. Minimize your surface area
(to reduce chance of abuse)
OO Design Guidelines
Interface Segregation Principle
Single Responsibility Principle, etc
Security Guidelines
Principle of Least Authority (POLA)
Ak.a. Minimize your surface area
(to reduce coupling, dependencies, etc)
Good security => Good design
Good design => Good security
INTRODUCING
“CAPABILITIES”
Capability based design
‱ In a cap-based design, the caller can only do
exactly one thing -- a "capability".
‱ In the example, the caller has a capability to
set the message flag, and that's all.
Prevents both maliciousness and stupidity!
A one method interface is a capability
interface IWarningMessageConfiguration
{
void SetMessageFlag(MessageFlag value);
}
A one method interface is a function
interface IWarningMessageConfiguration
{
void SetMessageFlag(MessageFlag value);
}
Action<MessageFlag> messageFlagCapability
X
Capability-based security
in the real world
Designing with Capabilities
Designing with Capabilities
What does “access-control” mean?
‱ Preventing any access at all.
‱ Limiting access to some things only.
‱ Revoking access when you are no longer
allowed.
‱ Granting and delegating access to some
subset of things.
It’s not always
about saying no!
Using capabilities to
control access to a resource
Secret
Files
Alice
Bob
X
Alternative for access control
Using auth/RBAC to
control access to a resource
Secret
Files
Alice
Bob
rejected
accepted
A weakness in capabilities
Secret
Files
Alice
Bob

Anyone with the
key can get access.
A weakness in auth/RBAC
Secret
Files
Alice
Bob

A weakness in any security system!
Secret
Files
Alice
Bob
Do you trust Alice?
Then you also trust anyone
whom Alice trusts too...
(Or whom Alice can be tricked into trusting)
“Don’t prohibit what
you can’t prevent”
Supplies
Closet
Alice
Bob
Secret
Files
X
Capabilities support
decentralized delegation
Supplies
Closet
Auth/RBAC systems are
centralized
Alice
Bob accepted
rejected
Bob has only been
working there for
6 months 
Auth/RBAC systems are
centralized
Alice
“Please grant Bob access
to the supplies closet”
“Then revoke access
after 20 minutes”
Authorization
System
Can your system do this?
Supplies
Closet
Auth/RBAC systems are
centralized
Alice
Bob
“Here, Bob. You can
borrow my swipe card”
Credential Sharing
Central systems can revoke access
Secret
Files
Alice
rejected
How to revoke access in a cap-based system?
Revoke?
How to revoke access in a cap-based system?
Demo: Capabilities as functions
DESIGNING AN
API USING CAPABILITIES
Client Service
Request
Response
Tic-Tac-Toe as a service
Proper name is "Noughts and Crosses" btw
Tic-Tac-Toe API (obvious version)
type TicTacToeRequest = {
player: Player
row: Row
col: Column
}
Tic-Tac-Toe API (obvious version)
type TicTacToeResponse = {
result : MoveResult
display: DisplayInfo
}
type MoveResult =
| KeepPlaying
| GameWon of Player
| GameTied
Demo:Tic-Tac-Toe
What kind of errors can happen?
‱ A player can play an already played move
‱ A player can play twice in a row
‱ A player can forget to check the Result and
keep playing
“Make illegal operations
unrepresentable”
Don’t let me do a bad thing and
then tell me off for doing it...
Yes, you could return errors, but...
Client Service
New Game
Available Moves
Tic-Tac-Toe service with caps
Nine available
moves
Client Service
1st move
Available Moves
Tic-Tac-Toe service with caps
Eight available
moves
Client Service
Available Moves
Tic-Tac-Toe service with caps
2nd move
Seven available
moves
Client Service
Available Moves
Tic-Tac-Toe service with caps
3rd move
Six available
moves
Client Service
No available
Moves
Tic-Tac-Toe service with caps
Winning
move
Tic-Tac-Toe API (cap-based version)
type MoveCapability =
unit -> TicTacToeResponse
type NextMoveInfo = {
playerToPlay : Player
posToPlay : CellPosition
capability : MoveCapability
} These are for client
information only.
The player and
position are baked
into the capability
Tic-Tac-Toe API (cap-based version)
type TicTacToeResponse =
| KeepPlaying of
(DisplayInfo * NextMoveInfo list)
| GameWon of
(DisplayInfo * Player)
| GameTied of DisplayInfo
Tic-Tac-Toe API (cap-based version)
type TicTacToeResponse =
| KeepPlaying of
(DisplayInfo * NextMoveInfo list)
| GameWon of
(DisplayInfo * Player)
| GameTied of DisplayInfo
Where did the "request" type go?
Where’s the authorization?
Demo: Cap-based Api
What kind of errors can happen?
‱ A player can play an already played move
‱ A player can play twice in a row
‱ A player can forget to check the Result and
keep playing
Is this good security or good design?
All fixed now! 
HATEOAS
Hypermedia As The Engine
Of Application State
“A REST client needs no prior knowledge
about how to interact with any particular
application or server beyond a generic
understanding of hypermedia.”
RESTful done right
How NOT to do HATEOAS
POST /customers/
GET /customer/42
If you can guess the API
you’re doing it wrong
Security problem!
Also, a design problem –
too much coupling.
How to do HATEOAS
POST /81f2300b618137d21d /
GET /da3f93e69b98
You can only know what URIs
to use by parsing the page
Each of these URIs is a capability
Demo: HATEOAS
Some Benefits of HATEOAS
‱ Client decoupled from server
– The server owns the API model and can change it
without breaking any clients
– E.g. Change links to point to CDN
– E.g. Versioning
‱ Simpler clients in many cases
– No need for client-side checking of moves
‱ Explorable API
– Choose your own adventure!
Client Service
Login
Available
Capabilities
Service API with caps
Many available
actions initially
Client Service
Use a
capability
Available
Capabilities
Service API with caps
Fewer available
actions in a
given state
CAPABILITY DRIVEN DESIGN
Using these design techniques throughout your domain
Controller/
API
Business
Logic
Database
Client Where shall we put the
authorization logic?
But then any other path
has complete access to
the database 
Controller/
API
Business
Logic
Database
Client Where shall we put the
authorization logic?
But then it doesn’t have
enough context 
Controller/
API
Business
Logic
Database
Client Where shall we put the
authorization logic?
Global
Authorizer
Are you doing this already?
public class CustomerController : ApiController
{
readonly ICustomerDb _db;
public CustomerController(ICustomerDb db)
{
_db = db;
}
[Route("customers/{customerId}")]
[HttpGet]
[GetCustomerProfileAuth]
public IHttpActionResult Get(int customerId)
{
var custId = new CustomerId(customerId);
var cust = _db.GetProfile(custId);
var dto = DtoConverter.CustomerToDto(cust);
return Ok(dto);
}
public interface ICustomerDb
{
CustomerProfile GetProfile(CustomerId id);
void UpdateProfile(CustomerId id, CustomerProfile cust);
void CreateAccount(CustomerId id, CustomerProfile cust);
void DeleteAccount(CustomerId id);
void UpdateLoginEmail(CustomerId id, string email);
void UpdatePassword(CustomerId id, string password);
}
How much authority do you really need?
Controller
/API
Business
Logic
Database
Use Case
Controller
/API
Business
Logic
Database
Use Case
Controller
/API
Business
Logic
Database
Use Case
Global
Authorizer
Every
controller
has one
method
Controller
/API
Business
Logic
Database
Use Case
Controller
/API
Business
Logic
Database
Use Case
Controller
/API
Business
Logic
Database
Use Case
Global
Authorizer
Vertical
slices
Don’t mention
microservices
Controller
/API
Business
Logic
Database
Use Case
Global
Authorizer
Services
Delegation of capabilities
Delegation with restriction
type MessageFlag =
ShowThisMessageAgain | DontShowThisMessageAgain
type ConfigurationCapabilities = {
GetMessageFlag : unit -> MessageFlag
SetMessageFlag : MessageFlag -> unit
GetBackgroundColor : unit -> Color
SetBackgroundColor : Color -> unit
GetConnectionString : unit -> ConnectionString
SetConnectionString : ConnectionString -> unit
}
◘
let dontShowMessageAgainDialogBox capabilities =
let getFlag,setFlag = capabilities
let ctrl= new CheckBox(
Text="Don't show this message again")
ctrl.Checked <- getFlag()
// etc
Demo:
Vertical slices and delegation
Controller
/API
Business
Logic
Database
Global
Authorizer
Passing capabilities
Controller
/API
Business
Logic
Database
Global
Authorizer
Passing tokens
Overkill?
Too complicated
to implement?
Controller
/API
Business
Logic
Database
Inject a value
of specific type
Global
Authorizer
Passing tokens as types
Represent tokens
by types!
Demo:
Using types for access tokens
What have we covered?
‱ Don’t use complicated security patterns
– This will ensure that they are never used, or used
wrong.
– Don’t rely on other people doing the right thing.
– Don’t rely on other people reading the
documentation!
‱ Do use techniques where you get security for
free.
– You can be lazy!
– You don’t have to remember to do anything.
What have we covered?
‱ Don’t develop first, add security later
– Right after you implement the “quality” module
‱ Do use security-driven design
– Bonus: get a modular architecture!
What have we covered?
‱ Don’t only do security at the outermost layer
‱ Do use POLA everywhere, which ensures that
you have minimal dependencies.
Common questions
‱ How do you pass these capabilities around?
– Dependency injection or equivalent
‱ Are you saying that all external IO should be
passed around as capabilities?
– Yes!You should never access any ambient
authority.
– You should be doing this anyway for mocking.
Common questions
‱ Won’t there be too many parameters?
– Less than you think!
– Encourages vertical slices (per use-case, scenario)
– “Functional core, imperative shell”
‱ Can’t this be bypassed by reflection or other
backdoors?
– Yes. This is really all about design not about total
security.
Resources for capability-based thinking
‱ LMGTFY “Capability based security”
‱ “Lazy developers guide to secure computing”
talk by Marc Stiegler
‱ erights.org
‱ Google's Caja built over JavaScript
‱ Emily, a capability based language (via Ocaml)
Thanks!
@ScottWlaschin
fsharpforfunandprofit.com/cap
Contact me
Slides and video here
Let us know if you
need help with F#

More Related Content

PDF
Redux Sagas - React Alicante
PDF
Railway Oriented Programming
PDF
The Functional Programming Toolkit (NDC Oslo 2019)
PPTX
Javascript Prototypal Inheritance - Big Picture
PDF
Pipeline oriented programming
PDF
An Introduction to Unit Test Using NUnit
PPSX
Javascript variables and datatypes
PDF
Lecture 8 Enterprise Java Beans (EJB)
Redux Sagas - React Alicante
Railway Oriented Programming
The Functional Programming Toolkit (NDC Oslo 2019)
Javascript Prototypal Inheritance - Big Picture
Pipeline oriented programming
An Introduction to Unit Test Using NUnit
Javascript variables and datatypes
Lecture 8 Enterprise Java Beans (EJB)

What's hot (20)

PDF
Domain Driven Design with the F# type System -- NDC London 2013
PDF
Functional Design Patterns (DevTernity 2018)
PPT
JavaScript & Dom Manipulation
PPTX
Why TypeScript?
 
PDF
Domain Modeling with FP (DDD Europe 2020)
PPTX
Lab #2: Introduction to Javascript
PDF
Introduction to JSON
ODP
Datatype in JavaScript
PDF
JavaScript Fetch API
PDF
Java 8, Streams & Collectors, patterns, performances and parallelization
PDF
The Power of Composition (NDC Oslo 2020)
PDF
Git tutorial
PDF
Introducing Playwright's New Test Runner
PPTX
JavaScript Promises
PDF
Unit testing with JUnit
PDF
Advanced javascript
PDF
In-Depth Model/View with QML
 
PDF
NEXT.JS
PDF
The lazy programmer's guide to writing thousands of tests
PDF
Web 5 | JavaScript Events
Domain Driven Design with the F# type System -- NDC London 2013
Functional Design Patterns (DevTernity 2018)
JavaScript & Dom Manipulation
Why TypeScript?
 
Domain Modeling with FP (DDD Europe 2020)
Lab #2: Introduction to Javascript
Introduction to JSON
Datatype in JavaScript
JavaScript Fetch API
Java 8, Streams & Collectors, patterns, performances and parallelization
The Power of Composition (NDC Oslo 2020)
Git tutorial
Introducing Playwright's New Test Runner
JavaScript Promises
Unit testing with JUnit
Advanced javascript
In-Depth Model/View with QML
 
NEXT.JS
The lazy programmer's guide to writing thousands of tests
Web 5 | JavaScript Events
Ad

Similar to Designing with Capabilities (20)

PDF
Designing with capabilities (DDD-EU 2017)
PPTX
Designing Flexibility in Software to Increase Security
PPT
Intro to-ssdl--lone-star-php-2013
PPTX
Integrating security into Continuous Delivery
PPTX
Security Design Concepts
PDF
AppSec in an Agile World
PPTX
Design Principles
PDF
Streamlining AppSec Policy Definition.pptx
PDF
An Introduction to Secure Application Development
PPTX
Ethical Hacking Conference 2015- Building Secure Products -a perspective
PDF
Secure .NET programming
PPT
Software Security in the Real World
PPTX
00. introduction to app sec v3
PPTX
BASC presentation on security and application architecture
PDF
04812167
PPTX
Ryan Elkins - Simple Security Defense to Thwart an Army of Cyber Ninja Warriors
PPTX
Information Security and the SDLC
PDF
Privileged Access Control & Task Automation: A Win Double of Security and Bus...
PPTX
2009 Dotnet Information Day: More effective c#
PDF
ProdSec: A Technical Approach
Designing with capabilities (DDD-EU 2017)
Designing Flexibility in Software to Increase Security
Intro to-ssdl--lone-star-php-2013
Integrating security into Continuous Delivery
Security Design Concepts
AppSec in an Agile World
Design Principles
Streamlining AppSec Policy Definition.pptx
An Introduction to Secure Application Development
Ethical Hacking Conference 2015- Building Secure Products -a perspective
Secure .NET programming
Software Security in the Real World
00. introduction to app sec v3
BASC presentation on security and application architecture
04812167
Ryan Elkins - Simple Security Defense to Thwart an Army of Cyber Ninja Warriors
Information Security and the SDLC
Privileged Access Control & Task Automation: A Win Double of Security and Bus...
2009 Dotnet Information Day: More effective c#
ProdSec: A Technical Approach
Ad

More from Scott Wlaschin (20)

PDF
Domain Modeling Made Functional (DevTernity 2022)
PDF
Building confidence in concurrent code with a model checker: TLA+ for program...
PDF
Reinventing the Transaction Script (NDC London 2020)
PDF
The Functional Programmer's Toolkit (NDC London 2019)
PDF
The Power Of Composition (DotNext 2019)
PDF
Domain Modeling Made Functional (KanDDDinsky 2019)
PDF
Four Languages From Forty Years Ago (NewCrafts 2019)
PDF
Four Languages From Forty Years Ago
PDF
The Power of Composition
PDF
F# for C# Programmers
PDF
Thirteen ways of looking at a turtle
PDF
Dr Frankenfunctor and the Monadster
PDF
Enterprise Tic-Tac-Toe
PDF
An introduction to property based testing
PDF
Functional Programming Patterns (NDC London 2014)
PDF
Functional Programming Patterns (BuildStuff '14)
PDF
Swift vs. Language X
PDF
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
PDF
Doge-driven design
PDF
The Theory of Chains
Domain Modeling Made Functional (DevTernity 2022)
Building confidence in concurrent code with a model checker: TLA+ for program...
Reinventing the Transaction Script (NDC London 2020)
The Functional Programmer's Toolkit (NDC London 2019)
The Power Of Composition (DotNext 2019)
Domain Modeling Made Functional (KanDDDinsky 2019)
Four Languages From Forty Years Ago (NewCrafts 2019)
Four Languages From Forty Years Ago
The Power of Composition
F# for C# Programmers
Thirteen ways of looking at a turtle
Dr Frankenfunctor and the Monadster
Enterprise Tic-Tac-Toe
An introduction to property based testing
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (BuildStuff '14)
Swift vs. Language X
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
Doge-driven design
The Theory of Chains

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Transform Your Business with a Software ERP System
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
System and Network Administration Chapter 2
PDF
Nekopoi APK 2025 free lastest update
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Strategies for Manufacturing Companies
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
AI in Product Development-omnex systems
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
medical staffing services at VALiNTRY
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Softaken Excel to vCard Converter Software.pdf
top salesforce developer skills in 2025.pdf
Online Work Permit System for Fast Permit Processing
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Transform Your Business with a Software ERP System
Odoo Companies in India – Driving Business Transformation.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
System and Network Administration Chapter 2
Nekopoi APK 2025 free lastest update
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Strategies for Manufacturing Companies
Which alternative to Crystal Reports is best for small or large businesses.pdf
Odoo POS Development Services by CandidRoot Solutions
AI in Product Development-omnex systems
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
medical staffing services at VALiNTRY
CHAPTER 2 - PM Management and IT Context
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Softaken Excel to vCard Converter Software.pdf

Designing with Capabilities

  • 1. Designing with capabilities for fun and profit @ScottWlaschin fsharpforfunandprofit.com/cap
  • 3. Good security => Complicated I won’t like doing this
  • 4. Good security => Complicated X
  • 5. Security for free! ...which is good, because I’m lazy Good security == Good design I like doing this
  • 6. The topic of this talk Not about OAuth, JWT etc Good security == Good design
  • 8. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, Temporibus autem quibus Dacei Megasystems Tech Inc necessitatibust aut officiis debitis auteo 2799 E Dragam Suite 7 quisquam saepe Itaque enieti Los Angeles CA 90002 ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Please deliver this letter A counterexample
  • 9. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, Temporibus autem quibus Dacei Megasystems Tech Inc necessitatibust aut officiis debitis auteo 2799 E Dragam Suite 7 quisquam saepe Itaque enieti Los Angeles CA 90002 ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Please deliver this letter It’s not just about security... ...hiding irrelevant information is good design!
  • 11. Evolution of an API (from the security point of view) Say that the UI needs to set a configuration option (e.g. DontShowThisMessageAgain) How can we stop a malicious caller doing bad things?
  • 12. Version1 Give the caller the configuration file name interface IConfiguration { string GetConfigFilename(); } var filename = config.GetConfigFilename(); // open file // write new config // close file API Caller  A malicious caller has the ability to open and write to any file on the filesystem
  • 13. Version 2 Give the caller aTextWriter interface IConfiguration { TextWriter GetConfigWriter(); } var writer = config.GetConfigWriter(); // write new config API Caller  A malicious caller can corrupt the config file We control which file is opened
  • 14. Version 3 Give the caller a key/value interface interface IConfiguration { void SetConfig(string key, string value); } config.SetConfig( "DontShowThisMessageAgain", "True"); API Caller  A malicious caller can set the value to a non-boolean
  • 15. Version 4 Give the caller a domain-centric interface enum MessageFlag { ShowThisMessageAgain, DontShowThisMessageAgain } interface IConfiguration { void SetMessageFlag(MessageFlag value); void SetConnectionString(ConnectionString value); void SetBackgroundColor(Color value); } API  What's to stop a malicious caller changing the connection string when they were only supposed to set the flag?
  • 16. Version 5 Give the caller only the interface they need interface IWarningMessageConfiguration { void SetMessageFlag(MessageFlag value); } API  The caller can *only* do the thing we allow them to do.
  • 17. Can’t get your work done Too much information passed in Just right Potential for abuse Principle of Least Authority (POLA) Too little information passed in Security spectrum
  • 18. Evolution of the same API (from the design point of view)
  • 19. DesignVersion1 Give the caller the configuration file name interface IConfiguration { string GetConfigFilename(); } API Bad design: Using a filename means we limit ourselves to file-based config files. Better design: A TextWriter would make the design more mockable.
  • 20. DesignVersion 2 Give the caller aTextWriter interface IConfiguration { TextWriter GetConfigWriter(); } API Better design: A generic KeyValue store would make implementation choices more flexible. Bad design: Using a TextWriter means exposing a specific storage format
  • 21. DesignVersion 3 Give the caller a key/value interface interface IConfiguration { void SetConfig(string key, string value); } API Bad design: A KeyValue store using strings means possible bugs. So we need to write validation and tests for that  Better design: A statically typed interface means no corruption checking code. 
  • 22. DesignVersion 4 Give the caller a domain-centric interface enum MessageFlag { ShowThisMessageAgain, DontShowThisMessageAgain } interface IConfiguration { void SetMessageFlag(MessageFlag value); void SetConnectionString(ConnectionString value); void SetBackgroundColor(Color value); } API Bad design: An interface with too many methods violates the ISP. Better design: Reduce the number of available methods to one!
  • 23. DesignVersion 5 Give the caller only the interface they need interface IWarningMessageConfiguration { void SetMessageFlag(MessageFlag value); } API Good design: The caller has no dependencies on anything else. Bonus: easy to mock! 
  • 24. Can’t do anything Too many dependencies Just right Unnecessary coupling Interface Segregation Principle Too few dependencies Design spectrum
  • 25. Ak.a. Minimize your surface area (to reduce chance of abuse) OO Design Guidelines Interface Segregation Principle Single Responsibility Principle, etc Security Guidelines Principle of Least Authority (POLA) Ak.a. Minimize your surface area (to reduce coupling, dependencies, etc)
  • 26. Good security => Good design Good design => Good security
  • 28. Capability based design ‱ In a cap-based design, the caller can only do exactly one thing -- a "capability". ‱ In the example, the caller has a capability to set the message flag, and that's all. Prevents both maliciousness and stupidity!
  • 29. A one method interface is a capability interface IWarningMessageConfiguration { void SetMessageFlag(MessageFlag value); }
  • 30. A one method interface is a function interface IWarningMessageConfiguration { void SetMessageFlag(MessageFlag value); } Action<MessageFlag> messageFlagCapability X
  • 34. What does “access-control” mean? ‱ Preventing any access at all. ‱ Limiting access to some things only. ‱ Revoking access when you are no longer allowed. ‱ Granting and delegating access to some subset of things. It’s not always about saying no!
  • 35. Using capabilities to control access to a resource Secret Files Alice Bob X
  • 37. Using auth/RBAC to control access to a resource Secret Files Alice Bob rejected accepted
  • 38. A weakness in capabilities Secret Files Alice Bob  Anyone with the key can get access.
  • 39. A weakness in auth/RBAC Secret Files Alice Bob 
  • 40. A weakness in any security system! Secret Files Alice Bob Do you trust Alice? Then you also trust anyone whom Alice trusts too... (Or whom Alice can be tricked into trusting)
  • 41. “Don’t prohibit what you can’t prevent”
  • 43. Supplies Closet Auth/RBAC systems are centralized Alice Bob accepted rejected Bob has only been working there for 6 months 
  • 44. Auth/RBAC systems are centralized Alice “Please grant Bob access to the supplies closet” “Then revoke access after 20 minutes” Authorization System Can your system do this?
  • 45. Supplies Closet Auth/RBAC systems are centralized Alice Bob “Here, Bob. You can borrow my swipe card” Credential Sharing
  • 46. Central systems can revoke access Secret Files Alice rejected
  • 47. How to revoke access in a cap-based system? Revoke?
  • 48. How to revoke access in a cap-based system?
  • 50. DESIGNING AN API USING CAPABILITIES
  • 51. Client Service Request Response Tic-Tac-Toe as a service Proper name is "Noughts and Crosses" btw
  • 52. Tic-Tac-Toe API (obvious version) type TicTacToeRequest = { player: Player row: Row col: Column }
  • 53. Tic-Tac-Toe API (obvious version) type TicTacToeResponse = { result : MoveResult display: DisplayInfo } type MoveResult = | KeepPlaying | GameWon of Player | GameTied
  • 55. What kind of errors can happen? ‱ A player can play an already played move ‱ A player can play twice in a row ‱ A player can forget to check the Result and keep playing
  • 56. “Make illegal operations unrepresentable” Don’t let me do a bad thing and then tell me off for doing it... Yes, you could return errors, but...
  • 57. Client Service New Game Available Moves Tic-Tac-Toe service with caps Nine available moves
  • 58. Client Service 1st move Available Moves Tic-Tac-Toe service with caps Eight available moves
  • 59. Client Service Available Moves Tic-Tac-Toe service with caps 2nd move Seven available moves
  • 60. Client Service Available Moves Tic-Tac-Toe service with caps 3rd move Six available moves
  • 61. Client Service No available Moves Tic-Tac-Toe service with caps Winning move
  • 62. Tic-Tac-Toe API (cap-based version) type MoveCapability = unit -> TicTacToeResponse type NextMoveInfo = { playerToPlay : Player posToPlay : CellPosition capability : MoveCapability } These are for client information only. The player and position are baked into the capability
  • 63. Tic-Tac-Toe API (cap-based version) type TicTacToeResponse = | KeepPlaying of (DisplayInfo * NextMoveInfo list) | GameWon of (DisplayInfo * Player) | GameTied of DisplayInfo
  • 64. Tic-Tac-Toe API (cap-based version) type TicTacToeResponse = | KeepPlaying of (DisplayInfo * NextMoveInfo list) | GameWon of (DisplayInfo * Player) | GameTied of DisplayInfo Where did the "request" type go? Where’s the authorization?
  • 66. What kind of errors can happen? ‱ A player can play an already played move ‱ A player can play twice in a row ‱ A player can forget to check the Result and keep playing Is this good security or good design? All fixed now! 
  • 67. HATEOAS Hypermedia As The Engine Of Application State “A REST client needs no prior knowledge about how to interact with any particular application or server beyond a generic understanding of hypermedia.” RESTful done right
  • 68. How NOT to do HATEOAS POST /customers/ GET /customer/42 If you can guess the API you’re doing it wrong Security problem! Also, a design problem – too much coupling.
  • 69. How to do HATEOAS POST /81f2300b618137d21d / GET /da3f93e69b98 You can only know what URIs to use by parsing the page Each of these URIs is a capability
  • 71. Some Benefits of HATEOAS ‱ Client decoupled from server – The server owns the API model and can change it without breaking any clients – E.g. Change links to point to CDN – E.g. Versioning ‱ Simpler clients in many cases – No need for client-side checking of moves ‱ Explorable API – Choose your own adventure!
  • 72. Client Service Login Available Capabilities Service API with caps Many available actions initially
  • 73. Client Service Use a capability Available Capabilities Service API with caps Fewer available actions in a given state
  • 74. CAPABILITY DRIVEN DESIGN Using these design techniques throughout your domain
  • 75. Controller/ API Business Logic Database Client Where shall we put the authorization logic? But then any other path has complete access to the database 
  • 76. Controller/ API Business Logic Database Client Where shall we put the authorization logic? But then it doesn’t have enough context 
  • 77. Controller/ API Business Logic Database Client Where shall we put the authorization logic? Global Authorizer Are you doing this already?
  • 78. public class CustomerController : ApiController { readonly ICustomerDb _db; public CustomerController(ICustomerDb db) { _db = db; } [Route("customers/{customerId}")] [HttpGet] [GetCustomerProfileAuth] public IHttpActionResult Get(int customerId) { var custId = new CustomerId(customerId); var cust = _db.GetProfile(custId); var dto = DtoConverter.CustomerToDto(cust); return Ok(dto); }
  • 79. public interface ICustomerDb { CustomerProfile GetProfile(CustomerId id); void UpdateProfile(CustomerId id, CustomerProfile cust); void CreateAccount(CustomerId id, CustomerProfile cust); void DeleteAccount(CustomerId id); void UpdateLoginEmail(CustomerId id, string email); void UpdatePassword(CustomerId id, string password); } How much authority do you really need?
  • 85. type MessageFlag = ShowThisMessageAgain | DontShowThisMessageAgain type ConfigurationCapabilities = { GetMessageFlag : unit -> MessageFlag SetMessageFlag : MessageFlag -> unit GetBackgroundColor : unit -> Color SetBackgroundColor : Color -> unit GetConnectionString : unit -> ConnectionString SetConnectionString : ConnectionString -> unit } ◘
  • 86. let dontShowMessageAgainDialogBox capabilities = let getFlag,setFlag = capabilities let ctrl= new CheckBox( Text="Don't show this message again") ctrl.Checked <- getFlag() // etc
  • 90. Controller /API Business Logic Database Inject a value of specific type Global Authorizer Passing tokens as types Represent tokens by types!
  • 91. Demo: Using types for access tokens
  • 92. What have we covered? ‱ Don’t use complicated security patterns – This will ensure that they are never used, or used wrong. – Don’t rely on other people doing the right thing. – Don’t rely on other people reading the documentation! ‱ Do use techniques where you get security for free. – You can be lazy! – You don’t have to remember to do anything.
  • 93. What have we covered? ‱ Don’t develop first, add security later – Right after you implement the “quality” module ‱ Do use security-driven design – Bonus: get a modular architecture!
  • 94. What have we covered? ‱ Don’t only do security at the outermost layer ‱ Do use POLA everywhere, which ensures that you have minimal dependencies.
  • 95. Common questions ‱ How do you pass these capabilities around? – Dependency injection or equivalent ‱ Are you saying that all external IO should be passed around as capabilities? – Yes!You should never access any ambient authority. – You should be doing this anyway for mocking.
  • 96. Common questions ‱ Won’t there be too many parameters? – Less than you think! – Encourages vertical slices (per use-case, scenario) – “Functional core, imperative shell” ‱ Can’t this be bypassed by reflection or other backdoors? – Yes. This is really all about design not about total security.
  • 97. Resources for capability-based thinking ‱ LMGTFY “Capability based security” ‱ “Lazy developers guide to secure computing” talk by Marc Stiegler ‱ erights.org ‱ Google's Caja built over JavaScript ‱ Emily, a capability based language (via Ocaml)
  • 98. Thanks! @ScottWlaschin fsharpforfunandprofit.com/cap Contact me Slides and video here Let us know if you need help with F#