forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helpers for rkey and tid syntax; validate rkey at record creation time (
bluesky-social#1738) * syntax: fix jest config displayName * syntax: TID validation * syntax: add recordkey validation * pds: verify rkey syntax at record creation time --------- Co-authored-by: dholms <[email protected]>
- Loading branch information
Showing
15 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
# not base32 | ||
3jzfcijpj2z21 | ||
0000000000000 | ||
|
||
# too long/short | ||
3jzfcijpj2z2aa | ||
3jzfcijpj2z2 | ||
|
||
# old dashes syntax not actually supported (TTTT-TTT-TTTT-CC) | ||
3jzf-cij-pj2z-2a | ||
|
||
# high bit can't be high | ||
zzzzzzzzzzzzz | ||
kjzfcijpj2z2a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# 13 digits | ||
# 234567abcdefghijklmnopqrstuvwxyz | ||
|
||
3jzfcijpj2z2a | ||
7777777777777 | ||
3zzzzzzzzzzzz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export const ensureValidRecordKey = (rkey: string): void => { | ||
if (rkey.length > 512 || rkey.length < 1) { | ||
throw new InvalidRecordKeyError('record key must be 1 to 512 characters') | ||
} | ||
// simple regex to enforce most constraints via just regex and length. | ||
if (!/^[a-zA-Z0-9_~.-]{1,512}$/.test(rkey)) { | ||
throw new InvalidRecordKeyError('record key syntax not valid (regex)') | ||
} | ||
if (rkey == '.' || rkey == '..') | ||
throw new InvalidRecordKeyError('record key can not be "." or ".."') | ||
} | ||
|
||
export const isValidRecordKey = (rkey: string): boolean => { | ||
try { | ||
ensureValidRecordKey(rkey) | ||
} catch (err) { | ||
if (err instanceof InvalidRecordKeyError) { | ||
return false | ||
} | ||
throw err | ||
} | ||
|
||
return true | ||
} | ||
|
||
export class InvalidRecordKeyError extends Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const ensureValidTid = (tid: string): void => { | ||
if (tid.length != 13) { | ||
throw new InvalidTidError('TID must be 13 characters') | ||
} | ||
// simple regex to enforce most constraints via just regex and length. | ||
if (!/^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/.test(tid)) { | ||
throw new InvalidTidError('TID syntax not valid (regex)') | ||
} | ||
} | ||
|
||
export const isValidTid = (tid: string): boolean => { | ||
try { | ||
ensureValidTid(tid) | ||
} catch (err) { | ||
if (err instanceof InvalidTidError) { | ||
return false | ||
} | ||
throw err | ||
} | ||
|
||
return true | ||
} | ||
|
||
export class InvalidTidError extends Error {} |
1 change: 1 addition & 0 deletions
1
packages/syntax/tests/interop-files/recordkey_syntax_invalid.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../interop-test-files/syntax/recordkey_syntax_invalid.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../interop-test-files/syntax/recordkey_syntax_valid.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../interop-test-files/syntax/tid_syntax_invalid.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../interop-test-files/syntax/tid_syntax_valid.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ensureValidRecordKey, InvalidRecordKeyError } from '../src' | ||
import * as readline from 'readline' | ||
import * as fs from 'fs' | ||
|
||
describe('recordkey validation', () => { | ||
const expectValid = (r: string) => { | ||
ensureValidRecordKey(r) | ||
} | ||
const expectInvalid = (r: string) => { | ||
expect(() => ensureValidRecordKey(r)).toThrow(InvalidRecordKeyError) | ||
} | ||
|
||
it('conforms to interop valid recordkey', () => { | ||
const lineReader = readline.createInterface({ | ||
input: fs.createReadStream( | ||
`${__dirname}/interop-files/recordkey_syntax_valid.txt`, | ||
), | ||
terminal: false, | ||
}) | ||
lineReader.on('line', (line) => { | ||
if (line.startsWith('#') || line.length == 0) { | ||
return | ||
} | ||
expectValid(line) | ||
}) | ||
}) | ||
|
||
it('conforms to interop invalid recordkeys', () => { | ||
const lineReader = readline.createInterface({ | ||
input: fs.createReadStream( | ||
`${__dirname}/interop-files/recordkey_syntax_invalid.txt`, | ||
), | ||
terminal: false, | ||
}) | ||
lineReader.on('line', (line) => { | ||
if (line.startsWith('#') || line.length == 0) { | ||
return | ||
} | ||
expectInvalid(line) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ensureValidTid, InvalidTidError } from '../src' | ||
import * as readline from 'readline' | ||
import * as fs from 'fs' | ||
|
||
describe('tid validation', () => { | ||
const expectValid = (t: string) => { | ||
ensureValidTid(t) | ||
} | ||
const expectInvalid = (t: string) => { | ||
expect(() => ensureValidTid(t)).toThrow(InvalidTidError) | ||
} | ||
|
||
it('conforms to interop valid tid', () => { | ||
const lineReader = readline.createInterface({ | ||
input: fs.createReadStream( | ||
`${__dirname}/interop-files/tid_syntax_valid.txt`, | ||
), | ||
terminal: false, | ||
}) | ||
lineReader.on('line', (line) => { | ||
if (line.startsWith('#') || line.length == 0) { | ||
return | ||
} | ||
expectValid(line) | ||
}) | ||
}) | ||
|
||
it('conforms to interop invalid tids', () => { | ||
const lineReader = readline.createInterface({ | ||
input: fs.createReadStream( | ||
`${__dirname}/interop-files/tid_syntax_invalid.txt`, | ||
), | ||
terminal: false, | ||
}) | ||
lineReader.on('line', (line) => { | ||
if (line.startsWith('#') || line.length == 0) { | ||
return | ||
} | ||
expectInvalid(line) | ||
}) | ||
}) | ||
}) |