SS-week9
Database Hardening
Mass Assignment Vulnerabilities
What
要解決問題就要先了解問題
Mass assignment is a computer vulnerability where an active record pattern in a web application is abused to modify data.
∵ In serialization formats is automatically converted on input into internal object and, in turn, into database record fields. It’s possible to overwrite fields that were never intended to be modified from outside.
最常見的在insert 或 update時 塞入id欄位的值
1 | # Project has many Configurations |
Solutions
Sequel has Default Mass Assignment Restrictions: Primary keys, Foreign keys🙏
除此之外,也可以有Custom Mass Attack Restrictions
1 | class Configuration < Sequel::Model |
SQL Injection Vulnerability
What
Any input from users should be considered dangerous and ‘dirty’ to your system
- SQL statements are inserted into an entry field for execution
- Passing web route parameters directly into the database permits SQL injection
1 | s = 'http://localhost:9292/api/v1/projects/2%20or%20id%3D1' |
Solutions
Validation : Essential, but has limited effect on SQL injection
Literalization : simplest way to prevent sql injection
1 | project = Project.where(id: 1) |
Query Parameterization : One of the best ways to prevent SQL injection
- Application side : Bound Statements
1 | projects = Project.where(id: :$find_id) |
Database side : Prepared Statements (wiki)
a feature used to execute the same or similar database statements repeatedly with high efficiency.
The overhead of compiling and optimizing the statement is incurred only once, although the statement is executed multiple times.
because parameter values need not be correctly escaped
1 | ds = DB[:items].where(:name=>:$n) |
UUID
Pros
- Unique across every table, every database, every server
- Allows easy merging of records from different databases
- Allows easy distribution of databases across multiple servers
- Can generate IDs without having to roundtrip to the database
- DB replication usually requires UUIDs
Cons
- Performance costs on inserts and joins
- Ugly URLs (if uuid is in URL)
- Cumbersome to debug: long, unpredictable
1 | SecureRandom.uuid |
1 | # migrations |