print.html 715 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651
  1. <!DOCTYPE HTML>
  2. <html lang="en" class="sidebar-visible no-js light">
  3. <head>
  4. <!-- Book generated using mdBook -->
  5. <meta charset="UTF-8">
  6. <title>Synapse</title>
  7. <meta name="robots" content="noindex" />
  8. <!-- Custom HTML head -->
  9. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  10. <meta name="description" content="">
  11. <meta name="viewport" content="width=device-width, initial-scale=1">
  12. <meta name="theme-color" content="#ffffff" />
  13. <link rel="icon" href="favicon.svg">
  14. <link rel="shortcut icon" href="favicon.png">
  15. <link rel="stylesheet" href="css/variables.css">
  16. <link rel="stylesheet" href="css/general.css">
  17. <link rel="stylesheet" href="css/chrome.css">
  18. <link rel="stylesheet" href="css/print.css" media="print">
  19. <!-- Fonts -->
  20. <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
  21. <link rel="stylesheet" href="fonts/fonts.css">
  22. <!-- Highlight.js Stylesheets -->
  23. <link rel="stylesheet" href="highlight.css">
  24. <link rel="stylesheet" href="tomorrow-night.css">
  25. <link rel="stylesheet" href="ayu-highlight.css">
  26. <!-- Custom theme stylesheets -->
  27. <link rel="stylesheet" href="docs/website_files/table-of-contents.css">
  28. <link rel="stylesheet" href="docs/website_files/remove-nav-buttons.css">
  29. <link rel="stylesheet" href="docs/website_files/indent-section-headers.css">
  30. <link rel="stylesheet" href="docs/website_files/version-picker.css">
  31. </head>
  32. <body>
  33. <!-- Provide site root to javascript -->
  34. <script type="text/javascript">
  35. var path_to_root = "";
  36. var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
  37. </script>
  38. <!-- Work around some values being stored in localStorage wrapped in quotes -->
  39. <script type="text/javascript">
  40. try {
  41. var theme = localStorage.getItem('mdbook-theme');
  42. var sidebar = localStorage.getItem('mdbook-sidebar');
  43. if (theme.startsWith('"') && theme.endsWith('"')) {
  44. localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
  45. }
  46. if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
  47. localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
  48. }
  49. } catch (e) { }
  50. </script>
  51. <!-- Set the theme before any content is loaded, prevents flash -->
  52. <script type="text/javascript">
  53. var theme;
  54. try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
  55. if (theme === null || theme === undefined) { theme = default_theme; }
  56. var html = document.querySelector('html');
  57. html.classList.remove('no-js')
  58. html.classList.remove('light')
  59. html.classList.add(theme);
  60. html.classList.add('js');
  61. </script>
  62. <!-- Hide / unhide sidebar before it is displayed -->
  63. <script type="text/javascript">
  64. var html = document.querySelector('html');
  65. var sidebar = 'hidden';
  66. if (document.body.clientWidth >= 1080) {
  67. try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
  68. sidebar = sidebar || 'visible';
  69. }
  70. html.classList.remove('sidebar-visible');
  71. html.classList.add("sidebar-" + sidebar);
  72. </script>
  73. <nav id="sidebar" class="sidebar" aria-label="Table of contents">
  74. <div class="sidebar-scrollbox">
  75. <ol class="chapter"><li class="chapter-item expanded affix "><li class="part-title">Introduction</li><li class="chapter-item expanded "><a href="welcome_and_overview.html">Welcome and Overview</a></li><li class="chapter-item expanded affix "><li class="part-title">Setup</li><li class="chapter-item expanded "><a href="setup/installation.html">Installation</a></li><li class="chapter-item expanded "><a href="postgres.html">Using Postgres</a></li><li class="chapter-item expanded "><a href="reverse_proxy.html">Configuring a Reverse Proxy</a></li><li class="chapter-item expanded "><a href="setup/forward_proxy.html">Configuring a Forward/Outbound Proxy</a></li><li class="chapter-item expanded "><a href="turn-howto.html">Configuring a Turn Server</a></li><li class="chapter-item expanded "><a href="delegate.html">Delegation</a></li><li class="chapter-item expanded affix "><li class="part-title">Upgrading</li><li class="chapter-item expanded "><a href="upgrade.html">Upgrading between Synapse Versions</a></li><li class="chapter-item expanded "><a href="MSC1711_certificates_FAQ.html">Upgrading from pre-Synapse 1.0</a></li><li class="chapter-item expanded affix "><li class="part-title">Usage</li><li class="chapter-item expanded "><a href="federate.html">Federation</a></li><li class="chapter-item expanded "><a href="usage/configuration/index.html">Configuration</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="usage/configuration/homeserver_sample_config.html">Homeserver Sample Config File</a></li><li class="chapter-item expanded "><a href="usage/configuration/logging_sample_config.html">Logging Sample Config File</a></li><li class="chapter-item expanded "><a href="structured_logging.html">Structured Logging</a></li><li class="chapter-item expanded "><a href="templates.html">Templates</a></li><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/index.html">User Authentication</a></li><li><ol class="section"><li class="chapter-item expanded "><div>Single-Sign On</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="openid.html">OpenID Connect</a></li><li class="chapter-item expanded "><div>SAML</div></li><li class="chapter-item expanded "><div>CAS</div></li><li class="chapter-item expanded "><a href="sso_mapping_providers.html">SSO Mapping Providers</a></li></ol></li><li class="chapter-item expanded "><a href="password_auth_providers.html">Password Auth Providers</a></li><li class="chapter-item expanded "><a href="jwt.html">JSON Web Tokens</a></li></ol></li><li class="chapter-item expanded "><a href="CAPTCHA_SETUP.html">Registration Captcha</a></li><li class="chapter-item expanded "><a href="application_services.html">Application Services</a></li><li class="chapter-item expanded "><a href="server_notices.html">Server Notices</a></li><li class="chapter-item expanded "><a href="consent_tracking.html">Consent Tracking</a></li><li class="chapter-item expanded "><a href="development/url_previews.html">URL Previews</a></li><li class="chapter-item expanded "><a href="user_directory.html">User Directory</a></li><li class="chapter-item expanded "><a href="message_retention_policies.html">Message Retention Policies</a></li><li class="chapter-item expanded "><a href="modules/index.html">Pluggable Modules</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="modules/writing_a_module.html">Writing a module</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="modules/spam_checker_callbacks.html">Spam checker callbacks</a></li><li class="chapter-item expanded "><a href="modules/third_party_rules_callbacks.html">Third-party rules callbacks</a></li><li class="chapter-item expanded "><a href="modules/presence_router_callbacks.html">Presence router callbacks</a></li><li class="chapter-item expanded "><a href="modules/account_validity_callbacks.html">Account validity callbacks</a></li><li class="chapter-item expanded "><a href="modules/password_auth_provider_callbacks.html">Password auth provider callbacks</a></li><li class="chapter-item expanded "><a href="modules/porting_legacy_module.html">Porting a legacy module to the new interface</a></li></ol></li></ol></li><li class="chapter-item expanded "><a href="workers.html">Workers</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="synctl_workers.html">Using synctl with Workers</a></li><li class="chapter-item expanded "><a href="systemd-with-workers/index.html">Systemd</a></li></ol></li></ol></li><li class="chapter-item expanded "><a href="usage/administration/index.html">Administration</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="usage/administration/admin_api/index.html">Admin API</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="admin_api/account_validity.html">Account Validity</a></li><li class="chapter-item expanded "><a href="usage/administration/admin_api/background_updates.html">Background Updates</a></li><li class="chapter-item expanded "><a href="admin_api/delete_group.html">Delete Group</a></li><li class="chapter-item expanded "><a href="admin_api/event_reports.html">Event Reports</a></li><li class="chapter-item expanded "><a href="admin_api/media_admin_api.html">Media</a></li><li class="chapter-item expanded "><a href="admin_api/purge_history_api.html">Purge History</a></li><li class="chapter-item expanded "><a href="admin_api/register_api.html">Register Users</a></li><li class="chapter-item expanded "><a href="usage/administration/admin_api/registration_tokens.html">Registration Tokens</a></li><li class="chapter-item expanded "><a href="admin_api/room_membership.html">Manipulate Room Membership</a></li><li class="chapter-item expanded "><a href="admin_api/rooms.html">Rooms</a></li><li class="chapter-item expanded "><a href="admin_api/server_notices.html">Server Notices</a></li><li class="chapter-item expanded "><a href="admin_api/statistics.html">Statistics</a></li><li class="chapter-item expanded "><a href="admin_api/user_admin_api.html">Users</a></li><li class="chapter-item expanded "><a href="admin_api/version_api.html">Server Version</a></li></ol></li><li class="chapter-item expanded "><a href="manhole.html">Manhole</a></li><li class="chapter-item expanded "><a href="metrics-howto.html">Monitoring</a></li><li class="chapter-item expanded "><a href="usage/administration/request_log.html">Request log format</a></li><li class="chapter-item expanded "><div>Scripts</div></li></ol></li><li class="chapter-item expanded "><li class="part-title">Development</li><li class="chapter-item expanded "><a href="development/contributing_guide.html">Contributing Guide</a></li><li class="chapter-item expanded "><a href="code_style.html">Code Style</a></li><li class="chapter-item expanded "><a href="development/git.html">Git Usage</a></li><li class="chapter-item expanded "><div>Testing</div></li><li class="chapter-item expanded "><a href="opentracing.html">OpenTracing</a></li><li class="chapter-item expanded "><a href="development/database_schema.html">Database Schemas</a></li><li class="chapter-item expanded "><a href="development/experimental_features.html">Experimental features</a></li><li class="chapter-item expanded "><div>Synapse Architecture</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="log_contexts.html">Log Contexts</a></li><li class="chapter-item expanded "><a href="replication.html">Replication</a></li><li class="chapter-item expanded "><a href="tcp_replication.html">TCP Replication</a></li></ol></li><li class="chapter-item expanded "><a href="development/internal_documentation/index.html">Internal Documentation</a></li><li><ol class="section"><li class="chapter-item expanded "><div>Single Sign-On</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="development/saml.html">SAML</a></li><li class="chapter-item expanded "><a href="development/cas.html">CAS</a></li></ol></li><li class="chapter-item expanded "><a href="development/room-dag-concepts.html">Room DAG concepts</a></li><li class="chapter-item expanded "><div>State Resolution</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="auth_chain_difference_algorithm.html">The Auth Chain Difference Algorithm</a></li></ol></li><li class="chapter-item expanded "><a href="media_repository.html">Media Repository</a></li><li class="chapter-item expanded "><a href="room_and_user_statistics.html">Room and User Statistics</a></li></ol></li><li class="chapter-item expanded "><div>Scripts</div></li><li class="chapter-item expanded affix "><li class="part-title">Other</li><li class="chapter-item expanded "><a href="deprecation_policy.html">Dependency Deprecation Policy</a></li></ol>
  76. </div>
  77. <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
  78. </nav>
  79. <div id="page-wrapper" class="page-wrapper">
  80. <div class="page">
  81. <div id="menu-bar-hover-placeholder"></div>
  82. <div id="menu-bar" class="menu-bar sticky bordered">
  83. <div class="left-buttons">
  84. <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
  85. <i class="fa fa-bars"></i>
  86. </button>
  87. <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
  88. <i class="fa fa-paint-brush"></i>
  89. </button>
  90. <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
  91. <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
  92. <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
  93. <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
  94. <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
  95. <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
  96. </ul>
  97. <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
  98. <i class="fa fa-search"></i>
  99. </button>
  100. <div class="version-picker">
  101. <div class="dropdown">
  102. <div class="select">
  103. <span></span>
  104. <i class="fa fa-chevron-down"></i>
  105. </div>
  106. <input type="hidden" name="version">
  107. <ul class="dropdown-menu">
  108. <!-- Versions will be added dynamically in version-picker.js -->
  109. </ul>
  110. </div>
  111. </div>
  112. </div>
  113. <h1 class="menu-title">Synapse</h1>
  114. <div class="right-buttons">
  115. <a href="print.html" title="Print this book" aria-label="Print this book">
  116. <i id="print-button" class="fa fa-print"></i>
  117. </a>
  118. <a href="https://github.com/matrix-org/synapse" title="Git repository" aria-label="Git repository">
  119. <i id="git-repository-button" class="fa fa-github"></i>
  120. </a>
  121. </div>
  122. </div>
  123. <div id="search-wrapper" class="hidden">
  124. <form id="searchbar-outer" class="searchbar-outer">
  125. <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
  126. </form>
  127. <div id="searchresults-outer" class="searchresults-outer hidden">
  128. <div id="searchresults-header" class="searchresults-header"></div>
  129. <ul id="searchresults">
  130. </ul>
  131. </div>
  132. </div>
  133. <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
  134. <script type="text/javascript">
  135. document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
  136. document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
  137. Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
  138. link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
  139. });
  140. </script>
  141. <div id="content" class="content">
  142. <main>
  143. <!-- Page table of contents -->
  144. <div class="sidetoc">
  145. <nav class="pagetoc"></nav>
  146. </div>
  147. <div style="break-before: page; page-break-before: always;"></div><h1 id="introduction"><a class="header" href="#introduction">Introduction</a></h1>
  148. <p>Welcome to the documentation repository for Synapse, a
  149. <a href="https://matrix.org">Matrix</a> homeserver implementation developed by the matrix.org core
  150. team.</p>
  151. <h2 id="installing-and-using-synapse"><a class="header" href="#installing-and-using-synapse">Installing and using Synapse</a></h2>
  152. <p>This documentation covers topics for <strong>installation</strong>, <strong>configuration</strong> and
  153. <strong>maintainence</strong> of your Synapse process:</p>
  154. <ul>
  155. <li>
  156. <p>Learn how to <a href="setup/installation.html">install</a> and
  157. <a href="usage/configuration/index.html">configure</a> your own instance, perhaps with <a href="usage/configuration/user_authentication/index.html">Single
  158. Sign-On</a>.</p>
  159. </li>
  160. <li>
  161. <p>See how to <a href="upgrade.html">upgrade</a> between Synapse versions.</p>
  162. </li>
  163. <li>
  164. <p>Administer your instance using the <a href="usage/administration/admin_api/index.html">Admin
  165. API</a>, installing <a href="modules/index.html">pluggable
  166. modules</a>, or by accessing the <a href="manhole.html">manhole</a>.</p>
  167. </li>
  168. <li>
  169. <p>Learn how to <a href="usage/administration/request_log.html">read log lines</a>, configure
  170. <a href="usage/configuration/logging_sample_config.html">logging</a> or set up <a href="structured_logging.html">structured
  171. logging</a>.</p>
  172. </li>
  173. <li>
  174. <p>Scale Synapse through additional <a href="workers.html">worker processes</a>.</p>
  175. </li>
  176. <li>
  177. <p>Set up <a href="metrics-howto.html">monitoring and metrics</a> to keep an eye on your
  178. Synapse instance's performance.</p>
  179. </li>
  180. </ul>
  181. <h2 id="developing-on-synapse"><a class="header" href="#developing-on-synapse">Developing on Synapse</a></h2>
  182. <p>Contributions are welcome! Synapse is primarily written in
  183. <a href="https://python.org">Python</a>. As a developer, you may be interested in the
  184. following documentation:</p>
  185. <ul>
  186. <li>
  187. <p>Read the <a href="development/contributing_guide.html">Contributing Guide</a>. It is meant
  188. to walk new contributors through the process of developing and submitting a
  189. change to the Synapse codebase (which is <a href="https://github.com/matrix-org/synapse">hosted on
  190. GitHub</a>).</p>
  191. </li>
  192. <li>
  193. <p>Set up your <a href="development/contributing_guide.html#2-what-do-i-need">development
  194. environment</a>, then learn
  195. how to <a href="development/contributing_guide.html#run-the-linters">lint</a> and
  196. <a href="development/contributing_guide.html#8-test-test-test">test</a> your code.</p>
  197. </li>
  198. <li>
  199. <p>Look at <a href="https://github.com/matrix-org/synapse/issues">the issue tracker</a> for
  200. bugs to fix or features to add. If you're new, it may be best to start with
  201. those labeled <a href="https://github.com/matrix-org/synapse/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22">good first
  202. issue</a>.</p>
  203. </li>
  204. <li>
  205. <p>Understand <a href="development/internal_documentation/index.html">how Synapse is
  206. built</a>, how to <a href="development/database_schema.html">migrate
  207. database schemas</a>, learn about
  208. <a href="federate.html">federation</a> and how to <a href="federate.html#running-a-demo-federation-of-synapses">set up a local
  209. federation</a> for development.</p>
  210. </li>
  211. <li>
  212. <p>We like to keep our <code>git</code> history clean. <a href="development/git.html">Learn</a> how to
  213. do so!</p>
  214. </li>
  215. <li>
  216. <p>And finally, contribute to this documentation! The source for which is
  217. <a href="https://github.com/matrix-org/synapse/tree/develop/docs">located here</a>.</p>
  218. </li>
  219. </ul>
  220. <h2 id="donating-to-synapse-development"><a class="header" href="#donating-to-synapse-development">Donating to Synapse development</a></h2>
  221. <p>Want to help keep Synapse going but don't know how to code? Synapse is a
  222. <a href="https://matrix.org">Matrix.org Foundation</a> project. Consider becoming a
  223. supportor on <a href="https://liberapay.com/matrixdotorg">Liberapay</a>,
  224. <a href="https://patreon.com/matrixdotorg">Patreon</a> or through
  225. <a href="https://paypal.me/matrixdotorg">PayPal</a> via a one-time donation.</p>
  226. <p>If you are an organisation or enterprise and would like to sponsor development,
  227. reach out to us over email at: support (at) matrix.org</p>
  228. <h2 id="reporting-a-security-vulnerability"><a class="header" href="#reporting-a-security-vulnerability">Reporting a security vulnerability</a></h2>
  229. <p>If you've found a security issue in Synapse or any other Matrix.org Foundation
  230. project, please report it to us in accordance with our <a href="https://www.matrix.org/security-disclosure-policy/">Security Disclosure
  231. Policy</a>. Thank you!</p>
  232. <div style="break-before: page; page-break-before: always;"></div><h1 id="installation-instructions"><a class="header" href="#installation-instructions">Installation Instructions</a></h1>
  233. <h2 id="choosing-your-server-name"><a class="header" href="#choosing-your-server-name">Choosing your server name</a></h2>
  234. <p>It is important to choose the name for your server before you install Synapse,
  235. because it cannot be changed later.</p>
  236. <p>The server name determines the &quot;domain&quot; part of user-ids for users on your
  237. server: these will all be of the format <code>@user:my.domain.name</code>. It also
  238. determines how other matrix servers will reach yours for federation.</p>
  239. <p>For a test configuration, set this to the hostname of your server. For a more
  240. production-ready setup, you will probably want to specify your domain
  241. (<code>example.com</code>) rather than a matrix-specific hostname here (in the same way
  242. that your email address is probably <code>user@example.com</code> rather than
  243. <code>user@email.example.com</code>) - but doing so may require more advanced setup: see
  244. <a href="setup/../federate.html">Setting up Federation</a>.</p>
  245. <h2 id="installing-synapse"><a class="header" href="#installing-synapse">Installing Synapse</a></h2>
  246. <h3 id="prebuilt-packages"><a class="header" href="#prebuilt-packages">Prebuilt packages</a></h3>
  247. <p>Prebuilt packages are available for a number of platforms. These are recommended
  248. for most users.</p>
  249. <h4 id="docker-images-and-ansible-playbooks"><a class="header" href="#docker-images-and-ansible-playbooks">Docker images and Ansible playbooks</a></h4>
  250. <p>There is an official synapse image available at
  251. <a href="https://hub.docker.com/r/matrixdotorg/synapse">https://hub.docker.com/r/matrixdotorg/synapse</a> which can be used with
  252. the docker-compose file available at
  253. <a href="https://github.com/matrix-org/synapse/tree/develop/contrib/docker">contrib/docker</a>.
  254. Further information on this including configuration options is available in the README
  255. on hub.docker.com.</p>
  256. <p>Alternatively, Andreas Peters (previously Silvio Fricke) has contributed a
  257. Dockerfile to automate a synapse server in a single Docker image, at
  258. <a href="https://hub.docker.com/r/avhost/docker-matrix/tags/">https://hub.docker.com/r/avhost/docker-matrix/tags/</a></p>
  259. <p>Slavi Pantaleev has created an Ansible playbook,
  260. which installs the offical Docker image of Matrix Synapse
  261. along with many other Matrix-related services (Postgres database, Element, coturn,
  262. ma1sd, SSL support, etc.).
  263. For more details, see
  264. <a href="https://github.com/spantaleev/matrix-docker-ansible-deploy">https://github.com/spantaleev/matrix-docker-ansible-deploy</a></p>
  265. <h4 id="debianubuntu"><a class="header" href="#debianubuntu">Debian/Ubuntu</a></h4>
  266. <h5 id="matrixorg-packages"><a class="header" href="#matrixorg-packages">Matrix.org packages</a></h5>
  267. <p>Matrix.org provides Debian/Ubuntu packages of Synapse, for the amd64
  268. architecture via <a href="https://packages.matrix.org/debian/">https://packages.matrix.org/debian/</a>.</p>
  269. <p>To install the latest release:</p>
  270. <pre><code class="language-sh">sudo apt install -y lsb-release wget apt-transport-https
  271. sudo wget -O /usr/share/keyrings/matrix-org-archive-keyring.gpg https://packages.matrix.org/debian/matrix-org-archive-keyring.gpg
  272. echo &quot;deb [signed-by=/usr/share/keyrings/matrix-org-archive-keyring.gpg] https://packages.matrix.org/debian/ $(lsb_release -cs) main&quot; |
  273. sudo tee /etc/apt/sources.list.d/matrix-org.list
  274. sudo apt update
  275. sudo apt install matrix-synapse-py3
  276. </code></pre>
  277. <p>Packages are also published for release candidates. To enable the prerelease
  278. channel, add <code>prerelease</code> to the <code>sources.list</code> line. For example:</p>
  279. <pre><code class="language-sh">sudo wget -O /usr/share/keyrings/matrix-org-archive-keyring.gpg https://packages.matrix.org/debian/matrix-org-archive-keyring.gpg
  280. echo &quot;deb [signed-by=/usr/share/keyrings/matrix-org-archive-keyring.gpg] https://packages.matrix.org/debian/ $(lsb_release -cs) main prerelease&quot; |
  281. sudo tee /etc/apt/sources.list.d/matrix-org.list
  282. sudo apt update
  283. sudo apt install matrix-synapse-py3
  284. </code></pre>
  285. <p>The fingerprint of the repository signing key (as shown by <code>gpg /usr/share/keyrings/matrix-org-archive-keyring.gpg</code>) is
  286. <code>AAF9AE843A7584B5A3E4CD2BCF45A512DE2DA058</code>.</p>
  287. <h5 id="downstream-debian-packages"><a class="header" href="#downstream-debian-packages">Downstream Debian packages</a></h5>
  288. <p>We do not recommend using the packages from the default Debian <code>buster</code>
  289. repository at this time, as they are old and suffer from known security
  290. vulnerabilities. You can install the latest version of Synapse from
  291. <a href="setup/installation.html#matrixorg-packages">our repository</a> or from <code>buster-backports</code>. Please
  292. see the <a href="https://backports.debian.org/Instructions/">Debian documentation</a>
  293. for information on how to use backports.</p>
  294. <p>If you are using Debian <code>sid</code> or testing, Synapse is available in the default
  295. repositories and it should be possible to install it simply with:</p>
  296. <pre><code class="language-sh">sudo apt install matrix-synapse
  297. </code></pre>
  298. <h5 id="downstream-ubuntu-packages"><a class="header" href="#downstream-ubuntu-packages">Downstream Ubuntu packages</a></h5>
  299. <p>We do not recommend using the packages in the default Ubuntu repository
  300. at this time, as they are old and suffer from known security vulnerabilities.
  301. The latest version of Synapse can be installed from <a href="setup/installation.html#matrixorg-packages">our repository</a>.</p>
  302. <h4 id="fedora"><a class="header" href="#fedora">Fedora</a></h4>
  303. <p>Synapse is in the Fedora repositories as <code>matrix-synapse</code>:</p>
  304. <pre><code class="language-sh">sudo dnf install matrix-synapse
  305. </code></pre>
  306. <p>Oleg Girko provides Fedora RPMs at
  307. <a href="https://obs.infoserver.lv/project/monitor/matrix-synapse">https://obs.infoserver.lv/project/monitor/matrix-synapse</a></p>
  308. <h4 id="opensuse"><a class="header" href="#opensuse">OpenSUSE</a></h4>
  309. <p>Synapse is in the OpenSUSE repositories as <code>matrix-synapse</code>:</p>
  310. <pre><code class="language-sh">sudo zypper install matrix-synapse
  311. </code></pre>
  312. <h4 id="suse-linux-enterprise-server"><a class="header" href="#suse-linux-enterprise-server">SUSE Linux Enterprise Server</a></h4>
  313. <p>Unofficial package are built for SLES 15 in the openSUSE:Backports:SLE-15 repository at
  314. <a href="https://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-15/standard/">https://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-15/standard/</a></p>
  315. <h4 id="archlinux"><a class="header" href="#archlinux">ArchLinux</a></h4>
  316. <p>The quickest way to get up and running with ArchLinux is probably with the community package
  317. <a href="https://www.archlinux.org/packages/community/any/matrix-synapse/">https://www.archlinux.org/packages/community/any/matrix-synapse/</a>, which should pull in most of
  318. the necessary dependencies.</p>
  319. <p>pip may be outdated (6.0.7-1 and needs to be upgraded to 6.0.8-1 ):</p>
  320. <pre><code class="language-sh">sudo pip install --upgrade pip
  321. </code></pre>
  322. <p>If you encounter an error with lib bcrypt causing an Wrong ELF Class:
  323. ELFCLASS32 (x64 Systems), you may need to reinstall py-bcrypt to correctly
  324. compile it under the right architecture. (This should not be needed if
  325. installing under virtualenv):</p>
  326. <pre><code class="language-sh">sudo pip uninstall py-bcrypt
  327. sudo pip install py-bcrypt
  328. </code></pre>
  329. <h4 id="void-linux"><a class="header" href="#void-linux">Void Linux</a></h4>
  330. <p>Synapse can be found in the void repositories as 'synapse':</p>
  331. <pre><code class="language-sh">xbps-install -Su
  332. xbps-install -S synapse
  333. </code></pre>
  334. <h4 id="freebsd"><a class="header" href="#freebsd">FreeBSD</a></h4>
  335. <p>Synapse can be installed via FreeBSD Ports or Packages contributed by Brendan Molloy from:</p>
  336. <ul>
  337. <li>Ports: <code>cd /usr/ports/net-im/py-matrix-synapse &amp;&amp; make install clean</code></li>
  338. <li>Packages: <code>pkg install py37-matrix-synapse</code></li>
  339. </ul>
  340. <h4 id="openbsd"><a class="header" href="#openbsd">OpenBSD</a></h4>
  341. <p>As of OpenBSD 6.7 Synapse is available as a pre-compiled binary. The filesystem
  342. underlying the homeserver directory (defaults to <code>/var/synapse</code>) has to be
  343. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>), so creating a separate filesystem
  344. and mounting it to <code>/var/synapse</code> should be taken into consideration.</p>
  345. <p>Installing Synapse:</p>
  346. <pre><code class="language-sh">doas pkg_add synapse
  347. </code></pre>
  348. <h4 id="nixos"><a class="header" href="#nixos">NixOS</a></h4>
  349. <p>Robin Lambertz has packaged Synapse for NixOS at:
  350. <a href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/matrix-synapse.nix">https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/matrix-synapse.nix</a></p>
  351. <h3 id="installing-as-a-python-module-from-pypi"><a class="header" href="#installing-as-a-python-module-from-pypi">Installing as a Python module from PyPI</a></h3>
  352. <p>It's also possible to install Synapse as a Python module from PyPI.</p>
  353. <p>When following this route please make sure that the <a href="setup/installation.html#platform-specific-prerequisites">Platform-specific prerequisites</a> are already installed.</p>
  354. <p>System requirements:</p>
  355. <ul>
  356. <li>POSIX-compliant system (tested on Linux &amp; OS X)</li>
  357. <li>Python 3.6 or later, up to Python 3.9.</li>
  358. <li>At least 1GB of free RAM if you want to join large public rooms like #matrix:matrix.org</li>
  359. </ul>
  360. <p>To install the Synapse homeserver run:</p>
  361. <pre><code class="language-sh">mkdir -p ~/synapse
  362. virtualenv -p python3 ~/synapse/env
  363. source ~/synapse/env/bin/activate
  364. pip install --upgrade pip
  365. pip install --upgrade setuptools
  366. pip install matrix-synapse
  367. </code></pre>
  368. <p>This will download Synapse from <a href="https://pypi.org/project/matrix-synapse">PyPI</a>
  369. and install it, along with the python libraries it uses, into a virtual environment
  370. under <code>~/synapse/env</code>. Feel free to pick a different directory if you
  371. prefer.</p>
  372. <p>This Synapse installation can then be later upgraded by using pip again with the
  373. update flag:</p>
  374. <pre><code class="language-sh">source ~/synapse/env/bin/activate
  375. pip install -U matrix-synapse
  376. </code></pre>
  377. <p>Before you can start Synapse, you will need to generate a configuration
  378. file. To do this, run (in your virtualenv, as before):</p>
  379. <pre><code class="language-sh">cd ~/synapse
  380. python -m synapse.app.homeserver \
  381. --server-name my.domain.name \
  382. --config-path homeserver.yaml \
  383. --generate-config \
  384. --report-stats=[yes|no]
  385. </code></pre>
  386. <p>... substituting an appropriate value for <code>--server-name</code>.</p>
  387. <p>This command will generate you a config file that you can then customise, but it will
  388. also generate a set of keys for you. These keys will allow your homeserver to
  389. identify itself to other homeserver, so don't lose or delete them. It would be
  390. wise to back them up somewhere safe. (If, for whatever reason, you do need to
  391. change your homeserver's keys, you may find that other homeserver have the
  392. old key cached. If you update the signing key, you should change the name of the
  393. key in the <code>&lt;server name&gt;.signing.key</code> file (the second word) to something
  394. different. See the <a href="https://matrix.org/docs/spec/server_server/latest.html#retrieving-server-keys">spec</a> for more information on key management).</p>
  395. <p>To actually run your new homeserver, pick a working directory for Synapse to
  396. run (e.g. <code>~/synapse</code>), and:</p>
  397. <pre><code class="language-sh">cd ~/synapse
  398. source env/bin/activate
  399. synctl start
  400. </code></pre>
  401. <h4 id="platform-specific-prerequisites"><a class="header" href="#platform-specific-prerequisites">Platform-specific prerequisites</a></h4>
  402. <p>Synapse is written in Python but some of the libraries it uses are written in
  403. C. So before we can install Synapse itself we need a working C compiler and the
  404. header files for Python C extensions.</p>
  405. <h5 id="debianubunturaspbian"><a class="header" href="#debianubunturaspbian">Debian/Ubuntu/Raspbian</a></h5>
  406. <p>Installing prerequisites on Ubuntu or Debian:</p>
  407. <pre><code class="language-sh">sudo apt install build-essential python3-dev libffi-dev \
  408. python3-pip python3-setuptools sqlite3 \
  409. libssl-dev virtualenv libjpeg-dev libxslt1-dev
  410. </code></pre>
  411. <h5 id="archlinux-1"><a class="header" href="#archlinux-1">ArchLinux</a></h5>
  412. <p>Installing prerequisites on ArchLinux:</p>
  413. <pre><code class="language-sh">sudo pacman -S base-devel python python-pip \
  414. python-setuptools python-virtualenv sqlite3
  415. </code></pre>
  416. <h5 id="centosfedora"><a class="header" href="#centosfedora">CentOS/Fedora</a></h5>
  417. <p>Installing prerequisites on CentOS or Fedora Linux:</p>
  418. <pre><code class="language-sh">sudo dnf install libtiff-devel libjpeg-devel libzip-devel freetype-devel \
  419. libwebp-devel libxml2-devel libxslt-devel libpq-devel \
  420. python3-virtualenv libffi-devel openssl-devel python3-devel
  421. sudo dnf groupinstall &quot;Development Tools&quot;
  422. </code></pre>
  423. <h5 id="macos"><a class="header" href="#macos">macOS</a></h5>
  424. <p>Installing prerequisites on macOS:</p>
  425. <p>You may need to install the latest Xcode developer tools:</p>
  426. <pre><code class="language-sh">xcode-select --install
  427. </code></pre>
  428. <p>On ARM-based Macs you may need to explicitly install libjpeg which is a pillow dependency. You can use Homebrew (https://brew.sh):</p>
  429. <pre><code class="language-sh"> brew install jpeg
  430. </code></pre>
  431. <p>On macOS Catalina (10.15) you may need to explicitly install OpenSSL
  432. via brew and inform <code>pip</code> about it so that <code>psycopg2</code> builds:</p>
  433. <pre><code class="language-sh">brew install openssl@1.1
  434. export LDFLAGS=&quot;-L/usr/local/opt/openssl/lib&quot;
  435. export CPPFLAGS=&quot;-I/usr/local/opt/openssl/include&quot;
  436. </code></pre>
  437. <h5 id="opensuse-1"><a class="header" href="#opensuse-1">OpenSUSE</a></h5>
  438. <p>Installing prerequisites on openSUSE:</p>
  439. <pre><code class="language-sh">sudo zypper in -t pattern devel_basis
  440. sudo zypper in python-pip python-setuptools sqlite3 python-virtualenv \
  441. python-devel libffi-devel libopenssl-devel libjpeg62-devel
  442. </code></pre>
  443. <h5 id="openbsd-1"><a class="header" href="#openbsd-1">OpenBSD</a></h5>
  444. <p>A port of Synapse is available under <code>net/synapse</code>. The filesystem
  445. underlying the homeserver directory (defaults to <code>/var/synapse</code>) has to be
  446. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>), so creating a separate filesystem
  447. and mounting it to <code>/var/synapse</code> should be taken into consideration.</p>
  448. <p>To be able to build Synapse's dependency on python the <code>WRKOBJDIR</code>
  449. (cf. <code>bsd.port.mk(5)</code>) for building python, too, needs to be on a filesystem
  450. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>).</p>
  451. <p>Creating a <code>WRKOBJDIR</code> for building python under <code>/usr/local</code> (which on a
  452. default OpenBSD installation is mounted with <code>wxallowed</code>):</p>
  453. <pre><code class="language-sh">doas mkdir /usr/local/pobj_wxallowed
  454. </code></pre>
  455. <p>Assuming <code>PORTS_PRIVSEP=Yes</code> (cf. <code>bsd.port.mk(5)</code>) and <code>SUDO=doas</code> are
  456. configured in <code>/etc/mk.conf</code>:</p>
  457. <pre><code class="language-sh">doas chown _pbuild:_pbuild /usr/local/pobj_wxallowed
  458. </code></pre>
  459. <p>Setting the <code>WRKOBJDIR</code> for building python:</p>
  460. <pre><code class="language-sh">echo WRKOBJDIR_lang/python/3.7=/usr/local/pobj_wxallowed \\nWRKOBJDIR_lang/python/2.7=/usr/local/pobj_wxallowed &gt;&gt; /etc/mk.conf
  461. </code></pre>
  462. <p>Building Synapse:</p>
  463. <pre><code class="language-sh">cd /usr/ports/net/synapse
  464. make install
  465. </code></pre>
  466. <h5 id="windows"><a class="header" href="#windows">Windows</a></h5>
  467. <p>Running Synapse natively on Windows is not officially supported.</p>
  468. <p>If you wish to run or develop Synapse on Windows, the Windows Subsystem for
  469. Linux provides a Linux environment which is capable of using the Debian, Fedora,
  470. or source installation methods. More information about WSL can be found at
  471. <a href="https://docs.microsoft.com/en-us/windows/wsl/install">https://docs.microsoft.com/en-us/windows/wsl/install</a> for Windows 10/11 and
  472. <a href="https://docs.microsoft.com/en-us/windows/wsl/install-on-server">https://docs.microsoft.com/en-us/windows/wsl/install-on-server</a> for
  473. Windows Server.</p>
  474. <h2 id="setting-up-synapse"><a class="header" href="#setting-up-synapse">Setting up Synapse</a></h2>
  475. <p>Once you have installed synapse as above, you will need to configure it.</p>
  476. <h3 id="using-postgresql"><a class="header" href="#using-postgresql">Using PostgreSQL</a></h3>
  477. <p>By default Synapse uses an <a href="https://sqlite.org/">SQLite</a> database and in doing so trades
  478. performance for convenience. Almost all installations should opt to use <a href="https://www.postgresql.org">PostgreSQL</a>
  479. instead. Advantages include:</p>
  480. <ul>
  481. <li>significant performance improvements due to the superior threading and
  482. caching model, smarter query optimiser</li>
  483. <li>allowing the DB to be run on separate hardware</li>
  484. </ul>
  485. <p>For information on how to install and use PostgreSQL in Synapse, please see
  486. <a href="setup/../postgres.html">Using Postgres</a></p>
  487. <p>SQLite is only acceptable for testing purposes. SQLite should not be used in
  488. a production server. Synapse will perform poorly when using
  489. SQLite, especially when participating in large rooms.</p>
  490. <h3 id="tls-certificates"><a class="header" href="#tls-certificates">TLS certificates</a></h3>
  491. <p>The default configuration exposes a single HTTP port on the local
  492. interface: <code>http://localhost:8008</code>. It is suitable for local testing,
  493. but for any practical use, you will need Synapse's APIs to be served
  494. over HTTPS.</p>
  495. <p>The recommended way to do so is to set up a reverse proxy on port
  496. <code>8448</code>. You can find documentation on doing so in
  497. <a href="setup/../reverse_proxy.html">the reverse proxy documentation</a>.</p>
  498. <p>Alternatively, you can configure Synapse to expose an HTTPS port. To do
  499. so, you will need to edit <code>homeserver.yaml</code>, as follows:</p>
  500. <ul>
  501. <li>First, under the <code>listeners</code> section, uncomment the configuration for the
  502. TLS-enabled listener. (Remove the hash sign (<code>#</code>) at the start of
  503. each line). The relevant lines are like this:</li>
  504. </ul>
  505. <pre><code class="language-yaml"> - port: 8448
  506. type: http
  507. tls: true
  508. resources:
  509. - names: [client, federation]
  510. </code></pre>
  511. <ul>
  512. <li>
  513. <p>You will also need to uncomment the <code>tls_certificate_path</code> and
  514. <code>tls_private_key_path</code> lines under the <code>TLS</code> section. You will need to manage
  515. provisioning of these certificates yourself.</p>
  516. <p>If you are using your own certificate, be sure to use a <code>.pem</code> file that
  517. includes the full certificate chain including any intermediate certificates
  518. (for instance, if using certbot, use <code>fullchain.pem</code> as your certificate, not
  519. <code>cert.pem</code>).</p>
  520. </li>
  521. </ul>
  522. <p>For a more detailed guide to configuring your server for federation, see
  523. <a href="setup/../federate.html">Federation</a>.</p>
  524. <h3 id="client-well-known-uri"><a class="header" href="#client-well-known-uri">Client Well-Known URI</a></h3>
  525. <p>Setting up the client Well-Known URI is optional but if you set it up, it will
  526. allow users to enter their full username (e.g. <code>@user:&lt;server_name&gt;</code>) into clients
  527. which support well-known lookup to automatically configure the homeserver and
  528. identity server URLs. This is useful so that users don't have to memorize or think
  529. about the actual homeserver URL you are using.</p>
  530. <p>The URL <code>https://&lt;server_name&gt;/.well-known/matrix/client</code> should return JSON in
  531. the following format.</p>
  532. <pre><code class="language-json">{
  533. &quot;m.homeserver&quot;: {
  534. &quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;
  535. }
  536. }
  537. </code></pre>
  538. <p>It can optionally contain identity server information as well.</p>
  539. <pre><code class="language-json">{
  540. &quot;m.homeserver&quot;: {
  541. &quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;
  542. },
  543. &quot;m.identity_server&quot;: {
  544. &quot;base_url&quot;: &quot;https://&lt;identity.example.com&gt;&quot;
  545. }
  546. }
  547. </code></pre>
  548. <p>To work in browser based clients, the file must be served with the appropriate
  549. Cross-Origin Resource Sharing (CORS) headers. A recommended value would be
  550. <code>Access-Control-Allow-Origin: *</code> which would allow all browser based clients to
  551. view it.</p>
  552. <p>In nginx this would be something like:</p>
  553. <pre><code class="language-nginx">location /.well-known/matrix/client {
  554. return 200 '{&quot;m.homeserver&quot;: {&quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;}}';
  555. default_type application/json;
  556. add_header Access-Control-Allow-Origin *;
  557. }
  558. </code></pre>
  559. <p>You should also ensure the <code>public_baseurl</code> option in <code>homeserver.yaml</code> is set
  560. correctly. <code>public_baseurl</code> should be set to the URL that clients will use to
  561. connect to your server. This is the same URL you put for the <code>m.homeserver</code>
  562. <code>base_url</code> above.</p>
  563. <pre><code class="language-yaml">public_baseurl: &quot;https://&lt;matrix.example.com&gt;&quot;
  564. </code></pre>
  565. <h3 id="email"><a class="header" href="#email">Email</a></h3>
  566. <p>It is desirable for Synapse to have the capability to send email. This allows
  567. Synapse to send password reset emails, send verifications when an email address
  568. is added to a user's account, and send email notifications to users when they
  569. receive new messages.</p>
  570. <p>To configure an SMTP server for Synapse, modify the configuration section
  571. headed <code>email</code>, and be sure to have at least the <code>smtp_host</code>, <code>smtp_port</code>
  572. and <code>notif_from</code> fields filled out. You may also need to set <code>smtp_user</code>,
  573. <code>smtp_pass</code>, and <code>require_transport_security</code>.</p>
  574. <p>If email is not configured, password reset, registration and notifications via
  575. email will be disabled.</p>
  576. <h3 id="registering-a-user"><a class="header" href="#registering-a-user">Registering a user</a></h3>
  577. <p>The easiest way to create a new user is to do so from a client like <a href="https://element.io/">Element</a>.</p>
  578. <p>Alternatively, you can do so from the command line. This can be done as follows:</p>
  579. <ol>
  580. <li>If synapse was installed via pip, activate the virtualenv as follows (if Synapse was
  581. installed via a prebuilt package, <code>register_new_matrix_user</code> should already be
  582. on the search path):
  583. <pre><code class="language-sh">cd ~/synapse
  584. source env/bin/activate
  585. synctl start # if not already running
  586. </code></pre>
  587. </li>
  588. <li>Run the following command:
  589. <pre><code class="language-sh">register_new_matrix_user -c homeserver.yaml http://localhost:8008
  590. </code></pre>
  591. </li>
  592. </ol>
  593. <p>This will prompt you to add details for the new user, and will then connect to
  594. the running Synapse to create the new user. For example:</p>
  595. <pre><code>New user localpart: erikj
  596. Password:
  597. Confirm password:
  598. Make admin [no]:
  599. Success!
  600. </code></pre>
  601. <p>This process uses a setting <code>registration_shared_secret</code> in
  602. <code>homeserver.yaml</code>, which is shared between Synapse itself and the
  603. <code>register_new_matrix_user</code> script. It doesn't matter what it is (a random
  604. value is generated by <code>--generate-config</code>), but it should be kept secret, as
  605. anyone with knowledge of it can register users, including admin accounts,
  606. on your server even if <code>enable_registration</code> is <code>false</code>.</p>
  607. <h3 id="setting-up-a-turn-server"><a class="header" href="#setting-up-a-turn-server">Setting up a TURN server</a></h3>
  608. <p>For reliable VoIP calls to be routed via this homeserver, you MUST configure
  609. a TURN server. See <a href="setup/../turn-howto.html">TURN setup</a> for details.</p>
  610. <h3 id="url-previews"><a class="header" href="#url-previews">URL previews</a></h3>
  611. <p>Synapse includes support for previewing URLs, which is disabled by default. To
  612. turn it on you must enable the <code>url_preview_enabled: True</code> config parameter
  613. and explicitly specify the IP ranges that Synapse is not allowed to spider for
  614. previewing in the <code>url_preview_ip_range_blacklist</code> configuration parameter.
  615. This is critical from a security perspective to stop arbitrary Matrix users
  616. spidering 'internal' URLs on your network. At the very least we recommend that
  617. your loopback and RFC1918 IP addresses are blacklisted.</p>
  618. <p>This also requires the optional <code>lxml</code> python dependency to be installed. This
  619. in turn requires the <code>libxml2</code> library to be available - on Debian/Ubuntu this
  620. means <code>apt-get install libxml2-dev</code>, or equivalent for your OS.</p>
  621. <h3 id="troubleshooting-installation"><a class="header" href="#troubleshooting-installation">Troubleshooting Installation</a></h3>
  622. <p><code>pip</code> seems to leak <em>lots</em> of memory during installation. For instance, a Linux
  623. host with 512MB of RAM may run out of memory whilst installing Twisted. If this
  624. happens, you will have to individually install the dependencies which are
  625. failing, e.g.:</p>
  626. <pre><code class="language-sh">pip install twisted
  627. </code></pre>
  628. <p>If you have any other problems, feel free to ask in
  629. <a href="https://matrix.to/#/#synapse:matrix.org">#synapse:matrix.org</a>.</p>
  630. <div style="break-before: page; page-break-before: always;"></div><h1 id="using-postgres"><a class="header" href="#using-postgres">Using Postgres</a></h1>
  631. <p>Synapse supports PostgreSQL versions 9.6 or later.</p>
  632. <h2 id="install-postgres-client-libraries"><a class="header" href="#install-postgres-client-libraries">Install postgres client libraries</a></h2>
  633. <p>Synapse will require the python postgres client library in order to
  634. connect to a postgres database.</p>
  635. <ul>
  636. <li>
  637. <p>If you are using the <a href="setup/installation.html#matrixorg-packages">matrix.org debian/ubuntu
  638. packages</a>, the necessary python
  639. library will already be installed, but you will need to ensure the
  640. low-level postgres library is installed, which you can do with
  641. <code>apt install libpq5</code>.</p>
  642. </li>
  643. <li>
  644. <p>For other pre-built packages, please consult the documentation from
  645. the relevant package.</p>
  646. </li>
  647. <li>
  648. <p>If you installed synapse <a href="setup/installation.html#installing-from-source">in a
  649. virtualenv</a>, you can install
  650. the library with:</p>
  651. <pre><code>~/synapse/env/bin/pip install &quot;matrix-synapse[postgres]&quot;
  652. </code></pre>
  653. <p>(substituting the path to your virtualenv for <code>~/synapse/env</code>, if
  654. you used a different path). You will require the postgres
  655. development files. These are in the <code>libpq-dev</code> package on
  656. Debian-derived distributions.</p>
  657. </li>
  658. </ul>
  659. <h2 id="set-up-database"><a class="header" href="#set-up-database">Set up database</a></h2>
  660. <p>Assuming your PostgreSQL database user is called <code>postgres</code>, first authenticate as the database user with:</p>
  661. <pre><code class="language-sh">su - postgres
  662. # Or, if your system uses sudo to get administrative rights
  663. sudo -u postgres bash
  664. </code></pre>
  665. <p>Then, create a postgres user and a database with:</p>
  666. <pre><code class="language-sh"># this will prompt for a password for the new user
  667. createuser --pwprompt synapse_user
  668. createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
  669. </code></pre>
  670. <p>The above will create a user called <code>synapse_user</code>, and a database called
  671. <code>synapse</code>.</p>
  672. <p>Note that the PostgreSQL database <em>must</em> have the correct encoding set
  673. (as shown above), otherwise it will not be able to store UTF8 strings.</p>
  674. <p>You may need to enable password authentication so <code>synapse_user</code> can
  675. connect to the database. See
  676. <a href="https://www.postgresql.org/docs/current/auth-pg-hba-conf.html">https://www.postgresql.org/docs/current/auth-pg-hba-conf.html</a>.</p>
  677. <h2 id="synapse-config"><a class="header" href="#synapse-config">Synapse config</a></h2>
  678. <p>When you are ready to start using PostgreSQL, edit the <code>database</code>
  679. section in your config file to match the following lines:</p>
  680. <pre><code class="language-yaml">database:
  681. name: psycopg2
  682. args:
  683. user: &lt;user&gt;
  684. password: &lt;pass&gt;
  685. database: &lt;db&gt;
  686. host: &lt;host&gt;
  687. cp_min: 5
  688. cp_max: 10
  689. </code></pre>
  690. <p>All key, values in <code>args</code> are passed to the <code>psycopg2.connect(..)</code>
  691. function, except keys beginning with <code>cp_</code>, which are consumed by the
  692. twisted adbapi connection pool. See the <a href="https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS">libpq
  693. documentation</a>
  694. for a list of options which can be passed.</p>
  695. <p>You should consider tuning the <code>args.keepalives_*</code> options if there is any danger of
  696. the connection between your homeserver and database dropping, otherwise Synapse
  697. may block for an extended period while it waits for a response from the
  698. database server. Example values might be:</p>
  699. <pre><code class="language-yaml">database:
  700. args:
  701. # ... as above
  702. # seconds of inactivity after which TCP should send a keepalive message to the server
  703. keepalives_idle: 10
  704. # the number of seconds after which a TCP keepalive message that is not
  705. # acknowledged by the server should be retransmitted
  706. keepalives_interval: 10
  707. # the number of TCP keepalives that can be lost before the client's connection
  708. # to the server is considered dead
  709. keepalives_count: 3
  710. </code></pre>
  711. <h2 id="tuning-postgres"><a class="header" href="#tuning-postgres">Tuning Postgres</a></h2>
  712. <p>The default settings should be fine for most deployments. For larger
  713. scale deployments tuning some of the settings is recommended, details of
  714. which can be found at
  715. <a href="https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server">https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server</a>.</p>
  716. <p>In particular, we've found tuning the following values helpful for
  717. performance:</p>
  718. <ul>
  719. <li><code>shared_buffers</code></li>
  720. <li><code>effective_cache_size</code></li>
  721. <li><code>work_mem</code></li>
  722. <li><code>maintenance_work_mem</code></li>
  723. <li><code>autovacuum_work_mem</code></li>
  724. </ul>
  725. <p>Note that the appropriate values for those fields depend on the amount
  726. of free memory the database host has available.</p>
  727. <h2 id="porting-from-sqlite"><a class="header" href="#porting-from-sqlite">Porting from SQLite</a></h2>
  728. <h3 id="overview"><a class="header" href="#overview">Overview</a></h3>
  729. <p>The script <code>synapse_port_db</code> allows porting an existing synapse server
  730. backed by SQLite to using PostgreSQL. This is done in as a two phase
  731. process:</p>
  732. <ol>
  733. <li>Copy the existing SQLite database to a separate location and run
  734. the port script against that offline database.</li>
  735. <li>Shut down the server. Rerun the port script to port any data that
  736. has come in since taking the first snapshot. Restart server against
  737. the PostgreSQL database.</li>
  738. </ol>
  739. <p>The port script is designed to be run repeatedly against newer snapshots
  740. of the SQLite database file. This makes it safe to repeat step 1 if
  741. there was a delay between taking the previous snapshot and being ready
  742. to do step 2.</p>
  743. <p>It is safe to at any time kill the port script and restart it.</p>
  744. <p>Note that the database may take up significantly more (25% - 100% more)
  745. space on disk after porting to Postgres.</p>
  746. <h3 id="using-the-port-script"><a class="header" href="#using-the-port-script">Using the port script</a></h3>
  747. <p>Firstly, shut down the currently running synapse server and copy its
  748. database file (typically <code>homeserver.db</code>) to another location. Once the
  749. copy is complete, restart synapse. For instance:</p>
  750. <pre><code class="language-sh">./synctl stop
  751. cp homeserver.db homeserver.db.snapshot
  752. ./synctl start
  753. </code></pre>
  754. <p>Copy the old config file into a new config file:</p>
  755. <pre><code class="language-sh">cp homeserver.yaml homeserver-postgres.yaml
  756. </code></pre>
  757. <p>Edit the database section as described in the section <em>Synapse config</em>
  758. above and with the SQLite snapshot located at <code>homeserver.db.snapshot</code>
  759. simply run:</p>
  760. <pre><code class="language-sh">synapse_port_db --sqlite-database homeserver.db.snapshot \
  761. --postgres-config homeserver-postgres.yaml
  762. </code></pre>
  763. <p>The flag <code>--curses</code> displays a coloured curses progress UI.</p>
  764. <p>If the script took a long time to complete, or time has otherwise passed
  765. since the original snapshot was taken, repeat the previous steps with a
  766. newer snapshot.</p>
  767. <p>To complete the conversion shut down the synapse server and run the port
  768. script one last time, e.g. if the SQLite database is at <code>homeserver.db</code>
  769. run:</p>
  770. <pre><code class="language-sh">synapse_port_db --sqlite-database homeserver.db \
  771. --postgres-config homeserver-postgres.yaml
  772. </code></pre>
  773. <p>Once that has completed, change the synapse config to point at the
  774. PostgreSQL database configuration file <code>homeserver-postgres.yaml</code>:</p>
  775. <pre><code class="language-sh">./synctl stop
  776. mv homeserver.yaml homeserver-old-sqlite.yaml
  777. mv homeserver-postgres.yaml homeserver.yaml
  778. ./synctl start
  779. </code></pre>
  780. <p>Synapse should now be running against PostgreSQL.</p>
  781. <h2 id="troubleshooting"><a class="header" href="#troubleshooting">Troubleshooting</a></h2>
  782. <h3 id="alternative-auth-methods"><a class="header" href="#alternative-auth-methods">Alternative auth methods</a></h3>
  783. <p>If you get an error along the lines of <code>FATAL: Ident authentication failed for user &quot;synapse_user&quot;</code>, you may need to use an authentication method other than
  784. <code>ident</code>:</p>
  785. <ul>
  786. <li>
  787. <p>If the <code>synapse_user</code> user has a password, add the password to the <code>database:</code>
  788. section of <code>homeserver.yaml</code>. Then add the following to <code>pg_hba.conf</code>:</p>
  789. <pre><code>host synapse synapse_user ::1/128 md5 # or `scram-sha-256` instead of `md5` if you use that
  790. </code></pre>
  791. </li>
  792. <li>
  793. <p>If the <code>synapse_user</code> user does not have a password, then a password doesn't
  794. have to be added to <code>homeserver.yaml</code>. But the following does need to be added
  795. to <code>pg_hba.conf</code>:</p>
  796. <pre><code>host synapse synapse_user ::1/128 trust
  797. </code></pre>
  798. </li>
  799. </ul>
  800. <p>Note that line order matters in <code>pg_hba.conf</code>, so make sure that if you do add a
  801. new line, it is inserted before:</p>
  802. <pre><code>host all all ::1/128 ident
  803. </code></pre>
  804. <h3 id="fixing-incorrect-collate-or-ctype"><a class="header" href="#fixing-incorrect-collate-or-ctype">Fixing incorrect <code>COLLATE</code> or <code>CTYPE</code></a></h3>
  805. <p>Synapse will refuse to set up a new database if it has the wrong values of
  806. <code>COLLATE</code> and <code>CTYPE</code> set, and will log warnings on existing databases. Using
  807. different locales can cause issues if the locale library is updated from
  808. underneath the database, or if a different version of the locale is used on any
  809. replicas.</p>
  810. <p>The safest way to fix the issue is to dump the database and recreate it with
  811. the correct locale parameter (as shown above). It is also possible to change the
  812. parameters on a live database and run a <code>REINDEX</code> on the entire database,
  813. however extreme care must be taken to avoid database corruption.</p>
  814. <p>Note that the above may fail with an error about duplicate rows if corruption
  815. has already occurred, and such duplicate rows will need to be manually removed.</p>
  816. <h3 id="fixing-inconsistent-sequences-error"><a class="header" href="#fixing-inconsistent-sequences-error">Fixing inconsistent sequences error</a></h3>
  817. <p>Synapse uses Postgres sequences to generate IDs for various tables. A sequence
  818. and associated table can get out of sync if, for example, Synapse has been
  819. downgraded and then upgraded again.</p>
  820. <p>To fix the issue shut down Synapse (including any and all workers) and run the
  821. SQL command included in the error message. Once done Synapse should start
  822. successfully.</p>
  823. <div style="break-before: page; page-break-before: always;"></div><h1 id="using-a-reverse-proxy-with-synapse"><a class="header" href="#using-a-reverse-proxy-with-synapse">Using a reverse proxy with Synapse</a></h1>
  824. <p>It is recommended to put a reverse proxy such as
  825. <a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html">nginx</a>,
  826. <a href="https://httpd.apache.org/docs/current/mod/mod_proxy_http.html">Apache</a>,
  827. <a href="https://caddyserver.com/docs/quick-starts/reverse-proxy">Caddy</a>,
  828. <a href="https://www.haproxy.org/">HAProxy</a> or
  829. <a href="https://man.openbsd.org/relayd.8">relayd</a> in front of Synapse. One advantage
  830. of doing so is that it means that you can expose the default https port
  831. (443) to Matrix clients without needing to run Synapse with root
  832. privileges.</p>
  833. <p>You should configure your reverse proxy to forward requests to <code>/_matrix</code> or
  834. <code>/_synapse/client</code> to Synapse, and have it set the <code>X-Forwarded-For</code> and
  835. <code>X-Forwarded-Proto</code> request headers.</p>
  836. <p>You should remember that Matrix clients and other Matrix servers do not
  837. necessarily need to connect to your server via the same server name or
  838. port. Indeed, clients will use port 443 by default, whereas servers default to
  839. port 8448. Where these are different, we refer to the 'client port' and the
  840. 'federation port'. See <a href="https://matrix.org/docs/spec/server_server/latest#resolving-server-names">the Matrix
  841. specification</a>
  842. for more details of the algorithm used for federation connections, and
  843. <a href="delegate.html">Delegation</a> for instructions on setting up delegation.</p>
  844. <p><strong>NOTE</strong>: Your reverse proxy must not <code>canonicalise</code> or <code>normalise</code>
  845. the requested URI in any way (for example, by decoding <code>%xx</code> escapes).
  846. Beware that Apache <em>will</em> canonicalise URIs unless you specify
  847. <code>nocanon</code>.</p>
  848. <p>Let's assume that we expect clients to connect to our server at
  849. <code>https://matrix.example.com</code>, and other servers to connect at
  850. <code>https://example.com:8448</code>. The following sections detail the configuration of
  851. the reverse proxy and the homeserver.</p>
  852. <h2 id="homeserver-configuration"><a class="header" href="#homeserver-configuration">Homeserver Configuration</a></h2>
  853. <p>The HTTP configuration will need to be updated for Synapse to correctly record
  854. client IP addresses and generate redirect URLs while behind a reverse proxy. </p>
  855. <p>In <code>homeserver.yaml</code> set <code>x_forwarded: true</code> in the port 8008 section and
  856. consider setting <code>bind_addresses: ['127.0.0.1']</code> so that the server only
  857. listens to traffic on localhost. (Do not change <code>bind_addresses</code> to <code>127.0.0.1</code>
  858. when using a containerized Synapse, as that will prevent it from responding
  859. to proxied traffic.)</p>
  860. <h2 id="reverse-proxy-configuration-examples"><a class="header" href="#reverse-proxy-configuration-examples">Reverse-proxy configuration examples</a></h2>
  861. <p><strong>NOTE</strong>: You only need one of these.</p>
  862. <h3 id="nginx"><a class="header" href="#nginx">nginx</a></h3>
  863. <pre><code class="language-nginx">server {
  864. listen 443 ssl http2;
  865. listen [::]:443 ssl http2;
  866. # For the federation port
  867. listen 8448 ssl http2 default_server;
  868. listen [::]:8448 ssl http2 default_server;
  869. server_name matrix.example.com;
  870. location ~* ^(\/_matrix|\/_synapse\/client) {
  871. # note: do not add a path (even a single /) after the port in `proxy_pass`,
  872. # otherwise nginx will canonicalise the URI and cause signature verification
  873. # errors.
  874. proxy_pass http://localhost:8008;
  875. proxy_set_header X-Forwarded-For $remote_addr;
  876. proxy_set_header X-Forwarded-Proto $scheme;
  877. proxy_set_header Host $host;
  878. # Nginx by default only allows file uploads up to 1M in size
  879. # Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
  880. client_max_body_size 50M;
  881. }
  882. }
  883. </code></pre>
  884. <h3 id="caddy-v1"><a class="header" href="#caddy-v1">Caddy v1</a></h3>
  885. <pre><code>matrix.example.com {
  886. proxy /_matrix http://localhost:8008 {
  887. transparent
  888. }
  889. proxy /_synapse/client http://localhost:8008 {
  890. transparent
  891. }
  892. }
  893. example.com:8448 {
  894. proxy / http://localhost:8008 {
  895. transparent
  896. }
  897. }
  898. </code></pre>
  899. <h3 id="caddy-v2"><a class="header" href="#caddy-v2">Caddy v2</a></h3>
  900. <pre><code>matrix.example.com {
  901. reverse_proxy /_matrix/* http://localhost:8008
  902. reverse_proxy /_synapse/client/* http://localhost:8008
  903. }
  904. example.com:8448 {
  905. reverse_proxy http://localhost:8008
  906. }
  907. </code></pre>
  908. <p><a href="delegate.html">Delegation</a> example:</p>
  909. <pre><code>(matrix-well-known-header) {
  910. # Headers
  911. header Access-Control-Allow-Origin &quot;*&quot;
  912. header Access-Control-Allow-Methods &quot;GET, POST, PUT, DELETE, OPTIONS&quot;
  913. header Access-Control-Allow-Headers &quot;Origin, X-Requested-With, Content-Type, Accept, Authorization&quot;
  914. header Content-Type &quot;application/json&quot;
  915. }
  916. example.com {
  917. handle /.well-known/matrix/server {
  918. import matrix-well-known-header
  919. respond `{&quot;m.server&quot;:&quot;matrix.example.com:443&quot;}`
  920. }
  921. handle /.well-known/matrix/client {
  922. import matrix-well-known-header
  923. respond `{&quot;m.homeserver&quot;:{&quot;base_url&quot;:&quot;https://matrix.example.com&quot;},&quot;m.identity_server&quot;:{&quot;base_url&quot;:&quot;https://identity.example.com&quot;}}`
  924. }
  925. }
  926. matrix.example.com {
  927. reverse_proxy /_matrix/* http://localhost:8008
  928. reverse_proxy /_synapse/client/* http://localhost:8008
  929. }
  930. </code></pre>
  931. <h3 id="apache"><a class="header" href="#apache">Apache</a></h3>
  932. <pre><code class="language-apache">&lt;VirtualHost *:443&gt;
  933. SSLEngine on
  934. ServerName matrix.example.com
  935. RequestHeader set &quot;X-Forwarded-Proto&quot; expr=%{REQUEST_SCHEME}
  936. AllowEncodedSlashes NoDecode
  937. ProxyPreserveHost on
  938. ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
  939. ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
  940. ProxyPass /_synapse/client http://127.0.0.1:8008/_synapse/client nocanon
  941. ProxyPassReverse /_synapse/client http://127.0.0.1:8008/_synapse/client
  942. &lt;/VirtualHost&gt;
  943. &lt;VirtualHost *:8448&gt;
  944. SSLEngine on
  945. ServerName example.com
  946. RequestHeader set &quot;X-Forwarded-Proto&quot; expr=%{REQUEST_SCHEME}
  947. AllowEncodedSlashes NoDecode
  948. ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
  949. ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
  950. &lt;/VirtualHost&gt;
  951. </code></pre>
  952. <p><strong>NOTE</strong>: ensure the <code>nocanon</code> options are included.</p>
  953. <p><strong>NOTE 2</strong>: It appears that Synapse is currently incompatible with the ModSecurity module for Apache (<code>mod_security2</code>). If you need it enabled for other services on your web server, you can disable it for Synapse's two VirtualHosts by including the following lines before each of the two <code>&lt;/VirtualHost&gt;</code> above:</p>
  954. <pre><code class="language-apache">&lt;IfModule security2_module&gt;
  955. SecRuleEngine off
  956. &lt;/IfModule&gt;
  957. </code></pre>
  958. <p><strong>NOTE 3</strong>: Missing <code>ProxyPreserveHost on</code> can lead to a redirect loop.</p>
  959. <h3 id="haproxy"><a class="header" href="#haproxy">HAProxy</a></h3>
  960. <pre><code>frontend https
  961. bind :::443 v4v6 ssl crt /etc/ssl/haproxy/ strict-sni alpn h2,http/1.1
  962. http-request set-header X-Forwarded-Proto https if { ssl_fc }
  963. http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  964. http-request set-header X-Forwarded-For %[src]
  965. # Matrix client traffic
  966. acl matrix-host hdr(host) -i matrix.example.com matrix.example.com:443
  967. acl matrix-path path_beg /_matrix
  968. acl matrix-path path_beg /_synapse/client
  969. use_backend matrix if matrix-host matrix-path
  970. frontend matrix-federation
  971. bind :::8448 v4v6 ssl crt /etc/ssl/haproxy/synapse.pem alpn h2,http/1.1
  972. http-request set-header X-Forwarded-Proto https if { ssl_fc }
  973. http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  974. http-request set-header X-Forwarded-For %[src]
  975. default_backend matrix
  976. backend matrix
  977. server matrix 127.0.0.1:8008
  978. </code></pre>
  979. <h3 id="relayd"><a class="header" href="#relayd">Relayd</a></h3>
  980. <pre><code>table &lt;webserver&gt; { 127.0.0.1 }
  981. table &lt;matrixserver&gt; { 127.0.0.1 }
  982. http protocol &quot;https&quot; {
  983. tls { no tlsv1.0, ciphers &quot;HIGH&quot; }
  984. tls keypair &quot;example.com&quot;
  985. match header set &quot;X-Forwarded-For&quot; value &quot;$REMOTE_ADDR&quot;
  986. match header set &quot;X-Forwarded-Proto&quot; value &quot;https&quot;
  987. # set CORS header for .well-known/matrix/server, .well-known/matrix/client
  988. # httpd does not support setting headers, so do it here
  989. match request path &quot;/.well-known/matrix/*&quot; tag &quot;matrix-cors&quot;
  990. match response tagged &quot;matrix-cors&quot; header set &quot;Access-Control-Allow-Origin&quot; value &quot;*&quot;
  991. pass quick path &quot;/_matrix/*&quot; forward to &lt;matrixserver&gt;
  992. pass quick path &quot;/_synapse/client/*&quot; forward to &lt;matrixserver&gt;
  993. # pass on non-matrix traffic to webserver
  994. pass forward to &lt;webserver&gt;
  995. }
  996. relay &quot;https_traffic&quot; {
  997. listen on egress port 443 tls
  998. protocol &quot;https&quot;
  999. forward to &lt;matrixserver&gt; port 8008 check tcp
  1000. forward to &lt;webserver&gt; port 8080 check tcp
  1001. }
  1002. http protocol &quot;matrix&quot; {
  1003. tls { no tlsv1.0, ciphers &quot;HIGH&quot; }
  1004. tls keypair &quot;example.com&quot;
  1005. block
  1006. pass quick path &quot;/_matrix/*&quot; forward to &lt;matrixserver&gt;
  1007. pass quick path &quot;/_synapse/client/*&quot; forward to &lt;matrixserver&gt;
  1008. }
  1009. relay &quot;matrix_federation&quot; {
  1010. listen on egress port 8448 tls
  1011. protocol &quot;matrix&quot;
  1012. forward to &lt;matrixserver&gt; port 8008 check tcp
  1013. }
  1014. </code></pre>
  1015. <h2 id="health-check-endpoint"><a class="header" href="#health-check-endpoint">Health check endpoint</a></h2>
  1016. <p>Synapse exposes a health check endpoint for use by reverse proxies.
  1017. Each configured HTTP listener has a <code>/health</code> endpoint which always returns
  1018. 200 OK (and doesn't get logged).</p>
  1019. <h2 id="synapse-administration-endpoints"><a class="header" href="#synapse-administration-endpoints">Synapse administration endpoints</a></h2>
  1020. <p>Endpoints for administering your Synapse instance are placed under
  1021. <code>/_synapse/admin</code>. These require authentication through an access token of an
  1022. admin user. However as access to these endpoints grants the caller a lot of power,
  1023. we do not recommend exposing them to the public internet without good reason.</p>
  1024. <div style="break-before: page; page-break-before: always;"></div><h1 id="using-a-forward-proxy-with-synapse"><a class="header" href="#using-a-forward-proxy-with-synapse">Using a forward proxy with Synapse</a></h1>
  1025. <p>You can use Synapse with a forward or outbound proxy. An example of when
  1026. this is necessary is in corporate environments behind a DMZ (demilitarized zone).
  1027. Synapse supports routing outbound HTTP(S) requests via a proxy. Only HTTP(S)
  1028. proxy is supported, not SOCKS proxy or anything else.</p>
  1029. <h2 id="configure"><a class="header" href="#configure">Configure</a></h2>
  1030. <p>The <code>http_proxy</code>, <code>https_proxy</code>, <code>no_proxy</code> environment variables are used to
  1031. specify proxy settings. The environment variable is not case sensitive.</p>
  1032. <ul>
  1033. <li><code>http_proxy</code>: Proxy server to use for HTTP requests.</li>
  1034. <li><code>https_proxy</code>: Proxy server to use for HTTPS requests.</li>
  1035. <li><code>no_proxy</code>: Comma-separated list of hosts, IP addresses, or IP ranges in CIDR
  1036. format which should not use the proxy. Synapse will directly connect to these hosts.</li>
  1037. </ul>
  1038. <p>The <code>http_proxy</code> and <code>https_proxy</code> environment variables have the form: <code>[scheme://][&lt;username&gt;:&lt;password&gt;@]&lt;host&gt;[:&lt;port&gt;]</code></p>
  1039. <ul>
  1040. <li>
  1041. <p>Supported schemes are <code>http://</code> and <code>https://</code>. The default scheme is <code>http://</code>
  1042. for compatibility reasons; it is recommended to set a scheme. If scheme is set
  1043. to <code>https://</code> the connection uses TLS between Synapse and the proxy.</p>
  1044. <p><strong>NOTE</strong>: Synapse validates the certificates. If the certificate is not
  1045. valid, then the connection is dropped.</p>
  1046. </li>
  1047. <li>
  1048. <p>Default port if not given is <code>1080</code>.</p>
  1049. </li>
  1050. <li>
  1051. <p>Username and password are optional and will be used to authenticate against
  1052. the proxy.</p>
  1053. </li>
  1054. </ul>
  1055. <p><strong>Examples</strong></p>
  1056. <ul>
  1057. <li>HTTP_PROXY=http://USERNAME:PASSWORD@10.0.1.1:8080/</li>
  1058. <li>HTTPS_PROXY=http://USERNAME:PASSWORD@proxy.example.com:8080/</li>
  1059. <li>NO_PROXY=master.hostname.example.com,10.1.0.0/16,172.30.0.0/16</li>
  1060. </ul>
  1061. <p><strong>NOTE</strong>:
  1062. Synapse does not apply the IP blacklist to connections through the proxy (since
  1063. the DNS resolution is done by the proxy). It is expected that the proxy or firewall
  1064. will apply blacklisting of IP addresses.</p>
  1065. <h2 id="connection-types"><a class="header" href="#connection-types">Connection types</a></h2>
  1066. <p>The proxy will be <strong>used</strong> for:</p>
  1067. <ul>
  1068. <li>push</li>
  1069. <li>url previews</li>
  1070. <li>phone-home stats</li>
  1071. <li>recaptcha validation</li>
  1072. <li>CAS auth validation</li>
  1073. <li>OpenID Connect</li>
  1074. <li>Outbound federation</li>
  1075. <li>Federation (checking public key revocation)</li>
  1076. <li>Fetching public keys of other servers</li>
  1077. <li>Downloading remote media</li>
  1078. </ul>
  1079. <p>It will <strong>not be used</strong> for:</p>
  1080. <ul>
  1081. <li>Application Services</li>
  1082. <li>Identity servers</li>
  1083. <li>In worker configurations
  1084. <ul>
  1085. <li>connections between workers</li>
  1086. <li>connections from workers to Redis</li>
  1087. </ul>
  1088. </li>
  1089. </ul>
  1090. <h2 id="troubleshooting-1"><a class="header" href="#troubleshooting-1">Troubleshooting</a></h2>
  1091. <p>If a proxy server is used with TLS (HTTPS) and no connections are established,
  1092. it is most likely due to the proxy's certificates. To test this, the validation
  1093. in Synapse can be deactivated.</p>
  1094. <p><strong>NOTE</strong>: This has an impact on security and is for testing purposes only!</p>
  1095. <p>To deactivate the certificate validation, the following setting must be made in
  1096. <a href="setup/../usage/configuration/homeserver_sample_config.html">homserver.yaml</a>.</p>
  1097. <pre><code class="language-yaml">use_insecure_ssl_client_just_for_testing_do_not_use: true
  1098. </code></pre>
  1099. <div style="break-before: page; page-break-before: always;"></div><h1 id="overview-1"><a class="header" href="#overview-1">Overview</a></h1>
  1100. <p>This document explains how to enable VoIP relaying on your Home Server with
  1101. TURN.</p>
  1102. <p>The synapse Matrix Home Server supports integration with TURN server via the
  1103. <a href="https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00">TURN server REST API</a>. This
  1104. allows the Home Server to generate credentials that are valid for use on the
  1105. TURN server through the use of a secret shared between the Home Server and the
  1106. TURN server.</p>
  1107. <p>The following sections describe how to install <a href="https://github.com/coturn/coturn">coturn</a> (which implements the TURN REST API) and integrate it with synapse.</p>
  1108. <h2 id="requirements"><a class="header" href="#requirements">Requirements</a></h2>
  1109. <p>For TURN relaying with <code>coturn</code> to work, it must be hosted on a server/endpoint with a public IP.</p>
  1110. <p>Hosting TURN behind a NAT (even with appropriate port forwarding) is known to cause issues
  1111. and to often not work.</p>
  1112. <h2 id="coturn-setup"><a class="header" href="#coturn-setup"><code>coturn</code> setup</a></h2>
  1113. <h3 id="initial-installation"><a class="header" href="#initial-installation">Initial installation</a></h3>
  1114. <p>The TURN daemon <code>coturn</code> is available from a variety of sources such as native package managers, or installation from source.</p>
  1115. <h4 id="debian-installation"><a class="header" href="#debian-installation">Debian installation</a></h4>
  1116. <p>Just install the debian package:</p>
  1117. <pre><code class="language-sh">apt install coturn
  1118. </code></pre>
  1119. <p>This will install and start a systemd service called <code>coturn</code>.</p>
  1120. <h4 id="source-installation"><a class="header" href="#source-installation">Source installation</a></h4>
  1121. <ol>
  1122. <li>
  1123. <p>Download the <a href="https://github.com/coturn/coturn/releases/latest">latest release</a> from github. Unpack it and <code>cd</code> into the directory.</p>
  1124. </li>
  1125. <li>
  1126. <p>Configure it:</p>
  1127. <pre><code class="language-sh">./configure
  1128. </code></pre>
  1129. <p>You may need to install <code>libevent2</code>: if so, you should do so in
  1130. the way recommended by your operating system. You can ignore
  1131. warnings about lack of database support: a database is unnecessary
  1132. for this purpose.</p>
  1133. </li>
  1134. <li>
  1135. <p>Build and install it:</p>
  1136. <pre><code class="language-sh">make
  1137. make install
  1138. </code></pre>
  1139. </li>
  1140. </ol>
  1141. <h3 id="configuration"><a class="header" href="#configuration">Configuration</a></h3>
  1142. <ol>
  1143. <li>
  1144. <p>Create or edit the config file in <code>/etc/turnserver.conf</code>. The relevant
  1145. lines, with example values, are:</p>
  1146. <pre><code>use-auth-secret
  1147. static-auth-secret=[your secret key here]
  1148. realm=turn.myserver.org
  1149. </code></pre>
  1150. <p>See <code>turnserver.conf</code> for explanations of the options. One way to generate
  1151. the <code>static-auth-secret</code> is with <code>pwgen</code>:</p>
  1152. <pre><code class="language-sh">pwgen -s 64 1
  1153. </code></pre>
  1154. <p>A <code>realm</code> must be specified, but its value is somewhat arbitrary. (It is
  1155. sent to clients as part of the authentication flow.) It is conventional to
  1156. set it to be your server name.</p>
  1157. </li>
  1158. <li>
  1159. <p>You will most likely want to configure coturn to write logs somewhere. The
  1160. easiest way is normally to send them to the syslog:</p>
  1161. <pre><code class="language-sh">syslog
  1162. </code></pre>
  1163. <p>(in which case, the logs will be available via <code>journalctl -u coturn</code> on a
  1164. systemd system). Alternatively, coturn can be configured to write to a
  1165. logfile - check the example config file supplied with coturn.</p>
  1166. </li>
  1167. <li>
  1168. <p>Consider your security settings. TURN lets users request a relay which will
  1169. connect to arbitrary IP addresses and ports. The following configuration is
  1170. suggested as a minimum starting point:</p>
  1171. <pre><code># VoIP traffic is all UDP. There is no reason to let users connect to arbitrary TCP endpoints via the relay.
  1172. no-tcp-relay
  1173. # don't let the relay ever try to connect to private IP address ranges within your network (if any)
  1174. # given the turn server is likely behind your firewall, remember to include any privileged public IPs too.
  1175. denied-peer-ip=10.0.0.0-10.255.255.255
  1176. denied-peer-ip=192.168.0.0-192.168.255.255
  1177. denied-peer-ip=172.16.0.0-172.31.255.255
  1178. # special case the turn server itself so that client-&gt;TURN-&gt;TURN-&gt;client flows work
  1179. allowed-peer-ip=10.0.0.1
  1180. # consider whether you want to limit the quota of relayed streams per user (or total) to avoid risk of DoS.
  1181. user-quota=12 # 4 streams per video call, so 12 streams = 3 simultaneous relayed calls per user.
  1182. total-quota=1200
  1183. </code></pre>
  1184. </li>
  1185. <li>
  1186. <p>Also consider supporting TLS/DTLS. To do this, add the following settings
  1187. to <code>turnserver.conf</code>:</p>
  1188. <pre><code># TLS certificates, including intermediate certs.
  1189. # For Let's Encrypt certificates, use `fullchain.pem` here.
  1190. cert=/path/to/fullchain.pem
  1191. # TLS private key file
  1192. pkey=/path/to/privkey.pem
  1193. </code></pre>
  1194. <p>In this case, replace the <code>turn:</code> schemes in the <code>turn_uri</code> settings below
  1195. with <code>turns:</code>.</p>
  1196. <p>We recommend that you only try to set up TLS/DTLS once you have set up a
  1197. basic installation and got it working.</p>
  1198. </li>
  1199. <li>
  1200. <p>Ensure your firewall allows traffic into the TURN server on the ports
  1201. you've configured it to listen on (By default: 3478 and 5349 for TURN
  1202. traffic (remember to allow both TCP and UDP traffic), and ports 49152-65535
  1203. for the UDP relay.)</p>
  1204. </li>
  1205. <li>
  1206. <p>We do not recommend running a TURN server behind NAT, and are not aware of
  1207. anyone doing so successfully.</p>
  1208. <p>If you want to try it anyway, you will at least need to tell coturn its
  1209. external IP address:</p>
  1210. <pre><code>external-ip=192.88.99.1
  1211. </code></pre>
  1212. <p>... and your NAT gateway must forward all of the relayed ports directly
  1213. (eg, port 56789 on the external IP must be always be forwarded to port
  1214. 56789 on the internal IP).</p>
  1215. <p>If you get this working, let us know!</p>
  1216. </li>
  1217. <li>
  1218. <p>(Re)start the turn server:</p>
  1219. <ul>
  1220. <li>
  1221. <p>If you used the Debian package (or have set up a systemd unit yourself):</p>
  1222. <pre><code class="language-sh">systemctl restart coturn
  1223. </code></pre>
  1224. </li>
  1225. <li>
  1226. <p>If you installed from source:</p>
  1227. <pre><code class="language-sh">bin/turnserver -o
  1228. </code></pre>
  1229. </li>
  1230. </ul>
  1231. </li>
  1232. </ol>
  1233. <h2 id="synapse-setup"><a class="header" href="#synapse-setup">Synapse setup</a></h2>
  1234. <p>Your home server configuration file needs the following extra keys:</p>
  1235. <ol>
  1236. <li>&quot;<code>turn_uris</code>&quot;: This needs to be a yaml list of public-facing URIs
  1237. for your TURN server to be given out to your clients. Add separate
  1238. entries for each transport your TURN server supports.</li>
  1239. <li>&quot;<code>turn_shared_secret</code>&quot;: This is the secret shared between your
  1240. Home server and your TURN server, so you should set it to the same
  1241. string you used in turnserver.conf.</li>
  1242. <li>&quot;<code>turn_user_lifetime</code>&quot;: This is the amount of time credentials
  1243. generated by your Home Server are valid for (in milliseconds).
  1244. Shorter times offer less potential for abuse at the expense of
  1245. increased traffic between web clients and your home server to
  1246. refresh credentials. The TURN REST API specification recommends
  1247. one day (86400000).</li>
  1248. <li>&quot;<code>turn_allow_guests</code>&quot;: Whether to allow guest users to use the
  1249. TURN server. This is enabled by default, as otherwise VoIP will
  1250. not work reliably for guests. However, it does introduce a
  1251. security risk as it lets guests connect to arbitrary endpoints
  1252. without having gone through a CAPTCHA or similar to register a
  1253. real account.</li>
  1254. </ol>
  1255. <p>As an example, here is the relevant section of the config file for <code>matrix.org</code>. The
  1256. <code>turn_uris</code> are appropriate for TURN servers listening on the default ports, with no TLS.</p>
  1257. <pre><code>turn_uris: [ &quot;turn:turn.matrix.org?transport=udp&quot;, &quot;turn:turn.matrix.org?transport=tcp&quot; ]
  1258. turn_shared_secret: &quot;n0t4ctuAllymatr1Xd0TorgSshar3d5ecret4obvIousreAsons&quot;
  1259. turn_user_lifetime: 86400000
  1260. turn_allow_guests: True
  1261. </code></pre>
  1262. <p>After updating the homeserver configuration, you must restart synapse:</p>
  1263. <ul>
  1264. <li>If you use synctl:
  1265. <pre><code class="language-sh">cd /where/you/run/synapse
  1266. ./synctl restart
  1267. </code></pre>
  1268. </li>
  1269. <li>If you use systemd:
  1270. <pre><code class="language-sh">systemctl restart matrix-synapse.service
  1271. </code></pre>
  1272. </li>
  1273. </ul>
  1274. <p>... and then reload any clients (or wait an hour for them to refresh their
  1275. settings).</p>
  1276. <h2 id="troubleshooting-2"><a class="header" href="#troubleshooting-2">Troubleshooting</a></h2>
  1277. <p>The normal symptoms of a misconfigured TURN server are that calls between
  1278. devices on different networks ring, but get stuck at &quot;call
  1279. connecting&quot;. Unfortunately, troubleshooting this can be tricky.</p>
  1280. <p>Here are a few things to try:</p>
  1281. <ul>
  1282. <li>
  1283. <p>Check that your TURN server is not behind NAT. As above, we're not aware of
  1284. anyone who has successfully set this up.</p>
  1285. </li>
  1286. <li>
  1287. <p>Check that you have opened your firewall to allow TCP and UDP traffic to the
  1288. TURN ports (normally 3478 and 5479).</p>
  1289. </li>
  1290. <li>
  1291. <p>Check that you have opened your firewall to allow UDP traffic to the UDP
  1292. relay ports (49152-65535 by default).</p>
  1293. </li>
  1294. <li>
  1295. <p>Some WebRTC implementations (notably, that of Google Chrome) appear to get
  1296. confused by TURN servers which are reachable over IPv6 (this appears to be
  1297. an unexpected side-effect of its handling of multiple IP addresses as
  1298. defined by
  1299. <a href="https://tools.ietf.org/html/draft-ietf-rtcweb-ip-handling-12"><code>draft-ietf-rtcweb-ip-handling</code></a>).</p>
  1300. <p>Try removing any AAAA records for your TURN server, so that it is only
  1301. reachable over IPv4.</p>
  1302. </li>
  1303. <li>
  1304. <p>Enable more verbose logging in coturn via the <code>verbose</code> setting:</p>
  1305. <pre><code>verbose
  1306. </code></pre>
  1307. <p>... and then see if there are any clues in its logs.</p>
  1308. </li>
  1309. <li>
  1310. <p>If you are using a browser-based client under Chrome, check
  1311. <code>chrome://webrtc-internals/</code> for insights into the internals of the
  1312. negotiation. On Firefox, check the &quot;Connection Log&quot; on <code>about:webrtc</code>.</p>
  1313. <p>(Understanding the output is beyond the scope of this document!)</p>
  1314. </li>
  1315. <li>
  1316. <p>You can test your Matrix homeserver TURN setup with https://test.voip.librepush.net/.
  1317. Note that this test is not fully reliable yet, so don't be discouraged if
  1318. the test fails.
  1319. <a href="https://github.com/matrix-org/voip-tester">Here</a> is the github repo of the
  1320. source of the tester, where you can file bug reports.</p>
  1321. </li>
  1322. <li>
  1323. <p>There is a WebRTC test tool at
  1324. https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/. To
  1325. use it, you will need a username/password for your TURN server. You can
  1326. either:</p>
  1327. <ul>
  1328. <li>
  1329. <p>look for the <code>GET /_matrix/client/r0/voip/turnServer</code> request made by a
  1330. matrix client to your homeserver in your browser's network inspector. In
  1331. the response you should see <code>username</code> and <code>password</code>. Or:</p>
  1332. </li>
  1333. <li>
  1334. <p>Use the following shell commands:</p>
  1335. <pre><code class="language-sh">secret=staticAuthSecretHere
  1336. u=$((`date +%s` + 3600)):test
  1337. p=$(echo -n $u | openssl dgst -hmac $secret -sha1 -binary | base64)
  1338. echo -e &quot;username: $u\npassword: $p&quot;
  1339. </code></pre>
  1340. <p>Or:</p>
  1341. </li>
  1342. <li>
  1343. <p>Temporarily configure coturn to accept a static username/password. To do
  1344. this, comment out <code>use-auth-secret</code> and <code>static-auth-secret</code> and add the
  1345. following:</p>
  1346. <pre><code>lt-cred-mech
  1347. user=username:password
  1348. </code></pre>
  1349. <p><strong>Note</strong>: these settings will not take effect unless <code>use-auth-secret</code>
  1350. and <code>static-auth-secret</code> are disabled.</p>
  1351. <p>Restart coturn after changing the configuration file.</p>
  1352. <p>Remember to restore the original settings to go back to testing with
  1353. Matrix clients!</p>
  1354. </li>
  1355. </ul>
  1356. <p>If the TURN server is working correctly, you should see at least one <code>relay</code>
  1357. entry in the results.</p>
  1358. </li>
  1359. </ul>
  1360. <div style="break-before: page; page-break-before: always;"></div><h1 id="delegation-of-incoming-federation-traffic"><a class="header" href="#delegation-of-incoming-federation-traffic">Delegation of incoming federation traffic</a></h1>
  1361. <p>In the following documentation, we use the term <code>server_name</code> to refer to that setting
  1362. in your homeserver configuration file. It appears at the ends of user ids, and tells
  1363. other homeservers where they can find your server.</p>
  1364. <p>By default, other homeservers will expect to be able to reach yours via
  1365. your <code>server_name</code>, on port 8448. For example, if you set your <code>server_name</code>
  1366. to <code>example.com</code> (so that your user names look like <code>@user:example.com</code>),
  1367. other servers will try to connect to yours at <code>https://example.com:8448/</code>.</p>
  1368. <p>Delegation is a Matrix feature allowing a homeserver admin to retain a
  1369. <code>server_name</code> of <code>example.com</code> so that user IDs, room aliases, etc continue
  1370. to look like <code>*:example.com</code>, whilst having federation traffic routed
  1371. to a different server and/or port (e.g. <code>synapse.example.com:443</code>).</p>
  1372. <h2 id="well-known-delegation"><a class="header" href="#well-known-delegation">.well-known delegation</a></h2>
  1373. <p>To use this method, you need to be able to configure the server at
  1374. <code>https://&lt;server_name&gt;</code> to serve a file at
  1375. <code>https://&lt;server_name&gt;/.well-known/matrix/server</code>. There are two ways to do this, shown below.</p>
  1376. <p>Note that the <code>.well-known</code> file is hosted on the default port for <code>https</code> (port 443).</p>
  1377. <h3 id="external-server"><a class="header" href="#external-server">External server</a></h3>
  1378. <p>For maximum flexibility, you need to configure an external server such as nginx, Apache
  1379. or HAProxy to serve the <code>https://&lt;server_name&gt;/.well-known/matrix/server</code> file. Setting
  1380. up such a server is out of the scope of this documentation, but note that it is often
  1381. possible to configure your <a href="reverse_proxy.html">reverse proxy</a> for this.</p>
  1382. <p>The URL <code>https://&lt;server_name&gt;/.well-known/matrix/server</code> should be configured
  1383. return a JSON structure containing the key <code>m.server</code> like this:</p>
  1384. <pre><code class="language-json">{
  1385. &quot;m.server&quot;: &quot;&lt;synapse.server.name&gt;[:&lt;yourport&gt;]&quot;
  1386. }
  1387. </code></pre>
  1388. <p>In our example (where we want federation traffic to be routed to
  1389. <code>https://synapse.example.com</code>, on port 443), this would mean that
  1390. <code>https://example.com/.well-known/matrix/server</code> should return:</p>
  1391. <pre><code class="language-json">{
  1392. &quot;m.server&quot;: &quot;synapse.example.com:443&quot;
  1393. }
  1394. </code></pre>
  1395. <p>Note, specifying a port is optional. If no port is specified, then it defaults
  1396. to 8448.</p>
  1397. <h3 id="serving-a-well-knownmatrixserver-file-with-synapse"><a class="header" href="#serving-a-well-knownmatrixserver-file-with-synapse">Serving a <code>.well-known/matrix/server</code> file with Synapse</a></h3>
  1398. <p>If you are able to set up your domain so that <code>https://&lt;server_name&gt;</code> is routed to
  1399. Synapse (i.e., the only change needed is to direct federation traffic to port 443
  1400. instead of port 8448), then it is possible to configure Synapse to serve a suitable
  1401. <code>.well-known/matrix/server</code> file. To do so, add the following to your <code>homeserver.yaml</code>
  1402. file:</p>
  1403. <pre><code class="language-yaml">serve_server_wellknown: true
  1404. </code></pre>
  1405. <p><strong>Note</strong>: this <em>only</em> works if <code>https://&lt;server_name&gt;</code> is routed to Synapse, so is
  1406. generally not suitable if Synapse is hosted at a subdomain such as
  1407. <code>https://synapse.example.com</code>.</p>
  1408. <h2 id="srv-dns-record-delegation"><a class="header" href="#srv-dns-record-delegation">SRV DNS record delegation</a></h2>
  1409. <p>It is also possible to do delegation using a SRV DNS record. However, that is generally
  1410. not recommended, as it can be difficult to configure the TLS certificates correctly in
  1411. this case, and it offers little advantage over <code>.well-known</code> delegation.</p>
  1412. <p>However, if you really need it, you can find some documentation on what such a
  1413. record should look like and how Synapse will use it in <a href="https://matrix.org/docs/spec/server_server/latest#resolving-server-names">the Matrix
  1414. specification</a>.</p>
  1415. <h2 id="delegation-faq"><a class="header" href="#delegation-faq">Delegation FAQ</a></h2>
  1416. <h3 id="when-do-i-need-delegation"><a class="header" href="#when-do-i-need-delegation">When do I need delegation?</a></h3>
  1417. <p>If your homeserver's APIs are accessible on the default federation port (8448)
  1418. and the domain your <code>server_name</code> points to, you do not need any delegation.</p>
  1419. <p>For instance, if you registered <code>example.com</code> and pointed its DNS A record at a
  1420. fresh server, you could install Synapse on that host, giving it a <code>server_name</code>
  1421. of <code>example.com</code>, and once a reverse proxy has been set up to proxy all requests
  1422. sent to the port <code>8448</code> and serve TLS certificates for <code>example.com</code>, you
  1423. wouldn't need any delegation set up.</p>
  1424. <p><strong>However</strong>, if your homeserver's APIs aren't accessible on port 8448 and on the
  1425. domain <code>server_name</code> points to, you will need to let other servers know how to
  1426. find it using delegation.</p>
  1427. <h3 id="should-i-use-a-reverse-proxy-for-federation-traffic"><a class="header" href="#should-i-use-a-reverse-proxy-for-federation-traffic">Should I use a reverse proxy for federation traffic?</a></h3>
  1428. <p>Generally, using a reverse proxy for both the federation and client traffic is a good
  1429. idea, since it saves handling TLS traffic in Synapse. See
  1430. <a href="reverse_proxy.html">the reverse proxy documentation</a> for information on setting up a
  1431. reverse proxy.</p>
  1432. <div style="break-before: page; page-break-before: always;"></div><h1 id="upgrading-synapse"><a class="header" href="#upgrading-synapse">Upgrading Synapse</a></h1>
  1433. <p>Before upgrading check if any special steps are required to upgrade from
  1434. the version you currently have installed to the current version of
  1435. Synapse. The extra instructions that may be required are listed later in
  1436. this document.</p>
  1437. <ul>
  1438. <li>
  1439. <p>Check that your versions of Python and PostgreSQL are still
  1440. supported.</p>
  1441. <p>Synapse follows upstream lifecycles for <a href="https://endoflife.date/python">Python</a> and
  1442. <a href="https://endoflife.date/postgresql">PostgreSQL</a>, and removes support for versions
  1443. which are no longer maintained.</p>
  1444. <p>The website <a href="https://endoflife.date">https://endoflife.date</a> also offers convenient
  1445. summaries.</p>
  1446. </li>
  1447. <li>
  1448. <p>If Synapse was installed using <a href="setup/installation.html#prebuilt-packages">prebuilt
  1449. packages</a>, you will need to follow the
  1450. normal process for upgrading those packages.</p>
  1451. </li>
  1452. <li>
  1453. <p>If Synapse was installed from source, then:</p>
  1454. <ol>
  1455. <li>
  1456. <p>Activate the virtualenv before upgrading. For example, if
  1457. Synapse is installed in a virtualenv in <code>~/synapse/env</code> then
  1458. run:</p>
  1459. <pre><code class="language-bash">source ~/synapse/env/bin/activate
  1460. </code></pre>
  1461. </li>
  1462. <li>
  1463. <p>If Synapse was installed using pip then upgrade to the latest
  1464. version by running:</p>
  1465. <pre><code class="language-bash">pip install --upgrade matrix-synapse
  1466. </code></pre>
  1467. <p>If Synapse was installed using git then upgrade to the latest
  1468. version by running:</p>
  1469. <pre><code class="language-bash">git pull
  1470. pip install --upgrade .
  1471. </code></pre>
  1472. </li>
  1473. <li>
  1474. <p>Restart Synapse:</p>
  1475. <pre><code class="language-bash">./synctl restart
  1476. </code></pre>
  1477. </li>
  1478. </ol>
  1479. </li>
  1480. </ul>
  1481. <p>To check whether your update was successful, you can check the running
  1482. server version with:</p>
  1483. <pre><code class="language-bash"># you may need to replace 'localhost:8008' if synapse is not configured
  1484. # to listen on port 8008.
  1485. curl http://localhost:8008/_synapse/admin/v1/server_version
  1486. </code></pre>
  1487. <h2 id="rolling-back-to-older-versions"><a class="header" href="#rolling-back-to-older-versions">Rolling back to older versions</a></h2>
  1488. <p>Rolling back to previous releases can be difficult, due to database
  1489. schema changes between releases. Where we have been able to test the
  1490. rollback process, this will be noted below.</p>
  1491. <p>In general, you will need to undo any changes made during the upgrade
  1492. process, for example:</p>
  1493. <ul>
  1494. <li>
  1495. <p>pip:</p>
  1496. <pre><code class="language-bash">source env/bin/activate
  1497. # replace `1.3.0` accordingly:
  1498. pip install matrix-synapse==1.3.0
  1499. </code></pre>
  1500. </li>
  1501. <li>
  1502. <p>Debian:</p>
  1503. <pre><code class="language-bash"># replace `1.3.0` and `stretch` accordingly:
  1504. wget https://packages.matrix.org/debian/pool/main/m/matrix-synapse-py3/matrix-synapse-py3_1.3.0+stretch1_amd64.deb
  1505. dpkg -i matrix-synapse-py3_1.3.0+stretch1_amd64.deb
  1506. </code></pre>
  1507. </li>
  1508. </ul>
  1509. <h1 id="upgrading-to-v1470"><a class="header" href="#upgrading-to-v1470">Upgrading to v1.47.0</a></h1>
  1510. <h2 id="removal-of-old-room-admin-api"><a class="header" href="#removal-of-old-room-admin-api">Removal of old Room Admin API</a></h2>
  1511. <p>The following admin APIs were deprecated in <a href="https://github.com/matrix-org/synapse/blob/v1.34.0/CHANGES.md#deprecations-and-removals">Synapse 1.34</a>
  1512. (released on 2021-05-17) and have now been removed:</p>
  1513. <ul>
  1514. <li><code>POST /_synapse/admin/v1/&lt;room_id&gt;/delete</code></li>
  1515. </ul>
  1516. <p>Any scripts still using the above APIs should be converted to use the
  1517. <a href="https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#delete-room-api">Delete Room API</a>.</p>
  1518. <h2 id="deprecation-of-the-user_may_create_room_with_invites-module-callback"><a class="header" href="#deprecation-of-the-user_may_create_room_with_invites-module-callback">Deprecation of the <code>user_may_create_room_with_invites</code> module callback</a></h2>
  1519. <p>The <code>user_may_create_room_with_invites</code> is deprecated and will be removed in a future
  1520. version of Synapse. Modules implementing this callback can instead implement
  1521. <a href="https://matrix-org.github.io/synapse/latest/modules/spam_checker_callbacks.html#user_may_invite"><code>user_may_invite</code></a>
  1522. and use the <a href="https://github.com/matrix-org/synapse/blob/872f23b95fa980a61b0866c1475e84491991fa20/synapse/module_api/__init__.py#L869-L876"><code>get_room_state</code></a>
  1523. module API method to infer whether the invite is happening in the context of creating a
  1524. room.</p>
  1525. <p>We plan to remove this callback in January 2022.</p>
  1526. <h1 id="upgrading-to-v1450"><a class="header" href="#upgrading-to-v1450">Upgrading to v1.45.0</a></h1>
  1527. <h2 id="changes-required-to-media-storage-provider-modules-when-reading-from-the-synapse-configuration-object"><a class="header" href="#changes-required-to-media-storage-provider-modules-when-reading-from-the-synapse-configuration-object">Changes required to media storage provider modules when reading from the Synapse configuration object</a></h2>
  1528. <p>Media storage provider modules that read from the Synapse configuration object (i.e. that
  1529. read the value of <code>hs.config.[...]</code>) now need to specify the configuration section they're
  1530. reading from. This means that if a module reads the value of e.g. <code>hs.config.media_store_path</code>,
  1531. it needs to replace it with <code>hs.config.media.media_store_path</code>.</p>
  1532. <h1 id="upgrading-to-v1440"><a class="header" href="#upgrading-to-v1440">Upgrading to v1.44.0</a></h1>
  1533. <h2 id="the-url-preview-cache-is-no-longer-mirrored-to-storage-providers"><a class="header" href="#the-url-preview-cache-is-no-longer-mirrored-to-storage-providers">The URL preview cache is no longer mirrored to storage providers</a></h2>
  1534. <p>The <code>url_cache/</code> and <code>url_cache_thumbnails/</code> directories in the media store are
  1535. no longer mirrored to storage providers. These two directories can be safely
  1536. deleted from any configured storage providers to reclaim space.</p>
  1537. <h1 id="upgrading-to-v1430"><a class="header" href="#upgrading-to-v1430">Upgrading to v1.43.0</a></h1>
  1538. <h2 id="the-spaces-summary-apis-can-now-be-handled-by-workers"><a class="header" href="#the-spaces-summary-apis-can-now-be-handled-by-workers">The spaces summary APIs can now be handled by workers</a></h2>
  1539. <p>The <a href="https://matrix-org.github.io/synapse/latest/workers.html#available-worker-applications">available worker applications documentation</a>
  1540. has been updated to reflect that calls to the <code>/spaces</code>, <code>/hierarchy</code>, and
  1541. <code>/summary</code> endpoints can now be routed to workers for both client API and
  1542. federation requests.</p>
  1543. <h1 id="upgrading-to-v1420"><a class="header" href="#upgrading-to-v1420">Upgrading to v1.42.0</a></h1>
  1544. <h2 id="removal-of-old-room-admin-api-1"><a class="header" href="#removal-of-old-room-admin-api-1">Removal of old Room Admin API</a></h2>
  1545. <p>The following admin APIs were deprecated in <a href="https://github.com/matrix-org/synapse/blob/v1.25.0/CHANGES.md#removal-warning">Synapse 1.25</a>
  1546. (released on 2021-01-13) and have now been removed:</p>
  1547. <ul>
  1548. <li><code>POST /_synapse/admin/v1/purge_room</code></li>
  1549. <li><code>POST /_synapse/admin/v1/shutdown_room/&lt;room_id&gt;</code></li>
  1550. </ul>
  1551. <p>Any scripts still using the above APIs should be converted to use the
  1552. <a href="https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#delete-room-api">Delete Room API</a>.</p>
  1553. <h2 id="user-interactive-authentication-fallback-templates-can-now-display-errors"><a class="header" href="#user-interactive-authentication-fallback-templates-can-now-display-errors">User-interactive authentication fallback templates can now display errors</a></h2>
  1554. <p>This may affect you if you make use of custom HTML templates for the
  1555. <a href="../synapse/res/templates/recaptcha.html">reCAPTCHA</a> or
  1556. <a href="../synapse/res/templates/terms.html">terms</a> fallback pages.</p>
  1557. <p>The template is now provided an <code>error</code> variable if the authentication
  1558. process failed. See the default templates linked above for an example.</p>
  1559. <h2 id="removal-of-out-of-date-email-pushers"><a class="header" href="#removal-of-out-of-date-email-pushers">Removal of out-of-date email pushers</a></h2>
  1560. <p>Users will stop receiving message updates via email for addresses that were
  1561. once, but not still, linked to their account.</p>
  1562. <h1 id="upgrading-to-v1410"><a class="header" href="#upgrading-to-v1410">Upgrading to v1.41.0</a></h1>
  1563. <h2 id="add-support-for-routing-outbound-http-requests-via-a-proxy-for-federation"><a class="header" href="#add-support-for-routing-outbound-http-requests-via-a-proxy-for-federation">Add support for routing outbound HTTP requests via a proxy for federation</a></h2>
  1564. <p>Since Synapse 1.6.0 (2019-11-26) you can set a proxy for outbound HTTP requests via
  1565. http_proxy/https_proxy environment variables. This proxy was set for:</p>
  1566. <ul>
  1567. <li>push</li>
  1568. <li>url previews</li>
  1569. <li>phone-home stats</li>
  1570. <li>recaptcha validation</li>
  1571. <li>CAS auth validation</li>
  1572. <li>OpenID Connect</li>
  1573. <li>Federation (checking public key revocation)</li>
  1574. </ul>
  1575. <p>In this version we have added support for outbound requests for:</p>
  1576. <ul>
  1577. <li>Outbound federation</li>
  1578. <li>Downloading remote media</li>
  1579. <li>Fetching public keys of other servers</li>
  1580. </ul>
  1581. <p>These requests use the same proxy configuration. If you have a proxy configuration we
  1582. recommend to verify the configuration. It may be necessary to adjust the <code>no_proxy</code>
  1583. environment variable.</p>
  1584. <p>See <a href="setup/forward_proxy.html">using a forward proxy with Synapse documentation</a> for
  1585. details.</p>
  1586. <h2 id="deprecation-of-template_dir"><a class="header" href="#deprecation-of-template_dir">Deprecation of <code>template_dir</code></a></h2>
  1587. <p>The <code>template_dir</code> settings in the <code>sso</code>, <code>account_validity</code> and <code>email</code> sections of the
  1588. configuration file are now deprecated. Server admins should use the new
  1589. <code>templates.custom_template_directory</code> setting in the configuration file and use one single
  1590. custom template directory for all aforementioned features. Template file names remain
  1591. unchanged. See <a href="https://matrix-org.github.io/synapse/latest/templates.html">the related documentation</a>
  1592. for more information and examples.</p>
  1593. <p>We plan to remove support for these settings in October 2021.</p>
  1594. <h2 id="_synapseadminv1usersuseridmedia-must-be-handled-by-media-workers"><a class="header" href="#_synapseadminv1usersuseridmedia-must-be-handled-by-media-workers"><code>/_synapse/admin/v1/users/{userId}/media</code> must be handled by media workers</a></h2>
  1595. <p>The <a href="https://matrix-org.github.io/synapse/latest/workers.html#synapseappmedia_repository">media repository worker documentation</a>
  1596. has been updated to reflect that calls to <code>/_synapse/admin/v1/users/{userId}/media</code>
  1597. must now be handled by media repository workers. This is due to the new <code>DELETE</code> method
  1598. of this endpoint modifying the media store.</p>
  1599. <h1 id="upgrading-to-v1390"><a class="header" href="#upgrading-to-v1390">Upgrading to v1.39.0</a></h1>
  1600. <h2 id="deprecation-of-the-current-third-party-rules-module-interface"><a class="header" href="#deprecation-of-the-current-third-party-rules-module-interface">Deprecation of the current third-party rules module interface</a></h2>
  1601. <p>The current third-party rules module interface is deprecated in favour of the new generic
  1602. modules system introduced in Synapse v1.37.0. Authors of third-party rules modules can refer
  1603. to <a href="modules/porting_legacy_module.html">this documentation</a>
  1604. to update their modules. Synapse administrators can refer to <a href="modules/index.html">this documentation</a>
  1605. to update their configuration once the modules they are using have been updated.</p>
  1606. <p>We plan to remove support for the current third-party rules interface in September 2021.</p>
  1607. <h1 id="upgrading-to-v1380"><a class="header" href="#upgrading-to-v1380">Upgrading to v1.38.0</a></h1>
  1608. <h2 id="re-indexing-of-events-table-on-postgres-databases"><a class="header" href="#re-indexing-of-events-table-on-postgres-databases">Re-indexing of <code>events</code> table on Postgres databases</a></h2>
  1609. <p>This release includes a database schema update which requires re-indexing one of
  1610. the larger tables in the database, <code>events</code>. This could result in increased
  1611. disk I/O for several hours or days after upgrading while the migration
  1612. completes. Furthermore, because we have to keep the old indexes until the new
  1613. indexes are ready, it could result in a significant, temporary, increase in
  1614. disk space.</p>
  1615. <p>To get a rough idea of the disk space required, check the current size of one
  1616. of the indexes. For example, from a <code>psql</code> shell, run the following sql:</p>
  1617. <pre><code class="language-sql">SELECT pg_size_pretty(pg_relation_size('events_order_room'));
  1618. </code></pre>
  1619. <p>We need to rebuild <strong>four</strong> indexes, so you will need to multiply this result
  1620. by four to give an estimate of the disk space required. For example, on one
  1621. particular server:</p>
  1622. <pre><code>synapse=# select pg_size_pretty(pg_relation_size('events_order_room'));
  1623. pg_size_pretty
  1624. ----------------
  1625. 288 MB
  1626. (1 row)
  1627. </code></pre>
  1628. <p>On this server, it would be wise to ensure that at least 1152MB are free.</p>
  1629. <p>The additional disk space will be freed once the migration completes.</p>
  1630. <p>SQLite databases are unaffected by this change.</p>
  1631. <h1 id="upgrading-to-v1370"><a class="header" href="#upgrading-to-v1370">Upgrading to v1.37.0</a></h1>
  1632. <h2 id="deprecation-of-the-current-spam-checker-interface"><a class="header" href="#deprecation-of-the-current-spam-checker-interface">Deprecation of the current spam checker interface</a></h2>
  1633. <p>The current spam checker interface is deprecated in favour of a new generic modules system.
  1634. Authors of spam checker modules can refer to [this
  1635. documentation](modules/porting_legacy_module.md
  1636. to update their modules. Synapse administrators can refer to <a href="modules/index.html">this
  1637. documentation</a>
  1638. to update their configuration once the modules they are using have been updated.</p>
  1639. <p>We plan to remove support for the current spam checker interface in August 2021.</p>
  1640. <p>More module interfaces will be ported over to this new generic system in future versions
  1641. of Synapse.</p>
  1642. <h1 id="upgrading-to-v1340"><a class="header" href="#upgrading-to-v1340">Upgrading to v1.34.0</a></h1>
  1643. <h2 id="room_invite_state_types-configuration-setting"><a class="header" href="#room_invite_state_types-configuration-setting"><code>room_invite_state_types</code> configuration setting</a></h2>
  1644. <p>The <code>room_invite_state_types</code> configuration setting has been deprecated
  1645. and replaced with <code>room_prejoin_state</code>. See the <a href="https://github.com/matrix-org/synapse/blob/v1.34.0/docs/sample_config.yaml#L1515">sample configuration
  1646. file</a>.</p>
  1647. <p>If you have set <code>room_invite_state_types</code> to the default value you
  1648. should simply remove it from your configuration file. The default value
  1649. used to be:</p>
  1650. <pre><code class="language-yaml">room_invite_state_types:
  1651. - &quot;m.room.join_rules&quot;
  1652. - &quot;m.room.canonical_alias&quot;
  1653. - &quot;m.room.avatar&quot;
  1654. - &quot;m.room.encryption&quot;
  1655. - &quot;m.room.name&quot;
  1656. </code></pre>
  1657. <p>If you have customised this value, you should remove
  1658. <code>room_invite_state_types</code> and configure <code>room_prejoin_state</code> instead.</p>
  1659. <h1 id="upgrading-to-v1330"><a class="header" href="#upgrading-to-v1330">Upgrading to v1.33.0</a></h1>
  1660. <h2 id="account-validity-html-templates-can-now-display-a-users-expiration-date"><a class="header" href="#account-validity-html-templates-can-now-display-a-users-expiration-date">Account Validity HTML templates can now display a user's expiration date</a></h2>
  1661. <p>This may affect you if you have enabled the account validity feature,
  1662. and have made use of a custom HTML template specified by the
  1663. <code>account_validity.template_dir</code> or
  1664. <code>account_validity.account_renewed_html_path</code> Synapse config options.</p>
  1665. <p>The template can now accept an <code>expiration_ts</code> variable, which
  1666. represents the unix timestamp in milliseconds for the future date of
  1667. which their account has been renewed until. See the <a href="https://github.com/matrix-org/synapse/blob/release-v1.33.0/synapse/res/templates/account_renewed.html">default
  1668. template</a>
  1669. for an example of usage.</p>
  1670. <p>ALso note that a new HTML template, <code>account_previously_renewed.html</code>,
  1671. has been added. This is is shown to users when they attempt to renew
  1672. their account with a valid renewal token that has already been used
  1673. before. The default template contents can been found
  1674. <a href="https://github.com/matrix-org/synapse/blob/release-v1.33.0/synapse/res/templates/account_previously_renewed.html">here</a>,
  1675. and can also accept an <code>expiration_ts</code> variable. This template replaces
  1676. the error message users would previously see upon attempting to use a
  1677. valid renewal token more than once.</p>
  1678. <h1 id="upgrading-to-v1320"><a class="header" href="#upgrading-to-v1320">Upgrading to v1.32.0</a></h1>
  1679. <h2 id="regression-causing-connected-prometheus-instances-to-become-overwhelmed"><a class="header" href="#regression-causing-connected-prometheus-instances-to-become-overwhelmed">Regression causing connected Prometheus instances to become overwhelmed</a></h2>
  1680. <p>This release introduces <a href="https://github.com/matrix-org/synapse/issues/9853">a
  1681. regression</a> that can
  1682. overwhelm connected Prometheus instances. This issue is not present in
  1683. Synapse v1.32.0rc1.</p>
  1684. <p>If you have been affected, please downgrade to 1.31.0. You then may need
  1685. to remove excess writeahead logs in order for Prometheus to recover.
  1686. Instructions for doing so are provided
  1687. <a href="https://github.com/matrix-org/synapse/pull/9854#issuecomment-823472183">here</a>.</p>
  1688. <h2 id="dropping-support-for-old-python-postgres-and-sqlite-versions"><a class="header" href="#dropping-support-for-old-python-postgres-and-sqlite-versions">Dropping support for old Python, Postgres and SQLite versions</a></h2>
  1689. <p>In line with our <a href="deprecation_policy.html">deprecation policy</a>,
  1690. we've dropped support for Python 3.5 and PostgreSQL 9.5, as they are no
  1691. longer supported upstream.</p>
  1692. <p>This release of Synapse requires Python 3.6+ and PostgresSQL 9.6+ or
  1693. SQLite 3.22+.</p>
  1694. <h2 id="removal-of-old-list-accounts-admin-api"><a class="header" href="#removal-of-old-list-accounts-admin-api">Removal of old List Accounts Admin API</a></h2>
  1695. <p>The deprecated v1 &quot;list accounts&quot; admin API
  1696. (<code>GET /_synapse/admin/v1/users/&lt;user_id&gt;</code>) has been removed in this
  1697. version.</p>
  1698. <p>The <a href="admin_api/user_admin_api.html#list-accounts">v2 list accounts API</a>
  1699. has been available since Synapse 1.7.0 (2019-12-13), and is accessible
  1700. under <code>GET /_synapse/admin/v2/users</code>.</p>
  1701. <p>The deprecation of the old endpoint was announced with Synapse 1.28.0
  1702. (released on 2021-02-25).</p>
  1703. <h2 id="application-services-must-use-type-mloginapplication_service-when-registering-users"><a class="header" href="#application-services-must-use-type-mloginapplication_service-when-registering-users">Application Services must use type <code>m.login.application_service</code> when registering users</a></h2>
  1704. <p>In compliance with the <a href="https://matrix.org/docs/spec/application_service/r0.1.2#server-admin-style-permissions">Application Service
  1705. spec</a>,
  1706. Application Services are now required to use the
  1707. <code>m.login.application_service</code> type when registering users via the
  1708. <code>/_matrix/client/r0/register</code> endpoint. This behaviour was deprecated in
  1709. Synapse v1.30.0.</p>
  1710. <p>Please ensure your Application Services are up to date.</p>
  1711. <h1 id="upgrading-to-v1290"><a class="header" href="#upgrading-to-v1290">Upgrading to v1.29.0</a></h1>
  1712. <h2 id="requirement-for-x-forwarded-proto-header"><a class="header" href="#requirement-for-x-forwarded-proto-header">Requirement for X-Forwarded-Proto header</a></h2>
  1713. <p>When using Synapse with a reverse proxy (in particular, when using the
  1714. <code>x_forwarded</code> option on an HTTP listener), Synapse now
  1715. expects to receive an <code>X-Forwarded-Proto</code> header on incoming
  1716. HTTP requests. If it is not set, Synapse will log a warning on each
  1717. received request.</p>
  1718. <p>To avoid the warning, administrators using a reverse proxy should ensure
  1719. that the reverse proxy sets <code>X-Forwarded-Proto</code> header to
  1720. <code>https</code> or <code>http</code> to indicate the protocol used
  1721. by the client.</p>
  1722. <p>Synapse also requires the <code>Host</code> header to be preserved.</p>
  1723. <p>See the <a href="reverse_proxy.html">reverse proxy documentation</a>, where the
  1724. example configurations have been updated to show how to set these
  1725. headers.</p>
  1726. <p>(Users of <a href="https://caddyserver.com/">Caddy</a> are unaffected, since we
  1727. believe it sets <code>X-Forwarded-Proto</code> by default.)</p>
  1728. <h1 id="upgrading-to-v1270"><a class="header" href="#upgrading-to-v1270">Upgrading to v1.27.0</a></h1>
  1729. <h2 id="changes-to-callback-uri-for-oauth2--openid-connect-and-saml2"><a class="header" href="#changes-to-callback-uri-for-oauth2--openid-connect-and-saml2">Changes to callback URI for OAuth2 / OpenID Connect and SAML2</a></h2>
  1730. <p>This version changes the URI used for callbacks from OAuth2 and SAML2
  1731. identity providers:</p>
  1732. <ul>
  1733. <li>
  1734. <p>If your server is configured for single sign-on via an OpenID
  1735. Connect or OAuth2 identity provider, you will need to add
  1736. <code>[synapse public baseurl]/_synapse/client/oidc/callback</code> to the list
  1737. of permitted &quot;redirect URIs&quot; at the identity provider.</p>
  1738. <p>See the <a href="openid.html">OpenID docs</a> for more information on setting
  1739. up OpenID Connect.</p>
  1740. </li>
  1741. <li>
  1742. <p>If your server is configured for single sign-on via a SAML2 identity
  1743. provider, you will need to add
  1744. <code>[synapse public baseurl]/_synapse/client/saml2/authn_response</code> as a
  1745. permitted &quot;ACS location&quot; (also known as &quot;allowed callback URLs&quot;)
  1746. at the identity provider.</p>
  1747. <p>The &quot;Issuer&quot; in the &quot;AuthnRequest&quot; to the SAML2 identity
  1748. provider is also updated to
  1749. <code>[synapse public baseurl]/_synapse/client/saml2/metadata.xml</code>. If
  1750. your SAML2 identity provider uses this property to validate or
  1751. otherwise identify Synapse, its configuration will need to be
  1752. updated to use the new URL. Alternatively you could create a new,
  1753. separate &quot;EntityDescriptor&quot; in your SAML2 identity provider with
  1754. the new URLs and leave the URLs in the existing &quot;EntityDescriptor&quot;
  1755. as they were.</p>
  1756. </li>
  1757. </ul>
  1758. <h2 id="changes-to-html-templates"><a class="header" href="#changes-to-html-templates">Changes to HTML templates</a></h2>
  1759. <p>The HTML templates for SSO and email notifications now have <a href="https://jinja.palletsprojects.com/en/2.11.x/api/#autoescaping">Jinja2's
  1760. autoescape</a>
  1761. enabled for files ending in <code>.html</code>, <code>.htm</code>, and <code>.xml</code>. If you have
  1762. customised these templates and see issues when viewing them you might
  1763. need to update them. It is expected that most configurations will need
  1764. no changes.</p>
  1765. <p>If you have customised the templates <em>names</em> for these templates, it is
  1766. recommended to verify they end in <code>.html</code> to ensure autoescape is
  1767. enabled.</p>
  1768. <p>The above applies to the following templates:</p>
  1769. <ul>
  1770. <li><code>add_threepid.html</code></li>
  1771. <li><code>add_threepid_failure.html</code></li>
  1772. <li><code>add_threepid_success.html</code></li>
  1773. <li><code>notice_expiry.html</code></li>
  1774. <li><code>notice_expiry.html</code></li>
  1775. <li><code>notif_mail.html</code> (which, by default, includes <code>room.html</code> and
  1776. <code>notif.html</code>)</li>
  1777. <li><code>password_reset.html</code></li>
  1778. <li><code>password_reset_confirmation.html</code></li>
  1779. <li><code>password_reset_failure.html</code></li>
  1780. <li><code>password_reset_success.html</code></li>
  1781. <li><code>registration.html</code></li>
  1782. <li><code>registration_failure.html</code></li>
  1783. <li><code>registration_success.html</code></li>
  1784. <li><code>sso_account_deactivated.html</code></li>
  1785. <li><code>sso_auth_bad_user.html</code></li>
  1786. <li><code>sso_auth_confirm.html</code></li>
  1787. <li><code>sso_auth_success.html</code></li>
  1788. <li><code>sso_error.html</code></li>
  1789. <li><code>sso_login_idp_picker.html</code></li>
  1790. <li><code>sso_redirect_confirm.html</code></li>
  1791. </ul>
  1792. <h1 id="upgrading-to-v1260"><a class="header" href="#upgrading-to-v1260">Upgrading to v1.26.0</a></h1>
  1793. <h2 id="rolling-back-to-v1250-after-a-failed-upgrade"><a class="header" href="#rolling-back-to-v1250-after-a-failed-upgrade">Rolling back to v1.25.0 after a failed upgrade</a></h2>
  1794. <p>v1.26.0 includes a lot of large changes. If something problematic
  1795. occurs, you may want to roll-back to a previous version of Synapse.
  1796. Because v1.26.0 also includes a new database schema version, reverting
  1797. that version is also required alongside the generic rollback
  1798. instructions mentioned above. In short, to roll back to v1.25.0 you need
  1799. to:</p>
  1800. <ol>
  1801. <li>
  1802. <p>Stop the server</p>
  1803. </li>
  1804. <li>
  1805. <p>Decrease the schema version in the database:</p>
  1806. <pre><code class="language-sql">UPDATE schema_version SET version = 58;
  1807. </code></pre>
  1808. </li>
  1809. <li>
  1810. <p>Delete the ignored users &amp; chain cover data:</p>
  1811. <pre><code class="language-sql">DROP TABLE IF EXISTS ignored_users;
  1812. UPDATE rooms SET has_auth_chain_index = false;
  1813. </code></pre>
  1814. <p>For PostgreSQL run:</p>
  1815. <pre><code class="language-sql">TRUNCATE event_auth_chain_links;
  1816. TRUNCATE event_auth_chains;
  1817. </code></pre>
  1818. <p>For SQLite run:</p>
  1819. <pre><code class="language-sql">DELETE FROM event_auth_chain_links;
  1820. DELETE FROM event_auth_chains;
  1821. </code></pre>
  1822. </li>
  1823. <li>
  1824. <p>Mark the deltas as not run (so they will re-run on upgrade).</p>
  1825. <pre><code class="language-sql">DELETE FROM applied_schema_deltas WHERE version = 59 AND file = &quot;59/01ignored_user.py&quot;;
  1826. DELETE FROM applied_schema_deltas WHERE version = 59 AND file = &quot;59/06chain_cover_index.sql&quot;;
  1827. </code></pre>
  1828. </li>
  1829. <li>
  1830. <p>Downgrade Synapse by following the instructions for your
  1831. installation method in the &quot;Rolling back to older versions&quot;
  1832. section above.</p>
  1833. </li>
  1834. </ol>
  1835. <h1 id="upgrading-to-v1250"><a class="header" href="#upgrading-to-v1250">Upgrading to v1.25.0</a></h1>
  1836. <h2 id="last-release-supporting-python-35"><a class="header" href="#last-release-supporting-python-35">Last release supporting Python 3.5</a></h2>
  1837. <p>This is the last release of Synapse which guarantees support with Python
  1838. 3.5, which passed its upstream End of Life date several months ago.</p>
  1839. <p>We will attempt to maintain support through March 2021, but without
  1840. guarantees.</p>
  1841. <p>In the future, Synapse will follow upstream schedules for ending support
  1842. of older versions of Python and PostgreSQL. Please upgrade to at least
  1843. Python 3.6 and PostgreSQL 9.6 as soon as possible.</p>
  1844. <h2 id="blacklisting-ip-ranges"><a class="header" href="#blacklisting-ip-ranges">Blacklisting IP ranges</a></h2>
  1845. <p>Synapse v1.25.0 includes new settings, <code>ip_range_blacklist</code> and
  1846. <code>ip_range_whitelist</code>, for controlling outgoing requests from Synapse for
  1847. federation, identity servers, push, and for checking key validity for
  1848. third-party invite events. The previous setting,
  1849. <code>federation_ip_range_blacklist</code>, is deprecated. The new
  1850. <code>ip_range_blacklist</code> defaults to private IP ranges if it is not defined.</p>
  1851. <p>If you have never customised <code>federation_ip_range_blacklist</code> it is
  1852. recommended that you remove that setting.</p>
  1853. <p>If you have customised <code>federation_ip_range_blacklist</code> you should update
  1854. the setting name to <code>ip_range_blacklist</code>.</p>
  1855. <p>If you have a custom push server that is reached via private IP space
  1856. you may need to customise <code>ip_range_blacklist</code> or <code>ip_range_whitelist</code>.</p>
  1857. <h1 id="upgrading-to-v1240"><a class="header" href="#upgrading-to-v1240">Upgrading to v1.24.0</a></h1>
  1858. <h2 id="custom-openid-connect-mapping-provider-breaking-change"><a class="header" href="#custom-openid-connect-mapping-provider-breaking-change">Custom OpenID Connect mapping provider breaking change</a></h2>
  1859. <p>This release allows the OpenID Connect mapping provider to perform
  1860. normalisation of the localpart of the Matrix ID. This allows for the
  1861. mapping provider to specify different algorithms, instead of the
  1862. <a href="https://matrix.org/docs/spec/appendices#mapping-from-other-character-sets">default
  1863. way</a>.</p>
  1864. <p>If your Synapse configuration uses a custom mapping provider
  1865. (<code>oidc_config.user_mapping_provider.module</code> is specified and
  1866. not equal to
  1867. <code>synapse.handlers.oidc_handler.JinjaOidcMappingProvider</code>)
  1868. then you <em>must</em> ensure that <code>map_user_attributes</code> of the
  1869. mapping provider performs some normalisation of the
  1870. <code>localpart</code> returned. To match previous behaviour you can
  1871. use the <code>map_username_to_mxid_localpart</code> function provided
  1872. by Synapse. An example is shown below:</p>
  1873. <pre><code class="language-python">from synapse.types import map_username_to_mxid_localpart
  1874. class MyMappingProvider:
  1875. def map_user_attributes(self, userinfo, token):
  1876. # ... your custom logic ...
  1877. sso_user_id = ...
  1878. localpart = map_username_to_mxid_localpart(sso_user_id)
  1879. return {&quot;localpart&quot;: localpart}
  1880. </code></pre>
  1881. <h2 id="removal-historical-synapse-admin-api"><a class="header" href="#removal-historical-synapse-admin-api">Removal historical Synapse Admin API</a></h2>
  1882. <p>Historically, the Synapse Admin API has been accessible under:</p>
  1883. <ul>
  1884. <li><code>/_matrix/client/api/v1/admin</code></li>
  1885. <li><code>/_matrix/client/unstable/admin</code></li>
  1886. <li><code>/_matrix/client/r0/admin</code></li>
  1887. <li><code>/_synapse/admin/v1</code></li>
  1888. </ul>
  1889. <p>The endpoints with <code>/_matrix/client/*</code> prefixes have been removed as of
  1890. v1.24.0. The Admin API is now only accessible under:</p>
  1891. <ul>
  1892. <li><code>/_synapse/admin/v1</code></li>
  1893. </ul>
  1894. <p>The only exception is the <code>/admin/whois</code> endpoint, which is
  1895. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-admin-whois-userid">also available via the client-server
  1896. API</a>.</p>
  1897. <p>The deprecation of the old endpoints was announced with Synapse 1.20.0
  1898. (released on 2020-09-22) and makes it easier for homeserver admins to
  1899. lock down external access to the Admin API endpoints.</p>
  1900. <h1 id="upgrading-to-v1230"><a class="header" href="#upgrading-to-v1230">Upgrading to v1.23.0</a></h1>
  1901. <h2 id="structured-logging-configuration-breaking-changes"><a class="header" href="#structured-logging-configuration-breaking-changes">Structured logging configuration breaking changes</a></h2>
  1902. <p>This release deprecates use of the <code>structured: true</code> logging
  1903. configuration for structured logging. If your logging configuration
  1904. contains <code>structured: true</code> then it should be modified based on the
  1905. <a href="structured_logging.html">structured logging documentation</a>.</p>
  1906. <p>The <code>structured</code> and <code>drains</code> logging options are now deprecated and
  1907. should be replaced by standard logging configuration of <code>handlers</code> and
  1908. <code>formatters</code>.</p>
  1909. <p>A future will release of Synapse will make using <code>structured: true</code> an
  1910. error.</p>
  1911. <h1 id="upgrading-to-v1220"><a class="header" href="#upgrading-to-v1220">Upgrading to v1.22.0</a></h1>
  1912. <h2 id="thirdpartyeventrules-breaking-changes"><a class="header" href="#thirdpartyeventrules-breaking-changes">ThirdPartyEventRules breaking changes</a></h2>
  1913. <p>This release introduces a backwards-incompatible change to modules
  1914. making use of <code>ThirdPartyEventRules</code> in Synapse. If you make use of a
  1915. module defined under the <code>third_party_event_rules</code> config option, please
  1916. make sure it is updated to handle the below change:</p>
  1917. <p>The <code>http_client</code> argument is no longer passed to modules as they are
  1918. initialised. Instead, modules are expected to make use of the
  1919. <code>http_client</code> property on the <code>ModuleApi</code> class. Modules are now passed
  1920. a <code>module_api</code> argument during initialisation, which is an instance of
  1921. <code>ModuleApi</code>. <code>ModuleApi</code> instances have a <code>http_client</code> property which
  1922. acts the same as the <code>http_client</code> argument previously passed to
  1923. <code>ThirdPartyEventRules</code> modules.</p>
  1924. <h1 id="upgrading-to-v1210"><a class="header" href="#upgrading-to-v1210">Upgrading to v1.21.0</a></h1>
  1925. <h2 id="forwarding-_synapseclient-through-your-reverse-proxy"><a class="header" href="#forwarding-_synapseclient-through-your-reverse-proxy">Forwarding <code>/_synapse/client</code> through your reverse proxy</a></h2>
  1926. <p>The <a href="reverse_proxy.html">reverse proxy documentation</a>
  1927. has been updated to include reverse proxy directives for
  1928. <code>/_synapse/client/*</code> endpoints. As the user password reset flow now uses
  1929. endpoints under this prefix, <strong>you must update your reverse proxy
  1930. configurations for user password reset to work</strong>.</p>
  1931. <p>Additionally, note that the <a href="workers.html">Synapse worker documentation</a> has been updated to</p>
  1932. <p>: state that the <code>/_synapse/client/password_reset/email/submit_token</code>
  1933. endpoint can be handled</p>
  1934. <p>by all workers. If you make use of Synapse's worker feature, please
  1935. update your reverse proxy configuration to reflect this change.</p>
  1936. <h2 id="new-html-templates"><a class="header" href="#new-html-templates">New HTML templates</a></h2>
  1937. <p>A new HTML template,
  1938. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/res/templates/password_reset_confirmation.html">password_reset_confirmation.html</a>,
  1939. has been added to the <code>synapse/res/templates</code> directory. If you are
  1940. using a custom template directory, you may want to copy the template
  1941. over and modify it.</p>
  1942. <p>Note that as of v1.20.0, templates do not need to be included in custom
  1943. template directories for Synapse to start. The default templates will be
  1944. used if a custom template cannot be found.</p>
  1945. <p>This page will appear to the user after clicking a password reset link
  1946. that has been emailed to them.</p>
  1947. <p>To complete password reset, the page must include a way to make a
  1948. <code>POST</code> request to
  1949. <code>/_synapse/client/password_reset/{medium}/submit_token</code> with the query
  1950. parameters from the original link, presented as a URL-encoded form. See
  1951. the file itself for more details.</p>
  1952. <h2 id="updated-single-sign-on-html-templates"><a class="header" href="#updated-single-sign-on-html-templates">Updated Single Sign-on HTML Templates</a></h2>
  1953. <p>The <code>saml_error.html</code> template was removed from Synapse and replaced
  1954. with the <code>sso_error.html</code> template. If your Synapse is configured to use
  1955. SAML and a custom <code>sso_redirect_confirm_template_dir</code> configuration then
  1956. any customisations of the <code>saml_error.html</code> template will need to be
  1957. merged into the <code>sso_error.html</code> template. These templates are similar,
  1958. but the parameters are slightly different:</p>
  1959. <ul>
  1960. <li>The <code>msg</code> parameter should be renamed to <code>error_description</code>.</li>
  1961. <li>There is no longer a <code>code</code> parameter for the response code.</li>
  1962. <li>A string <code>error</code> parameter is available that includes a short hint
  1963. of why a user is seeing the error page.</li>
  1964. </ul>
  1965. <h1 id="upgrading-to-v1180"><a class="header" href="#upgrading-to-v1180">Upgrading to v1.18.0</a></h1>
  1966. <h2 id="docker--py3-suffix-will-be-removed-in-future-versions"><a class="header" href="#docker--py3-suffix-will-be-removed-in-future-versions">Docker <code>-py3</code> suffix will be removed in future versions</a></h2>
  1967. <p>From 10th August 2020, we will no longer publish Docker images with the
  1968. <code>-py3</code> tag suffix. The images tagged with the
  1969. <code>-py3</code> suffix have been identical to the non-suffixed tags
  1970. since release 0.99.0, and the suffix is obsolete.</p>
  1971. <p>On 10th August, we will remove the <code>latest-py3</code> tag.
  1972. Existing per-release tags (such as <code>v1.18.0-py3</code> will not
  1973. be removed, but no new <code>-py3</code> tags will be added.</p>
  1974. <p>Scripts relying on the <code>-py3</code> suffix will need to be
  1975. updated.</p>
  1976. <h2 id="redis-replication-is-now-recommended-in-lieu-of-tcp-replication"><a class="header" href="#redis-replication-is-now-recommended-in-lieu-of-tcp-replication">Redis replication is now recommended in lieu of TCP replication</a></h2>
  1977. <p>When setting up worker processes, we now recommend the use of a Redis
  1978. server for replication. <strong>The old direct TCP connection method is
  1979. deprecated and will be removed in a future release.</strong> See
  1980. <a href="workers.html">workers</a> for more details.</p>
  1981. <h1 id="upgrading-to-v1140"><a class="header" href="#upgrading-to-v1140">Upgrading to v1.14.0</a></h1>
  1982. <p>This version includes a database update which is run as part of the
  1983. upgrade, and which may take a couple of minutes in the case of a large
  1984. server. Synapse will not respond to HTTP requests while this update is
  1985. taking place.</p>
  1986. <h1 id="upgrading-to-v1130"><a class="header" href="#upgrading-to-v1130">Upgrading to v1.13.0</a></h1>
  1987. <h2 id="incorrect-database-migration-in-old-synapse-versions"><a class="header" href="#incorrect-database-migration-in-old-synapse-versions">Incorrect database migration in old synapse versions</a></h2>
  1988. <p>A bug was introduced in Synapse 1.4.0 which could cause the room
  1989. directory to be incomplete or empty if Synapse was upgraded directly
  1990. from v1.2.1 or earlier, to versions between v1.4.0 and v1.12.x.</p>
  1991. <p>This will <em>not</em> be a problem for Synapse installations which were:</p>
  1992. <p>: - created at v1.4.0 or later,
  1993. - upgraded via v1.3.x, or
  1994. - upgraded straight from v1.2.1 or earlier to v1.13.0 or later.</p>
  1995. <p>If completeness of the room directory is a concern, installations which
  1996. are affected can be repaired as follows:</p>
  1997. <ol>
  1998. <li>
  1999. <p>Run the following sql from a <code>psql</code> or
  2000. <code>sqlite3</code> console:</p>
  2001. <pre><code class="language-sql">INSERT INTO background_updates (update_name, progress_json, depends_on) VALUES
  2002. ('populate_stats_process_rooms', '{}', 'current_state_events_membership');
  2003. INSERT INTO background_updates (update_name, progress_json, depends_on) VALUES
  2004. ('populate_stats_process_users', '{}', 'populate_stats_process_rooms');
  2005. </code></pre>
  2006. </li>
  2007. <li>
  2008. <p>Restart synapse.</p>
  2009. </li>
  2010. </ol>
  2011. <h2 id="new-single-sign-on-html-templates"><a class="header" href="#new-single-sign-on-html-templates">New Single Sign-on HTML Templates</a></h2>
  2012. <p>New templates (<code>sso_auth_confirm.html</code>, <code>sso_auth_success.html</code>, and
  2013. <code>sso_account_deactivated.html</code>) were added to Synapse. If your Synapse
  2014. is configured to use SSO and a custom
  2015. <code>sso_redirect_confirm_template_dir</code> configuration then these templates
  2016. will need to be copied from
  2017. <a href="synapse/res/templates">synapse/res/templates</a> into that directory.</p>
  2018. <h2 id="synapse-sso-plugins-method-deprecation"><a class="header" href="#synapse-sso-plugins-method-deprecation">Synapse SSO Plugins Method Deprecation</a></h2>
  2019. <p>Plugins using the <code>complete_sso_login</code> method of
  2020. <code>synapse.module_api.ModuleApi</code> should update to using the async/await
  2021. version <code>complete_sso_login_async</code> which includes additional checks. The
  2022. non-async version is considered deprecated.</p>
  2023. <h2 id="rolling-back-to-v1124-after-a-failed-upgrade"><a class="header" href="#rolling-back-to-v1124-after-a-failed-upgrade">Rolling back to v1.12.4 after a failed upgrade</a></h2>
  2024. <p>v1.13.0 includes a lot of large changes. If something problematic
  2025. occurs, you may want to roll-back to a previous version of Synapse.
  2026. Because v1.13.0 also includes a new database schema version, reverting
  2027. that version is also required alongside the generic rollback
  2028. instructions mentioned above. In short, to roll back to v1.12.4 you need
  2029. to:</p>
  2030. <ol>
  2031. <li>
  2032. <p>Stop the server</p>
  2033. </li>
  2034. <li>
  2035. <p>Decrease the schema version in the database:</p>
  2036. <pre><code class="language-sql">UPDATE schema_version SET version = 57;
  2037. </code></pre>
  2038. </li>
  2039. <li>
  2040. <p>Downgrade Synapse by following the instructions for your
  2041. installation method in the &quot;Rolling back to older versions&quot;
  2042. section above.</p>
  2043. </li>
  2044. </ol>
  2045. <h1 id="upgrading-to-v1120"><a class="header" href="#upgrading-to-v1120">Upgrading to v1.12.0</a></h1>
  2046. <p>This version includes a database update which is run as part of the
  2047. upgrade, and which may take some time (several hours in the case of a
  2048. large server). Synapse will not respond to HTTP requests while this
  2049. update is taking place.</p>
  2050. <p>This is only likely to be a problem in the case of a server which is
  2051. participating in many rooms.</p>
  2052. <ol start="0">
  2053. <li>
  2054. <p>As with all upgrades, it is recommended that you have a recent
  2055. backup of your database which can be used for recovery in the event
  2056. of any problems.</p>
  2057. </li>
  2058. <li>
  2059. <p>As an initial check to see if you will be affected, you can try
  2060. running the following query from the <code>psql</code> or
  2061. <code>sqlite3</code> console. It is safe to run it while Synapse is
  2062. still running.</p>
  2063. <pre><code class="language-sql">SELECT MAX(q.v) FROM (
  2064. SELECT (
  2065. SELECT ej.json AS v
  2066. FROM state_events se INNER JOIN event_json ej USING (event_id)
  2067. WHERE se.room_id=rooms.room_id AND se.type='m.room.create' AND se.state_key=''
  2068. LIMIT 1
  2069. ) FROM rooms WHERE rooms.room_version IS NULL
  2070. ) q;
  2071. </code></pre>
  2072. <p>This query will take about the same amount of time as the upgrade
  2073. process: ie, if it takes 5 minutes, then it is likely that Synapse
  2074. will be unresponsive for 5 minutes during the upgrade.</p>
  2075. <p>If you consider an outage of this duration to be acceptable, no
  2076. further action is necessary and you can simply start Synapse 1.12.0.</p>
  2077. <p>If you would prefer to reduce the downtime, continue with the steps
  2078. below.</p>
  2079. </li>
  2080. <li>
  2081. <p>The easiest workaround for this issue is to manually create a new
  2082. index before upgrading. On PostgreSQL, his can be done as follows:</p>
  2083. <pre><code class="language-sql">CREATE INDEX CONCURRENTLY tmp_upgrade_1_12_0_index
  2084. ON state_events(room_id) WHERE type = 'm.room.create';
  2085. </code></pre>
  2086. <p>The above query may take some time, but is also safe to run while
  2087. Synapse is running.</p>
  2088. <p>We assume that no SQLite users have databases large enough to be
  2089. affected. If you <em>are</em> affected, you can run a similar query,
  2090. omitting the <code>CONCURRENTLY</code> keyword. Note however that this
  2091. operation may in itself cause Synapse to stop running for some time.
  2092. Synapse admins are reminded that <a href="postgres.html">SQLite is not recommended for use
  2093. outside a test environment</a>.</p>
  2094. </li>
  2095. <li>
  2096. <p>Once the index has been created, the <code>SELECT</code> query in step 1 above
  2097. should complete quickly. It is therefore safe to upgrade to Synapse
  2098. 1.12.0.</p>
  2099. </li>
  2100. <li>
  2101. <p>Once Synapse 1.12.0 has successfully started and is responding to
  2102. HTTP requests, the temporary index can be removed:</p>
  2103. <pre><code class="language-sql">DROP INDEX tmp_upgrade_1_12_0_index;
  2104. </code></pre>
  2105. </li>
  2106. </ol>
  2107. <h1 id="upgrading-to-v1100"><a class="header" href="#upgrading-to-v1100">Upgrading to v1.10.0</a></h1>
  2108. <p>Synapse will now log a warning on start up if used with a PostgreSQL
  2109. database that has a non-recommended locale set.</p>
  2110. <p>See <a href="postgres.html">Postgres</a> for details.</p>
  2111. <h1 id="upgrading-to-v180"><a class="header" href="#upgrading-to-v180">Upgrading to v1.8.0</a></h1>
  2112. <p>Specifying a <code>log_file</code> config option will now cause Synapse to refuse
  2113. to start, and should be replaced by with the <code>log_config</code> option.
  2114. Support for the <code>log_file</code> option was removed in v1.3.0 and has since
  2115. had no effect.</p>
  2116. <h1 id="upgrading-to-v170"><a class="header" href="#upgrading-to-v170">Upgrading to v1.7.0</a></h1>
  2117. <p>In an attempt to configure Synapse in a privacy preserving way, the
  2118. default behaviours of <code>allow_public_rooms_without_auth</code> and
  2119. <code>allow_public_rooms_over_federation</code> have been inverted. This means that
  2120. by default, only authenticated users querying the Client/Server API will
  2121. be able to query the room directory, and relatedly that the server will
  2122. not share room directory information with other servers over federation.</p>
  2123. <p>If your installation does not explicitly set these settings one way or
  2124. the other and you want either setting to be <code>true</code> then it will
  2125. necessary to update your homeserver configuration file accordingly.</p>
  2126. <p>For more details on the surrounding context see our
  2127. <a href="https://matrix.org/blog/2019/11/09/avoiding-unwelcome-visitors-on-private-matrix-servers">explainer</a>.</p>
  2128. <h1 id="upgrading-to-v150"><a class="header" href="#upgrading-to-v150">Upgrading to v1.5.0</a></h1>
  2129. <p>This release includes a database migration which may take several
  2130. minutes to complete if there are a large number (more than a million or
  2131. so) of entries in the <code>devices</code> table. This is only likely to a be a
  2132. problem on very large installations.</p>
  2133. <h1 id="upgrading-to-v140"><a class="header" href="#upgrading-to-v140">Upgrading to v1.4.0</a></h1>
  2134. <h2 id="new-custom-templates"><a class="header" href="#new-custom-templates">New custom templates</a></h2>
  2135. <p>If you have configured a custom template directory with the
  2136. <code>email.template_dir</code> option, be aware that there are new templates
  2137. regarding registration and threepid management (see below) that must be
  2138. included.</p>
  2139. <ul>
  2140. <li><code>registration.html</code> and <code>registration.txt</code></li>
  2141. <li><code>registration_success.html</code> and <code>registration_failure.html</code></li>
  2142. <li><code>add_threepid.html</code> and <code>add_threepid.txt</code></li>
  2143. <li><code>add_threepid_failure.html</code> and <code>add_threepid_success.html</code></li>
  2144. </ul>
  2145. <p>Synapse will expect these files to exist inside the configured template
  2146. directory, and <strong>will fail to start</strong> if they are absent. To view the
  2147. default templates, see
  2148. <a href="https://github.com/matrix-org/synapse/tree/master/synapse/res/templates">synapse/res/templates</a>.</p>
  2149. <h2 id="3pid-verification-changes"><a class="header" href="#3pid-verification-changes">3pid verification changes</a></h2>
  2150. <p><strong>Note: As of this release, users will be unable to add phone numbers or
  2151. email addresses to their accounts, without changes to the Synapse
  2152. configuration. This includes adding an email address during
  2153. registration.</strong></p>
  2154. <p>It is possible for a user to associate an email address or phone number
  2155. with their account, for a number of reasons:</p>
  2156. <ul>
  2157. <li>for use when logging in, as an alternative to the user id.</li>
  2158. <li>in the case of email, as an alternative contact to help with account
  2159. recovery.</li>
  2160. <li>in the case of email, to receive notifications of missed messages.</li>
  2161. </ul>
  2162. <p>Before an email address or phone number can be added to a user's
  2163. account, or before such an address is used to carry out a
  2164. password-reset, Synapse must confirm the operation with the owner of the
  2165. email address or phone number. It does this by sending an email or text
  2166. giving the user a link or token to confirm receipt. This process is
  2167. known as '3pid verification'. ('3pid', or 'threepid', stands for
  2168. third-party identifier, and we use it to refer to external identifiers
  2169. such as email addresses and phone numbers.)</p>
  2170. <p>Previous versions of Synapse delegated the task of 3pid verification to
  2171. an identity server by default. In most cases this server is <code>vector.im</code>
  2172. or <code>matrix.org</code>.</p>
  2173. <p>In Synapse 1.4.0, for security and privacy reasons, the homeserver will
  2174. no longer delegate this task to an identity server by default. Instead,
  2175. the server administrator will need to explicitly decide how they would
  2176. like the verification messages to be sent.</p>
  2177. <p>In the medium term, the <code>vector.im</code> and <code>matrix.org</code> identity servers
  2178. will disable support for delegated 3pid verification entirely. However,
  2179. in order to ease the transition, they will retain the capability for a
  2180. limited period. Delegated email verification will be disabled on Monday
  2181. 2nd December 2019 (giving roughly 2 months notice). Disabling delegated
  2182. SMS verification will follow some time after that once SMS verification
  2183. support lands in Synapse.</p>
  2184. <p>Once delegated 3pid verification support has been disabled in the
  2185. <code>vector.im</code> and <code>matrix.org</code> identity servers, all Synapse versions that
  2186. depend on those instances will be unable to verify email and phone
  2187. numbers through them. There are no imminent plans to remove delegated
  2188. 3pid verification from Sydent generally. (Sydent is the identity server
  2189. project that backs the <code>vector.im</code> and <code>matrix.org</code> instances).</p>
  2190. <h3 id="email-1"><a class="header" href="#email-1">Email</a></h3>
  2191. <p>Following upgrade, to continue verifying email (e.g. as part of the
  2192. registration process), admins can either:-</p>
  2193. <ul>
  2194. <li>Configure Synapse to use an email server.</li>
  2195. <li>Run or choose an identity server which allows delegated email
  2196. verification and delegate to it.</li>
  2197. </ul>
  2198. <h4 id="configure-smtp-in-synapse"><a class="header" href="#configure-smtp-in-synapse">Configure SMTP in Synapse</a></h4>
  2199. <p>To configure an SMTP server for Synapse, modify the configuration
  2200. section headed <code>email</code>, and be sure to have at least the
  2201. <code>smtp_host, smtp_port</code> and <code>notif_from</code> fields filled out.</p>
  2202. <p>You may also need to set <code>smtp_user</code>, <code>smtp_pass</code>, and
  2203. <code>require_transport_security</code>.</p>
  2204. <p>See the <a href="usage/configuration/homeserver_sample_config.html">sample configuration file</a>
  2205. for more details on these settings.</p>
  2206. <h4 id="delegate-email-to-an-identity-server"><a class="header" href="#delegate-email-to-an-identity-server">Delegate email to an identity server</a></h4>
  2207. <p>Some admins will wish to continue using email verification as part of
  2208. the registration process, but will not immediately have an appropriate
  2209. SMTP server at hand.</p>
  2210. <p>To this end, we will continue to support email verification delegation
  2211. via the <code>vector.im</code> and <code>matrix.org</code> identity servers for two months.
  2212. Support for delegated email verification will be disabled on Monday 2nd
  2213. December.</p>
  2214. <p>The <code>account_threepid_delegates</code> dictionary defines whether the
  2215. homeserver should delegate an external server (typically an <a href="https://matrix.org/docs/spec/identity_service/r0.2.1">identity
  2216. server</a>) to handle
  2217. sending confirmation messages via email and SMS.</p>
  2218. <p>So to delegate email verification, in <code>homeserver.yaml</code>, set
  2219. <code>account_threepid_delegates.email</code> to the base URL of an identity
  2220. server. For example:</p>
  2221. <pre><code class="language-yaml">account_threepid_delegates:
  2222. email: https://example.com # Delegate email sending to example.com
  2223. </code></pre>
  2224. <p>Note that <code>account_threepid_delegates.email</code> replaces the deprecated
  2225. <code>email.trust_identity_server_for_password_resets</code>: if
  2226. <code>email.trust_identity_server_for_password_resets</code> is set to <code>true</code>, and
  2227. <code>account_threepid_delegates.email</code> is not set, then the first entry in
  2228. <code>trusted_third_party_id_servers</code> will be used as the
  2229. <code>account_threepid_delegate</code> for email. This is to ensure compatibility
  2230. with existing Synapse installs that set up external server handling for
  2231. these tasks before v1.4.0. If
  2232. <code>email.trust_identity_server_for_password_resets</code> is <code>true</code> and no
  2233. trusted identity server domains are configured, Synapse will report an
  2234. error and refuse to start.</p>
  2235. <p>If <code>email.trust_identity_server_for_password_resets</code> is <code>false</code> or
  2236. absent and no <code>email</code> delegate is configured in
  2237. <code>account_threepid_delegates</code>, then Synapse will send email verification
  2238. messages itself, using the configured SMTP server (see above). that
  2239. type.</p>
  2240. <h3 id="phone-numbers"><a class="header" href="#phone-numbers">Phone numbers</a></h3>
  2241. <p>Synapse does not support phone-number verification itself, so the only
  2242. way to maintain the ability for users to add phone numbers to their
  2243. accounts will be by continuing to delegate phone number verification to
  2244. the <code>matrix.org</code> and <code>vector.im</code> identity servers (or another identity
  2245. server that supports SMS sending).</p>
  2246. <p>The <code>account_threepid_delegates</code> dictionary defines whether the
  2247. homeserver should delegate an external server (typically an <a href="https://matrix.org/docs/spec/identity_service/r0.2.1">identity
  2248. server</a>) to handle
  2249. sending confirmation messages via email and SMS.</p>
  2250. <p>So to delegate phone number verification, in <code>homeserver.yaml</code>, set
  2251. <code>account_threepid_delegates.msisdn</code> to the base URL of an identity
  2252. server. For example:</p>
  2253. <pre><code class="language-yaml">account_threepid_delegates:
  2254. msisdn: https://example.com # Delegate sms sending to example.com
  2255. </code></pre>
  2256. <p>The <code>matrix.org</code> and <code>vector.im</code> identity servers will continue to
  2257. support delegated phone number verification via SMS until such time as
  2258. it is possible for admins to configure their servers to perform phone
  2259. number verification directly. More details will follow in a future
  2260. release.</p>
  2261. <h2 id="rolling-back-to-v131"><a class="header" href="#rolling-back-to-v131">Rolling back to v1.3.1</a></h2>
  2262. <p>If you encounter problems with v1.4.0, it should be possible to roll
  2263. back to v1.3.1, subject to the following:</p>
  2264. <ul>
  2265. <li>
  2266. <p>The 'room statistics' engine was heavily reworked in this release
  2267. (see <a href="https://github.com/matrix-org/synapse/pull/5971">#5971</a>),
  2268. including significant changes to the database schema, which are not
  2269. easily reverted. This will cause the room statistics engine to stop
  2270. updating when you downgrade.</p>
  2271. <p>The room statistics are essentially unused in v1.3.1 (in future
  2272. versions of Synapse, they will be used to populate the room
  2273. directory), so there should be no loss of functionality. However,
  2274. the statistics engine will write errors to the logs, which can be
  2275. avoided by setting the following in <code>homeserver.yaml</code>:</p>
  2276. <pre><code class="language-yaml">stats:
  2277. enabled: false
  2278. </code></pre>
  2279. <p>Don't forget to re-enable it when you upgrade again, in preparation
  2280. for its use in the room directory!</p>
  2281. </li>
  2282. </ul>
  2283. <h1 id="upgrading-to-v120"><a class="header" href="#upgrading-to-v120">Upgrading to v1.2.0</a></h1>
  2284. <p>Some counter metrics have been renamed, with the old names deprecated.
  2285. See <a href="metrics-howto.html#renaming-of-metrics--deprecation-of-old-names-in-12">the metrics
  2286. documentation</a>
  2287. for details.</p>
  2288. <h1 id="upgrading-to-v110"><a class="header" href="#upgrading-to-v110">Upgrading to v1.1.0</a></h1>
  2289. <p>Synapse v1.1.0 removes support for older Python and PostgreSQL versions,
  2290. as outlined in <a href="https://matrix.org/blog/2019/04/08/synapse-deprecating-postgres-9-4-and-python-2-x">our deprecation
  2291. notice</a>.</p>
  2292. <h2 id="minimum-python-version"><a class="header" href="#minimum-python-version">Minimum Python Version</a></h2>
  2293. <p>Synapse v1.1.0 has a minimum Python requirement of Python 3.5. Python
  2294. 3.6 or Python 3.7 are recommended as they have improved internal string
  2295. handling, significantly reducing memory usage.</p>
  2296. <p>If you use current versions of the Matrix.org-distributed Debian
  2297. packages or Docker images, action is not required.</p>
  2298. <p>If you install Synapse in a Python virtual environment, please see
  2299. &quot;Upgrading to v0.34.0&quot; for notes on setting up a new virtualenv under
  2300. Python 3.</p>
  2301. <h2 id="minimum-postgresql-version"><a class="header" href="#minimum-postgresql-version">Minimum PostgreSQL Version</a></h2>
  2302. <p>If using PostgreSQL under Synapse, you will need to use PostgreSQL 9.5
  2303. or above. Please see the <a href="https://www.postgresql.org/docs/11/upgrading.html">PostgreSQL
  2304. documentation</a> for
  2305. more details on upgrading your database.</p>
  2306. <h1 id="upgrading-to-v10"><a class="header" href="#upgrading-to-v10">Upgrading to v1.0</a></h1>
  2307. <h2 id="validation-of-tls-certificates"><a class="header" href="#validation-of-tls-certificates">Validation of TLS certificates</a></h2>
  2308. <p>Synapse v1.0 is the first release to enforce validation of TLS
  2309. certificates for the federation API. It is therefore essential that your
  2310. certificates are correctly configured. See the
  2311. <a href="MSC1711_certificates_FAQ.html">FAQ</a> for more information.</p>
  2312. <p>Note, v1.0 installations will also no longer be able to federate with
  2313. servers that have not correctly configured their certificates.</p>
  2314. <p>In rare cases, it may be desirable to disable certificate checking: for
  2315. example, it might be essential to be able to federate with a given
  2316. legacy server in a closed federation. This can be done in one of two
  2317. ways:-</p>
  2318. <ul>
  2319. <li>Configure the global switch <code>federation_verify_certificates</code> to
  2320. <code>false</code>.</li>
  2321. <li>Configure a whitelist of server domains to trust via
  2322. <code>federation_certificate_verification_whitelist</code>.</li>
  2323. </ul>
  2324. <p>See the <a href="usage/configuration/homeserver_sample_config.html">sample configuration file</a>
  2325. for more details on these settings.</p>
  2326. <h2 id="email-2"><a class="header" href="#email-2">Email</a></h2>
  2327. <p>When a user requests a password reset, Synapse will send an email to the
  2328. user to confirm the request.</p>
  2329. <p>Previous versions of Synapse delegated the job of sending this email to
  2330. an identity server. If the identity server was somehow malicious or
  2331. became compromised, it would be theoretically possible to hijack an
  2332. account through this means.</p>
  2333. <p>Therefore, by default, Synapse v1.0 will send the confirmation email
  2334. itself. If Synapse is not configured with an SMTP server, password reset
  2335. via email will be disabled.</p>
  2336. <p>To configure an SMTP server for Synapse, modify the configuration
  2337. section headed <code>email</code>, and be sure to have at least the <code>smtp_host</code>,
  2338. <code>smtp_port</code> and <code>notif_from</code> fields filled out. You may also need to set
  2339. <code>smtp_user</code>, <code>smtp_pass</code>, and <code>require_transport_security</code>.</p>
  2340. <p>If you are absolutely certain that you wish to continue using an
  2341. identity server for password resets, set
  2342. <code>trust_identity_server_for_password_resets</code> to <code>true</code>.</p>
  2343. <p>See the <a href="usage/configuration/homeserver_sample_config.html">sample configuration file</a>
  2344. for more details on these settings.</p>
  2345. <h2 id="new-email-templates"><a class="header" href="#new-email-templates">New email templates</a></h2>
  2346. <p>Some new templates have been added to the default template directory for the purpose of
  2347. the homeserver sending its own password reset emails. If you have configured a
  2348. custom <code>template_dir</code> in your Synapse config, these files will need to be added.</p>
  2349. <p><code>password_reset.html</code> and <code>password_reset.txt</code> are HTML and plain text
  2350. templates respectively that contain the contents of what will be emailed
  2351. to the user upon attempting to reset their password via email.
  2352. <code>password_reset_success.html</code> and <code>password_reset_failure.html</code> are HTML
  2353. files that the content of which (assuming no redirect URL is set) will
  2354. be shown to the user after they attempt to click the link in the email
  2355. sent to them.</p>
  2356. <h1 id="upgrading-to-v0990"><a class="header" href="#upgrading-to-v0990">Upgrading to v0.99.0</a></h1>
  2357. <p>Please be aware that, before Synapse v1.0 is released around March 2019,
  2358. you will need to replace any self-signed certificates with those
  2359. verified by a root CA. Information on how to do so can be found at the
  2360. ACME docs.</p>
  2361. <p>For more information on configuring TLS certificates see the
  2362. <a href="MSC1711_certificates_FAQ.html">FAQ</a>.</p>
  2363. <h1 id="upgrading-to-v0340"><a class="header" href="#upgrading-to-v0340">Upgrading to v0.34.0</a></h1>
  2364. <ol>
  2365. <li>
  2366. <p>This release is the first to fully support Python 3. Synapse will
  2367. now run on Python versions 3.5, or 3.6 (as well as 2.7). We
  2368. recommend switching to Python 3, as it has been shown to give
  2369. performance improvements.</p>
  2370. <p>For users who have installed Synapse into a virtualenv, we recommend
  2371. doing this by creating a new virtualenv. For example:</p>
  2372. <pre><code class="language-sh">virtualenv -p python3 ~/synapse/env3
  2373. source ~/synapse/env3/bin/activate
  2374. pip install matrix-synapse
  2375. </code></pre>
  2376. <p>You can then start synapse as normal, having activated the new
  2377. virtualenv:</p>
  2378. <pre><code class="language-sh">cd ~/synapse
  2379. source env3/bin/activate
  2380. synctl start
  2381. </code></pre>
  2382. <p>Users who have installed from distribution packages should see the
  2383. relevant package documentation. See below for notes on Debian
  2384. packages.</p>
  2385. <ul>
  2386. <li>
  2387. <p>When upgrading to Python 3, you <strong>must</strong> make sure that your log
  2388. files are configured as UTF-8, by adding <code>encoding: utf8</code> to the
  2389. <code>RotatingFileHandler</code> configuration (if you have one) in your
  2390. <code>&lt;server&gt;.log.config</code> file. For example, if your <code>log.config</code>
  2391. file contains:</p>
  2392. <pre><code class="language-yaml">handlers:
  2393. file:
  2394. class: logging.handlers.RotatingFileHandler
  2395. formatter: precise
  2396. filename: homeserver.log
  2397. maxBytes: 104857600
  2398. backupCount: 10
  2399. filters: [context]
  2400. console:
  2401. class: logging.StreamHandler
  2402. formatter: precise
  2403. filters: [context]
  2404. </code></pre>
  2405. <p>Then you should update this to be:</p>
  2406. <pre><code class="language-yaml">handlers:
  2407. file:
  2408. class: logging.handlers.RotatingFileHandler
  2409. formatter: precise
  2410. filename: homeserver.log
  2411. maxBytes: 104857600
  2412. backupCount: 10
  2413. filters: [context]
  2414. encoding: utf8
  2415. console:
  2416. class: logging.StreamHandler
  2417. formatter: precise
  2418. filters: [context]
  2419. </code></pre>
  2420. <p>There is no need to revert this change if downgrading to
  2421. Python 2.</p>
  2422. </li>
  2423. </ul>
  2424. <p>We are also making available Debian packages which will run Synapse
  2425. on Python 3. You can switch to these packages with
  2426. <code>apt-get install matrix-synapse-py3</code>, however, please read
  2427. <a href="https://github.com/matrix-org/synapse/blob/release-v0.34.0/debian/NEWS">debian/NEWS</a>
  2428. before doing so. The existing <code>matrix-synapse</code> packages will
  2429. continue to use Python 2 for the time being.</p>
  2430. </li>
  2431. <li>
  2432. <p>This release removes the <code>riot.im</code> from the default list of trusted
  2433. identity servers.</p>
  2434. <p>If <code>riot.im</code> is in your homeserver's list of
  2435. <code>trusted_third_party_id_servers</code>, you should remove it. It was added
  2436. in case a hypothetical future identity server was put there. If you
  2437. don't remove it, users may be unable to deactivate their accounts.</p>
  2438. </li>
  2439. <li>
  2440. <p>This release no longer installs the (unmaintained) Matrix Console
  2441. web client as part of the default installation. It is possible to
  2442. re-enable it by installing it separately and setting the
  2443. <code>web_client_location</code> config option, but please consider switching
  2444. to another client.</p>
  2445. </li>
  2446. </ol>
  2447. <h1 id="upgrading-to-v0337"><a class="header" href="#upgrading-to-v0337">Upgrading to v0.33.7</a></h1>
  2448. <p>This release removes the example email notification templates from
  2449. <code>res/templates</code> (they are now internal to the python package). This
  2450. should only affect you if you (a) deploy your Synapse instance from a
  2451. git checkout or a github snapshot URL, and (b) have email notifications
  2452. enabled.</p>
  2453. <p>If you have email notifications enabled, you should ensure that
  2454. <code>email.template_dir</code> is either configured to point at a directory where
  2455. you have installed customised templates, or leave it unset to use the
  2456. default templates.</p>
  2457. <h1 id="upgrading-to-v0273"><a class="header" href="#upgrading-to-v0273">Upgrading to v0.27.3</a></h1>
  2458. <p>This release expands the anonymous usage stats sent if the opt-in
  2459. <code>report_stats</code> configuration is set to <code>true</code>. We now capture RSS memory
  2460. and cpu use at a very coarse level. This requires administrators to
  2461. install the optional <code>psutil</code> python module.</p>
  2462. <p>We would appreciate it if you could assist by ensuring this module is
  2463. available and <code>report_stats</code> is enabled. This will let us see if
  2464. performance changes to synapse are having an impact to the general
  2465. community.</p>
  2466. <h1 id="upgrading-to-v0150"><a class="header" href="#upgrading-to-v0150">Upgrading to v0.15.0</a></h1>
  2467. <p>If you want to use the new URL previewing API
  2468. (<code>/_matrix/media/r0/preview_url</code>) then you have to explicitly enable it
  2469. in the config and update your dependencies dependencies. See README.rst
  2470. for details.</p>
  2471. <h1 id="upgrading-to-v0110"><a class="header" href="#upgrading-to-v0110">Upgrading to v0.11.0</a></h1>
  2472. <p>This release includes the option to send anonymous usage stats to
  2473. matrix.org, and requires that administrators explictly opt in or out by
  2474. setting the <code>report_stats</code> option to either <code>true</code> or <code>false</code>.</p>
  2475. <p>We would really appreciate it if you could help our project out by
  2476. reporting anonymized usage statistics from your homeserver. Only very
  2477. basic aggregate data (e.g. number of users) will be reported, but it
  2478. helps us to track the growth of the Matrix community, and helps us to
  2479. make Matrix a success, as well as to convince other networks that they
  2480. should peer with us.</p>
  2481. <h1 id="upgrading-to-v090"><a class="header" href="#upgrading-to-v090">Upgrading to v0.9.0</a></h1>
  2482. <p>Application services have had a breaking API change in this version.</p>
  2483. <p>They can no longer register themselves with a home server using the AS
  2484. HTTP API. This decision was made because a compromised application
  2485. service with free reign to register any regex in effect grants full
  2486. read/write access to the home server if a regex of <code>.*</code> is used. An
  2487. attack where a compromised AS re-registers itself with <code>.*</code> was deemed
  2488. too big of a security risk to ignore, and so the ability to register
  2489. with the HS remotely has been removed.</p>
  2490. <p>It has been replaced by specifying a list of application service
  2491. registrations in <code>homeserver.yaml</code>:</p>
  2492. <pre><code class="language-yaml">app_service_config_files: [&quot;registration-01.yaml&quot;, &quot;registration-02.yaml&quot;]
  2493. </code></pre>
  2494. <p>Where <code>registration-01.yaml</code> looks like:</p>
  2495. <pre><code class="language-yaml">url: &lt;String&gt; # e.g. &quot;https://my.application.service.com&quot;
  2496. as_token: &lt;String&gt;
  2497. hs_token: &lt;String&gt;
  2498. sender_localpart: &lt;String&gt; # This is a new field which denotes the user_id localpart when using the AS token
  2499. namespaces:
  2500. users:
  2501. - exclusive: &lt;Boolean&gt;
  2502. regex: &lt;String&gt; # e.g. &quot;@prefix_.*&quot;
  2503. aliases:
  2504. - exclusive: &lt;Boolean&gt;
  2505. regex: &lt;String&gt;
  2506. rooms:
  2507. - exclusive: &lt;Boolean&gt;
  2508. regex: &lt;String&gt;
  2509. </code></pre>
  2510. <h1 id="upgrading-to-v080"><a class="header" href="#upgrading-to-v080">Upgrading to v0.8.0</a></h1>
  2511. <p>Servers which use captchas will need to add their public key to:</p>
  2512. <pre><code>static/client/register/register_config.js
  2513. window.matrixRegistrationConfig = {
  2514. recaptcha_public_key: &quot;YOUR_PUBLIC_KEY&quot;
  2515. };
  2516. </code></pre>
  2517. <p>This is required in order to support registration fallback (typically
  2518. used on mobile devices).</p>
  2519. <h1 id="upgrading-to-v070"><a class="header" href="#upgrading-to-v070">Upgrading to v0.7.0</a></h1>
  2520. <p>New dependencies are:</p>
  2521. <ul>
  2522. <li>pydenticon</li>
  2523. <li>simplejson</li>
  2524. <li>syutil</li>
  2525. <li>matrix-angular-sdk</li>
  2526. </ul>
  2527. <p>To pull in these dependencies in a virtual env, run:</p>
  2528. <pre><code>python synapse/python_dependencies.py | xargs -n 1 pip install
  2529. </code></pre>
  2530. <h1 id="upgrading-to-v060"><a class="header" href="#upgrading-to-v060">Upgrading to v0.6.0</a></h1>
  2531. <p>To pull in new dependencies, run:</p>
  2532. <pre><code>python setup.py develop --user
  2533. </code></pre>
  2534. <p>This update includes a change to the database schema. To upgrade you
  2535. first need to upgrade the database by running:</p>
  2536. <pre><code>python scripts/upgrade_db_to_v0.6.0.py &lt;db&gt; &lt;server_name&gt; &lt;signing_key&gt;
  2537. </code></pre>
  2538. <p>Where <code>&lt;db&gt;</code> is the location of the database,
  2539. <code>&lt;server_name&gt;</code> is the server name as specified in the
  2540. synapse configuration, and <code>&lt;signing_key&gt;</code> is the location
  2541. of the signing key as specified in the synapse configuration.</p>
  2542. <p>This may take some time to complete. Failures of signatures and content
  2543. hashes can safely be ignored.</p>
  2544. <h1 id="upgrading-to-v051"><a class="header" href="#upgrading-to-v051">Upgrading to v0.5.1</a></h1>
  2545. <p>Depending on precisely when you installed v0.5.0 you may have ended up
  2546. with a stale release of the reference matrix webclient installed as a
  2547. python module. To uninstall it and ensure you are depending on the
  2548. latest module, please run:</p>
  2549. <pre><code>$ pip uninstall syweb
  2550. </code></pre>
  2551. <h1 id="upgrading-to-v050"><a class="header" href="#upgrading-to-v050">Upgrading to v0.5.0</a></h1>
  2552. <p>The webclient has been split out into a seperate repository/pacakage in
  2553. this release. Before you restart your homeserver you will need to pull
  2554. in the webclient package by running:</p>
  2555. <pre><code>python setup.py develop --user
  2556. </code></pre>
  2557. <p>This release completely changes the database schema and so requires
  2558. upgrading it before starting the new version of the homeserver.</p>
  2559. <p>The script &quot;database-prepare-for-0.5.0.sh&quot; should be used to upgrade
  2560. the database. This will save all user information, such as logins and
  2561. profiles, but will otherwise purge the database. This includes messages,
  2562. which rooms the home server was a member of and room alias mappings.</p>
  2563. <p>If you would like to keep your history, please take a copy of your
  2564. database file and ask for help in #matrix:matrix.org. The upgrade
  2565. process is, unfortunately, non trivial and requires human intervention
  2566. to resolve any resulting conflicts during the upgrade process.</p>
  2567. <p>Before running the command the homeserver should be first completely
  2568. shutdown. To run it, simply specify the location of the database, e.g.:</p>
  2569. <blockquote>
  2570. <p>./scripts/database-prepare-for-0.5.0.sh &quot;homeserver.db&quot;</p>
  2571. </blockquote>
  2572. <p>Once this has successfully completed it will be safe to restart the
  2573. homeserver. You may notice that the homeserver takes a few seconds
  2574. longer to restart than usual as it reinitializes the database.</p>
  2575. <p>On startup of the new version, users can either rejoin remote rooms
  2576. using room aliases or by being reinvited. Alternatively, if any other
  2577. homeserver sends a message to a room that the homeserver was previously
  2578. in the local HS will automatically rejoin the room.</p>
  2579. <h1 id="upgrading-to-v040"><a class="header" href="#upgrading-to-v040">Upgrading to v0.4.0</a></h1>
  2580. <p>This release needs an updated syutil version. Run:</p>
  2581. <pre><code>python setup.py develop
  2582. </code></pre>
  2583. <p>You will also need to upgrade your configuration as the signing key
  2584. format has changed. Run:</p>
  2585. <pre><code>python -m synapse.app.homeserver --config-path &lt;CONFIG&gt; --generate-config
  2586. </code></pre>
  2587. <h1 id="upgrading-to-v030"><a class="header" href="#upgrading-to-v030">Upgrading to v0.3.0</a></h1>
  2588. <p>This registration API now closely matches the login API. This introduces
  2589. a bit more backwards and forwards between the HS and the client, but
  2590. this improves the overall flexibility of the API. You can now GET on
  2591. /register to retrieve a list of valid registration flows. Upon choosing
  2592. one, they are submitted in the same way as login, e.g:</p>
  2593. <pre><code>{
  2594. type: m.login.password,
  2595. user: foo,
  2596. password: bar
  2597. }
  2598. </code></pre>
  2599. <p>The default HS supports 2 flows, with and without Identity Server email
  2600. authentication. Enabling captcha on the HS will add in an extra step to
  2601. all flows: <code>m.login.recaptcha</code> which must be completed before you can
  2602. transition to the next stage. There is a new login type:
  2603. <code>m.login.email.identity</code> which contains the <code>threepidCreds</code> key which
  2604. were previously sent in the original register request. For more
  2605. information on this, see the specification.</p>
  2606. <h2 id="web-client"><a class="header" href="#web-client">Web Client</a></h2>
  2607. <p>The VoIP specification has changed between v0.2.0 and v0.3.0. Users
  2608. should refresh any browser tabs to get the latest web client code. Users
  2609. on v0.2.0 of the web client will not be able to call those on v0.3.0 and
  2610. vice versa.</p>
  2611. <h1 id="upgrading-to-v020"><a class="header" href="#upgrading-to-v020">Upgrading to v0.2.0</a></h1>
  2612. <p>The home server now requires setting up of SSL config before it can run.
  2613. To automatically generate default config use:</p>
  2614. <pre><code>$ python synapse/app/homeserver.py \
  2615. --server-name machine.my.domain.name \
  2616. --bind-port 8448 \
  2617. --config-path homeserver.config \
  2618. --generate-config
  2619. </code></pre>
  2620. <p>This config can be edited if desired, for example to specify a different
  2621. SSL certificate to use. Once done you can run the home server using:</p>
  2622. <pre><code>$ python synapse/app/homeserver.py --config-path homeserver.config
  2623. </code></pre>
  2624. <p>See the README.rst for more information.</p>
  2625. <p>Also note that some config options have been renamed, including:</p>
  2626. <ul>
  2627. <li>&quot;host&quot; to &quot;server-name&quot;</li>
  2628. <li>&quot;database&quot; to &quot;database-path&quot;</li>
  2629. <li>&quot;port&quot; to &quot;bind-port&quot; and &quot;unsecure-port&quot;</li>
  2630. </ul>
  2631. <h1 id="upgrading-to-v001"><a class="header" href="#upgrading-to-v001">Upgrading to v0.0.1</a></h1>
  2632. <p>This release completely changes the database schema and so requires
  2633. upgrading it before starting the new version of the homeserver.</p>
  2634. <p>The script &quot;database-prepare-for-0.0.1.sh&quot; should be used to upgrade
  2635. the database. This will save all user information, such as logins and
  2636. profiles, but will otherwise purge the database. This includes messages,
  2637. which rooms the home server was a member of and room alias mappings.</p>
  2638. <p>Before running the command the homeserver should be first completely
  2639. shutdown. To run it, simply specify the location of the database, e.g.:</p>
  2640. <blockquote>
  2641. <p>./scripts/database-prepare-for-0.0.1.sh &quot;homeserver.db&quot;</p>
  2642. </blockquote>
  2643. <p>Once this has successfully completed it will be safe to restart the
  2644. homeserver. You may notice that the homeserver takes a few seconds
  2645. longer to restart than usual as it reinitializes the database.</p>
  2646. <p>On startup of the new version, users can either rejoin remote rooms
  2647. using room aliases or by being reinvited. Alternatively, if any other
  2648. homeserver sends a message to a room that the homeserver was previously
  2649. in the local HS will automatically rejoin the room.</p>
  2650. <div style="break-before: page; page-break-before: always;"></div><h1 id="msc1711-certificates-faq"><a class="header" href="#msc1711-certificates-faq">MSC1711 Certificates FAQ</a></h1>
  2651. <h2 id="historical-note"><a class="header" href="#historical-note">Historical Note</a></h2>
  2652. <p>This document was originally written to guide server admins through the upgrade
  2653. path towards Synapse 1.0. Specifically,
  2654. <a href="https://github.com/matrix-org/matrix-doc/blob/main/proposals/1711-x509-for-federation.md">MSC1711</a>
  2655. required that all servers present valid TLS certificates on their federation
  2656. API. Admins were encouraged to achieve compliance from version 0.99.0 (released
  2657. in February 2019) ahead of version 1.0 (released June 2019) enforcing the
  2658. certificate checks.</p>
  2659. <p>Much of what follows is now outdated since most admins will have already
  2660. upgraded, however it may be of use to those with old installs returning to the
  2661. project.</p>
  2662. <p>If you are setting up a server from scratch you almost certainly should look at
  2663. the <a href="setup/installation.html">installation guide</a> instead.</p>
  2664. <h2 id="introduction-1"><a class="header" href="#introduction-1">Introduction</a></h2>
  2665. <p>The goal of Synapse 0.99.0 is to act as a stepping stone to Synapse 1.0.0. It
  2666. supports the r0.1 release of the server to server specification, but is
  2667. compatible with both the legacy Matrix federation behaviour (pre-r0.1) as well
  2668. as post-r0.1 behaviour, in order to allow for a smooth upgrade across the
  2669. federation.</p>
  2670. <p>The most important thing to know is that Synapse 1.0.0 will require a valid TLS
  2671. certificate on federation endpoints. Self signed certificates will not be
  2672. sufficient.</p>
  2673. <p>Synapse 0.99.0 makes it easy to configure TLS certificates and will
  2674. interoperate with both &gt;= 1.0.0 servers as well as existing servers yet to
  2675. upgrade.</p>
  2676. <p><strong>It is critical that all admins upgrade to 0.99.0 and configure a valid TLS
  2677. certificate.</strong> Admins will have 1 month to do so, after which 1.0.0 will be
  2678. released and those servers without a valid certificate will not longer be able
  2679. to federate with &gt;= 1.0.0 servers.</p>
  2680. <p>Full details on how to carry out this configuration change is given
  2681. <a href="MSC1711_certificates_FAQ.html#configuring-certificates-for-compatibility-with-synapse-100">below</a>. A
  2682. timeline and some frequently asked questions are also given below.</p>
  2683. <p>For more details and context on the release of the r0.1 Server/Server API and
  2684. imminent Matrix 1.0 release, you can also see our
  2685. <a href="https://matrix.org/blog/2019/02/04/matrix-at-fosdem-2019/">main talk from FOSDEM 2019</a>.</p>
  2686. <h2 id="contents"><a class="header" href="#contents">Contents</a></h2>
  2687. <ul>
  2688. <li>Timeline</li>
  2689. <li>Configuring certificates for compatibility with Synapse 1.0</li>
  2690. <li>FAQ
  2691. <ul>
  2692. <li>Synapse 0.99.0 has just been released, what do I need to do right now?</li>
  2693. <li>How do I upgrade?</li>
  2694. <li>What will happen if I do not set up a valid federation certificate
  2695. immediately?</li>
  2696. <li>What will happen if I do nothing at all?</li>
  2697. <li>When do I need a SRV record or .well-known URI?</li>
  2698. <li>Can I still use an SRV record?</li>
  2699. <li>I have created a .well-known URI. Do I still need an SRV record?</li>
  2700. <li>It used to work just fine, why are you breaking everything?</li>
  2701. <li>Can I manage my own certificates rather than having Synapse renew
  2702. certificates itself?</li>
  2703. <li>Do you still recommend against using a reverse proxy on the federation port?</li>
  2704. <li>Do I still need to give my TLS certificates to Synapse if I am using a
  2705. reverse proxy?</li>
  2706. <li>Do I need the same certificate for the client and federation port?</li>
  2707. <li>How do I tell Synapse to reload my keys/certificates after I replace them?</li>
  2708. </ul>
  2709. </li>
  2710. </ul>
  2711. <h2 id="timeline"><a class="header" href="#timeline">Timeline</a></h2>
  2712. <p><strong>5th Feb 2019 - Synapse 0.99.0 is released.</strong></p>
  2713. <p>All server admins are encouraged to upgrade.</p>
  2714. <p>0.99.0:</p>
  2715. <ul>
  2716. <li>
  2717. <p>provides support for ACME to make setting up Let's Encrypt certs easy, as
  2718. well as .well-known support.</p>
  2719. </li>
  2720. <li>
  2721. <p>does not enforce that a valid CA cert is present on the federation API, but
  2722. rather makes it easy to set one up.</p>
  2723. </li>
  2724. <li>
  2725. <p>provides support for .well-known</p>
  2726. </li>
  2727. </ul>
  2728. <p>Admins should upgrade and configure a valid CA cert. Homeservers that require a
  2729. .well-known entry (see below), should retain their SRV record and use it
  2730. alongside their .well-known record.</p>
  2731. <p><strong>10th June 2019 - Synapse 1.0.0 is released</strong></p>
  2732. <p>1.0.0 is scheduled for release on 10th June. In
  2733. accordance with the the <a href="https://matrix.org/docs/spec/server_server/r0.1.0.html">S2S spec</a>
  2734. 1.0.0 will enforce certificate validity. This means that any homeserver without a
  2735. valid certificate after this point will no longer be able to federate with
  2736. 1.0.0 servers.</p>
  2737. <h2 id="configuring-certificates-for-compatibility-with-synapse-100"><a class="header" href="#configuring-certificates-for-compatibility-with-synapse-100">Configuring certificates for compatibility with Synapse 1.0.0</a></h2>
  2738. <h3 id="if-you-do-not-currently-have-an-srv-record"><a class="header" href="#if-you-do-not-currently-have-an-srv-record">If you do not currently have an SRV record</a></h3>
  2739. <p>In this case, your <code>server_name</code> points to the host where your Synapse is
  2740. running. There is no need to create a <code>.well-known</code> URI or an SRV record, but
  2741. you will need to give Synapse a valid, signed, certificate.</p>
  2742. <h3 id="if-you-do-have-an-srv-record-currently"><a class="header" href="#if-you-do-have-an-srv-record-currently">If you do have an SRV record currently</a></h3>
  2743. <p>If you are using an SRV record, your matrix domain (<code>server_name</code>) may not
  2744. point to the same host that your Synapse is running on (the 'target
  2745. domain'). (If it does, you can follow the recommendation above; otherwise, read
  2746. on.)</p>
  2747. <p>Let's assume that your <code>server_name</code> is <code>example.com</code>, and your Synapse is
  2748. hosted at a target domain of <code>customer.example.net</code>. Currently you should have
  2749. an SRV record which looks like:</p>
  2750. <pre><code>_matrix._tcp.example.com. IN SRV 10 5 8000 customer.example.net.
  2751. </code></pre>
  2752. <p>In this situation, you have three choices for how to proceed:</p>
  2753. <h4 id="option-1-give-synapse-a-certificate-for-your-matrix-domain"><a class="header" href="#option-1-give-synapse-a-certificate-for-your-matrix-domain">Option 1: give Synapse a certificate for your matrix domain</a></h4>
  2754. <p>Synapse 1.0 will expect your server to present a TLS certificate for your
  2755. <code>server_name</code> (<code>example.com</code> in the above example). You can achieve this by acquiring a
  2756. certificate for the <code>server_name</code> yourself (for example, using <code>certbot</code>), and giving it
  2757. and the key to Synapse via <code>tls_certificate_path</code> and <code>tls_private_key_path</code>.</p>
  2758. <h4 id="option-2-run-synapse-behind-a-reverse-proxy"><a class="header" href="#option-2-run-synapse-behind-a-reverse-proxy">Option 2: run Synapse behind a reverse proxy</a></h4>
  2759. <p>If you have an existing reverse proxy set up with correct TLS certificates for
  2760. your domain, you can simply route all traffic through the reverse proxy by
  2761. updating the SRV record appropriately (or removing it, if the proxy listens on
  2762. 8448).</p>
  2763. <p>See <a href="reverse_proxy.html">the reverse proxy documentation</a> for information on setting up a
  2764. reverse proxy.</p>
  2765. <h4 id="option-3-add-a-well-known-file-to-delegate-your-matrix-traffic"><a class="header" href="#option-3-add-a-well-known-file-to-delegate-your-matrix-traffic">Option 3: add a .well-known file to delegate your matrix traffic</a></h4>
  2766. <p>This will allow you to keep Synapse on a separate domain, without having to
  2767. give it a certificate for the matrix domain.</p>
  2768. <p>You can do this with a <code>.well-known</code> file as follows:</p>
  2769. <ol>
  2770. <li>
  2771. <p>Keep the SRV record in place - it is needed for backwards compatibility
  2772. with Synapse 0.34 and earlier.</p>
  2773. </li>
  2774. <li>
  2775. <p>Give Synapse a certificate corresponding to the target domain
  2776. (<code>customer.example.net</code> in the above example). You can do this by acquire a
  2777. certificate for the target domain and giving it to Synapse via <code>tls_certificate_path</code>
  2778. and <code>tls_private_key_path</code>.</p>
  2779. </li>
  2780. <li>
  2781. <p>Restart Synapse to ensure the new certificate is loaded.</p>
  2782. </li>
  2783. <li>
  2784. <p>Arrange for a <code>.well-known</code> file at
  2785. <code>https://&lt;server_name&gt;/.well-known/matrix/server</code> with contents:</p>
  2786. <pre><code class="language-json">{&quot;m.server&quot;: &quot;&lt;target server name&gt;&quot;}
  2787. </code></pre>
  2788. <p>where the target server name is resolved as usual (i.e. SRV lookup, falling
  2789. back to talking to port 8448).</p>
  2790. <p>In the above example, where synapse is listening on port 8000,
  2791. <code>https://example.com/.well-known/matrix/server</code> should have <code>m.server</code> set to one of:</p>
  2792. <ol>
  2793. <li>
  2794. <p><code>customer.example.net</code> ─ with a SRV record on
  2795. <code>_matrix._tcp.customer.example.com</code> pointing to port 8000, or:</p>
  2796. </li>
  2797. <li>
  2798. <p><code>customer.example.net</code> ─ updating synapse to listen on the default port
  2799. 8448, or:</p>
  2800. </li>
  2801. <li>
  2802. <p><code>customer.example.net:8000</code> ─ ensuring that if there is a reverse proxy
  2803. on <code>customer.example.net:8000</code> it correctly handles HTTP requests with
  2804. Host header set to <code>customer.example.net:8000</code>.</p>
  2805. </li>
  2806. </ol>
  2807. </li>
  2808. </ol>
  2809. <h2 id="faq"><a class="header" href="#faq">FAQ</a></h2>
  2810. <h3 id="synapse-0990-has-just-been-released-what-do-i-need-to-do-right-now"><a class="header" href="#synapse-0990-has-just-been-released-what-do-i-need-to-do-right-now">Synapse 0.99.0 has just been released, what do I need to do right now?</a></h3>
  2811. <p>Upgrade as soon as you can in preparation for Synapse 1.0.0, and update your
  2812. TLS certificates as <a href="MSC1711_certificates_FAQ.html#configuring-certificates-for-compatibility-with-synapse-100">above</a>.</p>
  2813. <h3 id="what-will-happen-if-i-do-not-set-up-a-valid-federation-certificate-immediately"><a class="header" href="#what-will-happen-if-i-do-not-set-up-a-valid-federation-certificate-immediately">What will happen if I do not set up a valid federation certificate immediately?</a></h3>
  2814. <p>Nothing initially, but once 1.0.0 is in the wild it will not be possible to
  2815. federate with 1.0.0 servers.</p>
  2816. <h3 id="what-will-happen-if-i-do-nothing-at-all"><a class="header" href="#what-will-happen-if-i-do-nothing-at-all">What will happen if I do nothing at all?</a></h3>
  2817. <p>If the admin takes no action at all, and remains on a Synapse &lt; 0.99.0 then the
  2818. homeserver will be unable to federate with those who have implemented
  2819. .well-known. Then, as above, once the month upgrade window has expired the
  2820. homeserver will not be able to federate with any Synapse &gt;= 1.0.0</p>
  2821. <h3 id="when-do-i-need-a-srv-record-or-well-known-uri"><a class="header" href="#when-do-i-need-a-srv-record-or-well-known-uri">When do I need a SRV record or .well-known URI?</a></h3>
  2822. <p>If your homeserver listens on the default federation port (8448), and your
  2823. <code>server_name</code> points to the host that your homeserver runs on, you do not need an
  2824. SRV record or <code>.well-known/matrix/server</code> URI.</p>
  2825. <p>For instance, if you registered <code>example.com</code> and pointed its DNS A record at a
  2826. fresh Upcloud VPS or similar, you could install Synapse 0.99 on that host,
  2827. giving it a server_name of <code>example.com</code>, and it would automatically generate a
  2828. valid TLS certificate for you via Let's Encrypt and no SRV record or
  2829. <code>.well-known</code> URI would be needed.</p>
  2830. <p>This is the common case, although you can add an SRV record or
  2831. <code>.well-known/matrix/server</code> URI for completeness if you wish.</p>
  2832. <p><strong>However</strong>, if your server does not listen on port 8448, or if your <code>server_name</code>
  2833. does not point to the host that your homeserver runs on, you will need to let
  2834. other servers know how to find it.</p>
  2835. <p>In this case, you should see <a href="MSC1711_certificates_FAQ.html#if-you-do-have-an-srv-record-currently">&quot;If you do have an SRV record
  2836. currently&quot;</a> above.</p>
  2837. <h3 id="can-i-still-use-an-srv-record"><a class="header" href="#can-i-still-use-an-srv-record">Can I still use an SRV record?</a></h3>
  2838. <p>Firstly, if you didn't need an SRV record before (because your server is
  2839. listening on port 8448 of your server_name), you certainly don't need one now:
  2840. the defaults are still the same.</p>
  2841. <p>If you previously had an SRV record, you can keep using it provided you are
  2842. able to give Synapse a TLS certificate corresponding to your server name. For
  2843. example, suppose you had the following SRV record, which directs matrix traffic
  2844. for example.com to matrix.example.com:443:</p>
  2845. <pre><code>_matrix._tcp.example.com. IN SRV 10 5 443 matrix.example.com
  2846. </code></pre>
  2847. <p>In this case, Synapse must be given a certificate for example.com - or be
  2848. configured to acquire one from Let's Encrypt.</p>
  2849. <p>If you are unable to give Synapse a certificate for your server_name, you will
  2850. also need to use a .well-known URI instead. However, see also &quot;I have created a
  2851. .well-known URI. Do I still need an SRV record?&quot;.</p>
  2852. <h3 id="i-have-created-a-well-known-uri-do-i-still-need-an-srv-record"><a class="header" href="#i-have-created-a-well-known-uri-do-i-still-need-an-srv-record">I have created a .well-known URI. Do I still need an SRV record?</a></h3>
  2853. <p>As of Synapse 0.99, Synapse will first check for the existence of a <code>.well-known</code>
  2854. URI and follow any delegation it suggests. It will only then check for the
  2855. existence of an SRV record.</p>
  2856. <p>That means that the SRV record will often be redundant. However, you should
  2857. remember that there may still be older versions of Synapse in the federation
  2858. which do not understand <code>.well-known</code> URIs, so if you removed your SRV record you
  2859. would no longer be able to federate with them.</p>
  2860. <p>It is therefore best to leave the SRV record in place for now. Synapse 0.34 and
  2861. earlier will follow the SRV record (and not care about the invalid
  2862. certificate). Synapse 0.99 and later will follow the .well-known URI, with the
  2863. correct certificate chain.</p>
  2864. <h3 id="it-used-to-work-just-fine-why-are-you-breaking-everything"><a class="header" href="#it-used-to-work-just-fine-why-are-you-breaking-everything">It used to work just fine, why are you breaking everything?</a></h3>
  2865. <p>We have always wanted Matrix servers to be as easy to set up as possible, and
  2866. so back when we started federation in 2014 we didn't want admins to have to go
  2867. through the cumbersome process of buying a valid TLS certificate to run a
  2868. server. This was before Let's Encrypt came along and made getting a free and
  2869. valid TLS certificate straightforward. So instead, we adopted a system based on
  2870. <a href="https://en.wikipedia.org/wiki/Convergence_(SSL)">Perspectives</a>: an approach
  2871. where you check a set of &quot;notary servers&quot; (in practice, homeservers) to vouch
  2872. for the validity of a certificate rather than having it signed by a CA. As long
  2873. as enough different notaries agree on the certificate's validity, then it is
  2874. trusted.</p>
  2875. <p>However, in practice this has never worked properly. Most people only use the
  2876. default notary server (matrix.org), leading to inadvertent centralisation which
  2877. we want to eliminate. Meanwhile, we never implemented the full consensus
  2878. algorithm to query the servers participating in a room to determine consensus
  2879. on whether a given certificate is valid. This is fiddly to get right
  2880. (especially in face of sybil attacks), and we found ourselves questioning
  2881. whether it was worth the effort to finish the work and commit to maintaining a
  2882. secure certificate validation system as opposed to focusing on core Matrix
  2883. development.</p>
  2884. <p>Meanwhile, Let's Encrypt came along in 2016, and put the final nail in the
  2885. coffin of the Perspectives project (which was already pretty dead). So, the
  2886. Spec Core Team decided that a better approach would be to mandate valid TLS
  2887. certificates for federation alongside the rest of the Web. More details can be
  2888. found in
  2889. <a href="https://github.com/matrix-org/matrix-doc/blob/main/proposals/1711-x509-for-federation.md#background-the-failure-of-the-perspectives-approach">MSC1711</a>.</p>
  2890. <p>This results in a breaking change, which is disruptive, but absolutely critical
  2891. for the security model. However, the existence of Let's Encrypt as a trivial
  2892. way to replace the old self-signed certificates with valid CA-signed ones helps
  2893. smooth things over massively, especially as Synapse can now automate Let's
  2894. Encrypt certificate generation if needed.</p>
  2895. <h3 id="can-i-manage-my-own-certificates-rather-than-having-synapse-renew-certificates-itself"><a class="header" href="#can-i-manage-my-own-certificates-rather-than-having-synapse-renew-certificates-itself">Can I manage my own certificates rather than having Synapse renew certificates itself?</a></h3>
  2896. <p>Yes, you are welcome to manage your certificates yourself. Synapse will only
  2897. attempt to obtain certificates from Let's Encrypt if you configure it to do
  2898. so.The only requirement is that there is a valid TLS cert present for
  2899. federation end points.</p>
  2900. <h3 id="do-you-still-recommend-against-using-a-reverse-proxy-on-the-federation-port"><a class="header" href="#do-you-still-recommend-against-using-a-reverse-proxy-on-the-federation-port">Do you still recommend against using a reverse proxy on the federation port?</a></h3>
  2901. <p>We no longer actively recommend against using a reverse proxy. Many admins will
  2902. find it easier to direct federation traffic to a reverse proxy and manage their
  2903. own TLS certificates, and this is a supported configuration.</p>
  2904. <p>See <a href="reverse_proxy.html">the reverse proxy documentation</a> for information on setting up a
  2905. reverse proxy.</p>
  2906. <h3 id="do-i-still-need-to-give-my-tls-certificates-to-synapse-if-i-am-using-a-reverse-proxy"><a class="header" href="#do-i-still-need-to-give-my-tls-certificates-to-synapse-if-i-am-using-a-reverse-proxy">Do I still need to give my TLS certificates to Synapse if I am using a reverse proxy?</a></h3>
  2907. <p>Practically speaking, this is no longer necessary.</p>
  2908. <p>If you are using a reverse proxy for all of your TLS traffic, then you can set
  2909. <code>no_tls: True</code>. In that case, the only reason Synapse needs the certificate is
  2910. to populate a legacy 'tls_fingerprints' field in the federation API. This is
  2911. ignored by Synapse 0.99.0 and later, and the only time pre-0.99 Synapses will
  2912. check it is when attempting to fetch the server keys - and generally this is
  2913. delegated via <code>matrix.org</code>, which is on 0.99.0.</p>
  2914. <p>However, there is a bug in Synapse 0.99.0
  2915. <a href="https://github.com/matrix-org/synapse/issues/4554">4554</a> which prevents
  2916. Synapse from starting if you do not give it a TLS certificate. To work around
  2917. this, you can give it any TLS certificate at all. This will be fixed soon.</p>
  2918. <h3 id="do-i-need-the-same-certificate-for-the-client-and-federation-port"><a class="header" href="#do-i-need-the-same-certificate-for-the-client-and-federation-port">Do I need the same certificate for the client and federation port?</a></h3>
  2919. <p>No. There is nothing stopping you from using different certificates,
  2920. particularly if you are using a reverse proxy. However, Synapse will use the
  2921. same certificate on any ports where TLS is configured.</p>
  2922. <h3 id="how-do-i-tell-synapse-to-reload-my-keyscertificates-after-i-replace-them"><a class="header" href="#how-do-i-tell-synapse-to-reload-my-keyscertificates-after-i-replace-them">How do I tell Synapse to reload my keys/certificates after I replace them?</a></h3>
  2923. <p>Synapse will reload the keys and certificates when it receives a SIGHUP - for
  2924. example <code>kill -HUP $(cat homeserver.pid)</code>. Alternatively, simply restart
  2925. Synapse, though this will result in downtime while it restarts.</p>
  2926. <div style="break-before: page; page-break-before: always;"></div><h1 id="setting-up-federation"><a class="header" href="#setting-up-federation">Setting up federation</a></h1>
  2927. <p>Federation is the process by which users on different servers can participate
  2928. in the same room. For this to work, those other servers must be able to contact
  2929. yours to send messages.</p>
  2930. <p>The <code>server_name</code> configured in the Synapse configuration file (often
  2931. <code>homeserver.yaml</code>) defines how resources (users, rooms, etc.) will be
  2932. identified (eg: <code>@user:example.com</code>, <code>#room:example.com</code>). By default,
  2933. it is also the domain that other servers will use to try to reach your
  2934. server (via port 8448). This is easy to set up and will work provided
  2935. you set the <code>server_name</code> to match your machine's public DNS hostname.</p>
  2936. <p>For this default configuration to work, you will need to listen for TLS
  2937. connections on port 8448. The preferred way to do that is by using a
  2938. reverse proxy: see <a href="reverse_proxy.html">the reverse proxy documentation</a> for instructions
  2939. on how to correctly set one up.</p>
  2940. <p>In some cases you might not want to run Synapse on the machine that has
  2941. the <code>server_name</code> as its public DNS hostname, or you might want federation
  2942. traffic to use a different port than 8448. For example, you might want to
  2943. have your user names look like <code>@user:example.com</code>, but you want to run
  2944. Synapse on <code>synapse.example.com</code> on port 443. This can be done using
  2945. delegation, which allows an admin to control where federation traffic should
  2946. be sent. See <a href="delegate.html">the delegation documentation</a> for instructions on how to set this up.</p>
  2947. <p>Once federation has been configured, you should be able to join a room over
  2948. federation. A good place to start is <code>#synapse:matrix.org</code> - a room for
  2949. Synapse admins.</p>
  2950. <h2 id="troubleshooting-3"><a class="header" href="#troubleshooting-3">Troubleshooting</a></h2>
  2951. <p>You can use the <a href="https://matrix.org/federationtester">federation tester</a>
  2952. to check if your homeserver is configured correctly. Alternatively try the
  2953. <a href="https://matrix.org/federationtester/api/report?server_name=DOMAIN">JSON API used by the federation tester</a>.
  2954. Note that you'll have to modify this URL to replace <code>DOMAIN</code> with your
  2955. <code>server_name</code>. Hitting the API directly provides extra detail.</p>
  2956. <p>The typical failure mode for federation is that when the server tries to join
  2957. a room, it is rejected with &quot;401: Unauthorized&quot;. Generally this means that other
  2958. servers in the room could not access yours. (Joining a room over federation is
  2959. a complicated dance which requires connections in both directions).</p>
  2960. <p>Another common problem is that people on other servers can't join rooms that
  2961. you invite them to. This can be caused by an incorrectly-configured reverse
  2962. proxy: see <a href="reverse_proxy.html">the reverse proxy documentation</a> for instructions on how
  2963. to correctly configure a reverse proxy.</p>
  2964. <h3 id="known-issues"><a class="header" href="#known-issues">Known issues</a></h3>
  2965. <p><strong>HTTP <code>308 Permanent Redirect</code> redirects are not followed</strong>: Due to missing features
  2966. in the HTTP library used by Synapse, 308 redirects are currently not followed by
  2967. federating servers, which can cause <code>M_UNKNOWN</code> or <code>401 Unauthorized</code> errors. This
  2968. may affect users who are redirecting apex-to-www (e.g. <code>example.com</code> -&gt; <code>www.example.com</code>),
  2969. and especially users of the Kubernetes <em>Nginx Ingress</em> module, which uses 308 redirect
  2970. codes by default. For those Kubernetes users, <a href="https://stackoverflow.com/a/52617528/5096871">this Stackoverflow post</a>
  2971. might be helpful. For other users, switching to a <code>301 Moved Permanently</code> code may be
  2972. an option. 308 redirect codes will be supported properly in a future
  2973. release of Synapse.</p>
  2974. <h2 id="running-a-demo-federation-of-synapses"><a class="header" href="#running-a-demo-federation-of-synapses">Running a demo federation of Synapses</a></h2>
  2975. <p>If you want to get up and running quickly with a trio of homeservers in a
  2976. private federation, there is a script in the <code>demo</code> directory. This is mainly
  2977. useful just for development purposes. See <a href="https://github.com/matrix-org/synapse/tree/develop/demo/">demo/README</a>.</p>
  2978. <div style="break-before: page; page-break-before: always;"></div><h1 id="configuration-1"><a class="header" href="#configuration-1">Configuration</a></h1>
  2979. <p>This section contains information on tweaking Synapse via the various options in the configuration file. A configuration
  2980. file should have been generated when you <a href="usage/configuration/../../setup/installation.html">installed Synapse</a>.</p>
  2981. <div style="break-before: page; page-break-before: always;"></div><h1 id="homeserver-sample-configuration-file"><a class="header" href="#homeserver-sample-configuration-file">Homeserver Sample Configuration File</a></h1>
  2982. <p>Below is a sample homeserver configuration file. The homeserver configuration file
  2983. can be tweaked to change the behaviour of your homeserver. A restart of the server is
  2984. generally required to apply any changes made to this file.</p>
  2985. <p>Note that the contents below are <em>not</em> intended to be copied and used as the basis for
  2986. a real homeserver.yaml. Instead, if you are starting from scratch, please generate
  2987. a fresh config using Synapse by following the instructions in
  2988. <a href="usage/configuration/../../setup/installation.html">Installation</a>.</p>
  2989. <pre><code class="language-yaml"># This file is maintained as an up-to-date snapshot of the default
  2990. # homeserver.yaml configuration generated by Synapse.
  2991. #
  2992. # It is intended to act as a reference for the default configuration,
  2993. # helping admins keep track of new options and other changes, and compare
  2994. # their configs with the current default. As such, many of the actual
  2995. # config values shown are placeholders.
  2996. #
  2997. # It is *not* intended to be copied and used as the basis for a real
  2998. # homeserver.yaml. Instead, if you are starting from scratch, please generate
  2999. # a fresh config using Synapse by following the instructions in
  3000. # https://matrix-org.github.io/synapse/latest/setup/installation.html.
  3001. # Configuration options that take a time period can be set using a number
  3002. # followed by a letter. Letters have the following meanings:
  3003. # s = second
  3004. # m = minute
  3005. # h = hour
  3006. # d = day
  3007. # w = week
  3008. # y = year
  3009. # For example, setting redaction_retention_period: 5m would remove redacted
  3010. # messages from the database after 5 minutes, rather than 5 months.
  3011. ################################################################################
  3012. # Configuration file for Synapse.
  3013. #
  3014. # This is a YAML file: see [1] for a quick introduction. Note in particular
  3015. # that *indentation is important*: all the elements of a list or dictionary
  3016. # should have the same indentation.
  3017. #
  3018. # [1] https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
  3019. ## Modules ##
  3020. # Server admins can expand Synapse's functionality with external modules.
  3021. #
  3022. # See https://matrix-org.github.io/synapse/latest/modules.html for more
  3023. # documentation on how to configure or create custom modules for Synapse.
  3024. #
  3025. modules:
  3026. # - module: my_super_module.MySuperClass
  3027. # config:
  3028. # do_thing: true
  3029. # - module: my_other_super_module.SomeClass
  3030. # config: {}
  3031. ## Server ##
  3032. # The public-facing domain of the server
  3033. #
  3034. # The server_name name will appear at the end of usernames and room addresses
  3035. # created on this server. For example if the server_name was example.com,
  3036. # usernames on this server would be in the format @user:example.com
  3037. #
  3038. # In most cases you should avoid using a matrix specific subdomain such as
  3039. # matrix.example.com or synapse.example.com as the server_name for the same
  3040. # reasons you wouldn't use user@email.example.com as your email address.
  3041. # See https://matrix-org.github.io/synapse/latest/delegate.html
  3042. # for information on how to host Synapse on a subdomain while preserving
  3043. # a clean server_name.
  3044. #
  3045. # The server_name cannot be changed later so it is important to
  3046. # configure this correctly before you start Synapse. It should be all
  3047. # lowercase and may contain an explicit port.
  3048. # Examples: matrix.org, localhost:8080
  3049. #
  3050. server_name: &quot;SERVERNAME&quot;
  3051. # When running as a daemon, the file to store the pid in
  3052. #
  3053. pid_file: DATADIR/homeserver.pid
  3054. # The absolute URL to the web client which /_matrix/client will redirect
  3055. # to if 'webclient' is configured under the 'listeners' configuration.
  3056. #
  3057. # This option can be also set to the filesystem path to the web client
  3058. # which will be served at /_matrix/client/ if 'webclient' is configured
  3059. # under the 'listeners' configuration, however this is a security risk:
  3060. # https://github.com/matrix-org/synapse#security-note
  3061. #
  3062. #web_client_location: https://riot.example.com/
  3063. # The public-facing base URL that clients use to access this Homeserver (not
  3064. # including _matrix/...). This is the same URL a user might enter into the
  3065. # 'Custom Homeserver URL' field on their client. If you use Synapse with a
  3066. # reverse proxy, this should be the URL to reach Synapse via the proxy.
  3067. # Otherwise, it should be the URL to reach Synapse's client HTTP listener (see
  3068. # 'listeners' below).
  3069. #
  3070. # Defaults to 'https://&lt;server_name&gt;/'.
  3071. #
  3072. #public_baseurl: https://example.com/
  3073. # Uncomment the following to tell other servers to send federation traffic on
  3074. # port 443.
  3075. #
  3076. # By default, other servers will try to reach our server on port 8448, which can
  3077. # be inconvenient in some environments.
  3078. #
  3079. # Provided 'https://&lt;server_name&gt;/' on port 443 is routed to Synapse, this
  3080. # option configures Synapse to serve a file at
  3081. # 'https://&lt;server_name&gt;/.well-known/matrix/server'. This will tell other
  3082. # servers to send traffic to port 443 instead.
  3083. #
  3084. # See https://matrix-org.github.io/synapse/latest/delegate.html for more
  3085. # information.
  3086. #
  3087. # Defaults to 'false'.
  3088. #
  3089. #serve_server_wellknown: true
  3090. # Set the soft limit on the number of file descriptors synapse can use
  3091. # Zero is used to indicate synapse should set the soft limit to the
  3092. # hard limit.
  3093. #
  3094. #soft_file_limit: 0
  3095. # Presence tracking allows users to see the state (e.g online/offline)
  3096. # of other local and remote users.
  3097. #
  3098. presence:
  3099. # Uncomment to disable presence tracking on this homeserver. This option
  3100. # replaces the previous top-level 'use_presence' option.
  3101. #
  3102. #enabled: false
  3103. # Whether to require authentication to retrieve profile data (avatars,
  3104. # display names) of other users through the client API. Defaults to
  3105. # 'false'. Note that profile data is also available via the federation
  3106. # API, unless allow_profile_lookup_over_federation is set to false.
  3107. #
  3108. #require_auth_for_profile_requests: true
  3109. # Uncomment to require a user to share a room with another user in order
  3110. # to retrieve their profile information. Only checked on Client-Server
  3111. # requests. Profile requests from other servers should be checked by the
  3112. # requesting server. Defaults to 'false'.
  3113. #
  3114. #limit_profile_requests_to_users_who_share_rooms: true
  3115. # Uncomment to prevent a user's profile data from being retrieved and
  3116. # displayed in a room until they have joined it. By default, a user's
  3117. # profile data is included in an invite event, regardless of the values
  3118. # of the above two settings, and whether or not the users share a server.
  3119. # Defaults to 'true'.
  3120. #
  3121. #include_profile_data_on_invite: false
  3122. # If set to 'true', removes the need for authentication to access the server's
  3123. # public rooms directory through the client API, meaning that anyone can
  3124. # query the room directory. Defaults to 'false'.
  3125. #
  3126. #allow_public_rooms_without_auth: true
  3127. # If set to 'true', allows any other homeserver to fetch the server's public
  3128. # rooms directory via federation. Defaults to 'false'.
  3129. #
  3130. #allow_public_rooms_over_federation: true
  3131. # The default room version for newly created rooms.
  3132. #
  3133. # Known room versions are listed here:
  3134. # https://matrix.org/docs/spec/#complete-list-of-room-versions
  3135. #
  3136. # For example, for room version 1, default_room_version should be set
  3137. # to &quot;1&quot;.
  3138. #
  3139. #default_room_version: &quot;6&quot;
  3140. # The GC threshold parameters to pass to `gc.set_threshold`, if defined
  3141. #
  3142. #gc_thresholds: [700, 10, 10]
  3143. # The minimum time in seconds between each GC for a generation, regardless of
  3144. # the GC thresholds. This ensures that we don't do GC too frequently.
  3145. #
  3146. # A value of `[1s, 10s, 30s]` indicates that a second must pass between consecutive
  3147. # generation 0 GCs, etc.
  3148. #
  3149. # Defaults to `[1s, 10s, 30s]`.
  3150. #
  3151. #gc_min_interval: [0.5s, 30s, 1m]
  3152. # Set the limit on the returned events in the timeline in the get
  3153. # and sync operations. The default value is 100. -1 means no upper limit.
  3154. #
  3155. # Uncomment the following to increase the limit to 5000.
  3156. #
  3157. #filter_timeline_limit: 5000
  3158. # Whether room invites to users on this server should be blocked
  3159. # (except those sent by local server admins). The default is False.
  3160. #
  3161. #block_non_admin_invites: true
  3162. # Room searching
  3163. #
  3164. # If disabled, new messages will not be indexed for searching and users
  3165. # will receive errors when searching for messages. Defaults to enabled.
  3166. #
  3167. #enable_search: false
  3168. # Prevent outgoing requests from being sent to the following blacklisted IP address
  3169. # CIDR ranges. If this option is not specified then it defaults to private IP
  3170. # address ranges (see the example below).
  3171. #
  3172. # The blacklist applies to the outbound requests for federation, identity servers,
  3173. # push servers, and for checking key validity for third-party invite events.
  3174. #
  3175. # (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
  3176. # listed here, since they correspond to unroutable addresses.)
  3177. #
  3178. # This option replaces federation_ip_range_blacklist in Synapse v1.25.0.
  3179. #
  3180. # Note: The value is ignored when an HTTP proxy is in use
  3181. #
  3182. #ip_range_blacklist:
  3183. # - '127.0.0.0/8'
  3184. # - '10.0.0.0/8'
  3185. # - '172.16.0.0/12'
  3186. # - '192.168.0.0/16'
  3187. # - '100.64.0.0/10'
  3188. # - '192.0.0.0/24'
  3189. # - '169.254.0.0/16'
  3190. # - '192.88.99.0/24'
  3191. # - '198.18.0.0/15'
  3192. # - '192.0.2.0/24'
  3193. # - '198.51.100.0/24'
  3194. # - '203.0.113.0/24'
  3195. # - '224.0.0.0/4'
  3196. # - '::1/128'
  3197. # - 'fe80::/10'
  3198. # - 'fc00::/7'
  3199. # - '2001:db8::/32'
  3200. # - 'ff00::/8'
  3201. # - 'fec0::/10'
  3202. # List of IP address CIDR ranges that should be allowed for federation,
  3203. # identity servers, push servers, and for checking key validity for
  3204. # third-party invite events. This is useful for specifying exceptions to
  3205. # wide-ranging blacklisted target IP ranges - e.g. for communication with
  3206. # a push server only visible in your network.
  3207. #
  3208. # This whitelist overrides ip_range_blacklist and defaults to an empty
  3209. # list.
  3210. #
  3211. #ip_range_whitelist:
  3212. # - '192.168.1.1'
  3213. # List of ports that Synapse should listen on, their purpose and their
  3214. # configuration.
  3215. #
  3216. # Options for each listener include:
  3217. #
  3218. # port: the TCP port to bind to
  3219. #
  3220. # bind_addresses: a list of local addresses to listen on. The default is
  3221. # 'all local interfaces'.
  3222. #
  3223. # type: the type of listener. Normally 'http', but other valid options are:
  3224. # 'manhole' (see https://matrix-org.github.io/synapse/latest/manhole.html),
  3225. # 'metrics' (see https://matrix-org.github.io/synapse/latest/metrics-howto.html),
  3226. # 'replication' (see https://matrix-org.github.io/synapse/latest/workers.html).
  3227. #
  3228. # tls: set to true to enable TLS for this listener. Will use the TLS
  3229. # key/cert specified in tls_private_key_path / tls_certificate_path.
  3230. #
  3231. # x_forwarded: Only valid for an 'http' listener. Set to true to use the
  3232. # X-Forwarded-For header as the client IP. Useful when Synapse is
  3233. # behind a reverse-proxy.
  3234. #
  3235. # resources: Only valid for an 'http' listener. A list of resources to host
  3236. # on this port. Options for each resource are:
  3237. #
  3238. # names: a list of names of HTTP resources. See below for a list of
  3239. # valid resource names.
  3240. #
  3241. # compress: set to true to enable HTTP compression for this resource.
  3242. #
  3243. # additional_resources: Only valid for an 'http' listener. A map of
  3244. # additional endpoints which should be loaded via dynamic modules.
  3245. #
  3246. # Valid resource names are:
  3247. #
  3248. # client: the client-server API (/_matrix/client), and the synapse admin
  3249. # API (/_synapse/admin). Also implies 'media' and 'static'.
  3250. #
  3251. # consent: user consent forms (/_matrix/consent).
  3252. # See https://matrix-org.github.io/synapse/latest/consent_tracking.html.
  3253. #
  3254. # federation: the server-server API (/_matrix/federation). Also implies
  3255. # 'media', 'keys', 'openid'
  3256. #
  3257. # keys: the key discovery API (/_matrix/keys).
  3258. #
  3259. # media: the media API (/_matrix/media).
  3260. #
  3261. # metrics: the metrics interface.
  3262. # See https://matrix-org.github.io/synapse/latest/metrics-howto.html.
  3263. #
  3264. # openid: OpenID authentication.
  3265. #
  3266. # replication: the HTTP replication API (/_synapse/replication).
  3267. # See https://matrix-org.github.io/synapse/latest/workers.html.
  3268. #
  3269. # static: static resources under synapse/static (/_matrix/static). (Mostly
  3270. # useful for 'fallback authentication'.)
  3271. #
  3272. # webclient: A web client. Requires web_client_location to be set.
  3273. #
  3274. listeners:
  3275. # TLS-enabled listener: for when matrix traffic is sent directly to synapse.
  3276. #
  3277. # Disabled by default. To enable it, uncomment the following. (Note that you
  3278. # will also need to give Synapse a TLS key and certificate: see the TLS section
  3279. # below.)
  3280. #
  3281. #- port: 8448
  3282. # type: http
  3283. # tls: true
  3284. # resources:
  3285. # - names: [client, federation]
  3286. # Unsecure HTTP listener: for when matrix traffic passes through a reverse proxy
  3287. # that unwraps TLS.
  3288. #
  3289. # If you plan to use a reverse proxy, please see
  3290. # https://matrix-org.github.io/synapse/latest/reverse_proxy.html.
  3291. #
  3292. - port: 8008
  3293. tls: false
  3294. type: http
  3295. x_forwarded: true
  3296. bind_addresses: ['::1', '127.0.0.1']
  3297. resources:
  3298. - names: [client, federation]
  3299. compress: false
  3300. # example additional_resources:
  3301. #
  3302. #additional_resources:
  3303. # &quot;/_matrix/my/custom/endpoint&quot;:
  3304. # module: my_module.CustomRequestHandler
  3305. # config: {}
  3306. # Turn on the twisted ssh manhole service on localhost on the given
  3307. # port.
  3308. #
  3309. #- port: 9000
  3310. # bind_addresses: ['::1', '127.0.0.1']
  3311. # type: manhole
  3312. # Connection settings for the manhole
  3313. #
  3314. manhole_settings:
  3315. # The username for the manhole. This defaults to 'matrix'.
  3316. #
  3317. #username: manhole
  3318. # The password for the manhole. This defaults to 'rabbithole'.
  3319. #
  3320. #password: mypassword
  3321. # The private and public SSH key pair used to encrypt the manhole traffic.
  3322. # If these are left unset, then hardcoded and non-secret keys are used,
  3323. # which could allow traffic to be intercepted if sent over a public network.
  3324. #
  3325. #ssh_priv_key_path: CONFDIR/id_rsa
  3326. #ssh_pub_key_path: CONFDIR/id_rsa.pub
  3327. # Forward extremities can build up in a room due to networking delays between
  3328. # homeservers. Once this happens in a large room, calculation of the state of
  3329. # that room can become quite expensive. To mitigate this, once the number of
  3330. # forward extremities reaches a given threshold, Synapse will send an
  3331. # org.matrix.dummy_event event, which will reduce the forward extremities
  3332. # in the room.
  3333. #
  3334. # This setting defines the threshold (i.e. number of forward extremities in the
  3335. # room) at which dummy events are sent. The default value is 10.
  3336. #
  3337. #dummy_events_threshold: 5
  3338. ## Homeserver blocking ##
  3339. # How to reach the server admin, used in ResourceLimitError
  3340. #
  3341. #admin_contact: 'mailto:admin@server.com'
  3342. # Global blocking
  3343. #
  3344. #hs_disabled: false
  3345. #hs_disabled_message: 'Human readable reason for why the HS is blocked'
  3346. # Monthly Active User Blocking
  3347. #
  3348. # Used in cases where the admin or server owner wants to limit to the
  3349. # number of monthly active users.
  3350. #
  3351. # 'limit_usage_by_mau' disables/enables monthly active user blocking. When
  3352. # enabled and a limit is reached the server returns a 'ResourceLimitError'
  3353. # with error type Codes.RESOURCE_LIMIT_EXCEEDED
  3354. #
  3355. # 'max_mau_value' is the hard limit of monthly active users above which
  3356. # the server will start blocking user actions.
  3357. #
  3358. # 'mau_trial_days' is a means to add a grace period for active users. It
  3359. # means that users must be active for this number of days before they
  3360. # can be considered active and guards against the case where lots of users
  3361. # sign up in a short space of time never to return after their initial
  3362. # session.
  3363. #
  3364. # 'mau_limit_alerting' is a means of limiting client side alerting
  3365. # should the mau limit be reached. This is useful for small instances
  3366. # where the admin has 5 mau seats (say) for 5 specific people and no
  3367. # interest increasing the mau limit further. Defaults to True, which
  3368. # means that alerting is enabled
  3369. #
  3370. #limit_usage_by_mau: false
  3371. #max_mau_value: 50
  3372. #mau_trial_days: 2
  3373. #mau_limit_alerting: false
  3374. # If enabled, the metrics for the number of monthly active users will
  3375. # be populated, however no one will be limited. If limit_usage_by_mau
  3376. # is true, this is implied to be true.
  3377. #
  3378. #mau_stats_only: false
  3379. # Sometimes the server admin will want to ensure certain accounts are
  3380. # never blocked by mau checking. These accounts are specified here.
  3381. #
  3382. #mau_limit_reserved_threepids:
  3383. # - medium: 'email'
  3384. # address: 'reserved_user@example.com'
  3385. # Used by phonehome stats to group together related servers.
  3386. #server_context: context
  3387. # Resource-constrained homeserver settings
  3388. #
  3389. # When this is enabled, the room &quot;complexity&quot; will be checked before a user
  3390. # joins a new remote room. If it is above the complexity limit, the server will
  3391. # disallow joining, or will instantly leave.
  3392. #
  3393. # Room complexity is an arbitrary measure based on factors such as the number of
  3394. # users in the room.
  3395. #
  3396. limit_remote_rooms:
  3397. # Uncomment to enable room complexity checking.
  3398. #
  3399. #enabled: true
  3400. # the limit above which rooms cannot be joined. The default is 1.0.
  3401. #
  3402. #complexity: 0.5
  3403. # override the error which is returned when the room is too complex.
  3404. #
  3405. #complexity_error: &quot;This room is too complex.&quot;
  3406. # allow server admins to join complex rooms. Default is false.
  3407. #
  3408. #admins_can_join: true
  3409. # Whether to require a user to be in the room to add an alias to it.
  3410. # Defaults to 'true'.
  3411. #
  3412. #require_membership_for_aliases: false
  3413. # Whether to allow per-room membership profiles through the send of membership
  3414. # events with profile information that differ from the target's global profile.
  3415. # Defaults to 'true'.
  3416. #
  3417. #allow_per_room_profiles: false
  3418. # How long to keep redacted events in unredacted form in the database. After
  3419. # this period redacted events get replaced with their redacted form in the DB.
  3420. #
  3421. # Defaults to `7d`. Set to `null` to disable.
  3422. #
  3423. #redaction_retention_period: 28d
  3424. # How long to track users' last seen time and IPs in the database.
  3425. #
  3426. # Defaults to `28d`. Set to `null` to disable clearing out of old rows.
  3427. #
  3428. #user_ips_max_age: 14d
  3429. # Inhibits the /requestToken endpoints from returning an error that might leak
  3430. # information about whether an e-mail address is in use or not on this
  3431. # homeserver.
  3432. # Note that for some endpoints the error situation is the e-mail already being
  3433. # used, and for others the error is entering the e-mail being unused.
  3434. # If this option is enabled, instead of returning an error, these endpoints will
  3435. # act as if no error happened and return a fake session ID ('sid') to clients.
  3436. #
  3437. #request_token_inhibit_3pid_errors: true
  3438. # A list of domains that the domain portion of 'next_link' parameters
  3439. # must match.
  3440. #
  3441. # This parameter is optionally provided by clients while requesting
  3442. # validation of an email or phone number, and maps to a link that
  3443. # users will be automatically redirected to after validation
  3444. # succeeds. Clients can make use this parameter to aid the validation
  3445. # process.
  3446. #
  3447. # The whitelist is applied whether the homeserver or an
  3448. # identity server is handling validation.
  3449. #
  3450. # The default value is no whitelist functionality; all domains are
  3451. # allowed. Setting this value to an empty list will instead disallow
  3452. # all domains.
  3453. #
  3454. #next_link_domain_whitelist: [&quot;matrix.org&quot;]
  3455. # Templates to use when generating email or HTML page contents.
  3456. #
  3457. templates:
  3458. # Directory in which Synapse will try to find template files to use to generate
  3459. # email or HTML page contents.
  3460. # If not set, or a file is not found within the template directory, a default
  3461. # template from within the Synapse package will be used.
  3462. #
  3463. # See https://matrix-org.github.io/synapse/latest/templates.html for more
  3464. # information about using custom templates.
  3465. #
  3466. #custom_template_directory: /path/to/custom/templates/
  3467. # Message retention policy at the server level.
  3468. #
  3469. # Room admins and mods can define a retention period for their rooms using the
  3470. # 'm.room.retention' state event, and server admins can cap this period by setting
  3471. # the 'allowed_lifetime_min' and 'allowed_lifetime_max' config options.
  3472. #
  3473. # If this feature is enabled, Synapse will regularly look for and purge events
  3474. # which are older than the room's maximum retention period. Synapse will also
  3475. # filter events received over federation so that events that should have been
  3476. # purged are ignored and not stored again.
  3477. #
  3478. retention:
  3479. # The message retention policies feature is disabled by default. Uncomment the
  3480. # following line to enable it.
  3481. #
  3482. #enabled: true
  3483. # Default retention policy. If set, Synapse will apply it to rooms that lack the
  3484. # 'm.room.retention' state event. Currently, the value of 'min_lifetime' doesn't
  3485. # matter much because Synapse doesn't take it into account yet.
  3486. #
  3487. #default_policy:
  3488. # min_lifetime: 1d
  3489. # max_lifetime: 1y
  3490. # Retention policy limits. If set, and the state of a room contains a
  3491. # 'm.room.retention' event in its state which contains a 'min_lifetime' or a
  3492. # 'max_lifetime' that's out of these bounds, Synapse will cap the room's policy
  3493. # to these limits when running purge jobs.
  3494. #
  3495. #allowed_lifetime_min: 1d
  3496. #allowed_lifetime_max: 1y
  3497. # Server admins can define the settings of the background jobs purging the
  3498. # events which lifetime has expired under the 'purge_jobs' section.
  3499. #
  3500. # If no configuration is provided, a single job will be set up to delete expired
  3501. # events in every room daily.
  3502. #
  3503. # Each job's configuration defines which range of message lifetimes the job
  3504. # takes care of. For example, if 'shortest_max_lifetime' is '2d' and
  3505. # 'longest_max_lifetime' is '3d', the job will handle purging expired events in
  3506. # rooms whose state defines a 'max_lifetime' that's both higher than 2 days, and
  3507. # lower than or equal to 3 days. Both the minimum and the maximum value of a
  3508. # range are optional, e.g. a job with no 'shortest_max_lifetime' and a
  3509. # 'longest_max_lifetime' of '3d' will handle every room with a retention policy
  3510. # which 'max_lifetime' is lower than or equal to three days.
  3511. #
  3512. # The rationale for this per-job configuration is that some rooms might have a
  3513. # retention policy with a low 'max_lifetime', where history needs to be purged
  3514. # of outdated messages on a more frequent basis than for the rest of the rooms
  3515. # (e.g. every 12h), but not want that purge to be performed by a job that's
  3516. # iterating over every room it knows, which could be heavy on the server.
  3517. #
  3518. # If any purge job is configured, it is strongly recommended to have at least
  3519. # a single job with neither 'shortest_max_lifetime' nor 'longest_max_lifetime'
  3520. # set, or one job without 'shortest_max_lifetime' and one job without
  3521. # 'longest_max_lifetime' set. Otherwise some rooms might be ignored, even if
  3522. # 'allowed_lifetime_min' and 'allowed_lifetime_max' are set, because capping a
  3523. # room's policy to these values is done after the policies are retrieved from
  3524. # Synapse's database (which is done using the range specified in a purge job's
  3525. # configuration).
  3526. #
  3527. #purge_jobs:
  3528. # - longest_max_lifetime: 3d
  3529. # interval: 12h
  3530. # - shortest_max_lifetime: 3d
  3531. # interval: 1d
  3532. ## TLS ##
  3533. # PEM-encoded X509 certificate for TLS.
  3534. # This certificate, as of Synapse 1.0, will need to be a valid and verifiable
  3535. # certificate, signed by a recognised Certificate Authority.
  3536. #
  3537. # Be sure to use a `.pem` file that includes the full certificate chain including
  3538. # any intermediate certificates (for instance, if using certbot, use
  3539. # `fullchain.pem` as your certificate, not `cert.pem`).
  3540. #
  3541. #tls_certificate_path: &quot;CONFDIR/SERVERNAME.tls.crt&quot;
  3542. # PEM-encoded private key for TLS
  3543. #
  3544. #tls_private_key_path: &quot;CONFDIR/SERVERNAME.tls.key&quot;
  3545. # Whether to verify TLS server certificates for outbound federation requests.
  3546. #
  3547. # Defaults to `true`. To disable certificate verification, uncomment the
  3548. # following line.
  3549. #
  3550. #federation_verify_certificates: false
  3551. # The minimum TLS version that will be used for outbound federation requests.
  3552. #
  3553. # Defaults to `1`. Configurable to `1`, `1.1`, `1.2`, or `1.3`. Note
  3554. # that setting this value higher than `1.2` will prevent federation to most
  3555. # of the public Matrix network: only configure it to `1.3` if you have an
  3556. # entirely private federation setup and you can ensure TLS 1.3 support.
  3557. #
  3558. #federation_client_minimum_tls_version: 1.2
  3559. # Skip federation certificate verification on the following whitelist
  3560. # of domains.
  3561. #
  3562. # This setting should only be used in very specific cases, such as
  3563. # federation over Tor hidden services and similar. For private networks
  3564. # of homeservers, you likely want to use a private CA instead.
  3565. #
  3566. # Only effective if federation_verify_certicates is `true`.
  3567. #
  3568. #federation_certificate_verification_whitelist:
  3569. # - lon.example.com
  3570. # - *.domain.com
  3571. # - *.onion
  3572. # List of custom certificate authorities for federation traffic.
  3573. #
  3574. # This setting should only normally be used within a private network of
  3575. # homeservers.
  3576. #
  3577. # Note that this list will replace those that are provided by your
  3578. # operating environment. Certificates must be in PEM format.
  3579. #
  3580. #federation_custom_ca_list:
  3581. # - myCA1.pem
  3582. # - myCA2.pem
  3583. # - myCA3.pem
  3584. ## Federation ##
  3585. # Restrict federation to the following whitelist of domains.
  3586. # N.B. we recommend also firewalling your federation listener to limit
  3587. # inbound federation traffic as early as possible, rather than relying
  3588. # purely on this application-layer restriction. If not specified, the
  3589. # default is to whitelist everything.
  3590. #
  3591. #federation_domain_whitelist:
  3592. # - lon.example.com
  3593. # - nyc.example.com
  3594. # - syd.example.com
  3595. # Report prometheus metrics on the age of PDUs being sent to and received from
  3596. # the following domains. This can be used to give an idea of &quot;delay&quot; on inbound
  3597. # and outbound federation, though be aware that any delay can be due to problems
  3598. # at either end or with the intermediate network.
  3599. #
  3600. # By default, no domains are monitored in this way.
  3601. #
  3602. #federation_metrics_domains:
  3603. # - matrix.org
  3604. # - example.com
  3605. # Uncomment to disable profile lookup over federation. By default, the
  3606. # Federation API allows other homeservers to obtain profile data of any user
  3607. # on this homeserver. Defaults to 'true'.
  3608. #
  3609. #allow_profile_lookup_over_federation: false
  3610. # Uncomment to disable device display name lookup over federation. By default, the
  3611. # Federation API allows other homeservers to obtain device display names of any user
  3612. # on this homeserver. Defaults to 'true'.
  3613. #
  3614. #allow_device_name_lookup_over_federation: false
  3615. ## Caching ##
  3616. # Caching can be configured through the following options.
  3617. #
  3618. # A cache 'factor' is a multiplier that can be applied to each of
  3619. # Synapse's caches in order to increase or decrease the maximum
  3620. # number of entries that can be stored.
  3621. # The number of events to cache in memory. Not affected by
  3622. # caches.global_factor.
  3623. #
  3624. #event_cache_size: 10K
  3625. caches:
  3626. # Controls the global cache factor, which is the default cache factor
  3627. # for all caches if a specific factor for that cache is not otherwise
  3628. # set.
  3629. #
  3630. # This can also be set by the &quot;SYNAPSE_CACHE_FACTOR&quot; environment
  3631. # variable. Setting by environment variable takes priority over
  3632. # setting through the config file.
  3633. #
  3634. # Defaults to 0.5, which will half the size of all caches.
  3635. #
  3636. #global_factor: 1.0
  3637. # A dictionary of cache name to cache factor for that individual
  3638. # cache. Overrides the global cache factor for a given cache.
  3639. #
  3640. # These can also be set through environment variables comprised
  3641. # of &quot;SYNAPSE_CACHE_FACTOR_&quot; + the name of the cache in capital
  3642. # letters and underscores. Setting by environment variable
  3643. # takes priority over setting through the config file.
  3644. # Ex. SYNAPSE_CACHE_FACTOR_GET_USERS_WHO_SHARE_ROOM_WITH_USER=2.0
  3645. #
  3646. # Some caches have '*' and other characters that are not
  3647. # alphanumeric or underscores. These caches can be named with or
  3648. # without the special characters stripped. For example, to specify
  3649. # the cache factor for `*stateGroupCache*` via an environment
  3650. # variable would be `SYNAPSE_CACHE_FACTOR_STATEGROUPCACHE=2.0`.
  3651. #
  3652. per_cache_factors:
  3653. #get_users_who_share_room_with_user: 2.0
  3654. # Controls how long an entry can be in a cache without having been
  3655. # accessed before being evicted. Defaults to None, which means
  3656. # entries are never evicted based on time.
  3657. #
  3658. #expiry_time: 30m
  3659. # Controls how long the results of a /sync request are cached for after
  3660. # a successful response is returned. A higher duration can help clients with
  3661. # intermittent connections, at the cost of higher memory usage.
  3662. #
  3663. # By default, this is zero, which means that sync responses are not cached
  3664. # at all.
  3665. #
  3666. #sync_response_cache_duration: 2m
  3667. ## Database ##
  3668. # The 'database' setting defines the database that synapse uses to store all of
  3669. # its data.
  3670. #
  3671. # 'name' gives the database engine to use: either 'sqlite3' (for SQLite) or
  3672. # 'psycopg2' (for PostgreSQL).
  3673. #
  3674. # 'txn_limit' gives the maximum number of transactions to run per connection
  3675. # before reconnecting. Defaults to 0, which means no limit.
  3676. #
  3677. # 'args' gives options which are passed through to the database engine,
  3678. # except for options starting 'cp_', which are used to configure the Twisted
  3679. # connection pool. For a reference to valid arguments, see:
  3680. # * for sqlite: https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
  3681. # * for postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
  3682. # * for the connection pool: https://twistedmatrix.com/documents/current/api/twisted.enterprise.adbapi.ConnectionPool.html#__init__
  3683. #
  3684. #
  3685. # Example SQLite configuration:
  3686. #
  3687. #database:
  3688. # name: sqlite3
  3689. # args:
  3690. # database: /path/to/homeserver.db
  3691. #
  3692. #
  3693. # Example Postgres configuration:
  3694. #
  3695. #database:
  3696. # name: psycopg2
  3697. # txn_limit: 10000
  3698. # args:
  3699. # user: synapse_user
  3700. # password: secretpassword
  3701. # database: synapse
  3702. # host: localhost
  3703. # port: 5432
  3704. # cp_min: 5
  3705. # cp_max: 10
  3706. #
  3707. # For more information on using Synapse with Postgres,
  3708. # see https://matrix-org.github.io/synapse/latest/postgres.html.
  3709. #
  3710. database:
  3711. name: sqlite3
  3712. args:
  3713. database: DATADIR/homeserver.db
  3714. ## Logging ##
  3715. # A yaml python logging config file as described by
  3716. # https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
  3717. #
  3718. log_config: &quot;CONFDIR/SERVERNAME.log.config&quot;
  3719. ## Ratelimiting ##
  3720. # Ratelimiting settings for client actions (registration, login, messaging).
  3721. #
  3722. # Each ratelimiting configuration is made of two parameters:
  3723. # - per_second: number of requests a client can send per second.
  3724. # - burst_count: number of requests a client can send before being throttled.
  3725. #
  3726. # Synapse currently uses the following configurations:
  3727. # - one for messages that ratelimits sending based on the account the client
  3728. # is using
  3729. # - one for registration that ratelimits registration requests based on the
  3730. # client's IP address.
  3731. # - one for checking the validity of registration tokens that ratelimits
  3732. # requests based on the client's IP address.
  3733. # - one for login that ratelimits login requests based on the client's IP
  3734. # address.
  3735. # - one for login that ratelimits login requests based on the account the
  3736. # client is attempting to log into.
  3737. # - one for login that ratelimits login requests based on the account the
  3738. # client is attempting to log into, based on the amount of failed login
  3739. # attempts for this account.
  3740. # - one for ratelimiting redactions by room admins. If this is not explicitly
  3741. # set then it uses the same ratelimiting as per rc_message. This is useful
  3742. # to allow room admins to deal with abuse quickly.
  3743. # - two for ratelimiting number of rooms a user can join, &quot;local&quot; for when
  3744. # users are joining rooms the server is already in (this is cheap) vs
  3745. # &quot;remote&quot; for when users are trying to join rooms not on the server (which
  3746. # can be more expensive)
  3747. # - one for ratelimiting how often a user or IP can attempt to validate a 3PID.
  3748. # - two for ratelimiting how often invites can be sent in a room or to a
  3749. # specific user.
  3750. #
  3751. # The defaults are as shown below.
  3752. #
  3753. #rc_message:
  3754. # per_second: 0.2
  3755. # burst_count: 10
  3756. #
  3757. #rc_registration:
  3758. # per_second: 0.17
  3759. # burst_count: 3
  3760. #
  3761. #rc_registration_token_validity:
  3762. # per_second: 0.1
  3763. # burst_count: 5
  3764. #
  3765. #rc_login:
  3766. # address:
  3767. # per_second: 0.17
  3768. # burst_count: 3
  3769. # account:
  3770. # per_second: 0.17
  3771. # burst_count: 3
  3772. # failed_attempts:
  3773. # per_second: 0.17
  3774. # burst_count: 3
  3775. #
  3776. #rc_admin_redaction:
  3777. # per_second: 1
  3778. # burst_count: 50
  3779. #
  3780. #rc_joins:
  3781. # local:
  3782. # per_second: 0.1
  3783. # burst_count: 10
  3784. # remote:
  3785. # per_second: 0.01
  3786. # burst_count: 10
  3787. #
  3788. #rc_3pid_validation:
  3789. # per_second: 0.003
  3790. # burst_count: 5
  3791. #
  3792. #rc_invites:
  3793. # per_room:
  3794. # per_second: 0.3
  3795. # burst_count: 10
  3796. # per_user:
  3797. # per_second: 0.003
  3798. # burst_count: 5
  3799. # Ratelimiting settings for incoming federation
  3800. #
  3801. # The rc_federation configuration is made up of the following settings:
  3802. # - window_size: window size in milliseconds
  3803. # - sleep_limit: number of federation requests from a single server in
  3804. # a window before the server will delay processing the request.
  3805. # - sleep_delay: duration in milliseconds to delay processing events
  3806. # from remote servers by if they go over the sleep limit.
  3807. # - reject_limit: maximum number of concurrent federation requests
  3808. # allowed from a single server
  3809. # - concurrent: number of federation requests to concurrently process
  3810. # from a single server
  3811. #
  3812. # The defaults are as shown below.
  3813. #
  3814. #rc_federation:
  3815. # window_size: 1000
  3816. # sleep_limit: 10
  3817. # sleep_delay: 500
  3818. # reject_limit: 50
  3819. # concurrent: 3
  3820. # Target outgoing federation transaction frequency for sending read-receipts,
  3821. # per-room.
  3822. #
  3823. # If we end up trying to send out more read-receipts, they will get buffered up
  3824. # into fewer transactions.
  3825. #
  3826. #federation_rr_transactions_per_room_per_second: 50
  3827. ## Media Store ##
  3828. # Enable the media store service in the Synapse master. Uncomment the
  3829. # following if you are using a separate media store worker.
  3830. #
  3831. #enable_media_repo: false
  3832. # Directory where uploaded images and attachments are stored.
  3833. #
  3834. media_store_path: &quot;DATADIR/media_store&quot;
  3835. # Media storage providers allow media to be stored in different
  3836. # locations.
  3837. #
  3838. #media_storage_providers:
  3839. # - module: file_system
  3840. # # Whether to store newly uploaded local files
  3841. # store_local: false
  3842. # # Whether to store newly downloaded remote files
  3843. # store_remote: false
  3844. # # Whether to wait for successful storage for local uploads
  3845. # store_synchronous: false
  3846. # config:
  3847. # directory: /mnt/some/other/directory
  3848. # The largest allowed upload size in bytes
  3849. #
  3850. # If you are using a reverse proxy you may also need to set this value in
  3851. # your reverse proxy's config. Notably Nginx has a small max body size by default.
  3852. # See https://matrix-org.github.io/synapse/latest/reverse_proxy.html.
  3853. #
  3854. #max_upload_size: 50M
  3855. # Maximum number of pixels that will be thumbnailed
  3856. #
  3857. #max_image_pixels: 32M
  3858. # Whether to generate new thumbnails on the fly to precisely match
  3859. # the resolution requested by the client. If true then whenever
  3860. # a new resolution is requested by the client the server will
  3861. # generate a new thumbnail. If false the server will pick a thumbnail
  3862. # from a precalculated list.
  3863. #
  3864. #dynamic_thumbnails: false
  3865. # List of thumbnails to precalculate when an image is uploaded.
  3866. #
  3867. #thumbnail_sizes:
  3868. # - width: 32
  3869. # height: 32
  3870. # method: crop
  3871. # - width: 96
  3872. # height: 96
  3873. # method: crop
  3874. # - width: 320
  3875. # height: 240
  3876. # method: scale
  3877. # - width: 640
  3878. # height: 480
  3879. # method: scale
  3880. # - width: 800
  3881. # height: 600
  3882. # method: scale
  3883. # Is the preview URL API enabled?
  3884. #
  3885. # 'false' by default: uncomment the following to enable it (and specify a
  3886. # url_preview_ip_range_blacklist blacklist).
  3887. #
  3888. #url_preview_enabled: true
  3889. # List of IP address CIDR ranges that the URL preview spider is denied
  3890. # from accessing. There are no defaults: you must explicitly
  3891. # specify a list for URL previewing to work. You should specify any
  3892. # internal services in your network that you do not want synapse to try
  3893. # to connect to, otherwise anyone in any Matrix room could cause your
  3894. # synapse to issue arbitrary GET requests to your internal services,
  3895. # causing serious security issues.
  3896. #
  3897. # (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
  3898. # listed here, since they correspond to unroutable addresses.)
  3899. #
  3900. # This must be specified if url_preview_enabled is set. It is recommended that
  3901. # you uncomment the following list as a starting point.
  3902. #
  3903. # Note: The value is ignored when an HTTP proxy is in use
  3904. #
  3905. #url_preview_ip_range_blacklist:
  3906. # - '127.0.0.0/8'
  3907. # - '10.0.0.0/8'
  3908. # - '172.16.0.0/12'
  3909. # - '192.168.0.0/16'
  3910. # - '100.64.0.0/10'
  3911. # - '192.0.0.0/24'
  3912. # - '169.254.0.0/16'
  3913. # - '192.88.99.0/24'
  3914. # - '198.18.0.0/15'
  3915. # - '192.0.2.0/24'
  3916. # - '198.51.100.0/24'
  3917. # - '203.0.113.0/24'
  3918. # - '224.0.0.0/4'
  3919. # - '::1/128'
  3920. # - 'fe80::/10'
  3921. # - 'fc00::/7'
  3922. # - '2001:db8::/32'
  3923. # - 'ff00::/8'
  3924. # - 'fec0::/10'
  3925. # List of IP address CIDR ranges that the URL preview spider is allowed
  3926. # to access even if they are specified in url_preview_ip_range_blacklist.
  3927. # This is useful for specifying exceptions to wide-ranging blacklisted
  3928. # target IP ranges - e.g. for enabling URL previews for a specific private
  3929. # website only visible in your network.
  3930. #
  3931. #url_preview_ip_range_whitelist:
  3932. # - '192.168.1.1'
  3933. # Optional list of URL matches that the URL preview spider is
  3934. # denied from accessing. You should use url_preview_ip_range_blacklist
  3935. # in preference to this, otherwise someone could define a public DNS
  3936. # entry that points to a private IP address and circumvent the blacklist.
  3937. # This is more useful if you know there is an entire shape of URL that
  3938. # you know that will never want synapse to try to spider.
  3939. #
  3940. # Each list entry is a dictionary of url component attributes as returned
  3941. # by urlparse.urlsplit as applied to the absolute form of the URL. See
  3942. # https://docs.python.org/2/library/urlparse.html#urlparse.urlsplit
  3943. # The values of the dictionary are treated as an filename match pattern
  3944. # applied to that component of URLs, unless they start with a ^ in which
  3945. # case they are treated as a regular expression match. If all the
  3946. # specified component matches for a given list item succeed, the URL is
  3947. # blacklisted.
  3948. #
  3949. #url_preview_url_blacklist:
  3950. # # blacklist any URL with a username in its URI
  3951. # - username: '*'
  3952. #
  3953. # # blacklist all *.google.com URLs
  3954. # - netloc: 'google.com'
  3955. # - netloc: '*.google.com'
  3956. #
  3957. # # blacklist all plain HTTP URLs
  3958. # - scheme: 'http'
  3959. #
  3960. # # blacklist http(s)://www.acme.com/foo
  3961. # - netloc: 'www.acme.com'
  3962. # path: '/foo'
  3963. #
  3964. # # blacklist any URL with a literal IPv4 address
  3965. # - netloc: '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
  3966. # The largest allowed URL preview spidering size in bytes
  3967. #
  3968. #max_spider_size: 10M
  3969. # A list of values for the Accept-Language HTTP header used when
  3970. # downloading webpages during URL preview generation. This allows
  3971. # Synapse to specify the preferred languages that URL previews should
  3972. # be in when communicating with remote servers.
  3973. #
  3974. # Each value is a IETF language tag; a 2-3 letter identifier for a
  3975. # language, optionally followed by subtags separated by '-', specifying
  3976. # a country or region variant.
  3977. #
  3978. # Multiple values can be provided, and a weight can be added to each by
  3979. # using quality value syntax (;q=). '*' translates to any language.
  3980. #
  3981. # Defaults to &quot;en&quot;.
  3982. #
  3983. # Example:
  3984. #
  3985. # url_preview_accept_language:
  3986. # - en-UK
  3987. # - en-US;q=0.9
  3988. # - fr;q=0.8
  3989. # - *;q=0.7
  3990. #
  3991. url_preview_accept_language:
  3992. # - en
  3993. # oEmbed allows for easier embedding content from a website. It can be
  3994. # used for generating URLs previews of services which support it.
  3995. #
  3996. oembed:
  3997. # A default list of oEmbed providers is included with Synapse.
  3998. #
  3999. # Uncomment the following to disable using these default oEmbed URLs.
  4000. # Defaults to 'false'.
  4001. #
  4002. #disable_default_providers: true
  4003. # Additional files with oEmbed configuration (each should be in the
  4004. # form of providers.json).
  4005. #
  4006. # By default, this list is empty (so only the default providers.json
  4007. # is used).
  4008. #
  4009. #additional_providers:
  4010. # - oembed/my_providers.json
  4011. ## Captcha ##
  4012. # See docs/CAPTCHA_SETUP.md for full details of configuring this.
  4013. # This homeserver's ReCAPTCHA public key. Must be specified if
  4014. # enable_registration_captcha is enabled.
  4015. #
  4016. #recaptcha_public_key: &quot;YOUR_PUBLIC_KEY&quot;
  4017. # This homeserver's ReCAPTCHA private key. Must be specified if
  4018. # enable_registration_captcha is enabled.
  4019. #
  4020. #recaptcha_private_key: &quot;YOUR_PRIVATE_KEY&quot;
  4021. # Uncomment to enable ReCaptcha checks when registering, preventing signup
  4022. # unless a captcha is answered. Requires a valid ReCaptcha
  4023. # public/private key. Defaults to 'false'.
  4024. #
  4025. #enable_registration_captcha: true
  4026. # The API endpoint to use for verifying m.login.recaptcha responses.
  4027. # Defaults to &quot;https://www.recaptcha.net/recaptcha/api/siteverify&quot;.
  4028. #
  4029. #recaptcha_siteverify_api: &quot;https://my.recaptcha.site&quot;
  4030. ## TURN ##
  4031. # The public URIs of the TURN server to give to clients
  4032. #
  4033. #turn_uris: []
  4034. # The shared secret used to compute passwords for the TURN server
  4035. #
  4036. #turn_shared_secret: &quot;YOUR_SHARED_SECRET&quot;
  4037. # The Username and password if the TURN server needs them and
  4038. # does not use a token
  4039. #
  4040. #turn_username: &quot;TURNSERVER_USERNAME&quot;
  4041. #turn_password: &quot;TURNSERVER_PASSWORD&quot;
  4042. # How long generated TURN credentials last
  4043. #
  4044. #turn_user_lifetime: 1h
  4045. # Whether guests should be allowed to use the TURN server.
  4046. # This defaults to True, otherwise VoIP will be unreliable for guests.
  4047. # However, it does introduce a slight security risk as it allows users to
  4048. # connect to arbitrary endpoints without having first signed up for a
  4049. # valid account (e.g. by passing a CAPTCHA).
  4050. #
  4051. #turn_allow_guests: true
  4052. ## Registration ##
  4053. #
  4054. # Registration can be rate-limited using the parameters in the &quot;Ratelimiting&quot;
  4055. # section of this file.
  4056. # Enable registration for new users.
  4057. #
  4058. #enable_registration: false
  4059. # Time that a user's session remains valid for, after they log in.
  4060. #
  4061. # Note that this is not currently compatible with guest logins.
  4062. #
  4063. # Note also that this is calculated at login time: changes are not applied
  4064. # retrospectively to users who have already logged in.
  4065. #
  4066. # By default, this is infinite.
  4067. #
  4068. #session_lifetime: 24h
  4069. # The user must provide all of the below types of 3PID when registering.
  4070. #
  4071. #registrations_require_3pid:
  4072. # - email
  4073. # - msisdn
  4074. # Explicitly disable asking for MSISDNs from the registration
  4075. # flow (overrides registrations_require_3pid if MSISDNs are set as required)
  4076. #
  4077. #disable_msisdn_registration: true
  4078. # Mandate that users are only allowed to associate certain formats of
  4079. # 3PIDs with accounts on this server.
  4080. #
  4081. #allowed_local_3pids:
  4082. # - medium: email
  4083. # pattern: '^[^@]+@matrix\.org$'
  4084. # - medium: email
  4085. # pattern: '^[^@]+@vector\.im$'
  4086. # - medium: msisdn
  4087. # pattern: '\+44'
  4088. # Enable 3PIDs lookup requests to identity servers from this server.
  4089. #
  4090. #enable_3pid_lookup: true
  4091. # Require users to submit a token during registration.
  4092. # Tokens can be managed using the admin API:
  4093. # https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/registration_tokens.html
  4094. # Note that `enable_registration` must be set to `true`.
  4095. # Disabling this option will not delete any tokens previously generated.
  4096. # Defaults to false. Uncomment the following to require tokens:
  4097. #
  4098. #registration_requires_token: true
  4099. # If set, allows registration of standard or admin accounts by anyone who
  4100. # has the shared secret, even if registration is otherwise disabled.
  4101. #
  4102. #registration_shared_secret: &lt;PRIVATE STRING&gt;
  4103. # Set the number of bcrypt rounds used to generate password hash.
  4104. # Larger numbers increase the work factor needed to generate the hash.
  4105. # The default number is 12 (which equates to 2^12 rounds).
  4106. # N.B. that increasing this will exponentially increase the time required
  4107. # to register or login - e.g. 24 =&gt; 2^24 rounds which will take &gt;20 mins.
  4108. #
  4109. #bcrypt_rounds: 12
  4110. # Allows users to register as guests without a password/email/etc, and
  4111. # participate in rooms hosted on this server which have been made
  4112. # accessible to anonymous users.
  4113. #
  4114. #allow_guest_access: false
  4115. # The identity server which we suggest that clients should use when users log
  4116. # in on this server.
  4117. #
  4118. # (By default, no suggestion is made, so it is left up to the client.
  4119. # This setting is ignored unless public_baseurl is also explicitly set.)
  4120. #
  4121. #default_identity_server: https://matrix.org
  4122. # Handle threepid (email/phone etc) registration and password resets through a set of
  4123. # *trusted* identity servers. Note that this allows the configured identity server to
  4124. # reset passwords for accounts!
  4125. #
  4126. # Be aware that if `email` is not set, and SMTP options have not been
  4127. # configured in the email config block, registration and user password resets via
  4128. # email will be globally disabled.
  4129. #
  4130. # Additionally, if `msisdn` is not set, registration and password resets via msisdn
  4131. # will be disabled regardless, and users will not be able to associate an msisdn
  4132. # identifier to their account. This is due to Synapse currently not supporting
  4133. # any method of sending SMS messages on its own.
  4134. #
  4135. # To enable using an identity server for operations regarding a particular third-party
  4136. # identifier type, set the value to the URL of that identity server as shown in the
  4137. # examples below.
  4138. #
  4139. # Servers handling the these requests must answer the `/requestToken` endpoints defined
  4140. # by the Matrix Identity Service API specification:
  4141. # https://matrix.org/docs/spec/identity_service/latest
  4142. #
  4143. account_threepid_delegates:
  4144. #email: https://example.com # Delegate email sending to example.com
  4145. #msisdn: http://localhost:8090 # Delegate SMS sending to this local process
  4146. # Whether users are allowed to change their displayname after it has
  4147. # been initially set. Useful when provisioning users based on the
  4148. # contents of a third-party directory.
  4149. #
  4150. # Does not apply to server administrators. Defaults to 'true'
  4151. #
  4152. #enable_set_displayname: false
  4153. # Whether users are allowed to change their avatar after it has been
  4154. # initially set. Useful when provisioning users based on the contents
  4155. # of a third-party directory.
  4156. #
  4157. # Does not apply to server administrators. Defaults to 'true'
  4158. #
  4159. #enable_set_avatar_url: false
  4160. # Whether users can change the 3PIDs associated with their accounts
  4161. # (email address and msisdn).
  4162. #
  4163. # Defaults to 'true'
  4164. #
  4165. #enable_3pid_changes: false
  4166. # Users who register on this homeserver will automatically be joined
  4167. # to these rooms.
  4168. #
  4169. # By default, any room aliases included in this list will be created
  4170. # as a publicly joinable room when the first user registers for the
  4171. # homeserver. This behaviour can be customised with the settings below.
  4172. # If the room already exists, make certain it is a publicly joinable
  4173. # room. The join rule of the room must be set to 'public'.
  4174. #
  4175. #auto_join_rooms:
  4176. # - &quot;#example:example.com&quot;
  4177. # Where auto_join_rooms are specified, setting this flag ensures that the
  4178. # the rooms exist by creating them when the first user on the
  4179. # homeserver registers.
  4180. #
  4181. # By default the auto-created rooms are publicly joinable from any federated
  4182. # server. Use the autocreate_auto_join_rooms_federated and
  4183. # autocreate_auto_join_room_preset settings below to customise this behaviour.
  4184. #
  4185. # Setting to false means that if the rooms are not manually created,
  4186. # users cannot be auto-joined since they do not exist.
  4187. #
  4188. # Defaults to true. Uncomment the following line to disable automatically
  4189. # creating auto-join rooms.
  4190. #
  4191. #autocreate_auto_join_rooms: false
  4192. # Whether the auto_join_rooms that are auto-created are available via
  4193. # federation. Only has an effect if autocreate_auto_join_rooms is true.
  4194. #
  4195. # Note that whether a room is federated cannot be modified after
  4196. # creation.
  4197. #
  4198. # Defaults to true: the room will be joinable from other servers.
  4199. # Uncomment the following to prevent users from other homeservers from
  4200. # joining these rooms.
  4201. #
  4202. #autocreate_auto_join_rooms_federated: false
  4203. # The room preset to use when auto-creating one of auto_join_rooms. Only has an
  4204. # effect if autocreate_auto_join_rooms is true.
  4205. #
  4206. # This can be one of &quot;public_chat&quot;, &quot;private_chat&quot;, or &quot;trusted_private_chat&quot;.
  4207. # If a value of &quot;private_chat&quot; or &quot;trusted_private_chat&quot; is used then
  4208. # auto_join_mxid_localpart must also be configured.
  4209. #
  4210. # Defaults to &quot;public_chat&quot;, meaning that the room is joinable by anyone, including
  4211. # federated servers if autocreate_auto_join_rooms_federated is true (the default).
  4212. # Uncomment the following to require an invitation to join these rooms.
  4213. #
  4214. #autocreate_auto_join_room_preset: private_chat
  4215. # The local part of the user id which is used to create auto_join_rooms if
  4216. # autocreate_auto_join_rooms is true. If this is not provided then the
  4217. # initial user account that registers will be used to create the rooms.
  4218. #
  4219. # The user id is also used to invite new users to any auto-join rooms which
  4220. # are set to invite-only.
  4221. #
  4222. # It *must* be configured if autocreate_auto_join_room_preset is set to
  4223. # &quot;private_chat&quot; or &quot;trusted_private_chat&quot;.
  4224. #
  4225. # Note that this must be specified in order for new users to be correctly
  4226. # invited to any auto-join rooms which have been set to invite-only (either
  4227. # at the time of creation or subsequently).
  4228. #
  4229. # Note that, if the room already exists, this user must be joined and
  4230. # have the appropriate permissions to invite new members.
  4231. #
  4232. #auto_join_mxid_localpart: system
  4233. # When auto_join_rooms is specified, setting this flag to false prevents
  4234. # guest accounts from being automatically joined to the rooms.
  4235. #
  4236. # Defaults to true.
  4237. #
  4238. #auto_join_rooms_for_guests: false
  4239. ## Metrics ###
  4240. # Enable collection and rendering of performance metrics
  4241. #
  4242. #enable_metrics: false
  4243. # Enable sentry integration
  4244. # NOTE: While attempts are made to ensure that the logs don't contain
  4245. # any sensitive information, this cannot be guaranteed. By enabling
  4246. # this option the sentry server may therefore receive sensitive
  4247. # information, and it in turn may then diseminate sensitive information
  4248. # through insecure notification channels if so configured.
  4249. #
  4250. #sentry:
  4251. # dsn: &quot;...&quot;
  4252. # Flags to enable Prometheus metrics which are not suitable to be
  4253. # enabled by default, either for performance reasons or limited use.
  4254. #
  4255. metrics_flags:
  4256. # Publish synapse_federation_known_servers, a gauge of the number of
  4257. # servers this homeserver knows about, including itself. May cause
  4258. # performance problems on large homeservers.
  4259. #
  4260. #known_servers: true
  4261. # Whether or not to report anonymized homeserver usage statistics.
  4262. #
  4263. #report_stats: true|false
  4264. # The endpoint to report the anonymized homeserver usage statistics to.
  4265. # Defaults to https://matrix.org/report-usage-stats/push
  4266. #
  4267. #report_stats_endpoint: https://example.com/report-usage-stats/push
  4268. ## API Configuration ##
  4269. # Controls for the state that is shared with users who receive an invite
  4270. # to a room
  4271. #
  4272. room_prejoin_state:
  4273. # By default, the following state event types are shared with users who
  4274. # receive invites to the room:
  4275. #
  4276. # - m.room.join_rules
  4277. # - m.room.canonical_alias
  4278. # - m.room.avatar
  4279. # - m.room.encryption
  4280. # - m.room.name
  4281. # - m.room.create
  4282. #
  4283. # Uncomment the following to disable these defaults (so that only the event
  4284. # types listed in 'additional_event_types' are shared). Defaults to 'false'.
  4285. #
  4286. #disable_default_event_types: true
  4287. # Additional state event types to share with users when they are invited
  4288. # to a room.
  4289. #
  4290. # By default, this list is empty (so only the default event types are shared).
  4291. #
  4292. #additional_event_types:
  4293. # - org.example.custom.event.type
  4294. # A list of application service config files to use
  4295. #
  4296. #app_service_config_files:
  4297. # - app_service_1.yaml
  4298. # - app_service_2.yaml
  4299. # Uncomment to enable tracking of application service IP addresses. Implicitly
  4300. # enables MAU tracking for application service users.
  4301. #
  4302. #track_appservice_user_ips: true
  4303. # a secret which is used to sign access tokens. If none is specified,
  4304. # the registration_shared_secret is used, if one is given; otherwise,
  4305. # a secret key is derived from the signing key.
  4306. #
  4307. #macaroon_secret_key: &lt;PRIVATE STRING&gt;
  4308. # a secret which is used to calculate HMACs for form values, to stop
  4309. # falsification of values. Must be specified for the User Consent
  4310. # forms to work.
  4311. #
  4312. #form_secret: &lt;PRIVATE STRING&gt;
  4313. ## Signing Keys ##
  4314. # Path to the signing key to sign messages with
  4315. #
  4316. signing_key_path: &quot;CONFDIR/SERVERNAME.signing.key&quot;
  4317. # The keys that the server used to sign messages with but won't use
  4318. # to sign new messages.
  4319. #
  4320. old_signing_keys:
  4321. # For each key, `key` should be the base64-encoded public key, and
  4322. # `expired_ts`should be the time (in milliseconds since the unix epoch) that
  4323. # it was last used.
  4324. #
  4325. # It is possible to build an entry from an old signing.key file using the
  4326. # `export_signing_key` script which is provided with synapse.
  4327. #
  4328. # For example:
  4329. #
  4330. #&quot;ed25519:id&quot;: { key: &quot;base64string&quot;, expired_ts: 123456789123 }
  4331. # How long key response published by this server is valid for.
  4332. # Used to set the valid_until_ts in /key/v2 APIs.
  4333. # Determines how quickly servers will query to check which keys
  4334. # are still valid.
  4335. #
  4336. #key_refresh_interval: 1d
  4337. # The trusted servers to download signing keys from.
  4338. #
  4339. # When we need to fetch a signing key, each server is tried in parallel.
  4340. #
  4341. # Normally, the connection to the key server is validated via TLS certificates.
  4342. # Additional security can be provided by configuring a `verify key`, which
  4343. # will make synapse check that the response is signed by that key.
  4344. #
  4345. # This setting supercedes an older setting named `perspectives`. The old format
  4346. # is still supported for backwards-compatibility, but it is deprecated.
  4347. #
  4348. # 'trusted_key_servers' defaults to matrix.org, but using it will generate a
  4349. # warning on start-up. To suppress this warning, set
  4350. # 'suppress_key_server_warning' to true.
  4351. #
  4352. # Options for each entry in the list include:
  4353. #
  4354. # server_name: the name of the server. required.
  4355. #
  4356. # verify_keys: an optional map from key id to base64-encoded public key.
  4357. # If specified, we will check that the response is signed by at least
  4358. # one of the given keys.
  4359. #
  4360. # accept_keys_insecurely: a boolean. Normally, if `verify_keys` is unset,
  4361. # and federation_verify_certificates is not `true`, synapse will refuse
  4362. # to start, because this would allow anyone who can spoof DNS responses
  4363. # to masquerade as the trusted key server. If you know what you are doing
  4364. # and are sure that your network environment provides a secure connection
  4365. # to the key server, you can set this to `true` to override this
  4366. # behaviour.
  4367. #
  4368. # An example configuration might look like:
  4369. #
  4370. #trusted_key_servers:
  4371. # - server_name: &quot;my_trusted_server.example.com&quot;
  4372. # verify_keys:
  4373. # &quot;ed25519:auto&quot;: &quot;abcdefghijklmnopqrstuvwxyzabcdefghijklmopqr&quot;
  4374. # - server_name: &quot;my_other_trusted_server.example.com&quot;
  4375. #
  4376. trusted_key_servers:
  4377. - server_name: &quot;matrix.org&quot;
  4378. # Uncomment the following to disable the warning that is emitted when the
  4379. # trusted_key_servers include 'matrix.org'. See above.
  4380. #
  4381. #suppress_key_server_warning: true
  4382. # The signing keys to use when acting as a trusted key server. If not specified
  4383. # defaults to the server signing key.
  4384. #
  4385. # Can contain multiple keys, one per line.
  4386. #
  4387. #key_server_signing_keys_path: &quot;key_server_signing_keys.key&quot;
  4388. ## Single sign-on integration ##
  4389. # The following settings can be used to make Synapse use a single sign-on
  4390. # provider for authentication, instead of its internal password database.
  4391. #
  4392. # You will probably also want to set the following options to `false` to
  4393. # disable the regular login/registration flows:
  4394. # * enable_registration
  4395. # * password_config.enabled
  4396. #
  4397. # You will also want to investigate the settings under the &quot;sso&quot; configuration
  4398. # section below.
  4399. # Enable SAML2 for registration and login. Uses pysaml2.
  4400. #
  4401. # At least one of `sp_config` or `config_path` must be set in this section to
  4402. # enable SAML login.
  4403. #
  4404. # Once SAML support is enabled, a metadata file will be exposed at
  4405. # https://&lt;server&gt;:&lt;port&gt;/_synapse/client/saml2/metadata.xml, which you may be able to
  4406. # use to configure your SAML IdP with. Alternatively, you can manually configure
  4407. # the IdP to use an ACS location of
  4408. # https://&lt;server&gt;:&lt;port&gt;/_synapse/client/saml2/authn_response.
  4409. #
  4410. saml2_config:
  4411. # `sp_config` is the configuration for the pysaml2 Service Provider.
  4412. # See pysaml2 docs for format of config.
  4413. #
  4414. # Default values will be used for the 'entityid' and 'service' settings,
  4415. # so it is not normally necessary to specify them unless you need to
  4416. # override them.
  4417. #
  4418. sp_config:
  4419. # Point this to the IdP's metadata. You must provide either a local
  4420. # file via the `local` attribute or (preferably) a URL via the
  4421. # `remote` attribute.
  4422. #
  4423. #metadata:
  4424. # local: [&quot;saml2/idp.xml&quot;]
  4425. # remote:
  4426. # - url: https://our_idp/metadata.xml
  4427. # Allowed clock difference in seconds between the homeserver and IdP.
  4428. #
  4429. # Uncomment the below to increase the accepted time difference from 0 to 3 seconds.
  4430. #
  4431. #accepted_time_diff: 3
  4432. # By default, the user has to go to our login page first. If you'd like
  4433. # to allow IdP-initiated login, set 'allow_unsolicited: true' in a
  4434. # 'service.sp' section:
  4435. #
  4436. #service:
  4437. # sp:
  4438. # allow_unsolicited: true
  4439. # The examples below are just used to generate our metadata xml, and you
  4440. # may well not need them, depending on your setup. Alternatively you
  4441. # may need a whole lot more detail - see the pysaml2 docs!
  4442. #description: [&quot;My awesome SP&quot;, &quot;en&quot;]
  4443. #name: [&quot;Test SP&quot;, &quot;en&quot;]
  4444. #ui_info:
  4445. # display_name:
  4446. # - lang: en
  4447. # text: &quot;Display Name is the descriptive name of your service.&quot;
  4448. # description:
  4449. # - lang: en
  4450. # text: &quot;Description should be a short paragraph explaining the purpose of the service.&quot;
  4451. # information_url:
  4452. # - lang: en
  4453. # text: &quot;https://example.com/terms-of-service&quot;
  4454. # privacy_statement_url:
  4455. # - lang: en
  4456. # text: &quot;https://example.com/privacy-policy&quot;
  4457. # keywords:
  4458. # - lang: en
  4459. # text: [&quot;Matrix&quot;, &quot;Element&quot;]
  4460. # logo:
  4461. # - lang: en
  4462. # text: &quot;https://example.com/logo.svg&quot;
  4463. # width: &quot;200&quot;
  4464. # height: &quot;80&quot;
  4465. #organization:
  4466. # name: Example com
  4467. # display_name:
  4468. # - [&quot;Example co&quot;, &quot;en&quot;]
  4469. # url: &quot;http://example.com&quot;
  4470. #contact_person:
  4471. # - given_name: Bob
  4472. # sur_name: &quot;the Sysadmin&quot;
  4473. # email_address&quot;: [&quot;admin@example.com&quot;]
  4474. # contact_type&quot;: technical
  4475. # Instead of putting the config inline as above, you can specify a
  4476. # separate pysaml2 configuration file:
  4477. #
  4478. #config_path: &quot;CONFDIR/sp_conf.py&quot;
  4479. # The lifetime of a SAML session. This defines how long a user has to
  4480. # complete the authentication process, if allow_unsolicited is unset.
  4481. # The default is 15 minutes.
  4482. #
  4483. #saml_session_lifetime: 5m
  4484. # An external module can be provided here as a custom solution to
  4485. # mapping attributes returned from a saml provider onto a matrix user.
  4486. #
  4487. user_mapping_provider:
  4488. # The custom module's class. Uncomment to use a custom module.
  4489. #
  4490. #module: mapping_provider.SamlMappingProvider
  4491. # Custom configuration values for the module. Below options are
  4492. # intended for the built-in provider, they should be changed if
  4493. # using a custom module. This section will be passed as a Python
  4494. # dictionary to the module's `parse_config` method.
  4495. #
  4496. config:
  4497. # The SAML attribute (after mapping via the attribute maps) to use
  4498. # to derive the Matrix ID from. 'uid' by default.
  4499. #
  4500. # Note: This used to be configured by the
  4501. # saml2_config.mxid_source_attribute option. If that is still
  4502. # defined, its value will be used instead.
  4503. #
  4504. #mxid_source_attribute: displayName
  4505. # The mapping system to use for mapping the saml attribute onto a
  4506. # matrix ID.
  4507. #
  4508. # Options include:
  4509. # * 'hexencode' (which maps unpermitted characters to '=xx')
  4510. # * 'dotreplace' (which replaces unpermitted characters with
  4511. # '.').
  4512. # The default is 'hexencode'.
  4513. #
  4514. # Note: This used to be configured by the
  4515. # saml2_config.mxid_mapping option. If that is still defined, its
  4516. # value will be used instead.
  4517. #
  4518. #mxid_mapping: dotreplace
  4519. # In previous versions of synapse, the mapping from SAML attribute to
  4520. # MXID was always calculated dynamically rather than stored in a
  4521. # table. For backwards- compatibility, we will look for user_ids
  4522. # matching such a pattern before creating a new account.
  4523. #
  4524. # This setting controls the SAML attribute which will be used for this
  4525. # backwards-compatibility lookup. Typically it should be 'uid', but if
  4526. # the attribute maps are changed, it may be necessary to change it.
  4527. #
  4528. # The default is 'uid'.
  4529. #
  4530. #grandfathered_mxid_source_attribute: upn
  4531. # It is possible to configure Synapse to only allow logins if SAML attributes
  4532. # match particular values. The requirements can be listed under
  4533. # `attribute_requirements` as shown below. All of the listed attributes must
  4534. # match for the login to be permitted.
  4535. #
  4536. #attribute_requirements:
  4537. # - attribute: userGroup
  4538. # value: &quot;staff&quot;
  4539. # - attribute: department
  4540. # value: &quot;sales&quot;
  4541. # If the metadata XML contains multiple IdP entities then the `idp_entityid`
  4542. # option must be set to the entity to redirect users to.
  4543. #
  4544. # Most deployments only have a single IdP entity and so should omit this
  4545. # option.
  4546. #
  4547. #idp_entityid: 'https://our_idp/entityid'
  4548. # List of OpenID Connect (OIDC) / OAuth 2.0 identity providers, for registration
  4549. # and login.
  4550. #
  4551. # Options for each entry include:
  4552. #
  4553. # idp_id: a unique identifier for this identity provider. Used internally
  4554. # by Synapse; should be a single word such as 'github'.
  4555. #
  4556. # Note that, if this is changed, users authenticating via that provider
  4557. # will no longer be recognised as the same user!
  4558. #
  4559. # (Use &quot;oidc&quot; here if you are migrating from an old &quot;oidc_config&quot;
  4560. # configuration.)
  4561. #
  4562. # idp_name: A user-facing name for this identity provider, which is used to
  4563. # offer the user a choice of login mechanisms.
  4564. #
  4565. # idp_icon: An optional icon for this identity provider, which is presented
  4566. # by clients and Synapse's own IdP picker page. If given, must be an
  4567. # MXC URI of the format mxc://&lt;server-name&gt;/&lt;media-id&gt;. (An easy way to
  4568. # obtain such an MXC URI is to upload an image to an (unencrypted) room
  4569. # and then copy the &quot;url&quot; from the source of the event.)
  4570. #
  4571. # idp_brand: An optional brand for this identity provider, allowing clients
  4572. # to style the login flow according to the identity provider in question.
  4573. # See the spec for possible options here.
  4574. #
  4575. # discover: set to 'false' to disable the use of the OIDC discovery mechanism
  4576. # to discover endpoints. Defaults to true.
  4577. #
  4578. # issuer: Required. The OIDC issuer. Used to validate tokens and (if discovery
  4579. # is enabled) to discover the provider's endpoints.
  4580. #
  4581. # client_id: Required. oauth2 client id to use.
  4582. #
  4583. # client_secret: oauth2 client secret to use. May be omitted if
  4584. # client_secret_jwt_key is given, or if client_auth_method is 'none'.
  4585. #
  4586. # client_secret_jwt_key: Alternative to client_secret: details of a key used
  4587. # to create a JSON Web Token to be used as an OAuth2 client secret. If
  4588. # given, must be a dictionary with the following properties:
  4589. #
  4590. # key: a pem-encoded signing key. Must be a suitable key for the
  4591. # algorithm specified. Required unless 'key_file' is given.
  4592. #
  4593. # key_file: the path to file containing a pem-encoded signing key file.
  4594. # Required unless 'key' is given.
  4595. #
  4596. # jwt_header: a dictionary giving properties to include in the JWT
  4597. # header. Must include the key 'alg', giving the algorithm used to
  4598. # sign the JWT, such as &quot;ES256&quot;, using the JWA identifiers in
  4599. # RFC7518.
  4600. #
  4601. # jwt_payload: an optional dictionary giving properties to include in
  4602. # the JWT payload. Normally this should include an 'iss' key.
  4603. #
  4604. # client_auth_method: auth method to use when exchanging the token. Valid
  4605. # values are 'client_secret_basic' (default), 'client_secret_post' and
  4606. # 'none'.
  4607. #
  4608. # scopes: list of scopes to request. This should normally include the &quot;openid&quot;
  4609. # scope. Defaults to [&quot;openid&quot;].
  4610. #
  4611. # authorization_endpoint: the oauth2 authorization endpoint. Required if
  4612. # provider discovery is disabled.
  4613. #
  4614. # token_endpoint: the oauth2 token endpoint. Required if provider discovery is
  4615. # disabled.
  4616. #
  4617. # userinfo_endpoint: the OIDC userinfo endpoint. Required if discovery is
  4618. # disabled and the 'openid' scope is not requested.
  4619. #
  4620. # jwks_uri: URI where to fetch the JWKS. Required if discovery is disabled and
  4621. # the 'openid' scope is used.
  4622. #
  4623. # skip_verification: set to 'true' to skip metadata verification. Use this if
  4624. # you are connecting to a provider that is not OpenID Connect compliant.
  4625. # Defaults to false. Avoid this in production.
  4626. #
  4627. # user_profile_method: Whether to fetch the user profile from the userinfo
  4628. # endpoint. Valid values are: 'auto' or 'userinfo_endpoint'.
  4629. #
  4630. # Defaults to 'auto', which fetches the userinfo endpoint if 'openid' is
  4631. # included in 'scopes'. Set to 'userinfo_endpoint' to always fetch the
  4632. # userinfo endpoint.
  4633. #
  4634. # allow_existing_users: set to 'true' to allow a user logging in via OIDC to
  4635. # match a pre-existing account instead of failing. This could be used if
  4636. # switching from password logins to OIDC. Defaults to false.
  4637. #
  4638. # user_mapping_provider: Configuration for how attributes returned from a OIDC
  4639. # provider are mapped onto a matrix user. This setting has the following
  4640. # sub-properties:
  4641. #
  4642. # module: The class name of a custom mapping module. Default is
  4643. # 'synapse.handlers.oidc.JinjaOidcMappingProvider'.
  4644. # See https://matrix-org.github.io/synapse/latest/sso_mapping_providers.html#openid-mapping-providers
  4645. # for information on implementing a custom mapping provider.
  4646. #
  4647. # config: Configuration for the mapping provider module. This section will
  4648. # be passed as a Python dictionary to the user mapping provider
  4649. # module's `parse_config` method.
  4650. #
  4651. # For the default provider, the following settings are available:
  4652. #
  4653. # subject_claim: name of the claim containing a unique identifier
  4654. # for the user. Defaults to 'sub', which OpenID Connect
  4655. # compliant providers should provide.
  4656. #
  4657. # localpart_template: Jinja2 template for the localpart of the MXID.
  4658. # If this is not set, the user will be prompted to choose their
  4659. # own username (see 'sso_auth_account_details.html' in the 'sso'
  4660. # section of this file).
  4661. #
  4662. # display_name_template: Jinja2 template for the display name to set
  4663. # on first login. If unset, no displayname will be set.
  4664. #
  4665. # email_template: Jinja2 template for the email address of the user.
  4666. # If unset, no email address will be added to the account.
  4667. #
  4668. # extra_attributes: a map of Jinja2 templates for extra attributes
  4669. # to send back to the client during login.
  4670. # Note that these are non-standard and clients will ignore them
  4671. # without modifications.
  4672. #
  4673. # When rendering, the Jinja2 templates are given a 'user' variable,
  4674. # which is set to the claims returned by the UserInfo Endpoint and/or
  4675. # in the ID Token.
  4676. #
  4677. # It is possible to configure Synapse to only allow logins if certain attributes
  4678. # match particular values in the OIDC userinfo. The requirements can be listed under
  4679. # `attribute_requirements` as shown below. All of the listed attributes must
  4680. # match for the login to be permitted. Additional attributes can be added to
  4681. # userinfo by expanding the `scopes` section of the OIDC config to retrieve
  4682. # additional information from the OIDC provider.
  4683. #
  4684. # If the OIDC claim is a list, then the attribute must match any value in the list.
  4685. # Otherwise, it must exactly match the value of the claim. Using the example
  4686. # below, the `family_name` claim MUST be &quot;Stephensson&quot;, but the `groups`
  4687. # claim MUST contain &quot;admin&quot;.
  4688. #
  4689. # attribute_requirements:
  4690. # - attribute: family_name
  4691. # value: &quot;Stephensson&quot;
  4692. # - attribute: groups
  4693. # value: &quot;admin&quot;
  4694. #
  4695. # See https://matrix-org.github.io/synapse/latest/openid.html
  4696. # for information on how to configure these options.
  4697. #
  4698. # For backwards compatibility, it is also possible to configure a single OIDC
  4699. # provider via an 'oidc_config' setting. This is now deprecated and admins are
  4700. # advised to migrate to the 'oidc_providers' format. (When doing that migration,
  4701. # use 'oidc' for the idp_id to ensure that existing users continue to be
  4702. # recognised.)
  4703. #
  4704. oidc_providers:
  4705. # Generic example
  4706. #
  4707. #- idp_id: my_idp
  4708. # idp_name: &quot;My OpenID provider&quot;
  4709. # idp_icon: &quot;mxc://example.com/mediaid&quot;
  4710. # discover: false
  4711. # issuer: &quot;https://accounts.example.com/&quot;
  4712. # client_id: &quot;provided-by-your-issuer&quot;
  4713. # client_secret: &quot;provided-by-your-issuer&quot;
  4714. # client_auth_method: client_secret_post
  4715. # scopes: [&quot;openid&quot;, &quot;profile&quot;]
  4716. # authorization_endpoint: &quot;https://accounts.example.com/oauth2/auth&quot;
  4717. # token_endpoint: &quot;https://accounts.example.com/oauth2/token&quot;
  4718. # userinfo_endpoint: &quot;https://accounts.example.com/userinfo&quot;
  4719. # jwks_uri: &quot;https://accounts.example.com/.well-known/jwks.json&quot;
  4720. # skip_verification: true
  4721. # user_mapping_provider:
  4722. # config:
  4723. # subject_claim: &quot;id&quot;
  4724. # localpart_template: &quot;{{ user.login }}&quot;
  4725. # display_name_template: &quot;{{ user.name }}&quot;
  4726. # email_template: &quot;{{ user.email }}&quot;
  4727. # attribute_requirements:
  4728. # - attribute: userGroup
  4729. # value: &quot;synapseUsers&quot;
  4730. # Enable Central Authentication Service (CAS) for registration and login.
  4731. #
  4732. cas_config:
  4733. # Uncomment the following to enable authorization against a CAS server.
  4734. # Defaults to false.
  4735. #
  4736. #enabled: true
  4737. # The URL of the CAS authorization endpoint.
  4738. #
  4739. #server_url: &quot;https://cas-server.com&quot;
  4740. # The attribute of the CAS response to use as the display name.
  4741. #
  4742. # If unset, no displayname will be set.
  4743. #
  4744. #displayname_attribute: name
  4745. # It is possible to configure Synapse to only allow logins if CAS attributes
  4746. # match particular values. All of the keys in the mapping below must exist
  4747. # and the values must match the given value. Alternately if the given value
  4748. # is None then any value is allowed (the attribute just must exist).
  4749. # All of the listed attributes must match for the login to be permitted.
  4750. #
  4751. #required_attributes:
  4752. # userGroup: &quot;staff&quot;
  4753. # department: None
  4754. # Additional settings to use with single-sign on systems such as OpenID Connect,
  4755. # SAML2 and CAS.
  4756. #
  4757. # Server admins can configure custom templates for pages related to SSO. See
  4758. # https://matrix-org.github.io/synapse/latest/templates.html for more information.
  4759. #
  4760. sso:
  4761. # A list of client URLs which are whitelisted so that the user does not
  4762. # have to confirm giving access to their account to the URL. Any client
  4763. # whose URL starts with an entry in the following list will not be subject
  4764. # to an additional confirmation step after the SSO login is completed.
  4765. #
  4766. # WARNING: An entry such as &quot;https://my.client&quot; is insecure, because it
  4767. # will also match &quot;https://my.client.evil.site&quot;, exposing your users to
  4768. # phishing attacks from evil.site. To avoid this, include a slash after the
  4769. # hostname: &quot;https://my.client/&quot;.
  4770. #
  4771. # The login fallback page (used by clients that don't natively support the
  4772. # required login flows) is whitelisted in addition to any URLs in this list.
  4773. #
  4774. # By default, this list contains only the login fallback page.
  4775. #
  4776. #client_whitelist:
  4777. # - https://riot.im/develop
  4778. # - https://my.custom.client/
  4779. # Uncomment to keep a user's profile fields in sync with information from
  4780. # the identity provider. Currently only syncing the displayname is
  4781. # supported. Fields are checked on every SSO login, and are updated
  4782. # if necessary.
  4783. #
  4784. # Note that enabling this option will override user profile information,
  4785. # regardless of whether users have opted-out of syncing that
  4786. # information when first signing in. Defaults to false.
  4787. #
  4788. #update_profile_information: true
  4789. # JSON web token integration. The following settings can be used to make
  4790. # Synapse JSON web tokens for authentication, instead of its internal
  4791. # password database.
  4792. #
  4793. # Each JSON Web Token needs to contain a &quot;sub&quot; (subject) claim, which is
  4794. # used as the localpart of the mxid.
  4795. #
  4796. # Additionally, the expiration time (&quot;exp&quot;), not before time (&quot;nbf&quot;),
  4797. # and issued at (&quot;iat&quot;) claims are validated if present.
  4798. #
  4799. # Note that this is a non-standard login type and client support is
  4800. # expected to be non-existent.
  4801. #
  4802. # See https://matrix-org.github.io/synapse/latest/jwt.html.
  4803. #
  4804. #jwt_config:
  4805. # Uncomment the following to enable authorization using JSON web
  4806. # tokens. Defaults to false.
  4807. #
  4808. #enabled: true
  4809. # This is either the private shared secret or the public key used to
  4810. # decode the contents of the JSON web token.
  4811. #
  4812. # Required if 'enabled' is true.
  4813. #
  4814. #secret: &quot;provided-by-your-issuer&quot;
  4815. # The algorithm used to sign the JSON web token.
  4816. #
  4817. # Supported algorithms are listed at
  4818. # https://pyjwt.readthedocs.io/en/latest/algorithms.html
  4819. #
  4820. # Required if 'enabled' is true.
  4821. #
  4822. #algorithm: &quot;provided-by-your-issuer&quot;
  4823. # The issuer to validate the &quot;iss&quot; claim against.
  4824. #
  4825. # Optional, if provided the &quot;iss&quot; claim will be required and
  4826. # validated for all JSON web tokens.
  4827. #
  4828. #issuer: &quot;provided-by-your-issuer&quot;
  4829. # A list of audiences to validate the &quot;aud&quot; claim against.
  4830. #
  4831. # Optional, if provided the &quot;aud&quot; claim will be required and
  4832. # validated for all JSON web tokens.
  4833. #
  4834. # Note that if the &quot;aud&quot; claim is included in a JSON web token then
  4835. # validation will fail without configuring audiences.
  4836. #
  4837. #audiences:
  4838. # - &quot;provided-by-your-issuer&quot;
  4839. password_config:
  4840. # Uncomment to disable password login
  4841. #
  4842. #enabled: false
  4843. # Uncomment to disable authentication against the local password
  4844. # database. This is ignored if `enabled` is false, and is only useful
  4845. # if you have other password_providers.
  4846. #
  4847. #localdb_enabled: false
  4848. # Uncomment and change to a secret random string for extra security.
  4849. # DO NOT CHANGE THIS AFTER INITIAL SETUP!
  4850. #
  4851. #pepper: &quot;EVEN_MORE_SECRET&quot;
  4852. # Define and enforce a password policy. Each parameter is optional.
  4853. # This is an implementation of MSC2000.
  4854. #
  4855. policy:
  4856. # Whether to enforce the password policy.
  4857. # Defaults to 'false'.
  4858. #
  4859. #enabled: true
  4860. # Minimum accepted length for a password.
  4861. # Defaults to 0.
  4862. #
  4863. #minimum_length: 15
  4864. # Whether a password must contain at least one digit.
  4865. # Defaults to 'false'.
  4866. #
  4867. #require_digit: true
  4868. # Whether a password must contain at least one symbol.
  4869. # A symbol is any character that's not a number or a letter.
  4870. # Defaults to 'false'.
  4871. #
  4872. #require_symbol: true
  4873. # Whether a password must contain at least one lowercase letter.
  4874. # Defaults to 'false'.
  4875. #
  4876. #require_lowercase: true
  4877. # Whether a password must contain at least one uppercase letter.
  4878. # Defaults to 'false'.
  4879. #
  4880. #require_uppercase: true
  4881. ui_auth:
  4882. # The amount of time to allow a user-interactive authentication session
  4883. # to be active.
  4884. #
  4885. # This defaults to 0, meaning the user is queried for their credentials
  4886. # before every action, but this can be overridden to allow a single
  4887. # validation to be re-used. This weakens the protections afforded by
  4888. # the user-interactive authentication process, by allowing for multiple
  4889. # (and potentially different) operations to use the same validation session.
  4890. #
  4891. # This is ignored for potentially &quot;dangerous&quot; operations (including
  4892. # deactivating an account, modifying an account password, and
  4893. # adding a 3PID).
  4894. #
  4895. # Uncomment below to allow for credential validation to last for 15
  4896. # seconds.
  4897. #
  4898. #session_timeout: &quot;15s&quot;
  4899. # Configuration for sending emails from Synapse.
  4900. #
  4901. # Server admins can configure custom templates for email content. See
  4902. # https://matrix-org.github.io/synapse/latest/templates.html for more information.
  4903. #
  4904. email:
  4905. # The hostname of the outgoing SMTP server to use. Defaults to 'localhost'.
  4906. #
  4907. #smtp_host: mail.server
  4908. # The port on the mail server for outgoing SMTP. Defaults to 25.
  4909. #
  4910. #smtp_port: 587
  4911. # Username/password for authentication to the SMTP server. By default, no
  4912. # authentication is attempted.
  4913. #
  4914. #smtp_user: &quot;exampleusername&quot;
  4915. #smtp_pass: &quot;examplepassword&quot;
  4916. # Uncomment the following to require TLS transport security for SMTP.
  4917. # By default, Synapse will connect over plain text, and will then switch to
  4918. # TLS via STARTTLS *if the SMTP server supports it*. If this option is set,
  4919. # Synapse will refuse to connect unless the server supports STARTTLS.
  4920. #
  4921. #require_transport_security: true
  4922. # Uncomment the following to disable TLS for SMTP.
  4923. #
  4924. # By default, if the server supports TLS, it will be used, and the server
  4925. # must present a certificate that is valid for 'smtp_host'. If this option
  4926. # is set to false, TLS will not be used.
  4927. #
  4928. #enable_tls: false
  4929. # notif_from defines the &quot;From&quot; address to use when sending emails.
  4930. # It must be set if email sending is enabled.
  4931. #
  4932. # The placeholder '%(app)s' will be replaced by the application name,
  4933. # which is normally 'app_name' (below), but may be overridden by the
  4934. # Matrix client application.
  4935. #
  4936. # Note that the placeholder must be written '%(app)s', including the
  4937. # trailing 's'.
  4938. #
  4939. #notif_from: &quot;Your Friendly %(app)s homeserver &lt;noreply@example.com&gt;&quot;
  4940. # app_name defines the default value for '%(app)s' in notif_from and email
  4941. # subjects. It defaults to 'Matrix'.
  4942. #
  4943. #app_name: my_branded_matrix_server
  4944. # Uncomment the following to enable sending emails for messages that the user
  4945. # has missed. Disabled by default.
  4946. #
  4947. #enable_notifs: true
  4948. # Uncomment the following to disable automatic subscription to email
  4949. # notifications for new users. Enabled by default.
  4950. #
  4951. #notif_for_new_users: false
  4952. # Custom URL for client links within the email notifications. By default
  4953. # links will be based on &quot;https://matrix.to&quot;.
  4954. #
  4955. # (This setting used to be called riot_base_url; the old name is still
  4956. # supported for backwards-compatibility but is now deprecated.)
  4957. #
  4958. #client_base_url: &quot;http://localhost/riot&quot;
  4959. # Configure the time that a validation email will expire after sending.
  4960. # Defaults to 1h.
  4961. #
  4962. #validation_token_lifetime: 15m
  4963. # The web client location to direct users to during an invite. This is passed
  4964. # to the identity server as the org.matrix.web_client_location key. Defaults
  4965. # to unset, giving no guidance to the identity server.
  4966. #
  4967. #invite_client_location: https://app.element.io
  4968. # Subjects to use when sending emails from Synapse.
  4969. #
  4970. # The placeholder '%(app)s' will be replaced with the value of the 'app_name'
  4971. # setting above, or by a value dictated by the Matrix client application.
  4972. #
  4973. # If a subject isn't overridden in this configuration file, the value used as
  4974. # its example will be used.
  4975. #
  4976. #subjects:
  4977. # Subjects for notification emails.
  4978. #
  4979. # On top of the '%(app)s' placeholder, these can use the following
  4980. # placeholders:
  4981. #
  4982. # * '%(person)s', which will be replaced by the display name of the user(s)
  4983. # that sent the message(s), e.g. &quot;Alice and Bob&quot;.
  4984. # * '%(room)s', which will be replaced by the name of the room the
  4985. # message(s) have been sent to, e.g. &quot;My super room&quot;.
  4986. #
  4987. # See the example provided for each setting to see which placeholder can be
  4988. # used and how to use them.
  4989. #
  4990. # Subject to use to notify about one message from one or more user(s) in a
  4991. # room which has a name.
  4992. #message_from_person_in_room: &quot;[%(app)s] You have a message on %(app)s from %(person)s in the %(room)s room...&quot;
  4993. #
  4994. # Subject to use to notify about one message from one or more user(s) in a
  4995. # room which doesn't have a name.
  4996. #message_from_person: &quot;[%(app)s] You have a message on %(app)s from %(person)s...&quot;
  4997. #
  4998. # Subject to use to notify about multiple messages from one or more users in
  4999. # a room which doesn't have a name.
  5000. #messages_from_person: &quot;[%(app)s] You have messages on %(app)s from %(person)s...&quot;
  5001. #
  5002. # Subject to use to notify about multiple messages in a room which has a
  5003. # name.
  5004. #messages_in_room: &quot;[%(app)s] You have messages on %(app)s in the %(room)s room...&quot;
  5005. #
  5006. # Subject to use to notify about multiple messages in multiple rooms.
  5007. #messages_in_room_and_others: &quot;[%(app)s] You have messages on %(app)s in the %(room)s room and others...&quot;
  5008. #
  5009. # Subject to use to notify about multiple messages from multiple persons in
  5010. # multiple rooms. This is similar to the setting above except it's used when
  5011. # the room in which the notification was triggered has no name.
  5012. #messages_from_person_and_others: &quot;[%(app)s] You have messages on %(app)s from %(person)s and others...&quot;
  5013. #
  5014. # Subject to use to notify about an invite to a room which has a name.
  5015. #invite_from_person_to_room: &quot;[%(app)s] %(person)s has invited you to join the %(room)s room on %(app)s...&quot;
  5016. #
  5017. # Subject to use to notify about an invite to a room which doesn't have a
  5018. # name.
  5019. #invite_from_person: &quot;[%(app)s] %(person)s has invited you to chat on %(app)s...&quot;
  5020. # Subject for emails related to account administration.
  5021. #
  5022. # On top of the '%(app)s' placeholder, these one can use the
  5023. # '%(server_name)s' placeholder, which will be replaced by the value of the
  5024. # 'server_name' setting in your Synapse configuration.
  5025. #
  5026. # Subject to use when sending a password reset email.
  5027. #password_reset: &quot;[%(server_name)s] Password reset&quot;
  5028. #
  5029. # Subject to use when sending a verification email to assert an address's
  5030. # ownership.
  5031. #email_validation: &quot;[%(server_name)s] Validate your email&quot;
  5032. ## Push ##
  5033. push:
  5034. # Clients requesting push notifications can either have the body of
  5035. # the message sent in the notification poke along with other details
  5036. # like the sender, or just the event ID and room ID (`event_id_only`).
  5037. # If clients choose the former, this option controls whether the
  5038. # notification request includes the content of the event (other details
  5039. # like the sender are still included). For `event_id_only` push, it
  5040. # has no effect.
  5041. #
  5042. # For modern android devices the notification content will still appear
  5043. # because it is loaded by the app. iPhone, however will send a
  5044. # notification saying only that a message arrived and who it came from.
  5045. #
  5046. # The default value is &quot;true&quot; to include message details. Uncomment to only
  5047. # include the event ID and room ID in push notification payloads.
  5048. #
  5049. #include_content: false
  5050. # When a push notification is received, an unread count is also sent.
  5051. # This number can either be calculated as the number of unread messages
  5052. # for the user, or the number of *rooms* the user has unread messages in.
  5053. #
  5054. # The default value is &quot;true&quot;, meaning push clients will see the number of
  5055. # rooms with unread messages in them. Uncomment to instead send the number
  5056. # of unread messages.
  5057. #
  5058. #group_unread_count_by_room: false
  5059. ## Rooms ##
  5060. # Controls whether locally-created rooms should be end-to-end encrypted by
  5061. # default.
  5062. #
  5063. # Possible options are &quot;all&quot;, &quot;invite&quot;, and &quot;off&quot;. They are defined as:
  5064. #
  5065. # * &quot;all&quot;: any locally-created room
  5066. # * &quot;invite&quot;: any room created with the &quot;private_chat&quot; or &quot;trusted_private_chat&quot;
  5067. # room creation presets
  5068. # * &quot;off&quot;: this option will take no effect
  5069. #
  5070. # The default value is &quot;off&quot;.
  5071. #
  5072. # Note that this option will only affect rooms created after it is set. It
  5073. # will also not affect rooms created by other servers.
  5074. #
  5075. #encryption_enabled_by_default_for_room_type: invite
  5076. # Uncomment to allow non-server-admin users to create groups on this server
  5077. #
  5078. #enable_group_creation: true
  5079. # If enabled, non server admins can only create groups with local parts
  5080. # starting with this prefix
  5081. #
  5082. #group_creation_prefix: &quot;unofficial_&quot;
  5083. # User Directory configuration
  5084. #
  5085. user_directory:
  5086. # Defines whether users can search the user directory. If false then
  5087. # empty responses are returned to all queries. Defaults to true.
  5088. #
  5089. # Uncomment to disable the user directory.
  5090. #
  5091. #enabled: false
  5092. # Defines whether to search all users visible to your HS when searching
  5093. # the user directory. If false, search results will only contain users
  5094. # visible in public rooms and users sharing a room with the requester.
  5095. # Defaults to false.
  5096. #
  5097. # NB. If you set this to true, and the last time the user_directory search
  5098. # indexes were (re)built was before Synapse 1.44, you'll have to
  5099. # rebuild the indexes in order to search through all known users.
  5100. # These indexes are built the first time Synapse starts; admins can
  5101. # manually trigger a rebuild following the instructions at
  5102. # https://matrix-org.github.io/synapse/latest/user_directory.html
  5103. #
  5104. # Uncomment to return search results containing all known users, even if that
  5105. # user does not share a room with the requester.
  5106. #
  5107. #search_all_users: true
  5108. # Defines whether to prefer local users in search query results.
  5109. # If True, local users are more likely to appear above remote users
  5110. # when searching the user directory. Defaults to false.
  5111. #
  5112. # Uncomment to prefer local over remote users in user directory search
  5113. # results.
  5114. #
  5115. #prefer_local_users: true
  5116. # User Consent configuration
  5117. #
  5118. # for detailed instructions, see
  5119. # https://matrix-org.github.io/synapse/latest/consent_tracking.html
  5120. #
  5121. # Parts of this section are required if enabling the 'consent' resource under
  5122. # 'listeners', in particular 'template_dir' and 'version'.
  5123. #
  5124. # 'template_dir' gives the location of the templates for the HTML forms.
  5125. # This directory should contain one subdirectory per language (eg, 'en', 'fr'),
  5126. # and each language directory should contain the policy document (named as
  5127. # '&lt;version&gt;.html') and a success page (success.html).
  5128. #
  5129. # 'version' specifies the 'current' version of the policy document. It defines
  5130. # the version to be served by the consent resource if there is no 'v'
  5131. # parameter.
  5132. #
  5133. # 'server_notice_content', if enabled, will send a user a &quot;Server Notice&quot;
  5134. # asking them to consent to the privacy policy. The 'server_notices' section
  5135. # must also be configured for this to work. Notices will *not* be sent to
  5136. # guest users unless 'send_server_notice_to_guests' is set to true.
  5137. #
  5138. # 'block_events_error', if set, will block any attempts to send events
  5139. # until the user consents to the privacy policy. The value of the setting is
  5140. # used as the text of the error.
  5141. #
  5142. # 'require_at_registration', if enabled, will add a step to the registration
  5143. # process, similar to how captcha works. Users will be required to accept the
  5144. # policy before their account is created.
  5145. #
  5146. # 'policy_name' is the display name of the policy users will see when registering
  5147. # for an account. Has no effect unless `require_at_registration` is enabled.
  5148. # Defaults to &quot;Privacy Policy&quot;.
  5149. #
  5150. #user_consent:
  5151. # template_dir: res/templates/privacy
  5152. # version: 1.0
  5153. # server_notice_content:
  5154. # msgtype: m.text
  5155. # body: &gt;-
  5156. # To continue using this homeserver you must review and agree to the
  5157. # terms and conditions at %(consent_uri)s
  5158. # send_server_notice_to_guests: true
  5159. # block_events_error: &gt;-
  5160. # To continue using this homeserver you must review and agree to the
  5161. # terms and conditions at %(consent_uri)s
  5162. # require_at_registration: false
  5163. # policy_name: Privacy Policy
  5164. #
  5165. # Settings for local room and user statistics collection. See
  5166. # https://matrix-org.github.io/synapse/latest/room_and_user_statistics.html.
  5167. #
  5168. stats:
  5169. # Uncomment the following to disable room and user statistics. Note that doing
  5170. # so may cause certain features (such as the room directory) not to work
  5171. # correctly.
  5172. #
  5173. #enabled: false
  5174. # Server Notices room configuration
  5175. #
  5176. # Uncomment this section to enable a room which can be used to send notices
  5177. # from the server to users. It is a special room which cannot be left; notices
  5178. # come from a special &quot;notices&quot; user id.
  5179. #
  5180. # If you uncomment this section, you *must* define the system_mxid_localpart
  5181. # setting, which defines the id of the user which will be used to send the
  5182. # notices.
  5183. #
  5184. # It's also possible to override the room name, the display name of the
  5185. # &quot;notices&quot; user, and the avatar for the user.
  5186. #
  5187. #server_notices:
  5188. # system_mxid_localpart: notices
  5189. # system_mxid_display_name: &quot;Server Notices&quot;
  5190. # system_mxid_avatar_url: &quot;mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ&quot;
  5191. # room_name: &quot;Server Notices&quot;
  5192. # Uncomment to disable searching the public room list. When disabled
  5193. # blocks searching local and remote room lists for local and remote
  5194. # users by always returning an empty list for all queries.
  5195. #
  5196. #enable_room_list_search: false
  5197. # The `alias_creation` option controls who's allowed to create aliases
  5198. # on this server.
  5199. #
  5200. # The format of this option is a list of rules that contain globs that
  5201. # match against user_id, room_id and the new alias (fully qualified with
  5202. # server name). The action in the first rule that matches is taken,
  5203. # which can currently either be &quot;allow&quot; or &quot;deny&quot;.
  5204. #
  5205. # Missing user_id/room_id/alias fields default to &quot;*&quot;.
  5206. #
  5207. # If no rules match the request is denied. An empty list means no one
  5208. # can create aliases.
  5209. #
  5210. # Options for the rules include:
  5211. #
  5212. # user_id: Matches against the creator of the alias
  5213. # alias: Matches against the alias being created
  5214. # room_id: Matches against the room ID the alias is being pointed at
  5215. # action: Whether to &quot;allow&quot; or &quot;deny&quot; the request if the rule matches
  5216. #
  5217. # The default is:
  5218. #
  5219. #alias_creation_rules:
  5220. # - user_id: &quot;*&quot;
  5221. # alias: &quot;*&quot;
  5222. # room_id: &quot;*&quot;
  5223. # action: allow
  5224. # The `room_list_publication_rules` option controls who can publish and
  5225. # which rooms can be published in the public room list.
  5226. #
  5227. # The format of this option is the same as that for
  5228. # `alias_creation_rules`.
  5229. #
  5230. # If the room has one or more aliases associated with it, only one of
  5231. # the aliases needs to match the alias rule. If there are no aliases
  5232. # then only rules with `alias: *` match.
  5233. #
  5234. # If no rules match the request is denied. An empty list means no one
  5235. # can publish rooms.
  5236. #
  5237. # Options for the rules include:
  5238. #
  5239. # user_id: Matches against the creator of the alias
  5240. # room_id: Matches against the room ID being published
  5241. # alias: Matches against any current local or canonical aliases
  5242. # associated with the room
  5243. # action: Whether to &quot;allow&quot; or &quot;deny&quot; the request if the rule matches
  5244. #
  5245. # The default is:
  5246. #
  5247. #room_list_publication_rules:
  5248. # - user_id: &quot;*&quot;
  5249. # alias: &quot;*&quot;
  5250. # room_id: &quot;*&quot;
  5251. # action: allow
  5252. ## Opentracing ##
  5253. # These settings enable opentracing, which implements distributed tracing.
  5254. # This allows you to observe the causal chains of events across servers
  5255. # including requests, key lookups etc., across any server running
  5256. # synapse or any other other services which supports opentracing
  5257. # (specifically those implemented with Jaeger).
  5258. #
  5259. opentracing:
  5260. # tracing is disabled by default. Uncomment the following line to enable it.
  5261. #
  5262. #enabled: true
  5263. # The list of homeservers we wish to send and receive span contexts and span baggage.
  5264. # See https://matrix-org.github.io/synapse/latest/opentracing.html.
  5265. #
  5266. # This is a list of regexes which are matched against the server_name of the
  5267. # homeserver.
  5268. #
  5269. # By default, it is empty, so no servers are matched.
  5270. #
  5271. #homeserver_whitelist:
  5272. # - &quot;.*&quot;
  5273. # A list of the matrix IDs of users whose requests will always be traced,
  5274. # even if the tracing system would otherwise drop the traces due to
  5275. # probabilistic sampling.
  5276. #
  5277. # By default, the list is empty.
  5278. #
  5279. #force_tracing_for_users:
  5280. # - &quot;@user1:server_name&quot;
  5281. # - &quot;@user2:server_name&quot;
  5282. # Jaeger can be configured to sample traces at different rates.
  5283. # All configuration options provided by Jaeger can be set here.
  5284. # Jaeger's configuration is mostly related to trace sampling which
  5285. # is documented here:
  5286. # https://www.jaegertracing.io/docs/latest/sampling/.
  5287. #
  5288. #jaeger_config:
  5289. # sampler:
  5290. # type: const
  5291. # param: 1
  5292. # logging:
  5293. # false
  5294. ## Workers ##
  5295. # Disables sending of outbound federation transactions on the main process.
  5296. # Uncomment if using a federation sender worker.
  5297. #
  5298. #send_federation: false
  5299. # It is possible to run multiple federation sender workers, in which case the
  5300. # work is balanced across them.
  5301. #
  5302. # This configuration must be shared between all federation sender workers, and if
  5303. # changed all federation sender workers must be stopped at the same time and then
  5304. # started, to ensure that all instances are running with the same config (otherwise
  5305. # events may be dropped).
  5306. #
  5307. #federation_sender_instances:
  5308. # - federation_sender1
  5309. # When using workers this should be a map from `worker_name` to the
  5310. # HTTP replication listener of the worker, if configured.
  5311. #
  5312. #instance_map:
  5313. # worker1:
  5314. # host: localhost
  5315. # port: 8034
  5316. # Experimental: When using workers you can define which workers should
  5317. # handle event persistence and typing notifications. Any worker
  5318. # specified here must also be in the `instance_map`.
  5319. #
  5320. #stream_writers:
  5321. # events: worker1
  5322. # typing: worker1
  5323. # The worker that is used to run background tasks (e.g. cleaning up expired
  5324. # data). If not provided this defaults to the main process.
  5325. #
  5326. #run_background_tasks_on: worker1
  5327. # A shared secret used by the replication APIs to authenticate HTTP requests
  5328. # from workers.
  5329. #
  5330. # By default this is unused and traffic is not authenticated.
  5331. #
  5332. #worker_replication_secret: &quot;&quot;
  5333. # Configuration for Redis when using workers. This *must* be enabled when
  5334. # using workers (unless using old style direct TCP configuration).
  5335. #
  5336. redis:
  5337. # Uncomment the below to enable Redis support.
  5338. #
  5339. #enabled: true
  5340. # Optional host and port to use to connect to redis. Defaults to
  5341. # localhost and 6379
  5342. #
  5343. #host: localhost
  5344. #port: 6379
  5345. # Optional password if configured on the Redis instance
  5346. #
  5347. #password: &lt;secret_password&gt;
  5348. </code></pre>
  5349. <div style="break-before: page; page-break-before: always;"></div><h1 id="logging-sample-configuration-file"><a class="header" href="#logging-sample-configuration-file">Logging Sample Configuration File</a></h1>
  5350. <p>Below is a sample logging configuration file. This file can be tweaked to control how your
  5351. homeserver will output logs. A restart of the server is generally required to apply any
  5352. changes made to this file. The value of the <code>log_config</code> option in your homeserver
  5353. config should be the path to this file.</p>
  5354. <p>Note that a default logging configuration (shown below) is created automatically alongside
  5355. the homeserver config when following the <a href="usage/configuration/../../setup/installation.html">installation instructions</a>.
  5356. It should be named <code>&lt;SERVERNAME&gt;.log.config</code> by default.</p>
  5357. <pre><code class="language-yaml"># Log configuration for Synapse.
  5358. #
  5359. # This is a YAML file containing a standard Python logging configuration
  5360. # dictionary. See [1] for details on the valid settings.
  5361. #
  5362. # Synapse also supports structured logging for machine readable logs which can
  5363. # be ingested by ELK stacks. See [2] for details.
  5364. #
  5365. # [1]: https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
  5366. # [2]: https://matrix-org.github.io/synapse/latest/structured_logging.html
  5367. version: 1
  5368. formatters:
  5369. precise:
  5370. format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'
  5371. handlers:
  5372. file:
  5373. class: logging.handlers.TimedRotatingFileHandler
  5374. formatter: precise
  5375. filename: /var/log/matrix-synapse/homeserver.log
  5376. when: midnight
  5377. backupCount: 3 # Does not include the current log file.
  5378. encoding: utf8
  5379. # Default to buffering writes to log file for efficiency.
  5380. # WARNING/ERROR logs will still be flushed immediately, but there will be a
  5381. # delay (of up to `period` seconds, or until the buffer is full with
  5382. # `capacity` messages) before INFO/DEBUG logs get written.
  5383. buffer:
  5384. class: synapse.logging.handlers.PeriodicallyFlushingMemoryHandler
  5385. target: file
  5386. # The capacity is the maximum number of log lines that are buffered
  5387. # before being written to disk. Increasing this will lead to better
  5388. # performance, at the expensive of it taking longer for log lines to
  5389. # be written to disk.
  5390. # This parameter is required.
  5391. capacity: 10
  5392. # Logs with a level at or above the flush level will cause the buffer to
  5393. # be flushed immediately.
  5394. # Default value: 40 (ERROR)
  5395. # Other values: 50 (CRITICAL), 30 (WARNING), 20 (INFO), 10 (DEBUG)
  5396. flushLevel: 30 # Flush immediately for WARNING logs and higher
  5397. # The period of time, in seconds, between forced flushes.
  5398. # Messages will not be delayed for longer than this time.
  5399. # Default value: 5 seconds
  5400. period: 5
  5401. # A handler that writes logs to stderr. Unused by default, but can be used
  5402. # instead of &quot;buffer&quot; and &quot;file&quot; in the logger handlers.
  5403. console:
  5404. class: logging.StreamHandler
  5405. formatter: precise
  5406. loggers:
  5407. synapse.storage.SQL:
  5408. # beware: increasing this to DEBUG will make synapse log sensitive
  5409. # information such as access tokens.
  5410. level: INFO
  5411. twisted:
  5412. # We send the twisted logging directly to the file handler,
  5413. # to work around https://github.com/matrix-org/synapse/issues/3471
  5414. # when using &quot;buffer&quot; logger. Use &quot;console&quot; to log to stderr instead.
  5415. handlers: [file]
  5416. propagate: false
  5417. root:
  5418. level: INFO
  5419. # Write logs to the `buffer` handler, which will buffer them together in memory,
  5420. # then write them to a file.
  5421. #
  5422. # Replace &quot;buffer&quot; with &quot;console&quot; to log to stderr instead. (Note that you'll
  5423. # also need to update the configuration for the `twisted` logger above, in
  5424. # this case.)
  5425. #
  5426. handlers: [buffer]
  5427. disable_existing_loggers: false
  5428. </code></pre>
  5429. <div style="break-before: page; page-break-before: always;"></div><h1 id="structured-logging"><a class="header" href="#structured-logging">Structured Logging</a></h1>
  5430. <p>A structured logging system can be useful when your logs are destined for a
  5431. machine to parse and process. By maintaining its machine-readable characteristics,
  5432. it enables more efficient searching and aggregations when consumed by software
  5433. such as the &quot;ELK stack&quot;.</p>
  5434. <p>Synapse's structured logging system is configured via the file that Synapse's
  5435. <code>log_config</code> config option points to. The file should include a formatter which
  5436. uses the <code>synapse.logging.TerseJsonFormatter</code> class included with Synapse and a
  5437. handler which uses the above formatter.</p>
  5438. <p>There is also a <code>synapse.logging.JsonFormatter</code> option which does not include
  5439. a timestamp in the resulting JSON. This is useful if the log ingester adds its
  5440. own timestamp.</p>
  5441. <p>A structured logging configuration looks similar to the following:</p>
  5442. <pre><code class="language-yaml">version: 1
  5443. formatters:
  5444. structured:
  5445. class: synapse.logging.TerseJsonFormatter
  5446. handlers:
  5447. file:
  5448. class: logging.handlers.TimedRotatingFileHandler
  5449. formatter: structured
  5450. filename: /path/to/my/logs/homeserver.log
  5451. when: midnight
  5452. backupCount: 3 # Does not include the current log file.
  5453. encoding: utf8
  5454. loggers:
  5455. synapse:
  5456. level: INFO
  5457. handlers: [remote]
  5458. synapse.storage.SQL:
  5459. level: WARNING
  5460. </code></pre>
  5461. <p>The above logging config will set Synapse as 'INFO' logging level by default,
  5462. with the SQL layer at 'WARNING', and will log to a file, stored as JSON.</p>
  5463. <p>It is also possible to figure Synapse to log to a remote endpoint by using the
  5464. <code>synapse.logging.RemoteHandler</code> class included with Synapse. It takes the
  5465. following arguments:</p>
  5466. <ul>
  5467. <li><code>host</code>: Hostname or IP address of the log aggregator.</li>
  5468. <li><code>port</code>: Numerical port to contact on the host.</li>
  5469. <li><code>maximum_buffer</code>: (Optional, defaults to 1000) The maximum buffer size to allow.</li>
  5470. </ul>
  5471. <p>A remote structured logging configuration looks similar to the following:</p>
  5472. <pre><code class="language-yaml">version: 1
  5473. formatters:
  5474. structured:
  5475. class: synapse.logging.TerseJsonFormatter
  5476. handlers:
  5477. remote:
  5478. class: synapse.logging.RemoteHandler
  5479. formatter: structured
  5480. host: 10.1.2.3
  5481. port: 9999
  5482. loggers:
  5483. synapse:
  5484. level: INFO
  5485. handlers: [remote]
  5486. synapse.storage.SQL:
  5487. level: WARNING
  5488. </code></pre>
  5489. <p>The above logging config will set Synapse as 'INFO' logging level by default,
  5490. with the SQL layer at 'WARNING', and will log JSON formatted messages to a
  5491. remote endpoint at 10.1.2.3:9999.</p>
  5492. <h2 id="upgrading-from-legacy-structured-logging-configuration"><a class="header" href="#upgrading-from-legacy-structured-logging-configuration">Upgrading from legacy structured logging configuration</a></h2>
  5493. <p>Versions of Synapse prior to v1.23.0 included a custom structured logging
  5494. configuration which is deprecated. It used a <code>structured: true</code> flag and
  5495. configured <code>drains</code> instead of <code>handlers</code> and <code>formatters</code>.</p>
  5496. <p>Synapse currently automatically converts the old configuration to the new
  5497. configuration, but this will be removed in a future version of Synapse. The
  5498. following reference can be used to update your configuration. Based on the drain
  5499. <code>type</code>, we can pick a new handler:</p>
  5500. <ol>
  5501. <li>For a type of <code>console</code>, <code>console_json</code>, or <code>console_json_terse</code>: a handler
  5502. with a class of <code>logging.StreamHandler</code> and a <code>stream</code> of <code>ext://sys.stdout</code>
  5503. or <code>ext://sys.stderr</code> should be used.</li>
  5504. <li>For a type of <code>file</code> or <code>file_json</code>: a handler of <code>logging.FileHandler</code> with
  5505. a location of the file path should be used.</li>
  5506. <li>For a type of <code>network_json_terse</code>: a handler of <code>synapse.logging.RemoteHandler</code>
  5507. with the host and port should be used.</li>
  5508. </ol>
  5509. <p>Then based on the drain <code>type</code> we can pick a new formatter:</p>
  5510. <ol>
  5511. <li>For a type of <code>console</code> or <code>file</code> no formatter is necessary.</li>
  5512. <li>For a type of <code>console_json</code> or <code>file_json</code>: a formatter of
  5513. <code>synapse.logging.JsonFormatter</code> should be used.</li>
  5514. <li>For a type of <code>console_json_terse</code> or <code>network_json_terse</code>: a formatter of
  5515. <code>synapse.logging.TerseJsonFormatter</code> should be used.</li>
  5516. </ol>
  5517. <p>For each new handler and formatter they should be added to the logging configuration
  5518. and then assigned to either a logger or the root logger.</p>
  5519. <p>An example legacy configuration:</p>
  5520. <pre><code class="language-yaml">structured: true
  5521. loggers:
  5522. synapse:
  5523. level: INFO
  5524. synapse.storage.SQL:
  5525. level: WARNING
  5526. drains:
  5527. console:
  5528. type: console
  5529. location: stdout
  5530. file:
  5531. type: file_json
  5532. location: homeserver.log
  5533. </code></pre>
  5534. <p>Would be converted into a new configuration:</p>
  5535. <pre><code class="language-yaml">version: 1
  5536. formatters:
  5537. json:
  5538. class: synapse.logging.JsonFormatter
  5539. handlers:
  5540. console:
  5541. class: logging.StreamHandler
  5542. location: ext://sys.stdout
  5543. file:
  5544. class: logging.FileHandler
  5545. formatter: json
  5546. filename: homeserver.log
  5547. loggers:
  5548. synapse:
  5549. level: INFO
  5550. handlers: [console, file]
  5551. synapse.storage.SQL:
  5552. level: WARNING
  5553. </code></pre>
  5554. <p>The new logging configuration is a bit more verbose, but significantly more
  5555. flexible. It allows for configuration that were not previously possible, such as
  5556. sending plain logs over the network, or using different handlers for different
  5557. modules.</p>
  5558. <div style="break-before: page; page-break-before: always;"></div><h1 id="templates"><a class="header" href="#templates">Templates</a></h1>
  5559. <p>Synapse uses parametrised templates to generate the content of emails it sends and
  5560. webpages it shows to users.</p>
  5561. <p>By default, Synapse will use the templates listed <a href="https://github.com/matrix-org/synapse/tree/master/synapse/res/templates">here</a>.
  5562. Server admins can configure an additional directory for Synapse to look for templates
  5563. in, allowing them to specify custom templates:</p>
  5564. <pre><code class="language-yaml">templates:
  5565. custom_templates_directory: /path/to/custom/templates/
  5566. </code></pre>
  5567. <p>If this setting is not set, or the files named below are not found within the directory,
  5568. default templates from within the Synapse package will be used.</p>
  5569. <p>Templates that are given variables when being rendered are rendered using <a href="https://jinja.palletsprojects.com/en/2.11.x/">Jinja 2</a>.
  5570. Templates rendered by Jinja 2 can also access two functions on top of the functions
  5571. already available as part of Jinja 2:</p>
  5572. <pre><code class="language-python">format_ts(value: int, format: str) -&gt; str
  5573. </code></pre>
  5574. <p>Formats a timestamp in milliseconds.</p>
  5575. <p>Example: <code>reason.last_sent_ts|format_ts(&quot;%c&quot;)</code></p>
  5576. <pre><code class="language-python">mxc_to_http(value: str, width: int, height: int, resize_method: str = &quot;crop&quot;) -&gt; str
  5577. </code></pre>
  5578. <p>Turns a <code>mxc://</code> URL for media content into an HTTP(S) one using the homeserver's
  5579. <code>public_baseurl</code> configuration setting as the URL's base.</p>
  5580. <p>Example: <code>message.sender_avatar_url|mxc_to_http(32,32)</code></p>
  5581. <h2 id="email-templates"><a class="header" href="#email-templates">Email templates</a></h2>
  5582. <p>Below are the templates Synapse will look for when generating the content of an email:</p>
  5583. <ul>
  5584. <li><code>notif_mail.html</code> and <code>notif_mail.txt</code>: The contents of email notifications of missed
  5585. events.
  5586. When rendering, this template is given the following variables:
  5587. <ul>
  5588. <li><code>user_display_name</code>: the display name for the user receiving the notification</li>
  5589. <li><code>unsubscribe_link</code>: the link users can click to unsubscribe from email notifications</li>
  5590. <li><code>summary_text</code>: a summary of the notification(s). The text used can be customised
  5591. by configuring the various settings in the <code>email.subjects</code> section of the
  5592. configuration file.</li>
  5593. <li><code>rooms</code>: a list of rooms containing events to include in the email. Each element is
  5594. an object with the following attributes:
  5595. <ul>
  5596. <li><code>title</code>: a human-readable name for the room</li>
  5597. <li><code>hash</code>: a hash of the ID of the room</li>
  5598. <li><code>invite</code>: a boolean, which is <code>True</code> if the room is an invite the user hasn't
  5599. accepted yet, <code>False</code> otherwise</li>
  5600. <li><code>notifs</code>: a list of events, or an empty list if <code>invite</code> is <code>True</code>. Each element
  5601. is an object with the following attributes:
  5602. <ul>
  5603. <li><code>link</code>: a <code>matrix.to</code> link to the event</li>
  5604. <li><code>ts</code>: the time in milliseconds at which the event was received</li>
  5605. <li><code>messages</code>: a list of messages containing one message before the event, the
  5606. message in the event, and one message after the event. Each element is an
  5607. object with the following attributes:
  5608. <ul>
  5609. <li><code>event_type</code>: the type of the event</li>
  5610. <li><code>is_historical</code>: a boolean, which is <code>False</code> if the message is the one
  5611. that triggered the notification, <code>True</code> otherwise</li>
  5612. <li><code>id</code>: the ID of the event</li>
  5613. <li><code>ts</code>: the time in milliseconds at which the event was sent</li>
  5614. <li><code>sender_name</code>: the display name for the event's sender</li>
  5615. <li><code>sender_avatar_url</code>: the avatar URL (as a <code>mxc://</code> URL) for the event's
  5616. sender</li>
  5617. <li><code>sender_hash</code>: a hash of the user ID of the sender</li>
  5618. </ul>
  5619. </li>
  5620. </ul>
  5621. </li>
  5622. <li><code>link</code>: a <code>matrix.to</code> link to the room</li>
  5623. </ul>
  5624. </li>
  5625. <li><code>reason</code>: information on the event that triggered the email to be sent. It's an
  5626. object with the following attributes:
  5627. <ul>
  5628. <li><code>room_id</code>: the ID of the room the event was sent in</li>
  5629. <li><code>room_name</code>: a human-readable name for the room the event was sent in</li>
  5630. <li><code>now</code>: the current time in milliseconds</li>
  5631. <li><code>received_at</code>: the time in milliseconds at which the event was received</li>
  5632. <li><code>delay_before_mail_ms</code>: the amount of time in milliseconds Synapse always waits
  5633. before ever emailing about a notification (to give the user a chance to respond
  5634. to other push or notice the window)</li>
  5635. <li><code>last_sent_ts</code>: the time in milliseconds at which a notification was last sent
  5636. for an event in this room</li>
  5637. <li><code>throttle_ms</code>: the minimum amount of time in milliseconds between two
  5638. notifications can be sent for this room</li>
  5639. </ul>
  5640. </li>
  5641. </ul>
  5642. </li>
  5643. <li><code>password_reset.html</code> and <code>password_reset.txt</code>: The contents of password reset emails
  5644. sent by the homeserver.
  5645. When rendering, these templates are given a <code>link</code> variable which contains the link the
  5646. user must click in order to reset their password.</li>
  5647. <li><code>registration.html</code> and <code>registration.txt</code>: The contents of address verification emails
  5648. sent during registration.
  5649. When rendering, these templates are given a <code>link</code> variable which contains the link the
  5650. user must click in order to validate their email address.</li>
  5651. <li><code>add_threepid.html</code> and <code>add_threepid.txt</code>: The contents of address verification emails
  5652. sent when an address is added to a Matrix account.
  5653. When rendering, these templates are given a <code>link</code> variable which contains the link the
  5654. user must click in order to validate their email address.</li>
  5655. </ul>
  5656. <h2 id="html-page-templates-for-registration-and-password-reset"><a class="header" href="#html-page-templates-for-registration-and-password-reset">HTML page templates for registration and password reset</a></h2>
  5657. <p>Below are the templates Synapse will look for when generating pages related to
  5658. registration and password reset:</p>
  5659. <ul>
  5660. <li><code>password_reset_confirmation.html</code>: An HTML page that a user will see when they follow
  5661. the link in the password reset email. The user will be asked to confirm the action
  5662. before their password is reset.
  5663. When rendering, this template is given the following variables:
  5664. <ul>
  5665. <li><code>sid</code>: the session ID for the password reset</li>
  5666. <li><code>token</code>: the token for the password reset</li>
  5667. <li><code>client_secret</code>: the client secret for the password reset</li>
  5668. </ul>
  5669. </li>
  5670. <li><code>password_reset_success.html</code> and <code>password_reset_failure.html</code>: HTML pages for success
  5671. and failure that a user will see when they confirm the password reset flow using the
  5672. page above.
  5673. When rendering, <code>password_reset_success.html</code> is given no variable, and
  5674. <code>password_reset_failure.html</code> is given a <code>failure_reason</code>, which contains the reason
  5675. for the password reset failure. </li>
  5676. <li><code>registration_success.html</code> and <code>registration_failure.html</code>: HTML pages for success and
  5677. failure that a user will see when they follow the link in an address verification email
  5678. sent during registration.
  5679. When rendering, <code>registration_success.html</code> is given no variable, and
  5680. <code>registration_failure.html</code> is given a <code>failure_reason</code>, which contains the reason
  5681. for the registration failure.</li>
  5682. <li><code>add_threepid_success.html</code> and <code>add_threepid_failure.html</code>: HTML pages for success and
  5683. failure that a user will see when they follow the link in an address verification email
  5684. sent when an address is added to a Matrix account.
  5685. When rendering, <code>add_threepid_success.html</code> is given no variable, and
  5686. <code>add_threepid_failure.html</code> is given a <code>failure_reason</code>, which contains the reason
  5687. for the registration failure.</li>
  5688. </ul>
  5689. <h2 id="html-page-templates-for-single-sign-on-sso"><a class="header" href="#html-page-templates-for-single-sign-on-sso">HTML page templates for Single Sign-On (SSO)</a></h2>
  5690. <p>Below are the templates Synapse will look for when generating pages related to SSO:</p>
  5691. <ul>
  5692. <li><code>sso_login_idp_picker.html</code>: HTML page to prompt the user to choose an
  5693. Identity Provider during login.
  5694. This is only used if multiple SSO Identity Providers are configured.
  5695. When rendering, this template is given the following variables:
  5696. <ul>
  5697. <li><code>redirect_url</code>: the URL that the user will be redirected to after
  5698. login.</li>
  5699. <li><code>server_name</code>: the homeserver's name.</li>
  5700. <li><code>providers</code>: a list of available Identity Providers. Each element is
  5701. an object with the following attributes:
  5702. <ul>
  5703. <li><code>idp_id</code>: unique identifier for the IdP</li>
  5704. <li><code>idp_name</code>: user-facing name for the IdP</li>
  5705. <li><code>idp_icon</code>: if specified in the IdP config, an MXC URI for an icon
  5706. for the IdP</li>
  5707. <li><code>idp_brand</code>: if specified in the IdP config, a textual identifier
  5708. for the brand of the IdP
  5709. The rendered HTML page should contain a form which submits its results
  5710. back as a GET request, with the following query parameters:</li>
  5711. </ul>
  5712. </li>
  5713. <li><code>redirectUrl</code>: the client redirect URI (ie, the <code>redirect_url</code> passed
  5714. to the template)</li>
  5715. <li><code>idp</code>: the 'idp_id' of the chosen IDP.</li>
  5716. </ul>
  5717. </li>
  5718. <li><code>sso_auth_account_details.html</code>: HTML page to prompt new users to enter a
  5719. userid and confirm other details. This is only shown if the
  5720. SSO implementation (with any <code>user_mapping_provider</code>) does not return
  5721. a localpart.
  5722. When rendering, this template is given the following variables:
  5723. <ul>
  5724. <li><code>server_name</code>: the homeserver's name.</li>
  5725. <li><code>idp</code>: details of the SSO Identity Provider that the user logged in
  5726. with: an object with the following attributes:
  5727. <ul>
  5728. <li><code>idp_id</code>: unique identifier for the IdP</li>
  5729. <li><code>idp_name</code>: user-facing name for the IdP</li>
  5730. <li><code>idp_icon</code>: if specified in the IdP config, an MXC URI for an icon
  5731. for the IdP</li>
  5732. <li><code>idp_brand</code>: if specified in the IdP config, a textual identifier
  5733. for the brand of the IdP</li>
  5734. </ul>
  5735. </li>
  5736. <li><code>user_attributes</code>: an object containing details about the user that
  5737. we received from the IdP. May have the following attributes:
  5738. <ul>
  5739. <li>display_name: the user's display_name</li>
  5740. <li>emails: a list of email addresses
  5741. The template should render a form which submits the following fields:</li>
  5742. </ul>
  5743. </li>
  5744. <li><code>username</code>: the localpart of the user's chosen user id</li>
  5745. </ul>
  5746. </li>
  5747. <li><code>sso_new_user_consent.html</code>: HTML page allowing the user to consent to the
  5748. server's terms and conditions. This is only shown for new users, and only if
  5749. <code>user_consent.require_at_registration</code> is set.
  5750. When rendering, this template is given the following variables:
  5751. <ul>
  5752. <li><code>server_name</code>: the homeserver's name.</li>
  5753. <li><code>user_id</code>: the user's matrix proposed ID.</li>
  5754. <li><code>user_profile.display_name</code>: the user's proposed display name, if any.</li>
  5755. <li>consent_version: the version of the terms that the user will be
  5756. shown</li>
  5757. <li><code>terms_url</code>: a link to the page showing the terms.
  5758. The template should render a form which submits the following fields:</li>
  5759. <li><code>accepted_version</code>: the version of the terms accepted by the user
  5760. (ie, 'consent_version' from the input variables).</li>
  5761. </ul>
  5762. </li>
  5763. <li><code>sso_redirect_confirm.html</code>: HTML page for a confirmation step before redirecting back
  5764. to the client with the login token.
  5765. When rendering, this template is given the following variables:
  5766. <ul>
  5767. <li><code>redirect_url</code>: the URL the user is about to be redirected to.</li>
  5768. <li><code>display_url</code>: the same as <code>redirect_url</code>, but with the query
  5769. parameters stripped. The intention is to have a
  5770. human-readable URL to show to users, not to use it as
  5771. the final address to redirect to.</li>
  5772. <li><code>server_name</code>: the homeserver's name.</li>
  5773. <li><code>new_user</code>: a boolean indicating whether this is the user's first time
  5774. logging in.</li>
  5775. <li><code>user_id</code>: the user's matrix ID.</li>
  5776. <li><code>user_profile.avatar_url</code>: an MXC URI for the user's avatar, if any.
  5777. <code>None</code> if the user has not set an avatar.</li>
  5778. <li><code>user_profile.display_name</code>: the user's display name. <code>None</code> if the user
  5779. has not set a display name.</li>
  5780. </ul>
  5781. </li>
  5782. <li><code>sso_auth_confirm.html</code>: HTML page which notifies the user that they are authenticating
  5783. to confirm an operation on their account during the user interactive authentication
  5784. process.
  5785. When rendering, this template is given the following variables:
  5786. <ul>
  5787. <li><code>redirect_url</code>: the URL the user is about to be redirected to.</li>
  5788. <li><code>description</code>: the operation which the user is being asked to confirm</li>
  5789. <li><code>idp</code>: details of the Identity Provider that we will use to confirm
  5790. the user's identity: an object with the following attributes:
  5791. <ul>
  5792. <li><code>idp_id</code>: unique identifier for the IdP</li>
  5793. <li><code>idp_name</code>: user-facing name for the IdP</li>
  5794. <li><code>idp_icon</code>: if specified in the IdP config, an MXC URI for an icon
  5795. for the IdP</li>
  5796. <li><code>idp_brand</code>: if specified in the IdP config, a textual identifier
  5797. for the brand of the IdP</li>
  5798. </ul>
  5799. </li>
  5800. </ul>
  5801. </li>
  5802. <li><code>sso_auth_success.html</code>: HTML page shown after a successful user interactive
  5803. authentication session.
  5804. Note that this page must include the JavaScript which notifies of a successful
  5805. authentication (see https://matrix.org/docs/spec/client_server/r0.6.0#fallback).
  5806. This template has no additional variables.</li>
  5807. <li><code>sso_auth_bad_user.html</code>: HTML page shown after a user-interactive authentication
  5808. session which does not map correctly onto the expected user.
  5809. When rendering, this template is given the following variables:
  5810. <ul>
  5811. <li><code>server_name</code>: the homeserver's name.</li>
  5812. <li><code>user_id_to_verify</code>: the MXID of the user that we are trying to
  5813. validate.</li>
  5814. </ul>
  5815. </li>
  5816. <li><code>sso_account_deactivated.html</code>: HTML page shown during single sign-on if a deactivated
  5817. user (according to Synapse's database) attempts to login.
  5818. This template has no additional variables.</li>
  5819. <li><code>sso_error.html</code>: HTML page to display to users if something goes wrong during the
  5820. OpenID Connect authentication process.
  5821. When rendering, this template is given two variables:
  5822. <ul>
  5823. <li><code>error</code>: the technical name of the error</li>
  5824. <li><code>error_description</code>: a human-readable message for the error</li>
  5825. </ul>
  5826. </li>
  5827. </ul>
  5828. <div style="break-before: page; page-break-before: always;"></div><h1 id="user-authentication"><a class="header" href="#user-authentication">User Authentication</a></h1>
  5829. <p>Synapse supports multiple methods of authenticating users, either out-of-the-box or through custom pluggable
  5830. authentication modules.</p>
  5831. <p>Included in Synapse is support for authenticating users via:</p>
  5832. <ul>
  5833. <li>A username and password.</li>
  5834. <li>An email address and password.</li>
  5835. <li>Single Sign-On through the SAML, Open ID Connect or CAS protocols.</li>
  5836. <li>JSON Web Tokens.</li>
  5837. <li>An administrator's shared secret.</li>
  5838. </ul>
  5839. <p>Synapse can additionally be extended to support custom authentication schemes through optional &quot;password auth provider&quot;
  5840. modules.</p>
  5841. <div style="break-before: page; page-break-before: always;"></div><h1 id="configuring-synapse-to-authenticate-against-an-openid-connect-provider"><a class="header" href="#configuring-synapse-to-authenticate-against-an-openid-connect-provider">Configuring Synapse to authenticate against an OpenID Connect provider</a></h1>
  5842. <p>Synapse can be configured to use an OpenID Connect Provider (OP) for
  5843. authentication, instead of its own local password database.</p>
  5844. <p>Any OP should work with Synapse, as long as it supports the authorization code
  5845. flow. There are a few options for that:</p>
  5846. <ul>
  5847. <li>
  5848. <p>start a local OP. Synapse has been tested with <a href="https://www.ory.sh/docs/hydra/">Hydra</a> and
  5849. <a href="https://github.com/dexidp/dex">Dex</a>. Note that for an OP to work, it should be served under a
  5850. secure (HTTPS) origin. A certificate signed with a self-signed, locally
  5851. trusted CA should work. In that case, start Synapse with a <code>SSL_CERT_FILE</code>
  5852. environment variable set to the path of the CA.</p>
  5853. </li>
  5854. <li>
  5855. <p>set up a SaaS OP, like <a href="https://developers.google.com/identity/protocols/oauth2/openid-connect">Google</a>, <a href="https://auth0.com/">Auth0</a> or
  5856. <a href="https://www.okta.com/">Okta</a>. Synapse has been tested with Auth0 and Google.</p>
  5857. </li>
  5858. </ul>
  5859. <p>It may also be possible to use other OAuth2 providers which provide the
  5860. <a href="https://tools.ietf.org/html/rfc6749#section-4.1">authorization code grant type</a>,
  5861. such as <a href="https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps">Github</a>.</p>
  5862. <h2 id="preparing-synapse"><a class="header" href="#preparing-synapse">Preparing Synapse</a></h2>
  5863. <p>The OpenID integration in Synapse uses the
  5864. <a href="https://pypi.org/project/Authlib/"><code>authlib</code></a> library, which must be installed
  5865. as follows:</p>
  5866. <ul>
  5867. <li>
  5868. <p>The relevant libraries are included in the Docker images and Debian packages
  5869. provided by <code>matrix.org</code> so no further action is needed.</p>
  5870. </li>
  5871. <li>
  5872. <p>If you installed Synapse into a virtualenv, run <code>/path/to/env/bin/pip install matrix-synapse[oidc]</code> to install the necessary dependencies.</p>
  5873. </li>
  5874. <li>
  5875. <p>For other installation mechanisms, see the documentation provided by the
  5876. maintainer.</p>
  5877. </li>
  5878. </ul>
  5879. <p>To enable the OpenID integration, you should then add a section to the <code>oidc_providers</code>
  5880. setting in your configuration file (or uncomment one of the existing examples).
  5881. See <a href="./sample_config.yaml">sample_config.yaml</a> for some sample settings, as well as
  5882. the text below for example configurations for specific providers.</p>
  5883. <h2 id="sample-configs"><a class="header" href="#sample-configs">Sample configs</a></h2>
  5884. <p>Here are a few configs for providers that should work with Synapse.</p>
  5885. <h3 id="microsoft-azure-active-directory"><a class="header" href="#microsoft-azure-active-directory">Microsoft Azure Active Directory</a></h3>
  5886. <p>Azure AD can act as an OpenID Connect Provider. Register a new application under
  5887. <em>App registrations</em> in the Azure AD management console. The RedirectURI for your
  5888. application should point to your matrix server:
  5889. <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></p>
  5890. <p>Go to <em>Certificates &amp; secrets</em> and register a new client secret. Make note of your
  5891. Directory (tenant) ID as it will be used in the Azure links.
  5892. Edit your Synapse config file and change the <code>oidc_config</code> section:</p>
  5893. <pre><code class="language-yaml">oidc_providers:
  5894. - idp_id: microsoft
  5895. idp_name: Microsoft
  5896. issuer: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/v2.0&quot;
  5897. client_id: &quot;&lt;client id&gt;&quot;
  5898. client_secret: &quot;&lt;client secret&gt;&quot;
  5899. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5900. authorization_endpoint: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/oauth2/v2.0/authorize&quot;
  5901. token_endpoint: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/oauth2/v2.0/token&quot;
  5902. userinfo_endpoint: &quot;https://graph.microsoft.com/oidc/userinfo&quot;
  5903. user_mapping_provider:
  5904. config:
  5905. localpart_template: &quot;{{ user.preferred_username.split('@')[0] }}&quot;
  5906. display_name_template: &quot;{{ user.name }}&quot;
  5907. </code></pre>
  5908. <h3 id="dex"><a class="header" href="#dex">Dex</a></h3>
  5909. <p><a href="https://github.com/dexidp/dex">Dex</a> is a simple, open-source, certified OpenID Connect Provider.
  5910. Although it is designed to help building a full-blown provider with an
  5911. external database, it can be configured with static passwords in a config file.</p>
  5912. <p>Follow the <a href="https://dexidp.io/docs/getting-started/">Getting Started guide</a>
  5913. to install Dex.</p>
  5914. <p>Edit <code>examples/config-dev.yaml</code> config file from the Dex repo to add a client:</p>
  5915. <pre><code class="language-yaml">staticClients:
  5916. - id: synapse
  5917. secret: secret
  5918. redirectURIs:
  5919. - '[synapse public baseurl]/_synapse/client/oidc/callback'
  5920. name: 'Synapse'
  5921. </code></pre>
  5922. <p>Run with <code>dex serve examples/config-dev.yaml</code>.</p>
  5923. <p>Synapse config:</p>
  5924. <pre><code class="language-yaml">oidc_providers:
  5925. - idp_id: dex
  5926. idp_name: &quot;My Dex server&quot;
  5927. skip_verification: true # This is needed as Dex is served on an insecure endpoint
  5928. issuer: &quot;http://127.0.0.1:5556/dex&quot;
  5929. client_id: &quot;synapse&quot;
  5930. client_secret: &quot;secret&quot;
  5931. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5932. user_mapping_provider:
  5933. config:
  5934. localpart_template: &quot;{{ user.name }}&quot;
  5935. display_name_template: &quot;{{ user.name|capitalize }}&quot;
  5936. </code></pre>
  5937. <h3 id="keycloak"><a class="header" href="#keycloak">Keycloak</a></h3>
  5938. <p><a href="https://www.keycloak.org/docs/latest/server_admin/#sso-protocols">Keycloak</a> is an opensource IdP maintained by Red Hat.</p>
  5939. <p>Follow the <a href="https://www.keycloak.org/getting-started">Getting Started Guide</a> to install Keycloak and set up a realm.</p>
  5940. <ol>
  5941. <li>
  5942. <p>Click <code>Clients</code> in the sidebar and click <code>Create</code></p>
  5943. </li>
  5944. <li>
  5945. <p>Fill in the fields as below:</p>
  5946. </li>
  5947. </ol>
  5948. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  5949. <tr><td>Client ID</td><td><code>synapse</code></td></tr>
  5950. <tr><td>Client Protocol</td><td><code>openid-connect</code></td></tr>
  5951. </tbody></table>
  5952. <ol start="3">
  5953. <li>Click <code>Save</code></li>
  5954. <li>Fill in the fields as below:</li>
  5955. </ol>
  5956. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  5957. <tr><td>Client ID</td><td><code>synapse</code></td></tr>
  5958. <tr><td>Enabled</td><td><code>On</code></td></tr>
  5959. <tr><td>Client Protocol</td><td><code>openid-connect</code></td></tr>
  5960. <tr><td>Access Type</td><td><code>confidential</code></td></tr>
  5961. <tr><td>Valid Redirect URIs</td><td><code>[synapse public baseurl]/_synapse/client/oidc/callback</code></td></tr>
  5962. </tbody></table>
  5963. <ol start="5">
  5964. <li>Click <code>Save</code></li>
  5965. <li>On the Credentials tab, update the fields:</li>
  5966. </ol>
  5967. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  5968. <tr><td>Client Authenticator</td><td><code>Client ID and Secret</code></td></tr>
  5969. </tbody></table>
  5970. <ol start="7">
  5971. <li>Click <code>Regenerate Secret</code></li>
  5972. <li>Copy Secret</li>
  5973. </ol>
  5974. <pre><code class="language-yaml">oidc_providers:
  5975. - idp_id: keycloak
  5976. idp_name: &quot;My KeyCloak server&quot;
  5977. issuer: &quot;https://127.0.0.1:8443/auth/realms/{realm_name}&quot;
  5978. client_id: &quot;synapse&quot;
  5979. client_secret: &quot;copy secret generated from above&quot;
  5980. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5981. user_mapping_provider:
  5982. config:
  5983. localpart_template: &quot;{{ user.preferred_username }}&quot;
  5984. display_name_template: &quot;{{ user.name }}&quot;
  5985. </code></pre>
  5986. <h3 id="auth0"><a class="header" href="#auth0">Auth0</a></h3>
  5987. <p><a href="https://auth0.com/">Auth0</a> is a hosted SaaS IdP solution.</p>
  5988. <ol>
  5989. <li>
  5990. <p>Create a regular web application for Synapse</p>
  5991. </li>
  5992. <li>
  5993. <p>Set the Allowed Callback URLs to <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></p>
  5994. </li>
  5995. <li>
  5996. <p>Add a rule to add the <code>preferred_username</code> claim.</p>
  5997. <details>
  5998. <summary>Code sample</summary>
  5999. <pre><code class="language-js">function addPersistenceAttribute(user, context, callback) {
  6000. user.user_metadata = user.user_metadata || {};
  6001. user.user_metadata.preferred_username = user.user_metadata.preferred_username || user.user_id;
  6002. context.idToken.preferred_username = user.user_metadata.preferred_username;
  6003. auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
  6004. .then(function(){
  6005. callback(null, user, context);
  6006. })
  6007. .catch(function(err){
  6008. callback(err);
  6009. });
  6010. }
  6011. </code></pre>
  6012. </li>
  6013. </ol>
  6014. </details>
  6015. <p>Synapse config:</p>
  6016. <pre><code class="language-yaml">oidc_providers:
  6017. - idp_id: auth0
  6018. idp_name: Auth0
  6019. issuer: &quot;https://your-tier.eu.auth0.com/&quot; # TO BE FILLED
  6020. client_id: &quot;your-client-id&quot; # TO BE FILLED
  6021. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  6022. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  6023. user_mapping_provider:
  6024. config:
  6025. localpart_template: &quot;{{ user.preferred_username }}&quot;
  6026. display_name_template: &quot;{{ user.name }}&quot;
  6027. </code></pre>
  6028. <h3 id="authentik"><a class="header" href="#authentik">Authentik</a></h3>
  6029. <p><a href="https://goauthentik.io/">Authentik</a> is an open-source IdP solution.</p>
  6030. <ol>
  6031. <li>Create a provider in Authentik, with type OAuth2/OpenID.</li>
  6032. <li>The parameters are:</li>
  6033. </ol>
  6034. <ul>
  6035. <li>Client Type: Confidential</li>
  6036. <li>JWT Algorithm: RS256</li>
  6037. <li>Scopes: OpenID, Email and Profile</li>
  6038. <li>RSA Key: Select any available key</li>
  6039. <li>Redirect URIs: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  6040. </ul>
  6041. <ol start="3">
  6042. <li>Create an application for synapse in Authentik and link it to the provider.</li>
  6043. <li>Note the slug of your application, Client ID and Client Secret.</li>
  6044. </ol>
  6045. <p>Synapse config:</p>
  6046. <pre><code class="language-yaml">oidc_providers:
  6047. - idp_id: authentik
  6048. idp_name: authentik
  6049. discover: true
  6050. issuer: &quot;https://your.authentik.example.org/application/o/your-app-slug/&quot; # TO BE FILLED: domain and slug
  6051. client_id: &quot;your client id&quot; # TO BE FILLED
  6052. client_secret: &quot;your client secret&quot; # TO BE FILLED
  6053. scopes:
  6054. - &quot;openid&quot;
  6055. - &quot;profile&quot;
  6056. - &quot;email&quot;
  6057. user_mapping_provider:
  6058. config:
  6059. localpart_template: &quot;{{ user.preferred_username }}}&quot;
  6060. display_name_template: &quot;{{ user.preferred_username|capitalize }}&quot; # TO BE FILLED: If your users have names in Authentik and you want those in Synapse, this should be replaced with user.name|capitalize.
  6061. </code></pre>
  6062. <h3 id="lemonldap"><a class="header" href="#lemonldap">LemonLDAP</a></h3>
  6063. <p><a href="https://lemonldap-ng.org/">LemonLDAP::NG</a> is an open-source IdP solution.</p>
  6064. <ol>
  6065. <li>Create an OpenID Connect Relying Parties in LemonLDAP::NG</li>
  6066. <li>The parameters are:</li>
  6067. </ol>
  6068. <ul>
  6069. <li>Client ID under the basic menu of the new Relying Parties (<code>Options &gt; Basic &gt; Client ID</code>)</li>
  6070. <li>Client secret (<code>Options &gt; Basic &gt; Client secret</code>)</li>
  6071. <li>JWT Algorithm: RS256 within the security menu of the new Relying Parties
  6072. (<code>Options &gt; Security &gt; ID Token signature algorithm</code> and <code>Options &gt; Security &gt; Access Token signature algorithm</code>)</li>
  6073. <li>Scopes: OpenID, Email and Profile</li>
  6074. <li>Allowed redirection addresses for login (<code>Options &gt; Basic &gt; Allowed redirection addresses for login</code> ) :
  6075. <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  6076. </ul>
  6077. <p>Synapse config:</p>
  6078. <pre><code class="language-yaml">oidc_providers:
  6079. - idp_id: lemonldap
  6080. idp_name: lemonldap
  6081. discover: true
  6082. issuer: &quot;https://auth.example.org/&quot; # TO BE FILLED: replace with your domain
  6083. client_id: &quot;your client id&quot; # TO BE FILLED
  6084. client_secret: &quot;your client secret&quot; # TO BE FILLED
  6085. scopes:
  6086. - &quot;openid&quot;
  6087. - &quot;profile&quot;
  6088. - &quot;email&quot;
  6089. user_mapping_provider:
  6090. config:
  6091. localpart_template: &quot;{{ user.preferred_username }}}&quot;
  6092. # TO BE FILLED: If your users have names in LemonLDAP::NG and you want those in Synapse, this should be replaced with user.name|capitalize or any valid filter.
  6093. display_name_template: &quot;{{ user.preferred_username|capitalize }}&quot;
  6094. </code></pre>
  6095. <h3 id="github"><a class="header" href="#github">GitHub</a></h3>
  6096. <p><a href="https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps">GitHub</a> is a bit special as it is not an OpenID Connect compliant provider, but
  6097. just a regular OAuth2 provider.</p>
  6098. <p>The <a href="https://developer.github.com/v3/users/#get-the-authenticated-user"><code>/user</code> API endpoint</a>
  6099. can be used to retrieve information on the authenticated user. As the Synapse
  6100. login mechanism needs an attribute to uniquely identify users, and that endpoint
  6101. does not return a <code>sub</code> property, an alternative <code>subject_claim</code> has to be set.</p>
  6102. <ol>
  6103. <li>Create a new OAuth application: https://github.com/settings/applications/new.</li>
  6104. <li>Set the callback URL to <code>[synapse public baseurl]/_synapse/client/oidc/callback</code>.</li>
  6105. </ol>
  6106. <p>Synapse config:</p>
  6107. <pre><code class="language-yaml">oidc_providers:
  6108. - idp_id: github
  6109. idp_name: Github
  6110. idp_brand: &quot;github&quot; # optional: styling hint for clients
  6111. discover: false
  6112. issuer: &quot;https://github.com/&quot;
  6113. client_id: &quot;your-client-id&quot; # TO BE FILLED
  6114. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  6115. authorization_endpoint: &quot;https://github.com/login/oauth/authorize&quot;
  6116. token_endpoint: &quot;https://github.com/login/oauth/access_token&quot;
  6117. userinfo_endpoint: &quot;https://api.github.com/user&quot;
  6118. scopes: [&quot;read:user&quot;]
  6119. user_mapping_provider:
  6120. config:
  6121. subject_claim: &quot;id&quot;
  6122. localpart_template: &quot;{{ user.login }}&quot;
  6123. display_name_template: &quot;{{ user.name }}&quot;
  6124. </code></pre>
  6125. <h3 id="google"><a class="header" href="#google">Google</a></h3>
  6126. <p><a href="https://developers.google.com/identity/protocols/oauth2/openid-connect">Google</a> is an OpenID certified authentication and authorisation provider.</p>
  6127. <ol>
  6128. <li>Set up a project in the Google API Console (see
  6129. https://developers.google.com/identity/protocols/oauth2/openid-connect#appsetup).</li>
  6130. <li>Add an &quot;OAuth Client ID&quot; for a Web Application under &quot;Credentials&quot;.</li>
  6131. <li>Copy the Client ID and Client Secret, and add the following to your synapse config:
  6132. <pre><code class="language-yaml">oidc_providers:
  6133. - idp_id: google
  6134. idp_name: Google
  6135. idp_brand: &quot;google&quot; # optional: styling hint for clients
  6136. issuer: &quot;https://accounts.google.com/&quot;
  6137. client_id: &quot;your-client-id&quot; # TO BE FILLED
  6138. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  6139. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  6140. user_mapping_provider:
  6141. config:
  6142. localpart_template: &quot;{{ user.given_name|lower }}&quot;
  6143. display_name_template: &quot;{{ user.name }}&quot;
  6144. </code></pre>
  6145. </li>
  6146. <li>Back in the Google console, add this Authorized redirect URI: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code>.</li>
  6147. </ol>
  6148. <h3 id="twitch"><a class="header" href="#twitch">Twitch</a></h3>
  6149. <ol>
  6150. <li>Setup a developer account on <a href="https://dev.twitch.tv/">Twitch</a></li>
  6151. <li>Obtain the OAuth 2.0 credentials by <a href="https://dev.twitch.tv/console/apps/">creating an app</a></li>
  6152. <li>Add this OAuth Redirect URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  6153. </ol>
  6154. <p>Synapse config:</p>
  6155. <pre><code class="language-yaml">oidc_providers:
  6156. - idp_id: twitch
  6157. idp_name: Twitch
  6158. issuer: &quot;https://id.twitch.tv/oauth2/&quot;
  6159. client_id: &quot;your-client-id&quot; # TO BE FILLED
  6160. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  6161. client_auth_method: &quot;client_secret_post&quot;
  6162. user_mapping_provider:
  6163. config:
  6164. localpart_template: &quot;{{ user.preferred_username }}&quot;
  6165. display_name_template: &quot;{{ user.name }}&quot;
  6166. </code></pre>
  6167. <h3 id="gitlab"><a class="header" href="#gitlab">GitLab</a></h3>
  6168. <ol>
  6169. <li>Create a <a href="https://gitlab.com/profile/applications">new application</a>.</li>
  6170. <li>Add the <code>read_user</code> and <code>openid</code> scopes.</li>
  6171. <li>Add this Callback URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  6172. </ol>
  6173. <p>Synapse config:</p>
  6174. <pre><code class="language-yaml">oidc_providers:
  6175. - idp_id: gitlab
  6176. idp_name: Gitlab
  6177. idp_brand: &quot;gitlab&quot; # optional: styling hint for clients
  6178. issuer: &quot;https://gitlab.com/&quot;
  6179. client_id: &quot;your-client-id&quot; # TO BE FILLED
  6180. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  6181. client_auth_method: &quot;client_secret_post&quot;
  6182. scopes: [&quot;openid&quot;, &quot;read_user&quot;]
  6183. user_profile_method: &quot;userinfo_endpoint&quot;
  6184. user_mapping_provider:
  6185. config:
  6186. localpart_template: '{{ user.nickname }}'
  6187. display_name_template: '{{ user.name }}'
  6188. </code></pre>
  6189. <h3 id="facebook"><a class="header" href="#facebook">Facebook</a></h3>
  6190. <p>Like Github, Facebook provide a custom OAuth2 API rather than an OIDC-compliant
  6191. one so requires a little more configuration.</p>
  6192. <ol start="0">
  6193. <li>You will need a Facebook developer account. You can register for one
  6194. <a href="https://developers.facebook.com/async/registration/">here</a>.</li>
  6195. <li>On the <a href="https://developers.facebook.com/apps/">apps</a> page of the developer
  6196. console, &quot;Create App&quot;, and choose &quot;Build Connected Experiences&quot;.</li>
  6197. <li>Once the app is created, add &quot;Facebook Login&quot; and choose &quot;Web&quot;. You don't
  6198. need to go through the whole form here.</li>
  6199. <li>In the left-hand menu, open &quot;Products&quot;/&quot;Facebook Login&quot;/&quot;Settings&quot;.
  6200. <ul>
  6201. <li>Add <code>[synapse public baseurl]/_synapse/client/oidc/callback</code> as an OAuth Redirect
  6202. URL.</li>
  6203. </ul>
  6204. </li>
  6205. <li>In the left-hand menu, open &quot;Settings/Basic&quot;. Here you can copy the &quot;App ID&quot;
  6206. and &quot;App Secret&quot; for use below.</li>
  6207. </ol>
  6208. <p>Synapse config:</p>
  6209. <pre><code class="language-yaml"> - idp_id: facebook
  6210. idp_name: Facebook
  6211. idp_brand: &quot;facebook&quot; # optional: styling hint for clients
  6212. discover: false
  6213. issuer: &quot;https://facebook.com&quot;
  6214. client_id: &quot;your-client-id&quot; # TO BE FILLED
  6215. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  6216. scopes: [&quot;openid&quot;, &quot;email&quot;]
  6217. authorization_endpoint: https://facebook.com/dialog/oauth
  6218. token_endpoint: https://graph.facebook.com/v9.0/oauth/access_token
  6219. user_profile_method: &quot;userinfo_endpoint&quot;
  6220. userinfo_endpoint: &quot;https://graph.facebook.com/v9.0/me?fields=id,name,email,picture&quot;
  6221. user_mapping_provider:
  6222. config:
  6223. subject_claim: &quot;id&quot;
  6224. display_name_template: &quot;{{ user.name }}&quot;
  6225. </code></pre>
  6226. <p>Relevant documents:</p>
  6227. <ul>
  6228. <li>https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow</li>
  6229. <li>Using Facebook's Graph API: https://developers.facebook.com/docs/graph-api/using-graph-api/</li>
  6230. <li>Reference to the User endpoint: https://developers.facebook.com/docs/graph-api/reference/user</li>
  6231. </ul>
  6232. <h3 id="gitea"><a class="header" href="#gitea">Gitea</a></h3>
  6233. <p>Gitea is, like Github, not an OpenID provider, but just an OAuth2 provider.</p>
  6234. <p>The <a href="https://try.gitea.io/api/swagger#/user/userGetCurrent"><code>/user</code> API endpoint</a>
  6235. can be used to retrieve information on the authenticated user. As the Synapse
  6236. login mechanism needs an attribute to uniquely identify users, and that endpoint
  6237. does not return a <code>sub</code> property, an alternative <code>subject_claim</code> has to be set.</p>
  6238. <ol>
  6239. <li>Create a new application.</li>
  6240. <li>Add this Callback URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  6241. </ol>
  6242. <p>Synapse config:</p>
  6243. <pre><code class="language-yaml">oidc_providers:
  6244. - idp_id: gitea
  6245. idp_name: Gitea
  6246. discover: false
  6247. issuer: &quot;https://your-gitea.com/&quot;
  6248. client_id: &quot;your-client-id&quot; # TO BE FILLED
  6249. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  6250. client_auth_method: client_secret_post
  6251. scopes: [] # Gitea doesn't support Scopes
  6252. authorization_endpoint: &quot;https://your-gitea.com/login/oauth/authorize&quot;
  6253. token_endpoint: &quot;https://your-gitea.com/login/oauth/access_token&quot;
  6254. userinfo_endpoint: &quot;https://your-gitea.com/api/v1/user&quot;
  6255. user_mapping_provider:
  6256. config:
  6257. subject_claim: &quot;id&quot;
  6258. localpart_template: &quot;{{ user.login }}&quot;
  6259. display_name_template: &quot;{{ user.full_name }}&quot;
  6260. </code></pre>
  6261. <h3 id="xwiki"><a class="header" href="#xwiki">XWiki</a></h3>
  6262. <p>Install <a href="https://extensions.xwiki.org/xwiki/bin/view/Extension/OpenID%20Connect/OpenID%20Connect%20Provider/">OpenID Connect Provider</a> extension in your <a href="https://www.xwiki.org">XWiki</a> instance.</p>
  6263. <p>Synapse config:</p>
  6264. <pre><code class="language-yaml">oidc_providers:
  6265. - idp_id: xwiki
  6266. idp_name: &quot;XWiki&quot;
  6267. issuer: &quot;https://myxwikihost/xwiki/oidc/&quot;
  6268. client_id: &quot;your-client-id&quot; # TO BE FILLED
  6269. client_auth_method: none
  6270. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  6271. user_profile_method: &quot;userinfo_endpoint&quot;
  6272. user_mapping_provider:
  6273. config:
  6274. localpart_template: &quot;{{ user.preferred_username }}&quot;
  6275. display_name_template: &quot;{{ user.name }}&quot;
  6276. </code></pre>
  6277. <h3 id="apple"><a class="header" href="#apple">Apple</a></h3>
  6278. <p>Configuring &quot;Sign in with Apple&quot; (SiWA) requires an Apple Developer account.</p>
  6279. <p>You will need to create a new &quot;Services ID&quot; for SiWA, and create and download a
  6280. private key with &quot;SiWA&quot; enabled.</p>
  6281. <p>As well as the private key file, you will need:</p>
  6282. <ul>
  6283. <li>Client ID: the &quot;identifier&quot; you gave the &quot;Services ID&quot;</li>
  6284. <li>Team ID: a 10-character ID associated with your developer account.</li>
  6285. <li>Key ID: the 10-character identifier for the key.</li>
  6286. </ul>
  6287. <p>https://help.apple.com/developer-account/?lang=en#/dev77c875b7e has more
  6288. documentation on setting up SiWA.</p>
  6289. <p>The synapse config will look like this:</p>
  6290. <pre><code class="language-yaml"> - idp_id: apple
  6291. idp_name: Apple
  6292. issuer: &quot;https://appleid.apple.com&quot;
  6293. client_id: &quot;your-client-id&quot; # Set to the &quot;identifier&quot; for your &quot;ServicesID&quot;
  6294. client_auth_method: &quot;client_secret_post&quot;
  6295. client_secret_jwt_key:
  6296. key_file: &quot;/path/to/AuthKey_KEYIDCODE.p8&quot; # point to your key file
  6297. jwt_header:
  6298. alg: ES256
  6299. kid: &quot;KEYIDCODE&quot; # Set to the 10-char Key ID
  6300. jwt_payload:
  6301. iss: TEAMIDCODE # Set to the 10-char Team ID
  6302. scopes: [&quot;name&quot;, &quot;email&quot;, &quot;openid&quot;]
  6303. authorization_endpoint: https://appleid.apple.com/auth/authorize?response_mode=form_post
  6304. user_mapping_provider:
  6305. config:
  6306. email_template: &quot;{{ user.email }}&quot;
  6307. </code></pre>
  6308. <h2 id="django-oauth-toolkit"><a class="header" href="#django-oauth-toolkit">Django OAuth Toolkit</a></h2>
  6309. <p><a href="https://github.com/jazzband/django-oauth-toolkit">django-oauth-toolkit</a> is a
  6310. Django application providing out of the box all the endpoints, data and logic
  6311. needed to add OAuth2 capabilities to your Django projects. It supports
  6312. <a href="https://django-oauth-toolkit.readthedocs.io/en/latest/oidc.html">OpenID Connect too</a>.</p>
  6313. <p>Configuration on Django's side:</p>
  6314. <ol>
  6315. <li>Add an application: https://example.com/admin/oauth2_provider/application/add/ and choose parameters like this:</li>
  6316. </ol>
  6317. <ul>
  6318. <li><code>Redirect uris</code>: https://synapse.example.com/_synapse/client/oidc/callback</li>
  6319. <li><code>Client type</code>: <code>Confidential</code></li>
  6320. <li><code>Authorization grant type</code>: <code>Authorization code</code></li>
  6321. <li><code>Algorithm</code>: <code>HMAC with SHA-2 256</code></li>
  6322. </ul>
  6323. <ol start="2">
  6324. <li>
  6325. <p>You can <a href="https://django-oauth-toolkit.readthedocs.io/en/latest/oidc.html#customizing-the-oidc-responses">customize the claims</a> Django gives to synapse (optional):</p>
  6326. <details>
  6327. <summary>Code sample</summary>
  6328. <pre><code class="language-python">class CustomOAuth2Validator(OAuth2Validator):
  6329. def get_additional_claims(self, request):
  6330. return {
  6331. &quot;sub&quot;: request.user.email,
  6332. &quot;email&quot;: request.user.email,
  6333. &quot;first_name&quot;: request.user.first_name,
  6334. &quot;last_name&quot;: request.user.last_name,
  6335. }
  6336. </code></pre>
  6337. </details>
  6338. </li>
  6339. </ol>
  6340. <p>Your synapse config is then:</p>
  6341. <pre><code class="language-yaml">oidc_providers:
  6342. - idp_id: django_example
  6343. idp_name: &quot;Django Example&quot;
  6344. issuer: &quot;https://example.com/o/&quot;
  6345. client_id: &quot;your-client-id&quot; # CHANGE ME
  6346. client_secret: &quot;your-client-secret&quot; # CHANGE ME
  6347. scopes: [&quot;openid&quot;]
  6348. user_profile_method: &quot;userinfo_endpoint&quot; # needed because oauth-toolkit does not include user information in the authorization response
  6349. user_mapping_provider:
  6350. config:
  6351. localpart_template: &quot;{{ user.email.split('@')[0] }}&quot;
  6352. display_name_template: &quot;{{ user.first_name }} {{ user.last_name }}&quot;
  6353. email_template: &quot;{{ user.email }}&quot;
  6354. </code></pre>
  6355. <div style="break-before: page; page-break-before: always;"></div><h1 id="sso-mapping-providers"><a class="header" href="#sso-mapping-providers">SSO Mapping Providers</a></h1>
  6356. <p>A mapping provider is a Python class (loaded via a Python module) that
  6357. works out how to map attributes of a SSO response to Matrix-specific
  6358. user attributes. Details such as user ID localpart, displayname, and even avatar
  6359. URLs are all things that can be mapped from talking to a SSO service.</p>
  6360. <p>As an example, a SSO service may return the email address
  6361. &quot;john.smith@example.com&quot; for a user, whereas Synapse will need to figure out how
  6362. to turn that into a displayname when creating a Matrix user for this individual.
  6363. It may choose <code>John Smith</code>, or <code>Smith, John [Example.com]</code> or any number of
  6364. variations. As each Synapse configuration may want something different, this is
  6365. where SAML mapping providers come into play.</p>
  6366. <p>SSO mapping providers are currently supported for OpenID and SAML SSO
  6367. configurations. Please see the details below for how to implement your own.</p>
  6368. <p>It is up to the mapping provider whether the user should be assigned a predefined
  6369. Matrix ID based on the SSO attributes, or if the user should be allowed to
  6370. choose their own username.</p>
  6371. <p>In the first case - where users are automatically allocated a Matrix ID - it is
  6372. the responsibility of the mapping provider to normalise the SSO attributes and
  6373. map them to a valid Matrix ID. The <a href="https://matrix.org/docs/spec/appendices#user-identifiers">specification for Matrix
  6374. IDs</a> has some
  6375. information about what is considered valid.</p>
  6376. <p>If the mapping provider does not assign a Matrix ID, then Synapse will
  6377. automatically serve an HTML page allowing the user to pick their own username.</p>
  6378. <p>External mapping providers are provided to Synapse in the form of an external
  6379. Python module. You can retrieve this module from <a href="https://pypi.org">PyPI</a> or elsewhere,
  6380. but it must be importable via Synapse (e.g. it must be in the same virtualenv
  6381. as Synapse). The Synapse config is then modified to point to the mapping provider
  6382. (and optionally provide additional configuration for it).</p>
  6383. <h2 id="openid-mapping-providers"><a class="header" href="#openid-mapping-providers">OpenID Mapping Providers</a></h2>
  6384. <p>The OpenID mapping provider can be customized by editing the
  6385. <code>oidc_config.user_mapping_provider.module</code> config option.</p>
  6386. <p><code>oidc_config.user_mapping_provider.config</code> allows you to provide custom
  6387. configuration options to the module. Check with the module's documentation for
  6388. what options it provides (if any). The options listed by default are for the
  6389. user mapping provider built in to Synapse. If using a custom module, you should
  6390. comment these options out and use those specified by the module instead.</p>
  6391. <h3 id="building-a-custom-openid-mapping-provider"><a class="header" href="#building-a-custom-openid-mapping-provider">Building a Custom OpenID Mapping Provider</a></h3>
  6392. <p>A custom mapping provider must specify the following methods:</p>
  6393. <ul>
  6394. <li><code>__init__(self, parsed_config)</code>
  6395. <ul>
  6396. <li>Arguments:
  6397. <ul>
  6398. <li><code>parsed_config</code> - A configuration object that is the return value of the
  6399. <code>parse_config</code> method. You should set any configuration options needed by
  6400. the module here.</li>
  6401. </ul>
  6402. </li>
  6403. </ul>
  6404. </li>
  6405. <li><code>parse_config(config)</code>
  6406. <ul>
  6407. <li>This method should have the <code>@staticmethod</code> decoration.</li>
  6408. <li>Arguments:
  6409. <ul>
  6410. <li><code>config</code> - A <code>dict</code> representing the parsed content of the
  6411. <code>oidc_config.user_mapping_provider.config</code> homeserver config option.
  6412. Runs on homeserver startup. Providers should extract and validate
  6413. any option values they need here.</li>
  6414. </ul>
  6415. </li>
  6416. <li>Whatever is returned will be passed back to the user mapping provider module's
  6417. <code>__init__</code> method during construction.</li>
  6418. </ul>
  6419. </li>
  6420. <li><code>get_remote_user_id(self, userinfo)</code>
  6421. <ul>
  6422. <li>Arguments:
  6423. <ul>
  6424. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  6425. information from.</li>
  6426. </ul>
  6427. </li>
  6428. <li>This method must return a string, which is the unique, immutable identifier
  6429. for the user. Commonly the <code>sub</code> claim of the response.</li>
  6430. </ul>
  6431. </li>
  6432. <li><code>map_user_attributes(self, userinfo, token, failures)</code>
  6433. <ul>
  6434. <li>This method must be async.</li>
  6435. <li>Arguments:
  6436. <ul>
  6437. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  6438. information from.</li>
  6439. <li><code>token</code> - A dictionary which includes information necessary to make
  6440. further requests to the OpenID provider.</li>
  6441. <li><code>failures</code> - An <code>int</code> that represents the amount of times the returned
  6442. mxid localpart mapping has failed. This should be used
  6443. to create a deduplicated mxid localpart which should be
  6444. returned instead. For example, if this method returns
  6445. <code>john.doe</code> as the value of <code>localpart</code> in the returned
  6446. dict, and that is already taken on the homeserver, this
  6447. method will be called again with the same parameters but
  6448. with failures=1. The method should then return a different
  6449. <code>localpart</code> value, such as <code>john.doe1</code>.</li>
  6450. </ul>
  6451. </li>
  6452. <li>Returns a dictionary with two keys:
  6453. <ul>
  6454. <li><code>localpart</code>: A string, used to generate the Matrix ID. If this is
  6455. <code>None</code>, the user is prompted to pick their own username. This is only used
  6456. during a user's first login. Once a localpart has been associated with a
  6457. remote user ID (see <code>get_remote_user_id</code>) it cannot be updated.</li>
  6458. <li><code>displayname</code>: An optional string, the display name for the user.</li>
  6459. </ul>
  6460. </li>
  6461. </ul>
  6462. </li>
  6463. <li><code>get_extra_attributes(self, userinfo, token)</code>
  6464. <ul>
  6465. <li>
  6466. <p>This method must be async.</p>
  6467. </li>
  6468. <li>
  6469. <p>Arguments:</p>
  6470. <ul>
  6471. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  6472. information from.</li>
  6473. <li><code>token</code> - A dictionary which includes information necessary to make
  6474. further requests to the OpenID provider.</li>
  6475. </ul>
  6476. </li>
  6477. <li>
  6478. <p>Returns a dictionary that is suitable to be serialized to JSON. This
  6479. will be returned as part of the response during a successful login.</p>
  6480. <p>Note that care should be taken to not overwrite any of the parameters
  6481. usually returned as part of the <a href="https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login">login response</a>.</p>
  6482. </li>
  6483. </ul>
  6484. </li>
  6485. </ul>
  6486. <h3 id="default-openid-mapping-provider"><a class="header" href="#default-openid-mapping-provider">Default OpenID Mapping Provider</a></h3>
  6487. <p>Synapse has a built-in OpenID mapping provider if a custom provider isn't
  6488. specified in the config. It is located at
  6489. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/handlers/oidc.py"><code>synapse.handlers.oidc.JinjaOidcMappingProvider</code></a>.</p>
  6490. <h2 id="saml-mapping-providers"><a class="header" href="#saml-mapping-providers">SAML Mapping Providers</a></h2>
  6491. <p>The SAML mapping provider can be customized by editing the
  6492. <code>saml2_config.user_mapping_provider.module</code> config option.</p>
  6493. <p><code>saml2_config.user_mapping_provider.config</code> allows you to provide custom
  6494. configuration options to the module. Check with the module's documentation for
  6495. what options it provides (if any). The options listed by default are for the
  6496. user mapping provider built in to Synapse. If using a custom module, you should
  6497. comment these options out and use those specified by the module instead.</p>
  6498. <h3 id="building-a-custom-saml-mapping-provider"><a class="header" href="#building-a-custom-saml-mapping-provider">Building a Custom SAML Mapping Provider</a></h3>
  6499. <p>A custom mapping provider must specify the following methods:</p>
  6500. <ul>
  6501. <li><code>__init__(self, parsed_config, module_api)</code>
  6502. <ul>
  6503. <li>Arguments:
  6504. <ul>
  6505. <li><code>parsed_config</code> - A configuration object that is the return value of the
  6506. <code>parse_config</code> method. You should set any configuration options needed by
  6507. the module here.</li>
  6508. <li><code>module_api</code> - a <code>synapse.module_api.ModuleApi</code> object which provides the
  6509. stable API available for extension modules.</li>
  6510. </ul>
  6511. </li>
  6512. </ul>
  6513. </li>
  6514. <li><code>parse_config(config)</code>
  6515. <ul>
  6516. <li>This method should have the <code>@staticmethod</code> decoration.</li>
  6517. <li>Arguments:
  6518. <ul>
  6519. <li><code>config</code> - A <code>dict</code> representing the parsed content of the
  6520. <code>saml_config.user_mapping_provider.config</code> homeserver config option.
  6521. Runs on homeserver startup. Providers should extract and validate
  6522. any option values they need here.</li>
  6523. </ul>
  6524. </li>
  6525. <li>Whatever is returned will be passed back to the user mapping provider module's
  6526. <code>__init__</code> method during construction.</li>
  6527. </ul>
  6528. </li>
  6529. <li><code>get_saml_attributes(config)</code>
  6530. <ul>
  6531. <li>This method should have the <code>@staticmethod</code> decoration.</li>
  6532. <li>Arguments:
  6533. <ul>
  6534. <li><code>config</code> - A object resulting from a call to <code>parse_config</code>.</li>
  6535. </ul>
  6536. </li>
  6537. <li>Returns a tuple of two sets. The first set equates to the SAML auth
  6538. response attributes that are required for the module to function, whereas
  6539. the second set consists of those attributes which can be used if available,
  6540. but are not necessary.</li>
  6541. </ul>
  6542. </li>
  6543. <li><code>get_remote_user_id(self, saml_response, client_redirect_url)</code>
  6544. <ul>
  6545. <li>Arguments:
  6546. <ul>
  6547. <li><code>saml_response</code> - A <code>saml2.response.AuthnResponse</code> object to extract user
  6548. information from.</li>
  6549. <li><code>client_redirect_url</code> - A string, the URL that the client will be
  6550. redirected to.</li>
  6551. </ul>
  6552. </li>
  6553. <li>This method must return a string, which is the unique, immutable identifier
  6554. for the user. Commonly the <code>uid</code> claim of the response.</li>
  6555. </ul>
  6556. </li>
  6557. <li><code>saml_response_to_user_attributes(self, saml_response, failures, client_redirect_url)</code>
  6558. <ul>
  6559. <li>
  6560. <p>Arguments:</p>
  6561. <ul>
  6562. <li><code>saml_response</code> - A <code>saml2.response.AuthnResponse</code> object to extract user
  6563. information from.</li>
  6564. <li><code>failures</code> - An <code>int</code> that represents the amount of times the returned
  6565. mxid localpart mapping has failed. This should be used
  6566. to create a deduplicated mxid localpart which should be
  6567. returned instead. For example, if this method returns
  6568. <code>john.doe</code> as the value of <code>mxid_localpart</code> in the returned
  6569. dict, and that is already taken on the homeserver, this
  6570. method will be called again with the same parameters but
  6571. with failures=1. The method should then return a different
  6572. <code>mxid_localpart</code> value, such as <code>john.doe1</code>.</li>
  6573. <li><code>client_redirect_url</code> - A string, the URL that the client will be
  6574. redirected to.</li>
  6575. </ul>
  6576. </li>
  6577. <li>
  6578. <p>This method must return a dictionary, which will then be used by Synapse
  6579. to build a new user. The following keys are allowed:</p>
  6580. <ul>
  6581. <li><code>mxid_localpart</code> - A string, the mxid localpart of the new user. If this is
  6582. <code>None</code>, the user is prompted to pick their own username. This is only used
  6583. during a user's first login. Once a localpart has been associated with a
  6584. remote user ID (see <code>get_remote_user_id</code>) it cannot be updated.</li>
  6585. <li><code>displayname</code> - The displayname of the new user. If not provided, will default to
  6586. the value of <code>mxid_localpart</code>.</li>
  6587. <li><code>emails</code> - A list of emails for the new user. If not provided, will
  6588. default to an empty list.</li>
  6589. </ul>
  6590. <p>Alternatively it can raise a <code>synapse.api.errors.RedirectException</code> to
  6591. redirect the user to another page. This is useful to prompt the user for
  6592. additional information, e.g. if you want them to provide their own username.
  6593. It is the responsibility of the mapping provider to either redirect back
  6594. to <code>client_redirect_url</code> (including any additional information) or to
  6595. complete registration using methods from the <code>ModuleApi</code>.</p>
  6596. </li>
  6597. </ul>
  6598. </li>
  6599. </ul>
  6600. <h3 id="default-saml-mapping-provider"><a class="header" href="#default-saml-mapping-provider">Default SAML Mapping Provider</a></h3>
  6601. <p>Synapse has a built-in SAML mapping provider if a custom provider isn't
  6602. specified in the config. It is located at
  6603. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/handlers/saml.py"><code>synapse.handlers.saml.DefaultSamlMappingProvider</code></a>.</p>
  6604. <div style="break-before: page; page-break-before: always;"></div><h2 style="color:red">
  6605. This page of the Synapse documentation is now deprecated. For up to date
  6606. documentation on setting up or writing a password auth provider module, please see
  6607. <a href="modules.html">this page</a>.
  6608. </h2>
  6609. <h1 id="password-auth-provider-modules"><a class="header" href="#password-auth-provider-modules">Password auth provider modules</a></h1>
  6610. <p>Password auth providers offer a way for server administrators to
  6611. integrate their Synapse installation with an existing authentication
  6612. system.</p>
  6613. <p>A password auth provider is a Python class which is dynamically loaded
  6614. into Synapse, and provides a number of methods by which it can integrate
  6615. with the authentication system.</p>
  6616. <p>This document serves as a reference for those looking to implement their
  6617. own password auth providers. Additionally, here is a list of known
  6618. password auth provider module implementations:</p>
  6619. <ul>
  6620. <li><a href="https://github.com/matrix-org/matrix-synapse-ldap3/">matrix-synapse-ldap3</a></li>
  6621. <li><a href="https://github.com/devture/matrix-synapse-shared-secret-auth">matrix-synapse-shared-secret-auth</a></li>
  6622. <li><a href="https://github.com/ma1uta/matrix-synapse-rest-password-provider">matrix-synapse-rest-password-provider</a></li>
  6623. </ul>
  6624. <h2 id="required-methods"><a class="header" href="#required-methods">Required methods</a></h2>
  6625. <p>Password auth provider classes must provide the following methods:</p>
  6626. <ul>
  6627. <li>
  6628. <p><code>parse_config(config)</code>
  6629. This method is passed the <code>config</code> object for this module from the
  6630. homeserver configuration file.</p>
  6631. <p>It should perform any appropriate sanity checks on the provided
  6632. configuration, and return an object which is then passed into
  6633. <code>__init__</code>.</p>
  6634. <p>This method should have the <code>@staticmethod</code> decoration.</p>
  6635. </li>
  6636. <li>
  6637. <p><code>__init__(self, config, account_handler)</code></p>
  6638. <p>The constructor is passed the config object returned by
  6639. <code>parse_config</code>, and a <code>synapse.module_api.ModuleApi</code> object which
  6640. allows the password provider to check if accounts exist and/or create
  6641. new ones.</p>
  6642. </li>
  6643. </ul>
  6644. <h2 id="optional-methods"><a class="header" href="#optional-methods">Optional methods</a></h2>
  6645. <p>Password auth provider classes may optionally provide the following methods:</p>
  6646. <ul>
  6647. <li>
  6648. <p><code>get_db_schema_files(self)</code></p>
  6649. <p>This method, if implemented, should return an Iterable of
  6650. <code>(name, stream)</code> pairs of database schema files. Each file is applied
  6651. in turn at initialisation, and a record is then made in the database
  6652. so that it is not re-applied on the next start.</p>
  6653. </li>
  6654. <li>
  6655. <p><code>get_supported_login_types(self)</code></p>
  6656. <p>This method, if implemented, should return a <code>dict</code> mapping from a
  6657. login type identifier (such as <code>m.login.password</code>) to an iterable
  6658. giving the fields which must be provided by the user in the submission
  6659. to <a href="https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login">the <code>/login</code> API</a>.
  6660. These fields are passed in the <code>login_dict</code> dictionary to <code>check_auth</code>.</p>
  6661. <p>For example, if a password auth provider wants to implement a custom
  6662. login type of <code>com.example.custom_login</code>, where the client is expected
  6663. to pass the fields <code>secret1</code> and <code>secret2</code>, the provider should
  6664. implement this method and return the following dict:</p>
  6665. <pre><code class="language-python">{&quot;com.example.custom_login&quot;: (&quot;secret1&quot;, &quot;secret2&quot;)}
  6666. </code></pre>
  6667. </li>
  6668. <li>
  6669. <p><code>check_auth(self, username, login_type, login_dict)</code></p>
  6670. <p>This method does the real work. If implemented, it
  6671. will be called for each login attempt where the login type matches one
  6672. of the keys returned by <code>get_supported_login_types</code>.</p>
  6673. <p>It is passed the (possibly unqualified) <code>user</code> field provided by the client,
  6674. the login type, and a dictionary of login secrets passed by the
  6675. client.</p>
  6676. <p>The method should return an <code>Awaitable</code> object, which resolves
  6677. to the canonical <code>@localpart:domain</code> user ID if authentication is
  6678. successful, and <code>None</code> if not.</p>
  6679. <p>Alternatively, the <code>Awaitable</code> can resolve to a <code>(str, func)</code> tuple, in
  6680. which case the second field is a callback which will be called with
  6681. the result from the <code>/login</code> call (including <code>access_token</code>,
  6682. <code>device_id</code>, etc.)</p>
  6683. </li>
  6684. <li>
  6685. <p><code>check_3pid_auth(self, medium, address, password)</code></p>
  6686. <p>This method, if implemented, is called when a user attempts to
  6687. register or log in with a third party identifier, such as email. It is
  6688. passed the medium (ex. &quot;email&quot;), an address (ex.
  6689. &quot;<a href="mailto:jdoe@example.com">jdoe@example.com</a>&quot;) and the user's password.</p>
  6690. <p>The method should return an <code>Awaitable</code> object, which resolves
  6691. to a <code>str</code> containing the user's (canonical) User id if
  6692. authentication was successful, and <code>None</code> if not.</p>
  6693. <p>As with <code>check_auth</code>, the <code>Awaitable</code> may alternatively resolve to a
  6694. <code>(user_id, callback)</code> tuple.</p>
  6695. </li>
  6696. <li>
  6697. <p><code>check_password(self, user_id, password)</code></p>
  6698. <p>This method provides a simpler interface than
  6699. <code>get_supported_login_types</code> and <code>check_auth</code> for password auth
  6700. providers that just want to provide a mechanism for validating
  6701. <code>m.login.password</code> logins.</p>
  6702. <p>If implemented, it will be called to check logins with an
  6703. <code>m.login.password</code> login type. It is passed a qualified
  6704. <code>@localpart:domain</code> user id, and the password provided by the user.</p>
  6705. <p>The method should return an <code>Awaitable</code> object, which resolves
  6706. to <code>True</code> if authentication is successful, and <code>False</code> if not.</p>
  6707. </li>
  6708. <li>
  6709. <p><code>on_logged_out(self, user_id, device_id, access_token)</code></p>
  6710. <p>This method, if implemented, is called when a user logs out. It is
  6711. passed the qualified user ID, the ID of the deactivated device (if
  6712. any: access tokens are occasionally created without an associated
  6713. device ID), and the (now deactivated) access token.</p>
  6714. <p>It may return an <code>Awaitable</code> object; the logout request will
  6715. wait for the <code>Awaitable</code> to complete, but the result is ignored.</p>
  6716. </li>
  6717. </ul>
  6718. <div style="break-before: page; page-break-before: always;"></div><h1 id="jwt-login-type"><a class="header" href="#jwt-login-type">JWT Login Type</a></h1>
  6719. <p>Synapse comes with a non-standard login type to support
  6720. <a href="https://en.wikipedia.org/wiki/JSON_Web_Token">JSON Web Tokens</a>. In general the
  6721. documentation for
  6722. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#login">the login endpoint</a>
  6723. is still valid (and the mechanism works similarly to the
  6724. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#token-based">token based login</a>).</p>
  6725. <p>To log in using a JSON Web Token, clients should submit a <code>/login</code> request as
  6726. follows:</p>
  6727. <pre><code class="language-json">{
  6728. &quot;type&quot;: &quot;org.matrix.login.jwt&quot;,
  6729. &quot;token&quot;: &quot;&lt;jwt&gt;&quot;
  6730. }
  6731. </code></pre>
  6732. <p>Note that the login type of <code>m.login.jwt</code> is supported, but is deprecated. This
  6733. will be removed in a future version of Synapse.</p>
  6734. <p>The <code>token</code> field should include the JSON web token with the following claims:</p>
  6735. <ul>
  6736. <li>The <code>sub</code> (subject) claim is required and should encode the local part of the
  6737. user ID.</li>
  6738. <li>The expiration time (<code>exp</code>), not before time (<code>nbf</code>), and issued at (<code>iat</code>)
  6739. claims are optional, but validated if present.</li>
  6740. <li>The issuer (<code>iss</code>) claim is optional, but required and validated if configured.</li>
  6741. <li>The audience (<code>aud</code>) claim is optional, but required and validated if configured.
  6742. Providing the audience claim when not configured will cause validation to fail.</li>
  6743. </ul>
  6744. <p>In the case that the token is not valid, the homeserver must respond with
  6745. <code>403 Forbidden</code> and an error code of <code>M_FORBIDDEN</code>.</p>
  6746. <p>As with other login types, there are additional fields (e.g. <code>device_id</code> and
  6747. <code>initial_device_display_name</code>) which can be included in the above request.</p>
  6748. <h2 id="preparing-synapse-1"><a class="header" href="#preparing-synapse-1">Preparing Synapse</a></h2>
  6749. <p>The JSON Web Token integration in Synapse uses the
  6750. <a href="https://pypi.org/project/pyjwt/"><code>PyJWT</code></a> library, which must be installed
  6751. as follows:</p>
  6752. <ul>
  6753. <li>
  6754. <p>The relevant libraries are included in the Docker images and Debian packages
  6755. provided by <code>matrix.org</code> so no further action is needed.</p>
  6756. </li>
  6757. <li>
  6758. <p>If you installed Synapse into a virtualenv, run <code>/path/to/env/bin/pip install synapse[pyjwt]</code> to install the necessary dependencies.</p>
  6759. </li>
  6760. <li>
  6761. <p>For other installation mechanisms, see the documentation provided by the
  6762. maintainer.</p>
  6763. </li>
  6764. </ul>
  6765. <p>To enable the JSON web token integration, you should then add an <code>jwt_config</code> section
  6766. to your configuration file (or uncomment the <code>enabled: true</code> line in the
  6767. existing section). See <a href="./sample_config.yaml">sample_config.yaml</a> for some
  6768. sample settings.</p>
  6769. <h2 id="how-to-test-jwt-as-a-developer"><a class="header" href="#how-to-test-jwt-as-a-developer">How to test JWT as a developer</a></h2>
  6770. <p>Although JSON Web Tokens are typically generated from an external server, the
  6771. examples below use <a href="https://pyjwt.readthedocs.io/en/latest/">PyJWT</a> directly.</p>
  6772. <ol>
  6773. <li>
  6774. <p>Configure Synapse with JWT logins, note that this example uses a pre-shared
  6775. secret and an algorithm of HS256:</p>
  6776. <pre><code class="language-yaml">jwt_config:
  6777. enabled: true
  6778. secret: &quot;my-secret-token&quot;
  6779. algorithm: &quot;HS256&quot;
  6780. </code></pre>
  6781. </li>
  6782. <li>
  6783. <p>Generate a JSON web token:</p>
  6784. <pre><code class="language-bash">$ pyjwt --key=my-secret-token --alg=HS256 encode sub=test-user
  6785. eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc
  6786. </code></pre>
  6787. </li>
  6788. <li>
  6789. <p>Query for the login types and ensure <code>org.matrix.login.jwt</code> is there:</p>
  6790. <pre><code class="language-bash">curl http://localhost:8080/_matrix/client/r0/login
  6791. </code></pre>
  6792. </li>
  6793. <li>
  6794. <p>Login used the generated JSON web token from above:</p>
  6795. <pre><code class="language-bash">$ curl http://localhost:8082/_matrix/client/r0/login -X POST \
  6796. --data '{&quot;type&quot;:&quot;org.matrix.login.jwt&quot;,&quot;token&quot;:&quot;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc&quot;}'
  6797. {
  6798. &quot;access_token&quot;: &quot;&lt;access token&gt;&quot;,
  6799. &quot;device_id&quot;: &quot;ACBDEFGHI&quot;,
  6800. &quot;home_server&quot;: &quot;localhost:8080&quot;,
  6801. &quot;user_id&quot;: &quot;@test-user:localhost:8480&quot;
  6802. }
  6803. </code></pre>
  6804. </li>
  6805. </ol>
  6806. <p>You should now be able to use the returned access token to query the client API.</p>
  6807. <div style="break-before: page; page-break-before: always;"></div><h1 id="overview-2"><a class="header" href="#overview-2">Overview</a></h1>
  6808. <p>A captcha can be enabled on your homeserver to help prevent bots from registering
  6809. accounts. Synapse currently uses Google's reCAPTCHA service which requires API keys
  6810. from Google.</p>
  6811. <h2 id="getting-api-keys"><a class="header" href="#getting-api-keys">Getting API keys</a></h2>
  6812. <ol>
  6813. <li>Create a new site at <a href="https://www.google.com/recaptcha/admin/create">https://www.google.com/recaptcha/admin/create</a></li>
  6814. <li>Set the label to anything you want</li>
  6815. <li>Set the type to reCAPTCHA v2 using the &quot;I'm not a robot&quot; Checkbox option.
  6816. This is the only type of captcha that works with Synapse.</li>
  6817. <li>Add the public hostname for your server, as set in <code>public_baseurl</code>
  6818. in <code>homeserver.yaml</code>, to the list of authorized domains. If you have not set
  6819. <code>public_baseurl</code>, use <code>server_name</code>.</li>
  6820. <li>Agree to the terms of service and submit.</li>
  6821. <li>Copy your site key and secret key and add them to your <code>homeserver.yaml</code>
  6822. configuration file
  6823. <pre><code class="language-yaml">recaptcha_public_key: YOUR_SITE_KEY
  6824. recaptcha_private_key: YOUR_SECRET_KEY
  6825. </code></pre>
  6826. </li>
  6827. <li>Enable the CAPTCHA for new registrations
  6828. <pre><code class="language-yaml">enable_registration_captcha: true
  6829. </code></pre>
  6830. </li>
  6831. <li>Go to the settings page for the CAPTCHA you just created</li>
  6832. <li>Uncheck the &quot;Verify the origin of reCAPTCHA solutions&quot; checkbox so that the
  6833. captcha can be displayed in any client. If you do not disable this option then you
  6834. must specify the domains of every client that is allowed to display the CAPTCHA.</li>
  6835. </ol>
  6836. <h2 id="configuring-ip-used-for-auth"><a class="header" href="#configuring-ip-used-for-auth">Configuring IP used for auth</a></h2>
  6837. <p>The reCAPTCHA API requires that the IP address of the user who solved the
  6838. CAPTCHA is sent. If the client is connecting through a proxy or load balancer,
  6839. it may be required to use the <code>X-Forwarded-For</code> (XFF) header instead of the origin
  6840. IP address. This can be configured using the <code>x_forwarded</code> directive in the
  6841. listeners section of the <code>homeserver.yaml</code> configuration file.</p>
  6842. <div style="break-before: page; page-break-before: always;"></div><h1 id="registering-an-application-service"><a class="header" href="#registering-an-application-service">Registering an Application Service</a></h1>
  6843. <p>The registration of new application services depends on the homeserver used.
  6844. In synapse, you need to create a new configuration file for your AS and add it
  6845. to the list specified under the <code>app_service_config_files</code> config
  6846. option in your synapse config.</p>
  6847. <p>For example:</p>
  6848. <pre><code class="language-yaml">app_service_config_files:
  6849. - /home/matrix/.synapse/&lt;your-AS&gt;.yaml
  6850. </code></pre>
  6851. <p>The format of the AS configuration file is as follows:</p>
  6852. <pre><code class="language-yaml">url: &lt;base url of AS&gt;
  6853. as_token: &lt;token AS will add to requests to HS&gt;
  6854. hs_token: &lt;token HS will add to requests to AS&gt;
  6855. sender_localpart: &lt;localpart of AS user&gt;
  6856. namespaces:
  6857. users: # List of users we're interested in
  6858. - exclusive: &lt;bool&gt;
  6859. regex: &lt;regex&gt;
  6860. group_id: &lt;group&gt;
  6861. - ...
  6862. aliases: [] # List of aliases we're interested in
  6863. rooms: [] # List of room ids we're interested in
  6864. </code></pre>
  6865. <p><code>exclusive</code>: If enabled, only this application service is allowed to register users in its namespace(s).
  6866. <code>group_id</code>: All users of this application service are dynamically joined to this group. This is useful for e.g user organisation or flairs.</p>
  6867. <p>See the <a href="https://matrix.org/docs/spec/application_service/unstable.html">spec</a> for further details on how application services work.</p>
  6868. <div style="break-before: page; page-break-before: always;"></div><h1 id="server-notices"><a class="header" href="#server-notices">Server Notices</a></h1>
  6869. <p>'Server Notices' are a new feature introduced in Synapse 0.30. They provide a
  6870. channel whereby server administrators can send messages to users on the server.</p>
  6871. <p>They are used as part of communication of the server polices (see
  6872. <a href="consent_tracking.html">Consent Tracking</a>), however the intention is that
  6873. they may also find a use for features such as &quot;Message of the day&quot;.</p>
  6874. <p>This is a feature specific to Synapse, but it uses standard Matrix
  6875. communication mechanisms, so should work with any Matrix client.</p>
  6876. <h2 id="user-experience"><a class="header" href="#user-experience">User experience</a></h2>
  6877. <p>When the user is first sent a server notice, they will get an invitation to a
  6878. room (typically called 'Server Notices', though this is configurable in
  6879. <code>homeserver.yaml</code>). They will be <strong>unable to reject</strong> this invitation -
  6880. attempts to do so will receive an error.</p>
  6881. <p>Once they accept the invitation, they will see the notice message in the room
  6882. history; it will appear to have come from the 'server notices user' (see
  6883. below).</p>
  6884. <p>The user is prevented from sending any messages in this room by the power
  6885. levels.</p>
  6886. <p>Having joined the room, the user can leave the room if they want. Subsequent
  6887. server notices will then cause a new room to be created.</p>
  6888. <h2 id="synapse-configuration"><a class="header" href="#synapse-configuration">Synapse configuration</a></h2>
  6889. <p>Server notices come from a specific user id on the server. Server
  6890. administrators are free to choose the user id - something like <code>server</code> is
  6891. suggested, meaning the notices will come from
  6892. <code>@server:&lt;your_server_name&gt;</code>. Once the Server Notices user is configured, that
  6893. user id becomes a special, privileged user, so administrators should ensure
  6894. that <strong>it is not already allocated</strong>.</p>
  6895. <p>In order to support server notices, it is necessary to add some configuration
  6896. to the <code>homeserver.yaml</code> file. In particular, you should add a <code>server_notices</code>
  6897. section, which should look like this:</p>
  6898. <pre><code class="language-yaml">server_notices:
  6899. system_mxid_localpart: server
  6900. system_mxid_display_name: &quot;Server Notices&quot;
  6901. system_mxid_avatar_url: &quot;mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ&quot;
  6902. room_name: &quot;Server Notices&quot;
  6903. </code></pre>
  6904. <p>The only compulsory setting is <code>system_mxid_localpart</code>, which defines the user
  6905. id of the Server Notices user, as above. <code>room_name</code> defines the name of the
  6906. room which will be created.</p>
  6907. <p><code>system_mxid_display_name</code> and <code>system_mxid_avatar_url</code> can be used to set the
  6908. displayname and avatar of the Server Notices user.</p>
  6909. <h2 id="sending-notices"><a class="header" href="#sending-notices">Sending notices</a></h2>
  6910. <p>To send server notices to users you can use the
  6911. <a href="admin_api/server_notices.html">admin_api</a>.</p>
  6912. <div style="break-before: page; page-break-before: always;"></div><h1 id="support-in-synapse-for-tracking-agreement-to-server-terms-and-conditions"><a class="header" href="#support-in-synapse-for-tracking-agreement-to-server-terms-and-conditions">Support in Synapse for tracking agreement to server terms and conditions</a></h1>
  6913. <p>Synapse 0.30 introduces support for tracking whether users have agreed to the
  6914. terms and conditions set by the administrator of a server - and blocking access
  6915. to the server until they have.</p>
  6916. <p>There are several parts to this functionality; each requires some specific
  6917. configuration in <code>homeserver.yaml</code> to be enabled.</p>
  6918. <p>Note that various parts of the configuation and this document refer to the
  6919. &quot;privacy policy&quot;: agreement with a privacy policy is one particular use of this
  6920. feature, but of course adminstrators can specify other terms and conditions
  6921. unrelated to &quot;privacy&quot; per se.</p>
  6922. <h2 id="collecting-policy-agreement-from-a-user"><a class="header" href="#collecting-policy-agreement-from-a-user">Collecting policy agreement from a user</a></h2>
  6923. <p>Synapse can be configured to serve the user a simple policy form with an
  6924. &quot;accept&quot; button. Clicking &quot;Accept&quot; records the user's acceptance in the
  6925. database and shows a success page.</p>
  6926. <p>To enable this, first create templates for the policy and success pages.
  6927. These should be stored on the local filesystem.</p>
  6928. <p>These templates use the <a href="http://jinja.pocoo.org">Jinja2</a> templating language,
  6929. and <a href="https://github.com/matrix-org/synapse/tree/develop/docs/privacy_policy_templates/">docs/privacy_policy_templates</a>
  6930. gives examples of the sort of thing that can be done.</p>
  6931. <p>Note that the templates must be stored under a name giving the language of the
  6932. template - currently this must always be <code>en</code> (for &quot;English&quot;);
  6933. internationalisation support is intended for the future.</p>
  6934. <p>The template for the policy itself should be versioned and named according to
  6935. the version: for example <code>1.0.html</code>. The version of the policy which the user
  6936. has agreed to is stored in the database.</p>
  6937. <p>Once the templates are in place, make the following changes to <code>homeserver.yaml</code>:</p>
  6938. <ol>
  6939. <li>
  6940. <p>Add a <code>user_consent</code> section, which should look like:</p>
  6941. <pre><code class="language-yaml">user_consent:
  6942. template_dir: privacy_policy_templates
  6943. version: 1.0
  6944. </code></pre>
  6945. <p><code>template_dir</code> points to the directory containing the policy
  6946. templates. <code>version</code> defines the version of the policy which will be served
  6947. to the user. In the example above, Synapse will serve
  6948. <code>privacy_policy_templates/en/1.0.html</code>.</p>
  6949. </li>
  6950. <li>
  6951. <p>Add a <code>form_secret</code> setting at the top level:</p>
  6952. <pre><code class="language-yaml">form_secret: &quot;&lt;unique secret&gt;&quot;
  6953. </code></pre>
  6954. <p>This should be set to an arbitrary secret string (try <code>pwgen -y 30</code> to
  6955. generate suitable secrets).</p>
  6956. <p>More on what this is used for below.</p>
  6957. </li>
  6958. <li>
  6959. <p>Add <code>consent</code> wherever the <code>client</code> resource is currently enabled in the
  6960. <code>listeners</code> configuration. For example:</p>
  6961. <pre><code class="language-yaml">listeners:
  6962. - port: 8008
  6963. resources:
  6964. - names:
  6965. - client
  6966. - consent
  6967. </code></pre>
  6968. </li>
  6969. </ol>
  6970. <p>Finally, ensure that <code>jinja2</code> is installed. If you are using a virtualenv, this
  6971. should be a matter of <code>pip install Jinja2</code>. On debian, try <code>apt-get install python-jinja2</code>.</p>
  6972. <p>Once this is complete, and the server has been restarted, try visiting
  6973. <code>https://&lt;server&gt;/_matrix/consent</code>. If correctly configured, this should give
  6974. an error &quot;Missing string query parameter 'u'&quot;. It is now possible to manually
  6975. construct URIs where users can give their consent.</p>
  6976. <h3 id="enabling-consent-tracking-at-registration"><a class="header" href="#enabling-consent-tracking-at-registration">Enabling consent tracking at registration</a></h3>
  6977. <ol>
  6978. <li>
  6979. <p>Add the following to your configuration:</p>
  6980. <pre><code class="language-yaml">user_consent:
  6981. require_at_registration: true
  6982. policy_name: &quot;Privacy Policy&quot; # or whatever you'd like to call the policy
  6983. </code></pre>
  6984. </li>
  6985. <li>
  6986. <p>In your consent templates, make use of the <code>public_version</code> variable to
  6987. see if an unauthenticated user is viewing the page. This is typically
  6988. wrapped around the form that would be used to actually agree to the document:</p>
  6989. <pre><code class="language-html">{% if not public_version %}
  6990. &lt;!-- The variables used here are only provided when the 'u' param is given to the homeserver --&gt;
  6991. &lt;form method=&quot;post&quot; action=&quot;consent&quot;&gt;
  6992. &lt;input type=&quot;hidden&quot; name=&quot;v&quot; value=&quot;{{version}}&quot;/&gt;
  6993. &lt;input type=&quot;hidden&quot; name=&quot;u&quot; value=&quot;{{user}}&quot;/&gt;
  6994. &lt;input type=&quot;hidden&quot; name=&quot;h&quot; value=&quot;{{userhmac}}&quot;/&gt;
  6995. &lt;input type=&quot;submit&quot; value=&quot;Sure thing!&quot;/&gt;
  6996. &lt;/form&gt;
  6997. {% endif %}
  6998. </code></pre>
  6999. </li>
  7000. <li>
  7001. <p>Restart Synapse to apply the changes.</p>
  7002. </li>
  7003. </ol>
  7004. <p>Visiting <code>https://&lt;server&gt;/_matrix/consent</code> should now give you a view of the privacy
  7005. document. This is what users will be able to see when registering for accounts.</p>
  7006. <h3 id="constructing-the-consent-uri"><a class="header" href="#constructing-the-consent-uri">Constructing the consent URI</a></h3>
  7007. <p>It may be useful to manually construct the &quot;consent URI&quot; for a given user - for
  7008. instance, in order to send them an email asking them to consent. To do this,
  7009. take the base <code>https://&lt;server&gt;/_matrix/consent</code> URL and add the following
  7010. query parameters:</p>
  7011. <ul>
  7012. <li>
  7013. <p><code>u</code>: the user id of the user. This can either be a full MXID
  7014. (<code>@user:server.com</code>) or just the localpart (<code>user</code>).</p>
  7015. </li>
  7016. <li>
  7017. <p><code>h</code>: hex-encoded HMAC-SHA256 of <code>u</code> using the <code>form_secret</code> as a key. It is
  7018. possible to calculate this on the commandline with something like:</p>
  7019. <pre><code class="language-bash">echo -n '&lt;user&gt;' | openssl sha256 -hmac '&lt;form_secret&gt;'
  7020. </code></pre>
  7021. <p>This should result in a URI which looks something like:
  7022. <code>https://&lt;server&gt;/_matrix/consent?u=&lt;user&gt;&amp;h=68a152465a4d...</code>.</p>
  7023. </li>
  7024. </ul>
  7025. <p>Note that not providing a <code>u</code> parameter will be interpreted as wanting to view
  7026. the document from an unauthenticated perspective, such as prior to registration.
  7027. Therefore, the <code>h</code> parameter is not required in this scenario. To enable this
  7028. behaviour, set <code>require_at_registration</code> to <code>true</code> in your <code>user_consent</code> config.</p>
  7029. <h2 id="sending-users-a-server-notice-asking-them-to-agree-to-the-policy"><a class="header" href="#sending-users-a-server-notice-asking-them-to-agree-to-the-policy">Sending users a server notice asking them to agree to the policy</a></h2>
  7030. <p>It is possible to configure Synapse to send a <a href="server_notices.html">server
  7031. notice</a> to anybody who has not yet agreed to the current
  7032. version of the policy. To do so:</p>
  7033. <ul>
  7034. <li>
  7035. <p>ensure that the consent resource is configured, as in the previous section</p>
  7036. </li>
  7037. <li>
  7038. <p>ensure that server notices are configured, as in <a href="server_notices.html">the server notice documentation</a>.</p>
  7039. </li>
  7040. <li>
  7041. <p>Add <code>server_notice_content</code> under <code>user_consent</code> in <code>homeserver.yaml</code>. For
  7042. example:</p>
  7043. <pre><code class="language-yaml">user_consent:
  7044. server_notice_content:
  7045. msgtype: m.text
  7046. body: &gt;-
  7047. Please give your consent to the privacy policy at %(consent_uri)s.
  7048. </code></pre>
  7049. <p>Synapse automatically replaces the placeholder <code>%(consent_uri)s</code> with the
  7050. consent uri for that user.</p>
  7051. </li>
  7052. <li>
  7053. <p>ensure that <code>public_baseurl</code> is set in <code>homeserver.yaml</code>, and gives the base
  7054. URI that clients use to connect to the server. (It is used to construct
  7055. <code>consent_uri</code> in the server notice.)</p>
  7056. </li>
  7057. </ul>
  7058. <h2 id="blocking-users-from-using-the-server-until-they-agree-to-the-policy"><a class="header" href="#blocking-users-from-using-the-server-until-they-agree-to-the-policy">Blocking users from using the server until they agree to the policy</a></h2>
  7059. <p>Synapse can be configured to block any attempts to join rooms or send messages
  7060. until the user has given their agreement to the policy. (Joining the server
  7061. notices room is exempted from this).</p>
  7062. <p>To enable this, add <code>block_events_error</code> under <code>user_consent</code>. For example:</p>
  7063. <pre><code class="language-yaml">user_consent:
  7064. block_events_error: &gt;-
  7065. You can't send any messages until you consent to the privacy policy at
  7066. %(consent_uri)s.
  7067. </code></pre>
  7068. <p>Synapse automatically replaces the placeholder <code>%(consent_uri)s</code> with the
  7069. consent uri for that user.</p>
  7070. <p>ensure that <code>public_baseurl</code> is set in <code>homeserver.yaml</code>, and gives the base
  7071. URI that clients use to connect to the server. (It is used to construct
  7072. <code>consent_uri</code> in the error.)</p>
  7073. <div style="break-before: page; page-break-before: always;"></div><h1 id="url-previews-1"><a class="header" href="#url-previews-1">URL Previews</a></h1>
  7074. <p>The <code>GET /_matrix/media/r0/preview_url</code> endpoint provides a generic preview API
  7075. for URLs which outputs <a href="https://ogp.me/">Open Graph</a> responses (with some Matrix
  7076. specific additions).</p>
  7077. <p>This does have trade-offs compared to other designs:</p>
  7078. <ul>
  7079. <li>Pros:
  7080. <ul>
  7081. <li>Simple and flexible; can be used by any clients at any point</li>
  7082. </ul>
  7083. </li>
  7084. <li>Cons:
  7085. <ul>
  7086. <li>If each homeserver provides one of these independently, all the HSes in a
  7087. room may needlessly DoS the target URI</li>
  7088. <li>The URL metadata must be stored somewhere, rather than just using Matrix
  7089. itself to store the media.</li>
  7090. <li>Matrix cannot be used to distribute the metadata between homeservers.</li>
  7091. </ul>
  7092. </li>
  7093. </ul>
  7094. <p>When Synapse is asked to preview a URL it does the following:</p>
  7095. <ol>
  7096. <li>Checks against a URL blacklist (defined as <code>url_preview_url_blacklist</code> in the
  7097. config).</li>
  7098. <li>Checks the in-memory cache by URLs and returns the result if it exists. (This
  7099. is also used to de-duplicate processing of multiple in-flight requests at once.)</li>
  7100. <li>Kicks off a background process to generate a preview:
  7101. <ol>
  7102. <li>Checks the database cache by URL and timestamp and returns the result if it
  7103. has not expired and was successful (a 2xx return code).</li>
  7104. <li>Checks if the URL matches an <a href="https://oembed.com/">oEmbed</a> pattern. If it
  7105. does, update the URL to download.</li>
  7106. <li>Downloads the URL and stores it into a file via the media storage provider
  7107. and saves the local media metadata.</li>
  7108. <li>If the media is an image:
  7109. <ol>
  7110. <li>Generates thumbnails.</li>
  7111. <li>Generates an Open Graph response based on image properties.</li>
  7112. </ol>
  7113. </li>
  7114. <li>If the media is HTML:
  7115. <ol>
  7116. <li>Decodes the HTML via the stored file.</li>
  7117. <li>Generates an Open Graph response from the HTML.</li>
  7118. <li>If an image exists in the Open Graph response:
  7119. <ol>
  7120. <li>Downloads the URL and stores it into a file via the media storage
  7121. provider and saves the local media metadata.</li>
  7122. <li>Generates thumbnails.</li>
  7123. <li>Updates the Open Graph response based on image properties.</li>
  7124. </ol>
  7125. </li>
  7126. </ol>
  7127. </li>
  7128. <li>If the media is JSON and an oEmbed URL was found:
  7129. <ol>
  7130. <li>Convert the oEmbed response to an Open Graph response.</li>
  7131. <li>If a thumbnail or image is in the oEmbed response:
  7132. <ol>
  7133. <li>Downloads the URL and stores it into a file via the media storage
  7134. provider and saves the local media metadata.</li>
  7135. <li>Generates thumbnails.</li>
  7136. <li>Updates the Open Graph response based on image properties.</li>
  7137. </ol>
  7138. </li>
  7139. </ol>
  7140. </li>
  7141. <li>Stores the result in the database cache.</li>
  7142. </ol>
  7143. </li>
  7144. <li>Returns the result.</li>
  7145. </ol>
  7146. <p>The in-memory cache expires after 1 hour.</p>
  7147. <p>Expired entries in the database cache (and their associated media files) are
  7148. deleted every 10 seconds. The default expiration time is 1 hour from download.</p>
  7149. <div style="break-before: page; page-break-before: always;"></div><h1 id="user-directory-api-implementation"><a class="header" href="#user-directory-api-implementation">User Directory API Implementation</a></h1>
  7150. <p>The user directory is currently maintained based on the 'visible' users
  7151. on this particular server - i.e. ones which your account shares a room with, or
  7152. who are present in a publicly viewable room present on the server.</p>
  7153. <p>The directory info is stored in various tables, which can (typically after
  7154. DB corruption) get stale or out of sync. If this happens, for now the
  7155. solution to fix it is to execute the SQL <a href="https://github.com/matrix-org/synapse/blob/master/synapse/storage/schema/main/delta/53/user_dir_populate.sql">here</a>
  7156. and then restart synapse. This should then start a background task to
  7157. flush the current tables and regenerate the directory.</p>
  7158. <h2 id="data-model"><a class="header" href="#data-model">Data model</a></h2>
  7159. <p>There are five relevant tables that collectively form the &quot;user directory&quot;.
  7160. Three of them track a master list of all the users we could search for.
  7161. The last two (collectively called the &quot;search tables&quot;) track who can
  7162. see who.</p>
  7163. <p>From all of these tables we exclude three types of local user:</p>
  7164. <ul>
  7165. <li>support users</li>
  7166. <li>appservice users</li>
  7167. <li>deactivated users</li>
  7168. </ul>
  7169. <ul>
  7170. <li>
  7171. <p><code>user_directory</code>. This contains the user_id, display name and avatar we'll
  7172. return when you search the directory.</p>
  7173. <ul>
  7174. <li>Because there's only one directory entry per user, it's important that we only
  7175. ever put publicly visible names here. Otherwise we might leak a private
  7176. nickname or avatar used in a private room.</li>
  7177. <li>Indexed on rooms. Indexed on users.</li>
  7178. </ul>
  7179. </li>
  7180. <li>
  7181. <p><code>user_directory_search</code>. To be joined to <code>user_directory</code>. It contains an extra
  7182. column that enables full text search based on user ids and display names.
  7183. Different schemas for SQLite and Postgres with different code paths to match.</p>
  7184. <ul>
  7185. <li>Indexed on the full text search data. Indexed on users.</li>
  7186. </ul>
  7187. </li>
  7188. <li>
  7189. <p><code>user_directory_stream_pos</code>. When the initial background update to populate
  7190. the directory is complete, we record a stream position here. This indicates
  7191. that synapse should now listen for room changes and incrementally update
  7192. the directory where necessary.</p>
  7193. </li>
  7194. <li>
  7195. <p><code>users_in_public_rooms</code>. Contains associations between users and the public rooms they're in.
  7196. Used to determine which users are in public rooms and should be publicly visible in the directory.</p>
  7197. </li>
  7198. <li>
  7199. <p><code>users_who_share_private_rooms</code>. Rows are triples <code>(L, M, room id)</code> where <code>L</code>
  7200. is a local user and <code>M</code> is a local or remote user. <code>L</code> and <code>M</code> should be
  7201. different, but this isn't enforced by a constraint.</p>
  7202. </li>
  7203. </ul>
  7204. <div style="break-before: page; page-break-before: always;"></div><h1 id="message-retention-policies"><a class="header" href="#message-retention-policies">Message retention policies</a></h1>
  7205. <p>Synapse admins can enable support for message retention policies on
  7206. their homeserver. Message retention policies exist at a room level,
  7207. follow the semantics described in
  7208. <a href="https://github.com/matrix-org/matrix-doc/blob/matthew/msc1763/proposals/1763-configurable-retention-periods.md">MSC1763</a>,
  7209. and allow server and room admins to configure how long messages should
  7210. be kept in a homeserver's database before being purged from it.
  7211. <strong>Please note that, as this feature isn't part of the Matrix
  7212. specification yet, this implementation is to be considered as
  7213. experimental.</strong> </p>
  7214. <p>A message retention policy is mainly defined by its <code>max_lifetime</code>
  7215. parameter, which defines how long a message can be kept around after
  7216. it was sent to the room. If a room doesn't have a message retention
  7217. policy, and there's no default one for a given server, then no message
  7218. sent in that room is ever purged on that server.</p>
  7219. <p>MSC1763 also specifies semantics for a <code>min_lifetime</code> parameter which
  7220. defines the amount of time after which an event <em>can</em> get purged (after
  7221. it was sent to the room), but Synapse doesn't currently support it
  7222. beyond registering it.</p>
  7223. <p>Both <code>max_lifetime</code> and <code>min_lifetime</code> are optional parameters.</p>
  7224. <p>Note that message retention policies don't apply to state events.</p>
  7225. <p>Once an event reaches its expiry date (defined as the time it was sent
  7226. plus the value for <code>max_lifetime</code> in the room), two things happen:</p>
  7227. <ul>
  7228. <li>Synapse stops serving the event to clients via any endpoint.</li>
  7229. <li>The message gets picked up by the next purge job (see the &quot;Purge jobs&quot;
  7230. section) and is removed from Synapse's database.</li>
  7231. </ul>
  7232. <p>Since purge jobs don't run continuously, this means that an event might
  7233. stay in a server's database for longer than the value for <code>max_lifetime</code>
  7234. in the room would allow, though hidden from clients.</p>
  7235. <p>Similarly, if a server (with support for message retention policies
  7236. enabled) receives from another server an event that should have been
  7237. purged according to its room's policy, then the receiving server will
  7238. process and store that event until it's picked up by the next purge job,
  7239. though it will always hide it from clients.</p>
  7240. <p>Synapse requires at least one message in each room, so it will never
  7241. delete the last message in a room. It will, however, hide it from
  7242. clients.</p>
  7243. <h2 id="server-configuration"><a class="header" href="#server-configuration">Server configuration</a></h2>
  7244. <p>Support for this feature can be enabled and configured in the
  7245. <code>retention</code> section of the Synapse configuration file (see the
  7246. <a href="https://github.com/matrix-org/synapse/blob/v1.36.0/docs/sample_config.yaml#L451-L518">sample file</a>).</p>
  7247. <p>To enable support for message retention policies, set the setting
  7248. <code>enabled</code> in this section to <code>true</code>.</p>
  7249. <h3 id="default-policy"><a class="header" href="#default-policy">Default policy</a></h3>
  7250. <p>A default message retention policy is a policy defined in Synapse's
  7251. configuration that is used by Synapse for every room that doesn't have a
  7252. message retention policy configured in its state. This allows server
  7253. admins to ensure that messages are never kept indefinitely in a server's
  7254. database. </p>
  7255. <p>A default policy can be defined as such, in the <code>retention</code> section of
  7256. the configuration file:</p>
  7257. <pre><code class="language-yaml">default_policy:
  7258. min_lifetime: 1d
  7259. max_lifetime: 1y
  7260. </code></pre>
  7261. <p>Here, <code>min_lifetime</code> and <code>max_lifetime</code> have the same meaning and level
  7262. of support as previously described. They can be expressed either as a
  7263. duration (using the units <code>s</code> (seconds), <code>m</code> (minutes), <code>h</code> (hours),
  7264. <code>d</code> (days), <code>w</code> (weeks) and <code>y</code> (years)) or as a number of milliseconds.</p>
  7265. <h3 id="purge-jobs"><a class="header" href="#purge-jobs">Purge jobs</a></h3>
  7266. <p>Purge jobs are the jobs that Synapse runs in the background to purge
  7267. expired events from the database. They are only run if support for
  7268. message retention policies is enabled in the server's configuration. If
  7269. no configuration for purge jobs is configured by the server admin,
  7270. Synapse will use a default configuration, which is described in the
  7271. <a href="https://github.com/matrix-org/synapse/blob/v1.36.0/docs/sample_config.yaml#L451-L518">sample configuration file</a>.</p>
  7272. <p>Some server admins might want a finer control on when events are removed
  7273. depending on an event's room's policy. This can be done by setting the
  7274. <code>purge_jobs</code> sub-section in the <code>retention</code> section of the configuration
  7275. file. An example of such configuration could be:</p>
  7276. <pre><code class="language-yaml">purge_jobs:
  7277. - longest_max_lifetime: 3d
  7278. interval: 12h
  7279. - shortest_max_lifetime: 3d
  7280. longest_max_lifetime: 1w
  7281. interval: 1d
  7282. - shortest_max_lifetime: 1w
  7283. interval: 2d
  7284. </code></pre>
  7285. <p>In this example, we define three jobs:</p>
  7286. <ul>
  7287. <li>one that runs twice a day (every 12 hours) and purges events in rooms
  7288. which policy's <code>max_lifetime</code> is lower or equal to 3 days.</li>
  7289. <li>one that runs once a day and purges events in rooms which policy's
  7290. <code>max_lifetime</code> is between 3 days and a week.</li>
  7291. <li>one that runs once every 2 days and purges events in rooms which
  7292. policy's <code>max_lifetime</code> is greater than a week.</li>
  7293. </ul>
  7294. <p>Note that this example is tailored to show different configurations and
  7295. features slightly more jobs than it's probably necessary (in practice, a
  7296. server admin would probably consider it better to replace the two last
  7297. jobs with one that runs once a day and handles rooms which which
  7298. policy's <code>max_lifetime</code> is greater than 3 days).</p>
  7299. <p>Keep in mind, when configuring these jobs, that a purge job can become
  7300. quite heavy on the server if it targets many rooms, therefore prefer
  7301. having jobs with a low interval that target a limited set of rooms. Also
  7302. make sure to include a job with no minimum and one with no maximum to
  7303. make sure your configuration handles every policy.</p>
  7304. <p>As previously mentioned in this documentation, while a purge job that
  7305. runs e.g. every day means that an expired event might stay in the
  7306. database for up to a day after its expiry, Synapse hides expired events
  7307. from clients as soon as they expire, so the event is not visible to
  7308. local users between its expiry date and the moment it gets purged from
  7309. the server's database.</p>
  7310. <h3 id="lifetime-limits"><a class="header" href="#lifetime-limits">Lifetime limits</a></h3>
  7311. <p>Server admins can set limits on the values of <code>max_lifetime</code> to use when
  7312. purging old events in a room. These limits can be defined as such in the
  7313. <code>retention</code> section of the configuration file:</p>
  7314. <pre><code class="language-yaml">allowed_lifetime_min: 1d
  7315. allowed_lifetime_max: 1y
  7316. </code></pre>
  7317. <p>The limits are considered when running purge jobs. If necessary, the
  7318. effective value of <code>max_lifetime</code> will be brought between
  7319. <code>allowed_lifetime_min</code> and <code>allowed_lifetime_max</code> (inclusive).
  7320. This means that, if the value of <code>max_lifetime</code> defined in the room's state
  7321. is lower than <code>allowed_lifetime_min</code>, the value of <code>allowed_lifetime_min</code>
  7322. will be used instead. Likewise, if the value of <code>max_lifetime</code> is higher
  7323. than <code>allowed_lifetime_max</code>, the value of <code>allowed_lifetime_max</code> will be
  7324. used instead.</p>
  7325. <p>In the example above, we ensure Synapse never deletes events that are less
  7326. than one day old, and that it always deletes events that are over a year
  7327. old.</p>
  7328. <p>If a default policy is set, and its <code>max_lifetime</code> value is lower than
  7329. <code>allowed_lifetime_min</code> or higher than <code>allowed_lifetime_max</code>, the same
  7330. process applies.</p>
  7331. <p>Both parameters are optional; if one is omitted Synapse won't use it to
  7332. adjust the effective value of <code>max_lifetime</code>.</p>
  7333. <p>Like other settings in this section, these parameters can be expressed
  7334. either as a duration or as a number of milliseconds.</p>
  7335. <h2 id="room-configuration"><a class="header" href="#room-configuration">Room configuration</a></h2>
  7336. <p>To configure a room's message retention policy, a room's admin or
  7337. moderator needs to send a state event in that room with the type
  7338. <code>m.room.retention</code> and the following content:</p>
  7339. <pre><code class="language-json">{
  7340. &quot;max_lifetime&quot;: ...
  7341. }
  7342. </code></pre>
  7343. <p>In this event's content, the <code>max_lifetime</code> parameter has the same
  7344. meaning as previously described, and needs to be expressed in
  7345. milliseconds. The event's content can also include a <code>min_lifetime</code>
  7346. parameter, which has the same meaning and limited support as previously
  7347. described.</p>
  7348. <p>Note that over every server in the room, only the ones with support for
  7349. message retention policies will actually remove expired events. This
  7350. support is currently not enabled by default in Synapse.</p>
  7351. <h2 id="note-on-reclaiming-disk-space"><a class="header" href="#note-on-reclaiming-disk-space">Note on reclaiming disk space</a></h2>
  7352. <p>While purge jobs actually delete data from the database, the disk space
  7353. used by the database might not decrease immediately on the database's
  7354. host. However, even though the database engine won't free up the disk
  7355. space, it will start writing new data into where the purged data was.</p>
  7356. <p>If you want to reclaim the freed disk space anyway and return it to the
  7357. operating system, the server admin needs to run <code>VACUUM FULL;</code> (or
  7358. <code>VACUUM;</code> for SQLite databases) on Synapse's database (see the related
  7359. <a href="https://www.postgresql.org/docs/current/sql-vacuum.html">PostgreSQL documentation</a>).</p>
  7360. <div style="break-before: page; page-break-before: always;"></div><h1 id="modules"><a class="header" href="#modules">Modules</a></h1>
  7361. <p>Synapse supports extending its functionality by configuring external modules.</p>
  7362. <p><strong>Note</strong>: When using third-party modules, you effectively allow someone else to run
  7363. custom code on your Synapse homeserver. Server admins are encouraged to verify the
  7364. provenance of the modules they use on their homeserver and make sure the modules aren't
  7365. running malicious code on their instance.</p>
  7366. <h2 id="using-modules"><a class="header" href="#using-modules">Using modules</a></h2>
  7367. <p>To use a module on Synapse, add it to the <code>modules</code> section of the configuration file:</p>
  7368. <pre><code class="language-yaml">modules:
  7369. - module: my_super_module.MySuperClass
  7370. config:
  7371. do_thing: true
  7372. - module: my_other_super_module.SomeClass
  7373. config: {}
  7374. </code></pre>
  7375. <p>Each module is defined by a path to a Python class as well as a configuration. This
  7376. information for a given module should be available in the module's own documentation.</p>
  7377. <h2 id="using-multiple-modules"><a class="header" href="#using-multiple-modules">Using multiple modules</a></h2>
  7378. <p>The order in which modules are listed in this section is important. When processing an
  7379. action that can be handled by several modules, Synapse will always prioritise the module
  7380. that appears first (i.e. is the highest in the list). This means:</p>
  7381. <ul>
  7382. <li>If several modules register the same callback, the callback registered by the module
  7383. that appears first is used.</li>
  7384. <li>If several modules try to register a handler for the same HTTP path, only the handler
  7385. registered by the module that appears first is used. Handlers registered by the other
  7386. module(s) are ignored and Synapse will log a warning message about them.</li>
  7387. </ul>
  7388. <p>Note that Synapse doesn't allow multiple modules implementing authentication checkers via
  7389. the password auth provider feature for the same login type with different fields. If this
  7390. happens, Synapse will refuse to start.</p>
  7391. <h2 id="current-status"><a class="header" href="#current-status">Current status</a></h2>
  7392. <p>We are currently in the process of migrating module interfaces to this system. While some
  7393. interfaces might be compatible with it, others still require configuring modules in
  7394. another part of Synapse's configuration file.</p>
  7395. <p>Currently, only the following pre-existing interfaces are compatible with this new system:</p>
  7396. <ul>
  7397. <li>spam checker</li>
  7398. <li>third-party rules</li>
  7399. <li>presence router</li>
  7400. <li>password auth providers</li>
  7401. </ul>
  7402. <div style="break-before: page; page-break-before: always;"></div><h1 id="writing-a-module"><a class="header" href="#writing-a-module">Writing a module</a></h1>
  7403. <p>A module is a Python class that uses Synapse's module API to interact with the
  7404. homeserver. It can register callbacks that Synapse will call on specific operations, as
  7405. well as web resources to attach to Synapse's web server.</p>
  7406. <p>When instantiated, a module is given its parsed configuration as well as an instance of
  7407. the <code>synapse.module_api.ModuleApi</code> class. The configuration is a dictionary, and is
  7408. either the output of the module's <code>parse_config</code> static method (see below), or the
  7409. configuration associated with the module in Synapse's configuration file.</p>
  7410. <p>See the documentation for the <code>ModuleApi</code> class
  7411. <a href="https://github.com/matrix-org/synapse/blob/master/synapse/module_api/__init__.py">here</a>.</p>
  7412. <h2 id="when-synapse-runs-with-several-modules-configured"><a class="header" href="#when-synapse-runs-with-several-modules-configured">When Synapse runs with several modules configured</a></h2>
  7413. <p>If Synapse is running with other modules configured, the order each module appears in
  7414. within the <code>modules</code> section of the Synapse configuration file might restrict what it can
  7415. or cannot register. See <a href="modules/index.html#using-multiple-modules">this section</a> for more
  7416. information.</p>
  7417. <p>On top of the rules listed in the link above, if a callback returns a value that should
  7418. cause the current operation to fail (e.g. if a callback checking an event returns with a
  7419. value that should cause the event to be denied), Synapse will fail the operation and
  7420. ignore any subsequent callbacks that should have been run after this one.</p>
  7421. <p>The documentation for each callback mentions how Synapse behaves when
  7422. multiple modules implement it.</p>
  7423. <h2 id="handling-the-modules-configuration"><a class="header" href="#handling-the-modules-configuration">Handling the module's configuration</a></h2>
  7424. <p>A module can implement the following static method:</p>
  7425. <pre><code class="language-python">@staticmethod
  7426. def parse_config(config: dict) -&gt; dict
  7427. </code></pre>
  7428. <p>This method is given a dictionary resulting from parsing the YAML configuration for the
  7429. module. It may modify it (for example by parsing durations expressed as strings (e.g.
  7430. &quot;5d&quot;) into milliseconds, etc.), and return the modified dictionary. It may also verify
  7431. that the configuration is correct, and raise an instance of
  7432. <code>synapse.module_api.errors.ConfigError</code> if not.</p>
  7433. <h2 id="registering-a-web-resource"><a class="header" href="#registering-a-web-resource">Registering a web resource</a></h2>
  7434. <p>Modules can register web resources onto Synapse's web server using the following module
  7435. API method:</p>
  7436. <pre><code class="language-python">def ModuleApi.register_web_resource(path: str, resource: IResource) -&gt; None
  7437. </code></pre>
  7438. <p>The path is the full absolute path to register the resource at. For example, if you
  7439. register a resource for the path <code>/_synapse/client/my_super_module/say_hello</code>, Synapse
  7440. will serve it at <code>http(s)://[HS_URL]/_synapse/client/my_super_module/say_hello</code>. Note
  7441. that Synapse does not allow registering resources for several sub-paths in the <code>/_matrix</code>
  7442. namespace (such as anything under <code>/_matrix/client</code> for example). It is strongly
  7443. recommended that modules register their web resources under the <code>/_synapse/client</code>
  7444. namespace.</p>
  7445. <p>The provided resource is a Python class that implements Twisted's <a href="https://twistedmatrix.com/documents/current/api/twisted.web.resource.IResource.html">IResource</a>
  7446. interface (such as <a href="https://twistedmatrix.com/documents/current/api/twisted.web.resource.Resource.html">Resource</a>).</p>
  7447. <p>Only one resource can be registered for a given path. If several modules attempt to
  7448. register a resource for the same path, the module that appears first in Synapse's
  7449. configuration file takes priority.</p>
  7450. <p>Modules <strong>must</strong> register their web resources in their <code>__init__</code> method.</p>
  7451. <h2 id="registering-a-callback"><a class="header" href="#registering-a-callback">Registering a callback</a></h2>
  7452. <p>Modules can use Synapse's module API to register callbacks. Callbacks are functions that
  7453. Synapse will call when performing specific actions. Callbacks must be asynchronous, and
  7454. are split in categories. A single module may implement callbacks from multiple categories,
  7455. and is under no obligation to implement all callbacks from the categories it registers
  7456. callbacks for.</p>
  7457. <p>Modules can register callbacks using one of the module API's <code>register_[...]_callbacks</code>
  7458. methods. The callback functions are passed to these methods as keyword arguments, with
  7459. the callback name as the argument name and the function as its value. This is demonstrated
  7460. in the example below. A <code>register_[...]_callbacks</code> method exists for each category.</p>
  7461. <p>Callbacks for each category can be found on their respective page of the
  7462. <a href="https://matrix-org.github.io/synapse">Synapse documentation website</a>.</p>
  7463. <div style="break-before: page; page-break-before: always;"></div><h1 id="spam-checker-callbacks"><a class="header" href="#spam-checker-callbacks">Spam checker callbacks</a></h1>
  7464. <p>Spam checker callbacks allow module developers to implement spam mitigation actions for
  7465. Synapse instances. Spam checker callbacks can be registered using the module API's
  7466. <code>register_spam_checker_callbacks</code> method.</p>
  7467. <h2 id="callbacks"><a class="header" href="#callbacks">Callbacks</a></h2>
  7468. <p>The available spam checker callbacks are:</p>
  7469. <h3 id="check_event_for_spam"><a class="header" href="#check_event_for_spam"><code>check_event_for_spam</code></a></h3>
  7470. <p><em>First introduced in Synapse v1.37.0</em></p>
  7471. <pre><code class="language-python">async def check_event_for_spam(event: &quot;synapse.events.EventBase&quot;) -&gt; Union[bool, str]
  7472. </code></pre>
  7473. <p>Called when receiving an event from a client or via federation. The module can return
  7474. either a <code>bool</code> to indicate whether the event must be rejected because of spam, or a <code>str</code>
  7475. to indicate the event must be rejected because of spam and to give a rejection reason to
  7476. forward to clients.</p>
  7477. <p>If multiple modules implement this callback, they will be considered in order. If a
  7478. callback returns <code>False</code>, Synapse falls through to the next one. The value of the first
  7479. callback that does not return <code>False</code> will be used. If this happens, Synapse will not call
  7480. any of the subsequent implementations of this callback.</p>
  7481. <h3 id="user_may_join_room"><a class="header" href="#user_may_join_room"><code>user_may_join_room</code></a></h3>
  7482. <p><em>First introduced in Synapse v1.37.0</em></p>
  7483. <pre><code class="language-python">async def user_may_join_room(user: str, room: str, is_invited: bool) -&gt; bool
  7484. </code></pre>
  7485. <p>Called when a user is trying to join a room. The module must return a <code>bool</code> to indicate
  7486. whether the user can join the room. The user is represented by their Matrix user ID (e.g.
  7487. <code>@alice:example.com</code>) and the room is represented by its Matrix ID (e.g.
  7488. <code>!room:example.com</code>). The module is also given a boolean to indicate whether the user
  7489. currently has a pending invite in the room.</p>
  7490. <p>This callback isn't called if the join is performed by a server administrator, or in the
  7491. context of a room creation.</p>
  7492. <p>If multiple modules implement this callback, they will be considered in order. If a
  7493. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7494. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7495. any of the subsequent implementations of this callback.</p>
  7496. <h3 id="user_may_invite"><a class="header" href="#user_may_invite"><code>user_may_invite</code></a></h3>
  7497. <p><em>First introduced in Synapse v1.37.0</em></p>
  7498. <pre><code class="language-python">async def user_may_invite(inviter: str, invitee: str, room_id: str) -&gt; bool
  7499. </code></pre>
  7500. <p>Called when processing an invitation. The module must return a <code>bool</code> indicating whether
  7501. the inviter can invite the invitee to the given room. Both inviter and invitee are
  7502. represented by their Matrix user ID (e.g. <code>@alice:example.com</code>).</p>
  7503. <p>If multiple modules implement this callback, they will be considered in order. If a
  7504. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7505. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7506. any of the subsequent implementations of this callback.</p>
  7507. <h3 id="user_may_send_3pid_invite"><a class="header" href="#user_may_send_3pid_invite"><code>user_may_send_3pid_invite</code></a></h3>
  7508. <p><em>First introduced in Synapse v1.45.0</em></p>
  7509. <pre><code class="language-python">async def user_may_send_3pid_invite(
  7510. inviter: str,
  7511. medium: str,
  7512. address: str,
  7513. room_id: str,
  7514. ) -&gt; bool
  7515. </code></pre>
  7516. <p>Called when processing an invitation using a third-party identifier (also called a 3PID,
  7517. e.g. an email address or a phone number). The module must return a <code>bool</code> indicating
  7518. whether the inviter can invite the invitee to the given room.</p>
  7519. <p>The inviter is represented by their Matrix user ID (e.g. <code>@alice:example.com</code>), and the
  7520. invitee is represented by its medium (e.g. &quot;email&quot;) and its address
  7521. (e.g. <code>alice@example.com</code>). See <a href="https://matrix.org/docs/spec/appendices#pid-types">the Matrix specification</a>
  7522. for more information regarding third-party identifiers.</p>
  7523. <p>For example, a call to this callback to send an invitation to the email address
  7524. <code>alice@example.com</code> would look like this:</p>
  7525. <pre><code class="language-python">await user_may_send_3pid_invite(
  7526. &quot;@bob:example.com&quot;, # The inviter's user ID
  7527. &quot;email&quot;, # The medium of the 3PID to invite
  7528. &quot;alice@example.com&quot;, # The address of the 3PID to invite
  7529. &quot;!some_room:example.com&quot;, # The ID of the room to send the invite into
  7530. )
  7531. </code></pre>
  7532. <p><strong>Note</strong>: If the third-party identifier is already associated with a matrix user ID,
  7533. <a href="modules/spam_checker_callbacks.html#user_may_invite"><code>user_may_invite</code></a> will be used instead.</p>
  7534. <p>If multiple modules implement this callback, they will be considered in order. If a
  7535. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7536. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7537. any of the subsequent implementations of this callback.</p>
  7538. <h3 id="user_may_create_room"><a class="header" href="#user_may_create_room"><code>user_may_create_room</code></a></h3>
  7539. <p><em>First introduced in Synapse v1.37.0</em></p>
  7540. <pre><code class="language-python">async def user_may_create_room(user: str) -&gt; bool
  7541. </code></pre>
  7542. <p>Called when processing a room creation request. The module must return a <code>bool</code> indicating
  7543. whether the given user (represented by their Matrix user ID) is allowed to create a room.</p>
  7544. <p>If multiple modules implement this callback, they will be considered in order. If a
  7545. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7546. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7547. any of the subsequent implementations of this callback.</p>
  7548. <h3 id="user_may_create_room_alias"><a class="header" href="#user_may_create_room_alias"><code>user_may_create_room_alias</code></a></h3>
  7549. <p><em>First introduced in Synapse v1.37.0</em></p>
  7550. <pre><code class="language-python">async def user_may_create_room_alias(user: str, room_alias: &quot;synapse.types.RoomAlias&quot;) -&gt; bool
  7551. </code></pre>
  7552. <p>Called when trying to associate an alias with an existing room. The module must return a
  7553. <code>bool</code> indicating whether the given user (represented by their Matrix user ID) is allowed
  7554. to set the given alias.</p>
  7555. <p>If multiple modules implement this callback, they will be considered in order. If a
  7556. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7557. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7558. any of the subsequent implementations of this callback.</p>
  7559. <h3 id="user_may_publish_room"><a class="header" href="#user_may_publish_room"><code>user_may_publish_room</code></a></h3>
  7560. <p><em>First introduced in Synapse v1.37.0</em></p>
  7561. <pre><code class="language-python">async def user_may_publish_room(user: str, room_id: str) -&gt; bool
  7562. </code></pre>
  7563. <p>Called when trying to publish a room to the homeserver's public rooms directory. The
  7564. module must return a <code>bool</code> indicating whether the given user (represented by their
  7565. Matrix user ID) is allowed to publish the given room.</p>
  7566. <p>If multiple modules implement this callback, they will be considered in order. If a
  7567. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7568. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7569. any of the subsequent implementations of this callback.</p>
  7570. <h3 id="check_username_for_spam"><a class="header" href="#check_username_for_spam"><code>check_username_for_spam</code></a></h3>
  7571. <p><em>First introduced in Synapse v1.37.0</em></p>
  7572. <pre><code class="language-python">async def check_username_for_spam(user_profile: Dict[str, str]) -&gt; bool
  7573. </code></pre>
  7574. <p>Called when computing search results in the user directory. The module must return a
  7575. <code>bool</code> indicating whether the given user profile can appear in search results. The profile
  7576. is represented as a dictionary with the following keys:</p>
  7577. <ul>
  7578. <li><code>user_id</code>: The Matrix ID for this user.</li>
  7579. <li><code>display_name</code>: The user's display name.</li>
  7580. <li><code>avatar_url</code>: The <code>mxc://</code> URL to the user's avatar.</li>
  7581. </ul>
  7582. <p>The module is given a copy of the original dictionary, so modifying it from within the
  7583. module cannot modify a user's profile when included in user directory search results.</p>
  7584. <p>If multiple modules implement this callback, they will be considered in order. If a
  7585. callback returns <code>False</code>, Synapse falls through to the next one. The value of the first
  7586. callback that does not return <code>False</code> will be used. If this happens, Synapse will not call
  7587. any of the subsequent implementations of this callback.</p>
  7588. <h3 id="check_registration_for_spam"><a class="header" href="#check_registration_for_spam"><code>check_registration_for_spam</code></a></h3>
  7589. <p><em>First introduced in Synapse v1.37.0</em></p>
  7590. <pre><code class="language-python">async def check_registration_for_spam(
  7591. email_threepid: Optional[dict],
  7592. username: Optional[str],
  7593. request_info: Collection[Tuple[str, str]],
  7594. auth_provider_id: Optional[str] = None,
  7595. ) -&gt; &quot;synapse.spam_checker_api.RegistrationBehaviour&quot;
  7596. </code></pre>
  7597. <p>Called when registering a new user. The module must return a <code>RegistrationBehaviour</code>
  7598. indicating whether the registration can go through or must be denied, or whether the user
  7599. may be allowed to register but will be shadow banned.</p>
  7600. <p>The arguments passed to this callback are:</p>
  7601. <ul>
  7602. <li><code>email_threepid</code>: The email address used for registering, if any.</li>
  7603. <li><code>username</code>: The username the user would like to register. Can be <code>None</code>, meaning that
  7604. Synapse will generate one later.</li>
  7605. <li><code>request_info</code>: A collection of tuples, which first item is a user agent, and which
  7606. second item is an IP address. These user agents and IP addresses are the ones that were
  7607. used during the registration process.</li>
  7608. <li><code>auth_provider_id</code>: The identifier of the SSO authentication provider, if any.</li>
  7609. </ul>
  7610. <p>If multiple modules implement this callback, they will be considered in order. If a
  7611. callback returns <code>RegistrationBehaviour.ALLOW</code>, Synapse falls through to the next one.
  7612. The value of the first callback that does not return <code>RegistrationBehaviour.ALLOW</code> will
  7613. be used. If this happens, Synapse will not call any of the subsequent implementations of
  7614. this callback.</p>
  7615. <h3 id="check_media_file_for_spam"><a class="header" href="#check_media_file_for_spam"><code>check_media_file_for_spam</code></a></h3>
  7616. <p><em>First introduced in Synapse v1.37.0</em></p>
  7617. <pre><code class="language-python">async def check_media_file_for_spam(
  7618. file_wrapper: &quot;synapse.rest.media.v1.media_storage.ReadableFileWrapper&quot;,
  7619. file_info: &quot;synapse.rest.media.v1._base.FileInfo&quot;,
  7620. ) -&gt; bool
  7621. </code></pre>
  7622. <p>Called when storing a local or remote file. The module must return a boolean indicating
  7623. whether the given file can be stored in the homeserver's media store.</p>
  7624. <p>If multiple modules implement this callback, they will be considered in order. If a
  7625. callback returns <code>False</code>, Synapse falls through to the next one. The value of the first
  7626. callback that does not return <code>False</code> will be used. If this happens, Synapse will not call
  7627. any of the subsequent implementations of this callback.</p>
  7628. <h2 id="example"><a class="header" href="#example">Example</a></h2>
  7629. <p>The example below is a module that implements the spam checker callback
  7630. <code>check_event_for_spam</code> to deny any message sent by users whose Matrix user IDs are
  7631. mentioned in a configured list, and registers a web resource to the path
  7632. <code>/_synapse/client/list_spam_checker/is_evil</code> that returns a JSON object indicating
  7633. whether the provided user appears in that list.</p>
  7634. <pre><code class="language-python">import json
  7635. from typing import Union
  7636. from twisted.web.resource import Resource
  7637. from twisted.web.server import Request
  7638. from synapse.module_api import ModuleApi
  7639. class IsUserEvilResource(Resource):
  7640. def __init__(self, config):
  7641. super(IsUserEvilResource, self).__init__()
  7642. self.evil_users = config.get(&quot;evil_users&quot;) or []
  7643. def render_GET(self, request: Request):
  7644. user = request.args.get(b&quot;user&quot;)[0].decode()
  7645. request.setHeader(b&quot;Content-Type&quot;, b&quot;application/json&quot;)
  7646. return json.dumps({&quot;evil&quot;: user in self.evil_users}).encode()
  7647. class ListSpamChecker:
  7648. def __init__(self, config: dict, api: ModuleApi):
  7649. self.api = api
  7650. self.evil_users = config.get(&quot;evil_users&quot;) or []
  7651. self.api.register_spam_checker_callbacks(
  7652. check_event_for_spam=self.check_event_for_spam,
  7653. )
  7654. self.api.register_web_resource(
  7655. path=&quot;/_synapse/client/list_spam_checker/is_evil&quot;,
  7656. resource=IsUserEvilResource(config),
  7657. )
  7658. async def check_event_for_spam(self, event: &quot;synapse.events.EventBase&quot;) -&gt; Union[bool, str]:
  7659. return event.sender not in self.evil_users
  7660. </code></pre>
  7661. <div style="break-before: page; page-break-before: always;"></div><h1 id="third-party-rules-callbacks"><a class="header" href="#third-party-rules-callbacks">Third party rules callbacks</a></h1>
  7662. <p>Third party rules callbacks allow module developers to add extra checks to verify the
  7663. validity of incoming events. Third party event rules callbacks can be registered using
  7664. the module API's <code>register_third_party_rules_callbacks</code> method.</p>
  7665. <h2 id="callbacks-1"><a class="header" href="#callbacks-1">Callbacks</a></h2>
  7666. <p>The available third party rules callbacks are:</p>
  7667. <h3 id="check_event_allowed"><a class="header" href="#check_event_allowed"><code>check_event_allowed</code></a></h3>
  7668. <p><em>First introduced in Synapse v1.39.0</em></p>
  7669. <pre><code class="language-python">async def check_event_allowed(
  7670. event: &quot;synapse.events.EventBase&quot;,
  7671. state_events: &quot;synapse.types.StateMap&quot;,
  7672. ) -&gt; Tuple[bool, Optional[dict]]
  7673. </code></pre>
  7674. <p><strong><span style="color:red">
  7675. This callback is very experimental and can and will break without notice. Module developers
  7676. are encouraged to implement <code>check_event_for_spam</code> from the spam checker category instead.
  7677. </span></strong></p>
  7678. <p>Called when processing any incoming event, with the event and a <code>StateMap</code>
  7679. representing the current state of the room the event is being sent into. A <code>StateMap</code> is
  7680. a dictionary that maps tuples containing an event type and a state key to the
  7681. corresponding state event. For example retrieving the room's <code>m.room.create</code> event from
  7682. the <code>state_events</code> argument would look like this: <code>state_events.get((&quot;m.room.create&quot;, &quot;&quot;))</code>.
  7683. The module must return a boolean indicating whether the event can be allowed.</p>
  7684. <p>Note that this callback function processes incoming events coming via federation
  7685. traffic (on top of client traffic). This means denying an event might cause the local
  7686. copy of the room's history to diverge from that of remote servers. This may cause
  7687. federation issues in the room. It is strongly recommended to only deny events using this
  7688. callback function if the sender is a local user, or in a private federation in which all
  7689. servers are using the same module, with the same configuration.</p>
  7690. <p>If the boolean returned by the module is <code>True</code>, it may also tell Synapse to replace the
  7691. event with new data by returning the new event's data as a dictionary. In order to do
  7692. that, it is recommended the module calls <code>event.get_dict()</code> to get the current event as a
  7693. dictionary, and modify the returned dictionary accordingly.</p>
  7694. <p>If <code>check_event_allowed</code> raises an exception, the module is assumed to have failed.
  7695. The event will not be accepted but is not treated as explicitly rejected, either.
  7696. An HTTP request causing the module check will likely result in a 500 Internal
  7697. Server Error.</p>
  7698. <p>When the boolean returned by the module is <code>False</code>, the event is rejected.
  7699. (Module developers should not use exceptions for rejection.)</p>
  7700. <p>Note that replacing the event only works for events sent by local users, not for events
  7701. received over federation.</p>
  7702. <p>If multiple modules implement this callback, they will be considered in order. If a
  7703. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7704. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7705. any of the subsequent implementations of this callback.</p>
  7706. <h3 id="on_create_room"><a class="header" href="#on_create_room"><code>on_create_room</code></a></h3>
  7707. <p><em>First introduced in Synapse v1.39.0</em></p>
  7708. <pre><code class="language-python">async def on_create_room(
  7709. requester: &quot;synapse.types.Requester&quot;,
  7710. request_content: dict,
  7711. is_requester_admin: bool,
  7712. ) -&gt; None
  7713. </code></pre>
  7714. <p>Called when processing a room creation request, with the <code>Requester</code> object for the user
  7715. performing the request, a dictionary representing the room creation request's JSON body
  7716. (see <a href="https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-createroom">the spec</a>
  7717. for a list of possible parameters), and a boolean indicating whether the user performing
  7718. the request is a server admin.</p>
  7719. <p>Modules can modify the <code>request_content</code> (by e.g. adding events to its <code>initial_state</code>),
  7720. or deny the room's creation by raising a <code>module_api.errors.SynapseError</code>.</p>
  7721. <p>If multiple modules implement this callback, they will be considered in order. If a
  7722. callback returns without raising an exception, Synapse falls through to the next one. The
  7723. room creation will be forbidden as soon as one of the callbacks raises an exception. If
  7724. this happens, Synapse will not call any of the subsequent implementations of this
  7725. callback.</p>
  7726. <h3 id="check_threepid_can_be_invited"><a class="header" href="#check_threepid_can_be_invited"><code>check_threepid_can_be_invited</code></a></h3>
  7727. <p><em>First introduced in Synapse v1.39.0</em></p>
  7728. <pre><code class="language-python">async def check_threepid_can_be_invited(
  7729. medium: str,
  7730. address: str,
  7731. state_events: &quot;synapse.types.StateMap&quot;,
  7732. ) -&gt; bool:
  7733. </code></pre>
  7734. <p>Called when processing an invite via a third-party identifier (i.e. email or phone number).
  7735. The module must return a boolean indicating whether the invite can go through.</p>
  7736. <p>If multiple modules implement this callback, they will be considered in order. If a
  7737. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7738. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7739. any of the subsequent implementations of this callback.</p>
  7740. <h3 id="check_visibility_can_be_modified"><a class="header" href="#check_visibility_can_be_modified"><code>check_visibility_can_be_modified</code></a></h3>
  7741. <p><em>First introduced in Synapse v1.39.0</em></p>
  7742. <pre><code class="language-python">async def check_visibility_can_be_modified(
  7743. room_id: str,
  7744. state_events: &quot;synapse.types.StateMap&quot;,
  7745. new_visibility: str,
  7746. ) -&gt; bool:
  7747. </code></pre>
  7748. <p>Called when changing the visibility of a room in the local public room directory. The
  7749. visibility is a string that's either &quot;public&quot; or &quot;private&quot;. The module must return a
  7750. boolean indicating whether the change can go through.</p>
  7751. <p>If multiple modules implement this callback, they will be considered in order. If a
  7752. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  7753. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  7754. any of the subsequent implementations of this callback.</p>
  7755. <h3 id="on_new_event"><a class="header" href="#on_new_event"><code>on_new_event</code></a></h3>
  7756. <p><em>First introduced in Synapse v1.47.0</em></p>
  7757. <pre><code class="language-python">async def on_new_event(
  7758. event: &quot;synapse.events.EventBase&quot;,
  7759. state_events: &quot;synapse.types.StateMap&quot;,
  7760. ) -&gt; None:
  7761. </code></pre>
  7762. <p>Called after sending an event into a room. The module is passed the event, as well
  7763. as the state of the room <em>after</em> the event. This means that if the event is a state event,
  7764. it will be included in this state.</p>
  7765. <p>Note that this callback is called when the event has already been processed and stored
  7766. into the room, which means this callback cannot be used to deny persisting the event. To
  7767. deny an incoming event, see <a href="modules/spam_checker_callbacks.html#check_event_for_spam"><code>check_event_for_spam</code></a> instead.</p>
  7768. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  7769. <h2 id="example-1"><a class="header" href="#example-1">Example</a></h2>
  7770. <p>The example below is a module that implements the third-party rules callback
  7771. <code>check_event_allowed</code> to censor incoming messages as dictated by a third-party service.</p>
  7772. <pre><code class="language-python">from typing import Optional, Tuple
  7773. from synapse.module_api import ModuleApi
  7774. _DEFAULT_CENSOR_ENDPOINT = &quot;https://my-internal-service.local/censor-event&quot;
  7775. class EventCensorer:
  7776. def __init__(self, config: dict, api: ModuleApi):
  7777. self.api = api
  7778. self._endpoint = config.get(&quot;endpoint&quot;, _DEFAULT_CENSOR_ENDPOINT)
  7779. self.api.register_third_party_rules_callbacks(
  7780. check_event_allowed=self.check_event_allowed,
  7781. )
  7782. async def check_event_allowed(
  7783. self,
  7784. event: &quot;synapse.events.EventBase&quot;,
  7785. state_events: &quot;synapse.types.StateMap&quot;,
  7786. ) -&gt; Tuple[bool, Optional[dict]]:
  7787. event_dict = event.get_dict()
  7788. new_event_content = await self.api.http_client.post_json_get_json(
  7789. uri=self._endpoint, post_json=event_dict,
  7790. )
  7791. event_dict[&quot;content&quot;] = new_event_content
  7792. return event_dict
  7793. </code></pre>
  7794. <div style="break-before: page; page-break-before: always;"></div><h1 id="presence-router-callbacks"><a class="header" href="#presence-router-callbacks">Presence router callbacks</a></h1>
  7795. <p>Presence router callbacks allow module developers to specify additional users (local or remote)
  7796. to receive certain presence updates from local users. Presence router callbacks can be
  7797. registered using the module API's <code>register_presence_router_callbacks</code> method.</p>
  7798. <h2 id="callbacks-2"><a class="header" href="#callbacks-2">Callbacks</a></h2>
  7799. <p>The available presence router callbacks are:</p>
  7800. <h3 id="get_users_for_states"><a class="header" href="#get_users_for_states"><code>get_users_for_states</code></a></h3>
  7801. <p><em>First introduced in Synapse v1.42.0</em></p>
  7802. <pre><code class="language-python">async def get_users_for_states(
  7803. state_updates: Iterable[&quot;synapse.api.UserPresenceState&quot;],
  7804. ) -&gt; Dict[str, Set[&quot;synapse.api.UserPresenceState&quot;]]
  7805. </code></pre>
  7806. <p><strong>Requires</strong> <code>get_interested_users</code> to also be registered</p>
  7807. <p>Called when processing updates to the presence state of one or more users. This callback can
  7808. be used to instruct the server to forward that presence state to specific users. The module
  7809. must return a dictionary that maps from Matrix user IDs (which can be local or remote) to the
  7810. <code>UserPresenceState</code> changes that they should be forwarded.</p>
  7811. <p>Synapse will then attempt to send the specified presence updates to each user when possible.</p>
  7812. <p>If multiple modules implement this callback, Synapse merges all the dictionaries returned
  7813. by the callbacks. If multiple callbacks return a dictionary containing the same key,
  7814. Synapse concatenates the sets associated with this key from each dictionary. </p>
  7815. <h3 id="get_interested_users"><a class="header" href="#get_interested_users"><code>get_interested_users</code></a></h3>
  7816. <p><em>First introduced in Synapse v1.42.0</em></p>
  7817. <pre><code class="language-python">async def get_interested_users(
  7818. user_id: str
  7819. ) -&gt; Union[Set[str], &quot;synapse.module_api.PRESENCE_ALL_USERS&quot;]
  7820. </code></pre>
  7821. <p><strong>Requires</strong> <code>get_users_for_states</code> to also be registered</p>
  7822. <p>Called when determining which users someone should be able to see the presence state of. This
  7823. callback should return complementary results to <code>get_users_for_state</code> or the presence information
  7824. may not be properly forwarded.</p>
  7825. <p>The callback is given the Matrix user ID for a local user that is requesting presence data and
  7826. should return the Matrix user IDs of the users whose presence state they are allowed to
  7827. query. The returned users can be local or remote. </p>
  7828. <p>Alternatively the callback can return <code>synapse.module_api.PRESENCE_ALL_USERS</code>
  7829. to indicate that the user should receive updates from all known users.</p>
  7830. <p>If multiple modules implement this callback, they will be considered in order. Synapse
  7831. calls each callback one by one, and use a concatenation of all the <code>set</code>s returned by the
  7832. callbacks. If one callback returns <code>synapse.module_api.PRESENCE_ALL_USERS</code>, Synapse uses
  7833. this value instead. If this happens, Synapse does not call any of the subsequent
  7834. implementations of this callback.</p>
  7835. <h2 id="example-2"><a class="header" href="#example-2">Example</a></h2>
  7836. <p>The example below is a module that implements both presence router callbacks, and ensures
  7837. that <code>@alice:example.org</code> receives all presence updates from <code>@bob:example.com</code> and
  7838. <code>@charlie:somewhere.org</code>, regardless of whether Alice shares a room with any of them.</p>
  7839. <pre><code class="language-python">from typing import Dict, Iterable, Set, Union
  7840. from synapse.module_api import ModuleApi
  7841. class CustomPresenceRouter:
  7842. def __init__(self, config: dict, api: ModuleApi):
  7843. self.api = api
  7844. self.api.register_presence_router_callbacks(
  7845. get_users_for_states=self.get_users_for_states,
  7846. get_interested_users=self.get_interested_users,
  7847. )
  7848. async def get_users_for_states(
  7849. self,
  7850. state_updates: Iterable[&quot;synapse.api.UserPresenceState&quot;],
  7851. ) -&gt; Dict[str, Set[&quot;synapse.api.UserPresenceState&quot;]]:
  7852. res = {}
  7853. for update in state_updates:
  7854. if (
  7855. update.user_id == &quot;@bob:example.com&quot;
  7856. or update.user_id == &quot;@charlie:somewhere.org&quot;
  7857. ):
  7858. res.setdefault(&quot;@alice:example.com&quot;, set()).add(update)
  7859. return res
  7860. async def get_interested_users(
  7861. self,
  7862. user_id: str,
  7863. ) -&gt; Union[Set[str], &quot;synapse.module_api.PRESENCE_ALL_USERS&quot;]:
  7864. if user_id == &quot;@alice:example.com&quot;:
  7865. return {&quot;@bob:example.com&quot;, &quot;@charlie:somewhere.org&quot;}
  7866. return set()
  7867. </code></pre>
  7868. <div style="break-before: page; page-break-before: always;"></div><h1 id="account-validity-callbacks"><a class="header" href="#account-validity-callbacks">Account validity callbacks</a></h1>
  7869. <p>Account validity callbacks allow module developers to add extra steps to verify the
  7870. validity on an account, i.e. see if a user can be granted access to their account on the
  7871. Synapse instance. Account validity callbacks can be registered using the module API's
  7872. <code>register_account_validity_callbacks</code> method.</p>
  7873. <p>The available account validity callbacks are:</p>
  7874. <h3 id="is_user_expired"><a class="header" href="#is_user_expired"><code>is_user_expired</code></a></h3>
  7875. <p><em>First introduced in Synapse v1.39.0</em></p>
  7876. <pre><code class="language-python">async def is_user_expired(user: str) -&gt; Optional[bool]
  7877. </code></pre>
  7878. <p>Called when processing any authenticated request (except for logout requests). The module
  7879. can return a <code>bool</code> to indicate whether the user has expired and should be locked out of
  7880. their account, or <code>None</code> if the module wasn't able to figure it out. The user is
  7881. represented by their Matrix user ID (e.g. <code>@alice:example.com</code>).</p>
  7882. <p>If the module returns <code>True</code>, the current request will be denied with the error code
  7883. <code>ORG_MATRIX_EXPIRED_ACCOUNT</code> and the HTTP status code 403. Note that this doesn't
  7884. invalidate the user's access token.</p>
  7885. <p>If multiple modules implement this callback, they will be considered in order. If a
  7886. callback returns <code>None</code>, Synapse falls through to the next one. The value of the first
  7887. callback that does not return <code>None</code> will be used. If this happens, Synapse will not call
  7888. any of the subsequent implementations of this callback.</p>
  7889. <h3 id="on_user_registration"><a class="header" href="#on_user_registration"><code>on_user_registration</code></a></h3>
  7890. <p><em>First introduced in Synapse v1.39.0</em></p>
  7891. <pre><code class="language-python">async def on_user_registration(user: str) -&gt; None
  7892. </code></pre>
  7893. <p>Called after successfully registering a user, in case the module needs to perform extra
  7894. operations to keep track of them. (e.g. add them to a database table). The user is
  7895. represented by their Matrix user ID.</p>
  7896. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  7897. <div style="break-before: page; page-break-before: always;"></div><h1 id="password-auth-provider-callbacks"><a class="header" href="#password-auth-provider-callbacks">Password auth provider callbacks</a></h1>
  7898. <p>Password auth providers offer a way for server administrators to integrate
  7899. their Synapse installation with an external authentication system. The callbacks can be
  7900. registered by using the Module API's <code>register_password_auth_provider_callbacks</code> method.</p>
  7901. <h2 id="callbacks-3"><a class="header" href="#callbacks-3">Callbacks</a></h2>
  7902. <h3 id="auth_checkers"><a class="header" href="#auth_checkers"><code>auth_checkers</code></a></h3>
  7903. <p><em>First introduced in Synapse v1.46.0</em></p>
  7904. <pre><code class="language-python">auth_checkers: Dict[Tuple[str, Tuple[str, ...]], Callable]
  7905. </code></pre>
  7906. <p>A dict mapping from tuples of a login type identifier (such as <code>m.login.password</code>) and a
  7907. tuple of field names (such as <code>(&quot;password&quot;, &quot;secret_thing&quot;)</code>) to authentication checking
  7908. callbacks, which should be of the following form:</p>
  7909. <pre><code class="language-python">async def check_auth(
  7910. user: str,
  7911. login_type: str,
  7912. login_dict: &quot;synapse.module_api.JsonDict&quot;,
  7913. ) -&gt; Optional[
  7914. Tuple[
  7915. str,
  7916. Optional[Callable[[&quot;synapse.module_api.LoginResponse&quot;], Awaitable[None]]]
  7917. ]
  7918. ]
  7919. </code></pre>
  7920. <p>The login type and field names should be provided by the user in the
  7921. request to the <code>/login</code> API. <a href="https://matrix.org/docs/spec/client_server/latest#authentication-types">The Matrix specification</a>
  7922. defines some types, however user defined ones are also allowed.</p>
  7923. <p>The callback is passed the <code>user</code> field provided by the client (which might not be in
  7924. <code>@username:server</code> form), the login type, and a dictionary of login secrets passed by
  7925. the client.</p>
  7926. <p>If the authentication is successful, the module must return the user's Matrix ID (e.g.
  7927. <code>@alice:example.com</code>) and optionally a callback to be called with the response to the
  7928. <code>/login</code> request. If the module doesn't wish to return a callback, it must return <code>None</code>
  7929. instead.</p>
  7930. <p>If the authentication is unsuccessful, the module must return <code>None</code>.</p>
  7931. <p>If multiple modules register an auth checker for the same login type but with different
  7932. fields, Synapse will refuse to start.</p>
  7933. <p>If multiple modules register an auth checker for the same login type with the same fields,
  7934. then the callbacks will be executed in order, until one returns a Matrix User ID (and
  7935. optionally a callback). In that case, the return value of that callback will be accepted
  7936. and subsequent callbacks will not be fired. If every callback returns <code>None</code>, then the
  7937. authentication fails.</p>
  7938. <h3 id="check_3pid_auth"><a class="header" href="#check_3pid_auth"><code>check_3pid_auth</code></a></h3>
  7939. <p><em>First introduced in Synapse v1.46.0</em></p>
  7940. <pre><code class="language-python">async def check_3pid_auth(
  7941. medium: str,
  7942. address: str,
  7943. password: str,
  7944. ) -&gt; Optional[
  7945. Tuple[
  7946. str,
  7947. Optional[Callable[[&quot;synapse.module_api.LoginResponse&quot;], Awaitable[None]]]
  7948. ]
  7949. ]
  7950. </code></pre>
  7951. <p>Called when a user attempts to register or log in with a third party identifier,
  7952. such as email. It is passed the medium (eg. <code>email</code>), an address (eg. <code>jdoe@example.com</code>)
  7953. and the user's password.</p>
  7954. <p>If the authentication is successful, the module must return the user's Matrix ID (e.g.
  7955. <code>@alice:example.com</code>) and optionally a callback to be called with the response to the <code>/login</code> request.
  7956. If the module doesn't wish to return a callback, it must return None instead.</p>
  7957. <p>If the authentication is unsuccessful, the module must return <code>None</code>.</p>
  7958. <p>If multiple modules implement this callback, they will be considered in order. If a
  7959. callback returns <code>None</code>, Synapse falls through to the next one. The value of the first
  7960. callback that does not return <code>None</code> will be used. If this happens, Synapse will not call
  7961. any of the subsequent implementations of this callback. If every callback return <code>None</code>,
  7962. the authentication is denied.</p>
  7963. <h3 id="on_logged_out"><a class="header" href="#on_logged_out"><code>on_logged_out</code></a></h3>
  7964. <p><em>First introduced in Synapse v1.46.0</em></p>
  7965. <pre><code class="language-python">async def on_logged_out(
  7966. user_id: str,
  7967. device_id: Optional[str],
  7968. access_token: str
  7969. ) -&gt; None
  7970. </code></pre>
  7971. <p>Called during a logout request for a user. It is passed the qualified user ID, the ID of the
  7972. deactivated device (if any: access tokens are occasionally created without an associated
  7973. device ID), and the (now deactivated) access token.</p>
  7974. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  7975. <h2 id="example-3"><a class="header" href="#example-3">Example</a></h2>
  7976. <p>The example module below implements authentication checkers for two different login types: </p>
  7977. <ul>
  7978. <li><code>my.login.type</code>
  7979. <ul>
  7980. <li>Expects a <code>my_field</code> field to be sent to <code>/login</code></li>
  7981. <li>Is checked by the method: <code>self.check_my_login</code></li>
  7982. </ul>
  7983. </li>
  7984. <li><code>m.login.password</code> (defined in <a href="https://matrix.org/docs/spec/client_server/latest#password-based">the spec</a>)
  7985. <ul>
  7986. <li>Expects a <code>password</code> field to be sent to <code>/login</code></li>
  7987. <li>Is checked by the method: <code>self.check_pass</code> </li>
  7988. </ul>
  7989. </li>
  7990. </ul>
  7991. <pre><code class="language-python">from typing import Awaitable, Callable, Optional, Tuple
  7992. import synapse
  7993. from synapse import module_api
  7994. class MyAuthProvider:
  7995. def __init__(self, config: dict, api: module_api):
  7996. self.api = api
  7997. self.credentials = {
  7998. &quot;bob&quot;: &quot;building&quot;,
  7999. &quot;@scoop:matrix.org&quot;: &quot;digging&quot;,
  8000. }
  8001. api.register_password_auth_provider_callbacks(
  8002. auth_checkers={
  8003. (&quot;my.login_type&quot;, (&quot;my_field&quot;,)): self.check_my_login,
  8004. (&quot;m.login.password&quot;, (&quot;password&quot;,)): self.check_pass,
  8005. },
  8006. )
  8007. async def check_my_login(
  8008. self,
  8009. username: str,
  8010. login_type: str,
  8011. login_dict: &quot;synapse.module_api.JsonDict&quot;,
  8012. ) -&gt; Optional[
  8013. Tuple[
  8014. str,
  8015. Optional[Callable[[&quot;synapse.module_api.LoginResponse&quot;], Awaitable[None]]],
  8016. ]
  8017. ]:
  8018. if login_type != &quot;my.login_type&quot;:
  8019. return None
  8020. if self.credentials.get(username) == login_dict.get(&quot;my_field&quot;):
  8021. return self.api.get_qualified_user_id(username)
  8022. async def check_pass(
  8023. self,
  8024. username: str,
  8025. login_type: str,
  8026. login_dict: &quot;synapse.module_api.JsonDict&quot;,
  8027. ) -&gt; Optional[
  8028. Tuple[
  8029. str,
  8030. Optional[Callable[[&quot;synapse.module_api.LoginResponse&quot;], Awaitable[None]]],
  8031. ]
  8032. ]:
  8033. if login_type != &quot;m.login.password&quot;:
  8034. return None
  8035. if self.credentials.get(username) == login_dict.get(&quot;password&quot;):
  8036. return self.api.get_qualified_user_id(username)
  8037. </code></pre>
  8038. <div style="break-before: page; page-break-before: always;"></div><h1 id="porting-an-existing-module-that-uses-the-old-interface"><a class="header" href="#porting-an-existing-module-that-uses-the-old-interface">Porting an existing module that uses the old interface</a></h1>
  8039. <p>In order to port a module that uses Synapse's old module interface, its author needs to:</p>
  8040. <ul>
  8041. <li>ensure the module's callbacks are all asynchronous.</li>
  8042. <li>register their callbacks using one or more of the <code>register_[...]_callbacks</code> methods
  8043. from the <code>ModuleApi</code> class in the module's <code>__init__</code> method (see <a href="modules/writing_a_module.html#registering-a-callback">this section</a>
  8044. for more info).</li>
  8045. </ul>
  8046. <p>Additionally, if the module is packaged with an additional web resource, the module
  8047. should register this resource in its <code>__init__</code> method using the <code>register_web_resource</code>
  8048. method from the <code>ModuleApi</code> class (see <a href="modules/writing_a_module.html#registering-a-web-resource">this section</a> for
  8049. more info).</p>
  8050. <p>There is no longer a <code>get_db_schema_files</code> callback provided for password auth provider modules. Any
  8051. changes to the database should now be made by the module using the module API class.</p>
  8052. <p>The module's author should also update any example in the module's configuration to only
  8053. use the new <code>modules</code> section in Synapse's configuration file (see <a href="modules/index.html#using-modules">this section</a>
  8054. for more info).</p>
  8055. <div style="break-before: page; page-break-before: always;"></div><h1 id="scaling-synapse-via-workers"><a class="header" href="#scaling-synapse-via-workers">Scaling synapse via workers</a></h1>
  8056. <p>For small instances it recommended to run Synapse in the default monolith mode.
  8057. For larger instances where performance is a concern it can be helpful to split
  8058. out functionality into multiple separate python processes. These processes are
  8059. called 'workers', and are (eventually) intended to scale horizontally
  8060. independently.</p>
  8061. <p>Synapse's worker support is under active development and subject to change as
  8062. we attempt to rapidly scale ever larger Synapse instances. However we are
  8063. documenting it here to help admins needing a highly scalable Synapse instance
  8064. similar to the one running <code>matrix.org</code>.</p>
  8065. <p>All processes continue to share the same database instance, and as such,
  8066. workers only work with PostgreSQL-based Synapse deployments. SQLite should only
  8067. be used for demo purposes and any admin considering workers should already be
  8068. running PostgreSQL.</p>
  8069. <p>See also <a href="https://matrix.org/blog/2020/11/03/how-we-fixed-synapses-scalability">Matrix.org blog post</a>
  8070. for a higher level overview.</p>
  8071. <h2 id="main-processworker-communication"><a class="header" href="#main-processworker-communication">Main process/worker communication</a></h2>
  8072. <p>The processes communicate with each other via a Synapse-specific protocol called
  8073. 'replication' (analogous to MySQL- or Postgres-style database replication) which
  8074. feeds streams of newly written data between processes so they can be kept in
  8075. sync with the database state.</p>
  8076. <p>When configured to do so, Synapse uses a
  8077. <a href="https://redis.io/topics/pubsub">Redis pub/sub channel</a> to send the replication
  8078. stream between all configured Synapse processes. Additionally, processes may
  8079. make HTTP requests to each other, primarily for operations which need to wait
  8080. for a reply ─ such as sending an event.</p>
  8081. <p>Redis support was added in v1.13.0 with it becoming the recommended method in
  8082. v1.18.0. It replaced the old direct TCP connections (which is deprecated as of
  8083. v1.18.0) to the main process. With Redis, rather than all the workers connecting
  8084. to the main process, all the workers and the main process connect to Redis,
  8085. which relays replication commands between processes. This can give a significant
  8086. cpu saving on the main process and will be a prerequisite for upcoming
  8087. performance improvements.</p>
  8088. <p>If Redis support is enabled Synapse will use it as a shared cache, as well as a
  8089. pub/sub mechanism.</p>
  8090. <p>See the <a href="workers.html#architectural-diagram">Architectural diagram</a> section at the end for
  8091. a visualisation of what this looks like.</p>
  8092. <h2 id="setting-up-workers"><a class="header" href="#setting-up-workers">Setting up workers</a></h2>
  8093. <p>A Redis server is required to manage the communication between the processes.
  8094. The Redis server should be installed following the normal procedure for your
  8095. distribution (e.g. <code>apt install redis-server</code> on Debian). It is safe to use an
  8096. existing Redis deployment if you have one.</p>
  8097. <p>Once installed, check that Redis is running and accessible from the host running
  8098. Synapse, for example by executing <code>echo PING | nc -q1 localhost 6379</code> and seeing
  8099. a response of <code>+PONG</code>.</p>
  8100. <p>The appropriate dependencies must also be installed for Synapse. If using a
  8101. virtualenv, these can be installed with:</p>
  8102. <pre><code class="language-sh">pip install &quot;matrix-synapse[redis]&quot;
  8103. </code></pre>
  8104. <p>Note that these dependencies are included when synapse is installed with <code>pip install matrix-synapse[all]</code>. They are also included in the debian packages from
  8105. <code>matrix.org</code> and in the docker images at
  8106. https://hub.docker.com/r/matrixdotorg/synapse/.</p>
  8107. <p>To make effective use of the workers, you will need to configure an HTTP
  8108. reverse-proxy such as nginx or haproxy, which will direct incoming requests to
  8109. the correct worker, or to the main synapse instance. See
  8110. <a href="reverse_proxy.html">the reverse proxy documentation</a> for information on setting up a reverse
  8111. proxy.</p>
  8112. <p>When using workers, each worker process has its own configuration file which
  8113. contains settings specific to that worker, such as the HTTP listener that it
  8114. provides (if any), logging configuration, etc.</p>
  8115. <p>Normally, the worker processes are configured to read from a shared
  8116. configuration file as well as the worker-specific configuration files. This
  8117. makes it easier to keep common configuration settings synchronised across all
  8118. the processes.</p>
  8119. <p>The main process is somewhat special in this respect: it does not normally
  8120. need its own configuration file and can take all of its configuration from the
  8121. shared configuration file.</p>
  8122. <h3 id="shared-configuration"><a class="header" href="#shared-configuration">Shared configuration</a></h3>
  8123. <p>Normally, only a couple of changes are needed to make an existing configuration
  8124. file suitable for use with workers. First, you need to enable an &quot;HTTP replication
  8125. listener&quot; for the main process; and secondly, you need to enable redis-based
  8126. replication. Optionally, a shared secret can be used to authenticate HTTP
  8127. traffic between workers. For example:</p>
  8128. <pre><code class="language-yaml"># extend the existing `listeners` section. This defines the ports that the
  8129. # main process will listen on.
  8130. listeners:
  8131. # The HTTP replication port
  8132. - port: 9093
  8133. bind_address: '127.0.0.1'
  8134. type: http
  8135. resources:
  8136. - names: [replication]
  8137. # Add a random shared secret to authenticate traffic.
  8138. worker_replication_secret: &quot;&quot;
  8139. redis:
  8140. enabled: true
  8141. </code></pre>
  8142. <p>See the sample config for the full documentation of each option.</p>
  8143. <p>Under <strong>no circumstances</strong> should the replication listener be exposed to the
  8144. public internet; it has no authentication and is unencrypted.</p>
  8145. <h3 id="worker-configuration"><a class="header" href="#worker-configuration">Worker configuration</a></h3>
  8146. <p>In the config file for each worker, you must specify the type of worker
  8147. application (<code>worker_app</code>), and you should specify a unique name for the worker
  8148. (<code>worker_name</code>). The currently available worker applications are listed below.
  8149. You must also specify the HTTP replication endpoint that it should talk to on
  8150. the main synapse process. <code>worker_replication_host</code> should specify the host of
  8151. the main synapse and <code>worker_replication_http_port</code> should point to the HTTP
  8152. replication port. If the worker will handle HTTP requests then the
  8153. <code>worker_listeners</code> option should be set with a <code>http</code> listener, in the same way
  8154. as the <code>listeners</code> option in the shared config.</p>
  8155. <p>For example:</p>
  8156. <pre><code class="language-yaml">worker_app: synapse.app.generic_worker
  8157. worker_name: worker1
  8158. # The replication listener on the main synapse process.
  8159. worker_replication_host: 127.0.0.1
  8160. worker_replication_http_port: 9093
  8161. worker_listeners:
  8162. - type: http
  8163. port: 8083
  8164. resources:
  8165. - names:
  8166. - client
  8167. - federation
  8168. worker_log_config: /home/matrix/synapse/config/worker1_log_config.yaml
  8169. </code></pre>
  8170. <p>...is a full configuration for a generic worker instance, which will expose a
  8171. plain HTTP endpoint on port 8083 separately serving various endpoints, e.g.
  8172. <code>/sync</code>, which are listed below.</p>
  8173. <p>Obviously you should configure your reverse-proxy to route the relevant
  8174. endpoints to the worker (<code>localhost:8083</code> in the above example).</p>
  8175. <h3 id="running-synapse-with-workers"><a class="header" href="#running-synapse-with-workers">Running Synapse with workers</a></h3>
  8176. <p>Finally, you need to start your worker processes. This can be done with either
  8177. <code>synctl</code> or your distribution's preferred service manager such as <code>systemd</code>. We
  8178. recommend the use of <code>systemd</code> where available: for information on setting up
  8179. <code>systemd</code> to start synapse workers, see
  8180. <a href="systemd-with-workers">Systemd with Workers</a>. To use <code>synctl</code>, see
  8181. <a href="synctl_workers.html">Using synctl with Workers</a>.</p>
  8182. <h2 id="available-worker-applications"><a class="header" href="#available-worker-applications">Available worker applications</a></h2>
  8183. <h3 id="synapseappgeneric_worker"><a class="header" href="#synapseappgeneric_worker"><code>synapse.app.generic_worker</code></a></h3>
  8184. <p>This worker can handle API requests matching the following regular
  8185. expressions:</p>
  8186. <pre><code># Sync requests
  8187. ^/_matrix/client/(v2_alpha|r0)/sync$
  8188. ^/_matrix/client/(api/v1|v2_alpha|r0)/events$
  8189. ^/_matrix/client/(api/v1|r0)/initialSync$
  8190. ^/_matrix/client/(api/v1|r0)/rooms/[^/]+/initialSync$
  8191. # Federation requests
  8192. ^/_matrix/federation/v1/event/
  8193. ^/_matrix/federation/v1/state/
  8194. ^/_matrix/federation/v1/state_ids/
  8195. ^/_matrix/federation/v1/backfill/
  8196. ^/_matrix/federation/v1/get_missing_events/
  8197. ^/_matrix/federation/v1/publicRooms
  8198. ^/_matrix/federation/v1/query/
  8199. ^/_matrix/federation/v1/make_join/
  8200. ^/_matrix/federation/v1/make_leave/
  8201. ^/_matrix/federation/v1/send_join/
  8202. ^/_matrix/federation/v2/send_join/
  8203. ^/_matrix/federation/v1/send_leave/
  8204. ^/_matrix/federation/v2/send_leave/
  8205. ^/_matrix/federation/v1/invite/
  8206. ^/_matrix/federation/v2/invite/
  8207. ^/_matrix/federation/v1/query_auth/
  8208. ^/_matrix/federation/v1/event_auth/
  8209. ^/_matrix/federation/v1/exchange_third_party_invite/
  8210. ^/_matrix/federation/v1/user/devices/
  8211. ^/_matrix/federation/v1/get_groups_publicised$
  8212. ^/_matrix/key/v2/query
  8213. ^/_matrix/federation/unstable/org.matrix.msc2946/spaces/
  8214. ^/_matrix/federation/unstable/org.matrix.msc2946/hierarchy/
  8215. # Inbound federation transaction request
  8216. ^/_matrix/federation/v1/send/
  8217. # Client API requests
  8218. ^/_matrix/client/(api/v1|r0|unstable)/createRoom$
  8219. ^/_matrix/client/(api/v1|r0|unstable)/publicRooms$
  8220. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/joined_members$
  8221. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/context/.*$
  8222. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/members$
  8223. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state$
  8224. ^/_matrix/client/unstable/org.matrix.msc2946/rooms/.*/spaces$
  8225. ^/_matrix/client/unstable/org.matrix.msc2946/rooms/.*/hierarchy$
  8226. ^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary$
  8227. ^/_matrix/client/(api/v1|r0|unstable)/account/3pid$
  8228. ^/_matrix/client/(api/v1|r0|unstable)/devices$
  8229. ^/_matrix/client/(api/v1|r0|unstable)/keys/query$
  8230. ^/_matrix/client/(api/v1|r0|unstable)/keys/changes$
  8231. ^/_matrix/client/versions$
  8232. ^/_matrix/client/(api/v1|r0|unstable)/voip/turnServer$
  8233. ^/_matrix/client/(api/v1|r0|unstable)/joined_groups$
  8234. ^/_matrix/client/(api/v1|r0|unstable)/publicised_groups$
  8235. ^/_matrix/client/(api/v1|r0|unstable)/publicised_groups/
  8236. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/event/
  8237. ^/_matrix/client/(api/v1|r0|unstable)/joined_rooms$
  8238. ^/_matrix/client/(api/v1|r0|unstable)/search$
  8239. # Registration/login requests
  8240. ^/_matrix/client/(api/v1|r0|unstable)/login$
  8241. ^/_matrix/client/(r0|unstable)/register$
  8242. ^/_matrix/client/unstable/org.matrix.msc3231/register/org.matrix.msc3231.login.registration_token/validity$
  8243. # Event sending requests
  8244. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/redact
  8245. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/send
  8246. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state/
  8247. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$
  8248. ^/_matrix/client/(api/v1|r0|unstable)/join/
  8249. ^/_matrix/client/(api/v1|r0|unstable)/profile/
  8250. </code></pre>
  8251. <p>Additionally, the following REST endpoints can be handled for GET requests:</p>
  8252. <pre><code>^/_matrix/federation/v1/groups/
  8253. </code></pre>
  8254. <p>Pagination requests can also be handled, but all requests for a given
  8255. room must be routed to the same instance. Additionally, care must be taken to
  8256. ensure that the purge history admin API is not used while pagination requests
  8257. for the room are in flight:</p>
  8258. <pre><code>^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/messages$
  8259. </code></pre>
  8260. <p>Additionally, the following endpoints should be included if Synapse is configured
  8261. to use SSO (you only need to include the ones for whichever SSO provider you're
  8262. using):</p>
  8263. <pre><code># for all SSO providers
  8264. ^/_matrix/client/(api/v1|r0|unstable)/login/sso/redirect
  8265. ^/_synapse/client/pick_idp$
  8266. ^/_synapse/client/pick_username
  8267. ^/_synapse/client/new_user_consent$
  8268. ^/_synapse/client/sso_register$
  8269. # OpenID Connect requests.
  8270. ^/_synapse/client/oidc/callback$
  8271. # SAML requests.
  8272. ^/_synapse/client/saml2/authn_response$
  8273. # CAS requests.
  8274. ^/_matrix/client/(api/v1|r0|unstable)/login/cas/ticket$
  8275. </code></pre>
  8276. <p>Ensure that all SSO logins go to a single process.
  8277. For multiple workers not handling the SSO endpoints properly, see
  8278. <a href="https://github.com/matrix-org/synapse/issues/7530">#7530</a> and
  8279. <a href="https://github.com/matrix-org/synapse/issues/9427">#9427</a>.</p>
  8280. <p>Note that a HTTP listener with <code>client</code> and <code>federation</code> resources must be
  8281. configured in the <code>worker_listeners</code> option in the worker config.</p>
  8282. <h4 id="load-balancing"><a class="header" href="#load-balancing">Load balancing</a></h4>
  8283. <p>It is possible to run multiple instances of this worker app, with incoming requests
  8284. being load-balanced between them by the reverse-proxy. However, different endpoints
  8285. have different characteristics and so admins
  8286. may wish to run multiple groups of workers handling different endpoints so that
  8287. load balancing can be done in different ways.</p>
  8288. <p>For <code>/sync</code> and <code>/initialSync</code> requests it will be more efficient if all
  8289. requests from a particular user are routed to a single instance. Extracting a
  8290. user ID from the access token or <code>Authorization</code> header is currently left as an
  8291. exercise for the reader. Admins may additionally wish to separate out <code>/sync</code>
  8292. requests that have a <code>since</code> query parameter from those that don't (and
  8293. <code>/initialSync</code>), as requests that don't are known as &quot;initial sync&quot; that happens
  8294. when a user logs in on a new device and can be <em>very</em> resource intensive, so
  8295. isolating these requests will stop them from interfering with other users ongoing
  8296. syncs.</p>
  8297. <p>Federation and client requests can be balanced via simple round robin.</p>
  8298. <p>The inbound federation transaction request <code>^/_matrix/federation/v1/send/</code>
  8299. should be balanced by source IP so that transactions from the same remote server
  8300. go to the same process.</p>
  8301. <p>Registration/login requests can be handled separately purely to help ensure that
  8302. unexpected load doesn't affect new logins and sign ups.</p>
  8303. <p>Finally, event sending requests can be balanced by the room ID in the URI (or
  8304. the full URI, or even just round robin), the room ID is the path component after
  8305. <code>/rooms/</code>. If there is a large bridge connected that is sending or may send lots
  8306. of events, then a dedicated set of workers can be provisioned to limit the
  8307. effects of bursts of events from that bridge on events sent by normal users.</p>
  8308. <h4 id="stream-writers"><a class="header" href="#stream-writers">Stream writers</a></h4>
  8309. <p>Additionally, there is <em>experimental</em> support for moving writing of specific
  8310. streams (such as events) off of the main process to a particular worker. (This
  8311. is only supported with Redis-based replication.)</p>
  8312. <p>Currently supported streams are <code>events</code> and <code>typing</code>.</p>
  8313. <p>To enable this, the worker must have a HTTP replication listener configured,
  8314. have a <code>worker_name</code> and be listed in the <code>instance_map</code> config. For example to
  8315. move event persistence off to a dedicated worker, the shared configuration would
  8316. include:</p>
  8317. <pre><code class="language-yaml">instance_map:
  8318. event_persister1:
  8319. host: localhost
  8320. port: 8034
  8321. stream_writers:
  8322. events: event_persister1
  8323. </code></pre>
  8324. <p>The <code>events</code> stream also experimentally supports having multiple writers, where
  8325. work is sharded between them by room ID. Note that you <em>must</em> restart all worker
  8326. instances when adding or removing event persisters. An example <code>stream_writers</code>
  8327. configuration with multiple writers:</p>
  8328. <pre><code class="language-yaml">stream_writers:
  8329. events:
  8330. - event_persister1
  8331. - event_persister2
  8332. </code></pre>
  8333. <h4 id="background-tasks"><a class="header" href="#background-tasks">Background tasks</a></h4>
  8334. <p>There is also <em>experimental</em> support for moving background tasks to a separate
  8335. worker. Background tasks are run periodically or started via replication. Exactly
  8336. which tasks are configured to run depends on your Synapse configuration (e.g. if
  8337. stats is enabled).</p>
  8338. <p>To enable this, the worker must have a <code>worker_name</code> and can be configured to run
  8339. background tasks. For example, to move background tasks to a dedicated worker,
  8340. the shared configuration would include:</p>
  8341. <pre><code class="language-yaml">run_background_tasks_on: background_worker
  8342. </code></pre>
  8343. <p>You might also wish to investigate the <code>update_user_directory</code> and
  8344. <code>media_instance_running_background_jobs</code> settings.</p>
  8345. <h3 id="synapseapppusher"><a class="header" href="#synapseapppusher"><code>synapse.app.pusher</code></a></h3>
  8346. <p>Handles sending push notifications to sygnal and email. Doesn't handle any
  8347. REST endpoints itself, but you should set <code>start_pushers: False</code> in the
  8348. shared configuration file to stop the main synapse sending push notifications.</p>
  8349. <p>To run multiple instances at once the <code>pusher_instances</code> option should list all
  8350. pusher instances by their worker name, e.g.:</p>
  8351. <pre><code class="language-yaml">pusher_instances:
  8352. - pusher_worker1
  8353. - pusher_worker2
  8354. </code></pre>
  8355. <h3 id="synapseappappservice"><a class="header" href="#synapseappappservice"><code>synapse.app.appservice</code></a></h3>
  8356. <p>Handles sending output traffic to Application Services. Doesn't handle any
  8357. REST endpoints itself, but you should set <code>notify_appservices: False</code> in the
  8358. shared configuration file to stop the main synapse sending appservice notifications.</p>
  8359. <p>Note this worker cannot be load-balanced: only one instance should be active.</p>
  8360. <h3 id="synapseappfederation_sender"><a class="header" href="#synapseappfederation_sender"><code>synapse.app.federation_sender</code></a></h3>
  8361. <p>Handles sending federation traffic to other servers. Doesn't handle any
  8362. REST endpoints itself, but you should set <code>send_federation: False</code> in the
  8363. shared configuration file to stop the main synapse sending this traffic.</p>
  8364. <p>If running multiple federation senders then you must list each
  8365. instance in the <code>federation_sender_instances</code> option by their <code>worker_name</code>.
  8366. All instances must be stopped and started when adding or removing instances.
  8367. For example:</p>
  8368. <pre><code class="language-yaml">federation_sender_instances:
  8369. - federation_sender1
  8370. - federation_sender2
  8371. </code></pre>
  8372. <h3 id="synapseappmedia_repository"><a class="header" href="#synapseappmedia_repository"><code>synapse.app.media_repository</code></a></h3>
  8373. <p>Handles the media repository. It can handle all endpoints starting with:</p>
  8374. <pre><code>/_matrix/media/
  8375. </code></pre>
  8376. <p>... and the following regular expressions matching media-specific administration APIs:</p>
  8377. <pre><code>^/_synapse/admin/v1/purge_media_cache$
  8378. ^/_synapse/admin/v1/room/.*/media.*$
  8379. ^/_synapse/admin/v1/user/.*/media.*$
  8380. ^/_synapse/admin/v1/media/.*$
  8381. ^/_synapse/admin/v1/quarantine_media/.*$
  8382. ^/_synapse/admin/v1/users/.*/media$
  8383. </code></pre>
  8384. <p>You should also set <code>enable_media_repo: False</code> in the shared configuration
  8385. file to stop the main synapse running background jobs related to managing the
  8386. media repository. Note that doing so will prevent the main process from being
  8387. able to handle the above endpoints.</p>
  8388. <p>In the <code>media_repository</code> worker configuration file, configure the http listener to
  8389. expose the <code>media</code> resource. For example:</p>
  8390. <pre><code class="language-yaml">worker_listeners:
  8391. - type: http
  8392. port: 8085
  8393. resources:
  8394. - names:
  8395. - media
  8396. </code></pre>
  8397. <p>Note that if running multiple media repositories they must be on the same server
  8398. and you must configure a single instance to run the background tasks, e.g.:</p>
  8399. <pre><code class="language-yaml">media_instance_running_background_jobs: &quot;media-repository-1&quot;
  8400. </code></pre>
  8401. <p>Note that if a reverse proxy is used , then <code>/_matrix/media/</code> must be routed for both inbound client and federation requests (if they are handled separately).</p>
  8402. <h3 id="synapseappuser_dir"><a class="header" href="#synapseappuser_dir"><code>synapse.app.user_dir</code></a></h3>
  8403. <p>Handles searches in the user directory. It can handle REST endpoints matching
  8404. the following regular expressions:</p>
  8405. <pre><code>^/_matrix/client/(api/v1|r0|unstable)/user_directory/search$
  8406. </code></pre>
  8407. <p>When using this worker you must also set <code>update_user_directory: False</code> in the
  8408. shared configuration file to stop the main synapse running background
  8409. jobs related to updating the user directory.</p>
  8410. <h3 id="synapseappfrontend_proxy"><a class="header" href="#synapseappfrontend_proxy"><code>synapse.app.frontend_proxy</code></a></h3>
  8411. <p>Proxies some frequently-requested client endpoints to add caching and remove
  8412. load from the main synapse. It can handle REST endpoints matching the following
  8413. regular expressions:</p>
  8414. <pre><code>^/_matrix/client/(api/v1|r0|unstable)/keys/upload
  8415. </code></pre>
  8416. <p>If <code>use_presence</code> is False in the homeserver config, it can also handle REST
  8417. endpoints matching the following regular expressions:</p>
  8418. <pre><code>^/_matrix/client/(api/v1|r0|unstable)/presence/[^/]+/status
  8419. </code></pre>
  8420. <p>This &quot;stub&quot; presence handler will pass through <code>GET</code> request but make the
  8421. <code>PUT</code> effectively a no-op.</p>
  8422. <p>It will proxy any requests it cannot handle to the main synapse instance. It
  8423. must therefore be configured with the location of the main instance, via
  8424. the <code>worker_main_http_uri</code> setting in the <code>frontend_proxy</code> worker configuration
  8425. file. For example:</p>
  8426. <pre><code class="language-yaml">worker_main_http_uri: http://127.0.0.1:8008
  8427. </code></pre>
  8428. <h3 id="historical-apps"><a class="header" href="#historical-apps">Historical apps</a></h3>
  8429. <p><em>Note:</em> Historically there used to be more apps, however they have been
  8430. amalgamated into a single <code>synapse.app.generic_worker</code> app. The remaining apps
  8431. are ones that do specific processing unrelated to requests, e.g. the <code>pusher</code>
  8432. that handles sending out push notifications for new events. The intention is for
  8433. all these to be folded into the <code>generic_worker</code> app and to use config to define
  8434. which processes handle the various proccessing such as push notifications.</p>
  8435. <h2 id="migration-from-old-config"><a class="header" href="#migration-from-old-config">Migration from old config</a></h2>
  8436. <p>There are two main independent changes that have been made: introducing Redis
  8437. support and merging apps into <code>synapse.app.generic_worker</code>. Both these changes
  8438. are backwards compatible and so no changes to the config are required, however
  8439. server admins are encouraged to plan to migrate to Redis as the old style direct
  8440. TCP replication config is deprecated.</p>
  8441. <p>To migrate to Redis add the <code>redis</code> config as above, and optionally remove the
  8442. TCP <code>replication</code> listener from master and <code>worker_replication_port</code> from worker
  8443. config.</p>
  8444. <p>To migrate apps to use <code>synapse.app.generic_worker</code> simply update the
  8445. <code>worker_app</code> option in the worker configs, and where worker are started (e.g.
  8446. in systemd service files, but not required for synctl).</p>
  8447. <h2 id="architectural-diagram"><a class="header" href="#architectural-diagram">Architectural diagram</a></h2>
  8448. <p>The following shows an example setup using Redis and a reverse proxy:</p>
  8449. <pre><code> Clients &amp; Federation
  8450. |
  8451. v
  8452. +-----------+
  8453. | |
  8454. | Reverse |
  8455. | Proxy |
  8456. | |
  8457. +-----------+
  8458. | | |
  8459. | | | HTTP requests
  8460. +-------------------+ | +-----------+
  8461. | +---+ |
  8462. | | |
  8463. v v v
  8464. +--------------+ +--------------+ +--------------+ +--------------+
  8465. | Main | | Generic | | Generic | | Event |
  8466. | Process | | Worker 1 | | Worker 2 | | Persister |
  8467. +--------------+ +--------------+ +--------------+ +--------------+
  8468. ^ ^ | ^ | | ^ | ^ ^
  8469. | | | | | | | | | |
  8470. | | | | | HTTP | | | | |
  8471. | +----------+&lt;--|---|---------+ | | | |
  8472. | | +-------------|--&gt;+----------+ |
  8473. | | | |
  8474. | | | |
  8475. v v v v
  8476. ====================================================================
  8477. Redis pub/sub channel
  8478. </code></pre>
  8479. <div style="break-before: page; page-break-before: always;"></div><h3 id="using-synctl-with-workers"><a class="header" href="#using-synctl-with-workers">Using synctl with workers</a></h3>
  8480. <p>If you want to use <code>synctl</code> to manage your synapse processes, you will need to
  8481. create an an additional configuration file for the main synapse process. That
  8482. configuration should look like this:</p>
  8483. <pre><code class="language-yaml">worker_app: synapse.app.homeserver
  8484. </code></pre>
  8485. <p>Additionally, each worker app must be configured with the name of a &quot;pid file&quot;,
  8486. to which it will write its process ID when it starts. For example, for a
  8487. synchrotron, you might write:</p>
  8488. <pre><code class="language-yaml">worker_pid_file: /home/matrix/synapse/worker1.pid
  8489. </code></pre>
  8490. <p>Finally, to actually run your worker-based synapse, you must pass synctl the <code>-a</code>
  8491. commandline option to tell it to operate on all the worker configurations found
  8492. in the given directory, e.g.:</p>
  8493. <pre><code class="language-sh">synctl -a $CONFIG/workers start
  8494. </code></pre>
  8495. <p>Currently one should always restart all workers when restarting or upgrading
  8496. synapse, unless you explicitly know it's safe not to. For instance, restarting
  8497. synapse without restarting all the synchrotrons may result in broken typing
  8498. notifications.</p>
  8499. <p>To manipulate a specific worker, you pass the -w option to synctl:</p>
  8500. <pre><code class="language-sh">synctl -w $CONFIG/workers/worker1.yaml restart
  8501. </code></pre>
  8502. <div style="break-before: page; page-break-before: always;"></div><h1 id="setting-up-synapse-with-workers-and-systemd"><a class="header" href="#setting-up-synapse-with-workers-and-systemd">Setting up Synapse with Workers and Systemd</a></h1>
  8503. <p>This is a setup for managing synapse with systemd, including support for
  8504. managing workers. It provides a <code>matrix-synapse</code> service for the master, as
  8505. well as a <code>matrix-synapse-worker@</code> service template for any workers you
  8506. require. Additionally, to group the required services, it sets up a
  8507. <code>matrix-synapse.target</code>.</p>
  8508. <p>See the folder <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/system/">system</a>
  8509. for the systemd unit files.</p>
  8510. <p>The folder <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/workers/">workers</a>
  8511. contains an example configuration for the <code>federation_reader</code> worker.</p>
  8512. <h2 id="synapse-configuration-files"><a class="header" href="#synapse-configuration-files">Synapse configuration files</a></h2>
  8513. <p>See <a href="systemd-with-workers/../workers.html">the worker documentation</a> for information on how to set up the
  8514. configuration files and reverse-proxy correctly.
  8515. Below is a sample <code>federation_reader</code> worker configuration file.</p>
  8516. <pre><code class="language-yaml">worker_app: synapse.app.federation_reader
  8517. worker_name: federation_reader1
  8518. worker_replication_host: 127.0.0.1
  8519. worker_replication_http_port: 9093
  8520. worker_listeners:
  8521. - type: http
  8522. port: 8011
  8523. resources:
  8524. - names: [federation]
  8525. worker_log_config: /etc/matrix-synapse/federation-reader-log.yaml
  8526. </code></pre>
  8527. <p>Systemd manages daemonization itself, so ensure that none of the configuration
  8528. files set either <code>daemonize</code> or <code>worker_daemonize</code>.</p>
  8529. <p>The config files of all workers are expected to be located in
  8530. <code>/etc/matrix-synapse/workers</code>. If you want to use a different location, edit
  8531. the provided <code>*.service</code> files accordingly.</p>
  8532. <p>There is no need for a separate configuration file for the master process.</p>
  8533. <h2 id="set-up"><a class="header" href="#set-up">Set up</a></h2>
  8534. <ol>
  8535. <li>Adjust synapse configuration files as above.</li>
  8536. <li>Copy the <code>*.service</code> and <code>*.target</code> files in <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/system/">system</a>
  8537. to <code>/etc/systemd/system</code>.</li>
  8538. <li>Run <code>systemctl daemon-reload</code> to tell systemd to load the new unit files.</li>
  8539. <li>Run <code>systemctl enable matrix-synapse.service</code>. This will configure the
  8540. synapse master process to be started as part of the <code>matrix-synapse.target</code>
  8541. target.</li>
  8542. <li>For each worker process to be enabled, run <code>systemctl enable matrix-synapse-worker@&lt;worker_name&gt;.service</code>. For each <code>&lt;worker_name&gt;</code>, there
  8543. should be a corresponding configuration file.
  8544. <code>/etc/matrix-synapse/workers/&lt;worker_name&gt;.yaml</code>.</li>
  8545. <li>Start all the synapse processes with <code>systemctl start matrix-synapse.target</code>.</li>
  8546. <li>Tell systemd to start synapse on boot with <code>systemctl enable matrix-synapse.target</code>.</li>
  8547. </ol>
  8548. <h2 id="usage"><a class="header" href="#usage">Usage</a></h2>
  8549. <p>Once the services are correctly set up, you can use the following commands
  8550. to manage your synapse installation:</p>
  8551. <pre><code class="language-sh"># Restart Synapse master and all workers
  8552. systemctl restart matrix-synapse.target
  8553. # Stop Synapse and all workers
  8554. systemctl stop matrix-synapse.target
  8555. # Restart the master alone
  8556. systemctl start matrix-synapse.service
  8557. # Restart a specific worker (eg. federation_reader); the master is
  8558. # unaffected by this.
  8559. systemctl restart matrix-synapse-worker@federation_reader.service
  8560. # Add a new worker (assuming all configs are set up already)
  8561. systemctl enable matrix-synapse-worker@federation_writer.service
  8562. systemctl restart matrix-synapse.target
  8563. </code></pre>
  8564. <h2 id="hardening"><a class="header" href="#hardening">Hardening</a></h2>
  8565. <p><strong>Optional:</strong> If further hardening is desired, the file
  8566. <code>override-hardened.conf</code> may be copied from
  8567. <a href="https://github.com/matrix-org/synapse/tree/develop/contrib/systemd/">contrib/systemd/override-hardened.conf</a>
  8568. in this repository to the location
  8569. <code>/etc/systemd/system/matrix-synapse.service.d/override-hardened.conf</code> (the
  8570. directory may have to be created). It enables certain sandboxing features in
  8571. systemd to further secure the synapse service. You may read the comments to
  8572. understand what the override file is doing. The same file will need to be copied to
  8573. <code>/etc/systemd/system/matrix-synapse-worker@.service.d/override-hardened-worker.conf</code>
  8574. (this directory may also have to be created) in order to apply the same
  8575. hardening options to any worker processes.</p>
  8576. <p>Once these files have been copied to their appropriate locations, simply reload
  8577. systemd's manager config files and restart all Synapse services to apply the hardening options. They will automatically
  8578. be applied at every restart as long as the override files are present at the
  8579. specified locations.</p>
  8580. <pre><code class="language-sh">systemctl daemon-reload
  8581. # Restart services
  8582. systemctl restart matrix-synapse.target
  8583. </code></pre>
  8584. <p>In order to see their effect, you may run <code>systemd-analyze security matrix-synapse.service</code> before and after applying the hardening options to see
  8585. the changes being applied at a glance.</p>
  8586. <div style="break-before: page; page-break-before: always;"></div><h1 id="administration"><a class="header" href="#administration">Administration</a></h1>
  8587. <p>This section contains information on managing your Synapse homeserver. This includes:</p>
  8588. <ul>
  8589. <li>Managing users, rooms and media via the Admin API.</li>
  8590. <li>Setting up metrics and monitoring to give you insight into your homeserver's health.</li>
  8591. <li>Configuring structured logging.</li>
  8592. </ul>
  8593. <div style="break-before: page; page-break-before: always;"></div><h1 id="the-admin-api"><a class="header" href="#the-admin-api">The Admin API</a></h1>
  8594. <h2 id="authenticate-as-a-server-admin"><a class="header" href="#authenticate-as-a-server-admin">Authenticate as a server admin</a></h2>
  8595. <p>Many of the API calls in the admin api will require an <code>access_token</code> for a
  8596. server admin. (Note that a server admin is distinct from a room admin.)</p>
  8597. <p>A user can be marked as a server admin by updating the database directly, e.g.:</p>
  8598. <pre><code class="language-sql">UPDATE users SET admin = 1 WHERE name = '@foo:bar.com';
  8599. </code></pre>
  8600. <p>A new server admin user can also be created using the <code>register_new_matrix_user</code>
  8601. command. This is a script that is located in the <code>scripts/</code> directory, or possibly
  8602. already on your <code>$PATH</code> depending on how Synapse was installed.</p>
  8603. <p>Finding your user's <code>access_token</code> is client-dependent, but will usually be shown in the client's settings.</p>
  8604. <h2 id="making-an-admin-api-request"><a class="header" href="#making-an-admin-api-request">Making an Admin API request</a></h2>
  8605. <p>Once you have your <code>access_token</code>, you will need to authenticate each request to an Admin API endpoint by
  8606. providing the token as either a query parameter or a request header. To add it as a request header in cURL:</p>
  8607. <pre><code class="language-sh">curl --header &quot;Authorization: Bearer &lt;access_token&gt;&quot; &lt;the_rest_of_your_API_request&gt;
  8608. </code></pre>
  8609. <p>For more details on access tokens in Matrix, please refer to the complete
  8610. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#using-access-tokens">matrix spec documentation</a>.</p>
  8611. <div style="break-before: page; page-break-before: always;"></div><h1 id="account-validity-api"><a class="header" href="#account-validity-api">Account validity API</a></h1>
  8612. <p>This API allows a server administrator to manage the validity of an account. To
  8613. use it, you must enable the account validity feature (under
  8614. <code>account_validity</code>) in Synapse's configuration.</p>
  8615. <h2 id="renew-account"><a class="header" href="#renew-account">Renew account</a></h2>
  8616. <p>This API extends the validity of an account by as much time as configured in the
  8617. <code>period</code> parameter from the <code>account_validity</code> configuration.</p>
  8618. <p>The API is:</p>
  8619. <pre><code>POST /_synapse/admin/v1/account_validity/validity
  8620. </code></pre>
  8621. <p>with the following body:</p>
  8622. <pre><code class="language-json">{
  8623. &quot;user_id&quot;: &quot;&lt;user ID for the account to renew&gt;&quot;,
  8624. &quot;expiration_ts&quot;: 0,
  8625. &quot;enable_renewal_emails&quot;: true
  8626. }
  8627. </code></pre>
  8628. <p><code>expiration_ts</code> is an optional parameter and overrides the expiration date,
  8629. which otherwise defaults to now + validity period.</p>
  8630. <p><code>enable_renewal_emails</code> is also an optional parameter and enables/disables
  8631. sending renewal emails to the user. Defaults to true.</p>
  8632. <p>The API returns with the new expiration date for this account, as a timestamp in
  8633. milliseconds since epoch:</p>
  8634. <pre><code class="language-json">{
  8635. &quot;expiration_ts&quot;: 0
  8636. }
  8637. </code></pre>
  8638. <div style="break-before: page; page-break-before: always;"></div><h1 id="background-updates-api"><a class="header" href="#background-updates-api">Background Updates API</a></h1>
  8639. <p>This API allows a server administrator to manage the background updates being
  8640. run against the database.</p>
  8641. <h2 id="status"><a class="header" href="#status">Status</a></h2>
  8642. <p>This API gets the current status of the background updates.</p>
  8643. <p>The API is:</p>
  8644. <pre><code>GET /_synapse/admin/v1/background_updates/status
  8645. </code></pre>
  8646. <p>Returning:</p>
  8647. <pre><code class="language-json">{
  8648. &quot;enabled&quot;: true,
  8649. &quot;current_updates&quot;: {
  8650. &quot;&lt;db_name&gt;&quot;: {
  8651. &quot;name&quot;: &quot;&lt;background_update_name&gt;&quot;,
  8652. &quot;total_item_count&quot;: 50,
  8653. &quot;total_duration_ms&quot;: 10000.0,
  8654. &quot;average_items_per_ms&quot;: 2.2,
  8655. },
  8656. }
  8657. }
  8658. </code></pre>
  8659. <p><code>enabled</code> whether the background updates are enabled or disabled.</p>
  8660. <p><code>db_name</code> the database name (usually Synapse is configured with a single database named 'master').</p>
  8661. <p>For each update:</p>
  8662. <p><code>name</code> the name of the update.
  8663. <code>total_item_count</code> total number of &quot;items&quot; processed (the meaning of 'items' depends on the update in question).
  8664. <code>total_duration_ms</code> how long the background process has been running, not including time spent sleeping.
  8665. <code>average_items_per_ms</code> how many items are processed per millisecond based on an exponential average.</p>
  8666. <h2 id="enabled"><a class="header" href="#enabled">Enabled</a></h2>
  8667. <p>This API allow pausing background updates.</p>
  8668. <p>Background updates should <em>not</em> be paused for significant periods of time, as
  8669. this can affect the performance of Synapse.</p>
  8670. <p><em>Note</em>: This won't persist over restarts.</p>
  8671. <p><em>Note</em>: This won't cancel any update query that is currently running. This is
  8672. usually fine since most queries are short lived, except for <code>CREATE INDEX</code>
  8673. background updates which won't be cancelled once started.</p>
  8674. <p>The API is:</p>
  8675. <pre><code>POST /_synapse/admin/v1/background_updates/enabled
  8676. </code></pre>
  8677. <p>with the following body:</p>
  8678. <pre><code class="language-json">{
  8679. &quot;enabled&quot;: false
  8680. }
  8681. </code></pre>
  8682. <p><code>enabled</code> sets whether the background updates are enabled or disabled.</p>
  8683. <p>The API returns the <code>enabled</code> param.</p>
  8684. <pre><code class="language-json">{
  8685. &quot;enabled&quot;: false
  8686. }
  8687. </code></pre>
  8688. <p>There is also a <code>GET</code> version which returns the <code>enabled</code> state.</p>
  8689. <div style="break-before: page; page-break-before: always;"></div><h1 id="delete-a-local-group"><a class="header" href="#delete-a-local-group">Delete a local group</a></h1>
  8690. <p>This API lets a server admin delete a local group. Doing so will kick all
  8691. users out of the group so that their clients will correctly handle the group
  8692. being deleted.</p>
  8693. <p>The API is:</p>
  8694. <pre><code>POST /_synapse/admin/v1/delete_group/&lt;group_id&gt;
  8695. </code></pre>
  8696. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8697. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8698. <div style="break-before: page; page-break-before: always;"></div><h1 id="show-reported-events"><a class="header" href="#show-reported-events">Show reported events</a></h1>
  8699. <p>This API returns information about reported events.</p>
  8700. <p>The api is:</p>
  8701. <pre><code>GET /_synapse/admin/v1/event_reports?from=0&amp;limit=10
  8702. </code></pre>
  8703. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8704. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8705. <p>It returns a JSON body like the following:</p>
  8706. <pre><code class="language-json">{
  8707. &quot;event_reports&quot;: [
  8708. {
  8709. &quot;event_id&quot;: &quot;$bNUFCwGzWca1meCGkjp-zwslF-GfVcXukvRLI1_FaVY&quot;,
  8710. &quot;id&quot;: 2,
  8711. &quot;reason&quot;: &quot;foo&quot;,
  8712. &quot;score&quot;: -100,
  8713. &quot;received_ts&quot;: 1570897107409,
  8714. &quot;canonical_alias&quot;: &quot;#alias1:matrix.org&quot;,
  8715. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  8716. &quot;name&quot;: &quot;Matrix HQ&quot;,
  8717. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  8718. &quot;user_id&quot;: &quot;@foo:matrix.org&quot;
  8719. },
  8720. {
  8721. &quot;event_id&quot;: &quot;$3IcdZsDaN_En-S1DF4EMCy3v4gNRKeOJs8W5qTOKj4I&quot;,
  8722. &quot;id&quot;: 3,
  8723. &quot;reason&quot;: &quot;bar&quot;,
  8724. &quot;score&quot;: -100,
  8725. &quot;received_ts&quot;: 1598889612059,
  8726. &quot;canonical_alias&quot;: &quot;#alias2:matrix.org&quot;,
  8727. &quot;room_id&quot;: &quot;!eGvUQuTCkHGVwNMOjv:matrix.org&quot;,
  8728. &quot;name&quot;: &quot;Your room name here&quot;,
  8729. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  8730. &quot;user_id&quot;: &quot;@bar:matrix.org&quot;
  8731. }
  8732. ],
  8733. &quot;next_token&quot;: 2,
  8734. &quot;total&quot;: 4
  8735. }
  8736. </code></pre>
  8737. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again with <code>from</code>
  8738. set to the value of <code>next_token</code>. This will return a new page.</p>
  8739. <p>If the endpoint does not return a <code>next_token</code> then there are no more reports to
  8740. paginate through.</p>
  8741. <p><strong>URL parameters:</strong></p>
  8742. <ul>
  8743. <li><code>limit</code>: integer - Is optional but is used for pagination, denoting the maximum number
  8744. of items to return in this call. Defaults to <code>100</code>.</li>
  8745. <li><code>from</code>: integer - Is optional but used for pagination, denoting the offset in the
  8746. returned results. This should be treated as an opaque value and not explicitly set to
  8747. anything other than the return value of <code>next_token</code> from a previous call. Defaults to <code>0</code>.</li>
  8748. <li><code>dir</code>: string - Direction of event report order. Whether to fetch the most recent
  8749. first (<code>b</code>) or the oldest first (<code>f</code>). Defaults to <code>b</code>.</li>
  8750. <li><code>user_id</code>: string - Is optional and filters to only return users with user IDs that
  8751. contain this value. This is the user who reported the event and wrote the reason.</li>
  8752. <li><code>room_id</code>: string - Is optional and filters to only return rooms with room IDs that
  8753. contain this value.</li>
  8754. </ul>
  8755. <p><strong>Response</strong></p>
  8756. <p>The following fields are returned in the JSON response body:</p>
  8757. <ul>
  8758. <li><code>id</code>: integer - ID of event report.</li>
  8759. <li><code>received_ts</code>: integer - The timestamp (in milliseconds since the unix epoch) when this
  8760. report was sent.</li>
  8761. <li><code>room_id</code>: string - The ID of the room in which the event being reported is located.</li>
  8762. <li><code>name</code>: string - The name of the room.</li>
  8763. <li><code>event_id</code>: string - The ID of the reported event.</li>
  8764. <li><code>user_id</code>: string - This is the user who reported the event and wrote the reason.</li>
  8765. <li><code>reason</code>: string - Comment made by the <code>user_id</code> in this report. May be blank or <code>null</code>.</li>
  8766. <li><code>score</code>: integer - Content is reported based upon a negative score, where -100 is
  8767. &quot;most offensive&quot; and 0 is &quot;inoffensive&quot;. May be <code>null</code>.</li>
  8768. <li><code>sender</code>: string - This is the ID of the user who sent the original message/event that
  8769. was reported.</li>
  8770. <li><code>canonical_alias</code>: string - The canonical alias of the room. <code>null</code> if the room does not
  8771. have a canonical alias set.</li>
  8772. <li><code>next_token</code>: integer - Indication for pagination. See above.</li>
  8773. <li><code>total</code>: integer - Total number of event reports related to the query
  8774. (<code>user_id</code> and <code>room_id</code>).</li>
  8775. </ul>
  8776. <h1 id="show-details-of-a-specific-event-report"><a class="header" href="#show-details-of-a-specific-event-report">Show details of a specific event report</a></h1>
  8777. <p>This API returns information about a specific event report.</p>
  8778. <p>The api is:</p>
  8779. <pre><code>GET /_synapse/admin/v1/event_reports/&lt;report_id&gt;
  8780. </code></pre>
  8781. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8782. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8783. <p>It returns a JSON body like the following:</p>
  8784. <pre><code class="language-json">{
  8785. &quot;event_id&quot;: &quot;$bNUFCwGzWca1meCGkjp-zwslF-GfVcXukvRLI1_FaVY&quot;,
  8786. &quot;event_json&quot;: {
  8787. &quot;auth_events&quot;: [
  8788. &quot;$YK4arsKKcc0LRoe700pS8DSjOvUT4NDv0HfInlMFw2M&quot;,
  8789. &quot;$oggsNXxzPFRE3y53SUNd7nsj69-QzKv03a1RucHu-ws&quot;
  8790. ],
  8791. &quot;content&quot;: {
  8792. &quot;body&quot;: &quot;matrix.org: This Week in Matrix&quot;,
  8793. &quot;format&quot;: &quot;org.matrix.custom.html&quot;,
  8794. &quot;formatted_body&quot;: &quot;&lt;strong&gt;matrix.org&lt;/strong&gt;:&lt;br&gt;&lt;a href=\&quot;https://matrix.org/blog/\&quot;&gt;&lt;strong&gt;This Week in Matrix&lt;/strong&gt;&lt;/a&gt;&quot;,
  8795. &quot;msgtype&quot;: &quot;m.notice&quot;
  8796. },
  8797. &quot;depth&quot;: 546,
  8798. &quot;hashes&quot;: {
  8799. &quot;sha256&quot;: &quot;xK1//xnmvHJIOvbgXlkI8eEqdvoMmihVDJ9J4SNlsAw&quot;
  8800. },
  8801. &quot;origin&quot;: &quot;matrix.org&quot;,
  8802. &quot;origin_server_ts&quot;: 1592291711430,
  8803. &quot;prev_events&quot;: [
  8804. &quot;$YK4arsKKcc0LRoe700pS8DSjOvUT4NDv0HfInlMFw2M&quot;
  8805. ],
  8806. &quot;prev_state&quot;: [],
  8807. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  8808. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  8809. &quot;signatures&quot;: {
  8810. &quot;matrix.org&quot;: {
  8811. &quot;ed25519:a_JaEG&quot;: &quot;cs+OUKW/iHx5pEidbWxh0UiNNHwe46Ai9LwNz+Ah16aWDNszVIe2gaAcVZfvNsBhakQTew51tlKmL2kspXk/Dg&quot;
  8812. }
  8813. },
  8814. &quot;type&quot;: &quot;m.room.message&quot;,
  8815. &quot;unsigned&quot;: {
  8816. &quot;age_ts&quot;: 1592291711430
  8817. }
  8818. },
  8819. &quot;id&quot;: &lt;report_id&gt;,
  8820. &quot;reason&quot;: &quot;foo&quot;,
  8821. &quot;score&quot;: -100,
  8822. &quot;received_ts&quot;: 1570897107409,
  8823. &quot;canonical_alias&quot;: &quot;#alias1:matrix.org&quot;,
  8824. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  8825. &quot;name&quot;: &quot;Matrix HQ&quot;,
  8826. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  8827. &quot;user_id&quot;: &quot;@foo:matrix.org&quot;
  8828. }
  8829. </code></pre>
  8830. <p><strong>URL parameters:</strong></p>
  8831. <ul>
  8832. <li><code>report_id</code>: string - The ID of the event report.</li>
  8833. </ul>
  8834. <p><strong>Response</strong></p>
  8835. <p>The following fields are returned in the JSON response body:</p>
  8836. <ul>
  8837. <li><code>id</code>: integer - ID of event report.</li>
  8838. <li><code>received_ts</code>: integer - The timestamp (in milliseconds since the unix epoch) when this
  8839. report was sent.</li>
  8840. <li><code>room_id</code>: string - The ID of the room in which the event being reported is located.</li>
  8841. <li><code>name</code>: string - The name of the room.</li>
  8842. <li><code>event_id</code>: string - The ID of the reported event.</li>
  8843. <li><code>user_id</code>: string - This is the user who reported the event and wrote the reason.</li>
  8844. <li><code>reason</code>: string - Comment made by the <code>user_id</code> in this report. May be blank.</li>
  8845. <li><code>score</code>: integer - Content is reported based upon a negative score, where -100 is
  8846. &quot;most offensive&quot; and 0 is &quot;inoffensive&quot;.</li>
  8847. <li><code>sender</code>: string - This is the ID of the user who sent the original message/event that
  8848. was reported.</li>
  8849. <li><code>canonical_alias</code>: string - The canonical alias of the room. <code>null</code> if the room does not
  8850. have a canonical alias set.</li>
  8851. <li><code>event_json</code>: object - Details of the original event that was reported.</li>
  8852. </ul>
  8853. <div style="break-before: page; page-break-before: always;"></div><h1 id="contents-1"><a class="header" href="#contents-1">Contents</a></h1>
  8854. <ul>
  8855. <li><a href="admin_api/media_admin_api.html#querying-media">Querying media</a>
  8856. <ul>
  8857. <li><a href="admin_api/media_admin_api.html#list-all-media-in-a-room">List all media in a room</a></li>
  8858. <li><a href="admin_api/media_admin_api.html#list-all-media-uploaded-by-a-user">List all media uploaded by a user</a></li>
  8859. </ul>
  8860. </li>
  8861. <li><a href="admin_api/media_admin_api.html#quarantine-media">Quarantine media</a>
  8862. <ul>
  8863. <li><a href="admin_api/media_admin_api.html#quarantining-media-by-id">Quarantining media by ID</a></li>
  8864. <li><a href="admin_api/media_admin_api.html#remove-media-from-quarantine-by-id">Remove media from quarantine by ID</a></li>
  8865. <li><a href="admin_api/media_admin_api.html#quarantining-media-in-a-room">Quarantining media in a room</a></li>
  8866. <li><a href="admin_api/media_admin_api.html#quarantining-all-media-of-a-user">Quarantining all media of a user</a></li>
  8867. <li><a href="admin_api/media_admin_api.html#protecting-media-from-being-quarantined">Protecting media from being quarantined</a></li>
  8868. <li><a href="admin_api/media_admin_api.html#unprotecting-media-from-being-quarantined">Unprotecting media from being quarantined</a></li>
  8869. </ul>
  8870. </li>
  8871. <li><a href="admin_api/media_admin_api.html#delete-local-media">Delete local media</a>
  8872. <ul>
  8873. <li><a href="admin_api/media_admin_api.html#delete-a-specific-local-media">Delete a specific local media</a></li>
  8874. <li><a href="admin_api/media_admin_api.html#delete-local-media-by-date-or-size">Delete local media by date or size</a></li>
  8875. <li><a href="admin_api/media_admin_api.html#delete-media-uploaded-by-a-user">Delete media uploaded by a user</a></li>
  8876. </ul>
  8877. </li>
  8878. <li><a href="admin_api/media_admin_api.html#purge-remote-media-api">Purge Remote Media API</a></li>
  8879. </ul>
  8880. <h1 id="querying-media"><a class="header" href="#querying-media">Querying media</a></h1>
  8881. <p>These APIs allow extracting media information from the homeserver.</p>
  8882. <h2 id="list-all-media-in-a-room"><a class="header" href="#list-all-media-in-a-room">List all media in a room</a></h2>
  8883. <p>This API gets a list of known media in a room.
  8884. However, it only shows media from unencrypted events or rooms.</p>
  8885. <p>The API is:</p>
  8886. <pre><code>GET /_synapse/admin/v1/room/&lt;room_id&gt;/media
  8887. </code></pre>
  8888. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8889. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8890. <p>The API returns a JSON body like the following:</p>
  8891. <pre><code class="language-json">{
  8892. &quot;local&quot;: [
  8893. &quot;mxc://localhost/xwvutsrqponmlkjihgfedcba&quot;,
  8894. &quot;mxc://localhost/abcdefghijklmnopqrstuvwx&quot;
  8895. ],
  8896. &quot;remote&quot;: [
  8897. &quot;mxc://matrix.org/xwvutsrqponmlkjihgfedcba&quot;,
  8898. &quot;mxc://matrix.org/abcdefghijklmnopqrstuvwx&quot;
  8899. ]
  8900. }
  8901. </code></pre>
  8902. <h2 id="list-all-media-uploaded-by-a-user"><a class="header" href="#list-all-media-uploaded-by-a-user">List all media uploaded by a user</a></h2>
  8903. <p>Listing all media that has been uploaded by a local user can be achieved through
  8904. the use of the
  8905. <a href="admin_api/user_admin_api.html#list-media-uploaded-by-a-user">List media uploaded by a user</a>
  8906. Admin API.</p>
  8907. <h1 id="quarantine-media"><a class="header" href="#quarantine-media">Quarantine media</a></h1>
  8908. <p>Quarantining media means that it is marked as inaccessible by users. It applies
  8909. to any local media, and any locally-cached copies of remote media.</p>
  8910. <p>The media file itself (and any thumbnails) is not deleted from the server.</p>
  8911. <h2 id="quarantining-media-by-id"><a class="header" href="#quarantining-media-by-id">Quarantining media by ID</a></h2>
  8912. <p>This API quarantines a single piece of local or remote media.</p>
  8913. <p>Request:</p>
  8914. <pre><code>POST /_synapse/admin/v1/media/quarantine/&lt;server_name&gt;/&lt;media_id&gt;
  8915. {}
  8916. </code></pre>
  8917. <p>Where <code>server_name</code> is in the form of <code>example.org</code>, and <code>media_id</code> is in the
  8918. form of <code>abcdefg12345...</code>.</p>
  8919. <p>Response:</p>
  8920. <pre><code class="language-json">{}
  8921. </code></pre>
  8922. <h2 id="remove-media-from-quarantine-by-id"><a class="header" href="#remove-media-from-quarantine-by-id">Remove media from quarantine by ID</a></h2>
  8923. <p>This API removes a single piece of local or remote media from quarantine.</p>
  8924. <p>Request:</p>
  8925. <pre><code>POST /_synapse/admin/v1/media/unquarantine/&lt;server_name&gt;/&lt;media_id&gt;
  8926. {}
  8927. </code></pre>
  8928. <p>Where <code>server_name</code> is in the form of <code>example.org</code>, and <code>media_id</code> is in the
  8929. form of <code>abcdefg12345...</code>.</p>
  8930. <p>Response:</p>
  8931. <pre><code class="language-json">{}
  8932. </code></pre>
  8933. <h2 id="quarantining-media-in-a-room"><a class="header" href="#quarantining-media-in-a-room">Quarantining media in a room</a></h2>
  8934. <p>This API quarantines all local and remote media in a room.</p>
  8935. <p>Request:</p>
  8936. <pre><code>POST /_synapse/admin/v1/room/&lt;room_id&gt;/media/quarantine
  8937. {}
  8938. </code></pre>
  8939. <p>Where <code>room_id</code> is in the form of <code>!roomid12345:example.org</code>.</p>
  8940. <p>Response:</p>
  8941. <pre><code class="language-json">{
  8942. &quot;num_quarantined&quot;: 10
  8943. }
  8944. </code></pre>
  8945. <p>The following fields are returned in the JSON response body:</p>
  8946. <ul>
  8947. <li><code>num_quarantined</code>: integer - The number of media items successfully quarantined</li>
  8948. </ul>
  8949. <p>Note that there is a legacy endpoint, <code>POST /_synapse/admin/v1/quarantine_media/&lt;room_id&gt;</code>, that operates the same.
  8950. However, it is deprecated and may be removed in a future release.</p>
  8951. <h2 id="quarantining-all-media-of-a-user"><a class="header" href="#quarantining-all-media-of-a-user">Quarantining all media of a user</a></h2>
  8952. <p>This API quarantines all <em>local</em> media that a <em>local</em> user has uploaded. That is to say, if
  8953. you would like to quarantine media uploaded by a user on a remote homeserver, you should
  8954. instead use one of the other APIs.</p>
  8955. <p>Request:</p>
  8956. <pre><code>POST /_synapse/admin/v1/user/&lt;user_id&gt;/media/quarantine
  8957. {}
  8958. </code></pre>
  8959. <p>URL Parameters</p>
  8960. <ul>
  8961. <li><code>user_id</code>: string - User ID in the form of <code>@bob:example.org</code></li>
  8962. </ul>
  8963. <p>Response:</p>
  8964. <pre><code class="language-json">{
  8965. &quot;num_quarantined&quot;: 10
  8966. }
  8967. </code></pre>
  8968. <p>The following fields are returned in the JSON response body:</p>
  8969. <ul>
  8970. <li><code>num_quarantined</code>: integer - The number of media items successfully quarantined</li>
  8971. </ul>
  8972. <h2 id="protecting-media-from-being-quarantined"><a class="header" href="#protecting-media-from-being-quarantined">Protecting media from being quarantined</a></h2>
  8973. <p>This API protects a single piece of local media from being quarantined using the
  8974. above APIs. This is useful for sticker packs and other shared media which you do
  8975. not want to get quarantined, especially when
  8976. <a href="admin_api/media_admin_api.html#quarantining-media-in-a-room">quarantining media in a room</a>.</p>
  8977. <p>Request:</p>
  8978. <pre><code>POST /_synapse/admin/v1/media/protect/&lt;media_id&gt;
  8979. {}
  8980. </code></pre>
  8981. <p>Where <code>media_id</code> is in the form of <code>abcdefg12345...</code>.</p>
  8982. <p>Response:</p>
  8983. <pre><code class="language-json">{}
  8984. </code></pre>
  8985. <h2 id="unprotecting-media-from-being-quarantined"><a class="header" href="#unprotecting-media-from-being-quarantined">Unprotecting media from being quarantined</a></h2>
  8986. <p>This API reverts the protection of a media.</p>
  8987. <p>Request:</p>
  8988. <pre><code>POST /_synapse/admin/v1/media/unprotect/&lt;media_id&gt;
  8989. {}
  8990. </code></pre>
  8991. <p>Where <code>media_id</code> is in the form of <code>abcdefg12345...</code>.</p>
  8992. <p>Response:</p>
  8993. <pre><code class="language-json">{}
  8994. </code></pre>
  8995. <h1 id="delete-local-media"><a class="header" href="#delete-local-media">Delete local media</a></h1>
  8996. <p>This API deletes the <em>local</em> media from the disk of your own server.
  8997. This includes any local thumbnails and copies of media downloaded from
  8998. remote homeservers.
  8999. This API will not affect media that has been uploaded to external
  9000. media repositories (e.g https://github.com/turt2live/matrix-media-repo/).
  9001. See also <a href="admin_api/media_admin_api.html#purge-remote-media-api">Purge Remote Media API</a>.</p>
  9002. <h2 id="delete-a-specific-local-media"><a class="header" href="#delete-a-specific-local-media">Delete a specific local media</a></h2>
  9003. <p>Delete a specific <code>media_id</code>.</p>
  9004. <p>Request:</p>
  9005. <pre><code>DELETE /_synapse/admin/v1/media/&lt;server_name&gt;/&lt;media_id&gt;
  9006. {}
  9007. </code></pre>
  9008. <p>URL Parameters</p>
  9009. <ul>
  9010. <li><code>server_name</code>: string - The name of your local server (e.g <code>matrix.org</code>)</li>
  9011. <li><code>media_id</code>: string - The ID of the media (e.g <code>abcdefghijklmnopqrstuvwx</code>)</li>
  9012. </ul>
  9013. <p>Response:</p>
  9014. <pre><code class="language-json">{
  9015. &quot;deleted_media&quot;: [
  9016. &quot;abcdefghijklmnopqrstuvwx&quot;
  9017. ],
  9018. &quot;total&quot;: 1
  9019. }
  9020. </code></pre>
  9021. <p>The following fields are returned in the JSON response body:</p>
  9022. <ul>
  9023. <li><code>deleted_media</code>: an array of strings - List of deleted <code>media_id</code></li>
  9024. <li><code>total</code>: integer - Total number of deleted <code>media_id</code></li>
  9025. </ul>
  9026. <h2 id="delete-local-media-by-date-or-size"><a class="header" href="#delete-local-media-by-date-or-size">Delete local media by date or size</a></h2>
  9027. <p>Request:</p>
  9028. <pre><code>POST /_synapse/admin/v1/media/&lt;server_name&gt;/delete?before_ts=&lt;before_ts&gt;
  9029. {}
  9030. </code></pre>
  9031. <p>URL Parameters</p>
  9032. <ul>
  9033. <li><code>server_name</code>: string - The name of your local server (e.g <code>matrix.org</code>).</li>
  9034. <li><code>before_ts</code>: string representing a positive integer - Unix timestamp in milliseconds.
  9035. Files that were last used before this timestamp will be deleted. It is the timestamp of
  9036. last access, not the timestamp when the file was created.</li>
  9037. <li><code>size_gt</code>: Optional - string representing a positive integer - Size of the media in bytes.
  9038. Files that are larger will be deleted. Defaults to <code>0</code>.</li>
  9039. <li><code>keep_profiles</code>: Optional - string representing a boolean - Switch to also delete files
  9040. that are still used in image data (e.g user profile, room avatar).
  9041. If <code>false</code> these files will be deleted. Defaults to <code>true</code>.</li>
  9042. </ul>
  9043. <p>Response:</p>
  9044. <pre><code class="language-json">{
  9045. &quot;deleted_media&quot;: [
  9046. &quot;abcdefghijklmnopqrstuvwx&quot;,
  9047. &quot;abcdefghijklmnopqrstuvwz&quot;
  9048. ],
  9049. &quot;total&quot;: 2
  9050. }
  9051. </code></pre>
  9052. <p>The following fields are returned in the JSON response body:</p>
  9053. <ul>
  9054. <li><code>deleted_media</code>: an array of strings - List of deleted <code>media_id</code></li>
  9055. <li><code>total</code>: integer - Total number of deleted <code>media_id</code></li>
  9056. </ul>
  9057. <h2 id="delete-media-uploaded-by-a-user"><a class="header" href="#delete-media-uploaded-by-a-user">Delete media uploaded by a user</a></h2>
  9058. <p>You can find details of how to delete multiple media uploaded by a user in
  9059. <a href="admin_api/user_admin_api.html#delete-media-uploaded-by-a-user">User Admin API</a>.</p>
  9060. <h1 id="purge-remote-media-api"><a class="header" href="#purge-remote-media-api">Purge Remote Media API</a></h1>
  9061. <p>The purge remote media API allows server admins to purge old cached remote media.</p>
  9062. <p>The API is:</p>
  9063. <pre><code>POST /_synapse/admin/v1/purge_media_cache?before_ts=&lt;unix_timestamp_in_ms&gt;
  9064. {}
  9065. </code></pre>
  9066. <p>URL Parameters</p>
  9067. <ul>
  9068. <li><code>unix_timestamp_in_ms</code>: string representing a positive integer - Unix timestamp in milliseconds.
  9069. All cached media that was last accessed before this timestamp will be removed.</li>
  9070. </ul>
  9071. <p>Response:</p>
  9072. <pre><code class="language-json">{
  9073. &quot;deleted&quot;: 10
  9074. }
  9075. </code></pre>
  9076. <p>The following fields are returned in the JSON response body:</p>
  9077. <ul>
  9078. <li><code>deleted</code>: integer - The number of media items successfully deleted</li>
  9079. </ul>
  9080. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9081. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  9082. <p>If the user re-requests purged remote media, synapse will re-request the media
  9083. from the originating server.</p>
  9084. <div style="break-before: page; page-break-before: always;"></div><h1 id="purge-history-api"><a class="header" href="#purge-history-api">Purge History API</a></h1>
  9085. <p>The purge history API allows server admins to purge historic events from their
  9086. database, reclaiming disk space.</p>
  9087. <p>Depending on the amount of history being purged a call to the API may take
  9088. several minutes or longer. During this period users will not be able to
  9089. paginate further back in the room from the point being purged from.</p>
  9090. <p>Note that Synapse requires at least one message in each room, so it will never
  9091. delete the last message in a room.</p>
  9092. <p>The API is:</p>
  9093. <pre><code>POST /_synapse/admin/v1/purge_history/&lt;room_id&gt;[/&lt;event_id&gt;]
  9094. </code></pre>
  9095. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9096. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9097. <p>By default, events sent by local users are not deleted, as they may represent
  9098. the only copies of this content in existence. (Events sent by remote users are
  9099. deleted.)</p>
  9100. <p>Room state data (such as joins, leaves, topic) is always preserved.</p>
  9101. <p>To delete local message events as well, set <code>delete_local_events</code> in the body:</p>
  9102. <pre><code class="language-json">{
  9103. &quot;delete_local_events&quot;: true
  9104. }
  9105. </code></pre>
  9106. <p>The caller must specify the point in the room to purge up to. This can be
  9107. specified by including an event_id in the URI, or by setting a
  9108. <code>purge_up_to_event_id</code> or <code>purge_up_to_ts</code> in the request body. If an event
  9109. id is given, that event (and others at the same graph depth) will be retained.
  9110. If <code>purge_up_to_ts</code> is given, it should be a timestamp since the unix epoch,
  9111. in milliseconds.</p>
  9112. <p>The API starts the purge running, and returns immediately with a JSON body with
  9113. a purge id:</p>
  9114. <pre><code class="language-json">{
  9115. &quot;purge_id&quot;: &quot;&lt;opaque id&gt;&quot;
  9116. }
  9117. </code></pre>
  9118. <h2 id="purge-status-query"><a class="header" href="#purge-status-query">Purge status query</a></h2>
  9119. <p>It is possible to poll for updates on recent purges with a second API;</p>
  9120. <pre><code>GET /_synapse/admin/v1/purge_history_status/&lt;purge_id&gt;
  9121. </code></pre>
  9122. <p>Again, you will need to authenticate by providing an <code>access_token</code> for a
  9123. server admin.</p>
  9124. <p>This API returns a JSON body like the following:</p>
  9125. <pre><code class="language-json">{
  9126. &quot;status&quot;: &quot;active&quot;
  9127. }
  9128. </code></pre>
  9129. <p>The status will be one of <code>active</code>, <code>complete</code>, or <code>failed</code>.</p>
  9130. <h2 id="reclaim-disk-space-postgres"><a class="header" href="#reclaim-disk-space-postgres">Reclaim disk space (Postgres)</a></h2>
  9131. <p>To reclaim the disk space and return it to the operating system, you need to run
  9132. <code>VACUUM FULL;</code> on the database.</p>
  9133. <p><a href="https://www.postgresql.org/docs/current/sql-vacuum.html">https://www.postgresql.org/docs/current/sql-vacuum.html</a></p>
  9134. <div style="break-before: page; page-break-before: always;"></div><h1 id="shared-secret-registration"><a class="header" href="#shared-secret-registration">Shared-Secret Registration</a></h1>
  9135. <p>This API allows for the creation of users in an administrative and
  9136. non-interactive way. This is generally used for bootstrapping a Synapse
  9137. instance with administrator accounts.</p>
  9138. <p>To authenticate yourself to the server, you will need both the shared secret
  9139. (<code>registration_shared_secret</code> in the homeserver configuration), and a
  9140. one-time nonce. If the registration shared secret is not configured, this API
  9141. is not enabled.</p>
  9142. <p>To fetch the nonce, you need to request one from the API:</p>
  9143. <pre><code>&gt; GET /_synapse/admin/v1/register
  9144. &lt; {&quot;nonce&quot;: &quot;thisisanonce&quot;}
  9145. </code></pre>
  9146. <p>Once you have the nonce, you can make a <code>POST</code> to the same URL with a JSON
  9147. body containing the nonce, username, password, whether they are an admin
  9148. (optional, False by default), and a HMAC digest of the content. Also you can
  9149. set the displayname (optional, <code>username</code> by default).</p>
  9150. <p>As an example:</p>
  9151. <pre><code>&gt; POST /_synapse/admin/v1/register
  9152. &gt; {
  9153. &quot;nonce&quot;: &quot;thisisanonce&quot;,
  9154. &quot;username&quot;: &quot;pepper_roni&quot;,
  9155. &quot;displayname&quot;: &quot;Pepper Roni&quot;,
  9156. &quot;password&quot;: &quot;pizza&quot;,
  9157. &quot;admin&quot;: true,
  9158. &quot;mac&quot;: &quot;mac_digest_here&quot;
  9159. }
  9160. &lt; {
  9161. &quot;access_token&quot;: &quot;token_here&quot;,
  9162. &quot;user_id&quot;: &quot;@pepper_roni:localhost&quot;,
  9163. &quot;home_server&quot;: &quot;test&quot;,
  9164. &quot;device_id&quot;: &quot;device_id_here&quot;
  9165. }
  9166. </code></pre>
  9167. <p>The MAC is the hex digest output of the HMAC-SHA1 algorithm, with the key being
  9168. the shared secret and the content being the nonce, user, password, either the
  9169. string &quot;admin&quot; or &quot;notadmin&quot;, and optionally the user_type
  9170. each separated by NULs. For an example of generation in Python:</p>
  9171. <pre><code class="language-python">import hmac, hashlib
  9172. def generate_mac(nonce, user, password, admin=False, user_type=None):
  9173. mac = hmac.new(
  9174. key=shared_secret,
  9175. digestmod=hashlib.sha1,
  9176. )
  9177. mac.update(nonce.encode('utf8'))
  9178. mac.update(b&quot;\x00&quot;)
  9179. mac.update(user.encode('utf8'))
  9180. mac.update(b&quot;\x00&quot;)
  9181. mac.update(password.encode('utf8'))
  9182. mac.update(b&quot;\x00&quot;)
  9183. mac.update(b&quot;admin&quot; if admin else b&quot;notadmin&quot;)
  9184. if user_type:
  9185. mac.update(b&quot;\x00&quot;)
  9186. mac.update(user_type.encode('utf8'))
  9187. return mac.hexdigest()
  9188. </code></pre>
  9189. <div style="break-before: page; page-break-before: always;"></div><h1 id="registration-tokens"><a class="header" href="#registration-tokens">Registration Tokens</a></h1>
  9190. <p>This API allows you to manage tokens which can be used to authenticate
  9191. registration requests, as proposed in
  9192. <a href="https://github.com/matrix-org/matrix-doc/blob/main/proposals/3231-token-authenticated-registration.md">MSC3231</a>.
  9193. To use it, you will need to enable the <code>registration_requires_token</code> config
  9194. option, and authenticate by providing an <code>access_token</code> for a server admin:
  9195. see <a href="usage/administration/admin_api/../../usage/administration/admin_api">Admin API</a>.
  9196. Note that this API is still experimental; not all clients may support it yet.</p>
  9197. <h2 id="registration-token-objects"><a class="header" href="#registration-token-objects">Registration token objects</a></h2>
  9198. <p>Most endpoints make use of JSON objects that contain details about tokens.
  9199. These objects have the following fields:</p>
  9200. <ul>
  9201. <li><code>token</code>: The token which can be used to authenticate registration.</li>
  9202. <li><code>uses_allowed</code>: The number of times the token can be used to complete a
  9203. registration before it becomes invalid.</li>
  9204. <li><code>pending</code>: The number of pending uses the token has. When someone uses
  9205. the token to authenticate themselves, the pending counter is incremented
  9206. so that the token is not used more than the permitted number of times.
  9207. When the person completes registration the pending counter is decremented,
  9208. and the completed counter is incremented.</li>
  9209. <li><code>completed</code>: The number of times the token has been used to successfully
  9210. complete a registration.</li>
  9211. <li><code>expiry_time</code>: The latest time the token is valid. Given as the number of
  9212. milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch).
  9213. To convert this into a human-readable form you can remove the milliseconds
  9214. and use the <code>date</code> command. For example, <code>date -d '@1625394937'</code>.</li>
  9215. </ul>
  9216. <h2 id="list-all-tokens"><a class="header" href="#list-all-tokens">List all tokens</a></h2>
  9217. <p>Lists all tokens and details about them. If the request is successful, the top
  9218. level JSON object will have a <code>registration_tokens</code> key which is an array of
  9219. registration token objects.</p>
  9220. <pre><code>GET /_synapse/admin/v1/registration_tokens
  9221. </code></pre>
  9222. <p>Optional query parameters:</p>
  9223. <ul>
  9224. <li><code>valid</code>: <code>true</code> or <code>false</code>. If <code>true</code>, only valid tokens are returned.
  9225. If <code>false</code>, only tokens that have expired or have had all uses exhausted are
  9226. returned. If omitted, all tokens are returned regardless of validity.</li>
  9227. </ul>
  9228. <p>Example:</p>
  9229. <pre><code>GET /_synapse/admin/v1/registration_tokens
  9230. </code></pre>
  9231. <pre><code>200 OK
  9232. {
  9233. &quot;registration_tokens&quot;: [
  9234. {
  9235. &quot;token&quot;: &quot;abcd&quot;,
  9236. &quot;uses_allowed&quot;: 3,
  9237. &quot;pending&quot;: 0,
  9238. &quot;completed&quot;: 1,
  9239. &quot;expiry_time&quot;: null
  9240. },
  9241. {
  9242. &quot;token&quot;: &quot;pqrs&quot;,
  9243. &quot;uses_allowed&quot;: 2,
  9244. &quot;pending&quot;: 1,
  9245. &quot;completed&quot;: 1,
  9246. &quot;expiry_time&quot;: null
  9247. },
  9248. {
  9249. &quot;token&quot;: &quot;wxyz&quot;,
  9250. &quot;uses_allowed&quot;: null,
  9251. &quot;pending&quot;: 0,
  9252. &quot;completed&quot;: 9,
  9253. &quot;expiry_time&quot;: 1625394937000 // 2021-07-04 10:35:37 UTC
  9254. }
  9255. ]
  9256. }
  9257. </code></pre>
  9258. <p>Example using the <code>valid</code> query parameter:</p>
  9259. <pre><code>GET /_synapse/admin/v1/registration_tokens?valid=false
  9260. </code></pre>
  9261. <pre><code>200 OK
  9262. {
  9263. &quot;registration_tokens&quot;: [
  9264. {
  9265. &quot;token&quot;: &quot;pqrs&quot;,
  9266. &quot;uses_allowed&quot;: 2,
  9267. &quot;pending&quot;: 1,
  9268. &quot;completed&quot;: 1,
  9269. &quot;expiry_time&quot;: null
  9270. },
  9271. {
  9272. &quot;token&quot;: &quot;wxyz&quot;,
  9273. &quot;uses_allowed&quot;: null,
  9274. &quot;pending&quot;: 0,
  9275. &quot;completed&quot;: 9,
  9276. &quot;expiry_time&quot;: 1625394937000 // 2021-07-04 10:35:37 UTC
  9277. }
  9278. ]
  9279. }
  9280. </code></pre>
  9281. <h2 id="get-one-token"><a class="header" href="#get-one-token">Get one token</a></h2>
  9282. <p>Get details about a single token. If the request is successful, the response
  9283. body will be a registration token object.</p>
  9284. <pre><code>GET /_synapse/admin/v1/registration_tokens/&lt;token&gt;
  9285. </code></pre>
  9286. <p>Path parameters:</p>
  9287. <ul>
  9288. <li><code>token</code>: The registration token to return details of.</li>
  9289. </ul>
  9290. <p>Example:</p>
  9291. <pre><code>GET /_synapse/admin/v1/registration_tokens/abcd
  9292. </code></pre>
  9293. <pre><code>200 OK
  9294. {
  9295. &quot;token&quot;: &quot;abcd&quot;,
  9296. &quot;uses_allowed&quot;: 3,
  9297. &quot;pending&quot;: 0,
  9298. &quot;completed&quot;: 1,
  9299. &quot;expiry_time&quot;: null
  9300. }
  9301. </code></pre>
  9302. <h2 id="create-token"><a class="header" href="#create-token">Create token</a></h2>
  9303. <p>Create a new registration token. If the request is successful, the newly created
  9304. token will be returned as a registration token object in the response body.</p>
  9305. <pre><code>POST /_synapse/admin/v1/registration_tokens/new
  9306. </code></pre>
  9307. <p>The request body must be a JSON object and can contain the following fields:</p>
  9308. <ul>
  9309. <li><code>token</code>: The registration token. A string of no more than 64 characters that
  9310. consists only of characters matched by the regex <code>[A-Za-z0-9._~-]</code>.
  9311. Default: randomly generated.</li>
  9312. <li><code>uses_allowed</code>: The integer number of times the token can be used to complete
  9313. a registration before it becomes invalid.
  9314. Default: <code>null</code> (unlimited uses).</li>
  9315. <li><code>expiry_time</code>: The latest time the token is valid. Given as the number of
  9316. milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch).
  9317. You could use, for example, <code>date '+%s000' -d 'tomorrow'</code>.
  9318. Default: <code>null</code> (token does not expire).</li>
  9319. <li><code>length</code>: The length of the token randomly generated if <code>token</code> is not
  9320. specified. Must be between 1 and 64 inclusive. Default: <code>16</code>.</li>
  9321. </ul>
  9322. <p>If a field is omitted the default is used.</p>
  9323. <p>Example using defaults:</p>
  9324. <pre><code>POST /_synapse/admin/v1/registration_tokens/new
  9325. {}
  9326. </code></pre>
  9327. <pre><code>200 OK
  9328. {
  9329. &quot;token&quot;: &quot;0M-9jbkf2t_Tgiw1&quot;,
  9330. &quot;uses_allowed&quot;: null,
  9331. &quot;pending&quot;: 0,
  9332. &quot;completed&quot;: 0,
  9333. &quot;expiry_time&quot;: null
  9334. }
  9335. </code></pre>
  9336. <p>Example specifying some fields:</p>
  9337. <pre><code>POST /_synapse/admin/v1/registration_tokens/new
  9338. {
  9339. &quot;token&quot;: &quot;defg&quot;,
  9340. &quot;uses_allowed&quot;: 1
  9341. }
  9342. </code></pre>
  9343. <pre><code>200 OK
  9344. {
  9345. &quot;token&quot;: &quot;defg&quot;,
  9346. &quot;uses_allowed&quot;: 1,
  9347. &quot;pending&quot;: 0,
  9348. &quot;completed&quot;: 0,
  9349. &quot;expiry_time&quot;: null
  9350. }
  9351. </code></pre>
  9352. <h2 id="update-token"><a class="header" href="#update-token">Update token</a></h2>
  9353. <p>Update the number of allowed uses or expiry time of a token. If the request is
  9354. successful, the updated token will be returned as a registration token object
  9355. in the response body.</p>
  9356. <pre><code>PUT /_synapse/admin/v1/registration_tokens/&lt;token&gt;
  9357. </code></pre>
  9358. <p>Path parameters:</p>
  9359. <ul>
  9360. <li><code>token</code>: The registration token to update.</li>
  9361. </ul>
  9362. <p>The request body must be a JSON object and can contain the following fields:</p>
  9363. <ul>
  9364. <li><code>uses_allowed</code>: The integer number of times the token can be used to complete
  9365. a registration before it becomes invalid. By setting <code>uses_allowed</code> to <code>0</code>
  9366. the token can be easily made invalid without deleting it.
  9367. If <code>null</code> the token will have an unlimited number of uses.</li>
  9368. <li><code>expiry_time</code>: The latest time the token is valid. Given as the number of
  9369. milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch).
  9370. If <code>null</code> the token will not expire.</li>
  9371. </ul>
  9372. <p>If a field is omitted its value is not modified.</p>
  9373. <p>Example:</p>
  9374. <pre><code>PUT /_synapse/admin/v1/registration_tokens/defg
  9375. {
  9376. &quot;expiry_time&quot;: 4781243146000 // 2121-07-06 11:05:46 UTC
  9377. }
  9378. </code></pre>
  9379. <pre><code>200 OK
  9380. {
  9381. &quot;token&quot;: &quot;defg&quot;,
  9382. &quot;uses_allowed&quot;: 1,
  9383. &quot;pending&quot;: 0,
  9384. &quot;completed&quot;: 0,
  9385. &quot;expiry_time&quot;: 4781243146000
  9386. }
  9387. </code></pre>
  9388. <h2 id="delete-token"><a class="header" href="#delete-token">Delete token</a></h2>
  9389. <p>Delete a registration token. If the request is successful, the response body
  9390. will be an empty JSON object.</p>
  9391. <pre><code>DELETE /_synapse/admin/v1/registration_tokens/&lt;token&gt;
  9392. </code></pre>
  9393. <p>Path parameters:</p>
  9394. <ul>
  9395. <li><code>token</code>: The registration token to delete.</li>
  9396. </ul>
  9397. <p>Example:</p>
  9398. <pre><code>DELETE /_synapse/admin/v1/registration_tokens/wxyz
  9399. </code></pre>
  9400. <pre><code>200 OK
  9401. {}
  9402. </code></pre>
  9403. <h2 id="errors"><a class="header" href="#errors">Errors</a></h2>
  9404. <p>If a request fails a &quot;standard error response&quot; will be returned as defined in
  9405. the <a href="https://matrix.org/docs/spec/client_server/r0.6.1#api-standards">Matrix Client-Server API specification</a>.</p>
  9406. <p>For example, if the token specified in a path parameter does not exist a
  9407. <code>404 Not Found</code> error will be returned.</p>
  9408. <pre><code>GET /_synapse/admin/v1/registration_tokens/1234
  9409. </code></pre>
  9410. <pre><code>404 Not Found
  9411. {
  9412. &quot;errcode&quot;: &quot;M_NOT_FOUND&quot;,
  9413. &quot;error&quot;: &quot;No such registration token: 1234&quot;
  9414. }
  9415. </code></pre>
  9416. <div style="break-before: page; page-break-before: always;"></div><h1 id="edit-room-membership-api"><a class="header" href="#edit-room-membership-api">Edit Room Membership API</a></h1>
  9417. <p>This API allows an administrator to join an user account with a given <code>user_id</code>
  9418. to a room with a given <code>room_id_or_alias</code>. You can only modify the membership of
  9419. local users. The server administrator must be in the room and have permission to
  9420. invite users.</p>
  9421. <h2 id="parameters"><a class="header" href="#parameters">Parameters</a></h2>
  9422. <p>The following parameters are available:</p>
  9423. <ul>
  9424. <li><code>user_id</code> - Fully qualified user: for example, <code>@user:server.com</code>.</li>
  9425. <li><code>room_id_or_alias</code> - The room identifier or alias to join: for example,
  9426. <code>!636q39766251:server.com</code>.</li>
  9427. </ul>
  9428. <h2 id="usage-1"><a class="header" href="#usage-1">Usage</a></h2>
  9429. <pre><code>POST /_synapse/admin/v1/join/&lt;room_id_or_alias&gt;
  9430. {
  9431. &quot;user_id&quot;: &quot;@user:server.com&quot;
  9432. }
  9433. </code></pre>
  9434. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9435. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  9436. <p>Response:</p>
  9437. <pre><code class="language-json">{
  9438. &quot;room_id&quot;: &quot;!636q39766251:server.com&quot;
  9439. }
  9440. </code></pre>
  9441. <div style="break-before: page; page-break-before: always;"></div><h1 id="contents-2"><a class="header" href="#contents-2">Contents</a></h1>
  9442. <ul>
  9443. <li><a href="admin_api/rooms.html#list-room-api">List Room API</a></li>
  9444. <li><a href="admin_api/rooms.html#room-details-api">Room Details API</a></li>
  9445. <li><a href="admin_api/rooms.html#room-members-api">Room Members API</a></li>
  9446. <li><a href="admin_api/rooms.html#room-state-api">Room State API</a></li>
  9447. <li><a href="admin_api/rooms.html#delete-room-api">Delete Room API</a>
  9448. <ul>
  9449. <li><a href="admin_api/rooms.html#undoing-room-shutdowns">Undoing room shutdowns</a></li>
  9450. </ul>
  9451. </li>
  9452. <li><a href="admin_api/rooms.html#make-room-admin-api">Make Room Admin API</a></li>
  9453. <li><a href="admin_api/rooms.html#forward-extremities-admin-api">Forward Extremities Admin API</a></li>
  9454. <li><a href="admin_api/rooms.html#event-context-api">Event Context API</a></li>
  9455. </ul>
  9456. <h1 id="list-room-api"><a class="header" href="#list-room-api">List Room API</a></h1>
  9457. <p>The List Room admin API allows server admins to get a list of rooms on their
  9458. server. There are various parameters available that allow for filtering and
  9459. sorting the returned list. This API supports pagination.</p>
  9460. <p><strong>Parameters</strong></p>
  9461. <p>The following query parameters are available:</p>
  9462. <ul>
  9463. <li>
  9464. <p><code>from</code> - Offset in the returned list. Defaults to <code>0</code>.</p>
  9465. </li>
  9466. <li>
  9467. <p><code>limit</code> - Maximum amount of rooms to return. Defaults to <code>100</code>.</p>
  9468. </li>
  9469. <li>
  9470. <p><code>order_by</code> - The method in which to sort the returned list of rooms. Valid values are:</p>
  9471. <ul>
  9472. <li><code>alphabetical</code> - Same as <code>name</code>. This is deprecated.</li>
  9473. <li><code>size</code> - Same as <code>joined_members</code>. This is deprecated.</li>
  9474. <li><code>name</code> - Rooms are ordered alphabetically by room name. This is the default.</li>
  9475. <li><code>canonical_alias</code> - Rooms are ordered alphabetically by main alias address of the room.</li>
  9476. <li><code>joined_members</code> - Rooms are ordered by the number of members. Largest to smallest.</li>
  9477. <li><code>joined_local_members</code> - Rooms are ordered by the number of local members. Largest to smallest.</li>
  9478. <li><code>version</code> - Rooms are ordered by room version. Largest to smallest.</li>
  9479. <li><code>creator</code> - Rooms are ordered alphabetically by creator of the room.</li>
  9480. <li><code>encryption</code> - Rooms are ordered alphabetically by the end-to-end encryption algorithm.</li>
  9481. <li><code>federatable</code> - Rooms are ordered by whether the room is federatable.</li>
  9482. <li><code>public</code> - Rooms are ordered by visibility in room list.</li>
  9483. <li><code>join_rules</code> - Rooms are ordered alphabetically by join rules of the room.</li>
  9484. <li><code>guest_access</code> - Rooms are ordered alphabetically by guest access option of the room.</li>
  9485. <li><code>history_visibility</code> - Rooms are ordered alphabetically by visibility of history of the room.</li>
  9486. <li><code>state_events</code> - Rooms are ordered by number of state events. Largest to smallest.</li>
  9487. </ul>
  9488. </li>
  9489. <li>
  9490. <p><code>dir</code> - Direction of room order. Either <code>f</code> for forwards or <code>b</code> for backwards. Setting
  9491. this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</p>
  9492. </li>
  9493. <li>
  9494. <p><code>search_term</code> - Filter rooms by their room name, canonical alias and room id.
  9495. Specifically, rooms are selected if the search term is contained in</p>
  9496. <ul>
  9497. <li>the room's name,</li>
  9498. <li>the local part of the room's canonical alias, or</li>
  9499. <li>the complete (local and server part) room's id (case sensitive).</li>
  9500. </ul>
  9501. <p>Defaults to no filtering.</p>
  9502. </li>
  9503. </ul>
  9504. <p><strong>Response</strong></p>
  9505. <p>The following fields are possible in the JSON response body:</p>
  9506. <ul>
  9507. <li><code>rooms</code> - An array of objects, each containing information about a room.
  9508. <ul>
  9509. <li>Room objects contain the following fields:
  9510. <ul>
  9511. <li><code>room_id</code> - The ID of the room.</li>
  9512. <li><code>name</code> - The name of the room.</li>
  9513. <li><code>canonical_alias</code> - The canonical (main) alias address of the room.</li>
  9514. <li><code>joined_members</code> - How many users are currently in the room.</li>
  9515. <li><code>joined_local_members</code> - How many local users are currently in the room.</li>
  9516. <li><code>version</code> - The version of the room as a string.</li>
  9517. <li><code>creator</code> - The <code>user_id</code> of the room creator.</li>
  9518. <li><code>encryption</code> - Algorithm of end-to-end encryption of messages. Is <code>null</code> if encryption is not active.</li>
  9519. <li><code>federatable</code> - Whether users on other servers can join this room.</li>
  9520. <li><code>public</code> - Whether the room is visible in room directory.</li>
  9521. <li><code>join_rules</code> - The type of rules used for users wishing to join this room. One of: [&quot;public&quot;, &quot;knock&quot;, &quot;invite&quot;, &quot;private&quot;].</li>
  9522. <li><code>guest_access</code> - Whether guests can join the room. One of: [&quot;can_join&quot;, &quot;forbidden&quot;].</li>
  9523. <li><code>history_visibility</code> - Who can see the room history. One of: [&quot;invited&quot;, &quot;joined&quot;, &quot;shared&quot;, &quot;world_readable&quot;].</li>
  9524. <li><code>state_events</code> - Total number of state_events of a room. Complexity of the room.</li>
  9525. </ul>
  9526. </li>
  9527. </ul>
  9528. </li>
  9529. <li><code>offset</code> - The current pagination offset in rooms. This parameter should be
  9530. used instead of <code>next_token</code> for room offset as <code>next_token</code> is
  9531. not intended to be parsed.</li>
  9532. <li><code>total_rooms</code> - The total number of rooms this query can return. Using this
  9533. and <code>offset</code>, you have enough information to know the current
  9534. progression through the list.</li>
  9535. <li><code>next_batch</code> - If this field is present, we know that there are potentially
  9536. more rooms on the server that did not all fit into this response.
  9537. We can use <code>next_batch</code> to get the &quot;next page&quot; of results. To do
  9538. so, simply repeat your request, setting the <code>from</code> parameter to
  9539. the value of <code>next_batch</code>.</li>
  9540. <li><code>prev_batch</code> - If this field is present, it is possible to paginate backwards.
  9541. Use <code>prev_batch</code> for the <code>from</code> value in the next request to
  9542. get the &quot;previous page&quot; of results.</li>
  9543. </ul>
  9544. <p>The API is:</p>
  9545. <p>A standard request with no filtering:</p>
  9546. <pre><code>GET /_synapse/admin/v1/rooms
  9547. </code></pre>
  9548. <p>A response body like the following is returned:</p>
  9549. <pre><code class="language-json">{
  9550. &quot;rooms&quot;: [
  9551. {
  9552. &quot;room_id&quot;: &quot;!OGEhHVWSdvArJzumhm:matrix.org&quot;,
  9553. &quot;name&quot;: &quot;Matrix HQ&quot;,
  9554. &quot;canonical_alias&quot;: &quot;#matrix:matrix.org&quot;,
  9555. &quot;joined_members&quot;: 8326,
  9556. &quot;joined_local_members&quot;: 2,
  9557. &quot;version&quot;: &quot;1&quot;,
  9558. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  9559. &quot;encryption&quot;: null,
  9560. &quot;federatable&quot;: true,
  9561. &quot;public&quot;: true,
  9562. &quot;join_rules&quot;: &quot;invite&quot;,
  9563. &quot;guest_access&quot;: null,
  9564. &quot;history_visibility&quot;: &quot;shared&quot;,
  9565. &quot;state_events&quot;: 93534
  9566. },
  9567. ... (8 hidden items) ...
  9568. {
  9569. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  9570. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  9571. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  9572. &quot;joined_members&quot;: 314,
  9573. &quot;joined_local_members&quot;: 20,
  9574. &quot;version&quot;: &quot;4&quot;,
  9575. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  9576. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  9577. &quot;federatable&quot;: true,
  9578. &quot;public&quot;: false,
  9579. &quot;join_rules&quot;: &quot;invite&quot;,
  9580. &quot;guest_access&quot;: null,
  9581. &quot;history_visibility&quot;: &quot;shared&quot;,
  9582. &quot;state_events&quot;: 8345
  9583. }
  9584. ],
  9585. &quot;offset&quot;: 0,
  9586. &quot;total_rooms&quot;: 10
  9587. }
  9588. </code></pre>
  9589. <p>Filtering by room name:</p>
  9590. <pre><code>GET /_synapse/admin/v1/rooms?search_term=TWIM
  9591. </code></pre>
  9592. <p>A response body like the following is returned:</p>
  9593. <pre><code class="language-json">{
  9594. &quot;rooms&quot;: [
  9595. {
  9596. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  9597. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  9598. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  9599. &quot;joined_members&quot;: 314,
  9600. &quot;joined_local_members&quot;: 20,
  9601. &quot;version&quot;: &quot;4&quot;,
  9602. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  9603. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  9604. &quot;federatable&quot;: true,
  9605. &quot;public&quot;: false,
  9606. &quot;join_rules&quot;: &quot;invite&quot;,
  9607. &quot;guest_access&quot;: null,
  9608. &quot;history_visibility&quot;: &quot;shared&quot;,
  9609. &quot;state_events&quot;: 8
  9610. }
  9611. ],
  9612. &quot;offset&quot;: 0,
  9613. &quot;total_rooms&quot;: 1
  9614. }
  9615. </code></pre>
  9616. <p>Paginating through a list of rooms:</p>
  9617. <pre><code>GET /_synapse/admin/v1/rooms?order_by=size
  9618. </code></pre>
  9619. <p>A response body like the following is returned:</p>
  9620. <pre><code class="language-json">{
  9621. &quot;rooms&quot;: [
  9622. {
  9623. &quot;room_id&quot;: &quot;!OGEhHVWSdvArJzumhm:matrix.org&quot;,
  9624. &quot;name&quot;: &quot;Matrix HQ&quot;,
  9625. &quot;canonical_alias&quot;: &quot;#matrix:matrix.org&quot;,
  9626. &quot;joined_members&quot;: 8326,
  9627. &quot;joined_local_members&quot;: 2,
  9628. &quot;version&quot;: &quot;1&quot;,
  9629. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  9630. &quot;encryption&quot;: null,
  9631. &quot;federatable&quot;: true,
  9632. &quot;public&quot;: true,
  9633. &quot;join_rules&quot;: &quot;invite&quot;,
  9634. &quot;guest_access&quot;: null,
  9635. &quot;history_visibility&quot;: &quot;shared&quot;,
  9636. &quot;state_events&quot;: 93534
  9637. },
  9638. ... (98 hidden items) ...
  9639. {
  9640. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  9641. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  9642. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  9643. &quot;joined_members&quot;: 314,
  9644. &quot;joined_local_members&quot;: 20,
  9645. &quot;version&quot;: &quot;4&quot;,
  9646. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  9647. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  9648. &quot;federatable&quot;: true,
  9649. &quot;public&quot;: false,
  9650. &quot;join_rules&quot;: &quot;invite&quot;,
  9651. &quot;guest_access&quot;: null,
  9652. &quot;history_visibility&quot;: &quot;shared&quot;,
  9653. &quot;state_events&quot;: 8345
  9654. }
  9655. ],
  9656. &quot;offset&quot;: 0,
  9657. &quot;total_rooms&quot;: 150,
  9658. &quot;next_token&quot;: 100
  9659. }
  9660. </code></pre>
  9661. <p>The presence of the <code>next_token</code> parameter tells us that there are more rooms
  9662. than returned in this request, and we need to make another request to get them.
  9663. To get the next batch of room results, we repeat our request, setting the <code>from</code>
  9664. parameter to the value of <code>next_token</code>.</p>
  9665. <pre><code>GET /_synapse/admin/v1/rooms?order_by=size&amp;from=100
  9666. </code></pre>
  9667. <p>A response body like the following is returned:</p>
  9668. <pre><code class="language-json">{
  9669. &quot;rooms&quot;: [
  9670. {
  9671. &quot;room_id&quot;: &quot;!mscvqgqpHYjBGDxNym:matrix.org&quot;,
  9672. &quot;name&quot;: &quot;Music Theory&quot;,
  9673. &quot;canonical_alias&quot;: &quot;#musictheory:matrix.org&quot;,
  9674. &quot;joined_members&quot;: 127,
  9675. &quot;joined_local_members&quot;: 2,
  9676. &quot;version&quot;: &quot;1&quot;,
  9677. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  9678. &quot;encryption&quot;: null,
  9679. &quot;federatable&quot;: true,
  9680. &quot;public&quot;: true,
  9681. &quot;join_rules&quot;: &quot;invite&quot;,
  9682. &quot;guest_access&quot;: null,
  9683. &quot;history_visibility&quot;: &quot;shared&quot;,
  9684. &quot;state_events&quot;: 93534
  9685. },
  9686. ... (48 hidden items) ...
  9687. {
  9688. &quot;room_id&quot;: &quot;!twcBhHVdZlQWuuxBhN:termina.org.uk&quot;,
  9689. &quot;name&quot;: &quot;weechat-matrix&quot;,
  9690. &quot;canonical_alias&quot;: &quot;#weechat-matrix:termina.org.uk&quot;,
  9691. &quot;joined_members&quot;: 137,
  9692. &quot;joined_local_members&quot;: 20,
  9693. &quot;version&quot;: &quot;4&quot;,
  9694. &quot;creator&quot;: &quot;@foo:termina.org.uk&quot;,
  9695. &quot;encryption&quot;: null,
  9696. &quot;federatable&quot;: true,
  9697. &quot;public&quot;: true,
  9698. &quot;join_rules&quot;: &quot;invite&quot;,
  9699. &quot;guest_access&quot;: null,
  9700. &quot;history_visibility&quot;: &quot;shared&quot;,
  9701. &quot;state_events&quot;: 8345
  9702. }
  9703. ],
  9704. &quot;offset&quot;: 100,
  9705. &quot;prev_batch&quot;: 0,
  9706. &quot;total_rooms&quot;: 150
  9707. }
  9708. </code></pre>
  9709. <p>Once the <code>next_token</code> parameter is no longer present, we know we've reached the
  9710. end of the list.</p>
  9711. <h1 id="room-details-api"><a class="header" href="#room-details-api">Room Details API</a></h1>
  9712. <p>The Room Details admin API allows server admins to get all details of a room.</p>
  9713. <p>The following fields are possible in the JSON response body:</p>
  9714. <ul>
  9715. <li><code>room_id</code> - The ID of the room.</li>
  9716. <li><code>name</code> - The name of the room.</li>
  9717. <li><code>topic</code> - The topic of the room.</li>
  9718. <li><code>avatar</code> - The <code>mxc</code> URI to the avatar of the room.</li>
  9719. <li><code>canonical_alias</code> - The canonical (main) alias address of the room.</li>
  9720. <li><code>joined_members</code> - How many users are currently in the room.</li>
  9721. <li><code>joined_local_members</code> - How many local users are currently in the room.</li>
  9722. <li><code>joined_local_devices</code> - How many local devices are currently in the room.</li>
  9723. <li><code>version</code> - The version of the room as a string.</li>
  9724. <li><code>creator</code> - The <code>user_id</code> of the room creator.</li>
  9725. <li><code>encryption</code> - Algorithm of end-to-end encryption of messages. Is <code>null</code> if encryption is not active.</li>
  9726. <li><code>federatable</code> - Whether users on other servers can join this room.</li>
  9727. <li><code>public</code> - Whether the room is visible in room directory.</li>
  9728. <li><code>join_rules</code> - The type of rules used for users wishing to join this room. One of: [&quot;public&quot;, &quot;knock&quot;, &quot;invite&quot;, &quot;private&quot;].</li>
  9729. <li><code>guest_access</code> - Whether guests can join the room. One of: [&quot;can_join&quot;, &quot;forbidden&quot;].</li>
  9730. <li><code>history_visibility</code> - Who can see the room history. One of: [&quot;invited&quot;, &quot;joined&quot;, &quot;shared&quot;, &quot;world_readable&quot;].</li>
  9731. <li><code>state_events</code> - Total number of state_events of a room. Complexity of the room.</li>
  9732. </ul>
  9733. <p>The API is:</p>
  9734. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;
  9735. </code></pre>
  9736. <p>A response body like the following is returned:</p>
  9737. <pre><code class="language-json">{
  9738. &quot;room_id&quot;: &quot;!mscvqgqpHYjBGDxNym:matrix.org&quot;,
  9739. &quot;name&quot;: &quot;Music Theory&quot;,
  9740. &quot;avatar&quot;: &quot;mxc://matrix.org/AQDaVFlbkQoErdOgqWRgiGSV&quot;,
  9741. &quot;topic&quot;: &quot;Theory, Composition, Notation, Analysis&quot;,
  9742. &quot;canonical_alias&quot;: &quot;#musictheory:matrix.org&quot;,
  9743. &quot;joined_members&quot;: 127,
  9744. &quot;joined_local_members&quot;: 2,
  9745. &quot;joined_local_devices&quot;: 2,
  9746. &quot;version&quot;: &quot;1&quot;,
  9747. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  9748. &quot;encryption&quot;: null,
  9749. &quot;federatable&quot;: true,
  9750. &quot;public&quot;: true,
  9751. &quot;join_rules&quot;: &quot;invite&quot;,
  9752. &quot;guest_access&quot;: null,
  9753. &quot;history_visibility&quot;: &quot;shared&quot;,
  9754. &quot;state_events&quot;: 93534
  9755. }
  9756. </code></pre>
  9757. <h1 id="room-members-api"><a class="header" href="#room-members-api">Room Members API</a></h1>
  9758. <p>The Room Members admin API allows server admins to get a list of all members of a room.</p>
  9759. <p>The response includes the following fields:</p>
  9760. <ul>
  9761. <li><code>members</code> - A list of all the members that are present in the room, represented by their ids.</li>
  9762. <li><code>total</code> - Total number of members in the room.</li>
  9763. </ul>
  9764. <p>The API is:</p>
  9765. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/members
  9766. </code></pre>
  9767. <p>A response body like the following is returned:</p>
  9768. <pre><code class="language-json">{
  9769. &quot;members&quot;: [
  9770. &quot;@foo:matrix.org&quot;,
  9771. &quot;@bar:matrix.org&quot;,
  9772. &quot;@foobar:matrix.org&quot;
  9773. ],
  9774. &quot;total&quot;: 3
  9775. }
  9776. </code></pre>
  9777. <h1 id="room-state-api"><a class="header" href="#room-state-api">Room State API</a></h1>
  9778. <p>The Room State admin API allows server admins to get a list of all state events in a room.</p>
  9779. <p>The response includes the following fields:</p>
  9780. <ul>
  9781. <li><code>state</code> - The current state of the room at the time of request.</li>
  9782. </ul>
  9783. <p>The API is:</p>
  9784. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/state
  9785. </code></pre>
  9786. <p>A response body like the following is returned:</p>
  9787. <pre><code class="language-json">{
  9788. &quot;state&quot;: [
  9789. {&quot;type&quot;: &quot;m.room.create&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true},
  9790. {&quot;type&quot;: &quot;m.room.power_levels&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true},
  9791. {&quot;type&quot;: &quot;m.room.name&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true}
  9792. ]
  9793. }
  9794. </code></pre>
  9795. <h1 id="delete-room-api"><a class="header" href="#delete-room-api">Delete Room API</a></h1>
  9796. <p>The Delete Room admin API allows server admins to remove rooms from the server
  9797. and block these rooms.</p>
  9798. <p>Shuts down a room. Moves all local users and room aliases automatically to a
  9799. new room if <code>new_room_user_id</code> is set. Otherwise local users only
  9800. leave the room without any information.</p>
  9801. <p>The new room will be created with the user specified by the <code>new_room_user_id</code> parameter
  9802. as room administrator and will contain a message explaining what happened. Users invited
  9803. to the new room will have power level <code>-10</code> by default, and thus be unable to speak.</p>
  9804. <p>If <code>block</code> is <code>True</code> it prevents new joins to the old room.</p>
  9805. <p>This API will remove all trace of the old room from your database after removing
  9806. all local users. If <code>purge</code> is <code>true</code> (the default), all traces of the old room will
  9807. be removed from your database after removing all local users. If you do not want
  9808. this to happen, set <code>purge</code> to <code>false</code>.
  9809. Depending on the amount of history being purged a call to the API may take
  9810. several minutes or longer.</p>
  9811. <p>The local server will only have the power to move local user and room aliases to
  9812. the new room. Users on other servers will be unaffected.</p>
  9813. <p>The API is:</p>
  9814. <pre><code>DELETE /_synapse/admin/v1/rooms/&lt;room_id&gt;
  9815. </code></pre>
  9816. <p>with a body of:</p>
  9817. <pre><code class="language-json">{
  9818. &quot;new_room_user_id&quot;: &quot;@someuser:example.com&quot;,
  9819. &quot;room_name&quot;: &quot;Content Violation Notification&quot;,
  9820. &quot;message&quot;: &quot;Bad Room has been shutdown due to content violations on this server. Please review our Terms of Service.&quot;,
  9821. &quot;block&quot;: true,
  9822. &quot;purge&quot;: true
  9823. }
  9824. </code></pre>
  9825. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9826. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  9827. <p>A response body like the following is returned:</p>
  9828. <pre><code class="language-json">{
  9829. &quot;kicked_users&quot;: [
  9830. &quot;@foobar:example.com&quot;
  9831. ],
  9832. &quot;failed_to_kick_users&quot;: [],
  9833. &quot;local_aliases&quot;: [
  9834. &quot;#badroom:example.com&quot;,
  9835. &quot;#evilsaloon:example.com&quot;
  9836. ],
  9837. &quot;new_room_id&quot;: &quot;!newroomid:example.com&quot;
  9838. }
  9839. </code></pre>
  9840. <p><strong>Parameters</strong></p>
  9841. <p>The following parameters should be set in the URL:</p>
  9842. <ul>
  9843. <li><code>room_id</code> - The ID of the room.</li>
  9844. </ul>
  9845. <p>The following JSON body parameters are available:</p>
  9846. <ul>
  9847. <li><code>new_room_user_id</code> - Optional. If set, a new room will be created with this user ID
  9848. as the creator and admin, and all users in the old room will be moved into that
  9849. room. If not set, no new room will be created and the users will just be removed
  9850. from the old room. The user ID must be on the local server, but does not necessarily
  9851. have to belong to a registered user.</li>
  9852. <li><code>room_name</code> - Optional. A string representing the name of the room that new users will be
  9853. invited to. Defaults to <code>Content Violation Notification</code></li>
  9854. <li><code>message</code> - Optional. A string containing the first message that will be sent as
  9855. <code>new_room_user_id</code> in the new room. Ideally this will clearly convey why the
  9856. original room was shut down. Defaults to <code>Sharing illegal content on this server is not permitted and rooms in violation will be blocked.</code></li>
  9857. <li><code>block</code> - Optional. If set to <code>true</code>, this room will be added to a blocking list, preventing
  9858. future attempts to join the room. Defaults to <code>false</code>.</li>
  9859. <li><code>purge</code> - Optional. If set to <code>true</code>, it will remove all traces of the room from your database.
  9860. Defaults to <code>true</code>.</li>
  9861. <li><code>force_purge</code> - Optional, and ignored unless <code>purge</code> is <code>true</code>. If set to <code>true</code>, it
  9862. will force a purge to go ahead even if there are local users still in the room. Do not
  9863. use this unless a regular <code>purge</code> operation fails, as it could leave those users'
  9864. clients in a confused state.</li>
  9865. </ul>
  9866. <p>The JSON body must not be empty. The body must be at least <code>{}</code>.</p>
  9867. <p><strong>Response</strong></p>
  9868. <p>The following fields are returned in the JSON response body:</p>
  9869. <ul>
  9870. <li><code>kicked_users</code> - An array of users (<code>user_id</code>) that were kicked.</li>
  9871. <li><code>failed_to_kick_users</code> - An array of users (<code>user_id</code>) that that were not kicked.</li>
  9872. <li><code>local_aliases</code> - An array of strings representing the local aliases that were migrated from
  9873. the old room to the new.</li>
  9874. <li><code>new_room_id</code> - A string representing the room ID of the new room.</li>
  9875. </ul>
  9876. <h2 id="undoing-room-deletions"><a class="header" href="#undoing-room-deletions">Undoing room deletions</a></h2>
  9877. <p><em>Note</em>: This guide may be outdated by the time you read it. By nature of room deletions being performed at the database level,
  9878. the structure can and does change without notice.</p>
  9879. <p>First, it's important to understand that a room deletion is very destructive. Undoing a deletion is not as simple as pretending it
  9880. never happened - work has to be done to move forward instead of resetting the past. In fact, in some cases it might not be possible
  9881. to recover at all:</p>
  9882. <ul>
  9883. <li>If the room was invite-only, your users will need to be re-invited.</li>
  9884. <li>If the room no longer has any members at all, it'll be impossible to rejoin.</li>
  9885. <li>The first user to rejoin will have to do so via an alias on a different
  9886. server (or receive an invite from a user on a different server).</li>
  9887. </ul>
  9888. <p>With all that being said, if you still want to try and recover the room:</p>
  9889. <ol>
  9890. <li>
  9891. <p>If the room was <code>block</code>ed, you must unblock it on your server. This can be
  9892. accomplished as follows:</p>
  9893. <ol>
  9894. <li>For safety reasons, shut down Synapse.</li>
  9895. <li>In the database, run <code>DELETE FROM blocked_rooms WHERE room_id = '!example:example.org';</code>
  9896. <ul>
  9897. <li>For caution: it's recommended to run this in a transaction: <code>BEGIN; DELETE ...;</code>, verify you got 1 result, then <code>COMMIT;</code>.</li>
  9898. <li>The room ID is the same one supplied to the delete room API, not the Content Violation room.</li>
  9899. </ul>
  9900. </li>
  9901. <li>Restart Synapse.</li>
  9902. </ol>
  9903. <p>This step is unnecessary if <code>block</code> was not set.</p>
  9904. </li>
  9905. <li>
  9906. <p>Any room aliases on your server that pointed to the deleted room may have
  9907. been deleted, or redirected to the Content Violation room. These will need
  9908. to be restored manually.</p>
  9909. </li>
  9910. <li>
  9911. <p>Users on your server that were in the deleted room will have been kicked
  9912. from the room. Consider whether you want to update their membership
  9913. (possibly via the <a href="admin_api/room_membership.html">Edit Room Membership API</a>) or let
  9914. them handle rejoining themselves.</p>
  9915. </li>
  9916. <li>
  9917. <p>If <code>new_room_user_id</code> was given, a 'Content Violation' will have been
  9918. created. Consider whether you want to delete that roomm.</p>
  9919. </li>
  9920. </ol>
  9921. <h1 id="make-room-admin-api"><a class="header" href="#make-room-admin-api">Make Room Admin API</a></h1>
  9922. <p>Grants another user the highest power available to a local user who is in the room.
  9923. If the user is not in the room, and it is not publicly joinable, then invite the user.</p>
  9924. <p>By default the server admin (the caller) is granted power, but another user can
  9925. optionally be specified, e.g.:</p>
  9926. <pre><code>POST /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/make_room_admin
  9927. {
  9928. &quot;user_id&quot;: &quot;@foo:example.com&quot;
  9929. }
  9930. </code></pre>
  9931. <h1 id="forward-extremities-admin-api"><a class="header" href="#forward-extremities-admin-api">Forward Extremities Admin API</a></h1>
  9932. <p>Enables querying and deleting forward extremities from rooms. When a lot of forward
  9933. extremities accumulate in a room, performance can become degraded. For details, see
  9934. <a href="https://github.com/matrix-org/synapse/issues/1760">#1760</a>.</p>
  9935. <h2 id="check-for-forward-extremities"><a class="header" href="#check-for-forward-extremities">Check for forward extremities</a></h2>
  9936. <p>To check the status of forward extremities for a room:</p>
  9937. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/forward_extremities
  9938. </code></pre>
  9939. <p>A response as follows will be returned:</p>
  9940. <pre><code class="language-json">{
  9941. &quot;count&quot;: 1,
  9942. &quot;results&quot;: [
  9943. {
  9944. &quot;event_id&quot;: &quot;$M5SP266vsnxctfwFgFLNceaCo3ujhRtg_NiiHabcdefgh&quot;,
  9945. &quot;state_group&quot;: 439,
  9946. &quot;depth&quot;: 123,
  9947. &quot;received_ts&quot;: 1611263016761
  9948. }
  9949. ]
  9950. }
  9951. </code></pre>
  9952. <h2 id="deleting-forward-extremities"><a class="header" href="#deleting-forward-extremities">Deleting forward extremities</a></h2>
  9953. <p><strong>WARNING</strong>: Please ensure you know what you're doing and have read
  9954. the related issue <a href="https://github.com/matrix-org/synapse/issues/1760">#1760</a>.
  9955. Under no situations should this API be executed as an automated maintenance task!</p>
  9956. <p>If a room has lots of forward extremities, the extra can be
  9957. deleted as follows:</p>
  9958. <pre><code>DELETE /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/forward_extremities
  9959. </code></pre>
  9960. <p>A response as follows will be returned, indicating the amount of forward extremities
  9961. that were deleted.</p>
  9962. <pre><code class="language-json">{
  9963. &quot;deleted&quot;: 1
  9964. }
  9965. </code></pre>
  9966. <h1 id="event-context-api"><a class="header" href="#event-context-api">Event Context API</a></h1>
  9967. <p>This API lets a client find the context of an event. This is designed primarily to investigate abuse reports.</p>
  9968. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/context/&lt;event_id&gt;
  9969. </code></pre>
  9970. <p>This API mimmicks <a href="https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-rooms-roomid-context-eventid">GET /_matrix/client/r0/rooms/{roomId}/context/{eventId}</a>. Please refer to the link for all details on parameters and reseponse.</p>
  9971. <p>Example response:</p>
  9972. <pre><code class="language-json">{
  9973. &quot;end&quot;: &quot;t29-57_2_0_2&quot;,
  9974. &quot;events_after&quot;: [
  9975. {
  9976. &quot;content&quot;: {
  9977. &quot;body&quot;: &quot;This is an example text message&quot;,
  9978. &quot;msgtype&quot;: &quot;m.text&quot;,
  9979. &quot;format&quot;: &quot;org.matrix.custom.html&quot;,
  9980. &quot;formatted_body&quot;: &quot;&lt;b&gt;This is an example text message&lt;/b&gt;&quot;
  9981. },
  9982. &quot;type&quot;: &quot;m.room.message&quot;,
  9983. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  9984. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  9985. &quot;sender&quot;: &quot;@example:example.org&quot;,
  9986. &quot;origin_server_ts&quot;: 1432735824653,
  9987. &quot;unsigned&quot;: {
  9988. &quot;age&quot;: 1234
  9989. }
  9990. }
  9991. ],
  9992. &quot;event&quot;: {
  9993. &quot;content&quot;: {
  9994. &quot;body&quot;: &quot;filename.jpg&quot;,
  9995. &quot;info&quot;: {
  9996. &quot;h&quot;: 398,
  9997. &quot;w&quot;: 394,
  9998. &quot;mimetype&quot;: &quot;image/jpeg&quot;,
  9999. &quot;size&quot;: 31037
  10000. },
  10001. &quot;url&quot;: &quot;mxc://example.org/JWEIFJgwEIhweiWJE&quot;,
  10002. &quot;msgtype&quot;: &quot;m.image&quot;
  10003. },
  10004. &quot;type&quot;: &quot;m.room.message&quot;,
  10005. &quot;event_id&quot;: &quot;$f3h4d129462ha:example.com&quot;,
  10006. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  10007. &quot;sender&quot;: &quot;@example:example.org&quot;,
  10008. &quot;origin_server_ts&quot;: 1432735824653,
  10009. &quot;unsigned&quot;: {
  10010. &quot;age&quot;: 1234
  10011. }
  10012. },
  10013. &quot;events_before&quot;: [
  10014. {
  10015. &quot;content&quot;: {
  10016. &quot;body&quot;: &quot;something-important.doc&quot;,
  10017. &quot;filename&quot;: &quot;something-important.doc&quot;,
  10018. &quot;info&quot;: {
  10019. &quot;mimetype&quot;: &quot;application/msword&quot;,
  10020. &quot;size&quot;: 46144
  10021. },
  10022. &quot;msgtype&quot;: &quot;m.file&quot;,
  10023. &quot;url&quot;: &quot;mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe&quot;
  10024. },
  10025. &quot;type&quot;: &quot;m.room.message&quot;,
  10026. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  10027. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  10028. &quot;sender&quot;: &quot;@example:example.org&quot;,
  10029. &quot;origin_server_ts&quot;: 1432735824653,
  10030. &quot;unsigned&quot;: {
  10031. &quot;age&quot;: 1234
  10032. }
  10033. }
  10034. ],
  10035. &quot;start&quot;: &quot;t27-54_2_0_2&quot;,
  10036. &quot;state&quot;: [
  10037. {
  10038. &quot;content&quot;: {
  10039. &quot;creator&quot;: &quot;@example:example.org&quot;,
  10040. &quot;room_version&quot;: &quot;1&quot;,
  10041. &quot;m.federate&quot;: true,
  10042. &quot;predecessor&quot;: {
  10043. &quot;event_id&quot;: &quot;$something:example.org&quot;,
  10044. &quot;room_id&quot;: &quot;!oldroom:example.org&quot;
  10045. }
  10046. },
  10047. &quot;type&quot;: &quot;m.room.create&quot;,
  10048. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  10049. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  10050. &quot;sender&quot;: &quot;@example:example.org&quot;,
  10051. &quot;origin_server_ts&quot;: 1432735824653,
  10052. &quot;unsigned&quot;: {
  10053. &quot;age&quot;: 1234
  10054. },
  10055. &quot;state_key&quot;: &quot;&quot;
  10056. },
  10057. {
  10058. &quot;content&quot;: {
  10059. &quot;membership&quot;: &quot;join&quot;,
  10060. &quot;avatar_url&quot;: &quot;mxc://example.org/SEsfnsuifSDFSSEF&quot;,
  10061. &quot;displayname&quot;: &quot;Alice Margatroid&quot;
  10062. },
  10063. &quot;type&quot;: &quot;m.room.member&quot;,
  10064. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  10065. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  10066. &quot;sender&quot;: &quot;@example:example.org&quot;,
  10067. &quot;origin_server_ts&quot;: 1432735824653,
  10068. &quot;unsigned&quot;: {
  10069. &quot;age&quot;: 1234
  10070. },
  10071. &quot;state_key&quot;: &quot;@alice:example.org&quot;
  10072. }
  10073. ]
  10074. }
  10075. </code></pre>
  10076. <div style="break-before: page; page-break-before: always;"></div><h1 id="server-notices-1"><a class="header" href="#server-notices-1">Server Notices</a></h1>
  10077. <p>The API to send notices is as follows:</p>
  10078. <pre><code>POST /_synapse/admin/v1/send_server_notice
  10079. </code></pre>
  10080. <p>or:</p>
  10081. <pre><code>PUT /_synapse/admin/v1/send_server_notice/{txnId}
  10082. </code></pre>
  10083. <p>You will need to authenticate with an access token for an admin user.</p>
  10084. <p>When using the <code>PUT</code> form, retransmissions with the same transaction ID will be
  10085. ignored in the same way as with <code>PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}</code>.</p>
  10086. <p>The request body should look something like the following:</p>
  10087. <pre><code class="language-json">{
  10088. &quot;user_id&quot;: &quot;@target_user:server_name&quot;,
  10089. &quot;content&quot;: {
  10090. &quot;msgtype&quot;: &quot;m.text&quot;,
  10091. &quot;body&quot;: &quot;This is my message&quot;
  10092. }
  10093. }
  10094. </code></pre>
  10095. <p>You can optionally include the following additional parameters:</p>
  10096. <ul>
  10097. <li><code>type</code>: the type of event. Defaults to <code>m.room.message</code>.</li>
  10098. <li><code>state_key</code>: Setting this will result in a state event being sent.</li>
  10099. </ul>
  10100. <p>Once the notice has been sent, the API will return the following response:</p>
  10101. <pre><code class="language-json">{
  10102. &quot;event_id&quot;: &quot;&lt;event_id&gt;&quot;
  10103. }
  10104. </code></pre>
  10105. <p>Note that server notices must be enabled in <code>homeserver.yaml</code> before this API
  10106. can be used. See <a href="admin_api/../server_notices.html">the server notices documentation</a> for more information.</p>
  10107. <div style="break-before: page; page-break-before: always;"></div><h1 id="users-media-usage-statistics"><a class="header" href="#users-media-usage-statistics">Users' media usage statistics</a></h1>
  10108. <p>Returns information about all local media usage of users. Gives the
  10109. possibility to filter them by time and user.</p>
  10110. <p>The API is:</p>
  10111. <pre><code>GET /_synapse/admin/v1/statistics/users/media
  10112. </code></pre>
  10113. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  10114. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  10115. <p>A response body like the following is returned:</p>
  10116. <pre><code class="language-json">{
  10117. &quot;users&quot;: [
  10118. {
  10119. &quot;displayname&quot;: &quot;foo_user_0&quot;,
  10120. &quot;media_count&quot;: 2,
  10121. &quot;media_length&quot;: 134,
  10122. &quot;user_id&quot;: &quot;@foo_user_0:test&quot;
  10123. },
  10124. {
  10125. &quot;displayname&quot;: &quot;foo_user_1&quot;,
  10126. &quot;media_count&quot;: 2,
  10127. &quot;media_length&quot;: 134,
  10128. &quot;user_id&quot;: &quot;@foo_user_1:test&quot;
  10129. }
  10130. ],
  10131. &quot;next_token&quot;: 3,
  10132. &quot;total&quot;: 10
  10133. }
  10134. </code></pre>
  10135. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint
  10136. again with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  10137. <p>If the endpoint does not return a <code>next_token</code> then there are no more
  10138. reports to paginate through.</p>
  10139. <p><strong>Parameters</strong></p>
  10140. <p>The following parameters should be set in the URL:</p>
  10141. <ul>
  10142. <li><code>limit</code>: string representing a positive integer - Is optional but is
  10143. used for pagination, denoting the maximum number of items to return
  10144. in this call. Defaults to <code>100</code>.</li>
  10145. <li><code>from</code>: string representing a positive integer - Is optional but used for pagination,
  10146. denoting the offset in the returned results. This should be treated as an opaque value
  10147. and not explicitly set to anything other than the return value of <code>next_token</code> from a
  10148. previous call. Defaults to <code>0</code>.</li>
  10149. <li><code>order_by</code> - string - The method in which to sort the returned list of users. Valid values are:
  10150. <ul>
  10151. <li><code>user_id</code> - Users are ordered alphabetically by <code>user_id</code>. This is the default.</li>
  10152. <li><code>displayname</code> - Users are ordered alphabetically by <code>displayname</code>.</li>
  10153. <li><code>media_length</code> - Users are ordered by the total size of uploaded media in bytes.
  10154. Smallest to largest.</li>
  10155. <li><code>media_count</code> - Users are ordered by number of uploaded media. Smallest to largest.</li>
  10156. </ul>
  10157. </li>
  10158. <li><code>from_ts</code> - string representing a positive integer - Considers only
  10159. files created at this timestamp or later. Unix timestamp in ms.</li>
  10160. <li><code>until_ts</code> - string representing a positive integer - Considers only
  10161. files created at this timestamp or earlier. Unix timestamp in ms.</li>
  10162. <li><code>search_term</code> - string - Filter users by their user ID localpart <strong>or</strong> displayname.
  10163. The search term can be found in any part of the string.
  10164. Defaults to no filtering.</li>
  10165. <li><code>dir</code> - string - Direction of order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  10166. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</li>
  10167. </ul>
  10168. <p><strong>Response</strong></p>
  10169. <p>The following fields are returned in the JSON response body:</p>
  10170. <ul>
  10171. <li><code>users</code> - An array of objects, each containing information
  10172. about the user and their local media. Objects contain the following fields:
  10173. <ul>
  10174. <li><code>displayname</code> - string - Displayname of this user.</li>
  10175. <li><code>media_count</code> - integer - Number of uploaded media by this user.</li>
  10176. <li><code>media_length</code> - integer - Size of uploaded media in bytes by this user.</li>
  10177. <li><code>user_id</code> - string - Fully-qualified user ID (ex. <code>@user:server.com</code>).</li>
  10178. </ul>
  10179. </li>
  10180. <li><code>next_token</code> - integer - Opaque value used for pagination. See above.</li>
  10181. <li><code>total</code> - integer - Total number of users after filtering.</li>
  10182. </ul>
  10183. <div style="break-before: page; page-break-before: always;"></div><h1 id="user-admin-api"><a class="header" href="#user-admin-api">User Admin API</a></h1>
  10184. <h2 id="query-user-account"><a class="header" href="#query-user-account">Query User Account</a></h2>
  10185. <p>This API returns information about a specific user account.</p>
  10186. <p>The api is:</p>
  10187. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;
  10188. </code></pre>
  10189. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10190. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10191. <p>It returns a JSON body like the following:</p>
  10192. <pre><code class="language-json">{
  10193. &quot;displayname&quot;: &quot;User&quot;,
  10194. &quot;threepids&quot;: [
  10195. {
  10196. &quot;medium&quot;: &quot;email&quot;,
  10197. &quot;address&quot;: &quot;&lt;user_mail_1&gt;&quot;,
  10198. &quot;added_at&quot;: 1586458409743,
  10199. &quot;validated_at&quot;: 1586458409743
  10200. },
  10201. {
  10202. &quot;medium&quot;: &quot;email&quot;,
  10203. &quot;address&quot;: &quot;&lt;user_mail_2&gt;&quot;,
  10204. &quot;added_at&quot;: 1586458409743,
  10205. &quot;validated_at&quot;: 1586458409743
  10206. }
  10207. ],
  10208. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;,
  10209. &quot;admin&quot;: 0,
  10210. &quot;deactivated&quot;: 0,
  10211. &quot;shadow_banned&quot;: 0,
  10212. &quot;password_hash&quot;: &quot;$2b$12$p9B4GkqYdRTPGD&quot;,
  10213. &quot;creation_ts&quot;: 1560432506,
  10214. &quot;appservice_id&quot;: null,
  10215. &quot;consent_server_notice_sent&quot;: null,
  10216. &quot;consent_version&quot;: null,
  10217. &quot;external_ids&quot;: [
  10218. {
  10219. &quot;auth_provider&quot;: &quot;&lt;provider1&gt;&quot;,
  10220. &quot;external_id&quot;: &quot;&lt;user_id_provider_1&gt;&quot;
  10221. },
  10222. {
  10223. &quot;auth_provider&quot;: &quot;&lt;provider2&gt;&quot;,
  10224. &quot;external_id&quot;: &quot;&lt;user_id_provider_2&gt;&quot;
  10225. }
  10226. ],
  10227. &quot;user_type&quot;: null
  10228. }
  10229. </code></pre>
  10230. <p>URL parameters:</p>
  10231. <ul>
  10232. <li><code>user_id</code>: fully-qualified user id: for example, <code>@user:server.com</code>.</li>
  10233. </ul>
  10234. <h2 id="create-or-modify-account"><a class="header" href="#create-or-modify-account">Create or modify Account</a></h2>
  10235. <p>This API allows an administrator to create or modify a user account with a
  10236. specific <code>user_id</code>.</p>
  10237. <p>This api is:</p>
  10238. <pre><code>PUT /_synapse/admin/v2/users/&lt;user_id&gt;
  10239. </code></pre>
  10240. <p>with a body of:</p>
  10241. <pre><code class="language-json">{
  10242. &quot;password&quot;: &quot;user_password&quot;,
  10243. &quot;displayname&quot;: &quot;User&quot;,
  10244. &quot;threepids&quot;: [
  10245. {
  10246. &quot;medium&quot;: &quot;email&quot;,
  10247. &quot;address&quot;: &quot;&lt;user_mail_1&gt;&quot;
  10248. },
  10249. {
  10250. &quot;medium&quot;: &quot;email&quot;,
  10251. &quot;address&quot;: &quot;&lt;user_mail_2&gt;&quot;
  10252. }
  10253. ],
  10254. &quot;external_ids&quot;: [
  10255. {
  10256. &quot;auth_provider&quot;: &quot;&lt;provider1&gt;&quot;,
  10257. &quot;external_id&quot;: &quot;&lt;user_id_provider_1&gt;&quot;
  10258. },
  10259. {
  10260. &quot;auth_provider&quot;: &quot;&lt;provider2&gt;&quot;,
  10261. &quot;external_id&quot;: &quot;&lt;user_id_provider_2&gt;&quot;
  10262. }
  10263. ],
  10264. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;,
  10265. &quot;admin&quot;: false,
  10266. &quot;deactivated&quot;: false,
  10267. &quot;user_type&quot;: null
  10268. }
  10269. </code></pre>
  10270. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10271. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10272. <p>Returns HTTP status code:</p>
  10273. <ul>
  10274. <li><code>201</code> - When a new user object was created.</li>
  10275. <li><code>200</code> - When a user was modified.</li>
  10276. </ul>
  10277. <p>URL parameters:</p>
  10278. <ul>
  10279. <li><code>user_id</code>: fully-qualified user id: for example, <code>@user:server.com</code>.</li>
  10280. </ul>
  10281. <p>Body parameters:</p>
  10282. <ul>
  10283. <li><code>password</code> - string, optional. If provided, the user's password is updated and all
  10284. devices are logged out.</li>
  10285. <li><code>displayname</code> - string, optional, defaults to the value of <code>user_id</code>.</li>
  10286. <li><code>threepids</code> - array, optional, allows setting the third-party IDs (email, msisdn)
  10287. <ul>
  10288. <li><code>medium</code> - string. Kind of third-party ID, either <code>email</code> or <code>msisdn</code>.</li>
  10289. <li><code>address</code> - string. Value of third-party ID.
  10290. belonging to a user.</li>
  10291. </ul>
  10292. </li>
  10293. <li><code>external_ids</code> - array, optional. Allow setting the identifier of the external identity
  10294. provider for SSO (Single sign-on). Details in
  10295. <a href="admin_api/../usage/configuration/homeserver_sample_config.html">Sample Configuration File</a>
  10296. section <code>sso</code> and <code>oidc_providers</code>.
  10297. <ul>
  10298. <li><code>auth_provider</code> - string. ID of the external identity provider. Value of <code>idp_id</code>
  10299. in homeserver configuration.</li>
  10300. <li><code>external_id</code> - string, user ID in the external identity provider.</li>
  10301. </ul>
  10302. </li>
  10303. <li><code>avatar_url</code> - string, optional, must be a
  10304. <a href="https://matrix.org/docs/spec/client_server/r0.6.0#matrix-content-mxc-uris">MXC URI</a>.</li>
  10305. <li><code>admin</code> - bool, optional, defaults to <code>false</code>.</li>
  10306. <li><code>deactivated</code> - bool, optional. If unspecified, deactivation state will be left
  10307. unchanged on existing accounts and set to <code>false</code> for new accounts.
  10308. A user cannot be erased by deactivating with this API. For details on
  10309. deactivating users see <a href="admin_api/user_admin_api.html#deactivate-account">Deactivate Account</a>.</li>
  10310. <li><code>user_type</code> - string or null, optional. If provided, the user type will be
  10311. adjusted. If <code>null</code> given, the user type will be cleared. Other
  10312. allowed options are: <code>bot</code> and <code>support</code>.</li>
  10313. </ul>
  10314. <p>If the user already exists then optional parameters default to the current value.</p>
  10315. <p>In order to re-activate an account <code>deactivated</code> must be set to <code>false</code>. If
  10316. users do not login via single-sign-on, a new <code>password</code> must be provided.</p>
  10317. <h2 id="list-accounts"><a class="header" href="#list-accounts">List Accounts</a></h2>
  10318. <p>This API returns all local user accounts.
  10319. By default, the response is ordered by ascending user ID.</p>
  10320. <pre><code>GET /_synapse/admin/v2/users?from=0&amp;limit=10&amp;guests=false
  10321. </code></pre>
  10322. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10323. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10324. <p>A response body like the following is returned:</p>
  10325. <pre><code class="language-json">{
  10326. &quot;users&quot;: [
  10327. {
  10328. &quot;name&quot;: &quot;&lt;user_id1&gt;&quot;,
  10329. &quot;is_guest&quot;: 0,
  10330. &quot;admin&quot;: 0,
  10331. &quot;user_type&quot;: null,
  10332. &quot;deactivated&quot;: 0,
  10333. &quot;shadow_banned&quot;: 0,
  10334. &quot;displayname&quot;: &quot;&lt;User One&gt;&quot;,
  10335. &quot;avatar_url&quot;: null,
  10336. &quot;creation_ts&quot;: 1560432668000
  10337. }, {
  10338. &quot;name&quot;: &quot;&lt;user_id2&gt;&quot;,
  10339. &quot;is_guest&quot;: 0,
  10340. &quot;admin&quot;: 1,
  10341. &quot;user_type&quot;: null,
  10342. &quot;deactivated&quot;: 0,
  10343. &quot;shadow_banned&quot;: 0,
  10344. &quot;displayname&quot;: &quot;&lt;User Two&gt;&quot;,
  10345. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;,
  10346. &quot;creation_ts&quot;: 1561550621000
  10347. }
  10348. ],
  10349. &quot;next_token&quot;: &quot;100&quot;,
  10350. &quot;total&quot;: 200
  10351. }
  10352. </code></pre>
  10353. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again
  10354. with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  10355. <p>If the endpoint does not return a <code>next_token</code> then there are no more users
  10356. to paginate through.</p>
  10357. <p><strong>Parameters</strong></p>
  10358. <p>The following parameters should be set in the URL:</p>
  10359. <ul>
  10360. <li>
  10361. <p><code>user_id</code> - Is optional and filters to only return users with user IDs
  10362. that contain this value. This parameter is ignored when using the <code>name</code> parameter.</p>
  10363. </li>
  10364. <li>
  10365. <p><code>name</code> - Is optional and filters to only return users with user ID localparts
  10366. <strong>or</strong> displaynames that contain this value.</p>
  10367. </li>
  10368. <li>
  10369. <p><code>guests</code> - string representing a bool - Is optional and if <code>false</code> will <strong>exclude</strong> guest users.
  10370. Defaults to <code>true</code> to include guest users.</p>
  10371. </li>
  10372. <li>
  10373. <p><code>deactivated</code> - string representing a bool - Is optional and if <code>true</code> will <strong>include</strong> deactivated users.
  10374. Defaults to <code>false</code> to exclude deactivated users.</p>
  10375. </li>
  10376. <li>
  10377. <p><code>limit</code> - string representing a positive integer - Is optional but is used for pagination,
  10378. denoting the maximum number of items to return in this call. Defaults to <code>100</code>.</p>
  10379. </li>
  10380. <li>
  10381. <p><code>from</code> - string representing a positive integer - Is optional but used for pagination,
  10382. denoting the offset in the returned results. This should be treated as an opaque value and
  10383. not explicitly set to anything other than the return value of <code>next_token</code> from a previous call.
  10384. Defaults to <code>0</code>.</p>
  10385. </li>
  10386. <li>
  10387. <p><code>order_by</code> - The method by which to sort the returned list of users.
  10388. If the ordered field has duplicates, the second order is always by ascending <code>name</code>,
  10389. which guarantees a stable ordering. Valid values are:</p>
  10390. <ul>
  10391. <li><code>name</code> - Users are ordered alphabetically by <code>name</code>. This is the default.</li>
  10392. <li><code>is_guest</code> - Users are ordered by <code>is_guest</code> status.</li>
  10393. <li><code>admin</code> - Users are ordered by <code>admin</code> status.</li>
  10394. <li><code>user_type</code> - Users are ordered alphabetically by <code>user_type</code>.</li>
  10395. <li><code>deactivated</code> - Users are ordered by <code>deactivated</code> status.</li>
  10396. <li><code>shadow_banned</code> - Users are ordered by <code>shadow_banned</code> status.</li>
  10397. <li><code>displayname</code> - Users are ordered alphabetically by <code>displayname</code>.</li>
  10398. <li><code>avatar_url</code> - Users are ordered alphabetically by avatar URL.</li>
  10399. <li><code>creation_ts</code> - Users are ordered by when the users was created in ms.</li>
  10400. </ul>
  10401. </li>
  10402. <li>
  10403. <p><code>dir</code> - Direction of media order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  10404. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</p>
  10405. </li>
  10406. </ul>
  10407. <p>Caution. The database only has indexes on the columns <code>name</code> and <code>creation_ts</code>.
  10408. This means that if a different sort order is used (<code>is_guest</code>, <code>admin</code>,
  10409. <code>user_type</code>, <code>deactivated</code>, <code>shadow_banned</code>, <code>avatar_url</code> or <code>displayname</code>),
  10410. this can cause a large load on the database, especially for large environments.</p>
  10411. <p><strong>Response</strong></p>
  10412. <p>The following fields are returned in the JSON response body:</p>
  10413. <ul>
  10414. <li>
  10415. <p><code>users</code> - An array of objects, each containing information about an user.
  10416. User objects contain the following fields:</p>
  10417. <ul>
  10418. <li><code>name</code> - string - Fully-qualified user ID (ex. <code>@user:server.com</code>).</li>
  10419. <li><code>is_guest</code> - bool - Status if that user is a guest account.</li>
  10420. <li><code>admin</code> - bool - Status if that user is a server administrator.</li>
  10421. <li><code>user_type</code> - string - Type of the user. Normal users are type <code>None</code>.
  10422. This allows user type specific behaviour. There are also types <code>support</code> and <code>bot</code>. </li>
  10423. <li><code>deactivated</code> - bool - Status if that user has been marked as deactivated.</li>
  10424. <li><code>shadow_banned</code> - bool - Status if that user has been marked as shadow banned.</li>
  10425. <li><code>displayname</code> - string - The user's display name if they have set one.</li>
  10426. <li><code>avatar_url</code> - string - The user's avatar URL if they have set one.</li>
  10427. <li><code>creation_ts</code> - integer - The user's creation timestamp in ms.</li>
  10428. </ul>
  10429. </li>
  10430. <li>
  10431. <p><code>next_token</code>: string representing a positive integer - Indication for pagination. See above.</p>
  10432. </li>
  10433. <li>
  10434. <p><code>total</code> - integer - Total number of media.</p>
  10435. </li>
  10436. </ul>
  10437. <h2 id="query-current-sessions-for-a-user"><a class="header" href="#query-current-sessions-for-a-user">Query current sessions for a user</a></h2>
  10438. <p>This API returns information about the active sessions for a specific user.</p>
  10439. <p>The endpoints are:</p>
  10440. <pre><code>GET /_synapse/admin/v1/whois/&lt;user_id&gt;
  10441. </code></pre>
  10442. <p>and:</p>
  10443. <pre><code>GET /_matrix/client/r0/admin/whois/&lt;userId&gt;
  10444. </code></pre>
  10445. <p>See also: <a href="https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-admin-whois-userid">Client Server
  10446. API Whois</a>.</p>
  10447. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10448. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10449. <p>It returns a JSON body like the following:</p>
  10450. <pre><code class="language-json">{
  10451. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;,
  10452. &quot;devices&quot;: {
  10453. &quot;&quot;: {
  10454. &quot;sessions&quot;: [
  10455. {
  10456. &quot;connections&quot;: [
  10457. {
  10458. &quot;ip&quot;: &quot;1.2.3.4&quot;,
  10459. &quot;last_seen&quot;: 1417222374433,
  10460. &quot;user_agent&quot;: &quot;Mozilla/5.0 ...&quot;
  10461. },
  10462. {
  10463. &quot;ip&quot;: &quot;1.2.3.10&quot;,
  10464. &quot;last_seen&quot;: 1417222374500,
  10465. &quot;user_agent&quot;: &quot;Dalvik/2.1.0 ...&quot;
  10466. }
  10467. ]
  10468. }
  10469. ]
  10470. }
  10471. }
  10472. }
  10473. </code></pre>
  10474. <p><code>last_seen</code> is measured in milliseconds since the Unix epoch.</p>
  10475. <h2 id="deactivate-account"><a class="header" href="#deactivate-account">Deactivate Account</a></h2>
  10476. <p>This API deactivates an account. It removes active access tokens, resets the
  10477. password, and deletes third-party IDs (to prevent the user requesting a
  10478. password reset).</p>
  10479. <p>It can also mark the user as GDPR-erased. This means messages sent by the
  10480. user will still be visible by anyone that was in the room when these messages
  10481. were sent, but hidden from users joining the room afterwards.</p>
  10482. <p>The api is:</p>
  10483. <pre><code>POST /_synapse/admin/v1/deactivate/&lt;user_id&gt;
  10484. </code></pre>
  10485. <p>with a body of:</p>
  10486. <pre><code class="language-json">{
  10487. &quot;erase&quot;: true
  10488. }
  10489. </code></pre>
  10490. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10491. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10492. <p>The erase parameter is optional and defaults to <code>false</code>.
  10493. An empty body may be passed for backwards compatibility.</p>
  10494. <p>The following actions are performed when deactivating an user:</p>
  10495. <ul>
  10496. <li>Try to unpind 3PIDs from the identity server</li>
  10497. <li>Remove all 3PIDs from the homeserver</li>
  10498. <li>Delete all devices and E2EE keys</li>
  10499. <li>Delete all access tokens</li>
  10500. <li>Delete all pushers</li>
  10501. <li>Delete the password hash</li>
  10502. <li>Removal from all rooms the user is a member of</li>
  10503. <li>Remove the user from the user directory</li>
  10504. <li>Reject all pending invites</li>
  10505. <li>Remove all account validity information related to the user</li>
  10506. </ul>
  10507. <p>The following additional actions are performed during deactivation if <code>erase</code>
  10508. is set to <code>true</code>:</p>
  10509. <ul>
  10510. <li>Remove the user's display name</li>
  10511. <li>Remove the user's avatar URL</li>
  10512. <li>Mark the user as erased</li>
  10513. </ul>
  10514. <p>The following actions are <strong>NOT</strong> performed. The list may be incomplete.</p>
  10515. <ul>
  10516. <li>Remove mappings of SSO IDs</li>
  10517. <li><a href="admin_api/user_admin_api.html#delete-media-uploaded-by-a-user">Delete media uploaded</a> by user (included avatar images)</li>
  10518. <li>Delete sent and received messages</li>
  10519. <li>Delete E2E cross-signing keys</li>
  10520. <li>Remove the user's creation (registration) timestamp</li>
  10521. <li><a href="admin_api/user_admin_api.html#override-ratelimiting-for-users">Remove rate limit overrides</a></li>
  10522. <li>Remove from monthly active users</li>
  10523. </ul>
  10524. <h2 id="reset-password"><a class="header" href="#reset-password">Reset password</a></h2>
  10525. <p>Changes the password of another user. This will automatically log the user out of all their devices.</p>
  10526. <p>The api is:</p>
  10527. <pre><code>POST /_synapse/admin/v1/reset_password/&lt;user_id&gt;
  10528. </code></pre>
  10529. <p>with a body of:</p>
  10530. <pre><code class="language-json">{
  10531. &quot;new_password&quot;: &quot;&lt;secret&gt;&quot;,
  10532. &quot;logout_devices&quot;: true
  10533. }
  10534. </code></pre>
  10535. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10536. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10537. <p>The parameter <code>new_password</code> is required.
  10538. The parameter <code>logout_devices</code> is optional and defaults to <code>true</code>.</p>
  10539. <h2 id="get-whether-a-user-is-a-server-administrator-or-not"><a class="header" href="#get-whether-a-user-is-a-server-administrator-or-not">Get whether a user is a server administrator or not</a></h2>
  10540. <p>The api is:</p>
  10541. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/admin
  10542. </code></pre>
  10543. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10544. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10545. <p>A response body like the following is returned:</p>
  10546. <pre><code class="language-json">{
  10547. &quot;admin&quot;: true
  10548. }
  10549. </code></pre>
  10550. <h2 id="change-whether-a-user-is-a-server-administrator-or-not"><a class="header" href="#change-whether-a-user-is-a-server-administrator-or-not">Change whether a user is a server administrator or not</a></h2>
  10551. <p>Note that you cannot demote yourself.</p>
  10552. <p>The api is:</p>
  10553. <pre><code>PUT /_synapse/admin/v1/users/&lt;user_id&gt;/admin
  10554. </code></pre>
  10555. <p>with a body of:</p>
  10556. <pre><code class="language-json">{
  10557. &quot;admin&quot;: true
  10558. }
  10559. </code></pre>
  10560. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10561. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10562. <h2 id="list-room-memberships-of-a-user"><a class="header" href="#list-room-memberships-of-a-user">List room memberships of a user</a></h2>
  10563. <p>Gets a list of all <code>room_id</code> that a specific <code>user_id</code> is member.</p>
  10564. <p>The API is:</p>
  10565. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/joined_rooms
  10566. </code></pre>
  10567. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10568. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10569. <p>A response body like the following is returned:</p>
  10570. <pre><code class="language-json"> {
  10571. &quot;joined_rooms&quot;: [
  10572. &quot;!DuGcnbhHGaSZQoNQR:matrix.org&quot;,
  10573. &quot;!ZtSaPCawyWtxfWiIy:matrix.org&quot;
  10574. ],
  10575. &quot;total&quot;: 2
  10576. }
  10577. </code></pre>
  10578. <p>The server returns the list of rooms of which the user and the server
  10579. are member. If the user is local, all the rooms of which the user is
  10580. member are returned.</p>
  10581. <p><strong>Parameters</strong></p>
  10582. <p>The following parameters should be set in the URL:</p>
  10583. <ul>
  10584. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  10585. </ul>
  10586. <p><strong>Response</strong></p>
  10587. <p>The following fields are returned in the JSON response body:</p>
  10588. <ul>
  10589. <li><code>joined_rooms</code> - An array of <code>room_id</code>.</li>
  10590. <li><code>total</code> - Number of rooms.</li>
  10591. </ul>
  10592. <h2 id="user-media"><a class="header" href="#user-media">User media</a></h2>
  10593. <h3 id="list-media-uploaded-by-a-user"><a class="header" href="#list-media-uploaded-by-a-user">List media uploaded by a user</a></h3>
  10594. <p>Gets a list of all local media that a specific <code>user_id</code> has created.
  10595. By default, the response is ordered by descending creation date and ascending media ID.
  10596. The newest media is on top. You can change the order with parameters
  10597. <code>order_by</code> and <code>dir</code>.</p>
  10598. <p>The API is:</p>
  10599. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/media
  10600. </code></pre>
  10601. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10602. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10603. <p>A response body like the following is returned:</p>
  10604. <pre><code class="language-json">{
  10605. &quot;media&quot;: [
  10606. {
  10607. &quot;created_ts&quot;: 100400,
  10608. &quot;last_access_ts&quot;: null,
  10609. &quot;media_id&quot;: &quot;qXhyRzulkwLsNHTbpHreuEgo&quot;,
  10610. &quot;media_length&quot;: 67,
  10611. &quot;media_type&quot;: &quot;image/png&quot;,
  10612. &quot;quarantined_by&quot;: null,
  10613. &quot;safe_from_quarantine&quot;: false,
  10614. &quot;upload_name&quot;: &quot;test1.png&quot;
  10615. },
  10616. {
  10617. &quot;created_ts&quot;: 200400,
  10618. &quot;last_access_ts&quot;: null,
  10619. &quot;media_id&quot;: &quot;FHfiSnzoINDatrXHQIXBtahw&quot;,
  10620. &quot;media_length&quot;: 67,
  10621. &quot;media_type&quot;: &quot;image/png&quot;,
  10622. &quot;quarantined_by&quot;: null,
  10623. &quot;safe_from_quarantine&quot;: false,
  10624. &quot;upload_name&quot;: &quot;test2.png&quot;
  10625. }
  10626. ],
  10627. &quot;next_token&quot;: 3,
  10628. &quot;total&quot;: 2
  10629. }
  10630. </code></pre>
  10631. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again
  10632. with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  10633. <p>If the endpoint does not return a <code>next_token</code> then there are no more
  10634. reports to paginate through.</p>
  10635. <p><strong>Parameters</strong></p>
  10636. <p>The following parameters should be set in the URL:</p>
  10637. <ul>
  10638. <li>
  10639. <p><code>user_id</code> - string - fully qualified: for example, <code>@user:server.com</code>.</p>
  10640. </li>
  10641. <li>
  10642. <p><code>limit</code>: string representing a positive integer - Is optional but is used for pagination,
  10643. denoting the maximum number of items to return in this call. Defaults to <code>100</code>.</p>
  10644. </li>
  10645. <li>
  10646. <p><code>from</code>: string representing a positive integer - Is optional but used for pagination,
  10647. denoting the offset in the returned results. This should be treated as an opaque value and
  10648. not explicitly set to anything other than the return value of <code>next_token</code> from a previous call.
  10649. Defaults to <code>0</code>.</p>
  10650. </li>
  10651. <li>
  10652. <p><code>order_by</code> - The method by which to sort the returned list of media.
  10653. If the ordered field has duplicates, the second order is always by ascending <code>media_id</code>,
  10654. which guarantees a stable ordering. Valid values are:</p>
  10655. <ul>
  10656. <li><code>media_id</code> - Media are ordered alphabetically by <code>media_id</code>.</li>
  10657. <li><code>upload_name</code> - Media are ordered alphabetically by name the media was uploaded with.</li>
  10658. <li><code>created_ts</code> - Media are ordered by when the content was uploaded in ms.
  10659. Smallest to largest. This is the default.</li>
  10660. <li><code>last_access_ts</code> - Media are ordered by when the content was last accessed in ms.
  10661. Smallest to largest.</li>
  10662. <li><code>media_length</code> - Media are ordered by length of the media in bytes.
  10663. Smallest to largest.</li>
  10664. <li><code>media_type</code> - Media are ordered alphabetically by MIME-type.</li>
  10665. <li><code>quarantined_by</code> - Media are ordered alphabetically by the user ID that
  10666. initiated the quarantine request for this media.</li>
  10667. <li><code>safe_from_quarantine</code> - Media are ordered by the status if this media is safe
  10668. from quarantining.</li>
  10669. </ul>
  10670. </li>
  10671. <li>
  10672. <p><code>dir</code> - Direction of media order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  10673. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</p>
  10674. </li>
  10675. </ul>
  10676. <p>If neither <code>order_by</code> nor <code>dir</code> is set, the default order is newest media on top
  10677. (corresponds to <code>order_by</code> = <code>created_ts</code> and <code>dir</code> = <code>b</code>).</p>
  10678. <p>Caution. The database only has indexes on the columns <code>media_id</code>,
  10679. <code>user_id</code> and <code>created_ts</code>. This means that if a different sort order is used
  10680. (<code>upload_name</code>, <code>last_access_ts</code>, <code>media_length</code>, <code>media_type</code>,
  10681. <code>quarantined_by</code> or <code>safe_from_quarantine</code>), this can cause a large load on the
  10682. database, especially for large environments.</p>
  10683. <p><strong>Response</strong></p>
  10684. <p>The following fields are returned in the JSON response body:</p>
  10685. <ul>
  10686. <li><code>media</code> - An array of objects, each containing information about a media.
  10687. Media objects contain the following fields:
  10688. <ul>
  10689. <li><code>created_ts</code> - integer - Timestamp when the content was uploaded in ms.</li>
  10690. <li><code>last_access_ts</code> - integer - Timestamp when the content was last accessed in ms.</li>
  10691. <li><code>media_id</code> - string - The id used to refer to the media.</li>
  10692. <li><code>media_length</code> - integer - Length of the media in bytes.</li>
  10693. <li><code>media_type</code> - string - The MIME-type of the media.</li>
  10694. <li><code>quarantined_by</code> - string - The user ID that initiated the quarantine request
  10695. for this media.</li>
  10696. <li><code>safe_from_quarantine</code> - bool - Status if this media is safe from quarantining.</li>
  10697. <li><code>upload_name</code> - string - The name the media was uploaded with.</li>
  10698. </ul>
  10699. </li>
  10700. <li><code>next_token</code>: integer - Indication for pagination. See above.</li>
  10701. <li><code>total</code> - integer - Total number of media.</li>
  10702. </ul>
  10703. <h3 id="delete-media-uploaded-by-a-user-1"><a class="header" href="#delete-media-uploaded-by-a-user-1">Delete media uploaded by a user</a></h3>
  10704. <p>This API deletes the <em>local</em> media from the disk of your own server
  10705. that a specific <code>user_id</code> has created. This includes any local thumbnails.</p>
  10706. <p>This API will not affect media that has been uploaded to external
  10707. media repositories (e.g https://github.com/turt2live/matrix-media-repo/).</p>
  10708. <p>By default, the API deletes media ordered by descending creation date and ascending media ID.
  10709. The newest media is deleted first. You can change the order with parameters
  10710. <code>order_by</code> and <code>dir</code>. If no <code>limit</code> is set the API deletes <code>100</code> files per request.</p>
  10711. <p>The API is:</p>
  10712. <pre><code>DELETE /_synapse/admin/v1/users/&lt;user_id&gt;/media
  10713. </code></pre>
  10714. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10715. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10716. <p>A response body like the following is returned:</p>
  10717. <pre><code class="language-json">{
  10718. &quot;deleted_media&quot;: [
  10719. &quot;abcdefghijklmnopqrstuvwx&quot;
  10720. ],
  10721. &quot;total&quot;: 1
  10722. }
  10723. </code></pre>
  10724. <p>The following fields are returned in the JSON response body:</p>
  10725. <ul>
  10726. <li><code>deleted_media</code>: an array of strings - List of deleted <code>media_id</code></li>
  10727. <li><code>total</code>: integer - Total number of deleted <code>media_id</code></li>
  10728. </ul>
  10729. <p><strong>Note</strong>: There is no <code>next_token</code>. This is not useful for deleting media, because
  10730. after deleting media the remaining media have a new order.</p>
  10731. <p><strong>Parameters</strong></p>
  10732. <p>This API has the same parameters as
  10733. <a href="admin_api/user_admin_api.html#list-media-uploaded-by-a-user">List media uploaded by a user</a>.
  10734. With the parameters you can for example limit the number of files to delete at once or
  10735. delete largest/smallest or newest/oldest files first.</p>
  10736. <h2 id="login-as-a-user"><a class="header" href="#login-as-a-user">Login as a user</a></h2>
  10737. <p>Get an access token that can be used to authenticate as that user. Useful for
  10738. when admins wish to do actions on behalf of a user.</p>
  10739. <p>The API is:</p>
  10740. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/login
  10741. {}
  10742. </code></pre>
  10743. <p>An optional <code>valid_until_ms</code> field can be specified in the request body as an
  10744. integer timestamp that specifies when the token should expire. By default tokens
  10745. do not expire.</p>
  10746. <p>A response body like the following is returned:</p>
  10747. <pre><code class="language-json">{
  10748. &quot;access_token&quot;: &quot;&lt;opaque_access_token_string&gt;&quot;
  10749. }
  10750. </code></pre>
  10751. <p>This API does <em>not</em> generate a new device for the user, and so will not appear
  10752. their <code>/devices</code> list, and in general the target user should not be able to
  10753. tell they have been logged in as.</p>
  10754. <p>To expire the token call the standard <code>/logout</code> API with the token.</p>
  10755. <p>Note: The token will expire if the <em>admin</em> user calls <code>/logout/all</code> from any
  10756. of their devices, but the token will <em>not</em> expire if the target user does the
  10757. same.</p>
  10758. <h2 id="user-devices"><a class="header" href="#user-devices">User devices</a></h2>
  10759. <h3 id="list-all-devices"><a class="header" href="#list-all-devices">List all devices</a></h3>
  10760. <p>Gets information about all devices for a specific <code>user_id</code>.</p>
  10761. <p>The API is:</p>
  10762. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;/devices
  10763. </code></pre>
  10764. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10765. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10766. <p>A response body like the following is returned:</p>
  10767. <pre><code class="language-json">{
  10768. &quot;devices&quot;: [
  10769. {
  10770. &quot;device_id&quot;: &quot;QBUAZIFURK&quot;,
  10771. &quot;display_name&quot;: &quot;android&quot;,
  10772. &quot;last_seen_ip&quot;: &quot;1.2.3.4&quot;,
  10773. &quot;last_seen_ts&quot;: 1474491775024,
  10774. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  10775. },
  10776. {
  10777. &quot;device_id&quot;: &quot;AUIECTSRND&quot;,
  10778. &quot;display_name&quot;: &quot;ios&quot;,
  10779. &quot;last_seen_ip&quot;: &quot;1.2.3.5&quot;,
  10780. &quot;last_seen_ts&quot;: 1474491775025,
  10781. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  10782. }
  10783. ],
  10784. &quot;total&quot;: 2
  10785. }
  10786. </code></pre>
  10787. <p><strong>Parameters</strong></p>
  10788. <p>The following parameters should be set in the URL:</p>
  10789. <ul>
  10790. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  10791. </ul>
  10792. <p><strong>Response</strong></p>
  10793. <p>The following fields are returned in the JSON response body:</p>
  10794. <ul>
  10795. <li>
  10796. <p><code>devices</code> - An array of objects, each containing information about a device.
  10797. Device objects contain the following fields:</p>
  10798. <ul>
  10799. <li><code>device_id</code> - Identifier of device.</li>
  10800. <li><code>display_name</code> - Display name set by the user for this device.
  10801. Absent if no name has been set.</li>
  10802. <li><code>last_seen_ip</code> - The IP address where this device was last seen.
  10803. (May be a few minutes out of date, for efficiency reasons).</li>
  10804. <li><code>last_seen_ts</code> - The timestamp (in milliseconds since the unix epoch) when this
  10805. devices was last seen. (May be a few minutes out of date, for efficiency reasons).</li>
  10806. <li><code>user_id</code> - Owner of device.</li>
  10807. </ul>
  10808. </li>
  10809. <li>
  10810. <p><code>total</code> - Total number of user's devices.</p>
  10811. </li>
  10812. </ul>
  10813. <h3 id="delete-multiple-devices"><a class="header" href="#delete-multiple-devices">Delete multiple devices</a></h3>
  10814. <p>Deletes the given devices for a specific <code>user_id</code>, and invalidates
  10815. any access token associated with them.</p>
  10816. <p>The API is:</p>
  10817. <pre><code>POST /_synapse/admin/v2/users/&lt;user_id&gt;/delete_devices
  10818. {
  10819. &quot;devices&quot;: [
  10820. &quot;QBUAZIFURK&quot;,
  10821. &quot;AUIECTSRND&quot;
  10822. ],
  10823. }
  10824. </code></pre>
  10825. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10826. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10827. <p>An empty JSON dict is returned.</p>
  10828. <p><strong>Parameters</strong></p>
  10829. <p>The following parameters should be set in the URL:</p>
  10830. <ul>
  10831. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  10832. </ul>
  10833. <p>The following fields are required in the JSON request body:</p>
  10834. <ul>
  10835. <li><code>devices</code> - The list of device IDs to delete.</li>
  10836. </ul>
  10837. <h3 id="show-a-device"><a class="header" href="#show-a-device">Show a device</a></h3>
  10838. <p>Gets information on a single device, by <code>device_id</code> for a specific <code>user_id</code>.</p>
  10839. <p>The API is:</p>
  10840. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  10841. </code></pre>
  10842. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10843. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10844. <p>A response body like the following is returned:</p>
  10845. <pre><code class="language-json">{
  10846. &quot;device_id&quot;: &quot;&lt;device_id&gt;&quot;,
  10847. &quot;display_name&quot;: &quot;android&quot;,
  10848. &quot;last_seen_ip&quot;: &quot;1.2.3.4&quot;,
  10849. &quot;last_seen_ts&quot;: 1474491775024,
  10850. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  10851. }
  10852. </code></pre>
  10853. <p><strong>Parameters</strong></p>
  10854. <p>The following parameters should be set in the URL:</p>
  10855. <ul>
  10856. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  10857. <li><code>device_id</code> - The device to retrieve.</li>
  10858. </ul>
  10859. <p><strong>Response</strong></p>
  10860. <p>The following fields are returned in the JSON response body:</p>
  10861. <ul>
  10862. <li><code>device_id</code> - Identifier of device.</li>
  10863. <li><code>display_name</code> - Display name set by the user for this device.
  10864. Absent if no name has been set.</li>
  10865. <li><code>last_seen_ip</code> - The IP address where this device was last seen.
  10866. (May be a few minutes out of date, for efficiency reasons).</li>
  10867. <li><code>last_seen_ts</code> - The timestamp (in milliseconds since the unix epoch) when this
  10868. devices was last seen. (May be a few minutes out of date, for efficiency reasons).</li>
  10869. <li><code>user_id</code> - Owner of device.</li>
  10870. </ul>
  10871. <h3 id="update-a-device"><a class="header" href="#update-a-device">Update a device</a></h3>
  10872. <p>Updates the metadata on the given <code>device_id</code> for a specific <code>user_id</code>.</p>
  10873. <p>The API is:</p>
  10874. <pre><code>PUT /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  10875. {
  10876. &quot;display_name&quot;: &quot;My other phone&quot;
  10877. }
  10878. </code></pre>
  10879. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10880. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10881. <p>An empty JSON dict is returned.</p>
  10882. <p><strong>Parameters</strong></p>
  10883. <p>The following parameters should be set in the URL:</p>
  10884. <ul>
  10885. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  10886. <li><code>device_id</code> - The device to update.</li>
  10887. </ul>
  10888. <p>The following fields are required in the JSON request body:</p>
  10889. <ul>
  10890. <li><code>display_name</code> - The new display name for this device. If not given,
  10891. the display name is unchanged.</li>
  10892. </ul>
  10893. <h3 id="delete-a-device"><a class="header" href="#delete-a-device">Delete a device</a></h3>
  10894. <p>Deletes the given <code>device_id</code> for a specific <code>user_id</code>,
  10895. and invalidates any access token associated with it.</p>
  10896. <p>The API is:</p>
  10897. <pre><code>DELETE /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  10898. {}
  10899. </code></pre>
  10900. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10901. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10902. <p>An empty JSON dict is returned.</p>
  10903. <p><strong>Parameters</strong></p>
  10904. <p>The following parameters should be set in the URL:</p>
  10905. <ul>
  10906. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  10907. <li><code>device_id</code> - The device to delete.</li>
  10908. </ul>
  10909. <h2 id="list-all-pushers"><a class="header" href="#list-all-pushers">List all pushers</a></h2>
  10910. <p>Gets information about all pushers for a specific <code>user_id</code>.</p>
  10911. <p>The API is:</p>
  10912. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/pushers
  10913. </code></pre>
  10914. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10915. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10916. <p>A response body like the following is returned:</p>
  10917. <pre><code class="language-json">{
  10918. &quot;pushers&quot;: [
  10919. {
  10920. &quot;app_display_name&quot;:&quot;HTTP Push Notifications&quot;,
  10921. &quot;app_id&quot;:&quot;m.http&quot;,
  10922. &quot;data&quot;: {
  10923. &quot;url&quot;:&quot;example.com&quot;
  10924. },
  10925. &quot;device_display_name&quot;:&quot;pushy push&quot;,
  10926. &quot;kind&quot;:&quot;http&quot;,
  10927. &quot;lang&quot;:&quot;None&quot;,
  10928. &quot;profile_tag&quot;:&quot;&quot;,
  10929. &quot;pushkey&quot;:&quot;a@example.com&quot;
  10930. }
  10931. ],
  10932. &quot;total&quot;: 1
  10933. }
  10934. </code></pre>
  10935. <p><strong>Parameters</strong></p>
  10936. <p>The following parameters should be set in the URL:</p>
  10937. <ul>
  10938. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  10939. </ul>
  10940. <p><strong>Response</strong></p>
  10941. <p>The following fields are returned in the JSON response body:</p>
  10942. <ul>
  10943. <li>
  10944. <p><code>pushers</code> - An array containing the current pushers for the user</p>
  10945. <ul>
  10946. <li>
  10947. <p><code>app_display_name</code> - string - A string that will allow the user to identify
  10948. what application owns this pusher.</p>
  10949. </li>
  10950. <li>
  10951. <p><code>app_id</code> - string - This is a reverse-DNS style identifier for the application.
  10952. Max length, 64 chars.</p>
  10953. </li>
  10954. <li>
  10955. <p><code>data</code> - A dictionary of information for the pusher implementation itself.</p>
  10956. <ul>
  10957. <li>
  10958. <p><code>url</code> - string - Required if <code>kind</code> is <code>http</code>. The URL to use to send
  10959. notifications to.</p>
  10960. </li>
  10961. <li>
  10962. <p><code>format</code> - string - The format to use when sending notifications to the
  10963. Push Gateway.</p>
  10964. </li>
  10965. </ul>
  10966. </li>
  10967. <li>
  10968. <p><code>device_display_name</code> - string - A string that will allow the user to identify
  10969. what device owns this pusher.</p>
  10970. </li>
  10971. <li>
  10972. <p><code>profile_tag</code> - string - This string determines which set of device specific rules
  10973. this pusher executes.</p>
  10974. </li>
  10975. <li>
  10976. <p><code>kind</code> - string - The kind of pusher. &quot;http&quot; is a pusher that sends HTTP pokes.</p>
  10977. </li>
  10978. <li>
  10979. <p><code>lang</code> - string - The preferred language for receiving notifications
  10980. (e.g. 'en' or 'en-US')</p>
  10981. </li>
  10982. <li>
  10983. <p><code>profile_tag</code> - string - This string determines which set of device specific rules
  10984. this pusher executes.</p>
  10985. </li>
  10986. <li>
  10987. <p><code>pushkey</code> - string - This is a unique identifier for this pusher.
  10988. Max length, 512 bytes.</p>
  10989. </li>
  10990. </ul>
  10991. </li>
  10992. <li>
  10993. <p><code>total</code> - integer - Number of pushers.</p>
  10994. </li>
  10995. </ul>
  10996. <p>See also the
  10997. <a href="https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushers">Client-Server API Spec on pushers</a>.</p>
  10998. <h2 id="shadow-banning-users"><a class="header" href="#shadow-banning-users">Shadow-banning users</a></h2>
  10999. <p>Shadow-banning is a useful tool for moderating malicious or egregiously abusive users.
  11000. A shadow-banned users receives successful responses to their client-server API requests,
  11001. but the events are not propagated into rooms. This can be an effective tool as it
  11002. (hopefully) takes longer for the user to realise they are being moderated before
  11003. pivoting to another account.</p>
  11004. <p>Shadow-banning a user should be used as a tool of last resort and may lead to confusing
  11005. or broken behaviour for the client. A shadow-banned user will not receive any
  11006. notification and it is generally more appropriate to ban or kick abusive users.
  11007. A shadow-banned user will be unable to contact anyone on the server.</p>
  11008. <p>The API is:</p>
  11009. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/shadow_ban
  11010. </code></pre>
  11011. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  11012. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  11013. <p>An empty JSON dict is returned.</p>
  11014. <p><strong>Parameters</strong></p>
  11015. <p>The following parameters should be set in the URL:</p>
  11016. <ul>
  11017. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  11018. be local.</li>
  11019. </ul>
  11020. <h2 id="override-ratelimiting-for-users"><a class="header" href="#override-ratelimiting-for-users">Override ratelimiting for users</a></h2>
  11021. <p>This API allows to override or disable ratelimiting for a specific user.
  11022. There are specific APIs to set, get and delete a ratelimit.</p>
  11023. <h3 id="get-status-of-ratelimit"><a class="header" href="#get-status-of-ratelimit">Get status of ratelimit</a></h3>
  11024. <p>The API is:</p>
  11025. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  11026. </code></pre>
  11027. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  11028. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  11029. <p>A response body like the following is returned:</p>
  11030. <pre><code class="language-json">{
  11031. &quot;messages_per_second&quot;: 0,
  11032. &quot;burst_count&quot;: 0
  11033. }
  11034. </code></pre>
  11035. <p><strong>Parameters</strong></p>
  11036. <p>The following parameters should be set in the URL:</p>
  11037. <ul>
  11038. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  11039. be local.</li>
  11040. </ul>
  11041. <p><strong>Response</strong></p>
  11042. <p>The following fields are returned in the JSON response body:</p>
  11043. <ul>
  11044. <li><code>messages_per_second</code> - integer - The number of actions that can
  11045. be performed in a second. <code>0</code> mean that ratelimiting is disabled for this user.</li>
  11046. <li><code>burst_count</code> - integer - How many actions that can be performed before
  11047. being limited.</li>
  11048. </ul>
  11049. <p>If <strong>no</strong> custom ratelimit is set, an empty JSON dict is returned.</p>
  11050. <pre><code class="language-json">{}
  11051. </code></pre>
  11052. <h3 id="set-ratelimit"><a class="header" href="#set-ratelimit">Set ratelimit</a></h3>
  11053. <p>The API is:</p>
  11054. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  11055. </code></pre>
  11056. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  11057. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  11058. <p>A response body like the following is returned:</p>
  11059. <pre><code class="language-json">{
  11060. &quot;messages_per_second&quot;: 0,
  11061. &quot;burst_count&quot;: 0
  11062. }
  11063. </code></pre>
  11064. <p><strong>Parameters</strong></p>
  11065. <p>The following parameters should be set in the URL:</p>
  11066. <ul>
  11067. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  11068. be local.</li>
  11069. </ul>
  11070. <p>Body parameters:</p>
  11071. <ul>
  11072. <li><code>messages_per_second</code> - positive integer, optional. The number of actions that can
  11073. be performed in a second. Defaults to <code>0</code>.</li>
  11074. <li><code>burst_count</code> - positive integer, optional. How many actions that can be performed
  11075. before being limited. Defaults to <code>0</code>.</li>
  11076. </ul>
  11077. <p>To disable users' ratelimit set both values to <code>0</code>.</p>
  11078. <p><strong>Response</strong></p>
  11079. <p>The following fields are returned in the JSON response body:</p>
  11080. <ul>
  11081. <li><code>messages_per_second</code> - integer - The number of actions that can
  11082. be performed in a second.</li>
  11083. <li><code>burst_count</code> - integer - How many actions that can be performed before
  11084. being limited.</li>
  11085. </ul>
  11086. <h3 id="delete-ratelimit"><a class="header" href="#delete-ratelimit">Delete ratelimit</a></h3>
  11087. <p>The API is:</p>
  11088. <pre><code>DELETE /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  11089. </code></pre>
  11090. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  11091. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  11092. <p>An empty JSON dict is returned.</p>
  11093. <pre><code class="language-json">{}
  11094. </code></pre>
  11095. <p><strong>Parameters</strong></p>
  11096. <p>The following parameters should be set in the URL:</p>
  11097. <ul>
  11098. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  11099. be local.</li>
  11100. </ul>
  11101. <h3 id="check-username-availability"><a class="header" href="#check-username-availability">Check username availability</a></h3>
  11102. <p>Checks to see if a username is available, and valid, for the server. See <a href="https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-register-available">the client-server
  11103. API</a>
  11104. for more information.</p>
  11105. <p>This endpoint will work even if registration is disabled on the server, unlike
  11106. <code>/_matrix/client/r0/register/available</code>.</p>
  11107. <p>The API is:</p>
  11108. <pre><code>POST /_synapse/admin/v1/username_availabile?username=$localpart
  11109. </code></pre>
  11110. <p>The request and response format is the same as the <a href="https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-register-available">/_matrix/client/r0/register/available</a> API.</p>
  11111. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  11112. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  11113. <div style="break-before: page; page-break-before: always;"></div><h1 id="version-api"><a class="header" href="#version-api">Version API</a></h1>
  11114. <p>This API returns the running Synapse version and the Python version
  11115. on which Synapse is being run. This is useful when a Synapse instance
  11116. is behind a proxy that does not forward the 'Server' header (which also
  11117. contains Synapse version information).</p>
  11118. <p>The api is:</p>
  11119. <pre><code>GET /_synapse/admin/v1/server_version
  11120. </code></pre>
  11121. <p>It returns a JSON body like the following:</p>
  11122. <pre><code class="language-json">{
  11123. &quot;server_version&quot;: &quot;0.99.2rc1 (b=develop, abcdef123)&quot;,
  11124. &quot;python_version&quot;: &quot;3.6.8&quot;
  11125. }
  11126. </code></pre>
  11127. <div style="break-before: page; page-break-before: always;"></div><h1 id="using-the-synapse-manhole"><a class="header" href="#using-the-synapse-manhole">Using the synapse manhole</a></h1>
  11128. <p>The &quot;manhole&quot; allows server administrators to access a Python shell on a running
  11129. Synapse installation. This is a very powerful mechanism for administration and
  11130. debugging.</p>
  11131. <p><strong><em>Security Warning</em></strong></p>
  11132. <p>Note that this will give administrative access to synapse to <strong>all users</strong> with
  11133. shell access to the server. It should therefore <strong>not</strong> be enabled in
  11134. environments where untrusted users have shell access.</p>
  11135. <h2 id="configuring-the-manhole"><a class="header" href="#configuring-the-manhole">Configuring the manhole</a></h2>
  11136. <p>To enable it, first uncomment the <code>manhole</code> listener configuration in
  11137. <code>homeserver.yaml</code>. The configuration is slightly different if you're using docker.</p>
  11138. <h4 id="docker-config"><a class="header" href="#docker-config">Docker config</a></h4>
  11139. <p>If you are using Docker, set <code>bind_addresses</code> to <code>['0.0.0.0']</code> as shown:</p>
  11140. <pre><code class="language-yaml">listeners:
  11141. - port: 9000
  11142. bind_addresses: ['0.0.0.0']
  11143. type: manhole
  11144. </code></pre>
  11145. <p>When using <code>docker run</code> to start the server, you will then need to change the command to the following to include the
  11146. <code>manhole</code> port forwarding. The <code>-p 127.0.0.1:9000:9000</code> below is important: it
  11147. ensures that access to the <code>manhole</code> is only possible for local users.</p>
  11148. <pre><code class="language-bash">docker run -d --name synapse \
  11149. --mount type=volume,src=synapse-data,dst=/data \
  11150. -p 8008:8008 \
  11151. -p 127.0.0.1:9000:9000 \
  11152. matrixdotorg/synapse:latest
  11153. </code></pre>
  11154. <h4 id="native-config"><a class="header" href="#native-config">Native config</a></h4>
  11155. <p>If you are not using docker, set <code>bind_addresses</code> to <code>['::1', '127.0.0.1']</code> as shown.
  11156. The <code>bind_addresses</code> in the example below is important: it ensures that access to the
  11157. <code>manhole</code> is only possible for local users).</p>
  11158. <pre><code class="language-yaml">listeners:
  11159. - port: 9000
  11160. bind_addresses: ['::1', '127.0.0.1']
  11161. type: manhole
  11162. </code></pre>
  11163. <h3 id="security-settings"><a class="header" href="#security-settings">Security settings</a></h3>
  11164. <p>The following config options are available:</p>
  11165. <ul>
  11166. <li><code>username</code> - The username for the manhole (defaults to <code>matrix</code>)</li>
  11167. <li><code>password</code> - The password for the manhole (defaults to <code>rabbithole</code>)</li>
  11168. <li><code>ssh_priv_key</code> - The path to a private SSH key (defaults to a hardcoded value)</li>
  11169. <li><code>ssh_pub_key</code> - The path to a public SSH key (defaults to a hardcoded value)</li>
  11170. </ul>
  11171. <p>For example:</p>
  11172. <pre><code class="language-yaml">manhole_settings:
  11173. username: manhole
  11174. password: mypassword
  11175. ssh_priv_key: &quot;/home/synapse/manhole_keys/id_rsa&quot;
  11176. ssh_pub_key: &quot;/home/synapse/manhole_keys/id_rsa.pub&quot;
  11177. </code></pre>
  11178. <h2 id="accessing-synapse-manhole"><a class="header" href="#accessing-synapse-manhole">Accessing synapse manhole</a></h2>
  11179. <p>Then restart synapse, and point an ssh client at port 9000 on localhost, using
  11180. the username and password configured in <code>homeserver.yaml</code> - with the default
  11181. configuration, this would be:</p>
  11182. <pre><code class="language-bash">ssh -p9000 matrix@localhost
  11183. </code></pre>
  11184. <p>Then enter the password when prompted (the default is <code>rabbithole</code>).</p>
  11185. <p>This gives a Python REPL in which <code>hs</code> gives access to the
  11186. <code>synapse.server.HomeServer</code> object - which in turn gives access to many other
  11187. parts of the process.</p>
  11188. <p>Note that, prior to Synapse 1.41, any call which returns a coroutine will need to be wrapped in <code>ensureDeferred</code>.</p>
  11189. <p>As a simple example, retrieving an event from the database:</p>
  11190. <pre><code class="language-pycon">&gt;&gt;&gt; from twisted.internet import defer
  11191. &gt;&gt;&gt; defer.ensureDeferred(hs.get_datastore().get_event('$1416420717069yeQaw:matrix.org'))
  11192. &lt;Deferred at 0x7ff253fc6998 current result: &lt;FrozenEvent event_id='$1416420717069yeQaw:matrix.org', type='m.room.create', state_key=''&gt;&gt;
  11193. </code></pre>
  11194. <div style="break-before: page; page-break-before: always;"></div><h1 id="how-to-monitor-synapse-metrics-using-prometheus"><a class="header" href="#how-to-monitor-synapse-metrics-using-prometheus">How to monitor Synapse metrics using Prometheus</a></h1>
  11195. <ol>
  11196. <li>
  11197. <p>Install Prometheus:</p>
  11198. <p>Follow instructions at
  11199. <a href="http://prometheus.io/docs/introduction/install/">http://prometheus.io/docs/introduction/install/</a></p>
  11200. </li>
  11201. <li>
  11202. <p>Enable Synapse metrics:</p>
  11203. <p>There are two methods of enabling metrics in Synapse.</p>
  11204. <p>The first serves the metrics as a part of the usual web server and
  11205. can be enabled by adding the &quot;metrics&quot; resource to the existing
  11206. listener as such:</p>
  11207. <pre><code class="language-yaml"> resources:
  11208. - names:
  11209. - client
  11210. - metrics
  11211. </code></pre>
  11212. <p>This provides a simple way of adding metrics to your Synapse
  11213. installation, and serves under <code>/_synapse/metrics</code>. If you do not
  11214. wish your metrics be publicly exposed, you will need to either
  11215. filter it out at your load balancer, or use the second method.</p>
  11216. <p>The second method runs the metrics server on a different port, in a
  11217. different thread to Synapse. This can make it more resilient to
  11218. heavy load meaning metrics cannot be retrieved, and can be exposed
  11219. to just internal networks easier. The served metrics are available
  11220. over HTTP only, and will be available at <code>/_synapse/metrics</code>.</p>
  11221. <p>Add a new listener to homeserver.yaml:</p>
  11222. <pre><code class="language-yaml"> listeners:
  11223. - type: metrics
  11224. port: 9000
  11225. bind_addresses:
  11226. - '0.0.0.0'
  11227. </code></pre>
  11228. <p>For both options, you will need to ensure that <code>enable_metrics</code> is
  11229. set to <code>True</code>.</p>
  11230. </li>
  11231. <li>
  11232. <p>Restart Synapse.</p>
  11233. </li>
  11234. <li>
  11235. <p>Add a Prometheus target for Synapse.</p>
  11236. <p>It needs to set the <code>metrics_path</code> to a non-default value (under
  11237. <code>scrape_configs</code>):</p>
  11238. <pre><code class="language-yaml"> - job_name: &quot;synapse&quot;
  11239. scrape_interval: 15s
  11240. metrics_path: &quot;/_synapse/metrics&quot;
  11241. static_configs:
  11242. - targets: [&quot;my.server.here:port&quot;]
  11243. </code></pre>
  11244. <p>where <code>my.server.here</code> is the IP address of Synapse, and <code>port</code> is
  11245. the listener port configured with the <code>metrics</code> resource.</p>
  11246. <p>If your prometheus is older than 1.5.2, you will need to replace
  11247. <code>static_configs</code> in the above with <code>target_groups</code>.</p>
  11248. </li>
  11249. <li>
  11250. <p>Restart Prometheus.</p>
  11251. </li>
  11252. <li>
  11253. <p>Consider using the <a href="https://github.com/matrix-org/synapse/tree/master/contrib/grafana/">grafana dashboard</a>
  11254. and required <a href="https://github.com/matrix-org/synapse/tree/master/contrib/prometheus/">recording rules</a> </p>
  11255. </li>
  11256. </ol>
  11257. <h2 id="monitoring-workers"><a class="header" href="#monitoring-workers">Monitoring workers</a></h2>
  11258. <p>To monitor a Synapse installation using <a href="workers.html">workers</a>,
  11259. every worker needs to be monitored independently, in addition to
  11260. the main homeserver process. This is because workers don't send
  11261. their metrics to the main homeserver process, but expose them
  11262. directly (if they are configured to do so).</p>
  11263. <p>To allow collecting metrics from a worker, you need to add a
  11264. <code>metrics</code> listener to its configuration, by adding the following
  11265. under <code>worker_listeners</code>:</p>
  11266. <pre><code class="language-yaml"> - type: metrics
  11267. bind_address: ''
  11268. port: 9101
  11269. </code></pre>
  11270. <p>The <code>bind_address</code> and <code>port</code> parameters should be set so that
  11271. the resulting listener can be reached by prometheus, and they
  11272. don't clash with an existing worker.
  11273. With this example, the worker's metrics would then be available
  11274. on <code>http://127.0.0.1:9101</code>.</p>
  11275. <p>Example Prometheus target for Synapse with workers:</p>
  11276. <pre><code class="language-yaml"> - job_name: &quot;synapse&quot;
  11277. scrape_interval: 15s
  11278. metrics_path: &quot;/_synapse/metrics&quot;
  11279. static_configs:
  11280. - targets: [&quot;my.server.here:port&quot;]
  11281. labels:
  11282. instance: &quot;my.server&quot;
  11283. job: &quot;master&quot;
  11284. index: 1
  11285. - targets: [&quot;my.workerserver.here:port&quot;]
  11286. labels:
  11287. instance: &quot;my.server&quot;
  11288. job: &quot;generic_worker&quot;
  11289. index: 1
  11290. - targets: [&quot;my.workerserver.here:port&quot;]
  11291. labels:
  11292. instance: &quot;my.server&quot;
  11293. job: &quot;generic_worker&quot;
  11294. index: 2
  11295. - targets: [&quot;my.workerserver.here:port&quot;]
  11296. labels:
  11297. instance: &quot;my.server&quot;
  11298. job: &quot;media_repository&quot;
  11299. index: 1
  11300. </code></pre>
  11301. <p>Labels (<code>instance</code>, <code>job</code>, <code>index</code>) can be defined as anything.
  11302. The labels are used to group graphs in grafana.</p>
  11303. <h2 id="renaming-of-metrics--deprecation-of-old-names-in-12"><a class="header" href="#renaming-of-metrics--deprecation-of-old-names-in-12">Renaming of metrics &amp; deprecation of old names in 1.2</a></h2>
  11304. <p>Synapse 1.2 updates the Prometheus metrics to match the naming
  11305. convention of the upstream <code>prometheus_client</code>. The old names are
  11306. considered deprecated and will be removed in a future version of
  11307. Synapse.</p>
  11308. <table><thead><tr><th>New Name</th><th>Old Name</th></tr></thead><tbody>
  11309. <tr><td>python_gc_objects_collected_total</td><td>python_gc_objects_collected</td></tr>
  11310. <tr><td>python_gc_objects_uncollectable_total</td><td>python_gc_objects_uncollectable</td></tr>
  11311. <tr><td>python_gc_collections_total</td><td>python_gc_collections</td></tr>
  11312. <tr><td>process_cpu_seconds_total</td><td>process_cpu_seconds</td></tr>
  11313. <tr><td>synapse_federation_client_sent_transactions_total</td><td>synapse_federation_client_sent_transactions</td></tr>
  11314. <tr><td>synapse_federation_client_events_processed_total</td><td>synapse_federation_client_events_processed</td></tr>
  11315. <tr><td>synapse_event_processing_loop_count_total</td><td>synapse_event_processing_loop_count</td></tr>
  11316. <tr><td>synapse_event_processing_loop_room_count_total</td><td>synapse_event_processing_loop_room_count</td></tr>
  11317. <tr><td>synapse_util_metrics_block_count_total</td><td>synapse_util_metrics_block_count</td></tr>
  11318. <tr><td>synapse_util_metrics_block_time_seconds_total</td><td>synapse_util_metrics_block_time_seconds</td></tr>
  11319. <tr><td>synapse_util_metrics_block_ru_utime_seconds_total</td><td>synapse_util_metrics_block_ru_utime_seconds</td></tr>
  11320. <tr><td>synapse_util_metrics_block_ru_stime_seconds_total</td><td>synapse_util_metrics_block_ru_stime_seconds</td></tr>
  11321. <tr><td>synapse_util_metrics_block_db_txn_count_total</td><td>synapse_util_metrics_block_db_txn_count</td></tr>
  11322. <tr><td>synapse_util_metrics_block_db_txn_duration_seconds_total</td><td>synapse_util_metrics_block_db_txn_duration_seconds</td></tr>
  11323. <tr><td>synapse_util_metrics_block_db_sched_duration_seconds_total</td><td>synapse_util_metrics_block_db_sched_duration_seconds</td></tr>
  11324. <tr><td>synapse_background_process_start_count_total</td><td>synapse_background_process_start_count</td></tr>
  11325. <tr><td>synapse_background_process_ru_utime_seconds_total</td><td>synapse_background_process_ru_utime_seconds</td></tr>
  11326. <tr><td>synapse_background_process_ru_stime_seconds_total</td><td>synapse_background_process_ru_stime_seconds</td></tr>
  11327. <tr><td>synapse_background_process_db_txn_count_total</td><td>synapse_background_process_db_txn_count</td></tr>
  11328. <tr><td>synapse_background_process_db_txn_duration_seconds_total</td><td>synapse_background_process_db_txn_duration_seconds</td></tr>
  11329. <tr><td>synapse_background_process_db_sched_duration_seconds_total</td><td>synapse_background_process_db_sched_duration_seconds</td></tr>
  11330. <tr><td>synapse_storage_events_persisted_events_total</td><td>synapse_storage_events_persisted_events</td></tr>
  11331. <tr><td>synapse_storage_events_persisted_events_sep_total</td><td>synapse_storage_events_persisted_events_sep</td></tr>
  11332. <tr><td>synapse_storage_events_state_delta_total</td><td>synapse_storage_events_state_delta</td></tr>
  11333. <tr><td>synapse_storage_events_state_delta_single_event_total</td><td>synapse_storage_events_state_delta_single_event</td></tr>
  11334. <tr><td>synapse_storage_events_state_delta_reuse_delta_total</td><td>synapse_storage_events_state_delta_reuse_delta</td></tr>
  11335. <tr><td>synapse_federation_server_received_pdus_total</td><td>synapse_federation_server_received_pdus</td></tr>
  11336. <tr><td>synapse_federation_server_received_edus_total</td><td>synapse_federation_server_received_edus</td></tr>
  11337. <tr><td>synapse_handler_presence_notified_presence_total</td><td>synapse_handler_presence_notified_presence</td></tr>
  11338. <tr><td>synapse_handler_presence_federation_presence_out_total</td><td>synapse_handler_presence_federation_presence_out</td></tr>
  11339. <tr><td>synapse_handler_presence_presence_updates_total</td><td>synapse_handler_presence_presence_updates</td></tr>
  11340. <tr><td>synapse_handler_presence_timers_fired_total</td><td>synapse_handler_presence_timers_fired</td></tr>
  11341. <tr><td>synapse_handler_presence_federation_presence_total</td><td>synapse_handler_presence_federation_presence</td></tr>
  11342. <tr><td>synapse_handler_presence_bump_active_time_total</td><td>synapse_handler_presence_bump_active_time</td></tr>
  11343. <tr><td>synapse_federation_client_sent_edus_total</td><td>synapse_federation_client_sent_edus</td></tr>
  11344. <tr><td>synapse_federation_client_sent_pdu_destinations_count_total</td><td>synapse_federation_client_sent_pdu_destinations:count</td></tr>
  11345. <tr><td>synapse_federation_client_sent_pdu_destinations_total</td><td>synapse_federation_client_sent_pdu_destinations:total</td></tr>
  11346. <tr><td>synapse_handlers_appservice_events_processed_total</td><td>synapse_handlers_appservice_events_processed</td></tr>
  11347. <tr><td>synapse_notifier_notified_events_total</td><td>synapse_notifier_notified_events</td></tr>
  11348. <tr><td>synapse_push_bulk_push_rule_evaluator_push_rules_invalidation_counter_total</td><td>synapse_push_bulk_push_rule_evaluator_push_rules_invalidation_counter</td></tr>
  11349. <tr><td>synapse_push_bulk_push_rule_evaluator_push_rules_state_size_counter_total</td><td>synapse_push_bulk_push_rule_evaluator_push_rules_state_size_counter</td></tr>
  11350. <tr><td>synapse_http_httppusher_http_pushes_processed_total</td><td>synapse_http_httppusher_http_pushes_processed</td></tr>
  11351. <tr><td>synapse_http_httppusher_http_pushes_failed_total</td><td>synapse_http_httppusher_http_pushes_failed</td></tr>
  11352. <tr><td>synapse_http_httppusher_badge_updates_processed_total</td><td>synapse_http_httppusher_badge_updates_processed</td></tr>
  11353. <tr><td>synapse_http_httppusher_badge_updates_failed_total</td><td>synapse_http_httppusher_badge_updates_failed</td></tr>
  11354. </tbody></table>
  11355. <h2 id="removal-of-deprecated-metrics--time-based-counters-becoming-histograms-in-0310"><a class="header" href="#removal-of-deprecated-metrics--time-based-counters-becoming-histograms-in-0310">Removal of deprecated metrics &amp; time based counters becoming histograms in 0.31.0</a></h2>
  11356. <p>The duplicated metrics deprecated in Synapse 0.27.0 have been removed.</p>
  11357. <p>All time duration-based metrics have been changed to be seconds. This
  11358. affects:</p>
  11359. <table><thead><tr><th>msec -&gt; sec metrics</th></tr></thead><tbody>
  11360. <tr><td>python_gc_time</td></tr>
  11361. <tr><td>python_twisted_reactor_tick_time</td></tr>
  11362. <tr><td>synapse_storage_query_time</td></tr>
  11363. <tr><td>synapse_storage_schedule_time</td></tr>
  11364. <tr><td>synapse_storage_transaction_time</td></tr>
  11365. </tbody></table>
  11366. <p>Several metrics have been changed to be histograms, which sort entries
  11367. into buckets and allow better analysis. The following metrics are now
  11368. histograms:</p>
  11369. <table><thead><tr><th>Altered metrics</th></tr></thead><tbody>
  11370. <tr><td>python_gc_time</td></tr>
  11371. <tr><td>python_twisted_reactor_pending_calls</td></tr>
  11372. <tr><td>python_twisted_reactor_tick_time</td></tr>
  11373. <tr><td>synapse_http_server_response_time_seconds</td></tr>
  11374. <tr><td>synapse_storage_query_time</td></tr>
  11375. <tr><td>synapse_storage_schedule_time</td></tr>
  11376. <tr><td>synapse_storage_transaction_time</td></tr>
  11377. </tbody></table>
  11378. <h2 id="block-and-response-metrics-renamed-for-0270"><a class="header" href="#block-and-response-metrics-renamed-for-0270">Block and response metrics renamed for 0.27.0</a></h2>
  11379. <p>Synapse 0.27.0 begins the process of rationalising the duplicate
  11380. <code>*:count</code> metrics reported for the resource tracking for code blocks and
  11381. HTTP requests.</p>
  11382. <p>At the same time, the corresponding <code>*:total</code> metrics are being renamed,
  11383. as the <code>:total</code> suffix no longer makes sense in the absence of a
  11384. corresponding <code>:count</code> metric.</p>
  11385. <p>To enable a graceful migration path, this release just adds new names
  11386. for the metrics being renamed. A future release will remove the old
  11387. ones.</p>
  11388. <p>The following table shows the new metrics, and the old metrics which
  11389. they are replacing.</p>
  11390. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  11391. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_timer:count</td></tr>
  11392. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_ru_utime:count</td></tr>
  11393. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_ru_stime:count</td></tr>
  11394. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_db_txn_count:count</td></tr>
  11395. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_db_txn_duration:count</td></tr>
  11396. <tr><td>synapse_util_metrics_block_time_seconds</td><td>synapse_util_metrics_block_timer:total</td></tr>
  11397. <tr><td>synapse_util_metrics_block_ru_utime_seconds</td><td>synapse_util_metrics_block_ru_utime:total</td></tr>
  11398. <tr><td>synapse_util_metrics_block_ru_stime_seconds</td><td>synapse_util_metrics_block_ru_stime:total</td></tr>
  11399. <tr><td>synapse_util_metrics_block_db_txn_count</td><td>synapse_util_metrics_block_db_txn_count:total</td></tr>
  11400. <tr><td>synapse_util_metrics_block_db_txn_duration_seconds</td><td>synapse_util_metrics_block_db_txn_duration:total</td></tr>
  11401. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_requests</td></tr>
  11402. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_time:count</td></tr>
  11403. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_ru_utime:count</td></tr>
  11404. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_ru_stime:count</td></tr>
  11405. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_db_txn_count:count</td></tr>
  11406. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_db_txn_duration:count</td></tr>
  11407. <tr><td>synapse_http_server_response_time_seconds</td><td>synapse_http_server_response_time:total</td></tr>
  11408. <tr><td>synapse_http_server_response_ru_utime_seconds</td><td>synapse_http_server_response_ru_utime:total</td></tr>
  11409. <tr><td>synapse_http_server_response_ru_stime_seconds</td><td>synapse_http_server_response_ru_stime:total</td></tr>
  11410. <tr><td>synapse_http_server_response_db_txn_count</td><td>synapse_http_server_response_db_txn_count:total</td></tr>
  11411. <tr><td>synapse_http_server_response_db_txn_duration_seconds</td><td>synapse_http_server_response_db_txn_duration:total</td></tr>
  11412. </tbody></table>
  11413. <h2 id="standard-metric-names"><a class="header" href="#standard-metric-names">Standard Metric Names</a></h2>
  11414. <p>As of synapse version 0.18.2, the format of the process-wide metrics has
  11415. been changed to fit prometheus standard naming conventions. Additionally
  11416. the units have been changed to seconds, from miliseconds.</p>
  11417. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  11418. <tr><td>process_cpu_user_seconds_total</td><td>process_resource_utime / 1000</td></tr>
  11419. <tr><td>process_cpu_system_seconds_total</td><td>process_resource_stime / 1000</td></tr>
  11420. <tr><td>process_open_fds (no 'type' label)</td><td>process_fds</td></tr>
  11421. </tbody></table>
  11422. <p>The python-specific counts of garbage collector performance have been
  11423. renamed.</p>
  11424. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  11425. <tr><td>python_gc_time</td><td>reactor_gc_time</td></tr>
  11426. <tr><td>python_gc_unreachable_total</td><td>reactor_gc_unreachable</td></tr>
  11427. <tr><td>python_gc_counts</td><td>reactor_gc_counts</td></tr>
  11428. </tbody></table>
  11429. <p>The twisted-specific reactor metrics have been renamed.</p>
  11430. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  11431. <tr><td>python_twisted_reactor_pending_calls</td><td>reactor_pending_calls</td></tr>
  11432. <tr><td>python_twisted_reactor_tick_time</td><td>reactor_tick_time</td></tr>
  11433. </tbody></table>
  11434. <div style="break-before: page; page-break-before: always;"></div><h1 id="request-log-format"><a class="header" href="#request-log-format">Request log format</a></h1>
  11435. <p>HTTP request logs are written by synapse (see <a href="usage/administration/../synapse/http/site.py"><code>site.py</code></a> for details).</p>
  11436. <p>See the following for how to decode the dense data available from the default logging configuration.</p>
  11437. <pre><code>2020-10-01 12:00:00,000 - synapse.access.http.8008 - 311 - INFO - PUT-1000- 192.168.0.1 - 8008 - {another-matrix-server.com} Processed request: 0.100sec/-0.000sec (0.000sec, 0.000sec) (0.001sec/0.090sec/3) 11B !200 &quot;PUT /_matrix/federation/v1/send/1600000000000 HTTP/1.1&quot; &quot;Synapse/1.20.1&quot; [0 dbevts]
  11438. -AAAAAAAAAAAAAAAAAAAAA- -BBBBBBBBBBBBBBBBBBBBBB- -C- -DD- -EEEEEE- -FFFFFFFFF- -GG- -HHHHHHHHHHHHHHHHHHHHHHH- -IIIIII- -JJJJJJJ- -KKKKKK-, -LLLLLL- -MMMMMMM- -NNNNNN- O -P- -QQ- -RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR- -SSSSSSSSSSSS- -TTTTTT-
  11439. </code></pre>
  11440. <table><thead><tr><th>Part</th><th>Explanation</th></tr></thead><tbody>
  11441. <tr><td>AAAA</td><td>Timestamp request was logged (not recieved)</td></tr>
  11442. <tr><td>BBBB</td><td>Logger name (<code>synapse.access.(http\|https).&lt;tag&gt;</code>, where 'tag' is defined in the <code>listeners</code> config section, normally the port)</td></tr>
  11443. <tr><td>CCCC</td><td>Line number in code</td></tr>
  11444. <tr><td>DDDD</td><td>Log Level</td></tr>
  11445. <tr><td>EEEE</td><td>Request Identifier (This identifier is shared by related log lines)</td></tr>
  11446. <tr><td>FFFF</td><td>Source IP (Or X-Forwarded-For if enabled)</td></tr>
  11447. <tr><td>GGGG</td><td>Server Port</td></tr>
  11448. <tr><td>HHHH</td><td>Federated Server or Local User making request (blank if unauthenticated or not supplied)</td></tr>
  11449. <tr><td>IIII</td><td>Total Time to process the request</td></tr>
  11450. <tr><td>JJJJ</td><td>Time to send response over network once generated (this may be negative if the socket is closed before the response is generated)</td></tr>
  11451. <tr><td>KKKK</td><td>Userland CPU time</td></tr>
  11452. <tr><td>LLLL</td><td>System CPU time</td></tr>
  11453. <tr><td>MMMM</td><td>Total time waiting for a free DB connection from the pool across all parallel DB work from this request</td></tr>
  11454. <tr><td>NNNN</td><td>Total time waiting for response to DB queries across all parallel DB work from this request</td></tr>
  11455. <tr><td>OOOO</td><td>Count of DB transactions performed</td></tr>
  11456. <tr><td>PPPP</td><td>Response body size</td></tr>
  11457. <tr><td>QQQQ</td><td>Response status code (prefixed with ! if the socket was closed before the response was generated)</td></tr>
  11458. <tr><td>RRRR</td><td>Request</td></tr>
  11459. <tr><td>SSSS</td><td>User-agent</td></tr>
  11460. <tr><td>TTTT</td><td>Events fetched from DB to service this request (note that this does not include events fetched from the cache)</td></tr>
  11461. </tbody></table>
  11462. <p>MMMM / NNNN can be greater than IIII if there are multiple slow database queries
  11463. running in parallel.</p>
  11464. <p>Some actions can result in multiple identical http requests, which will return
  11465. the same data, but only the first request will report time/transactions in
  11466. <code>KKKK</code>/<code>LLLL</code>/<code>MMMM</code>/<code>NNNN</code>/<code>OOOO</code> - the others will be awaiting the first query to return a
  11467. response and will simultaneously return with the first request, but with very
  11468. small processing times.</p>
  11469. <div style="break-before: page; page-break-before: always;"></div><h1 id="contributing"><a class="header" href="#contributing">Contributing</a></h1>
  11470. <p>This document aims to get you started with contributing to Synapse!</p>
  11471. <h1 id="1-who-can-contribute-to-synapse"><a class="header" href="#1-who-can-contribute-to-synapse">1. Who can contribute to Synapse?</a></h1>
  11472. <p>Everyone is welcome to contribute code to <a href="https://github.com/matrix-org">matrix.org
  11473. projects</a>, provided that they are willing to
  11474. license their contributions under the same license as the project itself. We
  11475. follow a simple 'inbound=outbound' model for contributions: the act of
  11476. submitting an 'inbound' contribution means that the contributor agrees to
  11477. license the code under the same terms as the project's overall 'outbound'
  11478. license - in our case, this is almost always Apache Software License v2 (see
  11479. <a href="https://github.com/matrix-org/synapse/blob/develop/LICENSE">LICENSE</a>).</p>
  11480. <h1 id="2-what-do-i-need"><a class="header" href="#2-what-do-i-need">2. What do I need?</a></h1>
  11481. <p>If you are running Windows, the Windows Subsystem for Linux (WSL) is strongly
  11482. recommended for development. More information about WSL can be found at
  11483. <a href="https://docs.microsoft.com/en-us/windows/wsl/install">https://docs.microsoft.com/en-us/windows/wsl/install</a>. Running Synapse natively
  11484. on Windows is not officially supported.</p>
  11485. <p>The code of Synapse is written in Python 3. To do pretty much anything, you'll need <a href="https://wiki.python.org/moin/BeginnersGuide/Download">a recent version of Python 3</a>.</p>
  11486. <p>The source code of Synapse is hosted on GitHub. You will also need <a href="https://github.com/git-guides/install-git">a recent version of git</a>.</p>
  11487. <p>For some tests, you will need <a href="https://docs.docker.com/get-docker/">a recent version of Docker</a>.</p>
  11488. <h1 id="3-get-the-source"><a class="header" href="#3-get-the-source">3. Get the source.</a></h1>
  11489. <p>The preferred and easiest way to contribute changes is to fork the relevant
  11490. project on GitHub, and then <a href="https://help.github.com/articles/using-pull-requests/">create a pull request</a> to ask us to pull your
  11491. changes into our repo.</p>
  11492. <p>Please base your changes on the <code>develop</code> branch.</p>
  11493. <pre><code class="language-sh">git clone git@github.com:YOUR_GITHUB_USER_NAME/synapse.git
  11494. git checkout develop
  11495. </code></pre>
  11496. <p>If you need help getting started with git, this is beyond the scope of the document, but you
  11497. can find many good git tutorials on the web.</p>
  11498. <h1 id="4-install-the-dependencies"><a class="header" href="#4-install-the-dependencies">4. Install the dependencies</a></h1>
  11499. <p>Once you have installed Python 3 and added the source, please open a terminal and
  11500. setup a <em>virtualenv</em>, as follows:</p>
  11501. <pre><code class="language-sh">cd path/where/you/have/cloned/the/repository
  11502. python3 -m venv ./env
  11503. source ./env/bin/activate
  11504. pip install -e &quot;.[all,dev]&quot;
  11505. pip install tox
  11506. </code></pre>
  11507. <p>This will install the developer dependencies for the project.</p>
  11508. <h1 id="5-get-in-touch"><a class="header" href="#5-get-in-touch">5. Get in touch.</a></h1>
  11509. <p>Join our developer community on Matrix: <a href="https://matrix.to/#/#synapse-dev:matrix.org">#synapse-dev:matrix.org</a>!</p>
  11510. <h1 id="6-pick-an-issue"><a class="header" href="#6-pick-an-issue">6. Pick an issue.</a></h1>
  11511. <p>Fix your favorite problem or perhaps find a <a href="https://github.com/matrix-org/synapse/issues?q=is%3Aopen+is%3Aissue+label%3A%22Good+First+Issue%22">Good First Issue</a>
  11512. to work on.</p>
  11513. <h1 id="7-turn-coffee-into-code-and-documentation"><a class="header" href="#7-turn-coffee-into-code-and-documentation">7. Turn coffee into code and documentation!</a></h1>
  11514. <p>There is a growing amount of documentation located in the
  11515. <a href="https://github.com/matrix-org/synapse/tree/develop/docs"><code>docs</code></a>
  11516. directory, with a rendered version <a href="https://matrix-org.github.io/synapse">available online</a>.
  11517. This documentation is intended primarily for sysadmins running their
  11518. own Synapse instance, as well as developers interacting externally with
  11519. Synapse.
  11520. <a href="https://github.com/matrix-org/synapse/tree/develop/docs/development"><code>docs/development</code></a>
  11521. exists primarily to house documentation for
  11522. Synapse developers.
  11523. <a href="https://github.com/matrix-org/synapse/tree/develop/docs/admin_api"><code>docs/admin_api</code></a> houses documentation
  11524. regarding Synapse's Admin API, which is used mostly by sysadmins and external
  11525. service developers.</p>
  11526. <p>Synapse's code style is documented <a href="development/../code_style.html">here</a>. Please follow
  11527. it, including the conventions for the <a href="development/../code_style.html#configuration-file-format">sample configuration
  11528. file</a>.</p>
  11529. <p>We welcome improvements and additions to our documentation itself! When
  11530. writing new pages, please
  11531. <a href="https://github.com/matrix-org/synapse/tree/develop/docs#adding-to-the-documentation">build <code>docs</code> to a book</a>
  11532. to check that your contributions render correctly. The docs are written in
  11533. <a href="https://guides.github.com/features/mastering-markdown/">GitHub-Flavoured Markdown</a>.</p>
  11534. <p>Some documentation also exists in <a href="https://github.com/matrix-org/synapse/wiki">Synapse's GitHub
  11535. Wiki</a>, although this is primarily
  11536. contributed to by community authors.</p>
  11537. <h1 id="8-test-test-test"><a class="header" href="#8-test-test-test">8. Test, test, test!</a></h1>
  11538. <p><a name="test-test-test"></a></p>
  11539. <p>While you're developing and before submitting a patch, you'll
  11540. want to test your code.</p>
  11541. <h2 id="run-the-linters"><a class="header" href="#run-the-linters">Run the linters.</a></h2>
  11542. <p>The linters look at your code and do two things:</p>
  11543. <ul>
  11544. <li>ensure that your code follows the coding style adopted by the project;</li>
  11545. <li>catch a number of errors in your code.</li>
  11546. </ul>
  11547. <p>They're pretty fast, don't hesitate!</p>
  11548. <pre><code class="language-sh">source ./env/bin/activate
  11549. ./scripts-dev/lint.sh
  11550. </code></pre>
  11551. <p>Note that this script <em>will modify your files</em> to fix styling errors.
  11552. Make sure that you have saved all your files.</p>
  11553. <p>If you wish to restrict the linters to only the files changed since the last commit
  11554. (much faster!), you can instead run:</p>
  11555. <pre><code class="language-sh">source ./env/bin/activate
  11556. ./scripts-dev/lint.sh -d
  11557. </code></pre>
  11558. <p>Or if you know exactly which files you wish to lint, you can instead run:</p>
  11559. <pre><code class="language-sh">source ./env/bin/activate
  11560. ./scripts-dev/lint.sh path/to/file1.py path/to/file2.py path/to/folder
  11561. </code></pre>
  11562. <h2 id="run-the-unit-tests-twisted-trial"><a class="header" href="#run-the-unit-tests-twisted-trial">Run the unit tests (Twisted trial).</a></h2>
  11563. <p>The unit tests run parts of Synapse, including your changes, to see if anything
  11564. was broken. They are slower than the linters but will typically catch more errors.</p>
  11565. <pre><code class="language-sh">source ./env/bin/activate
  11566. trial tests
  11567. </code></pre>
  11568. <p>If you wish to only run <em>some</em> unit tests, you may specify
  11569. another module instead of <code>tests</code> - or a test class or a method:</p>
  11570. <pre><code class="language-sh">source ./env/bin/activate
  11571. trial tests.rest.admin.test_room tests.handlers.test_admin.ExfiltrateData.test_invite
  11572. </code></pre>
  11573. <p>If your tests fail, you may wish to look at the logs (the default log level is <code>ERROR</code>):</p>
  11574. <pre><code class="language-sh">less _trial_temp/test.log
  11575. </code></pre>
  11576. <p>To increase the log level for the tests, set <code>SYNAPSE_TEST_LOG_LEVEL</code>:</p>
  11577. <pre><code class="language-sh">SYNAPSE_TEST_LOG_LEVEL=DEBUG trial tests
  11578. </code></pre>
  11579. <h3 id="running-tests-under-postgresql"><a class="header" href="#running-tests-under-postgresql">Running tests under PostgreSQL</a></h3>
  11580. <p>Invoking <code>trial</code> as above will use an in-memory SQLite database. This is great for
  11581. quick development and testing. However, we recommend using a PostgreSQL database
  11582. in production (and indeed, we have some code paths specific to each database).
  11583. This means that we need to run our unit tests against PostgreSQL too. Our CI does
  11584. this automatically for pull requests and release candidates, but it's sometimes
  11585. useful to reproduce this locally.</p>
  11586. <p>To do so, <a href="development/../postgres.html">configure Postgres</a> and run <code>trial</code> with the
  11587. following environment variables matching your configuration:</p>
  11588. <ul>
  11589. <li><code>SYNAPSE_POSTGRES</code> to anything nonempty</li>
  11590. <li><code>SYNAPSE_POSTGRES_HOST</code></li>
  11591. <li><code>SYNAPSE_POSTGRES_USER</code></li>
  11592. <li><code>SYNAPSE_POSTGRES_PASSWORD</code></li>
  11593. </ul>
  11594. <p>For example:</p>
  11595. <pre><code class="language-shell">export SYNAPSE_POSTGRES=1
  11596. export SYNAPSE_POSTGRES_HOST=localhost
  11597. export SYNAPSE_POSTGRES_USER=postgres
  11598. export SYNAPSE_POSTGRES_PASSWORD=mydevenvpassword
  11599. trial
  11600. </code></pre>
  11601. <h4 id="prebuilt-container"><a class="header" href="#prebuilt-container">Prebuilt container</a></h4>
  11602. <p>Since configuring PostgreSQL can be fiddly, we can make use of a pre-made
  11603. Docker container to set up PostgreSQL and run our tests for us. To do so, run</p>
  11604. <pre><code class="language-shell">scripts-dev/test_postgresql.sh
  11605. </code></pre>
  11606. <p>Any extra arguments to the script will be passed to <code>tox</code> and then to <code>trial</code>,
  11607. so we can run a specific test in this container with e.g.</p>
  11608. <pre><code class="language-shell">scripts-dev/test_postgresql.sh tests.replication.test_sharded_event_persister.EventPersisterShardTestCase
  11609. </code></pre>
  11610. <p>The container creates a folder in your Synapse checkout called
  11611. <code>.tox-pg-container</code> and uses this as a tox environment. The output of any
  11612. <code>trial</code> runs goes into <code>_trial_temp</code> in your synapse source directory — the same
  11613. as running <code>trial</code> directly on your host machine.</p>
  11614. <h2 id="run-the-integration-tests-sytest"><a class="header" href="#run-the-integration-tests-sytest">Run the integration tests (<a href="https://github.com/matrix-org/sytest">Sytest</a>).</a></h2>
  11615. <p>The integration tests are a more comprehensive suite of tests. They
  11616. run a full version of Synapse, including your changes, to check if
  11617. anything was broken. They are slower than the unit tests but will
  11618. typically catch more errors.</p>
  11619. <p>The following command will let you run the integration test with the most common
  11620. configuration:</p>
  11621. <pre><code class="language-sh">$ docker run --rm -it -v /path/where/you/have/cloned/the/repository\:/src:ro -v /path/to/where/you/want/logs\:/logs matrixdotorg/sytest-synapse:buster
  11622. </code></pre>
  11623. <p>This configuration should generally cover your needs. For more details about other configurations, see <a href="https://github.com/matrix-org/sytest/blob/develop/docker/README.md">documentation in the SyTest repo</a>.</p>
  11624. <h2 id="run-the-integration-tests-complement"><a class="header" href="#run-the-integration-tests-complement">Run the integration tests (<a href="https://github.com/matrix-org/complement">Complement</a>).</a></h2>
  11625. <p><a href="https://github.com/matrix-org/complement">Complement</a> is a suite of black box tests that can be run on any homeserver implementation. It can also be thought of as end-to-end (e2e) tests.</p>
  11626. <p>It's often nice to develop on Synapse and write Complement tests at the same time.
  11627. Here is how to run your local Synapse checkout against your local Complement checkout.</p>
  11628. <p>(checkout <a href="https://github.com/matrix-org/complement"><code>complement</code></a> alongside your <code>synapse</code> checkout)</p>
  11629. <pre><code class="language-sh">COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh
  11630. </code></pre>
  11631. <p>To run a specific test file, you can pass the test name at the end of the command. The name passed comes from the naming structure in your Complement tests. If you're unsure of the name, you can do a full run and copy it from the test output:</p>
  11632. <pre><code class="language-sh">COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh TestBackfillingHistory
  11633. </code></pre>
  11634. <p>To run a specific test, you can specify the whole name structure:</p>
  11635. <pre><code class="language-sh">COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh TestBackfillingHistory/parallel/Backfilled_historical_events_resolve_with_proper_state_in_correct_order
  11636. </code></pre>
  11637. <h3 id="access-database-for-homeserver-after-complement-test-runs"><a class="header" href="#access-database-for-homeserver-after-complement-test-runs">Access database for homeserver after Complement test runs.</a></h3>
  11638. <p>If you're curious what the database looks like after you run some tests, here are some steps to get you going in Synapse:</p>
  11639. <ol>
  11640. <li>In your Complement test comment out <code>defer deployment.Destroy(t)</code> and replace with <code>defer time.Sleep(2 * time.Hour)</code> to keep the homeserver running after the tests complete</li>
  11641. <li>Start the Complement tests</li>
  11642. <li>Find the name of the container, <code>docker ps -f name=complement_</code> (this will filter for just the Compelement related Docker containers)</li>
  11643. <li>Access the container replacing the name with what you found in the previous step: <code>docker exec -it complement_1_hs_with_application_service.hs1_2 /bin/bash</code></li>
  11644. <li>Install sqlite (database driver), <code>apt-get update &amp;&amp; apt-get install -y sqlite3</code></li>
  11645. <li>Then run <code>sqlite3</code> and open the database <code>.open /conf/homeserver.db</code> (this db path comes from the Synapse homeserver.yaml)</li>
  11646. </ol>
  11647. <h1 id="9-submit-your-patch"><a class="header" href="#9-submit-your-patch">9. Submit your patch.</a></h1>
  11648. <p>Once you're happy with your patch, it's time to prepare a Pull Request.</p>
  11649. <p>To prepare a Pull Request, please:</p>
  11650. <ol>
  11651. <li>verify that <a href="development/contributing_guide.html#test-test-test">all the tests pass</a>, including the coding style;</li>
  11652. <li><a href="development/contributing_guide.html#sign-off">sign off</a> your contribution;</li>
  11653. <li><code>git push</code> your commit to your fork of Synapse;</li>
  11654. <li>on GitHub, <a href="https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request">create the Pull Request</a>;</li>
  11655. <li>add a <a href="development/contributing_guide.html#changelog">changelog entry</a> and push it to your Pull Request;</li>
  11656. <li>for most contributors, that's all - however, if you are a member of the organization <code>matrix-org</code>, on GitHub, please request a review from <code>matrix.org / Synapse Core</code>.</li>
  11657. <li>if you need to update your PR, please avoid rebasing and just add new commits to your branch.</li>
  11658. </ol>
  11659. <h2 id="changelog"><a class="header" href="#changelog">Changelog</a></h2>
  11660. <p>All changes, even minor ones, need a corresponding changelog / newsfragment
  11661. entry. These are managed by <a href="https://github.com/hawkowl/towncrier">Towncrier</a>.</p>
  11662. <p>To create a changelog entry, make a new file in the <code>changelog.d</code> directory named
  11663. in the format of <code>PRnumber.type</code>. The type can be one of the following:</p>
  11664. <ul>
  11665. <li><code>feature</code></li>
  11666. <li><code>bugfix</code></li>
  11667. <li><code>docker</code> (for updates to the Docker image)</li>
  11668. <li><code>doc</code> (for updates to the documentation)</li>
  11669. <li><code>removal</code> (also used for deprecations)</li>
  11670. <li><code>misc</code> (for internal-only changes)</li>
  11671. </ul>
  11672. <p>This file will become part of our <a href="https://github.com/matrix-org/synapse/blob/master/CHANGES.md">changelog</a> at the next
  11673. release, so the content of the file should be a short description of your
  11674. change in the same style as the rest of the changelog. The file can contain Markdown
  11675. formatting, and should end with a full stop (.) or an exclamation mark (!) for
  11676. consistency.</p>
  11677. <p>Adding credits to the changelog is encouraged, we value your
  11678. contributions and would like to have you shouted out in the release notes!</p>
  11679. <p>For example, a fix in PR #1234 would have its changelog entry in
  11680. <code>changelog.d/1234.bugfix</code>, and contain content like:</p>
  11681. <blockquote>
  11682. <p>The security levels of Florbs are now validated when received
  11683. via the <code>/federation/florb</code> endpoint. Contributed by Jane Matrix.</p>
  11684. </blockquote>
  11685. <p>If there are multiple pull requests involved in a single bugfix/feature/etc,
  11686. then the content for each <code>changelog.d</code> file should be the same. Towncrier will
  11687. merge the matching files together into a single changelog entry when we come to
  11688. release.</p>
  11689. <h3 id="how-do-i-know-what-to-call-the-changelog-file-before-i-create-the-pr"><a class="header" href="#how-do-i-know-what-to-call-the-changelog-file-before-i-create-the-pr">How do I know what to call the changelog file before I create the PR?</a></h3>
  11690. <p>Obviously, you don't know if you should call your newsfile
  11691. <code>1234.bugfix</code> or <code>5678.bugfix</code> until you create the PR, which leads to a
  11692. chicken-and-egg problem.</p>
  11693. <p>There are two options for solving this:</p>
  11694. <ol>
  11695. <li>
  11696. <p>Open the PR without a changelog file, see what number you got, and <em>then</em>
  11697. add the changelog file to your branch (see <a href="development/contributing_guide.html#updating-your-pull-request">Updating your pull
  11698. request</a>), or:</p>
  11699. </li>
  11700. <li>
  11701. <p>Look at the <a href="https://github.com/matrix-org/synapse/issues?q=">list of all
  11702. issues/PRs</a>, add one to the
  11703. highest number you see, and quickly open the PR before somebody else claims
  11704. your number.</p>
  11705. <p><a href="https://github.com/richvdh/scripts/blob/master/next_github_number.sh">This
  11706. script</a>
  11707. might be helpful if you find yourself doing this a lot.</p>
  11708. </li>
  11709. </ol>
  11710. <p>Sorry, we know it's a bit fiddly, but it's <em>really</em> helpful for us when we come
  11711. to put together a release!</p>
  11712. <h3 id="debian-changelog"><a class="header" href="#debian-changelog">Debian changelog</a></h3>
  11713. <p>Changes which affect the debian packaging files (in <code>debian</code>) are an
  11714. exception to the rule that all changes require a <code>changelog.d</code> file.</p>
  11715. <p>In this case, you will need to add an entry to the debian changelog for the
  11716. next release. For this, run the following command:</p>
  11717. <pre><code>dch
  11718. </code></pre>
  11719. <p>This will make up a new version number (if there isn't already an unreleased
  11720. version in flight), and open an editor where you can add a new changelog entry.
  11721. (Our release process will ensure that the version number and maintainer name is
  11722. corrected for the release.)</p>
  11723. <p>If your change affects both the debian packaging <em>and</em> files outside the debian
  11724. directory, you will need both a regular newsfragment <em>and</em> an entry in the
  11725. debian changelog. (Though typically such changes should be submitted as two
  11726. separate pull requests.)</p>
  11727. <h2 id="sign-off"><a class="header" href="#sign-off">Sign off</a></h2>
  11728. <p>In order to have a concrete record that your contribution is intentional
  11729. and you agree to license it under the same terms as the project's license, we've adopted the
  11730. same lightweight approach that the Linux Kernel
  11731. <a href="https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin%3E">submitting patches process</a>,
  11732. <a href="https://github.com/docker/docker/blob/master/CONTRIBUTING.md">Docker</a>, and many other
  11733. projects use: the DCO (Developer Certificate of Origin:
  11734. http://developercertificate.org/). This is a simple declaration that you wrote
  11735. the contribution or otherwise have the right to contribute it to Matrix:</p>
  11736. <pre><code>Developer Certificate of Origin
  11737. Version 1.1
  11738. Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
  11739. 660 York Street, Suite 102,
  11740. San Francisco, CA 94110 USA
  11741. Everyone is permitted to copy and distribute verbatim copies of this
  11742. license document, but changing it is not allowed.
  11743. Developer's Certificate of Origin 1.1
  11744. By making a contribution to this project, I certify that:
  11745. (a) The contribution was created in whole or in part by me and I
  11746. have the right to submit it under the open source license
  11747. indicated in the file; or
  11748. (b) The contribution is based upon previous work that, to the best
  11749. of my knowledge, is covered under an appropriate open source
  11750. license and I have the right under that license to submit that
  11751. work with modifications, whether created in whole or in part
  11752. by me, under the same open source license (unless I am
  11753. permitted to submit under a different license), as indicated
  11754. in the file; or
  11755. (c) The contribution was provided directly to me by some other
  11756. person who certified (a), (b) or (c) and I have not modified
  11757. it.
  11758. (d) I understand and agree that this project and the contribution
  11759. are public and that a record of the contribution (including all
  11760. personal information I submit with it, including my sign-off) is
  11761. maintained indefinitely and may be redistributed consistent with
  11762. this project or the open source license(s) involved.
  11763. </code></pre>
  11764. <p>If you agree to this for your contribution, then all that's needed is to
  11765. include the line in your commit or pull request comment:</p>
  11766. <pre><code>Signed-off-by: Your Name &lt;your@email.example.org&gt;
  11767. </code></pre>
  11768. <p>We accept contributions under a legally identifiable name, such as
  11769. your name on government documentation or common-law names (names
  11770. claimed by legitimate usage or repute). Unfortunately, we cannot
  11771. accept anonymous contributions at this time.</p>
  11772. <p>Git allows you to add this signoff automatically when using the <code>-s</code>
  11773. flag to <code>git commit</code>, which uses the name and email set in your
  11774. <code>user.name</code> and <code>user.email</code> git configs.</p>
  11775. <h1 id="10-turn-feedback-into-better-code"><a class="header" href="#10-turn-feedback-into-better-code">10. Turn feedback into better code.</a></h1>
  11776. <p>Once the Pull Request is opened, you will see a few things:</p>
  11777. <ol>
  11778. <li>our automated CI (Continuous Integration) pipeline will run (again) the linters, the unit tests, the integration tests and more;</li>
  11779. <li>one or more of the developers will take a look at your Pull Request and offer feedback.</li>
  11780. </ol>
  11781. <p>From this point, you should:</p>
  11782. <ol>
  11783. <li>Look at the results of the CI pipeline.
  11784. <ul>
  11785. <li>If there is any error, fix the error.</li>
  11786. </ul>
  11787. </li>
  11788. <li>If a developer has requested changes, make these changes and let us know if it is ready for a developer to review again.</li>
  11789. <li>Create a new commit with the changes.
  11790. <ul>
  11791. <li>Please do NOT overwrite the history. New commits make the reviewer's life easier.</li>
  11792. <li>Push this commits to your Pull Request.</li>
  11793. </ul>
  11794. </li>
  11795. <li>Back to 1.</li>
  11796. </ol>
  11797. <p>Once both the CI and the developers are happy, the patch will be merged into Synapse and released shortly!</p>
  11798. <h1 id="11-find-a-new-issue"><a class="header" href="#11-find-a-new-issue">11. Find a new issue.</a></h1>
  11799. <p>By now, you know the drill!</p>
  11800. <h1 id="notes-for-maintainers-on-merging-prs-etc"><a class="header" href="#notes-for-maintainers-on-merging-prs-etc">Notes for maintainers on merging PRs etc</a></h1>
  11801. <p>There are some notes for those with commit access to the project on how we
  11802. manage git <a href="development/git.html">here</a>.</p>
  11803. <h1 id="conclusion"><a class="header" href="#conclusion">Conclusion</a></h1>
  11804. <p>That's it! Matrix is a very open and collaborative project as you might expect
  11805. given our obsession with open communication. If we're going to successfully
  11806. matrix together all the fragmented communication technologies out there we are
  11807. reliant on contributions and collaboration from the community to do so. So
  11808. please get involved - and we hope you have as much fun hacking on Matrix as we
  11809. do!</p>
  11810. <div style="break-before: page; page-break-before: always;"></div><h1 id="code-style"><a class="header" href="#code-style">Code Style</a></h1>
  11811. <h2 id="formatting-tools"><a class="header" href="#formatting-tools">Formatting tools</a></h2>
  11812. <p>The Synapse codebase uses a number of code formatting tools in order to
  11813. quickly and automatically check for formatting (and sometimes logical)
  11814. errors in code.</p>
  11815. <p>The necessary tools are detailed below.</p>
  11816. <p>First install them with:</p>
  11817. <pre><code class="language-sh">pip install -e &quot;.[lint,mypy]&quot;
  11818. </code></pre>
  11819. <ul>
  11820. <li>
  11821. <p><strong>black</strong></p>
  11822. <p>The Synapse codebase uses <a href="https://pypi.org/project/black/">black</a>
  11823. as an opinionated code formatter, ensuring all comitted code is
  11824. properly formatted.</p>
  11825. <p>Have <code>black</code> auto-format your code (it shouldn't change any
  11826. functionality) with:</p>
  11827. <pre><code class="language-sh">black . --exclude=&quot;\.tox|build|env&quot;
  11828. </code></pre>
  11829. </li>
  11830. <li>
  11831. <p><strong>flake8</strong></p>
  11832. <p><code>flake8</code> is a code checking tool. We require code to pass <code>flake8</code>
  11833. before being merged into the codebase.</p>
  11834. <p>Check all application and test code with:</p>
  11835. <pre><code class="language-sh">flake8 synapse tests
  11836. </code></pre>
  11837. </li>
  11838. <li>
  11839. <p><strong>isort</strong></p>
  11840. <p><code>isort</code> ensures imports are nicely formatted, and can suggest and
  11841. auto-fix issues such as double-importing.</p>
  11842. <p>Auto-fix imports with:</p>
  11843. <pre><code class="language-sh">isort -rc synapse tests
  11844. </code></pre>
  11845. <p><code>-rc</code> means to recursively search the given directories.</p>
  11846. </li>
  11847. </ul>
  11848. <p>It's worth noting that modern IDEs and text editors can run these tools
  11849. automatically on save. It may be worth looking into whether this
  11850. functionality is supported in your editor for a more convenient
  11851. development workflow. It is not, however, recommended to run <code>flake8</code> on
  11852. save as it takes a while and is very resource intensive.</p>
  11853. <h2 id="general-rules"><a class="header" href="#general-rules">General rules</a></h2>
  11854. <ul>
  11855. <li><strong>Naming</strong>:
  11856. <ul>
  11857. <li>Use camel case for class and type names</li>
  11858. <li>Use underscores for functions and variables.</li>
  11859. </ul>
  11860. </li>
  11861. <li><strong>Docstrings</strong>: should follow the <a href="https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings">google code
  11862. style</a>.
  11863. See the
  11864. <a href="http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html">examples</a>
  11865. in the sphinx documentation.</li>
  11866. <li><strong>Imports</strong>:
  11867. <ul>
  11868. <li>
  11869. <p>Imports should be sorted by <code>isort</code> as described above.</p>
  11870. </li>
  11871. <li>
  11872. <p>Prefer to import classes and functions rather than packages or
  11873. modules.</p>
  11874. <p>Example:</p>
  11875. <pre><code class="language-python">from synapse.types import UserID
  11876. ...
  11877. user_id = UserID(local, server)
  11878. </code></pre>
  11879. <p>is preferred over:</p>
  11880. <pre><code class="language-python">from synapse import types
  11881. ...
  11882. user_id = types.UserID(local, server)
  11883. </code></pre>
  11884. <p>(or any other variant).</p>
  11885. <p>This goes against the advice in the Google style guide, but it
  11886. means that errors in the name are caught early (at import time).</p>
  11887. </li>
  11888. <li>
  11889. <p>Avoid wildcard imports (<code>from synapse.types import *</code>) and
  11890. relative imports (<code>from .types import UserID</code>).</p>
  11891. </li>
  11892. </ul>
  11893. </li>
  11894. </ul>
  11895. <h2 id="configuration-file-format"><a class="header" href="#configuration-file-format">Configuration file format</a></h2>
  11896. <p>The <a href="./sample_config.yaml">sample configuration file</a> acts as a
  11897. reference to Synapse's configuration options for server administrators.
  11898. Remember that many readers will be unfamiliar with YAML and server
  11899. administration in general, so that it is important that the file be as
  11900. easy to understand as possible, which includes following a consistent
  11901. format.</p>
  11902. <p>Some guidelines follow:</p>
  11903. <ul>
  11904. <li>
  11905. <p>Sections should be separated with a heading consisting of a single
  11906. line prefixed and suffixed with <code>##</code>. There should be <strong>two</strong> blank
  11907. lines before the section header, and <strong>one</strong> after.</p>
  11908. </li>
  11909. <li>
  11910. <p>Each option should be listed in the file with the following format:</p>
  11911. <ul>
  11912. <li>
  11913. <p>A comment describing the setting. Each line of this comment
  11914. should be prefixed with a hash (<code>#</code>) and a space.</p>
  11915. <p>The comment should describe the default behaviour (ie, what
  11916. happens if the setting is omitted), as well as what the effect
  11917. will be if the setting is changed.</p>
  11918. <p>Often, the comment end with something like &quot;uncomment the
  11919. following to <do action>&quot;.</p>
  11920. </li>
  11921. <li>
  11922. <p>A line consisting of only <code>#</code>.</p>
  11923. </li>
  11924. <li>
  11925. <p>A commented-out example setting, prefixed with only <code>#</code>.</p>
  11926. <p>For boolean (on/off) options, convention is that this example
  11927. should be the <em>opposite</em> to the default (so the comment will end
  11928. with &quot;Uncomment the following to enable [or disable]
  11929. <feature>.&quot; For other options, the example should give some
  11930. non-default value which is likely to be useful to the reader.</p>
  11931. </li>
  11932. </ul>
  11933. </li>
  11934. <li>
  11935. <p>There should be a blank line between each option.</p>
  11936. </li>
  11937. <li>
  11938. <p>Where several settings are grouped into a single dict, <em>avoid</em> the
  11939. convention where the whole block is commented out, resulting in
  11940. comment lines starting <code># #</code>, as this is hard to read and confusing
  11941. to edit. Instead, leave the top-level config option uncommented, and
  11942. follow the conventions above for sub-options. Ensure that your code
  11943. correctly handles the top-level option being set to <code>None</code> (as it
  11944. will be if no sub-options are enabled).</p>
  11945. </li>
  11946. <li>
  11947. <p>Lines should be wrapped at 80 characters.</p>
  11948. </li>
  11949. <li>
  11950. <p>Use two-space indents.</p>
  11951. </li>
  11952. <li>
  11953. <p><code>true</code> and <code>false</code> are spelt thus (as opposed to <code>True</code>, etc.)</p>
  11954. </li>
  11955. <li>
  11956. <p>Use single quotes (<code>'</code>) rather than double-quotes (<code>&quot;</code>) or backticks
  11957. (<code>`</code>) to refer to configuration options.</p>
  11958. </li>
  11959. </ul>
  11960. <p>Example:</p>
  11961. <pre><code class="language-yaml">## Frobnication ##
  11962. # The frobnicator will ensure that all requests are fully frobnicated.
  11963. # To enable it, uncomment the following.
  11964. #
  11965. #frobnicator_enabled: true
  11966. # By default, the frobnicator will frobnicate with the default frobber.
  11967. # The following will make it use an alternative frobber.
  11968. #
  11969. #frobincator_frobber: special_frobber
  11970. # Settings for the frobber
  11971. #
  11972. frobber:
  11973. # frobbing speed. Defaults to 1.
  11974. #
  11975. #speed: 10
  11976. # frobbing distance. Defaults to 1000.
  11977. #
  11978. #distance: 100
  11979. </code></pre>
  11980. <p>Note that the sample configuration is generated from the synapse code
  11981. and is maintained by a script, <code>scripts-dev/generate_sample_config</code>.
  11982. Making sure that the output from this script matches the desired format
  11983. is left as an exercise for the reader!</p>
  11984. <div style="break-before: page; page-break-before: always;"></div><h1 id="some-notes-on-how-we-use-git"><a class="header" href="#some-notes-on-how-we-use-git">Some notes on how we use git</a></h1>
  11985. <h2 id="on-keeping-the-commit-history-clean"><a class="header" href="#on-keeping-the-commit-history-clean">On keeping the commit history clean</a></h2>
  11986. <p>In an ideal world, our git commit history would be a linear progression of
  11987. commits each of which contains a single change building on what came
  11988. before. Here, by way of an arbitrary example, is the top of <code>git log --graph b2dba0607</code>:</p>
  11989. <img src="development/img/git/clean.png" alt="clean git graph" width="500px">
  11990. <p>Note how the commit comment explains clearly what is changing and why. Also
  11991. note the <em>absence</em> of merge commits, as well as the absence of commits called
  11992. things like (to pick a few culprits):
  11993. <a href="https://github.com/matrix-org/synapse/commit/84691da6c">“pep8”</a>, <a href="https://github.com/matrix-org/synapse/commit/474810d9d">“fix broken
  11994. test”</a>,
  11995. <a href="https://github.com/matrix-org/synapse/commit/c9d72e457">“oops”</a>,
  11996. <a href="https://github.com/matrix-org/synapse/commit/836358823">“typo”</a>, or <a href="https://github.com/matrix-org/synapse/commit/707374d5d">“Who's
  11997. the president?”</a>.</p>
  11998. <p>There are a number of reasons why keeping a clean commit history is a good
  11999. thing:</p>
  12000. <ul>
  12001. <li>
  12002. <p>From time to time, after a change lands, it turns out to be necessary to
  12003. revert it, or to backport it to a release branch. Those operations are
  12004. <em>much</em> easier when the change is contained in a single commit.</p>
  12005. </li>
  12006. <li>
  12007. <p>Similarly, it's much easier to answer questions like “is the fix for
  12008. <code>/publicRooms</code> on the release branch?” if that change consists of a single
  12009. commit.</p>
  12010. </li>
  12011. <li>
  12012. <p>Likewise: “what has changed on this branch in the last week?” is much
  12013. clearer without merges and “pep8” commits everywhere.</p>
  12014. </li>
  12015. <li>
  12016. <p>Sometimes we need to figure out where a bug got introduced, or some
  12017. behaviour changed. One way of doing that is with <code>git bisect</code>: pick an
  12018. arbitrary commit between the known good point and the known bad point, and
  12019. see how the code behaves. However, that strategy fails if the commit you
  12020. chose is the middle of someone's epic branch in which they broke the world
  12021. before putting it back together again.</p>
  12022. </li>
  12023. </ul>
  12024. <p>One counterargument is that it is sometimes useful to see how a PR evolved as
  12025. it went through review cycles. This is true, but that information is always
  12026. available via the GitHub UI (or via the little-known <a href="https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally">refs/pull
  12027. namespace</a>).</p>
  12028. <p>Of course, in reality, things are more complicated than that. We have release
  12029. branches as well as <code>develop</code> and <code>master</code>, and we deliberately merge changes
  12030. between them. Bugs often slip through and have to be fixed later. That's all
  12031. fine: this not a cast-iron rule which must be obeyed, but an ideal to aim
  12032. towards.</p>
  12033. <h2 id="merges-squashes-rebases-wtf"><a class="header" href="#merges-squashes-rebases-wtf">Merges, squashes, rebases: wtf?</a></h2>
  12034. <p>Ok, so that's what we'd like to achieve. How do we achieve it?</p>
  12035. <p>The TL;DR is: when you come to merge a pull request, you <em>probably</em> want to
  12036. “squash and merge”:</p>
  12037. <p><img src="development/img/git/squash.png" alt="squash and merge" />.</p>
  12038. <p>(This applies whether you are merging your own PR, or that of another
  12039. contributor.)</p>
  12040. <p>“Squash and merge”<sup id="a1"><a href="development/git.html#f1">1</a></sup> takes all of the changes in the
  12041. PR, and bundles them into a single commit. GitHub gives you the opportunity to
  12042. edit the commit message before you confirm, and normally you should do so,
  12043. because the default will be useless (again: <code>* woops typo</code> is not a useful
  12044. thing to keep in the historical record).</p>
  12045. <p>The main problem with this approach comes when you have a series of pull
  12046. requests which build on top of one another: as soon as you squash-merge the
  12047. first PR, you'll end up with a stack of conflicts to resolve in all of the
  12048. others. In general, it's best to avoid this situation in the first place by
  12049. trying not to have multiple related PRs in flight at the same time. Still,
  12050. sometimes that's not possible and doing a regular merge is the lesser evil.</p>
  12051. <p>Another occasion in which a regular merge makes more sense is a PR where you've
  12052. deliberately created a series of commits each of which makes sense in its own
  12053. right. For example: <a href="https://github.com/matrix-org/synapse/pull/6837">a PR which gradually propagates a refactoring operation
  12054. through the codebase</a>, or <a href="https://github.com/matrix-org/synapse/pull/5987">a
  12055. PR which is the culmination of several other
  12056. PRs</a>. In this case the ability
  12057. to figure out when a particular change/bug was introduced could be very useful.</p>
  12058. <p>Ultimately: <strong>this is not a hard-and-fast-rule</strong>. If in doubt, ask yourself “do
  12059. each of the commits I am about to merge make sense in their own right”, but
  12060. remember that we're just doing our best to balance “keeping the commit history
  12061. clean” with other factors.</p>
  12062. <h2 id="git-branching-model"><a class="header" href="#git-branching-model">Git branching model</a></h2>
  12063. <p>A <a href="https://nvie.com/posts/a-successful-git-branching-model/">lot</a>
  12064. <a href="http://scottchacon.com/2011/08/31/github-flow.html">of</a>
  12065. <a href="https://www.endoflineblog.com/gitflow-considered-harmful">words</a> have been
  12066. written in the past about git branching models (no really, <a href="https://martinfowler.com/articles/branching-patterns.html">a
  12067. lot</a>). I tend to
  12068. think the whole thing is overblown. Fundamentally, it's not that
  12069. complicated. Here's how we do it.</p>
  12070. <p>Let's start with a picture:</p>
  12071. <p><img src="development/img/git/branches.jpg" alt="branching model" /></p>
  12072. <p>It looks complicated, but it's really not. There's one basic rule: <em>anyone</em> is
  12073. free to merge from <em>any</em> more-stable branch to <em>any</em> less-stable branch at
  12074. <em>any</em> time<sup id="a2"><a href="development/git.html#f2">2</a></sup>. (The principle behind this is that if a
  12075. change is good enough for the more-stable branch, then it's also good enough go
  12076. put in a less-stable branch.)</p>
  12077. <p>Meanwhile, merging (or squashing, as per the above) from a less-stable to a
  12078. more-stable branch is a deliberate action in which you want to publish a change
  12079. or a set of changes to (some subset of) the world: for example, this happens
  12080. when a PR is landed, or as part of our release process.</p>
  12081. <p>So, what counts as a more- or less-stable branch? A little reflection will show
  12082. that our active branches are ordered thus, from more-stable to less-stable:</p>
  12083. <ul>
  12084. <li><code>master</code> (tracks our last release).</li>
  12085. <li><code>release-vX.Y</code> (the branch where we prepare the next release)<sup
  12086. id="a3"><a href="development/git.html#f3">3</a></sup>.</li>
  12087. <li>PR branches which are targeting the release.</li>
  12088. <li><code>develop</code> (our &quot;mainline&quot; branch containing our bleeding-edge).</li>
  12089. <li>regular PR branches.</li>
  12090. </ul>
  12091. <p>The corollary is: if you have a bugfix that needs to land in both
  12092. <code>release-vX.Y</code> <em>and</em> <code>develop</code>, then you should base your PR on
  12093. <code>release-vX.Y</code>, get it merged there, and then merge from <code>release-vX.Y</code> to
  12094. <code>develop</code>. (If a fix lands in <code>develop</code> and we later need it in a
  12095. release-branch, we can of course cherry-pick it, but landing it in the release
  12096. branch first helps reduce the chance of annoying conflicts.)</p>
  12097. <hr />
  12098. <p><b id="f1">[1]</b>: “Squash and merge” is GitHub's term for this
  12099. operation. Given that there is no merge involved, I'm not convinced it's the
  12100. most intuitive name. <a href="development/git.html#a1">^</a></p>
  12101. <p><b id="f2">[2]</b>: Well, anyone with commit access.<a href="development/git.html#a2">^</a></p>
  12102. <p><b id="f3">[3]</b>: Very, very occasionally (I think this has happened once in
  12103. the history of Synapse), we've had two releases in flight at once. Obviously,
  12104. <code>release-v1.2</code> is more-stable than <code>release-v1.3</code>. <a href="development/git.html#a3">^</a></p>
  12105. <div style="break-before: page; page-break-before: always;"></div><h1 id="opentracing"><a class="header" href="#opentracing">OpenTracing</a></h1>
  12106. <h2 id="background"><a class="header" href="#background">Background</a></h2>
  12107. <p>OpenTracing is a semi-standard being adopted by a number of distributed
  12108. tracing platforms. It is a common api for facilitating vendor-agnostic
  12109. tracing instrumentation. That is, we can use the OpenTracing api and
  12110. select one of a number of tracer implementations to do the heavy lifting
  12111. in the background. Our current selected implementation is Jaeger.</p>
  12112. <p>OpenTracing is a tool which gives an insight into the causal
  12113. relationship of work done in and between servers. The servers each track
  12114. events and report them to a centralised server - in Synapse's case:
  12115. Jaeger. The basic unit used to represent events is the span. The span
  12116. roughly represents a single piece of work that was done and the time at
  12117. which it occurred. A span can have child spans, meaning that the work of
  12118. the child had to be completed for the parent span to complete, or it can
  12119. have follow-on spans which represent work that is undertaken as a result
  12120. of the parent but is not depended on by the parent to in order to
  12121. finish.</p>
  12122. <p>Since this is undertaken in a distributed environment a request to
  12123. another server, such as an RPC or a simple GET, can be considered a span
  12124. (a unit or work) for the local server. This causal link is what
  12125. OpenTracing aims to capture and visualise. In order to do this metadata
  12126. about the local server's span, i.e the 'span context', needs to be
  12127. included with the request to the remote.</p>
  12128. <p>It is up to the remote server to decide what it does with the spans it
  12129. creates. This is called the sampling policy and it can be configured
  12130. through Jaeger's settings.</p>
  12131. <p>For OpenTracing concepts see
  12132. <a href="https://opentracing.io/docs/overview/what-is-tracing/">https://opentracing.io/docs/overview/what-is-tracing/</a>.</p>
  12133. <p>For more information about Jaeger's implementation see
  12134. <a href="https://www.jaegertracing.io/docs/">https://www.jaegertracing.io/docs/</a></p>
  12135. <h2 id="setting-up-opentracing"><a class="header" href="#setting-up-opentracing">Setting up OpenTracing</a></h2>
  12136. <p>To receive OpenTracing spans, start up a Jaeger server. This can be done
  12137. using docker like so:</p>
  12138. <pre><code class="language-sh">docker run -d --name jaeger \
  12139. -p 6831:6831/udp \
  12140. -p 6832:6832/udp \
  12141. -p 5778:5778 \
  12142. -p 16686:16686 \
  12143. -p 14268:14268 \
  12144. jaegertracing/all-in-one:1
  12145. </code></pre>
  12146. <p>Latest documentation is probably at
  12147. https://www.jaegertracing.io/docs/latest/getting-started.</p>
  12148. <h2 id="enable-opentracing-in-synapse"><a class="header" href="#enable-opentracing-in-synapse">Enable OpenTracing in Synapse</a></h2>
  12149. <p>OpenTracing is not enabled by default. It must be enabled in the
  12150. homeserver config by uncommenting the config options under <code>opentracing</code>
  12151. as shown in the <a href="./sample_config.yaml">sample config</a>. For example:</p>
  12152. <pre><code class="language-yaml">opentracing:
  12153. enabled: true
  12154. homeserver_whitelist:
  12155. - &quot;mytrustedhomeserver.org&quot;
  12156. - &quot;*.myotherhomeservers.com&quot;
  12157. </code></pre>
  12158. <h2 id="homeserver-whitelisting"><a class="header" href="#homeserver-whitelisting">Homeserver whitelisting</a></h2>
  12159. <p>The homeserver whitelist is configured using regular expressions. A list
  12160. of regular expressions can be given and their union will be compared
  12161. when propagating any spans contexts to another homeserver.</p>
  12162. <p>Though it's mostly safe to send and receive span contexts to and from
  12163. untrusted users since span contexts are usually opaque ids it can lead
  12164. to two problems, namely:</p>
  12165. <ul>
  12166. <li>If the span context is marked as sampled by the sending homeserver
  12167. the receiver will sample it. Therefore two homeservers with wildly
  12168. different sampling policies could incur higher sampling counts than
  12169. intended.</li>
  12170. <li>Sending servers can attach arbitrary data to spans, known as
  12171. 'baggage'. For safety this has been disabled in Synapse but that
  12172. doesn't prevent another server sending you baggage which will be
  12173. logged to OpenTracing's logs.</li>
  12174. </ul>
  12175. <h2 id="configuring-jaeger"><a class="header" href="#configuring-jaeger">Configuring Jaeger</a></h2>
  12176. <p>Sampling strategies can be set as in this document:
  12177. <a href="https://www.jaegertracing.io/docs/latest/sampling/">https://www.jaegertracing.io/docs/latest/sampling/</a>.</p>
  12178. <div style="break-before: page; page-break-before: always;"></div><h1 id="synapse-database-schema-files"><a class="header" href="#synapse-database-schema-files">Synapse database schema files</a></h1>
  12179. <p>Synapse's database schema is stored in the <code>synapse.storage.schema</code> module.</p>
  12180. <h2 id="logical-databases"><a class="header" href="#logical-databases">Logical databases</a></h2>
  12181. <p>Synapse supports splitting its datastore across multiple physical databases (which can
  12182. be useful for large installations), and the schema files are therefore split according
  12183. to the logical database they apply to.</p>
  12184. <p>At the time of writing, the following &quot;logical&quot; databases are supported:</p>
  12185. <ul>
  12186. <li><code>state</code> - used to store Matrix room state (more specifically, <code>state_groups</code>,
  12187. their relationships and contents).</li>
  12188. <li><code>main</code> - stores everything else.</li>
  12189. </ul>
  12190. <p>Additionally, the <code>common</code> directory contains schema files for tables which must be
  12191. present on <em>all</em> physical databases.</p>
  12192. <h2 id="synapse-schema-versions"><a class="header" href="#synapse-schema-versions">Synapse schema versions</a></h2>
  12193. <p>Synapse manages its database schema via &quot;schema versions&quot;. These are mainly used to
  12194. help avoid confusion if the Synapse codebase is rolled back after the database is
  12195. updated. They work as follows:</p>
  12196. <ul>
  12197. <li>
  12198. <p>The Synapse codebase defines a constant <code>synapse.storage.schema.SCHEMA_VERSION</code>
  12199. which represents the expectations made about the database by that version. For
  12200. example, as of Synapse v1.36, this is <code>59</code>.</p>
  12201. </li>
  12202. <li>
  12203. <p>The database stores a &quot;compatibility version&quot; in
  12204. <code>schema_compat_version.compat_version</code> which defines the <code>SCHEMA_VERSION</code> of the
  12205. oldest version of Synapse which will work with the database. On startup, if
  12206. <code>compat_version</code> is found to be newer than <code>SCHEMA_VERSION</code>, Synapse will refuse to
  12207. start.</p>
  12208. <p>Synapse automatically updates this field from
  12209. <code>synapse.storage.schema.SCHEMA_COMPAT_VERSION</code>.</p>
  12210. </li>
  12211. <li>
  12212. <p>Whenever a backwards-incompatible change is made to the database format (normally
  12213. via a <code>delta</code> file), <code>synapse.storage.schema.SCHEMA_COMPAT_VERSION</code> is also updated
  12214. so that administrators can not accidentally roll back to a too-old version of Synapse.</p>
  12215. </li>
  12216. </ul>
  12217. <p>Generally, the goal is to maintain compatibility with at least one or two previous
  12218. releases of Synapse, so any substantial change tends to require multiple releases and a
  12219. bit of forward-planning to get right.</p>
  12220. <p>As a worked example: we want to remove the <code>room_stats_historical</code> table. Here is how it
  12221. might pan out.</p>
  12222. <ol>
  12223. <li>
  12224. <p>Replace any code that <em>reads</em> from <code>room_stats_historical</code> with alternative
  12225. implementations, but keep writing to it in case of rollback to an earlier version.
  12226. Also, increase <code>synapse.storage.schema.SCHEMA_VERSION</code>. In this
  12227. instance, there is no existing code which reads from <code>room_stats_historical</code>, so
  12228. our starting point is:</p>
  12229. <p>v1.36.0: <code>SCHEMA_VERSION=59</code>, <code>SCHEMA_COMPAT_VERSION=59</code></p>
  12230. </li>
  12231. <li>
  12232. <p>Next (say in Synapse v1.37.0): remove the code that <em>writes</em> to
  12233. <code>room_stats_historical</code>, but don’t yet remove the table in case of rollback to
  12234. v1.36.0. Again, we increase <code>synapse.storage.schema.SCHEMA_VERSION</code>, but
  12235. because we have not broken compatibility with v1.36, we do not yet update
  12236. <code>SCHEMA_COMPAT_VERSION</code>. We now have:</p>
  12237. <p>v1.37.0: <code>SCHEMA_VERSION=60</code>, <code>SCHEMA_COMPAT_VERSION=59</code>.</p>
  12238. </li>
  12239. <li>
  12240. <p>Later (say in Synapse v1.38.0): we can remove the table altogether. This will
  12241. break compatibility with v1.36.0, so we must update <code>SCHEMA_COMPAT_VERSION</code> accordingly.
  12242. There is no need to update <code>synapse.storage.schema.SCHEMA_VERSION</code>, since there is no
  12243. change to the Synapse codebase here. So we end up with:</p>
  12244. <p>v1.38.0: <code>SCHEMA_VERSION=60</code>, <code>SCHEMA_COMPAT_VERSION=60</code>.</p>
  12245. </li>
  12246. </ol>
  12247. <p>If in doubt about whether to update <code>SCHEMA_VERSION</code> or not, it is generally best to
  12248. lean towards doing so.</p>
  12249. <h2 id="full-schema-dumps"><a class="header" href="#full-schema-dumps">Full schema dumps</a></h2>
  12250. <p>In the <code>full_schemas</code> directories, only the most recently-numbered snapshot is used
  12251. (<code>54</code> at the time of writing). Older snapshots (eg, <code>16</code>) are present for historical
  12252. reference only.</p>
  12253. <h3 id="building-full-schema-dumps"><a class="header" href="#building-full-schema-dumps">Building full schema dumps</a></h3>
  12254. <p>If you want to recreate these schemas, they need to be made from a database that
  12255. has had all background updates run.</p>
  12256. <p>To do so, use <code>scripts-dev/make_full_schema.sh</code>. This will produce new
  12257. <code>full.sql.postgres</code> and <code>full.sql.sqlite</code> files.</p>
  12258. <p>Ensure postgres is installed, then run:</p>
  12259. <pre><code class="language-sh">./scripts-dev/make_full_schema.sh -p postgres_username -o output_dir/
  12260. </code></pre>
  12261. <p>NB at the time of writing, this script predates the split into separate <code>state</code>/<code>main</code>
  12262. databases so will require updates to handle that correctly.</p>
  12263. <h2 id="boolean-columns"><a class="header" href="#boolean-columns">Boolean columns</a></h2>
  12264. <p>Boolean columns require special treatment, since SQLite treats booleans the
  12265. same as integers.</p>
  12266. <p>There are three separate aspects to this:</p>
  12267. <ul>
  12268. <li>
  12269. <p>Any new boolean column must be added to the <code>BOOLEAN_COLUMNS</code> list in
  12270. <code>scripts/synapse_port_db</code>. This tells the port script to cast the integer
  12271. value from SQLite to a boolean before writing the value to the postgres
  12272. database.</p>
  12273. </li>
  12274. <li>
  12275. <p>Before SQLite 3.23, <code>TRUE</code> and <code>FALSE</code> were not recognised as constants by
  12276. SQLite, and the <code>IS [NOT] TRUE</code>/<code>IS [NOT] FALSE</code> operators were not
  12277. supported. This makes it necessary to avoid using <code>TRUE</code> and <code>FALSE</code>
  12278. constants in SQL commands.</p>
  12279. <p>For example, to insert a <code>TRUE</code> value into the database, write:</p>
  12280. <pre><code class="language-python">txn.execute(&quot;INSERT INTO tbl(col) VALUES (?)&quot;, (True, ))
  12281. </code></pre>
  12282. </li>
  12283. <li>
  12284. <p>Default values for new boolean columns present a particular
  12285. difficulty. Generally it is best to create separate schema files for
  12286. Postgres and SQLite. For example:</p>
  12287. <pre><code class="language-sql"># in 00delta.sql.postgres:
  12288. ALTER TABLE tbl ADD COLUMN col BOOLEAN DEFAULT FALSE;
  12289. </code></pre>
  12290. <pre><code class="language-sql"># in 00delta.sql.sqlite:
  12291. ALTER TABLE tbl ADD COLUMN col BOOLEAN DEFAULT 0;
  12292. </code></pre>
  12293. <p>Note that there is a particularly insidious failure mode here: the Postgres
  12294. flavour will be accepted by SQLite 3.22, but will give a column whose
  12295. default value is the <strong>string</strong> <code>&quot;FALSE&quot;</code> - which, when cast back to a boolean
  12296. in Python, evaluates to <code>True</code>.</p>
  12297. </li>
  12298. </ul>
  12299. <div style="break-before: page; page-break-before: always;"></div><h1 id="implementing-experimental-features-in-synapse"><a class="header" href="#implementing-experimental-features-in-synapse">Implementing experimental features in Synapse</a></h1>
  12300. <p>It can be desirable to implement &quot;experimental&quot; features which are disabled by
  12301. default and must be explicitly enabled via the Synapse configuration. This is
  12302. applicable for features which:</p>
  12303. <ul>
  12304. <li>Are unstable in the Matrix spec (e.g. those defined by an MSC that has not yet been merged).</li>
  12305. <li>Developers are not confident in their use by general Synapse administrators/users
  12306. (e.g. a feature is incomplete, buggy, performs poorly, or needs further testing).</li>
  12307. </ul>
  12308. <p>Note that this only really applies to features which are expected to be desirable
  12309. to a broad audience. The <a href="development/../modules/index.html">module infrastructure</a> should
  12310. instead be investigated for non-standard features.</p>
  12311. <p>Guarding experimental features behind configuration flags should help with some
  12312. of the following scenarios:</p>
  12313. <ul>
  12314. <li>Ensure that clients do not assume that unstable features exist (failing
  12315. gracefully if they do not).</li>
  12316. <li>Unstable features do not become de-facto standards and can be removed
  12317. aggressively (since only those who have opted-in will be affected).</li>
  12318. <li>Ease finding the implementation of unstable features in Synapse (for future
  12319. removal or stabilization).</li>
  12320. <li>Ease testing a feature (or removal of feature) due to enabling/disabling without
  12321. code changes. It also becomes possible to ask for wider testing, if desired.</li>
  12322. </ul>
  12323. <p>Experimental configuration flags should be disabled by default (requiring Synapse
  12324. administrators to explicitly opt-in), although there are situations where it makes
  12325. sense (from a product point-of-view) to enable features by default. This is
  12326. expected and not an issue.</p>
  12327. <p>It is not a requirement for experimental features to be behind a configuration flag,
  12328. but one should be used if unsure.</p>
  12329. <p>New experimental configuration flags should be added under the <code>experimental</code>
  12330. configuration key (see the <code>synapse.config.experimental</code> file) and either explain
  12331. (briefly) what is being enabled, or include the MSC number.</p>
  12332. <div style="break-before: page; page-break-before: always;"></div><h1 id="log-contexts"><a class="header" href="#log-contexts">Log Contexts</a></h1>
  12333. <p>To help track the processing of individual requests, synapse uses a
  12334. '<code>log context</code>' to track which request it is handling at any given
  12335. moment. This is done via a thread-local variable; a <code>logging.Filter</code> is
  12336. then used to fish the information back out of the thread-local variable
  12337. and add it to each log record.</p>
  12338. <p>Logcontexts are also used for CPU and database accounting, so that we
  12339. can track which requests were responsible for high CPU use or database
  12340. activity.</p>
  12341. <p>The <code>synapse.logging.context</code> module provides facilities for managing
  12342. the current log context (as well as providing the <code>LoggingContextFilter</code>
  12343. class).</p>
  12344. <p>Asynchronous functions make the whole thing complicated, so this document describes
  12345. how it all works, and how to write code which follows the rules.</p>
  12346. <p>In this document, &quot;awaitable&quot; refers to any object which can be <code>await</code>ed. In the context of
  12347. Synapse, that normally means either a coroutine or a Twisted
  12348. <a href="https://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferred.html"><code>Deferred</code></a>.</p>
  12349. <h2 id="logcontexts-without-asynchronous-code"><a class="header" href="#logcontexts-without-asynchronous-code">Logcontexts without asynchronous code</a></h2>
  12350. <p>In the absence of any asynchronous voodoo, things are simple enough. As with
  12351. any code of this nature, the rule is that our function should leave
  12352. things as it found them:</p>
  12353. <pre><code class="language-python">from synapse.logging import context # omitted from future snippets
  12354. def handle_request(request_id):
  12355. request_context = context.LoggingContext()
  12356. calling_context = context.set_current_context(request_context)
  12357. try:
  12358. request_context.request = request_id
  12359. do_request_handling()
  12360. logger.debug(&quot;finished&quot;)
  12361. finally:
  12362. context.set_current_context(calling_context)
  12363. def do_request_handling():
  12364. logger.debug(&quot;phew&quot;) # this will be logged against request_id
  12365. </code></pre>
  12366. <p>LoggingContext implements the context management methods, so the above
  12367. can be written much more succinctly as:</p>
  12368. <pre><code class="language-python">def handle_request(request_id):
  12369. with context.LoggingContext() as request_context:
  12370. request_context.request = request_id
  12371. do_request_handling()
  12372. logger.debug(&quot;finished&quot;)
  12373. def do_request_handling():
  12374. logger.debug(&quot;phew&quot;)
  12375. </code></pre>
  12376. <h2 id="using-logcontexts-with-awaitables"><a class="header" href="#using-logcontexts-with-awaitables">Using logcontexts with awaitables</a></h2>
  12377. <p>Awaitables break the linear flow of code so that there is no longer a single entry point
  12378. where we should set the logcontext and a single exit point where we should remove it.</p>
  12379. <p>Consider the example above, where <code>do_request_handling</code> needs to do some
  12380. blocking operation, and returns an awaitable:</p>
  12381. <pre><code class="language-python">async def handle_request(request_id):
  12382. with context.LoggingContext() as request_context:
  12383. request_context.request = request_id
  12384. await do_request_handling()
  12385. logger.debug(&quot;finished&quot;)
  12386. </code></pre>
  12387. <p>In the above flow:</p>
  12388. <ul>
  12389. <li>The logcontext is set</li>
  12390. <li><code>do_request_handling</code> is called, and returns an awaitable</li>
  12391. <li><code>handle_request</code> awaits the awaitable</li>
  12392. <li>Execution of <code>handle_request</code> is suspended</li>
  12393. </ul>
  12394. <p>So we have stopped processing the request (and will probably go on to
  12395. start processing the next), without clearing the logcontext.</p>
  12396. <p>To circumvent this problem, synapse code assumes that, wherever you have
  12397. an awaitable, you will want to <code>await</code> it. To that end, whereever
  12398. functions return awaitables, we adopt the following conventions:</p>
  12399. <p><strong>Rules for functions returning awaitables:</strong></p>
  12400. <blockquote>
  12401. <ul>
  12402. <li>If the awaitable is already complete, the function returns with the
  12403. same logcontext it started with.</li>
  12404. <li>If the awaitable is incomplete, the function clears the logcontext
  12405. before returning; when the awaitable completes, it restores the
  12406. logcontext before running any callbacks.</li>
  12407. </ul>
  12408. </blockquote>
  12409. <p>That sounds complicated, but actually it means a lot of code (including
  12410. the example above) &quot;just works&quot;. There are two cases:</p>
  12411. <ul>
  12412. <li>
  12413. <p>If <code>do_request_handling</code> returns a completed awaitable, then the
  12414. logcontext will still be in place. In this case, execution will
  12415. continue immediately after the <code>await</code>; the &quot;finished&quot; line will
  12416. be logged against the right context, and the <code>with</code> block restores
  12417. the original context before we return to the caller.</p>
  12418. </li>
  12419. <li>
  12420. <p>If the returned awaitable is incomplete, <code>do_request_handling</code> clears
  12421. the logcontext before returning. The logcontext is therefore clear
  12422. when <code>handle_request</code> <code>await</code>s the awaitable.</p>
  12423. <p>Once <code>do_request_handling</code>'s awaitable completes, it will reinstate
  12424. the logcontext, before running the second half of <code>handle_request</code>,
  12425. so again the &quot;finished&quot; line will be logged against the right context,
  12426. and the <code>with</code> block restores the original context.</p>
  12427. </li>
  12428. </ul>
  12429. <p>As an aside, it's worth noting that <code>handle_request</code> follows our rules</p>
  12430. <ul>
  12431. <li>though that only matters if the caller has its own logcontext which it
  12432. cares about.</li>
  12433. </ul>
  12434. <p>The following sections describe pitfalls and helpful patterns when
  12435. implementing these rules.</p>
  12436. <h2 id="always-await-your-awaitables"><a class="header" href="#always-await-your-awaitables">Always await your awaitables</a></h2>
  12437. <p>Whenever you get an awaitable back from a function, you should <code>await</code> on
  12438. it as soon as possible. Do not pass go; do not do any logging; do not
  12439. call any other functions.</p>
  12440. <pre><code class="language-python">async def fun():
  12441. logger.debug(&quot;starting&quot;)
  12442. await do_some_stuff() # just like this
  12443. coro = more_stuff()
  12444. result = await coro # also fine, of course
  12445. return result
  12446. </code></pre>
  12447. <p>Provided this pattern is followed all the way back up to the callchain
  12448. to where the logcontext was set, this will make things work out ok:
  12449. provided <code>do_some_stuff</code> and <code>more_stuff</code> follow the rules above, then
  12450. so will <code>fun</code>.</p>
  12451. <p>It's all too easy to forget to <code>await</code>: for instance if we forgot that
  12452. <code>do_some_stuff</code> returned an awaitable, we might plough on regardless. This
  12453. leads to a mess; it will probably work itself out eventually, but not
  12454. before a load of stuff has been logged against the wrong context.
  12455. (Normally, other things will break, more obviously, if you forget to
  12456. <code>await</code>, so this tends not to be a major problem in practice.)</p>
  12457. <p>Of course sometimes you need to do something a bit fancier with your
  12458. awaitable - not all code follows the linear A-then-B-then-C pattern.
  12459. Notes on implementing more complex patterns are in later sections.</p>
  12460. <h2 id="where-you-create-a-new-awaitable-make-it-follow-the-rules"><a class="header" href="#where-you-create-a-new-awaitable-make-it-follow-the-rules">Where you create a new awaitable, make it follow the rules</a></h2>
  12461. <p>Most of the time, an awaitable comes from another synapse function.
  12462. Sometimes, though, we need to make up a new awaitable, or we get an awaitable
  12463. back from external code. We need to make it follow our rules.</p>
  12464. <p>The easy way to do it is by using <code>context.make_deferred_yieldable</code>. Suppose we want to implement
  12465. <code>sleep</code>, which returns a deferred which will run its callbacks after a
  12466. given number of seconds. That might look like:</p>
  12467. <pre><code class="language-python"># not a logcontext-rules-compliant function
  12468. def get_sleep_deferred(seconds):
  12469. d = defer.Deferred()
  12470. reactor.callLater(seconds, d.callback, None)
  12471. return d
  12472. </code></pre>
  12473. <p>That doesn't follow the rules, but we can fix it by calling it through
  12474. <code>context.make_deferred_yieldable</code>:</p>
  12475. <pre><code class="language-python">async def sleep(seconds):
  12476. return await context.make_deferred_yieldable(get_sleep_deferred(seconds))
  12477. </code></pre>
  12478. <h2 id="fire-and-forget"><a class="header" href="#fire-and-forget">Fire-and-forget</a></h2>
  12479. <p>Sometimes you want to fire off a chain of execution, but not wait for
  12480. its result. That might look a bit like this:</p>
  12481. <pre><code class="language-python">async def do_request_handling():
  12482. await foreground_operation()
  12483. # *don't* do this
  12484. background_operation()
  12485. logger.debug(&quot;Request handling complete&quot;)
  12486. async def background_operation():
  12487. await first_background_step()
  12488. logger.debug(&quot;Completed first step&quot;)
  12489. await second_background_step()
  12490. logger.debug(&quot;Completed second step&quot;)
  12491. </code></pre>
  12492. <p>The above code does a couple of steps in the background after
  12493. <code>do_request_handling</code> has finished. The log lines are still logged
  12494. against the <code>request_context</code> logcontext, which may or may not be
  12495. desirable. There are two big problems with the above, however. The first
  12496. problem is that, if <code>background_operation</code> returns an incomplete
  12497. awaitable, it will expect its caller to <code>await</code> immediately, so will have
  12498. cleared the logcontext. In this example, that means that 'Request
  12499. handling complete' will be logged without any context.</p>
  12500. <p>The second problem, which is potentially even worse, is that when the
  12501. awaitable returned by <code>background_operation</code> completes, it will restore
  12502. the original logcontext. There is nothing waiting on that awaitable, so
  12503. the logcontext will leak into the reactor and possibly get attached to
  12504. some arbitrary future operation.</p>
  12505. <p>There are two potential solutions to this.</p>
  12506. <p>One option is to surround the call to <code>background_operation</code> with a
  12507. <code>PreserveLoggingContext</code> call. That will reset the logcontext before
  12508. starting <code>background_operation</code> (so the context restored when the
  12509. deferred completes will be the empty logcontext), and will restore the
  12510. current logcontext before continuing the foreground process:</p>
  12511. <pre><code class="language-python">async def do_request_handling():
  12512. await foreground_operation()
  12513. # start background_operation off in the empty logcontext, to
  12514. # avoid leaking the current context into the reactor.
  12515. with PreserveLoggingContext():
  12516. background_operation()
  12517. # this will now be logged against the request context
  12518. logger.debug(&quot;Request handling complete&quot;)
  12519. </code></pre>
  12520. <p>Obviously that option means that the operations done in
  12521. <code>background_operation</code> would be not be logged against a logcontext
  12522. (though that might be fixed by setting a different logcontext via a
  12523. <code>with LoggingContext(...)</code> in <code>background_operation</code>).</p>
  12524. <p>The second option is to use <code>context.run_in_background</code>, which wraps a
  12525. function so that it doesn't reset the logcontext even when it returns
  12526. an incomplete awaitable, and adds a callback to the returned awaitable to
  12527. reset the logcontext. In other words, it turns a function that follows
  12528. the Synapse rules about logcontexts and awaitables into one which behaves
  12529. more like an external function --- the opposite operation to that
  12530. described in the previous section. It can be used like this:</p>
  12531. <pre><code class="language-python">async def do_request_handling():
  12532. await foreground_operation()
  12533. context.run_in_background(background_operation)
  12534. # this will now be logged against the request context
  12535. logger.debug(&quot;Request handling complete&quot;)
  12536. </code></pre>
  12537. <h2 id="passing-synapse-deferreds-into-third-party-functions"><a class="header" href="#passing-synapse-deferreds-into-third-party-functions">Passing synapse deferreds into third-party functions</a></h2>
  12538. <p>A typical example of this is where we want to collect together two or
  12539. more awaitables via <code>defer.gatherResults</code>:</p>
  12540. <pre><code class="language-python">a1 = operation1()
  12541. a2 = operation2()
  12542. a3 = defer.gatherResults([a1, a2])
  12543. </code></pre>
  12544. <p>This is really a variation of the fire-and-forget problem above, in that
  12545. we are firing off <code>a1</code> and <code>a2</code> without awaiting on them. The difference
  12546. is that we now have third-party code attached to their callbacks. Anyway
  12547. either technique given in the <a href="log_contexts.html#fire-and-forget">Fire-and-forget</a>
  12548. section will work.</p>
  12549. <p>Of course, the new awaitable returned by <code>gather</code> needs to be
  12550. wrapped in order to make it follow the logcontext rules before we can
  12551. yield it, as described in <a href="log_contexts.html#where-you-create-a-new-awaitable-make-it-follow-the-rules">Where you create a new awaitable, make it
  12552. follow the
  12553. rules</a>.</p>
  12554. <p>So, option one: reset the logcontext before starting the operations to
  12555. be gathered:</p>
  12556. <pre><code class="language-python">async def do_request_handling():
  12557. with PreserveLoggingContext():
  12558. a1 = operation1()
  12559. a2 = operation2()
  12560. result = await defer.gatherResults([a1, a2])
  12561. </code></pre>
  12562. <p>In this case particularly, though, option two, of using
  12563. <code>context.run_in_background</code> almost certainly makes more sense, so that
  12564. <code>operation1</code> and <code>operation2</code> are both logged against the original
  12565. logcontext. This looks like:</p>
  12566. <pre><code class="language-python">async def do_request_handling():
  12567. a1 = context.run_in_background(operation1)
  12568. a2 = context.run_in_background(operation2)
  12569. result = await make_deferred_yieldable(defer.gatherResults([a1, a2]))
  12570. </code></pre>
  12571. <h2 id="a-note-on-garbage-collection-of-awaitable-chains"><a class="header" href="#a-note-on-garbage-collection-of-awaitable-chains">A note on garbage-collection of awaitable chains</a></h2>
  12572. <p>It turns out that our logcontext rules do not play nicely with awaitable
  12573. chains which get orphaned and garbage-collected.</p>
  12574. <p>Imagine we have some code that looks like this:</p>
  12575. <pre><code class="language-python">listener_queue = []
  12576. def on_something_interesting():
  12577. for d in listener_queue:
  12578. d.callback(&quot;foo&quot;)
  12579. async def await_something_interesting():
  12580. new_awaitable = defer.Deferred()
  12581. listener_queue.append(new_awaitable)
  12582. with PreserveLoggingContext():
  12583. await new_awaitable
  12584. </code></pre>
  12585. <p>Obviously, the idea here is that we have a bunch of things which are
  12586. waiting for an event. (It's just an example of the problem here, but a
  12587. relatively common one.)</p>
  12588. <p>Now let's imagine two further things happen. First of all, whatever was
  12589. waiting for the interesting thing goes away. (Perhaps the request times
  12590. out, or something <em>even more</em> interesting happens.)</p>
  12591. <p>Secondly, let's suppose that we decide that the interesting thing is
  12592. never going to happen, and we reset the listener queue:</p>
  12593. <pre><code class="language-python">def reset_listener_queue():
  12594. listener_queue.clear()
  12595. </code></pre>
  12596. <p>So, both ends of the awaitable chain have now dropped their references,
  12597. and the awaitable chain is now orphaned, and will be garbage-collected at
  12598. some point. Note that <code>await_something_interesting</code> is a coroutine,
  12599. which Python implements as a generator function. When Python
  12600. garbage-collects generator functions, it gives them a chance to
  12601. clean up by making the <code>await</code> (or <code>yield</code>) raise a <code>GeneratorExit</code>
  12602. exception. In our case, that means that the <code>__exit__</code> handler of
  12603. <code>PreserveLoggingContext</code> will carefully restore the request context, but
  12604. there is now nothing waiting for its return, so the request context is
  12605. never cleared.</p>
  12606. <p>To reiterate, this problem only arises when <em>both</em> ends of a awaitable
  12607. chain are dropped. Dropping the the reference to an awaitable you're
  12608. supposed to be awaiting is bad practice, so this doesn't
  12609. actually happen too much. Unfortunately, when it does happen, it will
  12610. lead to leaked logcontexts which are incredibly hard to track down.</p>
  12611. <div style="break-before: page; page-break-before: always;"></div><h1 id="replication-architecture"><a class="header" href="#replication-architecture">Replication Architecture</a></h1>
  12612. <h2 id="motivation"><a class="header" href="#motivation">Motivation</a></h2>
  12613. <p>We'd like to be able to split some of the work that synapse does into
  12614. multiple python processes. In theory multiple synapse processes could
  12615. share a single postgresql database and we'd scale up by running more
  12616. synapse processes. However much of synapse assumes that only one process
  12617. is interacting with the database, both for assigning unique identifiers
  12618. when inserting into tables, notifying components about new updates, and
  12619. for invalidating its caches.</p>
  12620. <p>So running multiple copies of the current code isn't an option. One way
  12621. to run multiple processes would be to have a single writer process and
  12622. multiple reader processes connected to the same database. In order to do
  12623. this we'd need a way for the reader process to invalidate its in-memory
  12624. caches when an update happens on the writer. One way to do this is for
  12625. the writer to present an append-only log of updates which the readers
  12626. can consume to invalidate their caches and to push updates to listening
  12627. clients or pushers.</p>
  12628. <p>Synapse already stores much of its data as an append-only log so that it
  12629. can correctly respond to <code>/sync</code> requests so the amount of code changes
  12630. needed to expose the append-only log to the readers should be fairly
  12631. minimal.</p>
  12632. <h2 id="architecture"><a class="header" href="#architecture">Architecture</a></h2>
  12633. <h3 id="the-replication-protocol"><a class="header" href="#the-replication-protocol">The Replication Protocol</a></h3>
  12634. <p>See <a href="tcp_replication.html">the TCP replication documentation</a>.</p>
  12635. <h3 id="the-slaved-datastore"><a class="header" href="#the-slaved-datastore">The Slaved DataStore</a></h3>
  12636. <p>There are read-only version of the synapse storage layer in
  12637. <code>synapse/replication/slave/storage</code> that use the response of the
  12638. replication API to invalidate their caches.</p>
  12639. <div style="break-before: page; page-break-before: always;"></div><h1 id="tcp-replication"><a class="header" href="#tcp-replication">TCP Replication</a></h1>
  12640. <h2 id="motivation-1"><a class="header" href="#motivation-1">Motivation</a></h2>
  12641. <p>Previously the workers used an HTTP long poll mechanism to get updates
  12642. from the master, which had the problem of causing a lot of duplicate
  12643. work on the server. This TCP protocol replaces those APIs with the aim
  12644. of increased efficiency.</p>
  12645. <h2 id="overview-3"><a class="header" href="#overview-3">Overview</a></h2>
  12646. <p>The protocol is based on fire and forget, line based commands. An
  12647. example flow would be (where '&gt;' indicates master to worker and
  12648. '&lt;' worker to master flows):</p>
  12649. <pre><code>&gt; SERVER example.com
  12650. &lt; REPLICATE
  12651. &gt; POSITION events master 53 53
  12652. &gt; RDATA events master 54 [&quot;$foo1:bar.com&quot;, ...]
  12653. &gt; RDATA events master 55 [&quot;$foo4:bar.com&quot;, ...]
  12654. </code></pre>
  12655. <p>The example shows the server accepting a new connection and sending its identity
  12656. with the <code>SERVER</code> command, followed by the client server to respond with the
  12657. position of all streams. The server then periodically sends <code>RDATA</code> commands
  12658. which have the format <code>RDATA &lt;stream_name&gt; &lt;instance_name&gt; &lt;token&gt; &lt;row&gt;</code>, where
  12659. the format of <code>&lt;row&gt;</code> is defined by the individual streams. The
  12660. <code>&lt;instance_name&gt;</code> is the name of the Synapse process that generated the data
  12661. (usually &quot;master&quot;).</p>
  12662. <p>Error reporting happens by either the client or server sending an ERROR
  12663. command, and usually the connection will be closed.</p>
  12664. <p>Since the protocol is a simple line based, its possible to manually
  12665. connect to the server using a tool like netcat. A few things should be
  12666. noted when manually using the protocol:</p>
  12667. <ul>
  12668. <li>The federation stream is only available if federation sending has
  12669. been disabled on the main process.</li>
  12670. <li>The server will only time connections out that have sent a <code>PING</code>
  12671. command. If a ping is sent then the connection will be closed if no
  12672. further commands are receieved within 15s. Both the client and
  12673. server protocol implementations will send an initial PING on
  12674. connection and ensure at least one command every 5s is sent (not
  12675. necessarily <code>PING</code>).</li>
  12676. <li><code>RDATA</code> commands <em>usually</em> include a numeric token, however if the
  12677. stream has multiple rows to replicate per token the server will send
  12678. multiple <code>RDATA</code> commands, with all but the last having a token of
  12679. <code>batch</code>. See the documentation on <code>commands.RdataCommand</code> for
  12680. further details.</li>
  12681. </ul>
  12682. <h2 id="architecture-1"><a class="header" href="#architecture-1">Architecture</a></h2>
  12683. <p>The basic structure of the protocol is line based, where the initial
  12684. word of each line specifies the command. The rest of the line is parsed
  12685. based on the command. For example, the RDATA command is defined as:</p>
  12686. <pre><code>RDATA &lt;stream_name&gt; &lt;instance_name&gt; &lt;token&gt; &lt;row_json&gt;
  12687. </code></pre>
  12688. <p>(Note that &lt;row_json&gt; may contains spaces, but cannot contain
  12689. newlines.)</p>
  12690. <p>Blank lines are ignored.</p>
  12691. <h3 id="keep-alives"><a class="header" href="#keep-alives">Keep alives</a></h3>
  12692. <p>Both sides are expected to send at least one command every 5s or so, and
  12693. should send a <code>PING</code> command if necessary. If either side do not receive
  12694. a command within e.g. 15s then the connection should be closed.</p>
  12695. <p>Because the server may be connected to manually using e.g. netcat, the
  12696. timeouts aren't enabled until an initial <code>PING</code> command is seen. Both
  12697. the client and server implementations below send a <code>PING</code> command
  12698. immediately on connection to ensure the timeouts are enabled.</p>
  12699. <p>This ensures that both sides can quickly realize if the tcp connection
  12700. has gone and handle the situation appropriately.</p>
  12701. <h3 id="start-up"><a class="header" href="#start-up">Start up</a></h3>
  12702. <p>When a new connection is made, the server:</p>
  12703. <ul>
  12704. <li>Sends a <code>SERVER</code> command, which includes the identity of the server,
  12705. allowing the client to detect if its connected to the expected
  12706. server</li>
  12707. <li>Sends a <code>PING</code> command as above, to enable the client to time out
  12708. connections promptly.</li>
  12709. </ul>
  12710. <p>The client:</p>
  12711. <ul>
  12712. <li>Sends a <code>NAME</code> command, allowing the server to associate a human
  12713. friendly name with the connection. This is optional.</li>
  12714. <li>Sends a <code>PING</code> as above</li>
  12715. <li>Sends a <code>REPLICATE</code> to get the current position of all streams.</li>
  12716. <li>On receipt of a <code>SERVER</code> command, checks that the server name
  12717. matches the expected server name.</li>
  12718. </ul>
  12719. <h3 id="error-handling"><a class="header" href="#error-handling">Error handling</a></h3>
  12720. <p>If either side detects an error it can send an <code>ERROR</code> command and close
  12721. the connection.</p>
  12722. <p>If the client side loses the connection to the server it should
  12723. reconnect, following the steps above.</p>
  12724. <h3 id="congestion"><a class="header" href="#congestion">Congestion</a></h3>
  12725. <p>If the server sends messages faster than the client can consume them the
  12726. server will first buffer a (fairly large) number of commands and then
  12727. disconnect the client. This ensures that we don't queue up an unbounded
  12728. number of commands in memory and gives us a potential oppurtunity to
  12729. squawk loudly. When/if the client recovers it can reconnect to the
  12730. server and ask for missed messages.</p>
  12731. <h3 id="reliability"><a class="header" href="#reliability">Reliability</a></h3>
  12732. <p>In general the replication stream should be considered an unreliable
  12733. transport since e.g. commands are not resent if the connection
  12734. disappears.</p>
  12735. <p>The exception to that are the replication streams, i.e. RDATA commands,
  12736. since these include tokens which can be used to restart the stream on
  12737. connection errors.</p>
  12738. <p>The client should keep track of the token in the last RDATA command
  12739. received for each stream so that on reconneciton it can start streaming
  12740. from the correct place. Note: not all RDATA have valid tokens due to
  12741. batching. See <code>RdataCommand</code> for more details.</p>
  12742. <h3 id="example-4"><a class="header" href="#example-4">Example</a></h3>
  12743. <p>An example iteraction is shown below. Each line is prefixed with '&gt;'
  12744. or '&lt;' to indicate which side is sending, these are <em>not</em> included on
  12745. the wire:</p>
  12746. <pre><code>* connection established *
  12747. &gt; SERVER localhost:8823
  12748. &gt; PING 1490197665618
  12749. &lt; NAME synapse.app.appservice
  12750. &lt; PING 1490197665618
  12751. &lt; REPLICATE
  12752. &gt; POSITION events master 1 1
  12753. &gt; POSITION backfill master 1 1
  12754. &gt; POSITION caches master 1 1
  12755. &gt; RDATA caches master 2 [&quot;get_user_by_id&quot;,[&quot;@01register-user:localhost:8823&quot;],1490197670513]
  12756. &gt; RDATA events master 14 [&quot;$149019767112vOHxz:localhost:8823&quot;,
  12757. &quot;!AFDCvgApUmpdfVjIXm:localhost:8823&quot;,&quot;m.room.guest_access&quot;,&quot;&quot;,null]
  12758. &lt; PING 1490197675618
  12759. &gt; ERROR server stopping
  12760. * connection closed by server *
  12761. </code></pre>
  12762. <p>The <code>POSITION</code> command sent by the server is used to set the clients
  12763. position without needing to send data with the <code>RDATA</code> command.</p>
  12764. <p>An example of a batched set of <code>RDATA</code> is:</p>
  12765. <pre><code>&gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test:localhost:8823&quot;],1490197670513]
  12766. &gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test2:localhost:8823&quot;],1490197670513]
  12767. &gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test3:localhost:8823&quot;],1490197670513]
  12768. &gt; RDATA caches master 54 [&quot;get_user_by_id&quot;,[&quot;@test4:localhost:8823&quot;],1490197670513]
  12769. </code></pre>
  12770. <p>In this case the client shouldn't advance their caches token until it
  12771. sees the the last <code>RDATA</code>.</p>
  12772. <h3 id="list-of-commands"><a class="header" href="#list-of-commands">List of commands</a></h3>
  12773. <p>The list of valid commands, with which side can send it: server (S) or
  12774. client (C):</p>
  12775. <h4 id="server-s"><a class="header" href="#server-s">SERVER (S)</a></h4>
  12776. <p>Sent at the start to identify which server the client is talking to</p>
  12777. <h4 id="rdata-s"><a class="header" href="#rdata-s">RDATA (S)</a></h4>
  12778. <p>A single update in a stream</p>
  12779. <h4 id="position-s"><a class="header" href="#position-s">POSITION (S)</a></h4>
  12780. <p>On receipt of a POSITION command clients should check if they have missed any
  12781. updates, and if so then fetch them out of band. Sent in response to a
  12782. REPLICATE command (but can happen at any time).</p>
  12783. <p>The POSITION command includes the source of the stream. Currently all streams
  12784. are written by a single process (usually &quot;master&quot;). If fetching missing
  12785. updates via HTTP API, rather than via the DB, then processes should make the
  12786. request to the appropriate process.</p>
  12787. <p>Two positions are included, the &quot;new&quot; position and the last position sent respectively.
  12788. This allows servers to tell instances that the positions have advanced but no
  12789. data has been written, without clients needlessly checking to see if they
  12790. have missed any updates.</p>
  12791. <h4 id="error-s-c"><a class="header" href="#error-s-c">ERROR (S, C)</a></h4>
  12792. <p>There was an error</p>
  12793. <h4 id="ping-s-c"><a class="header" href="#ping-s-c">PING (S, C)</a></h4>
  12794. <p>Sent periodically to ensure the connection is still alive</p>
  12795. <h4 id="name-c"><a class="header" href="#name-c">NAME (C)</a></h4>
  12796. <p>Sent at the start by client to inform the server who they are</p>
  12797. <h4 id="replicate-c"><a class="header" href="#replicate-c">REPLICATE (C)</a></h4>
  12798. <p>Asks the server for the current position of all streams.</p>
  12799. <h4 id="user_sync-c"><a class="header" href="#user_sync-c">USER_SYNC (C)</a></h4>
  12800. <p>A user has started or stopped syncing on this process.</p>
  12801. <h4 id="clear_user_sync-c"><a class="header" href="#clear_user_sync-c">CLEAR_USER_SYNC (C)</a></h4>
  12802. <p>The server should clear all associated user sync data from the worker.</p>
  12803. <p>This is used when a worker is shutting down.</p>
  12804. <h4 id="federation_ack-c"><a class="header" href="#federation_ack-c">FEDERATION_ACK (C)</a></h4>
  12805. <p>Acknowledge receipt of some federation data</p>
  12806. <h3 id="remote_server_up-s-c"><a class="header" href="#remote_server_up-s-c">REMOTE_SERVER_UP (S, C)</a></h3>
  12807. <p>Inform other processes that a remote server may have come back online.</p>
  12808. <p>See <code>synapse/replication/tcp/commands.py</code> for a detailed description and
  12809. the format of each command.</p>
  12810. <h3 id="cache-invalidation-stream"><a class="header" href="#cache-invalidation-stream">Cache Invalidation Stream</a></h3>
  12811. <p>The cache invalidation stream is used to inform workers when they need
  12812. to invalidate any of their caches in the data store. This is done by
  12813. streaming all cache invalidations done on master down to the workers,
  12814. assuming that any caches on the workers also exist on the master.</p>
  12815. <p>Each individual cache invalidation results in a row being sent down
  12816. replication, which includes the cache name (the name of the function)
  12817. and they key to invalidate. For example:</p>
  12818. <pre><code>&gt; RDATA caches master 550953771 [&quot;get_user_by_id&quot;, [&quot;@bob:example.com&quot;], 1550574873251]
  12819. </code></pre>
  12820. <p>Alternatively, an entire cache can be invalidated by sending down a <code>null</code>
  12821. instead of the key. For example:</p>
  12822. <pre><code>&gt; RDATA caches master 550953772 [&quot;get_user_by_id&quot;, null, 1550574873252]
  12823. </code></pre>
  12824. <p>However, there are times when a number of caches need to be invalidated
  12825. at the same time with the same key. To reduce traffic we batch those
  12826. invalidations into a single poke by defining a special cache name that
  12827. workers understand to mean to expand to invalidate the correct caches.</p>
  12828. <p>Currently the special cache names are declared in
  12829. <code>synapse/storage/_base.py</code> and are:</p>
  12830. <ol>
  12831. <li><code>cs_cache_fake</code> ─ invalidates caches that depend on the current
  12832. state</li>
  12833. </ol>
  12834. <div style="break-before: page; page-break-before: always;"></div><h1 id="internal-documentation"><a class="header" href="#internal-documentation">Internal Documentation</a></h1>
  12835. <p>This section covers implementation documentation for various parts of Synapse.</p>
  12836. <p>If a developer is planning to make a change to a feature of Synapse, it can be useful for
  12837. general documentation of how that feature is implemented to be available. This saves the
  12838. developer time in place of needing to understand how the feature works by reading the
  12839. code.</p>
  12840. <p>Documentation that would be more useful for the perspective of a system administrator,
  12841. rather than a developer who's intending to change to code, should instead be placed
  12842. under the Usage section of the documentation.</p>
  12843. <div style="break-before: page; page-break-before: always;"></div><h1 id="how-to-test-saml-as-a-developer-without-a-server"><a class="header" href="#how-to-test-saml-as-a-developer-without-a-server">How to test SAML as a developer without a server</a></h1>
  12844. <p>https://fujifish.github.io/samling/samling.html (https://github.com/fujifish/samling) is a great resource for being able to tinker with the
  12845. SAML options within Synapse without needing to deploy and configure a complicated software stack.</p>
  12846. <p>To make Synapse (and therefore Element) use it:</p>
  12847. <ol>
  12848. <li>Use the samling.html URL above or deploy your own and visit the IdP Metadata tab.</li>
  12849. <li>Copy the XML to your clipboard.</li>
  12850. <li>On your Synapse server, create a new file <code>samling.xml</code> next to your <code>homeserver.yaml</code> with
  12851. the XML from step 2 as the contents.</li>
  12852. <li>Edit your <code>homeserver.yaml</code> to include:
  12853. <pre><code class="language-yaml">saml2_config:
  12854. sp_config:
  12855. allow_unknown_attributes: true # Works around a bug with AVA Hashes: https://github.com/IdentityPython/pysaml2/issues/388
  12856. metadata:
  12857. local: [&quot;samling.xml&quot;]
  12858. </code></pre>
  12859. </li>
  12860. <li>Ensure that your <code>homeserver.yaml</code> has a setting for <code>public_baseurl</code>:
  12861. <pre><code class="language-yaml">public_baseurl: http://localhost:8080/
  12862. </code></pre>
  12863. </li>
  12864. <li>Run <code>apt-get install xmlsec1</code> and <code>pip install --upgrade --force 'pysaml2&gt;=4.5.0'</code> to ensure
  12865. the dependencies are installed and ready to go.</li>
  12866. <li>Restart Synapse.</li>
  12867. </ol>
  12868. <p>Then in Element:</p>
  12869. <ol>
  12870. <li>Visit the login page and point Element towards your homeserver using the <code>public_baseurl</code> above.</li>
  12871. <li>Click the Single Sign-On button.</li>
  12872. <li>On the samling page, enter a Name Identifier and add a SAML Attribute for <code>uid=your_localpart</code>.
  12873. The response must also be signed.</li>
  12874. <li>Click &quot;Next&quot;.</li>
  12875. <li>Click &quot;Post Response&quot; (change nothing).</li>
  12876. <li>You should be logged in.</li>
  12877. </ol>
  12878. <p>If you try and repeat this process, you may be automatically logged in using the information you
  12879. gave previously. To fix this, open your developer console (<code>F12</code> or <code>Ctrl+Shift+I</code>) while on the
  12880. samling page and clear the site data. In Chrome, this will be a button on the Application tab.</p>
  12881. <div style="break-before: page; page-break-before: always;"></div><h1 id="how-to-test-cas-as-a-developer-without-a-server"><a class="header" href="#how-to-test-cas-as-a-developer-without-a-server">How to test CAS as a developer without a server</a></h1>
  12882. <p>The <a href="https://github.com/jbittel/django-mama-cas">django-mama-cas</a> project is an
  12883. easy to run CAS implementation built on top of Django.</p>
  12884. <h2 id="prerequisites"><a class="header" href="#prerequisites">Prerequisites</a></h2>
  12885. <ol>
  12886. <li>Create a new virtualenv: <code>python3 -m venv &lt;your virtualenv&gt;</code></li>
  12887. <li>Activate your virtualenv: <code>source /path/to/your/virtualenv/bin/activate</code></li>
  12888. <li>Install Django and django-mama-cas:
  12889. <pre><code class="language-sh">python -m pip install &quot;django&lt;3&quot; &quot;django-mama-cas==2.4.0&quot;
  12890. </code></pre>
  12891. </li>
  12892. <li>Create a Django project in the current directory:
  12893. <pre><code class="language-sh">django-admin startproject cas_test .
  12894. </code></pre>
  12895. </li>
  12896. <li>Follow the <a href="https://django-mama-cas.readthedocs.io/en/latest/installation.html#configuring">install directions</a> for django-mama-cas</li>
  12897. <li>Setup the SQLite database: <code>python manage.py migrate</code></li>
  12898. <li>Create a user:
  12899. <pre><code class="language-sh">python manage.py createsuperuser
  12900. </code></pre>
  12901. <ol>
  12902. <li>Use whatever you want as the username and password.</li>
  12903. <li>Leave the other fields blank.</li>
  12904. </ol>
  12905. </li>
  12906. <li>Use the built-in Django test server to serve the CAS endpoints on port 8000:
  12907. <pre><code class="language-sh">python manage.py runserver
  12908. </code></pre>
  12909. </li>
  12910. </ol>
  12911. <p>You should now have a Django project configured to serve CAS authentication with
  12912. a single user created.</p>
  12913. <h2 id="configure-synapse-and-element-to-use-cas"><a class="header" href="#configure-synapse-and-element-to-use-cas">Configure Synapse (and Element) to use CAS</a></h2>
  12914. <ol>
  12915. <li>Modify your <code>homeserver.yaml</code> to enable CAS and point it to your locally
  12916. running Django test server:
  12917. <pre><code class="language-yaml">cas_config:
  12918. enabled: true
  12919. server_url: &quot;http://localhost:8000&quot;
  12920. service_url: &quot;http://localhost:8081&quot;
  12921. #displayname_attribute: name
  12922. #required_attributes:
  12923. # name: value
  12924. </code></pre>
  12925. </li>
  12926. <li>Restart Synapse.</li>
  12927. </ol>
  12928. <p>Note that the above configuration assumes the homeserver is running on port 8081
  12929. and that the CAS server is on port 8000, both on localhost.</p>
  12930. <h2 id="testing-the-configuration"><a class="header" href="#testing-the-configuration">Testing the configuration</a></h2>
  12931. <p>Then in Element:</p>
  12932. <ol>
  12933. <li>Visit the login page with a Element pointing at your homeserver.</li>
  12934. <li>Click the Single Sign-On button.</li>
  12935. <li>Login using the credentials created with <code>createsuperuser</code>.</li>
  12936. <li>You should be logged in.</li>
  12937. </ol>
  12938. <p>If you want to repeat this process you'll need to manually logout first:</p>
  12939. <ol>
  12940. <li>http://localhost:8000/admin/</li>
  12941. <li>Click &quot;logout&quot; in the top right.</li>
  12942. </ol>
  12943. <div style="break-before: page; page-break-before: always;"></div><h1 id="room-dag-concepts"><a class="header" href="#room-dag-concepts">Room DAG concepts</a></h1>
  12944. <h2 id="edges"><a class="header" href="#edges">Edges</a></h2>
  12945. <p>The word &quot;edge&quot; comes from graph theory lingo. An edge is just a connection
  12946. between two events. In Synapse, we connect events by specifying their
  12947. <code>prev_events</code>. A subsequent event points back at a previous event.</p>
  12948. <pre><code>A (oldest) &lt;---- B &lt;---- C (most recent)
  12949. </code></pre>
  12950. <h2 id="depth-and-stream-ordering"><a class="header" href="#depth-and-stream-ordering">Depth and stream ordering</a></h2>
  12951. <p>Events are normally sorted by <code>(topological_ordering, stream_ordering)</code> where
  12952. <code>topological_ordering</code> is just <code>depth</code>. In other words, we first sort by <code>depth</code>
  12953. and then tie-break based on <code>stream_ordering</code>. <code>depth</code> is incremented as new
  12954. messages are added to the DAG. Normally, <code>stream_ordering</code> is an auto
  12955. incrementing integer, but backfilled events start with <code>stream_ordering=-1</code> and decrement.</p>
  12956. <hr />
  12957. <ul>
  12958. <li><code>/sync</code> returns things in the order they arrive at the server (<code>stream_ordering</code>).</li>
  12959. <li><code>/messages</code> (and <code>/backfill</code> in the federation API) return them in the order determined by the event graph <code>(topological_ordering, stream_ordering)</code>.</li>
  12960. </ul>
  12961. <p>The general idea is that, if you're following a room in real-time (i.e.
  12962. <code>/sync</code>), you probably want to see the messages as they arrive at your server,
  12963. rather than skipping any that arrived late; whereas if you're looking at a
  12964. historical section of timeline (i.e. <code>/messages</code>), you want to see the best
  12965. representation of the state of the room as others were seeing it at the time.</p>
  12966. <h2 id="forward-extremity"><a class="header" href="#forward-extremity">Forward extremity</a></h2>
  12967. <p>Most-recent-in-time events in the DAG which are not referenced by any other events' <code>prev_events</code> yet.</p>
  12968. <p>The forward extremities of a room are used as the <code>prev_events</code> when the next event is sent.</p>
  12969. <h2 id="backwards-extremity"><a class="header" href="#backwards-extremity">Backwards extremity</a></h2>
  12970. <p>The current marker of where we have backfilled up to and will generally be the
  12971. oldest-in-time events we know of in the DAG.</p>
  12972. <p>This is an event where we haven't fetched all of the <code>prev_events</code> for.</p>
  12973. <p>Once we have fetched all of its <code>prev_events</code>, it's unmarked as a backwards
  12974. extremity (although we may have formed new backwards extremities from the prev
  12975. events during the backfilling process).</p>
  12976. <h2 id="outliers"><a class="header" href="#outliers">Outliers</a></h2>
  12977. <p>We mark an event as an <code>outlier</code> when we haven't figured out the state for the
  12978. room at that point in the DAG yet.</p>
  12979. <p>We won't <em>necessarily</em> have the <code>prev_events</code> of an <code>outlier</code> in the database,
  12980. but it's entirely possible that we <em>might</em>. The status of whether we have all of
  12981. the <code>prev_events</code> is marked as a <a href="development/room-dag-concepts.html#backwards-extremity">backwards extremity</a>.</p>
  12982. <p>For example, when we fetch the event auth chain or state for a given event, we
  12983. mark all of those claimed auth events as outliers because we haven't done the
  12984. state calculation ourself.</p>
  12985. <h2 id="state-groups"><a class="header" href="#state-groups">State groups</a></h2>
  12986. <p>For every non-outlier event we need to know the state at that event. Instead of
  12987. storing the full state for each event in the DB (i.e. a <code>event_id -&gt; state</code>
  12988. mapping), which is <em>very</em> space inefficient when state doesn't change, we
  12989. instead assign each different set of state a &quot;state group&quot; and then have
  12990. mappings of <code>event_id -&gt; state_group</code> and <code>state_group -&gt; state</code>.</p>
  12991. <h3 id="stage-group-edges"><a class="header" href="#stage-group-edges">Stage group edges</a></h3>
  12992. <p>TODO: <code>state_group_edges</code> is a further optimization...
  12993. notes from @Azrenbeth, https://pastebin.com/seUGVGeT</p>
  12994. <div style="break-before: page; page-break-before: always;"></div><h1 id="auth-chain-difference-algorithm"><a class="header" href="#auth-chain-difference-algorithm">Auth Chain Difference Algorithm</a></h1>
  12995. <p>The auth chain difference algorithm is used by V2 state resolution, where a
  12996. naive implementation can be a significant source of CPU and DB usage.</p>
  12997. <h3 id="definitions"><a class="header" href="#definitions">Definitions</a></h3>
  12998. <p>A <em>state set</em> is a set of state events; e.g. the input of a state resolution
  12999. algorithm is a collection of state sets.</p>
  13000. <p>The <em>auth chain</em> of a set of events are all the events' auth events and <em>their</em>
  13001. auth events, recursively (i.e. the events reachable by walking the graph induced
  13002. by an event's auth events links).</p>
  13003. <p>The <em>auth chain difference</em> of a collection of state sets is the union minus the
  13004. intersection of the sets of auth chains corresponding to the state sets, i.e an
  13005. event is in the auth chain difference if it is reachable by walking the auth
  13006. event graph from at least one of the state sets but not from <em>all</em> of the state
  13007. sets.</p>
  13008. <h2 id="breadth-first-walk-algorithm"><a class="header" href="#breadth-first-walk-algorithm">Breadth First Walk Algorithm</a></h2>
  13009. <p>A way of calculating the auth chain difference without calculating the full auth
  13010. chains for each state set is to do a parallel breadth first walk (ordered by
  13011. depth) of each state set's auth chain. By tracking which events are reachable
  13012. from each state set we can finish early if every pending event is reachable from
  13013. every state set.</p>
  13014. <p>This can work well for state sets that have a small auth chain difference, but
  13015. can be very inefficient for larger differences. However, this algorithm is still
  13016. used if we don't have a chain cover index for the room (e.g. because we're in
  13017. the process of indexing it).</p>
  13018. <h2 id="chain-cover-index"><a class="header" href="#chain-cover-index">Chain Cover Index</a></h2>
  13019. <p>Synapse computes auth chain differences by pre-computing a &quot;chain cover&quot; index
  13020. for the auth chain in a room, allowing efficient reachability queries like &quot;is
  13021. event A in the auth chain of event B&quot;. This is done by assigning every event a
  13022. <em>chain ID</em> and <em>sequence number</em> (e.g. <code>(5,3)</code>), and having a map of <em>links</em>
  13023. between chains (e.g. <code>(5,3) -&gt; (2,4)</code>) such that A is reachable by B (i.e. <code>A</code>
  13024. is in the auth chain of <code>B</code>) if and only if either:</p>
  13025. <ol>
  13026. <li>A and B have the same chain ID and <code>A</code>'s sequence number is less than <code>B</code>'s
  13027. sequence number; or</li>
  13028. <li>there is a link <code>L</code> between <code>B</code>'s chain ID and <code>A</code>'s chain ID such that
  13029. <code>L.start_seq_no</code> &lt;= <code>B.seq_no</code> and <code>A.seq_no</code> &lt;= <code>L.end_seq_no</code>.</li>
  13030. </ol>
  13031. <p>There are actually two potential implementations, one where we store links from
  13032. each chain to every other reachable chain (the transitive closure of the links
  13033. graph), and one where we remove redundant links (the transitive reduction of the
  13034. links graph) e.g. if we have chains <code>C3 -&gt; C2 -&gt; C1</code> then the link <code>C3 -&gt; C1</code>
  13035. would not be stored. Synapse uses the former implementations so that it doesn't
  13036. need to recurse to test reachability between chains.</p>
  13037. <h3 id="example-5"><a class="header" href="#example-5">Example</a></h3>
  13038. <p>An example auth graph would look like the following, where chains have been
  13039. formed based on type/state_key and are denoted by colour and are labelled with
  13040. <code>(chain ID, sequence number)</code>. Links are denoted by the arrows (links in grey
  13041. are those that would be remove in the second implementation described above).</p>
  13042. <p><img src="auth_chain_diff.dot.png" alt="Example" /></p>
  13043. <p>Note that we don't include all links between events and their auth events, as
  13044. most of those links would be redundant. For example, all events point to the
  13045. create event, but each chain only needs the one link from it's base to the
  13046. create event.</p>
  13047. <h2 id="using-the-index"><a class="header" href="#using-the-index">Using the Index</a></h2>
  13048. <p>This index can be used to calculate the auth chain difference of the state sets
  13049. by looking at the chain ID and sequence numbers reachable from each state set:</p>
  13050. <ol>
  13051. <li>For every state set lookup the chain ID/sequence numbers of each state event</li>
  13052. <li>Use the index to find all chains and the maximum sequence number reachable
  13053. from each state set.</li>
  13054. <li>The auth chain difference is then all events in each chain that have sequence
  13055. numbers between the maximum sequence number reachable from <em>any</em> state set and
  13056. the minimum reachable by <em>all</em> state sets (if any).</li>
  13057. </ol>
  13058. <p>Note that steps 2 is effectively calculating the auth chain for each state set
  13059. (in terms of chain IDs and sequence numbers), and step 3 is calculating the
  13060. difference between the union and intersection of the auth chains.</p>
  13061. <h3 id="worked-example"><a class="header" href="#worked-example">Worked Example</a></h3>
  13062. <p>For example, given the above graph, we can calculate the difference between
  13063. state sets consisting of:</p>
  13064. <ol>
  13065. <li><code>S1</code>: Alice's invite <code>(4,1)</code> and Bob's second join <code>(2,2)</code>; and</li>
  13066. <li><code>S2</code>: Alice's second join <code>(4,3)</code> and Bob's first join <code>(2,1)</code>.</li>
  13067. </ol>
  13068. <p>Using the index we see that the following auth chains are reachable from each
  13069. state set:</p>
  13070. <ol>
  13071. <li><code>S1</code>: <code>(1,1)</code>, <code>(2,2)</code>, <code>(3,1)</code> &amp; <code>(4,1)</code></li>
  13072. <li><code>S2</code>: <code>(1,1)</code>, <code>(2,1)</code>, <code>(3,2)</code> &amp; <code>(4,3)</code></li>
  13073. </ol>
  13074. <p>And so, for each the ranges that are in the auth chain difference:</p>
  13075. <ol>
  13076. <li>Chain 1: None, (since everything can reach the create event).</li>
  13077. <li>Chain 2: The range <code>(1, 2]</code> (i.e. just <code>2</code>), as <code>1</code> is reachable by all state
  13078. sets and the maximum reachable is <code>2</code> (corresponding to Bob's second join).</li>
  13079. <li>Chain 3: Similarly the range <code>(1, 2]</code> (corresponding to the second power
  13080. level).</li>
  13081. <li>Chain 4: The range <code>(1, 3]</code> (corresponding to both of Alice's joins).</li>
  13082. </ol>
  13083. <p>So the final result is: Bob's second join <code>(2,2)</code>, the second power level
  13084. <code>(3,2)</code> and both of Alice's joins <code>(4,2)</code> &amp; <code>(4,3)</code>.</p>
  13085. <div style="break-before: page; page-break-before: always;"></div><h1 id="media-repository"><a class="header" href="#media-repository">Media Repository</a></h1>
  13086. <p><em>Synapse implementation-specific details for the media repository</em></p>
  13087. <p>The media repository is where attachments and avatar photos are stored.
  13088. It stores attachment content and thumbnails for media uploaded by local users.
  13089. It caches attachment content and thumbnails for media uploaded by remote users.</p>
  13090. <h2 id="storage"><a class="header" href="#storage">Storage</a></h2>
  13091. <p>Each item of media is assigned a <code>media_id</code> when it is uploaded.
  13092. The <code>media_id</code> is a randomly chosen, URL safe 24 character string.</p>
  13093. <p>Metadata such as the MIME type, upload time and length are stored in the
  13094. sqlite3 database indexed by <code>media_id</code>.</p>
  13095. <p>Content is stored on the filesystem under a <code>&quot;local_content&quot;</code> directory.</p>
  13096. <p>Thumbnails are stored under a <code>&quot;local_thumbnails&quot;</code> directory.</p>
  13097. <p>The item with <code>media_id</code> <code>&quot;aabbccccccccdddddddddddd&quot;</code> is stored under
  13098. <code>&quot;local_content/aa/bb/ccccccccdddddddddddd&quot;</code>. Its thumbnail with width
  13099. <code>128</code> and height <code>96</code> and type <code>&quot;image/jpeg&quot;</code> is stored under
  13100. <code>&quot;local_thumbnails/aa/bb/ccccccccdddddddddddd/128-96-image-jpeg&quot;</code></p>
  13101. <p>Remote content is cached under <code>&quot;remote_content&quot;</code> directory. Each item of
  13102. remote content is assigned a local <code>&quot;filesystem_id&quot;</code> to ensure that the
  13103. directory structure <code>&quot;remote_content/server_name/aa/bb/ccccccccdddddddddddd&quot;</code>
  13104. is appropriate. Thumbnails for remote content are stored under
  13105. <code>&quot;remote_thumbnail/server_name/...&quot;</code></p>
  13106. <div style="break-before: page; page-break-before: always;"></div><h1 id="room-and-user-statistics"><a class="header" href="#room-and-user-statistics">Room and User Statistics</a></h1>
  13107. <p>Synapse maintains room and user statistics in various tables. These can be used
  13108. for administrative purposes but are also used when generating the public room
  13109. directory.</p>
  13110. <h1 id="synapse-developer-documentation"><a class="header" href="#synapse-developer-documentation">Synapse Developer Documentation</a></h1>
  13111. <h2 id="high-level-concepts"><a class="header" href="#high-level-concepts">High-Level Concepts</a></h2>
  13112. <h3 id="definitions-1"><a class="header" href="#definitions-1">Definitions</a></h3>
  13113. <ul>
  13114. <li><strong>subject</strong>: Something we are tracking stats about – currently a room or user.</li>
  13115. <li><strong>current row</strong>: An entry for a subject in the appropriate current statistics
  13116. table. Each subject can have only one.</li>
  13117. </ul>
  13118. <h3 id="overview-4"><a class="header" href="#overview-4">Overview</a></h3>
  13119. <p>Stats correspond to the present values. Current rows contain the most up-to-date
  13120. statistics for a room. Each subject can only have one entry.</p>
  13121. <div style="break-before: page; page-break-before: always;"></div><h1 id="deprecation-policy-for-platform-dependencies"><a class="header" href="#deprecation-policy-for-platform-dependencies">Deprecation Policy for Platform Dependencies</a></h1>
  13122. <p>Synapse has a number of platform dependencies, including Python and PostgreSQL.
  13123. This document outlines the policy towards which versions we support, and when we
  13124. drop support for versions in the future.</p>
  13125. <h2 id="policy"><a class="header" href="#policy">Policy</a></h2>
  13126. <p>Synapse follows the upstream support life cycles for Python and PostgreSQL,
  13127. i.e. when a version reaches End of Life Synapse will withdraw support for that
  13128. version in future releases.</p>
  13129. <p>Details on the upstream support life cycles for Python and PostgreSQL are
  13130. documented at https://endoflife.date/python and
  13131. https://endoflife.date/postgresql.</p>
  13132. <h2 id="context"><a class="header" href="#context">Context</a></h2>
  13133. <p>It is important for system admins to have a clear understanding of the platform
  13134. requirements of Synapse and its deprecation policies so that they can
  13135. effectively plan upgrading their infrastructure ahead of time. This is
  13136. especially important in contexts where upgrading the infrastructure requires
  13137. auditing and approval from a security team, or where otherwise upgrading is a
  13138. long process.</p>
  13139. <p>By following the upstream support life cycles Synapse can ensure that its
  13140. dependencies continue to get security patches, while not requiring system admins
  13141. to constantly update their platform dependencies to the latest versions.</p>
  13142. </main>
  13143. <nav class="nav-wrapper" aria-label="Page navigation">
  13144. <!-- Mobile navigation buttons -->
  13145. <div style="clear: both"></div>
  13146. </nav>
  13147. </div>
  13148. </div>
  13149. <nav class="nav-wide-wrapper" aria-label="Page navigation">
  13150. </nav>
  13151. </div>
  13152. <script type="text/javascript">
  13153. window.playground_copyable = true;
  13154. </script>
  13155. <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
  13156. <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
  13157. <script src="searcher.js" type="text/javascript" charset="utf-8"></script>
  13158. <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
  13159. <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
  13160. <script src="book.js" type="text/javascript" charset="utf-8"></script>
  13161. <!-- Custom JS scripts -->
  13162. <script type="text/javascript" src="docs/website_files/table-of-contents.js"></script>
  13163. <script type="text/javascript" src="docs/website_files/version-picker.js"></script>
  13164. <script type="text/javascript" src="docs/website_files/version.js"></script>
  13165. <script type="text/javascript">
  13166. window.addEventListener('load', function() {
  13167. window.setTimeout(window.print, 100);
  13168. });
  13169. </script>
  13170. </body>
  13171. </html>