Skip to content

Commit 0cd9949

Browse files
committed
Update pvl for facebook
1 parent 6b243b4 commit 0cd9949

5 files changed

Lines changed: 263 additions & 124 deletions

File tree

‎go/pvl/hardcoded.go‎

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,29 @@ var hardcodedPVLString = `
6666
],
6767
"facebook": [
6868
[
69+
{
70+
"assert_regex_match": {
71+
"error": [
72+
"BAD_USERNAME",
73+
"Invalid characters in username '%{username_service}'"
74+
],
75+
"from": "username_service",
76+
"pattern": "^[a-zA-Z0-9\\.]+$"
77+
}
78+
},
6979
{
7080
"regex_capture": {
7181
"error": [
7282
"BAD_API_URL",
73-
"Bad hint from server; URL should start with 'https://m.facebook.com/%{username_service}/posts/', received '%{hint_url}'"
83+
"Bad hint from server; URL should start with 'https://m.facebook.com/%{username_service}/posts/', got '%{hint_url}'"
7484
],
7585
"from": "hint_url",
7686
"into": [
77-
"username_from_url"
87+
"unused1",
88+
"username_from_url",
89+
"post_id"
7890
],
79-
"pattern": "^https://m\\.facebook\\.com/([^/]*)/posts/.*$"
91+
"pattern": "^https://(m|www)\\.facebook\\.com/([^/]*)/posts/([0-9]+)$"
8092
}
8193
},
8294
{
@@ -86,121 +98,118 @@ var hardcodedPVLString = `
8698
"cmp": "stripdots-then-cicmp",
8799
"error": [
88100
"BAD_API_URL",
89-
"Bad hint from server; username in URL match '%{username_service}', received '%{username_from_url}'"
101+
"Bad hint from server; username in URL should match '%{username_service}', received '%{username_from_url}'"
90102
]
91103
}
92104
},
93105
{
94-
"fetch": {
95-
"from": "hint_url",
96-
"kind": "html"
106+
"fill": {
107+
"into": "our_url",
108+
"with": "https://www.facebook.com/%{username_from_url}/posts/%{post_id}"
97109
}
98110
},
99111
{
100-
"selector_css": {
101-
"error": [
102-
"FAILED_PARSE",
103-
"Couldn't find facebook post %{hint_url}. Is it deleted or private?"
104-
],
105-
"into": "unused",
106-
"multi": true,
107-
"selectors": [
108-
"#m_story_permalink_view"
109-
]
112+
"fetch": {
113+
"from": "our_url",
114+
"kind": "html"
110115
}
111116
},
112117
{
113118
"selector_css": {
114-
"attr": "href",
119+
"data": true,
115120
"error": [
116121
"FAILED_PARSE",
117-
"Couldn't find username href"
122+
"Could not find proof markup comment in Facebook's response"
118123
],
119-
"into": "username_link",
124+
"into": "first_code_comment",
120125
"selectors": [
121-
"#m_story_permalink_view > div:first-child > div:first-child > div:first-child h3",
126+
"code",
122127
0,
123-
"a",
128+
{
129+
"contents": true
130+
},
124131
0
125132
]
126133
}
127134
},
128135
{
129-
"parse_url": {
130-
"error": [
131-
"FAILED_PARSE",
132-
"Failed to parse username URL: %{username_link}"
133-
],
134-
"from": "username_link",
135-
"path": "path"
136+
"replace_all": {
137+
"from": "first_code_comment",
138+
"into": "fcc2",
139+
"new": "--",
140+
"old": "-\\-\\"
136141
}
137142
},
138143
{
139-
"regex_capture": {
140-
"error": [
141-
"FAILED_PARSE",
142-
"Username URL has no path"
143-
],
144-
"from": "path",
145-
"into": [
146-
"split_path_1"
147-
],
148-
"pattern": "^[^/]*/([^/]*)$"
144+
"replace_all": {
145+
"from": "fcc2",
146+
"into": "fcc3",
147+
"new": "\\",
148+
"old": "\\\\"
149149
}
150150
},
151151
{
152-
"assert_compare": {
153-
"a": "split_path_1",
154-
"b": "username_service",
155-
"cmp": "stripdots-then-cicmp",
152+
"parse_html": {
156153
"error": [
157-
"BAD_USERNAME",
158-
"Usernames don't match '%{split_path_1}' vs '%{username_service}'"
159-
]
154+
"FAILED_PARSE",
155+
"Failed to parse proof markup comment in Facebook post: %{fcc3}"
156+
],
157+
"from": "fcc3"
160158
}
161159
},
162160
{
163161
"selector_css": {
164162
"error": [
165163
"FAILED_PARSE",
166-
"Couldn't find proof text header"
164+
"Could not find link text in Facebook's response"
167165
],
168-
"into": "header",
166+
"into": "link_text",
167+
"multi": true,
169168
"selectors": [
170-
"#m_story_permalink_view > div:first-child > div:first-child > div:first-child h3",
171-
1
169+
"div.userContent+div a"
172170
]
173171
}
174172
},
175173
{
176174
"whitespace_normalize": {
177-
"from": "header",
178-
"into": "header_nw"
175+
"from": "link_text",
176+
"into": "link_text_nw"
179177
}
180178
},
181179
{
182180
"regex_capture": {
183181
"error": [
184182
"TEXT_NOT_FOUND",
185-
"Proof text not found: 'Verifying myself: I am %{username_keybase} on Keybase.io. %{sig_id_medium}' != '%{header_nw}'"
183+
"Could not find Verifying myself: I am %{username_keybase} on Keybase.io. (%{sig_id_medium})"
186184
],
187-
"from": "header_nw",
185+
"from": "link_text_nw",
188186
"into": [
189-
"username_from_header"
187+
"username_from_link",
188+
"sig_from_link"
190189
],
191-
"pattern": "^Verifying myself: I am (\\S+) on Keybase\\.io\\. %{sig_id_medium}$"
190+
"pattern": "^.*Verifying myself: I am (\\S+) on Keybase.io. (\\S+).*$"
192191
}
193192
},
194193
{
195194
"assert_compare": {
196-
"a": "username_from_header",
195+
"a": "username_from_link",
197196
"b": "username_keybase",
198197
"cmp": "cicmp",
199198
"error": [
200-
"TEXT_NOT_FOUND",
201-
"Wrong keybase username in proof text '%{username_from_header}' != 'username_keybase'"
199+
"BAD_USERNAME",
200+
"Wrong keybase username in post '%{username_from_link}' should be '%{username_keybase}'"
202201
]
203202
}
203+
},
204+
{
205+
"assert_regex_match": {
206+
"error": [
207+
"BAD_SIGNATURE",
208+
"Could not find sig; '%{sig_from_link}' != '%{sig_id_medium}'"
209+
],
210+
"from": "sig_from_link",
211+
"pattern": "^%{sig_id_medium}$"
212+
}
204213
}
205214
]
206215
],

‎go/pvl/helpers.go‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,26 @@ func jsonStringSimple(object *jsonw.Wrapper) (string, error) {
130130
return "", fmt.Errorf("Non-simple object: %v", object)
131131
}
132132

133-
// selectionContents gets the HTML contents of all elements in a selection, concatenated by a space.
133+
// selectionContents gets the value of all elements in a selection, concatenated by a space.
134134
// If getting the contents/attr value of any elements fails, that does not cause an error.
135+
// The value of an element can be:
136+
// 1. its Text (if attr=="" and data==false)
137+
// 2. its Data (if attr=="" and data==true)
138+
// 3. an attribute (if attr=="something" and data==false)
139+
// The value of an element is its Text, unless one of
135140
// The result can be an empty string.
136-
func selectionContents(selection *goquery.Selection, useAttr bool, attr string) string {
141+
func selectionContents(selection *goquery.Selection, attr string, data bool) string {
137142
var results []string
138143
selection.Each(func(i int, element *goquery.Selection) {
139-
if useAttr {
144+
if attr != "" {
140145
res, ok := element.Attr(attr)
141146
if ok {
142147
results = append(results, res)
143148
}
149+
} else if data {
150+
if len(element.Nodes) > 0 {
151+
results = append(results, element.Nodes[0].Data)
152+
}
144153
} else {
145154
results = append(results, element.Text())
146155
}

‎go/pvl/interp.go‎

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package pvl
55

66
import (
7+
"bytes"
78
b64 "encoding/base64"
89
"net"
910
"net/url"
@@ -66,8 +67,10 @@ const (
6667
cmdAssertCompare commandName = "assert_compare"
6768
cmdWhitespaceNormalize commandName = "whitespace_normalize"
6869
cmdRegexCapture commandName = "regex_capture"
70+
cmdReplaceAll commandName = "replace_all"
6971
cmdParseURL commandName = "parse_url"
7072
cmdFetch commandName = "fetch"
73+
cmdParseHTML commandName = "parse_html"
7174
cmdSelectorJSON commandName = "selector_json"
7275
cmdSelectorCSS commandName = "selector_css"
7376
cmdFill commandName = "fill"
@@ -357,6 +360,7 @@ func validateScript(g proofContextExt, script *scriptT, service keybase1.ProofTy
357360
case ins.AssertCompare != nil:
358361
case ins.WhitespaceNormalize != nil:
359362
case ins.RegexCapture != nil:
363+
case ins.ReplaceAll != nil:
360364
case ins.ParseURL != nil:
361365
case ins.Fill != nil:
362366

@@ -388,6 +392,7 @@ func validateScript(g proofContextExt, script *scriptT, service keybase1.ProofTy
388392
return logerr(g, service, whichscript, i,
389393
"Unsupported fetch type: %v", fetchType)
390394
}
395+
case ins.ParseHTML != nil:
391396
case ins.SelectorJSON != nil:
392397
// Can only select after fetching.
393398
switch {
@@ -414,6 +419,12 @@ func validateScript(g proofContextExt, script *scriptT, service keybase1.ProofTy
414419
return logerr(g, service, whichscript, i,
415420
"Script contains css selector in non-html mode")
416421
}
422+
423+
// Can only select one of text, attr, or data.
424+
if ins.SelectorCSS.Attr != "" && ins.SelectorCSS.Data {
425+
return logerr(g, service, whichscript, i,
426+
"Script contains css selector with both 'attr' and 'data' set")
427+
}
417428
default:
418429
return logerr(g, service, whichscript, i,
419430
"Unsupported PVL instruction: %v", ins)
@@ -548,12 +559,18 @@ func stepInstruction(g proofContextExt, ins instructionT, state scriptState) (sc
548559
case ins.RegexCapture != nil:
549560
newState, stepErr = stepRegexCapture(g, *ins.RegexCapture, state)
550561
customErrSpec = ins.RegexCapture.Error
562+
case ins.ReplaceAll != nil:
563+
newState, stepErr = stepReplaceAll(g, *ins.ReplaceAll, state)
564+
customErrSpec = ins.ReplaceAll.Error
551565
case ins.ParseURL != nil:
552566
newState, stepErr = stepParseURL(g, *ins.ParseURL, state)
553567
customErrSpec = ins.ParseURL.Error
554568
case ins.Fetch != nil:
555569
newState, stepErr = stepFetch(g, *ins.Fetch, state)
556570
customErrSpec = ins.Fetch.Error
571+
case ins.ParseHTML != nil:
572+
newState, stepErr = stepParseHTML(g, *ins.ParseHTML, state)
573+
customErrSpec = ins.ParseHTML.Error
557574
case ins.SelectorJSON != nil:
558575
newState, stepErr = stepSelectorJSON(g, *ins.SelectorJSON, state)
559576
customErrSpec = ins.SelectorJSON.Error
@@ -704,6 +721,21 @@ func stepRegexCapture(g proofContextExt, ins regexCaptureT, state scriptState) (
704721
return state, nil
705722
}
706723

724+
func stepReplaceAll(g proofContextExt, ins replaceAllT, state scriptState) (scriptState, libkb.ProofError) {
725+
from, err := state.Regs.Get(ins.From)
726+
if err != nil {
727+
return state, err
728+
}
729+
730+
replaced := strings.Replace(from, ins.Old, ins.New, -1)
731+
err = state.Regs.Set(ins.Into, replaced)
732+
if err != nil {
733+
return state, err
734+
}
735+
736+
return state, nil
737+
}
738+
707739
func stepParseURL(g proofContextExt, ins parseURLT, state scriptState) (scriptState, libkb.ProofError) {
708740
s, err := state.Regs.Get(ins.From)
709741
if err != nil {
@@ -805,6 +837,24 @@ func stepFetch(g proofContextExt, ins fetchT, state scriptState) (scriptState, l
805837
}
806838
}
807839

840+
func stepParseHTML(g proofContextExt, ins parseHTMLT, state scriptState) (scriptState, libkb.ProofError) {
841+
from, err := state.Regs.Get(ins.From)
842+
if err != nil {
843+
return state, err
844+
}
845+
846+
gq, err2 := goquery.NewDocumentFromReader(bytes.NewBuffer([]byte(from)))
847+
if err2 != nil {
848+
return state, libkb.NewProofError(keybase1.ProofStatus_FAILED_PARSE, "Failed to parse html from '%v': %v", ins.From, err2)
849+
}
850+
851+
state.FetchResult = &fetchResult{
852+
fetchMode: fetchModeHTML,
853+
HTML: gq,
854+
}
855+
return state, nil
856+
}
857+
808858
func stepSelectorJSON(g proofContextExt, ins selectorJSONT, state scriptState) (scriptState, libkb.ProofError) {
809859
if state.FetchResult == nil || state.FetchResult.fetchMode != fetchModeJSON {
810860
return state, libkb.NewProofError(keybase1.ProofStatus_INVALID_PVL,
@@ -852,8 +902,7 @@ func stepSelectorCSS(g proofContextExt, ins selectorCSST, state scriptState) (sc
852902
}
853903

854904
// Whether to get an attribute or the text contents.
855-
useAttr := ins.Attr != ""
856-
res := selectionContents(selection, useAttr, ins.Attr)
905+
res := selectionContents(selection, ins.Attr, ins.Data)
857906

858907
err := state.Regs.Set(ins.Into, res)
859908
return state, err
@@ -890,9 +939,11 @@ func runCSSSelectorInner(g proofContextExt, html *goquery.Selection, selectors [
890939
selection = selection.Eq(selector.Index)
891940
case selector.IsKey:
892941
selection = selection.Find(selector.Key)
942+
case selector.IsContents:
943+
selection = selection.Contents()
893944
default:
894945
return nil, libkb.NewProofError(keybase1.ProofStatus_INVALID_PVL,
895-
"Selector entry must be a string or int %v", selector)
946+
"CSS selector entry must be a string, int, or 'contents' %v", selector)
896947
}
897948
}
898949

@@ -957,9 +1008,10 @@ func runSelectorJSONInner(g proofContextExt, state scriptState, selectedObject *
9571008
results = append(results, innerresults...)
9581009
}
9591010
return results, nil
1011+
default:
1012+
return nil, libkb.NewProofError(keybase1.ProofStatus_INVALID_PVL,
1013+
"JSON selector entry must be a string, int, or 'all' %v", selector)
9601014
}
961-
return []string{}, libkb.NewProofError(keybase1.ProofStatus_INVALID_PVL,
962-
"Invalid selector entry: %v", selector)
9631015
}
9641016

9651017
// Take a regex descriptor, do variable substitution, and build a regex.

0 commit comments

Comments
 (0)