social bookmarking for atproto

[appview/indexer] Obey new Lexicon constraints and add more tests

hexmani.ac 42c68e49 4c57816d

verified
+78 -6
+78 -6
backend/src/network/validator.ts
··· 9 9 SocialClipprFeedClip, 10 10 SocialClipprFeedTag, 11 11 } from "@clipprjs/lexicons"; 12 - import { isDatetime, isLanguageCode } from "@atcute/lexicons/syntax"; 12 + import { 13 + isDatetime, 14 + isGenericUri, 15 + isLanguageCode, 16 + } from "@atcute/lexicons/syntax"; 13 17 import Logger from "../logger.js"; 18 + import { ComAtprotoRepoStrongRef } from "@atcute/atproto"; 19 + import { is } from "@atcute/lexicons"; 14 20 15 21 export async function validateProfile( 16 22 record: SocialClipprActorProfile.Main, ··· 31 37 ); 32 38 return false; 33 39 } 40 + 41 + if (record.displayName.length < 1) { 42 + Logger.verbose( 43 + "Too short displayName from incoming profile record", 44 + record, 45 + ); 46 + return false; 47 + } 34 48 } else { 35 49 Logger.verbose("No displayName from incoming profile record", record); 36 50 return false; ··· 44 58 ); 45 59 return false; 46 60 } 61 + 62 + if (record.description.length < 1) { 63 + Logger.verbose( 64 + "Too short description from incoming profile record", 65 + record, 66 + ); 67 + return false; 68 + } 47 69 } 48 70 49 71 return true; ··· 61 83 } 62 84 63 85 if (record.name.length > 64) { 64 - Logger.verbose("Invalid name length for incoming tag record", record); 86 + Logger.verbose("Name from incoming tag record is too long", record); 65 87 return false; 66 88 } 67 89 90 + if (record.name.length < 1) { 91 + Logger.verbose("Name from incoming tag record is too short", record); 92 + } 93 + 68 94 if (record.color) { 69 95 if (record.color.length > 7) { 70 - Logger.verbose("Invalid color length for incoming tag record", record); 96 + Logger.verbose("Color from incoming tag record is too long", record); 97 + return false; 98 + } 99 + 100 + if (record.color.length < 4) { 101 + Logger.verbose("Color from incoming tag record is too short", record); 71 102 return false; 72 103 } 73 104 ··· 86 117 export async function validateClip( 87 118 record: SocialClipprFeedClip.Main, 88 119 ): Promise<boolean> { 120 + if (!isGenericUri(record.url)) { 121 + Logger.verbose("Invalid url from incoming clip record", record); 122 + return false; 123 + } 124 + 89 125 if (record.url.length > 2000) { 90 126 Logger.verbose("Too long url from incoming clip record", record); 91 127 return false; 92 128 } 93 129 130 + if (record.url.length < 3) { 131 + Logger.verbose("Too short url from incoming clip record", record); 132 + return false; 133 + } 134 + 94 135 if (record.title.length > 2048) { 95 136 Logger.verbose("Too long title from incoming clip record", record); 96 137 return false; 97 138 } 98 139 140 + if (record.title.length < 1) { 141 + Logger.verbose("Too short title from incoming clip record", record); 142 + return false; 143 + } 144 + 99 145 if (record.description.length > 4096) { 100 146 Logger.verbose("Too long description from incoming clip record", record); 101 147 return false; 102 148 } 103 149 150 + if (record.description.length < 1) { 151 + Logger.verbose("Too short description from incoming clip record", record); 152 + return false; 153 + } 154 + 104 155 if (record.notes) { 105 156 if (record.notes.length > 10000) { 106 157 Logger.verbose("Too long notes from incoming clip record", record); 107 158 return false; 108 159 } 160 + 161 + if (record.notes.length < 1) { 162 + Logger.verbose("Too short notes from incoming clip record", record); 163 + return false; 164 + } 109 165 } 110 166 111 167 if (record.tags) { 112 - if (record.tags.some((tag) => tag.$type !== "com.atproto.repo.strongRef")) { 168 + if ( 169 + record.tags.some((tag) => { 170 + return tag.$type !== "com.atproto.repo.strongRef"; 171 + }) 172 + ) { 113 173 Logger.verbose( 114 - "A tag ref from incoming clip record is not a strongRef", 174 + "A tag from incoming clip record is not typed as strongRef", 175 + record, 176 + ); 177 + return false; 178 + } 179 + 180 + if ( 181 + record.tags.some((tag) => { 182 + return !is(ComAtprotoRepoStrongRef.mainSchema, tag); 183 + }) 184 + ) { 185 + Logger.verbose( 186 + "A tag from incoming clip record is not a valid strongRef", 115 187 record, 116 188 ); 117 189 return false; 118 190 } 119 191 120 - // There should be more tests here, but I'm not exactly sure what to add... 192 + // There should definitely be more tests here, but I'm not exactly sure what to add... 121 193 } 122 194 123 195 if (typeof record.unlisted !== "boolean") {