print.html 996 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824
  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 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/config_documentation.html">Configuration Manual</a></li><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 "><a href="usage/configuration/user_authentication/single_sign_on/index.html">Single-Sign On</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="openid.html">OpenID Connect</a></li><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/single_sign_on/saml.html">SAML</a></li><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/single_sign_on/cas.html">CAS</a></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><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/refresh_tokens.html">Refresh 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/background_update_controller_callbacks.html">Background update controller callbacks</a></li><li class="chapter-item expanded "><a href="modules/account_data_callbacks.html">Account data 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><li class="chapter-item expanded "><a href="usage/administration/admin_api/federation.html">Federation</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/understanding_synapse_through_grafana_graphs.html">Understanding Synapse Through Grafana Graphs</a></li><li class="chapter-item expanded "><a href="usage/administration/useful_sql_for_admins.html">Useful SQL for Admins</a></li><li class="chapter-item expanded "><a href="usage/administration/database_maintenance_tools.html">Database Maintenance Tools</a></li><li class="chapter-item expanded "><a href="usage/administration/state_groups.html">State Groups</a></li><li class="chapter-item expanded "><a href="usage/administration/request_log.html">Request log format</a></li><li class="chapter-item expanded "><a href="usage/administration/admin_faq.html">Admin FAQ</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/releases.html">Release Cycle</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><ol class="section"><li class="chapter-item expanded "><a href="development/demo.html">Demo scripts</a></li></ol></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="development/synapse_architecture/cancellation.html">Cancellation</a></li><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><li class="chapter-item expanded "><a href="other/running_synapse_on_single_board_computers.html">Running Synapse on a Single-Board Computer</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>maintenance</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/config_documentation.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. supporter 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. <p>When installing with Debian packages, you might prefer to place files in
  288. <code>/etc/matrix-synapse/conf.d/</code> to override your configuration without editing
  289. the main configuration file at <code>/etc/matrix-synapse/homeserver.yaml</code>.
  290. By doing that, you won't be asked if you want to replace your configuration
  291. file when you upgrade the Debian package to a later version.</p>
  292. <h5 id="downstream-debian-packages"><a class="header" href="#downstream-debian-packages">Downstream Debian packages</a></h5>
  293. <p>We do not recommend using the packages from the default Debian <code>buster</code>
  294. repository at this time, as they are old and suffer from known security
  295. vulnerabilities. You can install the latest version of Synapse from
  296. <a href="setup/installation.html#matrixorg-packages">our repository</a> or from <code>buster-backports</code>. Please
  297. see the <a href="https://backports.debian.org/Instructions/">Debian documentation</a>
  298. for information on how to use backports.</p>
  299. <p>If you are using Debian <code>sid</code> or testing, Synapse is available in the default
  300. repositories and it should be possible to install it simply with:</p>
  301. <pre><code class="language-sh">sudo apt install matrix-synapse
  302. </code></pre>
  303. <h5 id="downstream-ubuntu-packages"><a class="header" href="#downstream-ubuntu-packages">Downstream Ubuntu packages</a></h5>
  304. <p>We do not recommend using the packages in the default Ubuntu repository
  305. at this time, as they are old and suffer from known security vulnerabilities.
  306. The latest version of Synapse can be installed from <a href="setup/installation.html#matrixorg-packages">our repository</a>.</p>
  307. <h4 id="fedora"><a class="header" href="#fedora">Fedora</a></h4>
  308. <p>Synapse is in the Fedora repositories as <code>matrix-synapse</code>:</p>
  309. <pre><code class="language-sh">sudo dnf install matrix-synapse
  310. </code></pre>
  311. <p>Oleg Girko provides Fedora RPMs at
  312. <a href="https://obs.infoserver.lv/project/monitor/matrix-synapse">https://obs.infoserver.lv/project/monitor/matrix-synapse</a></p>
  313. <h4 id="opensuse"><a class="header" href="#opensuse">OpenSUSE</a></h4>
  314. <p>Synapse is in the OpenSUSE repositories as <code>matrix-synapse</code>:</p>
  315. <pre><code class="language-sh">sudo zypper install matrix-synapse
  316. </code></pre>
  317. <h4 id="suse-linux-enterprise-server"><a class="header" href="#suse-linux-enterprise-server">SUSE Linux Enterprise Server</a></h4>
  318. <p>Unofficial package are built for SLES 15 in the openSUSE:Backports:SLE-15 repository at
  319. <a href="https://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-15/standard/">https://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-15/standard/</a></p>
  320. <h4 id="archlinux"><a class="header" href="#archlinux">ArchLinux</a></h4>
  321. <p>The quickest way to get up and running with ArchLinux is probably with the community package
  322. <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
  323. the necessary dependencies.</p>
  324. <p>pip may be outdated (6.0.7-1 and needs to be upgraded to 6.0.8-1 ):</p>
  325. <pre><code class="language-sh">sudo pip install --upgrade pip
  326. </code></pre>
  327. <p>If you encounter an error with lib bcrypt causing an Wrong ELF Class:
  328. ELFCLASS32 (x64 Systems), you may need to reinstall py-bcrypt to correctly
  329. compile it under the right architecture. (This should not be needed if
  330. installing under virtualenv):</p>
  331. <pre><code class="language-sh">sudo pip uninstall py-bcrypt
  332. sudo pip install py-bcrypt
  333. </code></pre>
  334. <h4 id="void-linux"><a class="header" href="#void-linux">Void Linux</a></h4>
  335. <p>Synapse can be found in the void repositories as 'synapse':</p>
  336. <pre><code class="language-sh">xbps-install -Su
  337. xbps-install -S synapse
  338. </code></pre>
  339. <h4 id="freebsd"><a class="header" href="#freebsd">FreeBSD</a></h4>
  340. <p>Synapse can be installed via FreeBSD Ports or Packages contributed by Brendan Molloy from:</p>
  341. <ul>
  342. <li>Ports: <code>cd /usr/ports/net-im/py-matrix-synapse &amp;&amp; make install clean</code></li>
  343. <li>Packages: <code>pkg install py38-matrix-synapse</code></li>
  344. </ul>
  345. <h4 id="openbsd"><a class="header" href="#openbsd">OpenBSD</a></h4>
  346. <p>As of OpenBSD 6.7 Synapse is available as a pre-compiled binary. The filesystem
  347. underlying the homeserver directory (defaults to <code>/var/synapse</code>) has to be
  348. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>), so creating a separate filesystem
  349. and mounting it to <code>/var/synapse</code> should be taken into consideration.</p>
  350. <p>Installing Synapse:</p>
  351. <pre><code class="language-sh">doas pkg_add synapse
  352. </code></pre>
  353. <h4 id="nixos"><a class="header" href="#nixos">NixOS</a></h4>
  354. <p>Robin Lambertz has packaged Synapse for NixOS at:
  355. <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>
  356. <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>
  357. <p>It's also possible to install Synapse as a Python module from PyPI.</p>
  358. <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>
  359. <p>System requirements:</p>
  360. <ul>
  361. <li>POSIX-compliant system (tested on Linux &amp; OS X)</li>
  362. <li>Python 3.7 or later, up to Python 3.10.</li>
  363. <li>At least 1GB of free RAM if you want to join large public rooms like #matrix:matrix.org</li>
  364. </ul>
  365. <p>To install the Synapse homeserver run:</p>
  366. <pre><code class="language-sh">mkdir -p ~/synapse
  367. virtualenv -p python3 ~/synapse/env
  368. source ~/synapse/env/bin/activate
  369. pip install --upgrade pip
  370. pip install --upgrade setuptools
  371. pip install matrix-synapse
  372. </code></pre>
  373. <p>This will download Synapse from <a href="https://pypi.org/project/matrix-synapse">PyPI</a>
  374. and install it, along with the python libraries it uses, into a virtual environment
  375. under <code>~/synapse/env</code>. Feel free to pick a different directory if you
  376. prefer.</p>
  377. <p>This Synapse installation can then be later upgraded by using pip again with the
  378. update flag:</p>
  379. <pre><code class="language-sh">source ~/synapse/env/bin/activate
  380. pip install -U matrix-synapse
  381. </code></pre>
  382. <p>Before you can start Synapse, you will need to generate a configuration
  383. file. To do this, run (in your virtualenv, as before):</p>
  384. <pre><code class="language-sh">cd ~/synapse
  385. python -m synapse.app.homeserver \
  386. --server-name my.domain.name \
  387. --config-path homeserver.yaml \
  388. --generate-config \
  389. --report-stats=[yes|no]
  390. </code></pre>
  391. <p>... substituting an appropriate value for <code>--server-name</code>.</p>
  392. <p>This command will generate you a config file that you can then customise, but it will
  393. also generate a set of keys for you. These keys will allow your homeserver to
  394. identify itself to other homeserver, so don't lose or delete them. It would be
  395. wise to back them up somewhere safe. (If, for whatever reason, you do need to
  396. change your homeserver's keys, you may find that other homeserver have the
  397. old key cached. If you update the signing key, you should change the name of the
  398. key in the <code>&lt;server name&gt;.signing.key</code> file (the second word) to something
  399. 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>
  400. <p>To actually run your new homeserver, pick a working directory for Synapse to
  401. run (e.g. <code>~/synapse</code>), and:</p>
  402. <pre><code class="language-sh">cd ~/synapse
  403. source env/bin/activate
  404. synctl start
  405. </code></pre>
  406. <h4 id="platform-specific-prerequisites"><a class="header" href="#platform-specific-prerequisites">Platform-specific prerequisites</a></h4>
  407. <p>Synapse is written in Python but some of the libraries it uses are written in
  408. C. So before we can install Synapse itself we need a working C compiler and the
  409. header files for Python C extensions.</p>
  410. <h5 id="debianubunturaspbian"><a class="header" href="#debianubunturaspbian">Debian/Ubuntu/Raspbian</a></h5>
  411. <p>Installing prerequisites on Ubuntu or Debian:</p>
  412. <pre><code class="language-sh">sudo apt install build-essential python3-dev libffi-dev \
  413. python3-pip python3-setuptools sqlite3 \
  414. libssl-dev virtualenv libjpeg-dev libxslt1-dev
  415. </code></pre>
  416. <h5 id="archlinux-1"><a class="header" href="#archlinux-1">ArchLinux</a></h5>
  417. <p>Installing prerequisites on ArchLinux:</p>
  418. <pre><code class="language-sh">sudo pacman -S base-devel python python-pip \
  419. python-setuptools python-virtualenv sqlite3
  420. </code></pre>
  421. <h5 id="centosfedora"><a class="header" href="#centosfedora">CentOS/Fedora</a></h5>
  422. <p>Installing prerequisites on CentOS or Fedora Linux:</p>
  423. <pre><code class="language-sh">sudo dnf install libtiff-devel libjpeg-devel libzip-devel freetype-devel \
  424. libwebp-devel libxml2-devel libxslt-devel libpq-devel \
  425. python3-virtualenv libffi-devel openssl-devel python3-devel
  426. sudo dnf groupinstall &quot;Development Tools&quot;
  427. </code></pre>
  428. <h5 id="macos"><a class="header" href="#macos">macOS</a></h5>
  429. <p>Installing prerequisites on macOS:</p>
  430. <p>You may need to install the latest Xcode developer tools:</p>
  431. <pre><code class="language-sh">xcode-select --install
  432. </code></pre>
  433. <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>
  434. <pre><code class="language-sh"> brew install jpeg
  435. </code></pre>
  436. <p>On macOS Catalina (10.15) you may need to explicitly install OpenSSL
  437. via brew and inform <code>pip</code> about it so that <code>psycopg2</code> builds:</p>
  438. <pre><code class="language-sh">brew install openssl@1.1
  439. export LDFLAGS=&quot;-L/usr/local/opt/openssl/lib&quot;
  440. export CPPFLAGS=&quot;-I/usr/local/opt/openssl/include&quot;
  441. </code></pre>
  442. <h5 id="opensuse-1"><a class="header" href="#opensuse-1">OpenSUSE</a></h5>
  443. <p>Installing prerequisites on openSUSE:</p>
  444. <pre><code class="language-sh">sudo zypper in -t pattern devel_basis
  445. sudo zypper in python-pip python-setuptools sqlite3 python-virtualenv \
  446. python-devel libffi-devel libopenssl-devel libjpeg62-devel
  447. </code></pre>
  448. <h5 id="openbsd-1"><a class="header" href="#openbsd-1">OpenBSD</a></h5>
  449. <p>A port of Synapse is available under <code>net/synapse</code>. The filesystem
  450. underlying the homeserver directory (defaults to <code>/var/synapse</code>) has to be
  451. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>), so creating a separate filesystem
  452. and mounting it to <code>/var/synapse</code> should be taken into consideration.</p>
  453. <p>To be able to build Synapse's dependency on python the <code>WRKOBJDIR</code>
  454. (cf. <code>bsd.port.mk(5)</code>) for building python, too, needs to be on a filesystem
  455. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>).</p>
  456. <p>Creating a <code>WRKOBJDIR</code> for building python under <code>/usr/local</code> (which on a
  457. default OpenBSD installation is mounted with <code>wxallowed</code>):</p>
  458. <pre><code class="language-sh">doas mkdir /usr/local/pobj_wxallowed
  459. </code></pre>
  460. <p>Assuming <code>PORTS_PRIVSEP=Yes</code> (cf. <code>bsd.port.mk(5)</code>) and <code>SUDO=doas</code> are
  461. configured in <code>/etc/mk.conf</code>:</p>
  462. <pre><code class="language-sh">doas chown _pbuild:_pbuild /usr/local/pobj_wxallowed
  463. </code></pre>
  464. <p>Setting the <code>WRKOBJDIR</code> for building python:</p>
  465. <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
  466. </code></pre>
  467. <p>Building Synapse:</p>
  468. <pre><code class="language-sh">cd /usr/ports/net/synapse
  469. make install
  470. </code></pre>
  471. <h5 id="windows"><a class="header" href="#windows">Windows</a></h5>
  472. <p>Running Synapse natively on Windows is not officially supported.</p>
  473. <p>If you wish to run or develop Synapse on Windows, the Windows Subsystem for
  474. Linux provides a Linux environment which is capable of using the Debian, Fedora,
  475. or source installation methods. More information about WSL can be found at
  476. <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
  477. <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
  478. Windows Server.</p>
  479. <h2 id="setting-up-synapse"><a class="header" href="#setting-up-synapse">Setting up Synapse</a></h2>
  480. <p>Once you have installed synapse as above, you will need to configure it.</p>
  481. <h3 id="using-postgresql"><a class="header" href="#using-postgresql">Using PostgreSQL</a></h3>
  482. <p>By default Synapse uses an <a href="https://sqlite.org/">SQLite</a> database and in doing so trades
  483. performance for convenience. Almost all installations should opt to use <a href="https://www.postgresql.org">PostgreSQL</a>
  484. instead. Advantages include:</p>
  485. <ul>
  486. <li>significant performance improvements due to the superior threading and
  487. caching model, smarter query optimiser</li>
  488. <li>allowing the DB to be run on separate hardware</li>
  489. </ul>
  490. <p>For information on how to install and use PostgreSQL in Synapse, please see
  491. <a href="setup/../postgres.html">Using Postgres</a></p>
  492. <p>SQLite is only acceptable for testing purposes. SQLite should not be used in
  493. a production server. Synapse will perform poorly when using
  494. SQLite, especially when participating in large rooms.</p>
  495. <h3 id="tls-certificates"><a class="header" href="#tls-certificates">TLS certificates</a></h3>
  496. <p>The default configuration exposes a single HTTP port on the local
  497. interface: <code>http://localhost:8008</code>. It is suitable for local testing,
  498. but for any practical use, you will need Synapse's APIs to be served
  499. over HTTPS.</p>
  500. <p>The recommended way to do so is to set up a reverse proxy on port
  501. <code>8448</code>. You can find documentation on doing so in
  502. <a href="setup/../reverse_proxy.html">the reverse proxy documentation</a>.</p>
  503. <p>Alternatively, you can configure Synapse to expose an HTTPS port. To do
  504. so, you will need to edit <code>homeserver.yaml</code>, as follows:</p>
  505. <ul>
  506. <li>First, under the <code>listeners</code> section, uncomment the configuration for the
  507. TLS-enabled listener. (Remove the hash sign (<code>#</code>) at the start of
  508. each line). The relevant lines are like this:</li>
  509. </ul>
  510. <pre><code class="language-yaml"> - port: 8448
  511. type: http
  512. tls: true
  513. resources:
  514. - names: [client, federation]
  515. </code></pre>
  516. <ul>
  517. <li>
  518. <p>You will also need to uncomment the <code>tls_certificate_path</code> and
  519. <code>tls_private_key_path</code> lines under the <code>TLS</code> section. You will need to manage
  520. provisioning of these certificates yourself.</p>
  521. <p>If you are using your own certificate, be sure to use a <code>.pem</code> file that
  522. includes the full certificate chain including any intermediate certificates
  523. (for instance, if using certbot, use <code>fullchain.pem</code> as your certificate, not
  524. <code>cert.pem</code>).</p>
  525. </li>
  526. </ul>
  527. <p>For a more detailed guide to configuring your server for federation, see
  528. <a href="setup/../federate.html">Federation</a>.</p>
  529. <h3 id="client-well-known-uri"><a class="header" href="#client-well-known-uri">Client Well-Known URI</a></h3>
  530. <p>Setting up the client Well-Known URI is optional but if you set it up, it will
  531. allow users to enter their full username (e.g. <code>@user:&lt;server_name&gt;</code>) into clients
  532. which support well-known lookup to automatically configure the homeserver and
  533. identity server URLs. This is useful so that users don't have to memorize or think
  534. about the actual homeserver URL you are using.</p>
  535. <p>The URL <code>https://&lt;server_name&gt;/.well-known/matrix/client</code> should return JSON in
  536. the following format.</p>
  537. <pre><code class="language-json">{
  538. &quot;m.homeserver&quot;: {
  539. &quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;
  540. }
  541. }
  542. </code></pre>
  543. <p>It can optionally contain identity server information as well.</p>
  544. <pre><code class="language-json">{
  545. &quot;m.homeserver&quot;: {
  546. &quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;
  547. },
  548. &quot;m.identity_server&quot;: {
  549. &quot;base_url&quot;: &quot;https://&lt;identity.example.com&gt;&quot;
  550. }
  551. }
  552. </code></pre>
  553. <p>To work in browser based clients, the file must be served with the appropriate
  554. Cross-Origin Resource Sharing (CORS) headers. A recommended value would be
  555. <code>Access-Control-Allow-Origin: *</code> which would allow all browser based clients to
  556. view it.</p>
  557. <p>In nginx this would be something like:</p>
  558. <pre><code class="language-nginx">location /.well-known/matrix/client {
  559. return 200 '{&quot;m.homeserver&quot;: {&quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;}}';
  560. default_type application/json;
  561. add_header Access-Control-Allow-Origin *;
  562. }
  563. </code></pre>
  564. <p>You should also ensure the <code>public_baseurl</code> option in <code>homeserver.yaml</code> is set
  565. correctly. <code>public_baseurl</code> should be set to the URL that clients will use to
  566. connect to your server. This is the same URL you put for the <code>m.homeserver</code>
  567. <code>base_url</code> above.</p>
  568. <pre><code class="language-yaml">public_baseurl: &quot;https://&lt;matrix.example.com&gt;&quot;
  569. </code></pre>
  570. <h3 id="email"><a class="header" href="#email">Email</a></h3>
  571. <p>It is desirable for Synapse to have the capability to send email. This allows
  572. Synapse to send password reset emails, send verifications when an email address
  573. is added to a user's account, and send email notifications to users when they
  574. receive new messages.</p>
  575. <p>To configure an SMTP server for Synapse, modify the configuration section
  576. headed <code>email</code>, and be sure to have at least the <code>smtp_host</code>, <code>smtp_port</code>
  577. and <code>notif_from</code> fields filled out. You may also need to set <code>smtp_user</code>,
  578. <code>smtp_pass</code>, and <code>require_transport_security</code>.</p>
  579. <p>If email is not configured, password reset, registration and notifications via
  580. email will be disabled.</p>
  581. <h3 id="registering-a-user"><a class="header" href="#registering-a-user">Registering a user</a></h3>
  582. <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>
  583. <p>Alternatively, you can do so from the command line. This can be done as follows:</p>
  584. <ol>
  585. <li>If synapse was installed via pip, activate the virtualenv as follows (if Synapse was
  586. installed via a prebuilt package, <code>register_new_matrix_user</code> should already be
  587. on the search path):
  588. <pre><code class="language-sh">cd ~/synapse
  589. source env/bin/activate
  590. synctl start # if not already running
  591. </code></pre>
  592. </li>
  593. <li>Run the following command:
  594. <pre><code class="language-sh">register_new_matrix_user -c homeserver.yaml http://localhost:8008
  595. </code></pre>
  596. </li>
  597. </ol>
  598. <p>This will prompt you to add details for the new user, and will then connect to
  599. the running Synapse to create the new user. For example:</p>
  600. <pre><code>New user localpart: erikj
  601. Password:
  602. Confirm password:
  603. Make admin [no]:
  604. Success!
  605. </code></pre>
  606. <p>This process uses a setting <code>registration_shared_secret</code> in
  607. <code>homeserver.yaml</code>, which is shared between Synapse itself and the
  608. <code>register_new_matrix_user</code> script. It doesn't matter what it is (a random
  609. value is generated by <code>--generate-config</code>), but it should be kept secret, as
  610. anyone with knowledge of it can register users, including admin accounts,
  611. on your server even if <code>enable_registration</code> is <code>false</code>.</p>
  612. <h3 id="setting-up-a-turn-server"><a class="header" href="#setting-up-a-turn-server">Setting up a TURN server</a></h3>
  613. <p>For reliable VoIP calls to be routed via this homeserver, you MUST configure
  614. a TURN server. See <a href="setup/../turn-howto.html">TURN setup</a> for details.</p>
  615. <h3 id="url-previews"><a class="header" href="#url-previews">URL previews</a></h3>
  616. <p>Synapse includes support for previewing URLs, which is disabled by default. To
  617. turn it on you must enable the <code>url_preview_enabled: True</code> config parameter
  618. and explicitly specify the IP ranges that Synapse is not allowed to spider for
  619. previewing in the <code>url_preview_ip_range_blacklist</code> configuration parameter.
  620. This is critical from a security perspective to stop arbitrary Matrix users
  621. spidering 'internal' URLs on your network. At the very least we recommend that
  622. your loopback and RFC1918 IP addresses are blacklisted.</p>
  623. <p>This also requires the optional <code>lxml</code> python dependency to be installed. This
  624. in turn requires the <code>libxml2</code> library to be available - on Debian/Ubuntu this
  625. means <code>apt-get install libxml2-dev</code>, or equivalent for your OS.</p>
  626. <h3 id="troubleshooting-installation"><a class="header" href="#troubleshooting-installation">Troubleshooting Installation</a></h3>
  627. <p><code>pip</code> seems to leak <em>lots</em> of memory during installation. For instance, a Linux
  628. host with 512MB of RAM may run out of memory whilst installing Twisted. If this
  629. happens, you will have to individually install the dependencies which are
  630. failing, e.g.:</p>
  631. <pre><code class="language-sh">pip install twisted
  632. </code></pre>
  633. <p>If you have any other problems, feel free to ask in
  634. <a href="https://matrix.to/#/#synapse:matrix.org">#synapse:matrix.org</a>.</p>
  635. <div style="break-before: page; page-break-before: always;"></div><h1 id="using-postgres"><a class="header" href="#using-postgres">Using Postgres</a></h1>
  636. <p>Synapse supports PostgreSQL versions 10 or later.</p>
  637. <h2 id="install-postgres-client-libraries"><a class="header" href="#install-postgres-client-libraries">Install postgres client libraries</a></h2>
  638. <p>Synapse will require the python postgres client library in order to
  639. connect to a postgres database.</p>
  640. <ul>
  641. <li>
  642. <p>If you are using the <a href="setup/installation.html#matrixorg-packages">matrix.org debian/ubuntu
  643. packages</a>, the necessary python
  644. library will already be installed, but you will need to ensure the
  645. low-level postgres library is installed, which you can do with
  646. <code>apt install libpq5</code>.</p>
  647. </li>
  648. <li>
  649. <p>For other pre-built packages, please consult the documentation from
  650. the relevant package.</p>
  651. </li>
  652. <li>
  653. <p>If you installed synapse <a href="setup/installation.html#installing-from-source">in a
  654. virtualenv</a>, you can install
  655. the library with:</p>
  656. <pre><code>~/synapse/env/bin/pip install &quot;matrix-synapse[postgres]&quot;
  657. </code></pre>
  658. <p>(substituting the path to your virtualenv for <code>~/synapse/env</code>, if
  659. you used a different path). You will require the postgres
  660. development files. These are in the <code>libpq-dev</code> package on
  661. Debian-derived distributions.</p>
  662. </li>
  663. </ul>
  664. <h2 id="set-up-database"><a class="header" href="#set-up-database">Set up database</a></h2>
  665. <p>Assuming your PostgreSQL database user is called <code>postgres</code>, first authenticate as the database user with:</p>
  666. <pre><code class="language-sh">su - postgres
  667. # Or, if your system uses sudo to get administrative rights
  668. sudo -u postgres bash
  669. </code></pre>
  670. <p>Then, create a postgres user and a database with:</p>
  671. <pre><code class="language-sh"># this will prompt for a password for the new user
  672. createuser --pwprompt synapse_user
  673. createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
  674. </code></pre>
  675. <p>The above will create a user called <code>synapse_user</code>, and a database called
  676. <code>synapse</code>.</p>
  677. <p>Note that the PostgreSQL database <em>must</em> have the correct encoding set
  678. (as shown above), otherwise it will not be able to store UTF8 strings.</p>
  679. <p>You may need to enable password authentication so <code>synapse_user</code> can
  680. connect to the database. See
  681. <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>
  682. <h2 id="synapse-config"><a class="header" href="#synapse-config">Synapse config</a></h2>
  683. <p>When you are ready to start using PostgreSQL, edit the <code>database</code>
  684. section in your config file to match the following lines:</p>
  685. <pre><code class="language-yaml">database:
  686. name: psycopg2
  687. args:
  688. user: &lt;user&gt;
  689. password: &lt;pass&gt;
  690. database: &lt;db&gt;
  691. host: &lt;host&gt;
  692. cp_min: 5
  693. cp_max: 10
  694. </code></pre>
  695. <p>All key, values in <code>args</code> are passed to the <code>psycopg2.connect(..)</code>
  696. function, except keys beginning with <code>cp_</code>, which are consumed by the
  697. twisted adbapi connection pool. See the <a href="https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS">libpq
  698. documentation</a>
  699. for a list of options which can be passed.</p>
  700. <p>You should consider tuning the <code>args.keepalives_*</code> options if there is any danger of
  701. the connection between your homeserver and database dropping, otherwise Synapse
  702. may block for an extended period while it waits for a response from the
  703. database server. Example values might be:</p>
  704. <pre><code class="language-yaml">database:
  705. args:
  706. # ... as above
  707. # seconds of inactivity after which TCP should send a keepalive message to the server
  708. keepalives_idle: 10
  709. # the number of seconds after which a TCP keepalive message that is not
  710. # acknowledged by the server should be retransmitted
  711. keepalives_interval: 10
  712. # the number of TCP keepalives that can be lost before the client's connection
  713. # to the server is considered dead
  714. keepalives_count: 3
  715. </code></pre>
  716. <h2 id="tuning-postgres"><a class="header" href="#tuning-postgres">Tuning Postgres</a></h2>
  717. <p>The default settings should be fine for most deployments. For larger
  718. scale deployments tuning some of the settings is recommended, details of
  719. which can be found at
  720. <a href="https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server">https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server</a>.</p>
  721. <p>In particular, we've found tuning the following values helpful for
  722. performance:</p>
  723. <ul>
  724. <li><code>shared_buffers</code></li>
  725. <li><code>effective_cache_size</code></li>
  726. <li><code>work_mem</code></li>
  727. <li><code>maintenance_work_mem</code></li>
  728. <li><code>autovacuum_work_mem</code></li>
  729. </ul>
  730. <p>Note that the appropriate values for those fields depend on the amount
  731. of free memory the database host has available.</p>
  732. <p>Additionally, admins of large deployments might want to consider using huge pages
  733. to help manage memory, especially when using large values of <code>shared_buffers</code>. You
  734. can read more about that <a href="https://www.postgresql.org/docs/10/kernel-resources.html#LINUX-HUGE-PAGES">here</a>.</p>
  735. <h2 id="porting-from-sqlite"><a class="header" href="#porting-from-sqlite">Porting from SQLite</a></h2>
  736. <h3 id="overview"><a class="header" href="#overview">Overview</a></h3>
  737. <p>The script <code>synapse_port_db</code> allows porting an existing synapse server
  738. backed by SQLite to using PostgreSQL. This is done in as a two phase
  739. process:</p>
  740. <ol>
  741. <li>Copy the existing SQLite database to a separate location and run
  742. the port script against that offline database.</li>
  743. <li>Shut down the server. Rerun the port script to port any data that
  744. has come in since taking the first snapshot. Restart server against
  745. the PostgreSQL database.</li>
  746. </ol>
  747. <p>The port script is designed to be run repeatedly against newer snapshots
  748. of the SQLite database file. This makes it safe to repeat step 1 if
  749. there was a delay between taking the previous snapshot and being ready
  750. to do step 2.</p>
  751. <p>It is safe to at any time kill the port script and restart it.</p>
  752. <p>Note that the database may take up significantly more (25% - 100% more)
  753. space on disk after porting to Postgres.</p>
  754. <h3 id="using-the-port-script"><a class="header" href="#using-the-port-script">Using the port script</a></h3>
  755. <p>Firstly, shut down the currently running synapse server and copy its
  756. database file (typically <code>homeserver.db</code>) to another location. Once the
  757. copy is complete, restart synapse. For instance:</p>
  758. <pre><code class="language-sh">synctl stop
  759. cp homeserver.db homeserver.db.snapshot
  760. synctl start
  761. </code></pre>
  762. <p>Copy the old config file into a new config file:</p>
  763. <pre><code class="language-sh">cp homeserver.yaml homeserver-postgres.yaml
  764. </code></pre>
  765. <p>Edit the database section as described in the section <em>Synapse config</em>
  766. above and with the SQLite snapshot located at <code>homeserver.db.snapshot</code>
  767. simply run:</p>
  768. <pre><code class="language-sh">synapse_port_db --sqlite-database homeserver.db.snapshot \
  769. --postgres-config homeserver-postgres.yaml
  770. </code></pre>
  771. <p>The flag <code>--curses</code> displays a coloured curses progress UI.</p>
  772. <p>If the script took a long time to complete, or time has otherwise passed
  773. since the original snapshot was taken, repeat the previous steps with a
  774. newer snapshot.</p>
  775. <p>To complete the conversion shut down the synapse server and run the port
  776. script one last time, e.g. if the SQLite database is at <code>homeserver.db</code>
  777. run:</p>
  778. <pre><code class="language-sh">synapse_port_db --sqlite-database homeserver.db \
  779. --postgres-config homeserver-postgres.yaml
  780. </code></pre>
  781. <p>Once that has completed, change the synapse config to point at the
  782. PostgreSQL database configuration file <code>homeserver-postgres.yaml</code>:</p>
  783. <pre><code class="language-sh">synctl stop
  784. mv homeserver.yaml homeserver-old-sqlite.yaml
  785. mv homeserver-postgres.yaml homeserver.yaml
  786. synctl start
  787. </code></pre>
  788. <p>Synapse should now be running against PostgreSQL.</p>
  789. <h2 id="troubleshooting"><a class="header" href="#troubleshooting">Troubleshooting</a></h2>
  790. <h3 id="alternative-auth-methods"><a class="header" href="#alternative-auth-methods">Alternative auth methods</a></h3>
  791. <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
  792. <code>ident</code>:</p>
  793. <ul>
  794. <li>
  795. <p>If the <code>synapse_user</code> user has a password, add the password to the <code>database:</code>
  796. section of <code>homeserver.yaml</code>. Then add the following to <code>pg_hba.conf</code>:</p>
  797. <pre><code>host synapse synapse_user ::1/128 md5 # or `scram-sha-256` instead of `md5` if you use that
  798. </code></pre>
  799. </li>
  800. <li>
  801. <p>If the <code>synapse_user</code> user does not have a password, then a password doesn't
  802. have to be added to <code>homeserver.yaml</code>. But the following does need to be added
  803. to <code>pg_hba.conf</code>:</p>
  804. <pre><code>host synapse synapse_user ::1/128 trust
  805. </code></pre>
  806. </li>
  807. </ul>
  808. <p>Note that line order matters in <code>pg_hba.conf</code>, so make sure that if you do add a
  809. new line, it is inserted before:</p>
  810. <pre><code>host all all ::1/128 ident
  811. </code></pre>
  812. <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>
  813. <p>Synapse will refuse to set up a new database if it has the wrong values of
  814. <code>COLLATE</code> and <code>CTYPE</code> set. Synapse will also refuse to start an existing database with incorrect values
  815. of <code>COLLATE</code> and <code>CTYPE</code> unless the config flag <code>allow_unsafe_locale</code>, found in the
  816. <code>database</code> section of the config, is set to true. Using different locales can cause issues if the locale library is updated from
  817. underneath the database, or if a different version of the locale is used on any
  818. replicas.</p>
  819. <p>If you have a databse with an unsafe locale, the safest way to fix the issue is to dump the database and recreate it with
  820. the correct locale parameter (as shown above). It is also possible to change the
  821. parameters on a live database and run a <code>REINDEX</code> on the entire database,
  822. however extreme care must be taken to avoid database corruption.</p>
  823. <p>Note that the above may fail with an error about duplicate rows if corruption
  824. has already occurred, and such duplicate rows will need to be manually removed.</p>
  825. <h3 id="fixing-inconsistent-sequences-error"><a class="header" href="#fixing-inconsistent-sequences-error">Fixing inconsistent sequences error</a></h3>
  826. <p>Synapse uses Postgres sequences to generate IDs for various tables. A sequence
  827. and associated table can get out of sync if, for example, Synapse has been
  828. downgraded and then upgraded again.</p>
  829. <p>To fix the issue shut down Synapse (including any and all workers) and run the
  830. SQL command included in the error message. Once done Synapse should start
  831. successfully.</p>
  832. <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>
  833. <p>It is recommended to put a reverse proxy such as
  834. <a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html">nginx</a>,
  835. <a href="https://httpd.apache.org/docs/current/mod/mod_proxy_http.html">Apache</a>,
  836. <a href="https://caddyserver.com/docs/quick-starts/reverse-proxy">Caddy</a>,
  837. <a href="https://www.haproxy.org/">HAProxy</a> or
  838. <a href="https://man.openbsd.org/relayd.8">relayd</a> in front of Synapse. One advantage
  839. of doing so is that it means that you can expose the default https port
  840. (443) to Matrix clients without needing to run Synapse with root
  841. privileges.</p>
  842. <p>You should configure your reverse proxy to forward requests to <code>/_matrix</code> or
  843. <code>/_synapse/client</code> to Synapse, and have it set the <code>X-Forwarded-For</code> and
  844. <code>X-Forwarded-Proto</code> request headers.</p>
  845. <p>You should remember that Matrix clients and other Matrix servers do not
  846. necessarily need to connect to your server via the same server name or
  847. port. Indeed, clients will use port 443 by default, whereas servers default to
  848. port 8448. Where these are different, we refer to the 'client port' and the
  849. 'federation port'. See <a href="https://matrix.org/docs/spec/server_server/latest#resolving-server-names">the Matrix
  850. specification</a>
  851. for more details of the algorithm used for federation connections, and
  852. <a href="delegate.html">Delegation</a> for instructions on setting up delegation.</p>
  853. <p><strong>NOTE</strong>: Your reverse proxy must not <code>canonicalise</code> or <code>normalise</code>
  854. the requested URI in any way (for example, by decoding <code>%xx</code> escapes).
  855. Beware that Apache <em>will</em> canonicalise URIs unless you specify
  856. <code>nocanon</code>.</p>
  857. <p>Let's assume that we expect clients to connect to our server at
  858. <code>https://matrix.example.com</code>, and other servers to connect at
  859. <code>https://example.com:8448</code>. The following sections detail the configuration of
  860. the reverse proxy and the homeserver.</p>
  861. <h2 id="homeserver-configuration"><a class="header" href="#homeserver-configuration">Homeserver Configuration</a></h2>
  862. <p>The HTTP configuration will need to be updated for Synapse to correctly record
  863. client IP addresses and generate redirect URLs while behind a reverse proxy. </p>
  864. <p>In <code>homeserver.yaml</code> set <code>x_forwarded: true</code> in the port 8008 section and
  865. consider setting <code>bind_addresses: ['127.0.0.1']</code> so that the server only
  866. listens to traffic on localhost. (Do not change <code>bind_addresses</code> to <code>127.0.0.1</code>
  867. when using a containerized Synapse, as that will prevent it from responding
  868. to proxied traffic.)</p>
  869. <h2 id="reverse-proxy-configuration-examples"><a class="header" href="#reverse-proxy-configuration-examples">Reverse-proxy configuration examples</a></h2>
  870. <p><strong>NOTE</strong>: You only need one of these.</p>
  871. <h3 id="nginx"><a class="header" href="#nginx">nginx</a></h3>
  872. <pre><code class="language-nginx">server {
  873. listen 443 ssl http2;
  874. listen [::]:443 ssl http2;
  875. # For the federation port
  876. listen 8448 ssl http2 default_server;
  877. listen [::]:8448 ssl http2 default_server;
  878. server_name matrix.example.com;
  879. location ~ ^(/_matrix|/_synapse/client) {
  880. # note: do not add a path (even a single /) after the port in `proxy_pass`,
  881. # otherwise nginx will canonicalise the URI and cause signature verification
  882. # errors.
  883. proxy_pass http://localhost:8008;
  884. proxy_set_header X-Forwarded-For $remote_addr;
  885. proxy_set_header X-Forwarded-Proto $scheme;
  886. proxy_set_header Host $host;
  887. # Nginx by default only allows file uploads up to 1M in size
  888. # Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
  889. client_max_body_size 50M;
  890. }
  891. }
  892. </code></pre>
  893. <h3 id="caddy-v1"><a class="header" href="#caddy-v1">Caddy v1</a></h3>
  894. <pre><code>matrix.example.com {
  895. proxy /_matrix http://localhost:8008 {
  896. transparent
  897. }
  898. proxy /_synapse/client http://localhost:8008 {
  899. transparent
  900. }
  901. }
  902. example.com:8448 {
  903. proxy / http://localhost:8008 {
  904. transparent
  905. }
  906. }
  907. </code></pre>
  908. <h3 id="caddy-v2"><a class="header" href="#caddy-v2">Caddy v2</a></h3>
  909. <pre><code>matrix.example.com {
  910. reverse_proxy /_matrix/* http://localhost:8008
  911. reverse_proxy /_synapse/client/* http://localhost:8008
  912. }
  913. example.com:8448 {
  914. reverse_proxy http://localhost:8008
  915. }
  916. </code></pre>
  917. <p><a href="delegate.html">Delegation</a> example:</p>
  918. <pre><code>(matrix-well-known-header) {
  919. # Headers
  920. header Access-Control-Allow-Origin &quot;*&quot;
  921. header Access-Control-Allow-Methods &quot;GET, POST, PUT, DELETE, OPTIONS&quot;
  922. header Access-Control-Allow-Headers &quot;Origin, X-Requested-With, Content-Type, Accept, Authorization&quot;
  923. header Content-Type &quot;application/json&quot;
  924. }
  925. example.com {
  926. handle /.well-known/matrix/server {
  927. import matrix-well-known-header
  928. respond `{&quot;m.server&quot;:&quot;matrix.example.com:443&quot;}`
  929. }
  930. handle /.well-known/matrix/client {
  931. import matrix-well-known-header
  932. 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;}}`
  933. }
  934. }
  935. matrix.example.com {
  936. reverse_proxy /_matrix/* http://localhost:8008
  937. reverse_proxy /_synapse/client/* http://localhost:8008
  938. }
  939. </code></pre>
  940. <h3 id="apache"><a class="header" href="#apache">Apache</a></h3>
  941. <pre><code class="language-apache">&lt;VirtualHost *:443&gt;
  942. SSLEngine on
  943. ServerName matrix.example.com
  944. RequestHeader set &quot;X-Forwarded-Proto&quot; expr=%{REQUEST_SCHEME}
  945. AllowEncodedSlashes NoDecode
  946. ProxyPreserveHost on
  947. ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
  948. ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
  949. ProxyPass /_synapse/client http://127.0.0.1:8008/_synapse/client nocanon
  950. ProxyPassReverse /_synapse/client http://127.0.0.1:8008/_synapse/client
  951. &lt;/VirtualHost&gt;
  952. &lt;VirtualHost *:8448&gt;
  953. SSLEngine on
  954. ServerName example.com
  955. RequestHeader set &quot;X-Forwarded-Proto&quot; expr=%{REQUEST_SCHEME}
  956. AllowEncodedSlashes NoDecode
  957. ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
  958. ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
  959. &lt;/VirtualHost&gt;
  960. </code></pre>
  961. <p><strong>NOTE</strong>: ensure the <code>nocanon</code> options are included.</p>
  962. <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>
  963. <pre><code class="language-apache">&lt;IfModule security2_module&gt;
  964. SecRuleEngine off
  965. &lt;/IfModule&gt;
  966. </code></pre>
  967. <p><strong>NOTE 3</strong>: Missing <code>ProxyPreserveHost on</code> can lead to a redirect loop.</p>
  968. <h3 id="haproxy"><a class="header" href="#haproxy">HAProxy</a></h3>
  969. <pre><code>frontend https
  970. bind *:443,[::]:443 ssl crt /etc/ssl/haproxy/ strict-sni alpn h2,http/1.1
  971. http-request set-header X-Forwarded-Proto https if { ssl_fc }
  972. http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  973. http-request set-header X-Forwarded-For %[src]
  974. # Matrix client traffic
  975. acl matrix-host hdr(host) -i matrix.example.com matrix.example.com:443
  976. acl matrix-path path_beg /_matrix
  977. acl matrix-path path_beg /_synapse/client
  978. use_backend matrix if matrix-host matrix-path
  979. frontend matrix-federation
  980. bind *:8448,[::]:8448 ssl crt /etc/ssl/haproxy/synapse.pem alpn h2,http/1.1
  981. http-request set-header X-Forwarded-Proto https if { ssl_fc }
  982. http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  983. http-request set-header X-Forwarded-For %[src]
  984. default_backend matrix
  985. backend matrix
  986. server matrix 127.0.0.1:8008
  987. </code></pre>
  988. <p><a href="delegate.html">Delegation</a> example:</p>
  989. <pre><code>frontend https
  990. acl matrix-well-known-client-path path /.well-known/matrix/client
  991. acl matrix-well-known-server-path path /.well-known/matrix/server
  992. use_backend matrix-well-known-client if matrix-well-known-client-path
  993. use_backend matrix-well-known-server if matrix-well-known-server-path
  994. backend matrix-well-known-client
  995. http-after-response set-header Access-Control-Allow-Origin &quot;*&quot;
  996. http-after-response set-header Access-Control-Allow-Methods &quot;GET, POST, PUT, DELETE, OPTIONS&quot;
  997. http-after-response set-header Access-Control-Allow-Headers &quot;Origin, X-Requested-With, Content-Type, Accept, Authorization&quot;
  998. http-request return status 200 content-type application/json string '{&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;}}'
  999. backend matrix-well-known-server
  1000. http-after-response set-header Access-Control-Allow-Origin &quot;*&quot;
  1001. http-after-response set-header Access-Control-Allow-Methods &quot;GET, POST, PUT, DELETE, OPTIONS&quot;
  1002. http-after-response set-header Access-Control-Allow-Headers &quot;Origin, X-Requested-With, Content-Type, Accept, Authorization&quot;
  1003. http-request return status 200 content-type application/json string '{&quot;m.server&quot;:&quot;matrix.example.com:443&quot;}'
  1004. </code></pre>
  1005. <h3 id="relayd"><a class="header" href="#relayd">Relayd</a></h3>
  1006. <pre><code>table &lt;webserver&gt; { 127.0.0.1 }
  1007. table &lt;matrixserver&gt; { 127.0.0.1 }
  1008. http protocol &quot;https&quot; {
  1009. tls { no tlsv1.0, ciphers &quot;HIGH&quot; }
  1010. tls keypair &quot;example.com&quot;
  1011. match header set &quot;X-Forwarded-For&quot; value &quot;$REMOTE_ADDR&quot;
  1012. match header set &quot;X-Forwarded-Proto&quot; value &quot;https&quot;
  1013. # set CORS header for .well-known/matrix/server, .well-known/matrix/client
  1014. # httpd does not support setting headers, so do it here
  1015. match request path &quot;/.well-known/matrix/*&quot; tag &quot;matrix-cors&quot;
  1016. match response tagged &quot;matrix-cors&quot; header set &quot;Access-Control-Allow-Origin&quot; value &quot;*&quot;
  1017. pass quick path &quot;/_matrix/*&quot; forward to &lt;matrixserver&gt;
  1018. pass quick path &quot;/_synapse/client/*&quot; forward to &lt;matrixserver&gt;
  1019. # pass on non-matrix traffic to webserver
  1020. pass forward to &lt;webserver&gt;
  1021. }
  1022. relay &quot;https_traffic&quot; {
  1023. listen on egress port 443 tls
  1024. protocol &quot;https&quot;
  1025. forward to &lt;matrixserver&gt; port 8008 check tcp
  1026. forward to &lt;webserver&gt; port 8080 check tcp
  1027. }
  1028. http protocol &quot;matrix&quot; {
  1029. tls { no tlsv1.0, ciphers &quot;HIGH&quot; }
  1030. tls keypair &quot;example.com&quot;
  1031. block
  1032. pass quick path &quot;/_matrix/*&quot; forward to &lt;matrixserver&gt;
  1033. pass quick path &quot;/_synapse/client/*&quot; forward to &lt;matrixserver&gt;
  1034. }
  1035. relay &quot;matrix_federation&quot; {
  1036. listen on egress port 8448 tls
  1037. protocol &quot;matrix&quot;
  1038. forward to &lt;matrixserver&gt; port 8008 check tcp
  1039. }
  1040. </code></pre>
  1041. <h2 id="health-check-endpoint"><a class="header" href="#health-check-endpoint">Health check endpoint</a></h2>
  1042. <p>Synapse exposes a health check endpoint for use by reverse proxies.
  1043. Each configured HTTP listener has a <code>/health</code> endpoint which always returns
  1044. 200 OK (and doesn't get logged).</p>
  1045. <h2 id="synapse-administration-endpoints"><a class="header" href="#synapse-administration-endpoints">Synapse administration endpoints</a></h2>
  1046. <p>Endpoints for administering your Synapse instance are placed under
  1047. <code>/_synapse/admin</code>. These require authentication through an access token of an
  1048. admin user. However as access to these endpoints grants the caller a lot of power,
  1049. we do not recommend exposing them to the public internet without good reason.</p>
  1050. <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>
  1051. <p>You can use Synapse with a forward or outbound proxy. An example of when
  1052. this is necessary is in corporate environments behind a DMZ (demilitarized zone).
  1053. Synapse supports routing outbound HTTP(S) requests via a proxy. Only HTTP(S)
  1054. proxy is supported, not SOCKS proxy or anything else.</p>
  1055. <h2 id="configure"><a class="header" href="#configure">Configure</a></h2>
  1056. <p>The <code>http_proxy</code>, <code>https_proxy</code>, <code>no_proxy</code> environment variables are used to
  1057. specify proxy settings. The environment variable is not case sensitive.</p>
  1058. <ul>
  1059. <li><code>http_proxy</code>: Proxy server to use for HTTP requests.</li>
  1060. <li><code>https_proxy</code>: Proxy server to use for HTTPS requests.</li>
  1061. <li><code>no_proxy</code>: Comma-separated list of hosts, IP addresses, or IP ranges in CIDR
  1062. format which should not use the proxy. Synapse will directly connect to these hosts.</li>
  1063. </ul>
  1064. <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>
  1065. <ul>
  1066. <li>
  1067. <p>Supported schemes are <code>http://</code> and <code>https://</code>. The default scheme is <code>http://</code>
  1068. for compatibility reasons; it is recommended to set a scheme. If scheme is set
  1069. to <code>https://</code> the connection uses TLS between Synapse and the proxy.</p>
  1070. <p><strong>NOTE</strong>: Synapse validates the certificates. If the certificate is not
  1071. valid, then the connection is dropped.</p>
  1072. </li>
  1073. <li>
  1074. <p>Default port if not given is <code>1080</code>.</p>
  1075. </li>
  1076. <li>
  1077. <p>Username and password are optional and will be used to authenticate against
  1078. the proxy.</p>
  1079. </li>
  1080. </ul>
  1081. <p><strong>Examples</strong></p>
  1082. <ul>
  1083. <li>HTTP_PROXY=http://USERNAME:PASSWORD@10.0.1.1:8080/</li>
  1084. <li>HTTPS_PROXY=http://USERNAME:PASSWORD@proxy.example.com:8080/</li>
  1085. <li>NO_PROXY=master.hostname.example.com,10.1.0.0/16,172.30.0.0/16</li>
  1086. </ul>
  1087. <p><strong>NOTE</strong>:
  1088. Synapse does not apply the IP blacklist to connections through the proxy (since
  1089. the DNS resolution is done by the proxy). It is expected that the proxy or firewall
  1090. will apply blacklisting of IP addresses.</p>
  1091. <h2 id="connection-types"><a class="header" href="#connection-types">Connection types</a></h2>
  1092. <p>The proxy will be <strong>used</strong> for:</p>
  1093. <ul>
  1094. <li>push</li>
  1095. <li>url previews</li>
  1096. <li>phone-home stats</li>
  1097. <li>recaptcha validation</li>
  1098. <li>CAS auth validation</li>
  1099. <li>OpenID Connect</li>
  1100. <li>Outbound federation</li>
  1101. <li>Federation (checking public key revocation)</li>
  1102. <li>Fetching public keys of other servers</li>
  1103. <li>Downloading remote media</li>
  1104. </ul>
  1105. <p>It will <strong>not be used</strong> for:</p>
  1106. <ul>
  1107. <li>Application Services</li>
  1108. <li>Identity servers</li>
  1109. <li>In worker configurations
  1110. <ul>
  1111. <li>connections between workers</li>
  1112. <li>connections from workers to Redis</li>
  1113. </ul>
  1114. </li>
  1115. </ul>
  1116. <h2 id="troubleshooting-1"><a class="header" href="#troubleshooting-1">Troubleshooting</a></h2>
  1117. <p>If a proxy server is used with TLS (HTTPS) and no connections are established,
  1118. it is most likely due to the proxy's certificates. To test this, the validation
  1119. in Synapse can be deactivated.</p>
  1120. <p><strong>NOTE</strong>: This has an impact on security and is for testing purposes only!</p>
  1121. <p>To deactivate the certificate validation, the following setting must be made in
  1122. <a href="setup/../usage/configuration/homeserver_sample_config.html">homserver.yaml</a>.</p>
  1123. <pre><code class="language-yaml">use_insecure_ssl_client_just_for_testing_do_not_use: true
  1124. </code></pre>
  1125. <div style="break-before: page; page-break-before: always;"></div><h1 id="overview-1"><a class="header" href="#overview-1">Overview</a></h1>
  1126. <p>This document explains how to enable VoIP relaying on your homeserver with
  1127. TURN.</p>
  1128. <p>The synapse Matrix homeserver supports integration with TURN server via the
  1129. <a href="https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00">TURN server REST API</a>. This
  1130. allows the homeserver to generate credentials that are valid for use on the
  1131. TURN server through the use of a secret shared between the homeserver and the
  1132. TURN server.</p>
  1133. <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>
  1134. <h2 id="requirements"><a class="header" href="#requirements">Requirements</a></h2>
  1135. <p>For TURN relaying with <code>coturn</code> to work, it must be hosted on a server/endpoint with a public IP.</p>
  1136. <p>Hosting TURN behind NAT requires port forwaring and for the NAT gateway to have a public IP.
  1137. However, even with appropriate configuration, NAT is known to cause issues and to often not work.</p>
  1138. <h2 id="coturn-setup"><a class="header" href="#coturn-setup"><code>coturn</code> setup</a></h2>
  1139. <h3 id="initial-installation"><a class="header" href="#initial-installation">Initial installation</a></h3>
  1140. <p>The TURN daemon <code>coturn</code> is available from a variety of sources such as native package managers, or installation from source.</p>
  1141. <h4 id="debian-installation"><a class="header" href="#debian-installation">Debian installation</a></h4>
  1142. <p>Just install the debian package:</p>
  1143. <pre><code class="language-sh">apt install coturn
  1144. </code></pre>
  1145. <p>This will install and start a systemd service called <code>coturn</code>.</p>
  1146. <h4 id="source-installation"><a class="header" href="#source-installation">Source installation</a></h4>
  1147. <ol>
  1148. <li>
  1149. <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>
  1150. </li>
  1151. <li>
  1152. <p>Configure it:</p>
  1153. <pre><code class="language-sh">./configure
  1154. </code></pre>
  1155. <p>You may need to install <code>libevent2</code>: if so, you should do so in
  1156. the way recommended by your operating system. You can ignore
  1157. warnings about lack of database support: a database is unnecessary
  1158. for this purpose.</p>
  1159. </li>
  1160. <li>
  1161. <p>Build and install it:</p>
  1162. <pre><code class="language-sh">make
  1163. make install
  1164. </code></pre>
  1165. </li>
  1166. </ol>
  1167. <h3 id="configuration"><a class="header" href="#configuration">Configuration</a></h3>
  1168. <ol>
  1169. <li>
  1170. <p>Create or edit the config file in <code>/etc/turnserver.conf</code>. The relevant
  1171. lines, with example values, are:</p>
  1172. <pre><code>use-auth-secret
  1173. static-auth-secret=[your secret key here]
  1174. realm=turn.myserver.org
  1175. </code></pre>
  1176. <p>See <code>turnserver.conf</code> for explanations of the options. One way to generate
  1177. the <code>static-auth-secret</code> is with <code>pwgen</code>:</p>
  1178. <pre><code class="language-sh">pwgen -s 64 1
  1179. </code></pre>
  1180. <p>A <code>realm</code> must be specified, but its value is somewhat arbitrary. (It is
  1181. sent to clients as part of the authentication flow.) It is conventional to
  1182. set it to be your server name.</p>
  1183. </li>
  1184. <li>
  1185. <p>You will most likely want to configure coturn to write logs somewhere. The
  1186. easiest way is normally to send them to the syslog:</p>
  1187. <pre><code class="language-sh">syslog
  1188. </code></pre>
  1189. <p>(in which case, the logs will be available via <code>journalctl -u coturn</code> on a
  1190. systemd system). Alternatively, coturn can be configured to write to a
  1191. logfile - check the example config file supplied with coturn.</p>
  1192. </li>
  1193. <li>
  1194. <p>Consider your security settings. TURN lets users request a relay which will
  1195. connect to arbitrary IP addresses and ports. The following configuration is
  1196. suggested as a minimum starting point:</p>
  1197. <pre><code># VoIP traffic is all UDP. There is no reason to let users connect to arbitrary TCP endpoints via the relay.
  1198. no-tcp-relay
  1199. # don't let the relay ever try to connect to private IP address ranges within your network (if any)
  1200. # given the turn server is likely behind your firewall, remember to include any privileged public IPs too.
  1201. denied-peer-ip=10.0.0.0-10.255.255.255
  1202. denied-peer-ip=192.168.0.0-192.168.255.255
  1203. denied-peer-ip=172.16.0.0-172.31.255.255
  1204. # recommended additional local peers to block, to mitigate external access to internal services.
  1205. # https://www.rtcsec.com/article/slack-webrtc-turn-compromise-and-bug-bounty/#how-to-fix-an-open-turn-relay-to-address-this-vulnerability
  1206. no-multicast-peers
  1207. denied-peer-ip=0.0.0.0-0.255.255.255
  1208. denied-peer-ip=100.64.0.0-100.127.255.255
  1209. denied-peer-ip=127.0.0.0-127.255.255.255
  1210. denied-peer-ip=169.254.0.0-169.254.255.255
  1211. denied-peer-ip=192.0.0.0-192.0.0.255
  1212. denied-peer-ip=192.0.2.0-192.0.2.255
  1213. denied-peer-ip=192.88.99.0-192.88.99.255
  1214. denied-peer-ip=198.18.0.0-198.19.255.255
  1215. denied-peer-ip=198.51.100.0-198.51.100.255
  1216. denied-peer-ip=203.0.113.0-203.0.113.255
  1217. denied-peer-ip=240.0.0.0-255.255.255.255
  1218. # special case the turn server itself so that client-&gt;TURN-&gt;TURN-&gt;client flows work
  1219. # this should be one of the turn server's listening IPs
  1220. allowed-peer-ip=10.0.0.1
  1221. # consider whether you want to limit the quota of relayed streams per user (or total) to avoid risk of DoS.
  1222. user-quota=12 # 4 streams per video call, so 12 streams = 3 simultaneous relayed calls per user.
  1223. total-quota=1200
  1224. </code></pre>
  1225. </li>
  1226. <li>
  1227. <p>Also consider supporting TLS/DTLS. To do this, add the following settings
  1228. to <code>turnserver.conf</code>:</p>
  1229. <pre><code># TLS certificates, including intermediate certs.
  1230. # For Let's Encrypt certificates, use `fullchain.pem` here.
  1231. cert=/path/to/fullchain.pem
  1232. # TLS private key file
  1233. pkey=/path/to/privkey.pem
  1234. # Ensure the configuration lines that disable TLS/DTLS are commented-out or removed
  1235. #no-tls
  1236. #no-dtls
  1237. </code></pre>
  1238. <p>In this case, replace the <code>turn:</code> schemes in the <code>turn_uris</code> settings below
  1239. with <code>turns:</code>.</p>
  1240. <p>We recommend that you only try to set up TLS/DTLS once you have set up a
  1241. basic installation and got it working.</p>
  1242. <p>NB: If your TLS certificate was provided by Let's Encrypt, TLS/DTLS will
  1243. not work with any Matrix client that uses Chromium's WebRTC library. This
  1244. currently includes Element Android &amp; iOS; for more details, see their
  1245. <a href="https://github.com/vector-im/element-android/issues/1533">respective</a>
  1246. <a href="https://github.com/vector-im/element-ios/issues/2712">issues</a> as well as the underlying
  1247. <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=11710">WebRTC issue</a>.
  1248. Consider using a ZeroSSL certificate for your TURN server as a working alternative.</p>
  1249. </li>
  1250. <li>
  1251. <p>Ensure your firewall allows traffic into the TURN server on the ports
  1252. you've configured it to listen on (By default: 3478 and 5349 for TURN
  1253. traffic (remember to allow both TCP and UDP traffic), and ports 49152-65535
  1254. for the UDP relay.)</p>
  1255. </li>
  1256. <li>
  1257. <p>If your TURN server is behind NAT, the NAT gateway must have an external,
  1258. publicly-reachable IP address. You must configure coturn to advertise that
  1259. address to connecting clients:</p>
  1260. <pre><code>external-ip=EXTERNAL_NAT_IPv4_ADDRESS
  1261. </code></pre>
  1262. <p>You may optionally limit the TURN server to listen only on the local
  1263. address that is mapped by NAT to the external address:</p>
  1264. <pre><code>listening-ip=INTERNAL_TURNSERVER_IPv4_ADDRESS
  1265. </code></pre>
  1266. <p>If your NAT gateway is reachable over both IPv4 and IPv6, you may
  1267. configure coturn to advertise each available address:</p>
  1268. <pre><code>external-ip=EXTERNAL_NAT_IPv4_ADDRESS
  1269. external-ip=EXTERNAL_NAT_IPv6_ADDRESS
  1270. </code></pre>
  1271. <p>When advertising an external IPv6 address, ensure that the firewall and
  1272. network settings of the system running your TURN server are configured to
  1273. accept IPv6 traffic, and that the TURN server is listening on the local
  1274. IPv6 address that is mapped by NAT to the external IPv6 address.</p>
  1275. </li>
  1276. <li>
  1277. <p>(Re)start the turn server:</p>
  1278. <ul>
  1279. <li>
  1280. <p>If you used the Debian package (or have set up a systemd unit yourself):</p>
  1281. <pre><code class="language-sh">systemctl restart coturn
  1282. </code></pre>
  1283. </li>
  1284. <li>
  1285. <p>If you installed from source:</p>
  1286. <pre><code class="language-sh">bin/turnserver -o
  1287. </code></pre>
  1288. </li>
  1289. </ul>
  1290. </li>
  1291. </ol>
  1292. <h2 id="synapse-setup"><a class="header" href="#synapse-setup">Synapse setup</a></h2>
  1293. <p>Your homeserver configuration file needs the following extra keys:</p>
  1294. <ol>
  1295. <li>&quot;<code>turn_uris</code>&quot;: This needs to be a yaml list of public-facing URIs
  1296. for your TURN server to be given out to your clients. Add separate
  1297. entries for each transport your TURN server supports.</li>
  1298. <li>&quot;<code>turn_shared_secret</code>&quot;: This is the secret shared between your
  1299. homeserver and your TURN server, so you should set it to the same
  1300. string you used in turnserver.conf.</li>
  1301. <li>&quot;<code>turn_user_lifetime</code>&quot;: This is the amount of time credentials
  1302. generated by your homeserver are valid for (in milliseconds).
  1303. Shorter times offer less potential for abuse at the expense of
  1304. increased traffic between web clients and your homeserver to
  1305. refresh credentials. The TURN REST API specification recommends
  1306. one day (86400000).</li>
  1307. <li>&quot;<code>turn_allow_guests</code>&quot;: Whether to allow guest users to use the
  1308. TURN server. This is enabled by default, as otherwise VoIP will
  1309. not work reliably for guests. However, it does introduce a
  1310. security risk as it lets guests connect to arbitrary endpoints
  1311. without having gone through a CAPTCHA or similar to register a
  1312. real account.</li>
  1313. </ol>
  1314. <p>As an example, here is the relevant section of the config file for <code>matrix.org</code>. The
  1315. <code>turn_uris</code> are appropriate for TURN servers listening on the default ports, with no TLS.</p>
  1316. <pre><code>turn_uris: [ &quot;turn:turn.matrix.org?transport=udp&quot;, &quot;turn:turn.matrix.org?transport=tcp&quot; ]
  1317. turn_shared_secret: &quot;n0t4ctuAllymatr1Xd0TorgSshar3d5ecret4obvIousreAsons&quot;
  1318. turn_user_lifetime: 86400000
  1319. turn_allow_guests: True
  1320. </code></pre>
  1321. <p>After updating the homeserver configuration, you must restart synapse:</p>
  1322. <ul>
  1323. <li>If you use synctl:
  1324. <pre><code class="language-sh"># Depending on how Synapse is installed, synctl may already be on
  1325. # your PATH. If not, you may need to activate a virtual environment.
  1326. synctl restart
  1327. </code></pre>
  1328. </li>
  1329. <li>If you use systemd:
  1330. <pre><code class="language-sh">systemctl restart matrix-synapse.service
  1331. </code></pre>
  1332. </li>
  1333. </ul>
  1334. <p>... and then reload any clients (or wait an hour for them to refresh their
  1335. settings).</p>
  1336. <h2 id="troubleshooting-2"><a class="header" href="#troubleshooting-2">Troubleshooting</a></h2>
  1337. <p>The normal symptoms of a misconfigured TURN server are that calls between
  1338. devices on different networks ring, but get stuck at &quot;call
  1339. connecting&quot;. Unfortunately, troubleshooting this can be tricky.</p>
  1340. <p>Here are a few things to try:</p>
  1341. <ul>
  1342. <li>
  1343. <p>Check that you have opened your firewall to allow TCP and UDP traffic to the
  1344. TURN ports (normally 3478 and 5349).</p>
  1345. </li>
  1346. <li>
  1347. <p>Check that you have opened your firewall to allow UDP traffic to the UDP
  1348. relay ports (49152-65535 by default).</p>
  1349. </li>
  1350. <li>
  1351. <p>Try disabling <code>coturn</code>'s TLS/DTLS listeners and enable only its (unencrypted)
  1352. TCP/UDP listeners. (This will only leave signaling traffic unencrypted;
  1353. voice &amp; video WebRTC traffic is always encrypted.)</p>
  1354. </li>
  1355. <li>
  1356. <p>Some WebRTC implementations (notably, that of Google Chrome) appear to get
  1357. confused by TURN servers which are reachable over IPv6 (this appears to be
  1358. an unexpected side-effect of its handling of multiple IP addresses as
  1359. defined by
  1360. <a href="https://tools.ietf.org/html/draft-ietf-rtcweb-ip-handling-12"><code>draft-ietf-rtcweb-ip-handling</code></a>).</p>
  1361. <p>Try removing any AAAA records for your TURN server, so that it is only
  1362. reachable over IPv4.</p>
  1363. </li>
  1364. <li>
  1365. <p>If your TURN server is behind NAT:</p>
  1366. <ul>
  1367. <li>
  1368. <p>double-check that your NAT gateway is correctly forwarding all TURN
  1369. ports (normally 3478 &amp; 5349 for TCP &amp; UDP TURN traffic, and 49152-65535 for the UDP
  1370. relay) to the NAT-internal address of your TURN server. If advertising
  1371. both IPv4 and IPv6 external addresses via the <code>external-ip</code> option, ensure
  1372. that the NAT is forwarding both IPv4 and IPv6 traffic to the IPv4 and IPv6
  1373. internal addresses of your TURN server. When in doubt, remove AAAA records
  1374. for your TURN server and specify only an IPv4 address as your <code>external-ip</code>.</p>
  1375. </li>
  1376. <li>
  1377. <p>ensure that your TURN server uses the NAT gateway as its default route.</p>
  1378. </li>
  1379. </ul>
  1380. </li>
  1381. <li>
  1382. <p>Enable more verbose logging in coturn via the <code>verbose</code> setting:</p>
  1383. <pre><code>verbose
  1384. </code></pre>
  1385. <p>... and then see if there are any clues in its logs.</p>
  1386. </li>
  1387. <li>
  1388. <p>If you are using a browser-based client under Chrome, check
  1389. <code>chrome://webrtc-internals/</code> for insights into the internals of the
  1390. negotiation. On Firefox, check the &quot;Connection Log&quot; on <code>about:webrtc</code>.</p>
  1391. <p>(Understanding the output is beyond the scope of this document!)</p>
  1392. </li>
  1393. <li>
  1394. <p>You can test your Matrix homeserver TURN setup with <a href="https://test.voip.librepush.net/">https://test.voip.librepush.net/</a>.
  1395. Note that this test is not fully reliable yet, so don't be discouraged if
  1396. the test fails.
  1397. <a href="https://github.com/matrix-org/voip-tester">Here</a> is the github repo of the
  1398. source of the tester, where you can file bug reports.</p>
  1399. </li>
  1400. <li>
  1401. <p>There is a WebRTC test tool at
  1402. <a href="https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/">https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/</a>. To
  1403. use it, you will need a username/password for your TURN server. You can
  1404. either:</p>
  1405. <ul>
  1406. <li>
  1407. <p>look for the <code>GET /_matrix/client/r0/voip/turnServer</code> request made by a
  1408. matrix client to your homeserver in your browser's network inspector. In
  1409. the response you should see <code>username</code> and <code>password</code>. Or:</p>
  1410. </li>
  1411. <li>
  1412. <p>Use the following shell commands:</p>
  1413. <pre><code class="language-sh">secret=staticAuthSecretHere
  1414. u=$((`date +%s` + 3600)):test
  1415. p=$(echo -n $u | openssl dgst -hmac $secret -sha1 -binary | base64)
  1416. echo -e &quot;username: $u\npassword: $p&quot;
  1417. </code></pre>
  1418. <p>Or:</p>
  1419. </li>
  1420. <li>
  1421. <p>Temporarily configure coturn to accept a static username/password. To do
  1422. this, comment out <code>use-auth-secret</code> and <code>static-auth-secret</code> and add the
  1423. following:</p>
  1424. <pre><code>lt-cred-mech
  1425. user=username:password
  1426. </code></pre>
  1427. <p><strong>Note</strong>: these settings will not take effect unless <code>use-auth-secret</code>
  1428. and <code>static-auth-secret</code> are disabled.</p>
  1429. <p>Restart coturn after changing the configuration file.</p>
  1430. <p>Remember to restore the original settings to go back to testing with
  1431. Matrix clients!</p>
  1432. </li>
  1433. </ul>
  1434. <p>If the TURN server is working correctly, you should see at least one <code>relay</code>
  1435. entry in the results.</p>
  1436. </li>
  1437. </ul>
  1438. <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>
  1439. <p>In the following documentation, we use the term <code>server_name</code> to refer to that setting
  1440. in your homeserver configuration file. It appears at the ends of user ids, and tells
  1441. other homeservers where they can find your server.</p>
  1442. <p>By default, other homeservers will expect to be able to reach yours via
  1443. your <code>server_name</code>, on port 8448. For example, if you set your <code>server_name</code>
  1444. to <code>example.com</code> (so that your user names look like <code>@user:example.com</code>),
  1445. other servers will try to connect to yours at <code>https://example.com:8448/</code>.</p>
  1446. <p>Delegation is a Matrix feature allowing a homeserver admin to retain a
  1447. <code>server_name</code> of <code>example.com</code> so that user IDs, room aliases, etc continue
  1448. to look like <code>*:example.com</code>, whilst having federation traffic routed
  1449. to a different server and/or port (e.g. <code>synapse.example.com:443</code>).</p>
  1450. <h2 id="well-known-delegation"><a class="header" href="#well-known-delegation">.well-known delegation</a></h2>
  1451. <p>To use this method, you need to be able to configure the server at
  1452. <code>https://&lt;server_name&gt;</code> to serve a file at
  1453. <code>https://&lt;server_name&gt;/.well-known/matrix/server</code>. There are two ways to do this, shown below.</p>
  1454. <p>Note that the <code>.well-known</code> file is hosted on the default port for <code>https</code> (port 443).</p>
  1455. <h3 id="external-server"><a class="header" href="#external-server">External server</a></h3>
  1456. <p>For maximum flexibility, you need to configure an external server such as nginx, Apache
  1457. or HAProxy to serve the <code>https://&lt;server_name&gt;/.well-known/matrix/server</code> file. Setting
  1458. up such a server is out of the scope of this documentation, but note that it is often
  1459. possible to configure your <a href="reverse_proxy.html">reverse proxy</a> for this.</p>
  1460. <p>The URL <code>https://&lt;server_name&gt;/.well-known/matrix/server</code> should be configured
  1461. return a JSON structure containing the key <code>m.server</code> like this:</p>
  1462. <pre><code class="language-json">{
  1463. &quot;m.server&quot;: &quot;&lt;synapse.server.name&gt;[:&lt;yourport&gt;]&quot;
  1464. }
  1465. </code></pre>
  1466. <p>In our example (where we want federation traffic to be routed to
  1467. <code>https://synapse.example.com</code>, on port 443), this would mean that
  1468. <code>https://example.com/.well-known/matrix/server</code> should return:</p>
  1469. <pre><code class="language-json">{
  1470. &quot;m.server&quot;: &quot;synapse.example.com:443&quot;
  1471. }
  1472. </code></pre>
  1473. <p>Note, specifying a port is optional. If no port is specified, then it defaults
  1474. to 8448.</p>
  1475. <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>
  1476. <p>If you are able to set up your domain so that <code>https://&lt;server_name&gt;</code> is routed to
  1477. Synapse (i.e., the only change needed is to direct federation traffic to port 443
  1478. instead of port 8448), then it is possible to configure Synapse to serve a suitable
  1479. <code>.well-known/matrix/server</code> file. To do so, add the following to your <code>homeserver.yaml</code>
  1480. file:</p>
  1481. <pre><code class="language-yaml">serve_server_wellknown: true
  1482. </code></pre>
  1483. <p><strong>Note</strong>: this <em>only</em> works if <code>https://&lt;server_name&gt;</code> is routed to Synapse, so is
  1484. generally not suitable if Synapse is hosted at a subdomain such as
  1485. <code>https://synapse.example.com</code>.</p>
  1486. <h2 id="srv-dns-record-delegation"><a class="header" href="#srv-dns-record-delegation">SRV DNS record delegation</a></h2>
  1487. <p>It is also possible to do delegation using a SRV DNS record. However, that is generally
  1488. not recommended, as it can be difficult to configure the TLS certificates correctly in
  1489. this case, and it offers little advantage over <code>.well-known</code> delegation.</p>
  1490. <p>However, if you really need it, you can find some documentation on what such a
  1491. 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
  1492. specification</a>.</p>
  1493. <h2 id="delegation-faq"><a class="header" href="#delegation-faq">Delegation FAQ</a></h2>
  1494. <h3 id="when-do-i-need-delegation"><a class="header" href="#when-do-i-need-delegation">When do I need delegation?</a></h3>
  1495. <p>If your homeserver's APIs are accessible on the default federation port (8448)
  1496. and the domain your <code>server_name</code> points to, you do not need any delegation.</p>
  1497. <p>For instance, if you registered <code>example.com</code> and pointed its DNS A record at a
  1498. fresh server, you could install Synapse on that host, giving it a <code>server_name</code>
  1499. of <code>example.com</code>, and once a reverse proxy has been set up to proxy all requests
  1500. sent to the port <code>8448</code> and serve TLS certificates for <code>example.com</code>, you
  1501. wouldn't need any delegation set up.</p>
  1502. <p><strong>However</strong>, if your homeserver's APIs aren't accessible on port 8448 and on the
  1503. domain <code>server_name</code> points to, you will need to let other servers know how to
  1504. find it using delegation.</p>
  1505. <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>
  1506. <p>Generally, using a reverse proxy for both the federation and client traffic is a good
  1507. idea, since it saves handling TLS traffic in Synapse. See
  1508. <a href="reverse_proxy.html">the reverse proxy documentation</a> for information on setting up a
  1509. reverse proxy.</p>
  1510. <div style="break-before: page; page-break-before: always;"></div><h1 id="upgrading-synapse"><a class="header" href="#upgrading-synapse">Upgrading Synapse</a></h1>
  1511. <p>Before upgrading check if any special steps are required to upgrade from
  1512. the version you currently have installed to the current version of
  1513. Synapse. The extra instructions that may be required are listed later in
  1514. this document.</p>
  1515. <ul>
  1516. <li>
  1517. <p>Check that your versions of Python and PostgreSQL are still
  1518. supported.</p>
  1519. <p>Synapse follows upstream lifecycles for <a href="https://endoflife.date/python">Python</a> and
  1520. <a href="https://endoflife.date/postgresql">PostgreSQL</a>, and removes support for versions
  1521. which are no longer maintained.</p>
  1522. <p>The website <a href="https://endoflife.date">https://endoflife.date</a> also offers convenient
  1523. summaries.</p>
  1524. </li>
  1525. <li>
  1526. <p>If Synapse was installed using <a href="setup/installation.html#prebuilt-packages">prebuilt
  1527. packages</a>, you will need to follow the
  1528. normal process for upgrading those packages.</p>
  1529. </li>
  1530. <li>
  1531. <p>If Synapse was installed using pip then upgrade to the latest
  1532. version by running:</p>
  1533. <pre><code class="language-bash">pip install --upgrade matrix-synapse
  1534. </code></pre>
  1535. </li>
  1536. <li>
  1537. <p>If Synapse was installed from source, then:</p>
  1538. <ol>
  1539. <li>
  1540. <p>Obtain the latest version of the source code. Git users can run
  1541. <code>git pull</code> to do this.</p>
  1542. </li>
  1543. <li>
  1544. <p>If you're running Synapse in a virtualenv, make sure to activate it before
  1545. upgrading. For example, if Synapse is installed in a virtualenv in <code>~/synapse/env</code> then
  1546. run:</p>
  1547. <pre><code class="language-bash">source ~/synapse/env/bin/activate
  1548. pip install --upgrade .
  1549. </code></pre>
  1550. <p>Include any relevant extras between square brackets, e.g. <code>pip install --upgrade &quot;.[postgres,oidc]&quot;</code>.</p>
  1551. </li>
  1552. <li>
  1553. <p>If you're using <code>poetry</code> to manage a Synapse installation, run:</p>
  1554. <pre><code class="language-bash">poetry install
  1555. </code></pre>
  1556. <p>Include any relevant extras with <code>--extras</code>, e.g. <code>poetry install --extras postgres --extras oidc</code>.
  1557. It's probably easiest to run <code>poetry install --extras all</code>.</p>
  1558. </li>
  1559. <li>
  1560. <p>Restart Synapse:</p>
  1561. <pre><code class="language-bash">synctl restart
  1562. </code></pre>
  1563. </li>
  1564. </ol>
  1565. </li>
  1566. </ul>
  1567. <p>To check whether your update was successful, you can check the running
  1568. server version with:</p>
  1569. <pre><code class="language-bash"># you may need to replace 'localhost:8008' if synapse is not configured
  1570. # to listen on port 8008.
  1571. curl http://localhost:8008/_synapse/admin/v1/server_version
  1572. </code></pre>
  1573. <h2 id="rolling-back-to-older-versions"><a class="header" href="#rolling-back-to-older-versions">Rolling back to older versions</a></h2>
  1574. <p>Rolling back to previous releases can be difficult, due to database
  1575. schema changes between releases. Where we have been able to test the
  1576. rollback process, this will be noted below.</p>
  1577. <p>In general, you will need to undo any changes made during the upgrade
  1578. process, for example:</p>
  1579. <ul>
  1580. <li>
  1581. <p>pip:</p>
  1582. <pre><code class="language-bash">source env/bin/activate
  1583. # replace `1.3.0` accordingly:
  1584. pip install matrix-synapse==1.3.0
  1585. </code></pre>
  1586. </li>
  1587. <li>
  1588. <p>Debian:</p>
  1589. <pre><code class="language-bash"># replace `1.3.0` and `stretch` accordingly:
  1590. wget https://packages.matrix.org/debian/pool/main/m/matrix-synapse-py3/matrix-synapse-py3_1.3.0+stretch1_amd64.deb
  1591. dpkg -i matrix-synapse-py3_1.3.0+stretch1_amd64.deb
  1592. </code></pre>
  1593. </li>
  1594. </ul>
  1595. <h1 id="upgrading-to-v1600"><a class="header" href="#upgrading-to-v1600">Upgrading to v1.60.0</a></h1>
  1596. <h2 id="adding-a-new-unique-index-to-state_group_edges-could-fail-if-your-database-is-corrupted"><a class="header" href="#adding-a-new-unique-index-to-state_group_edges-could-fail-if-your-database-is-corrupted">Adding a new unique index to <code>state_group_edges</code> could fail if your database is corrupted</a></h2>
  1597. <p>This release of Synapse will add a unique index to the <code>state_group_edges</code> table, in order
  1598. to prevent accidentally introducing duplicate information (for example, because a database
  1599. backup was restored multiple times).</p>
  1600. <p>Duplicate rows being present in this table could cause drastic performance problems; see
  1601. <a href="https://github.com/matrix-org/synapse/issues/11779">issue 11779</a> for more details.</p>
  1602. <p>If your Synapse database already has had duplicate rows introduced into this table,
  1603. this could fail, with either of these errors:</p>
  1604. <p><strong>On Postgres:</strong></p>
  1605. <pre><code>synapse.storage.background_updates - 623 - INFO - background_updates-0 - Adding index state_group_edges_unique_idx to state_group_edges
  1606. synapse.storage.background_updates - 282 - ERROR - background_updates-0 - Error doing update
  1607. ...
  1608. psycopg2.errors.UniqueViolation: could not create unique index &quot;state_group_edges_unique_idx&quot;
  1609. DETAIL: Key (state_group, prev_state_group)=(2, 1) is duplicated.
  1610. </code></pre>
  1611. <p>(The numbers may be different.)</p>
  1612. <p><strong>On SQLite:</strong></p>
  1613. <pre><code>synapse.storage.background_updates - 623 - INFO - background_updates-0 - Adding index state_group_edges_unique_idx to state_group_edges
  1614. synapse.storage.background_updates - 282 - ERROR - background_updates-0 - Error doing update
  1615. ...
  1616. sqlite3.IntegrityError: UNIQUE constraint failed: state_group_edges.state_group, state_group_edges.prev_state_group
  1617. </code></pre>
  1618. <details>
  1619. <summary><b>Expand this section for steps to resolve this problem</b></summary>
  1620. <h3 id="on-postgres"><a class="header" href="#on-postgres">On Postgres</a></h3>
  1621. <p>Connect to your database with <code>psql</code>.</p>
  1622. <pre><code class="language-sql">BEGIN;
  1623. DELETE FROM state_group_edges WHERE (ctid, state_group, prev_state_group) IN (
  1624. SELECT row_id, state_group, prev_state_group
  1625. FROM (
  1626. SELECT
  1627. ctid AS row_id,
  1628. MIN(ctid) OVER (PARTITION BY state_group, prev_state_group) AS min_row_id,
  1629. state_group,
  1630. prev_state_group
  1631. FROM state_group_edges
  1632. ) AS t1
  1633. WHERE row_id &lt;&gt; min_row_id
  1634. );
  1635. COMMIT;
  1636. </code></pre>
  1637. <h3 id="on-sqlite"><a class="header" href="#on-sqlite">On SQLite</a></h3>
  1638. <p>At the command-line, use <code>sqlite3 path/to/your-homeserver-database.db</code>:</p>
  1639. <pre><code class="language-sql">BEGIN;
  1640. DELETE FROM state_group_edges WHERE (rowid, state_group, prev_state_group) IN (
  1641. SELECT row_id, state_group, prev_state_group
  1642. FROM (
  1643. SELECT
  1644. rowid AS row_id,
  1645. MIN(rowid) OVER (PARTITION BY state_group, prev_state_group) AS min_row_id,
  1646. state_group,
  1647. prev_state_group
  1648. FROM state_group_edges
  1649. )
  1650. WHERE row_id &lt;&gt; min_row_id
  1651. );
  1652. COMMIT;
  1653. </code></pre>
  1654. <h3 id="for-more-details"><a class="header" href="#for-more-details">For more details</a></h3>
  1655. <p><a href="https://github.com/matrix-org/synapse/issues/11779#issuecomment-1131545970">This comment on issue 11779</a>
  1656. has queries that can be used to check a database for this problem in advance.</p>
  1657. </details>
  1658. <h2 id="new-signature-for-the-spam-checker-callback-check_event_for_spam"><a class="header" href="#new-signature-for-the-spam-checker-callback-check_event_for_spam">New signature for the spam checker callback <code>check_event_for_spam</code></a></h2>
  1659. <p>The previous signature has been deprecated.</p>
  1660. <p>Whereas <code>check_event_for_spam</code> callbacks used to return <code>Union[str, bool]</code>, they should now return <code>Union[&quot;synapse.module_api.NOT_SPAM&quot;, &quot;synapse.module_api.errors.Codes&quot;]</code>.</p>
  1661. <p>This is part of an ongoing refactoring of the SpamChecker API to make it less ambiguous and more powerful.</p>
  1662. <p>If your module implements <code>check_event_for_spam</code> as follows:</p>
  1663. <pre><code class="language-python">async def check_event_for_spam(event):
  1664. if ...:
  1665. # Event is spam
  1666. return True
  1667. # Event is not spam
  1668. return False
  1669. </code></pre>
  1670. <p>you should rewrite it as follows:</p>
  1671. <pre><code class="language-python">async def check_event_for_spam(event):
  1672. if ...:
  1673. # Event is spam, mark it as forbidden (you may use some more precise error
  1674. # code if it is useful).
  1675. return synapse.module_api.errors.Codes.FORBIDDEN
  1676. # Event is not spam, mark it as such.
  1677. return synapse.module_api.NOT_SPAM
  1678. </code></pre>
  1679. <h1 id="upgrading-to-v1590"><a class="header" href="#upgrading-to-v1590">Upgrading to v1.59.0</a></h1>
  1680. <h2 id="device-name-lookup-over-federation-has-been-disabled-by-default"><a class="header" href="#device-name-lookup-over-federation-has-been-disabled-by-default">Device name lookup over federation has been disabled by default</a></h2>
  1681. <p>The names of user devices are no longer visible to users on other homeservers by default.
  1682. Device IDs are unaffected, as these are necessary to facilitate end-to-end encryption.</p>
  1683. <p>To re-enable this functionality, set the
  1684. <a href="https://matrix-org.github.io/synapse/v1.59/usage/configuration/config_documentation.html#federation"><code>allow_device_name_lookup_over_federation</code></a>
  1685. homeserver config option to <code>true</code>.</p>
  1686. <h2 id="deprecation-of-the-synapseappappservice-and-synapseappuser_dir-worker-application-types"><a class="header" href="#deprecation-of-the-synapseappappservice-and-synapseappuser_dir-worker-application-types">Deprecation of the <code>synapse.app.appservice</code> and <code>synapse.app.user_dir</code> worker application types</a></h2>
  1687. <p>The <code>synapse.app.appservice</code> worker application type allowed you to configure a
  1688. single worker to use to notify application services of new events, as long
  1689. as this functionality was disabled on the main process with <code>notify_appservices: False</code>.
  1690. Further, the <code>synapse.app.user_dir</code> worker application type allowed you to configure
  1691. a single worker to be responsible for updating the user directory, as long as this
  1692. was disabled on the main process with <code>update_user_directory: False</code>.</p>
  1693. <p>To unify Synapse's worker types, the <code>synapse.app.appservice</code> worker application
  1694. type and the <code>notify_appservices</code> configuration option have been deprecated.
  1695. The <code>synapse.app.user_dir</code> worker application type and <code>update_user_directory</code>
  1696. configuration option have also been deprecated.</p>
  1697. <p>To get the same functionality as was provided by the deprecated options, it's now recommended that the <code>synapse.app.generic_worker</code>
  1698. worker application type is used and that the <code>notify_appservices_from_worker</code> and/or
  1699. <code>update_user_directory_from_worker</code> options are set to the name of a worker.</p>
  1700. <p>For the time being, the old options can be used alongside the new options to make
  1701. it easier to transition between the two configurations, however please note that:</p>
  1702. <ul>
  1703. <li>the options must not contradict each other (otherwise Synapse won't start); and</li>
  1704. <li>the <code>notify_appservices</code> and <code>update_user_directory</code> options will be removed in a future release of Synapse.</li>
  1705. </ul>
  1706. <p>Please see the <a href="workers.html#notifying-application-services"><em>Notifying Application Services</em></a> and
  1707. <a href="workers.html#updating-the-user-directory"><em>Updating the User Directory</em></a> sections of the worker
  1708. documentation for more information.</p>
  1709. <h1 id="upgrading-to-v1580"><a class="header" href="#upgrading-to-v1580">Upgrading to v1.58.0</a></h1>
  1710. <h2 id="groupscommunities-feature-has-been-disabled-by-default"><a class="header" href="#groupscommunities-feature-has-been-disabled-by-default">Groups/communities feature has been disabled by default</a></h2>
  1711. <p>The non-standard groups/communities feature in Synapse has been disabled by default
  1712. and will be removed in Synapse v1.61.0.</p>
  1713. <h1 id="upgrading-to-v1570"><a class="header" href="#upgrading-to-v1570">Upgrading to v1.57.0</a></h1>
  1714. <h2 id="changes-to-database-schema-for-application-services"><a class="header" href="#changes-to-database-schema-for-application-services">Changes to database schema for application services</a></h2>
  1715. <p>Synapse v1.57.0 includes a <a href="https://github.com/matrix-org/synapse/pull/12209">change</a> to the
  1716. way transaction IDs are managed for application services. If your deployment uses a dedicated
  1717. worker for application service traffic, <strong>it must be stopped</strong> when the database is upgraded
  1718. (which normally happens when the main process is upgraded), to ensure the change is made safely
  1719. without any risk of reusing transaction IDs.</p>
  1720. <p>Deployments which do not use separate worker processes can be upgraded as normal. Similarly,
  1721. deployments where no application services are in use can be upgraded as normal.</p>
  1722. <details>
  1723. <summary><b>Recovering from an incorrect upgrade</b></summary>
  1724. <p>If the database schema is upgraded <em>without</em> stopping the worker responsible
  1725. for AS traffic, then the following error may be given when attempting to start
  1726. a Synapse worker or master process:</p>
  1727. <pre><code>**********************************************************************************
  1728. Error during initialisation:
  1729. Postgres sequence 'application_services_txn_id_seq' is inconsistent with associated
  1730. table 'application_services_txns'. This can happen if Synapse has been downgraded and
  1731. then upgraded again, or due to a bad migration.
  1732. To fix this error, shut down Synapse (including any and all workers)
  1733. and run the following SQL:
  1734. SELECT setval('application_services_txn_id_seq', (
  1735. SELECT GREATEST(MAX(txn_id), 0) FROM application_services_txns
  1736. ));
  1737. See docs/postgres.md for more information.
  1738. There may be more information in the logs.
  1739. **********************************************************************************
  1740. </code></pre>
  1741. <p>This error may also be seen if Synapse is <em>downgraded</em> to an earlier version,
  1742. and then upgraded again to v1.57.0 or later.</p>
  1743. <p>In either case:</p>
  1744. <ol>
  1745. <li>Ensure that the worker responsible for AS traffic is stopped.</li>
  1746. <li>Run the SQL command given in the error message via <code>psql</code>.</li>
  1747. </ol>
  1748. <p>Synapse should then start correctly.</p>
  1749. </details>
  1750. <h1 id="upgrading-to-v1560"><a class="header" href="#upgrading-to-v1560">Upgrading to v1.56.0</a></h1>
  1751. <h2 id="open-registration-without-verification-is-now-disabled-by-default"><a class="header" href="#open-registration-without-verification-is-now-disabled-by-default">Open registration without verification is now disabled by default</a></h2>
  1752. <p>Synapse will refuse to start if registration is enabled without email, captcha, or token-based verification unless the new config
  1753. flag <code>enable_registration_without_verification</code> is set to &quot;true&quot;.</p>
  1754. <h2 id="groupscommunities-feature-has-been-deprecated"><a class="header" href="#groupscommunities-feature-has-been-deprecated">Groups/communities feature has been deprecated</a></h2>
  1755. <p>The non-standard groups/communities feature in Synapse has been deprecated and will
  1756. be disabled by default in Synapse v1.58.0.</p>
  1757. <p>You can test disabling it by adding the following to your homeserver configuration:</p>
  1758. <pre><code class="language-yaml">experimental_features:
  1759. groups_enabled: false
  1760. </code></pre>
  1761. <h2 id="change-in-behaviour-for-postgresql-databases-with-unsafe-locale"><a class="header" href="#change-in-behaviour-for-postgresql-databases-with-unsafe-locale">Change in behaviour for PostgreSQL databases with unsafe locale</a></h2>
  1762. <p>Synapse now refuses to start when using PostgreSQL with non-<code>C</code> values for <code>COLLATE</code> and
  1763. <code>CTYPE</code> unless the config flag <code>allow_unsafe_locale</code>, found in the database section of
  1764. the configuration file, is set to <code>true</code>. See the <a href="https://matrix-org.github.io/synapse/latest/postgres.html#fixing-incorrect-collate-or-ctype">PostgreSQL documentation</a>
  1765. for more information and instructions on how to fix a database with incorrect values.</p>
  1766. <h1 id="upgrading-to-v1550"><a class="header" href="#upgrading-to-v1550">Upgrading to v1.55.0</a></h1>
  1767. <h2 id="synctl-script-has-been-moved"><a class="header" href="#synctl-script-has-been-moved"><code>synctl</code> script has been moved</a></h2>
  1768. <p>The <code>synctl</code> script
  1769. <a href="https://github.com/matrix-org/synapse/pull/12140">has been made</a> an
  1770. <a href="https://packaging.python.org/en/latest/specifications/entry-points/">entry point</a>
  1771. and no longer exists at the root of Synapse's source tree. If you wish to use
  1772. <code>synctl</code> to manage your homeserver, you should invoke <code>synctl</code> directly, e.g.
  1773. <code>synctl start</code> instead of <code>./synctl start</code> or <code>/path/to/synctl start</code>.</p>
  1774. <p>You will need to ensure <code>synctl</code> is on your <code>PATH</code>.</p>
  1775. <ul>
  1776. <li>This is automatically the case when using
  1777. <a href="https://packages.matrix.org/debian/">Debian packages</a> or
  1778. <a href="https://hub.docker.com/r/matrixdotorg/synapse">docker images</a>
  1779. provided by Matrix.org.</li>
  1780. <li>When installing from a wheel, sdist, or PyPI, a <code>synctl</code> executable is added
  1781. to your Python installation's <code>bin</code>. This should be on your <code>PATH</code>
  1782. automatically, though you might need to activate a virtual environment
  1783. depending on how you installed Synapse.</li>
  1784. </ul>
  1785. <h2 id="compatibility-dropped-for-mjolnir-131-and-earlier"><a class="header" href="#compatibility-dropped-for-mjolnir-131-and-earlier">Compatibility dropped for Mjolnir 1.3.1 and earlier</a></h2>
  1786. <p>Synapse v1.55.0 drops support for Mjolnir 1.3.1 and earlier.
  1787. If you use the Mjolnir module to moderate your homeserver,
  1788. please upgrade Mjolnir to version 1.3.2 or later before upgrading Synapse.</p>
  1789. <h1 id="upgrading-to-v1540"><a class="header" href="#upgrading-to-v1540">Upgrading to v1.54.0</a></h1>
  1790. <h2 id="legacy-structured-logging-configuration-removal"><a class="header" href="#legacy-structured-logging-configuration-removal">Legacy structured logging configuration removal</a></h2>
  1791. <p>This release removes support for the <code>structured: true</code> logging configuration
  1792. which was deprecated in Synapse v1.23.0. If your logging configuration contains
  1793. <code>structured: true</code> then it should be modified based on the
  1794. <a href="https://matrix-org.github.io/synapse/v1.56/structured_logging.html#upgrading-from-legacy-structured-logging-configuration">structured logging documentation</a>.</p>
  1795. <h1 id="upgrading-to-v1530"><a class="header" href="#upgrading-to-v1530">Upgrading to v1.53.0</a></h1>
  1796. <h2 id="dropping-support-for-webclient-listeners-and-non-https-web_client_location"><a class="header" href="#dropping-support-for-webclient-listeners-and-non-https-web_client_location">Dropping support for <code>webclient</code> listeners and non-HTTP(S) <code>web_client_location</code></a></h2>
  1797. <p>Per the deprecation notice in Synapse v1.51.0, listeners of type <code>webclient</code>
  1798. are no longer supported and configuring them is a now a configuration error.</p>
  1799. <p>Configuring a non-HTTP(S) <code>web_client_location</code> configuration is is now a
  1800. configuration error. Since the <code>webclient</code> listener is no longer supported, this
  1801. setting only applies to the root path <code>/</code> of Synapse's web server and no longer
  1802. the <code>/_matrix/client/</code> path.</p>
  1803. <h2 id="stablisation-of-msc3231"><a class="header" href="#stablisation-of-msc3231">Stablisation of MSC3231</a></h2>
  1804. <p>The unstable validity-check endpoint for the
  1805. <a href="https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv1registermloginregistration_tokenvalidity">Registration Tokens</a>
  1806. feature has been stabilised and moved from:</p>
  1807. <p><code>/_matrix/client/unstable/org.matrix.msc3231/register/org.matrix.msc3231.login.registration_token/validity</code></p>
  1808. <p>to:</p>
  1809. <p><code>/_matrix/client/v1/register/m.login.registration_token/validity</code></p>
  1810. <p>Please update any relevant reverse proxy or firewall configurations appropriately.</p>
  1811. <h2 id="time-based-cache-expiry-is-now-enabled-by-default"><a class="header" href="#time-based-cache-expiry-is-now-enabled-by-default">Time-based cache expiry is now enabled by default</a></h2>
  1812. <p>Formerly, entries in the cache were not evicted regardless of whether they were accessed after storing.
  1813. This behavior has now changed. By default entries in the cache are now evicted after 30m of not being accessed.
  1814. To change the default behavior, go to the <code>caches</code> section of the config and change the <code>expire_caches</code> and
  1815. <code>cache_entry_ttl</code> flags as necessary. Please note that these flags replace the <code>expiry_time</code> flag in the config.
  1816. The <code>expiry_time</code> flag will still continue to work, but it has been deprecated and will be removed in the future.</p>
  1817. <h2 id="deprecation-of-capability-orgmatrixmsc3283"><a class="header" href="#deprecation-of-capability-orgmatrixmsc3283">Deprecation of <code>capability</code> <code>org.matrix.msc3283.*</code></a></h2>
  1818. <p>The <code>capabilities</code> of MSC3283 from the REST API <code>/_matrix/client/r0/capabilities</code>
  1819. becomes stable.</p>
  1820. <p>The old <code>capabilities</code></p>
  1821. <ul>
  1822. <li><code>org.matrix.msc3283.set_displayname</code>,</li>
  1823. <li><code>org.matrix.msc3283.set_avatar_url</code> and</li>
  1824. <li><code>org.matrix.msc3283.3pid_changes</code></li>
  1825. </ul>
  1826. <p>are deprecated and scheduled to be removed in Synapse v1.54.0.</p>
  1827. <p>The new <code>capabilities</code></p>
  1828. <ul>
  1829. <li><code>m.set_displayname</code>,</li>
  1830. <li><code>m.set_avatar_url</code> and</li>
  1831. <li><code>m.3pid_changes</code></li>
  1832. </ul>
  1833. <p>are now active by default.</p>
  1834. <h2 id="removal-of-user_may_create_room_with_invites"><a class="header" href="#removal-of-user_may_create_room_with_invites">Removal of <code>user_may_create_room_with_invites</code></a></h2>
  1835. <p>As announced with the release of <a href="upgrade.html#deprecation-of-the-user_may_create_room_with_invites-module-callback">Synapse 1.47.0</a>,
  1836. the deprecated <code>user_may_create_room_with_invites</code> module callback has been removed.</p>
  1837. <p>Modules relying on it can instead implement <a href="https://matrix-org.github.io/synapse/latest/modules/spam_checker_callbacks.html#user_may_invite"><code>user_may_invite</code></a>
  1838. 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>
  1839. module API to infer whether the invite is happening while creating a room (see <a href="https://github.com/matrix-org/synapse-domain-rule-checker/blob/e7d092dd9f2a7f844928771dbfd9fd24c2332e48/synapse_domain_rule_checker/__init__.py#L56-L89">this function</a>
  1840. as an example). Alternately, modules can also implement <a href="https://matrix-org.github.io/synapse/latest/modules/third_party_rules_callbacks.html#on_create_room"><code>on_create_room</code></a>.</p>
  1841. <h1 id="upgrading-to-v1520"><a class="header" href="#upgrading-to-v1520">Upgrading to v1.52.0</a></h1>
  1842. <h2 id="twisted-security-release"><a class="header" href="#twisted-security-release">Twisted security release</a></h2>
  1843. <p>Note that <a href="https://github.com/twisted/twisted/releases/tag/twisted-22.1.0">Twisted 22.1.0</a>
  1844. has recently been released, which fixes a <a href="https://github.com/twisted/twisted/security/advisories/GHSA-92x2-jw7w-xvvx">security issue</a>
  1845. within the Twisted library. We do not believe Synapse is affected by this vulnerability,
  1846. though we advise server administrators who installed Synapse via pip to upgrade Twisted
  1847. with <code>pip install --upgrade Twisted treq</code> as a matter of good practice. The Docker image
  1848. <code>matrixdotorg/synapse</code> and the Debian packages from <code>packages.matrix.org</code> are using the
  1849. updated library.</p>
  1850. <h1 id="upgrading-to-v1510"><a class="header" href="#upgrading-to-v1510">Upgrading to v1.51.0</a></h1>
  1851. <h2 id="deprecation-of-webclient-listeners-and-non-https-web_client_location"><a class="header" href="#deprecation-of-webclient-listeners-and-non-https-web_client_location">Deprecation of <code>webclient</code> listeners and non-HTTP(S) <code>web_client_location</code></a></h2>
  1852. <p>Listeners of type <code>webclient</code> are deprecated and scheduled to be removed in
  1853. Synapse v1.53.0.</p>
  1854. <p>Similarly, a non-HTTP(S) <code>web_client_location</code> configuration is deprecated and
  1855. will become a configuration error in Synapse v1.53.0.</p>
  1856. <h1 id="upgrading-to-v1500"><a class="header" href="#upgrading-to-v1500">Upgrading to v1.50.0</a></h1>
  1857. <h2 id="dropping-support-for-old-python-and-postgres-versions"><a class="header" href="#dropping-support-for-old-python-and-postgres-versions">Dropping support for old Python and Postgres versions</a></h2>
  1858. <p>In line with our <a href="deprecation_policy.html">deprecation policy</a>,
  1859. we've dropped support for Python 3.6 and PostgreSQL 9.6, as they are no
  1860. longer supported upstream.</p>
  1861. <p>This release of Synapse requires Python 3.7+ and PostgreSQL 10+.</p>
  1862. <h1 id="upgrading-to-v1470"><a class="header" href="#upgrading-to-v1470">Upgrading to v1.47.0</a></h1>
  1863. <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>
  1864. <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>
  1865. (released on 2021-05-17) and have now been removed:</p>
  1866. <ul>
  1867. <li><code>POST /_synapse/admin/v1/&lt;room_id&gt;/delete</code></li>
  1868. </ul>
  1869. <p>Any scripts still using the above APIs should be converted to use the
  1870. <a href="https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#delete-room-api">Delete Room API</a>.</p>
  1871. <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>
  1872. <p>The <code>user_may_create_room_with_invites</code> is deprecated and will be removed in a future
  1873. version of Synapse. Modules implementing this callback can instead implement
  1874. <a href="https://matrix-org.github.io/synapse/latest/modules/spam_checker_callbacks.html#user_may_invite"><code>user_may_invite</code></a>
  1875. 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>
  1876. module API method to infer whether the invite is happening in the context of creating a
  1877. room.</p>
  1878. <p>We plan to remove this callback in January 2022.</p>
  1879. <h1 id="upgrading-to-v1450"><a class="header" href="#upgrading-to-v1450">Upgrading to v1.45.0</a></h1>
  1880. <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>
  1881. <p>Media storage provider modules that read from the Synapse configuration object (i.e. that
  1882. read the value of <code>hs.config.[...]</code>) now need to specify the configuration section they're
  1883. reading from. This means that if a module reads the value of e.g. <code>hs.config.media_store_path</code>,
  1884. it needs to replace it with <code>hs.config.media.media_store_path</code>.</p>
  1885. <h1 id="upgrading-to-v1440"><a class="header" href="#upgrading-to-v1440">Upgrading to v1.44.0</a></h1>
  1886. <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>
  1887. <p>The <code>url_cache/</code> and <code>url_cache_thumbnails/</code> directories in the media store are
  1888. no longer mirrored to storage providers. These two directories can be safely
  1889. deleted from any configured storage providers to reclaim space.</p>
  1890. <h1 id="upgrading-to-v1430"><a class="header" href="#upgrading-to-v1430">Upgrading to v1.43.0</a></h1>
  1891. <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>
  1892. <p>The <a href="https://matrix-org.github.io/synapse/latest/workers.html#available-worker-applications">available worker applications documentation</a>
  1893. has been updated to reflect that calls to the <code>/spaces</code>, <code>/hierarchy</code>, and
  1894. <code>/summary</code> endpoints can now be routed to workers for both client API and
  1895. federation requests.</p>
  1896. <h1 id="upgrading-to-v1420"><a class="header" href="#upgrading-to-v1420">Upgrading to v1.42.0</a></h1>
  1897. <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>
  1898. <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>
  1899. (released on 2021-01-13) and have now been removed:</p>
  1900. <ul>
  1901. <li><code>POST /_synapse/admin/v1/purge_room</code></li>
  1902. <li><code>POST /_synapse/admin/v1/shutdown_room/&lt;room_id&gt;</code></li>
  1903. </ul>
  1904. <p>Any scripts still using the above APIs should be converted to use the
  1905. <a href="https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#delete-room-api">Delete Room API</a>.</p>
  1906. <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>
  1907. <p>This may affect you if you make use of custom HTML templates for the
  1908. <a href="../synapse/res/templates/recaptcha.html">reCAPTCHA</a> or
  1909. <a href="../synapse/res/templates/terms.html">terms</a> fallback pages.</p>
  1910. <p>The template is now provided an <code>error</code> variable if the authentication
  1911. process failed. See the default templates linked above for an example.</p>
  1912. <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>
  1913. <p>Users will stop receiving message updates via email for addresses that were
  1914. once, but not still, linked to their account.</p>
  1915. <h1 id="upgrading-to-v1410"><a class="header" href="#upgrading-to-v1410">Upgrading to v1.41.0</a></h1>
  1916. <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>
  1917. <p>Since Synapse 1.6.0 (2019-11-26) you can set a proxy for outbound HTTP requests via
  1918. http_proxy/https_proxy environment variables. This proxy was set for:</p>
  1919. <ul>
  1920. <li>push</li>
  1921. <li>url previews</li>
  1922. <li>phone-home stats</li>
  1923. <li>recaptcha validation</li>
  1924. <li>CAS auth validation</li>
  1925. <li>OpenID Connect</li>
  1926. <li>Federation (checking public key revocation)</li>
  1927. </ul>
  1928. <p>In this version we have added support for outbound requests for:</p>
  1929. <ul>
  1930. <li>Outbound federation</li>
  1931. <li>Downloading remote media</li>
  1932. <li>Fetching public keys of other servers</li>
  1933. </ul>
  1934. <p>These requests use the same proxy configuration. If you have a proxy configuration we
  1935. recommend to verify the configuration. It may be necessary to adjust the <code>no_proxy</code>
  1936. environment variable.</p>
  1937. <p>See <a href="setup/forward_proxy.html">using a forward proxy with Synapse documentation</a> for
  1938. details.</p>
  1939. <h2 id="deprecation-of-template_dir"><a class="header" href="#deprecation-of-template_dir">Deprecation of <code>template_dir</code></a></h2>
  1940. <p>The <code>template_dir</code> settings in the <code>sso</code>, <code>account_validity</code> and <code>email</code> sections of the
  1941. configuration file are now deprecated. Server admins should use the new
  1942. <code>templates.custom_template_directory</code> setting in the configuration file and use one single
  1943. custom template directory for all aforementioned features. Template file names remain
  1944. unchanged. See <a href="https://matrix-org.github.io/synapse/latest/templates.html">the related documentation</a>
  1945. for more information and examples.</p>
  1946. <p>We plan to remove support for these settings in October 2021.</p>
  1947. <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>
  1948. <p>The <a href="https://matrix-org.github.io/synapse/latest/workers.html#synapseappmedia_repository">media repository worker documentation</a>
  1949. has been updated to reflect that calls to <code>/_synapse/admin/v1/users/{userId}/media</code>
  1950. must now be handled by media repository workers. This is due to the new <code>DELETE</code> method
  1951. of this endpoint modifying the media store.</p>
  1952. <h1 id="upgrading-to-v1390"><a class="header" href="#upgrading-to-v1390">Upgrading to v1.39.0</a></h1>
  1953. <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>
  1954. <p>The current third-party rules module interface is deprecated in favour of the new generic
  1955. modules system introduced in Synapse v1.37.0. Authors of third-party rules modules can refer
  1956. to <a href="modules/porting_legacy_module.html">this documentation</a>
  1957. to update their modules. Synapse administrators can refer to <a href="modules/index.html">this documentation</a>
  1958. to update their configuration once the modules they are using have been updated.</p>
  1959. <p>We plan to remove support for the current third-party rules interface in September 2021.</p>
  1960. <h1 id="upgrading-to-v1380"><a class="header" href="#upgrading-to-v1380">Upgrading to v1.38.0</a></h1>
  1961. <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>
  1962. <p>This release includes a database schema update which requires re-indexing one of
  1963. the larger tables in the database, <code>events</code>. This could result in increased
  1964. disk I/O for several hours or days after upgrading while the migration
  1965. completes. Furthermore, because we have to keep the old indexes until the new
  1966. indexes are ready, it could result in a significant, temporary, increase in
  1967. disk space.</p>
  1968. <p>To get a rough idea of the disk space required, check the current size of one
  1969. of the indexes. For example, from a <code>psql</code> shell, run the following sql:</p>
  1970. <pre><code class="language-sql">SELECT pg_size_pretty(pg_relation_size('events_order_room'));
  1971. </code></pre>
  1972. <p>We need to rebuild <strong>four</strong> indexes, so you will need to multiply this result
  1973. by four to give an estimate of the disk space required. For example, on one
  1974. particular server:</p>
  1975. <pre><code>synapse=# select pg_size_pretty(pg_relation_size('events_order_room'));
  1976. pg_size_pretty
  1977. ----------------
  1978. 288 MB
  1979. (1 row)
  1980. </code></pre>
  1981. <p>On this server, it would be wise to ensure that at least 1152MB are free.</p>
  1982. <p>The additional disk space will be freed once the migration completes.</p>
  1983. <p>SQLite databases are unaffected by this change.</p>
  1984. <h1 id="upgrading-to-v1370"><a class="header" href="#upgrading-to-v1370">Upgrading to v1.37.0</a></h1>
  1985. <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>
  1986. <p>The current spam checker interface is deprecated in favour of a new generic modules system.
  1987. Authors of spam checker modules can refer to [this
  1988. documentation](modules/porting_legacy_module.md
  1989. to update their modules. Synapse administrators can refer to <a href="modules/index.html">this
  1990. documentation</a>
  1991. to update their configuration once the modules they are using have been updated.</p>
  1992. <p>We plan to remove support for the current spam checker interface in August 2021.</p>
  1993. <p>More module interfaces will be ported over to this new generic system in future versions
  1994. of Synapse.</p>
  1995. <h1 id="upgrading-to-v1340"><a class="header" href="#upgrading-to-v1340">Upgrading to v1.34.0</a></h1>
  1996. <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>
  1997. <p>The <code>room_invite_state_types</code> configuration setting has been deprecated
  1998. 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
  1999. file</a>.</p>
  2000. <p>If you have set <code>room_invite_state_types</code> to the default value you
  2001. should simply remove it from your configuration file. The default value
  2002. used to be:</p>
  2003. <pre><code class="language-yaml">room_invite_state_types:
  2004. - &quot;m.room.join_rules&quot;
  2005. - &quot;m.room.canonical_alias&quot;
  2006. - &quot;m.room.avatar&quot;
  2007. - &quot;m.room.encryption&quot;
  2008. - &quot;m.room.name&quot;
  2009. </code></pre>
  2010. <p>If you have customised this value, you should remove
  2011. <code>room_invite_state_types</code> and configure <code>room_prejoin_state</code> instead.</p>
  2012. <h1 id="upgrading-to-v1330"><a class="header" href="#upgrading-to-v1330">Upgrading to v1.33.0</a></h1>
  2013. <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>
  2014. <p>This may affect you if you have enabled the account validity feature,
  2015. and have made use of a custom HTML template specified by the
  2016. <code>account_validity.template_dir</code> or
  2017. <code>account_validity.account_renewed_html_path</code> Synapse config options.</p>
  2018. <p>The template can now accept an <code>expiration_ts</code> variable, which
  2019. represents the unix timestamp in milliseconds for the future date of
  2020. 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
  2021. template</a>
  2022. for an example of usage.</p>
  2023. <p>ALso note that a new HTML template, <code>account_previously_renewed.html</code>,
  2024. has been added. This is is shown to users when they attempt to renew
  2025. their account with a valid renewal token that has already been used
  2026. before. The default template contents can been found
  2027. <a href="https://github.com/matrix-org/synapse/blob/release-v1.33.0/synapse/res/templates/account_previously_renewed.html">here</a>,
  2028. and can also accept an <code>expiration_ts</code> variable. This template replaces
  2029. the error message users would previously see upon attempting to use a
  2030. valid renewal token more than once.</p>
  2031. <h1 id="upgrading-to-v1320"><a class="header" href="#upgrading-to-v1320">Upgrading to v1.32.0</a></h1>
  2032. <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>
  2033. <p>This release introduces <a href="https://github.com/matrix-org/synapse/issues/9853">a
  2034. regression</a> that can
  2035. overwhelm connected Prometheus instances. This issue is not present in
  2036. Synapse v1.32.0rc1.</p>
  2037. <p>If you have been affected, please downgrade to 1.31.0. You then may need
  2038. to remove excess writeahead logs in order for Prometheus to recover.
  2039. Instructions for doing so are provided
  2040. <a href="https://github.com/matrix-org/synapse/pull/9854#issuecomment-823472183">here</a>.</p>
  2041. <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>
  2042. <p>In line with our <a href="deprecation_policy.html">deprecation policy</a>,
  2043. we've dropped support for Python 3.5 and PostgreSQL 9.5, as they are no
  2044. longer supported upstream.</p>
  2045. <p>This release of Synapse requires Python 3.6+ and PostgresSQL 9.6+ or
  2046. SQLite 3.22+.</p>
  2047. <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>
  2048. <p>The deprecated v1 &quot;list accounts&quot; admin API
  2049. (<code>GET /_synapse/admin/v1/users/&lt;user_id&gt;</code>) has been removed in this
  2050. version.</p>
  2051. <p>The <a href="admin_api/user_admin_api.html#list-accounts">v2 list accounts API</a>
  2052. has been available since Synapse 1.7.0 (2019-12-13), and is accessible
  2053. under <code>GET /_synapse/admin/v2/users</code>.</p>
  2054. <p>The deprecation of the old endpoint was announced with Synapse 1.28.0
  2055. (released on 2021-02-25).</p>
  2056. <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>
  2057. <p>In compliance with the <a href="https://matrix.org/docs/spec/application_service/r0.1.2#server-admin-style-permissions">Application Service
  2058. spec</a>,
  2059. Application Services are now required to use the
  2060. <code>m.login.application_service</code> type when registering users via the
  2061. <code>/_matrix/client/r0/register</code> endpoint. This behaviour was deprecated in
  2062. Synapse v1.30.0.</p>
  2063. <p>Please ensure your Application Services are up to date.</p>
  2064. <h1 id="upgrading-to-v1290"><a class="header" href="#upgrading-to-v1290">Upgrading to v1.29.0</a></h1>
  2065. <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>
  2066. <p>When using Synapse with a reverse proxy (in particular, when using the
  2067. <code>x_forwarded</code> option on an HTTP listener), Synapse now
  2068. expects to receive an <code>X-Forwarded-Proto</code> header on incoming
  2069. HTTP requests. If it is not set, Synapse will log a warning on each
  2070. received request.</p>
  2071. <p>To avoid the warning, administrators using a reverse proxy should ensure
  2072. that the reverse proxy sets <code>X-Forwarded-Proto</code> header to
  2073. <code>https</code> or <code>http</code> to indicate the protocol used
  2074. by the client.</p>
  2075. <p>Synapse also requires the <code>Host</code> header to be preserved.</p>
  2076. <p>See the <a href="reverse_proxy.html">reverse proxy documentation</a>, where the
  2077. example configurations have been updated to show how to set these
  2078. headers.</p>
  2079. <p>(Users of <a href="https://caddyserver.com/">Caddy</a> are unaffected, since we
  2080. believe it sets <code>X-Forwarded-Proto</code> by default.)</p>
  2081. <h1 id="upgrading-to-v1270"><a class="header" href="#upgrading-to-v1270">Upgrading to v1.27.0</a></h1>
  2082. <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>
  2083. <p>This version changes the URI used for callbacks from OAuth2 and SAML2
  2084. identity providers:</p>
  2085. <ul>
  2086. <li>
  2087. <p>If your server is configured for single sign-on via an OpenID
  2088. Connect or OAuth2 identity provider, you will need to add
  2089. <code>[synapse public baseurl]/_synapse/client/oidc/callback</code> to the list
  2090. of permitted &quot;redirect URIs&quot; at the identity provider.</p>
  2091. <p>See the <a href="openid.html">OpenID docs</a> for more information on setting
  2092. up OpenID Connect.</p>
  2093. </li>
  2094. <li>
  2095. <p>If your server is configured for single sign-on via a SAML2 identity
  2096. provider, you will need to add
  2097. <code>[synapse public baseurl]/_synapse/client/saml2/authn_response</code> as a
  2098. permitted &quot;ACS location&quot; (also known as &quot;allowed callback URLs&quot;)
  2099. at the identity provider.</p>
  2100. <p>The &quot;Issuer&quot; in the &quot;AuthnRequest&quot; to the SAML2 identity
  2101. provider is also updated to
  2102. <code>[synapse public baseurl]/_synapse/client/saml2/metadata.xml</code>. If
  2103. your SAML2 identity provider uses this property to validate or
  2104. otherwise identify Synapse, its configuration will need to be
  2105. updated to use the new URL. Alternatively you could create a new,
  2106. separate &quot;EntityDescriptor&quot; in your SAML2 identity provider with
  2107. the new URLs and leave the URLs in the existing &quot;EntityDescriptor&quot;
  2108. as they were.</p>
  2109. </li>
  2110. </ul>
  2111. <h2 id="changes-to-html-templates"><a class="header" href="#changes-to-html-templates">Changes to HTML templates</a></h2>
  2112. <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
  2113. autoescape</a>
  2114. enabled for files ending in <code>.html</code>, <code>.htm</code>, and <code>.xml</code>. If you have
  2115. customised these templates and see issues when viewing them you might
  2116. need to update them. It is expected that most configurations will need
  2117. no changes.</p>
  2118. <p>If you have customised the templates <em>names</em> for these templates, it is
  2119. recommended to verify they end in <code>.html</code> to ensure autoescape is
  2120. enabled.</p>
  2121. <p>The above applies to the following templates:</p>
  2122. <ul>
  2123. <li><code>add_threepid.html</code></li>
  2124. <li><code>add_threepid_failure.html</code></li>
  2125. <li><code>add_threepid_success.html</code></li>
  2126. <li><code>notice_expiry.html</code></li>
  2127. <li><code>notice_expiry.html</code></li>
  2128. <li><code>notif_mail.html</code> (which, by default, includes <code>room.html</code> and
  2129. <code>notif.html</code>)</li>
  2130. <li><code>password_reset.html</code></li>
  2131. <li><code>password_reset_confirmation.html</code></li>
  2132. <li><code>password_reset_failure.html</code></li>
  2133. <li><code>password_reset_success.html</code></li>
  2134. <li><code>registration.html</code></li>
  2135. <li><code>registration_failure.html</code></li>
  2136. <li><code>registration_success.html</code></li>
  2137. <li><code>sso_account_deactivated.html</code></li>
  2138. <li><code>sso_auth_bad_user.html</code></li>
  2139. <li><code>sso_auth_confirm.html</code></li>
  2140. <li><code>sso_auth_success.html</code></li>
  2141. <li><code>sso_error.html</code></li>
  2142. <li><code>sso_login_idp_picker.html</code></li>
  2143. <li><code>sso_redirect_confirm.html</code></li>
  2144. </ul>
  2145. <h1 id="upgrading-to-v1260"><a class="header" href="#upgrading-to-v1260">Upgrading to v1.26.0</a></h1>
  2146. <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>
  2147. <p>v1.26.0 includes a lot of large changes. If something problematic
  2148. occurs, you may want to roll-back to a previous version of Synapse.
  2149. Because v1.26.0 also includes a new database schema version, reverting
  2150. that version is also required alongside the generic rollback
  2151. instructions mentioned above. In short, to roll back to v1.25.0 you need
  2152. to:</p>
  2153. <ol>
  2154. <li>
  2155. <p>Stop the server</p>
  2156. </li>
  2157. <li>
  2158. <p>Decrease the schema version in the database:</p>
  2159. <pre><code class="language-sql">UPDATE schema_version SET version = 58;
  2160. </code></pre>
  2161. </li>
  2162. <li>
  2163. <p>Delete the ignored users &amp; chain cover data:</p>
  2164. <pre><code class="language-sql">DROP TABLE IF EXISTS ignored_users;
  2165. UPDATE rooms SET has_auth_chain_index = false;
  2166. </code></pre>
  2167. <p>For PostgreSQL run:</p>
  2168. <pre><code class="language-sql">TRUNCATE event_auth_chain_links;
  2169. TRUNCATE event_auth_chains;
  2170. </code></pre>
  2171. <p>For SQLite run:</p>
  2172. <pre><code class="language-sql">DELETE FROM event_auth_chain_links;
  2173. DELETE FROM event_auth_chains;
  2174. </code></pre>
  2175. </li>
  2176. <li>
  2177. <p>Mark the deltas as not run (so they will re-run on upgrade).</p>
  2178. <pre><code class="language-sql">DELETE FROM applied_schema_deltas WHERE version = 59 AND file = &quot;59/01ignored_user.py&quot;;
  2179. DELETE FROM applied_schema_deltas WHERE version = 59 AND file = &quot;59/06chain_cover_index.sql&quot;;
  2180. </code></pre>
  2181. </li>
  2182. <li>
  2183. <p>Downgrade Synapse by following the instructions for your
  2184. installation method in the &quot;Rolling back to older versions&quot;
  2185. section above.</p>
  2186. </li>
  2187. </ol>
  2188. <h1 id="upgrading-to-v1250"><a class="header" href="#upgrading-to-v1250">Upgrading to v1.25.0</a></h1>
  2189. <h2 id="last-release-supporting-python-35"><a class="header" href="#last-release-supporting-python-35">Last release supporting Python 3.5</a></h2>
  2190. <p>This is the last release of Synapse which guarantees support with Python
  2191. 3.5, which passed its upstream End of Life date several months ago.</p>
  2192. <p>We will attempt to maintain support through March 2021, but without
  2193. guarantees.</p>
  2194. <p>In the future, Synapse will follow upstream schedules for ending support
  2195. of older versions of Python and PostgreSQL. Please upgrade to at least
  2196. Python 3.6 and PostgreSQL 9.6 as soon as possible.</p>
  2197. <h2 id="blacklisting-ip-ranges"><a class="header" href="#blacklisting-ip-ranges">Blacklisting IP ranges</a></h2>
  2198. <p>Synapse v1.25.0 includes new settings, <code>ip_range_blacklist</code> and
  2199. <code>ip_range_whitelist</code>, for controlling outgoing requests from Synapse for
  2200. federation, identity servers, push, and for checking key validity for
  2201. third-party invite events. The previous setting,
  2202. <code>federation_ip_range_blacklist</code>, is deprecated. The new
  2203. <code>ip_range_blacklist</code> defaults to private IP ranges if it is not defined.</p>
  2204. <p>If you have never customised <code>federation_ip_range_blacklist</code> it is
  2205. recommended that you remove that setting.</p>
  2206. <p>If you have customised <code>federation_ip_range_blacklist</code> you should update
  2207. the setting name to <code>ip_range_blacklist</code>.</p>
  2208. <p>If you have a custom push server that is reached via private IP space
  2209. you may need to customise <code>ip_range_blacklist</code> or <code>ip_range_whitelist</code>.</p>
  2210. <h1 id="upgrading-to-v1240"><a class="header" href="#upgrading-to-v1240">Upgrading to v1.24.0</a></h1>
  2211. <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>
  2212. <p>This release allows the OpenID Connect mapping provider to perform
  2213. normalisation of the localpart of the Matrix ID. This allows for the
  2214. mapping provider to specify different algorithms, instead of the
  2215. <a href="https://matrix.org/docs/spec/appendices#mapping-from-other-character-sets">default
  2216. way</a>.</p>
  2217. <p>If your Synapse configuration uses a custom mapping provider
  2218. (<code>oidc_config.user_mapping_provider.module</code> is specified and
  2219. not equal to
  2220. <code>synapse.handlers.oidc_handler.JinjaOidcMappingProvider</code>)
  2221. then you <em>must</em> ensure that <code>map_user_attributes</code> of the
  2222. mapping provider performs some normalisation of the
  2223. <code>localpart</code> returned. To match previous behaviour you can
  2224. use the <code>map_username_to_mxid_localpart</code> function provided
  2225. by Synapse. An example is shown below:</p>
  2226. <pre><code class="language-python">from synapse.types import map_username_to_mxid_localpart
  2227. class MyMappingProvider:
  2228. def map_user_attributes(self, userinfo, token):
  2229. # ... your custom logic ...
  2230. sso_user_id = ...
  2231. localpart = map_username_to_mxid_localpart(sso_user_id)
  2232. return {&quot;localpart&quot;: localpart}
  2233. </code></pre>
  2234. <h2 id="removal-historical-synapse-admin-api"><a class="header" href="#removal-historical-synapse-admin-api">Removal historical Synapse Admin API</a></h2>
  2235. <p>Historically, the Synapse Admin API has been accessible under:</p>
  2236. <ul>
  2237. <li><code>/_matrix/client/api/v1/admin</code></li>
  2238. <li><code>/_matrix/client/unstable/admin</code></li>
  2239. <li><code>/_matrix/client/r0/admin</code></li>
  2240. <li><code>/_synapse/admin/v1</code></li>
  2241. </ul>
  2242. <p>The endpoints with <code>/_matrix/client/*</code> prefixes have been removed as of
  2243. v1.24.0. The Admin API is now only accessible under:</p>
  2244. <ul>
  2245. <li><code>/_synapse/admin/v1</code></li>
  2246. </ul>
  2247. <p>The only exception is the <code>/admin/whois</code> endpoint, which is
  2248. <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
  2249. API</a>.</p>
  2250. <p>The deprecation of the old endpoints was announced with Synapse 1.20.0
  2251. (released on 2020-09-22) and makes it easier for homeserver admins to
  2252. lock down external access to the Admin API endpoints.</p>
  2253. <h1 id="upgrading-to-v1230"><a class="header" href="#upgrading-to-v1230">Upgrading to v1.23.0</a></h1>
  2254. <h2 id="structured-logging-configuration-breaking-changes"><a class="header" href="#structured-logging-configuration-breaking-changes">Structured logging configuration breaking changes</a></h2>
  2255. <p>This release deprecates use of the <code>structured: true</code> logging
  2256. configuration for structured logging. If your logging configuration
  2257. contains <code>structured: true</code> then it should be modified based on the
  2258. <a href="https://matrix-org.github.io/synapse/v1.56/structured_logging.html#upgrading-from-legacy-structured-logging-configuration">structured logging documentation</a>.</p>
  2259. <p>The <code>structured</code> and <code>drains</code> logging options are now deprecated and
  2260. should be replaced by standard logging configuration of <code>handlers</code> and
  2261. <code>formatters</code>.</p>
  2262. <p>A future will release of Synapse will make using <code>structured: true</code> an
  2263. error.</p>
  2264. <h1 id="upgrading-to-v1220"><a class="header" href="#upgrading-to-v1220">Upgrading to v1.22.0</a></h1>
  2265. <h2 id="thirdpartyeventrules-breaking-changes"><a class="header" href="#thirdpartyeventrules-breaking-changes">ThirdPartyEventRules breaking changes</a></h2>
  2266. <p>This release introduces a backwards-incompatible change to modules
  2267. making use of <code>ThirdPartyEventRules</code> in Synapse. If you make use of a
  2268. module defined under the <code>third_party_event_rules</code> config option, please
  2269. make sure it is updated to handle the below change:</p>
  2270. <p>The <code>http_client</code> argument is no longer passed to modules as they are
  2271. initialised. Instead, modules are expected to make use of the
  2272. <code>http_client</code> property on the <code>ModuleApi</code> class. Modules are now passed
  2273. a <code>module_api</code> argument during initialisation, which is an instance of
  2274. <code>ModuleApi</code>. <code>ModuleApi</code> instances have a <code>http_client</code> property which
  2275. acts the same as the <code>http_client</code> argument previously passed to
  2276. <code>ThirdPartyEventRules</code> modules.</p>
  2277. <h1 id="upgrading-to-v1210"><a class="header" href="#upgrading-to-v1210">Upgrading to v1.21.0</a></h1>
  2278. <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>
  2279. <p>The <a href="reverse_proxy.html">reverse proxy documentation</a>
  2280. has been updated to include reverse proxy directives for
  2281. <code>/_synapse/client/*</code> endpoints. As the user password reset flow now uses
  2282. endpoints under this prefix, <strong>you must update your reverse proxy
  2283. configurations for user password reset to work</strong>.</p>
  2284. <p>Additionally, note that the <a href="workers.html">Synapse worker documentation</a> has been updated to</p>
  2285. <p>: state that the <code>/_synapse/client/password_reset/email/submit_token</code>
  2286. endpoint can be handled</p>
  2287. <p>by all workers. If you make use of Synapse's worker feature, please
  2288. update your reverse proxy configuration to reflect this change.</p>
  2289. <h2 id="new-html-templates"><a class="header" href="#new-html-templates">New HTML templates</a></h2>
  2290. <p>A new HTML template,
  2291. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/res/templates/password_reset_confirmation.html">password_reset_confirmation.html</a>,
  2292. has been added to the <code>synapse/res/templates</code> directory. If you are
  2293. using a custom template directory, you may want to copy the template
  2294. over and modify it.</p>
  2295. <p>Note that as of v1.20.0, templates do not need to be included in custom
  2296. template directories for Synapse to start. The default templates will be
  2297. used if a custom template cannot be found.</p>
  2298. <p>This page will appear to the user after clicking a password reset link
  2299. that has been emailed to them.</p>
  2300. <p>To complete password reset, the page must include a way to make a
  2301. <code>POST</code> request to
  2302. <code>/_synapse/client/password_reset/{medium}/submit_token</code> with the query
  2303. parameters from the original link, presented as a URL-encoded form. See
  2304. the file itself for more details.</p>
  2305. <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>
  2306. <p>The <code>saml_error.html</code> template was removed from Synapse and replaced
  2307. with the <code>sso_error.html</code> template. If your Synapse is configured to use
  2308. SAML and a custom <code>sso_redirect_confirm_template_dir</code> configuration then
  2309. any customisations of the <code>saml_error.html</code> template will need to be
  2310. merged into the <code>sso_error.html</code> template. These templates are similar,
  2311. but the parameters are slightly different:</p>
  2312. <ul>
  2313. <li>The <code>msg</code> parameter should be renamed to <code>error_description</code>.</li>
  2314. <li>There is no longer a <code>code</code> parameter for the response code.</li>
  2315. <li>A string <code>error</code> parameter is available that includes a short hint
  2316. of why a user is seeing the error page.</li>
  2317. </ul>
  2318. <h1 id="upgrading-to-v1180"><a class="header" href="#upgrading-to-v1180">Upgrading to v1.18.0</a></h1>
  2319. <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>
  2320. <p>From 10th August 2020, we will no longer publish Docker images with the
  2321. <code>-py3</code> tag suffix. The images tagged with the
  2322. <code>-py3</code> suffix have been identical to the non-suffixed tags
  2323. since release 0.99.0, and the suffix is obsolete.</p>
  2324. <p>On 10th August, we will remove the <code>latest-py3</code> tag.
  2325. Existing per-release tags (such as <code>v1.18.0-py3</code> will not
  2326. be removed, but no new <code>-py3</code> tags will be added.</p>
  2327. <p>Scripts relying on the <code>-py3</code> suffix will need to be
  2328. updated.</p>
  2329. <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>
  2330. <p>When setting up worker processes, we now recommend the use of a Redis
  2331. server for replication. <strong>The old direct TCP connection method is
  2332. deprecated and will be removed in a future release.</strong> See
  2333. <a href="workers.html">workers</a> for more details.</p>
  2334. <h1 id="upgrading-to-v1140"><a class="header" href="#upgrading-to-v1140">Upgrading to v1.14.0</a></h1>
  2335. <p>This version includes a database update which is run as part of the
  2336. upgrade, and which may take a couple of minutes in the case of a large
  2337. server. Synapse will not respond to HTTP requests while this update is
  2338. taking place.</p>
  2339. <h1 id="upgrading-to-v1130"><a class="header" href="#upgrading-to-v1130">Upgrading to v1.13.0</a></h1>
  2340. <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>
  2341. <p>A bug was introduced in Synapse 1.4.0 which could cause the room
  2342. directory to be incomplete or empty if Synapse was upgraded directly
  2343. from v1.2.1 or earlier, to versions between v1.4.0 and v1.12.x.</p>
  2344. <p>This will <em>not</em> be a problem for Synapse installations which were:</p>
  2345. <p>: - created at v1.4.0 or later,
  2346. - upgraded via v1.3.x, or
  2347. - upgraded straight from v1.2.1 or earlier to v1.13.0 or later.</p>
  2348. <p>If completeness of the room directory is a concern, installations which
  2349. are affected can be repaired as follows:</p>
  2350. <ol>
  2351. <li>
  2352. <p>Run the following sql from a <code>psql</code> or
  2353. <code>sqlite3</code> console:</p>
  2354. <pre><code class="language-sql">INSERT INTO background_updates (update_name, progress_json, depends_on) VALUES
  2355. ('populate_stats_process_rooms', '{}', 'current_state_events_membership');
  2356. INSERT INTO background_updates (update_name, progress_json, depends_on) VALUES
  2357. ('populate_stats_process_users', '{}', 'populate_stats_process_rooms');
  2358. </code></pre>
  2359. </li>
  2360. <li>
  2361. <p>Restart synapse.</p>
  2362. </li>
  2363. </ol>
  2364. <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>
  2365. <p>New templates (<code>sso_auth_confirm.html</code>, <code>sso_auth_success.html</code>, and
  2366. <code>sso_account_deactivated.html</code>) were added to Synapse. If your Synapse
  2367. is configured to use SSO and a custom
  2368. <code>sso_redirect_confirm_template_dir</code> configuration then these templates
  2369. will need to be copied from
  2370. <a href="synapse/res/templates">synapse/res/templates</a> into that directory.</p>
  2371. <h2 id="synapse-sso-plugins-method-deprecation"><a class="header" href="#synapse-sso-plugins-method-deprecation">Synapse SSO Plugins Method Deprecation</a></h2>
  2372. <p>Plugins using the <code>complete_sso_login</code> method of
  2373. <code>synapse.module_api.ModuleApi</code> should update to using the async/await
  2374. version <code>complete_sso_login_async</code> which includes additional checks. The
  2375. non-async version is considered deprecated.</p>
  2376. <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>
  2377. <p>v1.13.0 includes a lot of large changes. If something problematic
  2378. occurs, you may want to roll-back to a previous version of Synapse.
  2379. Because v1.13.0 also includes a new database schema version, reverting
  2380. that version is also required alongside the generic rollback
  2381. instructions mentioned above. In short, to roll back to v1.12.4 you need
  2382. to:</p>
  2383. <ol>
  2384. <li>
  2385. <p>Stop the server</p>
  2386. </li>
  2387. <li>
  2388. <p>Decrease the schema version in the database:</p>
  2389. <pre><code class="language-sql">UPDATE schema_version SET version = 57;
  2390. </code></pre>
  2391. </li>
  2392. <li>
  2393. <p>Downgrade Synapse by following the instructions for your
  2394. installation method in the &quot;Rolling back to older versions&quot;
  2395. section above.</p>
  2396. </li>
  2397. </ol>
  2398. <h1 id="upgrading-to-v1120"><a class="header" href="#upgrading-to-v1120">Upgrading to v1.12.0</a></h1>
  2399. <p>This version includes a database update which is run as part of the
  2400. upgrade, and which may take some time (several hours in the case of a
  2401. large server). Synapse will not respond to HTTP requests while this
  2402. update is taking place.</p>
  2403. <p>This is only likely to be a problem in the case of a server which is
  2404. participating in many rooms.</p>
  2405. <ol start="0">
  2406. <li>
  2407. <p>As with all upgrades, it is recommended that you have a recent
  2408. backup of your database which can be used for recovery in the event
  2409. of any problems.</p>
  2410. </li>
  2411. <li>
  2412. <p>As an initial check to see if you will be affected, you can try
  2413. running the following query from the <code>psql</code> or
  2414. <code>sqlite3</code> console. It is safe to run it while Synapse is
  2415. still running.</p>
  2416. <pre><code class="language-sql">SELECT MAX(q.v) FROM (
  2417. SELECT (
  2418. SELECT ej.json AS v
  2419. FROM state_events se INNER JOIN event_json ej USING (event_id)
  2420. WHERE se.room_id=rooms.room_id AND se.type='m.room.create' AND se.state_key=''
  2421. LIMIT 1
  2422. ) FROM rooms WHERE rooms.room_version IS NULL
  2423. ) q;
  2424. </code></pre>
  2425. <p>This query will take about the same amount of time as the upgrade
  2426. process: ie, if it takes 5 minutes, then it is likely that Synapse
  2427. will be unresponsive for 5 minutes during the upgrade.</p>
  2428. <p>If you consider an outage of this duration to be acceptable, no
  2429. further action is necessary and you can simply start Synapse 1.12.0.</p>
  2430. <p>If you would prefer to reduce the downtime, continue with the steps
  2431. below.</p>
  2432. </li>
  2433. <li>
  2434. <p>The easiest workaround for this issue is to manually create a new
  2435. index before upgrading. On PostgreSQL, his can be done as follows:</p>
  2436. <pre><code class="language-sql">CREATE INDEX CONCURRENTLY tmp_upgrade_1_12_0_index
  2437. ON state_events(room_id) WHERE type = 'm.room.create';
  2438. </code></pre>
  2439. <p>The above query may take some time, but is also safe to run while
  2440. Synapse is running.</p>
  2441. <p>We assume that no SQLite users have databases large enough to be
  2442. affected. If you <em>are</em> affected, you can run a similar query,
  2443. omitting the <code>CONCURRENTLY</code> keyword. Note however that this
  2444. operation may in itself cause Synapse to stop running for some time.
  2445. Synapse admins are reminded that <a href="postgres.html">SQLite is not recommended for use
  2446. outside a test environment</a>.</p>
  2447. </li>
  2448. <li>
  2449. <p>Once the index has been created, the <code>SELECT</code> query in step 1 above
  2450. should complete quickly. It is therefore safe to upgrade to Synapse
  2451. 1.12.0.</p>
  2452. </li>
  2453. <li>
  2454. <p>Once Synapse 1.12.0 has successfully started and is responding to
  2455. HTTP requests, the temporary index can be removed:</p>
  2456. <pre><code class="language-sql">DROP INDEX tmp_upgrade_1_12_0_index;
  2457. </code></pre>
  2458. </li>
  2459. </ol>
  2460. <h1 id="upgrading-to-v1100"><a class="header" href="#upgrading-to-v1100">Upgrading to v1.10.0</a></h1>
  2461. <p>Synapse will now log a warning on start up if used with a PostgreSQL
  2462. database that has a non-recommended locale set.</p>
  2463. <p>See <a href="postgres.html">Postgres</a> for details.</p>
  2464. <h1 id="upgrading-to-v180"><a class="header" href="#upgrading-to-v180">Upgrading to v1.8.0</a></h1>
  2465. <p>Specifying a <code>log_file</code> config option will now cause Synapse to refuse
  2466. to start, and should be replaced by with the <code>log_config</code> option.
  2467. Support for the <code>log_file</code> option was removed in v1.3.0 and has since
  2468. had no effect.</p>
  2469. <h1 id="upgrading-to-v170"><a class="header" href="#upgrading-to-v170">Upgrading to v1.7.0</a></h1>
  2470. <p>In an attempt to configure Synapse in a privacy preserving way, the
  2471. default behaviours of <code>allow_public_rooms_without_auth</code> and
  2472. <code>allow_public_rooms_over_federation</code> have been inverted. This means that
  2473. by default, only authenticated users querying the Client/Server API will
  2474. be able to query the room directory, and relatedly that the server will
  2475. not share room directory information with other servers over federation.</p>
  2476. <p>If your installation does not explicitly set these settings one way or
  2477. the other and you want either setting to be <code>true</code> then it will
  2478. necessary to update your homeserver configuration file accordingly.</p>
  2479. <p>For more details on the surrounding context see our
  2480. <a href="https://matrix.org/blog/2019/11/09/avoiding-unwelcome-visitors-on-private-matrix-servers">explainer</a>.</p>
  2481. <h1 id="upgrading-to-v150"><a class="header" href="#upgrading-to-v150">Upgrading to v1.5.0</a></h1>
  2482. <p>This release includes a database migration which may take several
  2483. minutes to complete if there are a large number (more than a million or
  2484. so) of entries in the <code>devices</code> table. This is only likely to a be a
  2485. problem on very large installations.</p>
  2486. <h1 id="upgrading-to-v140"><a class="header" href="#upgrading-to-v140">Upgrading to v1.4.0</a></h1>
  2487. <h2 id="new-custom-templates"><a class="header" href="#new-custom-templates">New custom templates</a></h2>
  2488. <p>If you have configured a custom template directory with the
  2489. <code>email.template_dir</code> option, be aware that there are new templates
  2490. regarding registration and threepid management (see below) that must be
  2491. included.</p>
  2492. <ul>
  2493. <li><code>registration.html</code> and <code>registration.txt</code></li>
  2494. <li><code>registration_success.html</code> and <code>registration_failure.html</code></li>
  2495. <li><code>add_threepid.html</code> and <code>add_threepid.txt</code></li>
  2496. <li><code>add_threepid_failure.html</code> and <code>add_threepid_success.html</code></li>
  2497. </ul>
  2498. <p>Synapse will expect these files to exist inside the configured template
  2499. directory, and <strong>will fail to start</strong> if they are absent. To view the
  2500. default templates, see
  2501. <a href="https://github.com/matrix-org/synapse/tree/master/synapse/res/templates">synapse/res/templates</a>.</p>
  2502. <h2 id="3pid-verification-changes"><a class="header" href="#3pid-verification-changes">3pid verification changes</a></h2>
  2503. <p><strong>Note: As of this release, users will be unable to add phone numbers or
  2504. email addresses to their accounts, without changes to the Synapse
  2505. configuration. This includes adding an email address during
  2506. registration.</strong></p>
  2507. <p>It is possible for a user to associate an email address or phone number
  2508. with their account, for a number of reasons:</p>
  2509. <ul>
  2510. <li>for use when logging in, as an alternative to the user id.</li>
  2511. <li>in the case of email, as an alternative contact to help with account
  2512. recovery.</li>
  2513. <li>in the case of email, to receive notifications of missed messages.</li>
  2514. </ul>
  2515. <p>Before an email address or phone number can be added to a user's
  2516. account, or before such an address is used to carry out a
  2517. password-reset, Synapse must confirm the operation with the owner of the
  2518. email address or phone number. It does this by sending an email or text
  2519. giving the user a link or token to confirm receipt. This process is
  2520. known as '3pid verification'. ('3pid', or 'threepid', stands for
  2521. third-party identifier, and we use it to refer to external identifiers
  2522. such as email addresses and phone numbers.)</p>
  2523. <p>Previous versions of Synapse delegated the task of 3pid verification to
  2524. an identity server by default. In most cases this server is <code>vector.im</code>
  2525. or <code>matrix.org</code>.</p>
  2526. <p>In Synapse 1.4.0, for security and privacy reasons, the homeserver will
  2527. no longer delegate this task to an identity server by default. Instead,
  2528. the server administrator will need to explicitly decide how they would
  2529. like the verification messages to be sent.</p>
  2530. <p>In the medium term, the <code>vector.im</code> and <code>matrix.org</code> identity servers
  2531. will disable support for delegated 3pid verification entirely. However,
  2532. in order to ease the transition, they will retain the capability for a
  2533. limited period. Delegated email verification will be disabled on Monday
  2534. 2nd December 2019 (giving roughly 2 months notice). Disabling delegated
  2535. SMS verification will follow some time after that once SMS verification
  2536. support lands in Synapse.</p>
  2537. <p>Once delegated 3pid verification support has been disabled in the
  2538. <code>vector.im</code> and <code>matrix.org</code> identity servers, all Synapse versions that
  2539. depend on those instances will be unable to verify email and phone
  2540. numbers through them. There are no imminent plans to remove delegated
  2541. 3pid verification from Sydent generally. (Sydent is the identity server
  2542. project that backs the <code>vector.im</code> and <code>matrix.org</code> instances).</p>
  2543. <h3 id="email-1"><a class="header" href="#email-1">Email</a></h3>
  2544. <p>Following upgrade, to continue verifying email (e.g. as part of the
  2545. registration process), admins can either:-</p>
  2546. <ul>
  2547. <li>Configure Synapse to use an email server.</li>
  2548. <li>Run or choose an identity server which allows delegated email
  2549. verification and delegate to it.</li>
  2550. </ul>
  2551. <h4 id="configure-smtp-in-synapse"><a class="header" href="#configure-smtp-in-synapse">Configure SMTP in Synapse</a></h4>
  2552. <p>To configure an SMTP server for Synapse, modify the configuration
  2553. section headed <code>email</code>, and be sure to have at least the
  2554. <code>smtp_host, smtp_port</code> and <code>notif_from</code> fields filled out.</p>
  2555. <p>You may also need to set <code>smtp_user</code>, <code>smtp_pass</code>, and
  2556. <code>require_transport_security</code>.</p>
  2557. <p>See the <a href="usage/configuration/homeserver_sample_config.html">sample configuration file</a>
  2558. for more details on these settings.</p>
  2559. <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>
  2560. <p>Some admins will wish to continue using email verification as part of
  2561. the registration process, but will not immediately have an appropriate
  2562. SMTP server at hand.</p>
  2563. <p>To this end, we will continue to support email verification delegation
  2564. via the <code>vector.im</code> and <code>matrix.org</code> identity servers for two months.
  2565. Support for delegated email verification will be disabled on Monday 2nd
  2566. December.</p>
  2567. <p>The <code>account_threepid_delegates</code> dictionary defines whether the
  2568. homeserver should delegate an external server (typically an <a href="https://matrix.org/docs/spec/identity_service/r0.2.1">identity
  2569. server</a>) to handle
  2570. sending confirmation messages via email and SMS.</p>
  2571. <p>So to delegate email verification, in <code>homeserver.yaml</code>, set
  2572. <code>account_threepid_delegates.email</code> to the base URL of an identity
  2573. server. For example:</p>
  2574. <pre><code class="language-yaml">account_threepid_delegates:
  2575. email: https://example.com # Delegate email sending to example.com
  2576. </code></pre>
  2577. <p>Note that <code>account_threepid_delegates.email</code> replaces the deprecated
  2578. <code>email.trust_identity_server_for_password_resets</code>: if
  2579. <code>email.trust_identity_server_for_password_resets</code> is set to <code>true</code>, and
  2580. <code>account_threepid_delegates.email</code> is not set, then the first entry in
  2581. <code>trusted_third_party_id_servers</code> will be used as the
  2582. <code>account_threepid_delegate</code> for email. This is to ensure compatibility
  2583. with existing Synapse installs that set up external server handling for
  2584. these tasks before v1.4.0. If
  2585. <code>email.trust_identity_server_for_password_resets</code> is <code>true</code> and no
  2586. trusted identity server domains are configured, Synapse will report an
  2587. error and refuse to start.</p>
  2588. <p>If <code>email.trust_identity_server_for_password_resets</code> is <code>false</code> or
  2589. absent and no <code>email</code> delegate is configured in
  2590. <code>account_threepid_delegates</code>, then Synapse will send email verification
  2591. messages itself, using the configured SMTP server (see above). that
  2592. type.</p>
  2593. <h3 id="phone-numbers"><a class="header" href="#phone-numbers">Phone numbers</a></h3>
  2594. <p>Synapse does not support phone-number verification itself, so the only
  2595. way to maintain the ability for users to add phone numbers to their
  2596. accounts will be by continuing to delegate phone number verification to
  2597. the <code>matrix.org</code> and <code>vector.im</code> identity servers (or another identity
  2598. server that supports SMS sending).</p>
  2599. <p>The <code>account_threepid_delegates</code> dictionary defines whether the
  2600. homeserver should delegate an external server (typically an <a href="https://matrix.org/docs/spec/identity_service/r0.2.1">identity
  2601. server</a>) to handle
  2602. sending confirmation messages via email and SMS.</p>
  2603. <p>So to delegate phone number verification, in <code>homeserver.yaml</code>, set
  2604. <code>account_threepid_delegates.msisdn</code> to the base URL of an identity
  2605. server. For example:</p>
  2606. <pre><code class="language-yaml">account_threepid_delegates:
  2607. msisdn: https://example.com # Delegate sms sending to example.com
  2608. </code></pre>
  2609. <p>The <code>matrix.org</code> and <code>vector.im</code> identity servers will continue to
  2610. support delegated phone number verification via SMS until such time as
  2611. it is possible for admins to configure their servers to perform phone
  2612. number verification directly. More details will follow in a future
  2613. release.</p>
  2614. <h2 id="rolling-back-to-v131"><a class="header" href="#rolling-back-to-v131">Rolling back to v1.3.1</a></h2>
  2615. <p>If you encounter problems with v1.4.0, it should be possible to roll
  2616. back to v1.3.1, subject to the following:</p>
  2617. <ul>
  2618. <li>
  2619. <p>The 'room statistics' engine was heavily reworked in this release
  2620. (see <a href="https://github.com/matrix-org/synapse/pull/5971">#5971</a>),
  2621. including significant changes to the database schema, which are not
  2622. easily reverted. This will cause the room statistics engine to stop
  2623. updating when you downgrade.</p>
  2624. <p>The room statistics are essentially unused in v1.3.1 (in future
  2625. versions of Synapse, they will be used to populate the room
  2626. directory), so there should be no loss of functionality. However,
  2627. the statistics engine will write errors to the logs, which can be
  2628. avoided by setting the following in <code>homeserver.yaml</code>:</p>
  2629. <pre><code class="language-yaml">stats:
  2630. enabled: false
  2631. </code></pre>
  2632. <p>Don't forget to re-enable it when you upgrade again, in preparation
  2633. for its use in the room directory!</p>
  2634. </li>
  2635. </ul>
  2636. <h1 id="upgrading-to-v120"><a class="header" href="#upgrading-to-v120">Upgrading to v1.2.0</a></h1>
  2637. <p>Some counter metrics have been renamed, with the old names deprecated.
  2638. See <a href="metrics-howto.html#renaming-of-metrics--deprecation-of-old-names-in-12">the metrics
  2639. documentation</a>
  2640. for details.</p>
  2641. <h1 id="upgrading-to-v110"><a class="header" href="#upgrading-to-v110">Upgrading to v1.1.0</a></h1>
  2642. <p>Synapse v1.1.0 removes support for older Python and PostgreSQL versions,
  2643. as outlined in <a href="https://matrix.org/blog/2019/04/08/synapse-deprecating-postgres-9-4-and-python-2-x">our deprecation
  2644. notice</a>.</p>
  2645. <h2 id="minimum-python-version"><a class="header" href="#minimum-python-version">Minimum Python Version</a></h2>
  2646. <p>Synapse v1.1.0 has a minimum Python requirement of Python 3.5. Python
  2647. 3.6 or Python 3.7 are recommended as they have improved internal string
  2648. handling, significantly reducing memory usage.</p>
  2649. <p>If you use current versions of the Matrix.org-distributed Debian
  2650. packages or Docker images, action is not required.</p>
  2651. <p>If you install Synapse in a Python virtual environment, please see
  2652. &quot;Upgrading to v0.34.0&quot; for notes on setting up a new virtualenv under
  2653. Python 3.</p>
  2654. <h2 id="minimum-postgresql-version"><a class="header" href="#minimum-postgresql-version">Minimum PostgreSQL Version</a></h2>
  2655. <p>If using PostgreSQL under Synapse, you will need to use PostgreSQL 9.5
  2656. or above. Please see the <a href="https://www.postgresql.org/docs/11/upgrading.html">PostgreSQL
  2657. documentation</a> for
  2658. more details on upgrading your database.</p>
  2659. <h1 id="upgrading-to-v10"><a class="header" href="#upgrading-to-v10">Upgrading to v1.0</a></h1>
  2660. <h2 id="validation-of-tls-certificates"><a class="header" href="#validation-of-tls-certificates">Validation of TLS certificates</a></h2>
  2661. <p>Synapse v1.0 is the first release to enforce validation of TLS
  2662. certificates for the federation API. It is therefore essential that your
  2663. certificates are correctly configured.</p>
  2664. <p>Note, v1.0 installations will also no longer be able to federate with
  2665. servers that have not correctly configured their certificates.</p>
  2666. <p>In rare cases, it may be desirable to disable certificate checking: for
  2667. example, it might be essential to be able to federate with a given
  2668. legacy server in a closed federation. This can be done in one of two
  2669. ways:-</p>
  2670. <ul>
  2671. <li>Configure the global switch <code>federation_verify_certificates</code> to
  2672. <code>false</code>.</li>
  2673. <li>Configure a whitelist of server domains to trust via
  2674. <code>federation_certificate_verification_whitelist</code>.</li>
  2675. </ul>
  2676. <p>See the <a href="usage/configuration/homeserver_sample_config.html">sample configuration file</a>
  2677. for more details on these settings.</p>
  2678. <h2 id="email-2"><a class="header" href="#email-2">Email</a></h2>
  2679. <p>When a user requests a password reset, Synapse will send an email to the
  2680. user to confirm the request.</p>
  2681. <p>Previous versions of Synapse delegated the job of sending this email to
  2682. an identity server. If the identity server was somehow malicious or
  2683. became compromised, it would be theoretically possible to hijack an
  2684. account through this means.</p>
  2685. <p>Therefore, by default, Synapse v1.0 will send the confirmation email
  2686. itself. If Synapse is not configured with an SMTP server, password reset
  2687. via email will be disabled.</p>
  2688. <p>To configure an SMTP server for Synapse, modify the configuration
  2689. section headed <code>email</code>, and be sure to have at least the <code>smtp_host</code>,
  2690. <code>smtp_port</code> and <code>notif_from</code> fields filled out. You may also need to set
  2691. <code>smtp_user</code>, <code>smtp_pass</code>, and <code>require_transport_security</code>.</p>
  2692. <p>If you are absolutely certain that you wish to continue using an
  2693. identity server for password resets, set
  2694. <code>trust_identity_server_for_password_resets</code> to <code>true</code>.</p>
  2695. <p>See the <a href="usage/configuration/homeserver_sample_config.html">sample configuration file</a>
  2696. for more details on these settings.</p>
  2697. <h2 id="new-email-templates"><a class="header" href="#new-email-templates">New email templates</a></h2>
  2698. <p>Some new templates have been added to the default template directory for the purpose of
  2699. the homeserver sending its own password reset emails. If you have configured a
  2700. custom <code>template_dir</code> in your Synapse config, these files will need to be added.</p>
  2701. <p><code>password_reset.html</code> and <code>password_reset.txt</code> are HTML and plain text
  2702. templates respectively that contain the contents of what will be emailed
  2703. to the user upon attempting to reset their password via email.
  2704. <code>password_reset_success.html</code> and <code>password_reset_failure.html</code> are HTML
  2705. files that the content of which (assuming no redirect URL is set) will
  2706. be shown to the user after they attempt to click the link in the email
  2707. sent to them.</p>
  2708. <h1 id="upgrading-to-v0990"><a class="header" href="#upgrading-to-v0990">Upgrading to v0.99.0</a></h1>
  2709. <p>Please be aware that, before Synapse v1.0 is released around March 2019,
  2710. you will need to replace any self-signed certificates with those
  2711. verified by a root CA. Information on how to do so can be found at the
  2712. ACME docs.</p>
  2713. <h1 id="upgrading-to-v0340"><a class="header" href="#upgrading-to-v0340">Upgrading to v0.34.0</a></h1>
  2714. <ol>
  2715. <li>
  2716. <p>This release is the first to fully support Python 3. Synapse will
  2717. now run on Python versions 3.5, or 3.6 (as well as 2.7). We
  2718. recommend switching to Python 3, as it has been shown to give
  2719. performance improvements.</p>
  2720. <p>For users who have installed Synapse into a virtualenv, we recommend
  2721. doing this by creating a new virtualenv. For example:</p>
  2722. <pre><code class="language-sh">virtualenv -p python3 ~/synapse/env3
  2723. source ~/synapse/env3/bin/activate
  2724. pip install matrix-synapse
  2725. </code></pre>
  2726. <p>You can then start synapse as normal, having activated the new
  2727. virtualenv:</p>
  2728. <pre><code class="language-sh">cd ~/synapse
  2729. source env3/bin/activate
  2730. synctl start
  2731. </code></pre>
  2732. <p>Users who have installed from distribution packages should see the
  2733. relevant package documentation. See below for notes on Debian
  2734. packages.</p>
  2735. <ul>
  2736. <li>
  2737. <p>When upgrading to Python 3, you <strong>must</strong> make sure that your log
  2738. files are configured as UTF-8, by adding <code>encoding: utf8</code> to the
  2739. <code>RotatingFileHandler</code> configuration (if you have one) in your
  2740. <code>&lt;server&gt;.log.config</code> file. For example, if your <code>log.config</code>
  2741. file contains:</p>
  2742. <pre><code class="language-yaml">handlers:
  2743. file:
  2744. class: logging.handlers.RotatingFileHandler
  2745. formatter: precise
  2746. filename: homeserver.log
  2747. maxBytes: 104857600
  2748. backupCount: 10
  2749. filters: [context]
  2750. console:
  2751. class: logging.StreamHandler
  2752. formatter: precise
  2753. filters: [context]
  2754. </code></pre>
  2755. <p>Then you should update this to be:</p>
  2756. <pre><code class="language-yaml">handlers:
  2757. file:
  2758. class: logging.handlers.RotatingFileHandler
  2759. formatter: precise
  2760. filename: homeserver.log
  2761. maxBytes: 104857600
  2762. backupCount: 10
  2763. filters: [context]
  2764. encoding: utf8
  2765. console:
  2766. class: logging.StreamHandler
  2767. formatter: precise
  2768. filters: [context]
  2769. </code></pre>
  2770. <p>There is no need to revert this change if downgrading to
  2771. Python 2.</p>
  2772. </li>
  2773. </ul>
  2774. <p>We are also making available Debian packages which will run Synapse
  2775. on Python 3. You can switch to these packages with
  2776. <code>apt-get install matrix-synapse-py3</code>, however, please read
  2777. <a href="https://github.com/matrix-org/synapse/blob/release-v0.34.0/debian/NEWS">debian/NEWS</a>
  2778. before doing so. The existing <code>matrix-synapse</code> packages will
  2779. continue to use Python 2 for the time being.</p>
  2780. </li>
  2781. <li>
  2782. <p>This release removes the <code>riot.im</code> from the default list of trusted
  2783. identity servers.</p>
  2784. <p>If <code>riot.im</code> is in your homeserver's list of
  2785. <code>trusted_third_party_id_servers</code>, you should remove it. It was added
  2786. in case a hypothetical future identity server was put there. If you
  2787. don't remove it, users may be unable to deactivate their accounts.</p>
  2788. </li>
  2789. <li>
  2790. <p>This release no longer installs the (unmaintained) Matrix Console
  2791. web client as part of the default installation. It is possible to
  2792. re-enable it by installing it separately and setting the
  2793. <code>web_client_location</code> config option, but please consider switching
  2794. to another client.</p>
  2795. </li>
  2796. </ol>
  2797. <h1 id="upgrading-to-v0337"><a class="header" href="#upgrading-to-v0337">Upgrading to v0.33.7</a></h1>
  2798. <p>This release removes the example email notification templates from
  2799. <code>res/templates</code> (they are now internal to the python package). This
  2800. should only affect you if you (a) deploy your Synapse instance from a
  2801. git checkout or a github snapshot URL, and (b) have email notifications
  2802. enabled.</p>
  2803. <p>If you have email notifications enabled, you should ensure that
  2804. <code>email.template_dir</code> is either configured to point at a directory where
  2805. you have installed customised templates, or leave it unset to use the
  2806. default templates.</p>
  2807. <h1 id="upgrading-to-v0273"><a class="header" href="#upgrading-to-v0273">Upgrading to v0.27.3</a></h1>
  2808. <p>This release expands the anonymous usage stats sent if the opt-in
  2809. <code>report_stats</code> configuration is set to <code>true</code>. We now capture RSS memory
  2810. and cpu use at a very coarse level. This requires administrators to
  2811. install the optional <code>psutil</code> python module.</p>
  2812. <p>We would appreciate it if you could assist by ensuring this module is
  2813. available and <code>report_stats</code> is enabled. This will let us see if
  2814. performance changes to synapse are having an impact to the general
  2815. community.</p>
  2816. <h1 id="upgrading-to-v0150"><a class="header" href="#upgrading-to-v0150">Upgrading to v0.15.0</a></h1>
  2817. <p>If you want to use the new URL previewing API
  2818. (<code>/_matrix/media/r0/preview_url</code>) then you have to explicitly enable it
  2819. in the config and update your dependencies dependencies. See README.rst
  2820. for details.</p>
  2821. <h1 id="upgrading-to-v0110"><a class="header" href="#upgrading-to-v0110">Upgrading to v0.11.0</a></h1>
  2822. <p>This release includes the option to send anonymous usage stats to
  2823. matrix.org, and requires that administrators explictly opt in or out by
  2824. setting the <code>report_stats</code> option to either <code>true</code> or <code>false</code>.</p>
  2825. <p>We would really appreciate it if you could help our project out by
  2826. reporting anonymized usage statistics from your homeserver. Only very
  2827. basic aggregate data (e.g. number of users) will be reported, but it
  2828. helps us to track the growth of the Matrix community, and helps us to
  2829. make Matrix a success, as well as to convince other networks that they
  2830. should peer with us.</p>
  2831. <h1 id="upgrading-to-v090"><a class="header" href="#upgrading-to-v090">Upgrading to v0.9.0</a></h1>
  2832. <p>Application services have had a breaking API change in this version.</p>
  2833. <p>They can no longer register themselves with a home server using the AS
  2834. HTTP API. This decision was made because a compromised application
  2835. service with free reign to register any regex in effect grants full
  2836. read/write access to the home server if a regex of <code>.*</code> is used. An
  2837. attack where a compromised AS re-registers itself with <code>.*</code> was deemed
  2838. too big of a security risk to ignore, and so the ability to register
  2839. with the HS remotely has been removed.</p>
  2840. <p>It has been replaced by specifying a list of application service
  2841. registrations in <code>homeserver.yaml</code>:</p>
  2842. <pre><code class="language-yaml">app_service_config_files: [&quot;registration-01.yaml&quot;, &quot;registration-02.yaml&quot;]
  2843. </code></pre>
  2844. <p>Where <code>registration-01.yaml</code> looks like:</p>
  2845. <pre><code class="language-yaml">url: &lt;String&gt; # e.g. &quot;https://my.application.service.com&quot;
  2846. as_token: &lt;String&gt;
  2847. hs_token: &lt;String&gt;
  2848. sender_localpart: &lt;String&gt; # This is a new field which denotes the user_id localpart when using the AS token
  2849. namespaces:
  2850. users:
  2851. - exclusive: &lt;Boolean&gt;
  2852. regex: &lt;String&gt; # e.g. &quot;@prefix_.*&quot;
  2853. aliases:
  2854. - exclusive: &lt;Boolean&gt;
  2855. regex: &lt;String&gt;
  2856. rooms:
  2857. - exclusive: &lt;Boolean&gt;
  2858. regex: &lt;String&gt;
  2859. </code></pre>
  2860. <h1 id="upgrading-to-v080"><a class="header" href="#upgrading-to-v080">Upgrading to v0.8.0</a></h1>
  2861. <p>Servers which use captchas will need to add their public key to:</p>
  2862. <pre><code>static/client/register/register_config.js
  2863. window.matrixRegistrationConfig = {
  2864. recaptcha_public_key: &quot;YOUR_PUBLIC_KEY&quot;
  2865. };
  2866. </code></pre>
  2867. <p>This is required in order to support registration fallback (typically
  2868. used on mobile devices).</p>
  2869. <h1 id="upgrading-to-v070"><a class="header" href="#upgrading-to-v070">Upgrading to v0.7.0</a></h1>
  2870. <p>New dependencies are:</p>
  2871. <ul>
  2872. <li>pydenticon</li>
  2873. <li>simplejson</li>
  2874. <li>syutil</li>
  2875. <li>matrix-angular-sdk</li>
  2876. </ul>
  2877. <p>To pull in these dependencies in a virtual env, run:</p>
  2878. <pre><code>python synapse/python_dependencies.py | xargs -n 1 pip install
  2879. </code></pre>
  2880. <h1 id="upgrading-to-v060"><a class="header" href="#upgrading-to-v060">Upgrading to v0.6.0</a></h1>
  2881. <p>To pull in new dependencies, run:</p>
  2882. <pre><code>python setup.py develop --user
  2883. </code></pre>
  2884. <p>This update includes a change to the database schema. To upgrade you
  2885. first need to upgrade the database by running:</p>
  2886. <pre><code>python scripts/upgrade_db_to_v0.6.0.py &lt;db&gt; &lt;server_name&gt; &lt;signing_key&gt;
  2887. </code></pre>
  2888. <p>Where <code>&lt;db&gt;</code> is the location of the database,
  2889. <code>&lt;server_name&gt;</code> is the server name as specified in the
  2890. synapse configuration, and <code>&lt;signing_key&gt;</code> is the location
  2891. of the signing key as specified in the synapse configuration.</p>
  2892. <p>This may take some time to complete. Failures of signatures and content
  2893. hashes can safely be ignored.</p>
  2894. <h1 id="upgrading-to-v051"><a class="header" href="#upgrading-to-v051">Upgrading to v0.5.1</a></h1>
  2895. <p>Depending on precisely when you installed v0.5.0 you may have ended up
  2896. with a stale release of the reference matrix webclient installed as a
  2897. python module. To uninstall it and ensure you are depending on the
  2898. latest module, please run:</p>
  2899. <pre><code>$ pip uninstall syweb
  2900. </code></pre>
  2901. <h1 id="upgrading-to-v050"><a class="header" href="#upgrading-to-v050">Upgrading to v0.5.0</a></h1>
  2902. <p>The webclient has been split out into a seperate repository/pacakage in
  2903. this release. Before you restart your homeserver you will need to pull
  2904. in the webclient package by running:</p>
  2905. <pre><code>python setup.py develop --user
  2906. </code></pre>
  2907. <p>This release completely changes the database schema and so requires
  2908. upgrading it before starting the new version of the homeserver.</p>
  2909. <p>The script &quot;database-prepare-for-0.5.0.sh&quot; should be used to upgrade
  2910. the database. This will save all user information, such as logins and
  2911. profiles, but will otherwise purge the database. This includes messages,
  2912. which rooms the home server was a member of and room alias mappings.</p>
  2913. <p>If you would like to keep your history, please take a copy of your
  2914. database file and ask for help in #matrix:matrix.org. The upgrade
  2915. process is, unfortunately, non trivial and requires human intervention
  2916. to resolve any resulting conflicts during the upgrade process.</p>
  2917. <p>Before running the command the homeserver should be first completely
  2918. shutdown. To run it, simply specify the location of the database, e.g.:</p>
  2919. <blockquote>
  2920. <p>./scripts/database-prepare-for-0.5.0.sh &quot;homeserver.db&quot;</p>
  2921. </blockquote>
  2922. <p>Once this has successfully completed it will be safe to restart the
  2923. homeserver. You may notice that the homeserver takes a few seconds
  2924. longer to restart than usual as it reinitializes the database.</p>
  2925. <p>On startup of the new version, users can either rejoin remote rooms
  2926. using room aliases or by being reinvited. Alternatively, if any other
  2927. homeserver sends a message to a room that the homeserver was previously
  2928. in the local HS will automatically rejoin the room.</p>
  2929. <h1 id="upgrading-to-v040"><a class="header" href="#upgrading-to-v040">Upgrading to v0.4.0</a></h1>
  2930. <p>This release needs an updated syutil version. Run:</p>
  2931. <pre><code>python setup.py develop
  2932. </code></pre>
  2933. <p>You will also need to upgrade your configuration as the signing key
  2934. format has changed. Run:</p>
  2935. <pre><code>python -m synapse.app.homeserver --config-path &lt;CONFIG&gt; --generate-config
  2936. </code></pre>
  2937. <h1 id="upgrading-to-v030"><a class="header" href="#upgrading-to-v030">Upgrading to v0.3.0</a></h1>
  2938. <p>This registration API now closely matches the login API. This introduces
  2939. a bit more backwards and forwards between the HS and the client, but
  2940. this improves the overall flexibility of the API. You can now GET on
  2941. /register to retrieve a list of valid registration flows. Upon choosing
  2942. one, they are submitted in the same way as login, e.g:</p>
  2943. <pre><code>{
  2944. type: m.login.password,
  2945. user: foo,
  2946. password: bar
  2947. }
  2948. </code></pre>
  2949. <p>The default HS supports 2 flows, with and without Identity Server email
  2950. authentication. Enabling captcha on the HS will add in an extra step to
  2951. all flows: <code>m.login.recaptcha</code> which must be completed before you can
  2952. transition to the next stage. There is a new login type:
  2953. <code>m.login.email.identity</code> which contains the <code>threepidCreds</code> key which
  2954. were previously sent in the original register request. For more
  2955. information on this, see the specification.</p>
  2956. <h2 id="web-client"><a class="header" href="#web-client">Web Client</a></h2>
  2957. <p>The VoIP specification has changed between v0.2.0 and v0.3.0. Users
  2958. should refresh any browser tabs to get the latest web client code. Users
  2959. on v0.2.0 of the web client will not be able to call those on v0.3.0 and
  2960. vice versa.</p>
  2961. <h1 id="upgrading-to-v020"><a class="header" href="#upgrading-to-v020">Upgrading to v0.2.0</a></h1>
  2962. <p>The home server now requires setting up of SSL config before it can run.
  2963. To automatically generate default config use:</p>
  2964. <pre><code>$ python synapse/app/homeserver.py \
  2965. --server-name machine.my.domain.name \
  2966. --bind-port 8448 \
  2967. --config-path homeserver.config \
  2968. --generate-config
  2969. </code></pre>
  2970. <p>This config can be edited if desired, for example to specify a different
  2971. SSL certificate to use. Once done you can run the home server using:</p>
  2972. <pre><code>$ python synapse/app/homeserver.py --config-path homeserver.config
  2973. </code></pre>
  2974. <p>See the README.rst for more information.</p>
  2975. <p>Also note that some config options have been renamed, including:</p>
  2976. <ul>
  2977. <li>&quot;host&quot; to &quot;server-name&quot;</li>
  2978. <li>&quot;database&quot; to &quot;database-path&quot;</li>
  2979. <li>&quot;port&quot; to &quot;bind-port&quot; and &quot;unsecure-port&quot;</li>
  2980. </ul>
  2981. <h1 id="upgrading-to-v001"><a class="header" href="#upgrading-to-v001">Upgrading to v0.0.1</a></h1>
  2982. <p>This release completely changes the database schema and so requires
  2983. upgrading it before starting the new version of the homeserver.</p>
  2984. <p>The script &quot;database-prepare-for-0.0.1.sh&quot; should be used to upgrade
  2985. the database. This will save all user information, such as logins and
  2986. profiles, but will otherwise purge the database. This includes messages,
  2987. which rooms the home server was a member of and room alias mappings.</p>
  2988. <p>Before running the command the homeserver should be first completely
  2989. shutdown. To run it, simply specify the location of the database, e.g.:</p>
  2990. <blockquote>
  2991. <p>./scripts/database-prepare-for-0.0.1.sh &quot;homeserver.db&quot;</p>
  2992. </blockquote>
  2993. <p>Once this has successfully completed it will be safe to restart the
  2994. homeserver. You may notice that the homeserver takes a few seconds
  2995. longer to restart than usual as it reinitializes the database.</p>
  2996. <p>On startup of the new version, users can either rejoin remote rooms
  2997. using room aliases or by being reinvited. Alternatively, if any other
  2998. homeserver sends a message to a room that the homeserver was previously
  2999. in the local HS will automatically rejoin the room.</p>
  3000. <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>
  3001. <p>Federation is the process by which users on different servers can participate
  3002. in the same room. For this to work, those other servers must be able to contact
  3003. yours to send messages.</p>
  3004. <p>The <code>server_name</code> configured in the Synapse configuration file (often
  3005. <code>homeserver.yaml</code>) defines how resources (users, rooms, etc.) will be
  3006. identified (eg: <code>@user:example.com</code>, <code>#room:example.com</code>). By default,
  3007. it is also the domain that other servers will use to try to reach your
  3008. server (via port 8448). This is easy to set up and will work provided
  3009. you set the <code>server_name</code> to match your machine's public DNS hostname.</p>
  3010. <p>For this default configuration to work, you will need to listen for TLS
  3011. connections on port 8448. The preferred way to do that is by using a
  3012. reverse proxy: see <a href="reverse_proxy.html">the reverse proxy documentation</a> for instructions
  3013. on how to correctly set one up.</p>
  3014. <p>In some cases you might not want to run Synapse on the machine that has
  3015. the <code>server_name</code> as its public DNS hostname, or you might want federation
  3016. traffic to use a different port than 8448. For example, you might want to
  3017. have your user names look like <code>@user:example.com</code>, but you want to run
  3018. Synapse on <code>synapse.example.com</code> on port 443. This can be done using
  3019. delegation, which allows an admin to control where federation traffic should
  3020. be sent. See <a href="delegate.html">the delegation documentation</a> for instructions on how to set this up.</p>
  3021. <p>Once federation has been configured, you should be able to join a room over
  3022. federation. A good place to start is <code>#synapse:matrix.org</code> - a room for
  3023. Synapse admins.</p>
  3024. <h2 id="troubleshooting-3"><a class="header" href="#troubleshooting-3">Troubleshooting</a></h2>
  3025. <p>You can use the <a href="https://matrix.org/federationtester">federation tester</a>
  3026. to check if your homeserver is configured correctly. Alternatively try the
  3027. <a href="https://matrix.org/federationtester/api/report?server_name=DOMAIN">JSON API used by the federation tester</a>.
  3028. Note that you'll have to modify this URL to replace <code>DOMAIN</code> with your
  3029. <code>server_name</code>. Hitting the API directly provides extra detail.</p>
  3030. <p>The typical failure mode for federation is that when the server tries to join
  3031. a room, it is rejected with &quot;401: Unauthorized&quot;. Generally this means that other
  3032. servers in the room could not access yours. (Joining a room over federation is
  3033. a complicated dance which requires connections in both directions).</p>
  3034. <p>Another common problem is that people on other servers can't join rooms that
  3035. you invite them to. This can be caused by an incorrectly-configured reverse
  3036. proxy: see <a href="reverse_proxy.html">the reverse proxy documentation</a> for instructions on how
  3037. to correctly configure a reverse proxy.</p>
  3038. <h3 id="known-issues"><a class="header" href="#known-issues">Known issues</a></h3>
  3039. <p><strong>HTTP <code>308 Permanent Redirect</code> redirects are not followed</strong>: Due to missing features
  3040. in the HTTP library used by Synapse, 308 redirects are currently not followed by
  3041. federating servers, which can cause <code>M_UNKNOWN</code> or <code>401 Unauthorized</code> errors. This
  3042. may affect users who are redirecting apex-to-www (e.g. <code>example.com</code> -&gt; <code>www.example.com</code>),
  3043. and especially users of the Kubernetes <em>Nginx Ingress</em> module, which uses 308 redirect
  3044. codes by default. For those Kubernetes users, <a href="https://stackoverflow.com/a/52617528/5096871">this Stackoverflow post</a>
  3045. might be helpful. For other users, switching to a <code>301 Moved Permanently</code> code may be
  3046. an option. 308 redirect codes will be supported properly in a future
  3047. release of Synapse.</p>
  3048. <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>
  3049. <p>If you want to get up and running quickly with a trio of homeservers in a
  3050. private federation, there is a script in the <code>demo</code> directory. This is mainly
  3051. useful just for development purposes. See
  3052. <a href="https://matrix-org.github.io/synapse/develop/development/demo.html">demo scripts</a>.</p>
  3053. <div style="break-before: page; page-break-before: always;"></div><h1 id="configuration-1"><a class="header" href="#configuration-1">Configuration</a></h1>
  3054. <p>This section contains information on tweaking Synapse via the various options in the configuration file. A configuration
  3055. file should have been generated when you <a href="usage/configuration/../../setup/installation.html">installed Synapse</a>.</p>
  3056. <div style="break-before: page; page-break-before: always;"></div><h1 id="configuring-synapse"><a class="header" href="#configuring-synapse">Configuring Synapse</a></h1>
  3057. <p>This is intended as a guide to the Synapse configuration. The behavior of a Synapse instance can be modified
  3058. through the many configuration settings documented here — each config option is explained,
  3059. including what the default is, how to change the default and what sort of behaviour the setting governs.
  3060. Also included is an example configuration for each setting. If you don't want to spend a lot of time
  3061. thinking about options, the config as generated sets sensible defaults for all values. Do note however that the
  3062. database defaults to SQLite, which is not recommended for production usage. You can read more on this subject
  3063. <a href="usage/configuration/../../setup/installation.html#using-postgresql">here</a>.</p>
  3064. <h2 id="config-conventions"><a class="header" href="#config-conventions">Config Conventions</a></h2>
  3065. <p>Configuration options that take a time period can be set using a number
  3066. followed by a letter. Letters have the following meanings:</p>
  3067. <ul>
  3068. <li><code>s</code> = second</li>
  3069. <li><code>m</code> = minute</li>
  3070. <li><code>h</code> = hour</li>
  3071. <li><code>d</code> = day</li>
  3072. <li><code>w</code> = week</li>
  3073. <li><code>y</code> = year</li>
  3074. </ul>
  3075. <p>For example, setting <code>redaction_retention_period: 5m</code> would remove redacted
  3076. messages from the database after 5 minutes, rather than 5 months.</p>
  3077. <p>In addition, configuration options referring to size use the following suffixes:</p>
  3078. <ul>
  3079. <li><code>M</code> = MiB, or 1,048,576 bytes</li>
  3080. <li><code>K</code> = KiB, or 1024 bytes </li>
  3081. </ul>
  3082. <p>For example, setting <code>max_avatar_size: 10M</code> means that Synapse will not accept files larger than 10,485,760 bytes
  3083. for a user avatar. </p>
  3084. <h3 id="yaml"><a class="header" href="#yaml">YAML</a></h3>
  3085. <p>The configuration file is a <a href="https://yaml.org/">YAML</a> file, which means that certain syntax rules
  3086. apply if you want your config file to be read properly. A few helpful things to know:</p>
  3087. <ul>
  3088. <li>
  3089. <p><code>#</code> before any option in the config will comment out that setting and either a default (if available) will
  3090. be applied or Synapse will ignore the setting. Thus, in example #1 below, the setting will be read and
  3091. applied, but in example #2 the setting will not be read and a default will be applied.</p>
  3092. <p>Example #1:</p>
  3093. <pre><code class="language-yaml">pid_file: DATADIR/homeserver.pid
  3094. </code></pre>
  3095. <p>Example #2:</p>
  3096. <pre><code class="language-yaml">#pid_file: DATADIR/homeserver.pid
  3097. </code></pre>
  3098. </li>
  3099. <li>
  3100. <p>Indentation matters! The indentation before a setting
  3101. will determine whether a given setting is read as part of another
  3102. setting, or considered on its own. Thus, in example #1, the <code>enabled</code> setting
  3103. is read as a sub-option of the <code>presence</code> setting, and will be properly applied.</p>
  3104. <p>However, the lack of indentation before the <code>enabled</code> setting in example #2 means
  3105. that when reading the config, Synapse will consider both <code>presence</code> and <code>enabled</code> as
  3106. different settings. In this case, <code>presence</code> has no value, and thus a default applied, and <code>enabled</code>
  3107. is an option that Synapse doesn't recognize and thus ignores.</p>
  3108. <p>Example #1: </p>
  3109. <pre><code class="language-yaml">presence:
  3110. enabled: false
  3111. </code></pre>
  3112. <p>Example #2:</p>
  3113. <pre><code class="language-yaml">presence:
  3114. enabled: false
  3115. </code></pre>
  3116. <p>In this manual, all top-level settings (ones with no indentation) are identified
  3117. at the beginning of their section (i.e. &quot;Config option: <code>example_setting</code>&quot;) and
  3118. the sub-options, if any, are identified and listed in the body of the section.
  3119. In addition, each setting has an example of its usage, with the proper indentation
  3120. shown. </p>
  3121. </li>
  3122. </ul>
  3123. <h2 id="contents"><a class="header" href="#contents">Contents</a></h2>
  3124. <p><a href="usage/configuration/config_documentation.html#modules">Modules</a></p>
  3125. <p><a href="usage/configuration/config_documentation.html#server">Server</a></p>
  3126. <p><a href="usage/configuration/config_documentation.html#homeserver-blocking">Homeserver Blocking</a></p>
  3127. <p><a href="usage/configuration/config_documentation.html#tls">TLS</a></p>
  3128. <p><a href="usage/configuration/config_documentation.html#federation">Federation</a></p>
  3129. <p><a href="usage/configuration/config_documentation.html#caching">Caching</a></p>
  3130. <p><a href="usage/configuration/config_documentation.html#database">Database</a></p>
  3131. <p><a href="usage/configuration/config_documentation.html#logging">Logging</a></p>
  3132. <p><a href="usage/configuration/config_documentation.html#ratelimiting">Ratelimiting</a></p>
  3133. <p><a href="usage/configuration/config_documentation.html#media-store">Media Store</a></p>
  3134. <p><a href="usage/configuration/config_documentation.html#captcha">Captcha</a></p>
  3135. <p><a href="usage/configuration/config_documentation.html#turn">TURN</a></p>
  3136. <p><a href="usage/configuration/config_documentation.html#registration">Registration</a></p>
  3137. <p><a href="usage/configuration/config_documentation.html#api-configuration">API Configuration</a></p>
  3138. <p><a href="usage/configuration/config_documentation.html#signing-keys">Signing Keys</a></p>
  3139. <p><a href="usage/configuration/config_documentation.html#single-sign-on-integration">Single Sign On Integration</a></p>
  3140. <p><a href="usage/configuration/config_documentation.html#push">Push</a></p>
  3141. <p><a href="usage/configuration/config_documentation.html#rooms">Rooms</a></p>
  3142. <p><a href="usage/configuration/config_documentation.html#opentracing">Opentracing</a></p>
  3143. <p><a href="usage/configuration/config_documentation.html#workers">Workers</a></p>
  3144. <p><a href="usage/configuration/config_documentation.html#background-updates">Background Updates</a></p>
  3145. <h2 id="modules"><a class="header" href="#modules">Modules</a></h2>
  3146. <p>Server admins can expand Synapse's functionality with external modules.</p>
  3147. <p>See <a href="usage/configuration/../../modules/index.html">here</a> for more
  3148. documentation on how to configure or create custom modules for Synapse.</p>
  3149. <hr />
  3150. <p>Config option: <code>modules</code></p>
  3151. <p>Use the <code>module</code> sub-option to add modules under this option to extend functionality.
  3152. The <code>module</code> setting then has a sub-option, <code>config</code>, which can be used to define some configuration
  3153. for the <code>module</code>.</p>
  3154. <p>Defaults to none.</p>
  3155. <p>Example configuration:</p>
  3156. <pre><code class="language-yaml">modules:
  3157. - module: my_super_module.MySuperClass
  3158. config:
  3159. do_thing: true
  3160. - module: my_other_super_module.SomeClass
  3161. config: {}
  3162. </code></pre>
  3163. <hr />
  3164. <h2 id="server"><a class="header" href="#server">Server</a></h2>
  3165. <p>Define your homeserver name and other base options.</p>
  3166. <hr />
  3167. <p>Config option: <code>server_name</code></p>
  3168. <p>This sets the public-facing domain of the server.</p>
  3169. <p>The <code>server_name</code> name will appear at the end of usernames and room addresses
  3170. created on your server. For example if the <code>server_name</code> was example.com,
  3171. usernames on your server would be in the format <code>@user:example.com</code></p>
  3172. <p>In most cases you should avoid using a matrix specific subdomain such as
  3173. matrix.example.com or synapse.example.com as the <code>server_name</code> for the same
  3174. reasons you wouldn't use user@email.example.com as your email address.
  3175. See <a href="usage/configuration/../../delegate.html">here</a>
  3176. for information on how to host Synapse on a subdomain while preserving
  3177. a clean <code>server_name</code>.</p>
  3178. <p>The <code>server_name</code> cannot be changed later so it is important to
  3179. configure this correctly before you start Synapse. It should be all
  3180. lowercase and may contain an explicit port.</p>
  3181. <p>There is no default for this option. </p>
  3182. <p>Example configuration #1:</p>
  3183. <pre><code class="language-yaml">server_name: matrix.org
  3184. </code></pre>
  3185. <p>Example configuration #2:</p>
  3186. <pre><code class="language-yaml">server_name: localhost:8080
  3187. </code></pre>
  3188. <hr />
  3189. <p>Config option: <code>pid_file</code></p>
  3190. <p>When running Synapse as a daemon, the file to store the pid in. Defaults to none.</p>
  3191. <p>Example configuration:</p>
  3192. <pre><code class="language-yaml">pid_file: DATADIR/homeserver.pid
  3193. </code></pre>
  3194. <hr />
  3195. <p>Config option: <code>web_client_location</code></p>
  3196. <p>The absolute URL to the web client which <code>/</code> will redirect to. Defaults to none. </p>
  3197. <p>Example configuration:</p>
  3198. <pre><code class="language-yaml">web_client_location: https://riot.example.com/
  3199. </code></pre>
  3200. <hr />
  3201. <p>Config option: <code>public_baseurl</code></p>
  3202. <p>The public-facing base URL that clients use to access this Homeserver (not
  3203. including _matrix/...). This is the same URL a user might enter into the
  3204. 'Custom Homeserver URL' field on their client. If you use Synapse with a
  3205. reverse proxy, this should be the URL to reach Synapse via the proxy.
  3206. Otherwise, it should be the URL to reach Synapse's client HTTP listener (see
  3207. 'listeners' below).</p>
  3208. <p>Defaults to <code>https://&lt;server_name&gt;/</code>.</p>
  3209. <p>Example configuration:</p>
  3210. <pre><code class="language-yaml">public_baseurl: https://example.com/
  3211. </code></pre>
  3212. <hr />
  3213. <p>Config option: <code>serve_server_wellknown</code></p>
  3214. <p>By default, other servers will try to reach our server on port 8448, which can
  3215. be inconvenient in some environments.</p>
  3216. <p>Provided <code>https://&lt;server_name&gt;/</code> on port 443 is routed to Synapse, this
  3217. option configures Synapse to serve a file at <code>https://&lt;server_name&gt;/.well-known/matrix/server</code>.
  3218. This will tell other servers to send traffic to port 443 instead.</p>
  3219. <p>This option currently defaults to false.</p>
  3220. <p>See https://matrix-org.github.io/synapse/latest/delegate.html for more
  3221. information.</p>
  3222. <p>Example configuration:</p>
  3223. <pre><code class="language-yaml">serve_server_wellknown: true
  3224. </code></pre>
  3225. <hr />
  3226. <p>Config option: <code>soft_file_limit</code></p>
  3227. <p>Set the soft limit on the number of file descriptors synapse can use.
  3228. Zero is used to indicate synapse should set the soft limit to the hard limit.
  3229. Defaults to 0. </p>
  3230. <p>Example configuration:</p>
  3231. <pre><code class="language-yaml">soft_file_limit: 3
  3232. </code></pre>
  3233. <hr />
  3234. <p>Config option: <code>presence</code></p>
  3235. <p>Presence tracking allows users to see the state (e.g online/offline)
  3236. of other local and remote users. Set the <code>enabled</code> sub-option to false to<br />
  3237. disable presence tracking on this homeserver. Defaults to true.
  3238. This option replaces the previous top-level 'use_presence' option.</p>
  3239. <p>Example configuration:</p>
  3240. <pre><code class="language-yaml">presence:
  3241. enabled: false
  3242. </code></pre>
  3243. <hr />
  3244. <p>Config option: <code>require_auth_for_profile_requests</code></p>
  3245. <p>Whether to require authentication to retrieve profile data (avatars, display names) of other
  3246. users through the client API. Defaults to false. Note that profile data is also available
  3247. via the federation API, unless <code>allow_profile_lookup_over_federation</code> is set to false.</p>
  3248. <p>Example configuration:</p>
  3249. <pre><code class="language-yaml">require_auth_for_profile_requests: true
  3250. </code></pre>
  3251. <hr />
  3252. <p>Config option: <code>limit_profile_requests_to_users_who_share_rooms</code></p>
  3253. <p>Use this option to require a user to share a room with another user in order
  3254. to retrieve their profile information. Only checked on Client-Server
  3255. requests. Profile requests from other servers should be checked by the
  3256. requesting server. Defaults to false.</p>
  3257. <p>Example configuration: </p>
  3258. <pre><code class="language-yaml">limit_profile_requests_to_users_who_share_rooms: true
  3259. </code></pre>
  3260. <hr />
  3261. <p>Config option: <code>include_profile_data_on_invite</code></p>
  3262. <p>Use this option to prevent a user's profile data from being retrieved and
  3263. displayed in a room until they have joined it. By default, a user's
  3264. profile data is included in an invite event, regardless of the values
  3265. of the above two settings, and whether or not the users share a server.
  3266. Defaults to true.</p>
  3267. <p>Example configuration:</p>
  3268. <pre><code class="language-yaml">include_profile_data_on_invite: false
  3269. </code></pre>
  3270. <hr />
  3271. <p>Config option: <code>allow_public_rooms_without_auth</code></p>
  3272. <p>If set to true, removes the need for authentication to access the server's
  3273. public rooms directory through the client API, meaning that anyone can
  3274. query the room directory. Defaults to false.</p>
  3275. <p>Example configuration:</p>
  3276. <pre><code class="language-yaml">allow_public_rooms_without_auth: true
  3277. </code></pre>
  3278. <hr />
  3279. <p>Config option: <code>allow_public_rooms_without_auth</code></p>
  3280. <p>If set to true, allows any other homeserver to fetch the server's public
  3281. rooms directory via federation. Defaults to false.</p>
  3282. <p>Example configuration:</p>
  3283. <pre><code class="language-yaml">allow_public_rooms_over_federation: true
  3284. </code></pre>
  3285. <hr />
  3286. <p>Config option: <code>default_room_version</code></p>
  3287. <p>The default room version for newly created rooms on this server.</p>
  3288. <p>Known room versions are listed <a href="https://spec.matrix.org/latest/rooms/#complete-list-of-room-versions">here</a></p>
  3289. <p>For example, for room version 1, <code>default_room_version</code> should be set
  3290. to &quot;1&quot;. </p>
  3291. <p>Currently defaults to &quot;9&quot;.</p>
  3292. <p>Example configuration:</p>
  3293. <pre><code class="language-yaml">default_room_version: &quot;8&quot;
  3294. </code></pre>
  3295. <hr />
  3296. <p>Config option: <code>gc_thresholds</code></p>
  3297. <p>The garbage collection threshold parameters to pass to <code>gc.set_threshold</code>, if defined.
  3298. Defaults to none. </p>
  3299. <p>Example configuration:</p>
  3300. <pre><code class="language-yaml">gc_thresholds: [700, 10, 10]
  3301. </code></pre>
  3302. <hr />
  3303. <p>Config option: <code>gc_min_interval</code></p>
  3304. <p>The minimum time in seconds between each GC for a generation, regardless of
  3305. the GC thresholds. This ensures that we don't do GC too frequently. A value of <code>[1s, 10s, 30s]</code>
  3306. indicates that a second must pass between consecutive generation 0 GCs, etc.</p>
  3307. <p>Defaults to <code>[1s, 10s, 30s]</code>.</p>
  3308. <p>Example configuration:</p>
  3309. <pre><code class="language-yaml">gc_min_interval: [0.5s, 30s, 1m]
  3310. </code></pre>
  3311. <hr />
  3312. <p>Config option: <code>filter_timeline_limit</code></p>
  3313. <p>Set the limit on the returned events in the timeline in the get
  3314. and sync operations. Defaults to 100. A value of -1 means no upper limit.</p>
  3315. <p>Example configuration:</p>
  3316. <pre><code class="language-yaml">filter_timeline_limit: 5000
  3317. </code></pre>
  3318. <hr />
  3319. <p>Config option: <code>block_non_admin_invites</code></p>
  3320. <p>Whether room invites to users on this server should be blocked
  3321. (except those sent by local server admins). Defaults to false.</p>
  3322. <p>Example configuration:</p>
  3323. <pre><code class="language-yaml">block_non_admin_invites: true
  3324. </code></pre>
  3325. <hr />
  3326. <p>Config option: <code>enable_search</code></p>
  3327. <p>If set to false, new messages will not be indexed for searching and users
  3328. will receive errors when searching for messages. Defaults to true.</p>
  3329. <p>Example configuration:</p>
  3330. <pre><code class="language-yaml">enable_search: false
  3331. </code></pre>
  3332. <hr />
  3333. <p>Config option: <code>ip_range_blacklist</code></p>
  3334. <p>This option prevents outgoing requests from being sent to the specified blacklisted IP address
  3335. CIDR ranges. If this option is not specified then it defaults to private IP
  3336. address ranges (see the example below).</p>
  3337. <p>The blacklist applies to the outbound requests for federation, identity servers,
  3338. push servers, and for checking key validity for third-party invite events.</p>
  3339. <p>(0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
  3340. listed here, since they correspond to unroutable addresses.)</p>
  3341. <p>This option replaces <code>federation_ip_range_blacklist</code> in Synapse v1.25.0.</p>
  3342. <p>Note: The value is ignored when an HTTP proxy is in use.</p>
  3343. <p>Example configuration:</p>
  3344. <pre><code class="language-yaml">ip_range_blacklist:
  3345. - '127.0.0.0/8'
  3346. - '10.0.0.0/8'
  3347. - '172.16.0.0/12'
  3348. - '192.168.0.0/16'
  3349. - '100.64.0.0/10'
  3350. - '192.0.0.0/24'
  3351. - '169.254.0.0/16'
  3352. - '192.88.99.0/24'
  3353. - '198.18.0.0/15'
  3354. - '192.0.2.0/24'
  3355. - '198.51.100.0/24'
  3356. - '203.0.113.0/24'
  3357. - '224.0.0.0/4'
  3358. - '::1/128'
  3359. - 'fe80::/10'
  3360. - 'fc00::/7'
  3361. - '2001:db8::/32'
  3362. - 'ff00::/8'
  3363. - 'fec0::/10'
  3364. </code></pre>
  3365. <hr />
  3366. <p>Config option: <code>ip_range_whitelist</code></p>
  3367. <p>List of IP address CIDR ranges that should be allowed for federation,
  3368. identity servers, push servers, and for checking key validity for
  3369. third-party invite events. This is useful for specifying exceptions to
  3370. wide-ranging blacklisted target IP ranges - e.g. for communication with
  3371. a push server only visible in your network.</p>
  3372. <p>This whitelist overrides <code>ip_range_blacklist</code> and defaults to an empty
  3373. list.</p>
  3374. <p>Example configuration:</p>
  3375. <pre><code class="language-yaml">ip_range_whitelist:
  3376. - '192.168.1.1'
  3377. </code></pre>
  3378. <hr />
  3379. <p>Config option: <code>listeners</code></p>
  3380. <p>List of ports that Synapse should listen on, their purpose and their
  3381. configuration.</p>
  3382. <p>Sub-options for each listener include:</p>
  3383. <ul>
  3384. <li>
  3385. <p><code>port</code>: the TCP port to bind to. </p>
  3386. </li>
  3387. <li>
  3388. <p><code>bind_addresses</code>: a list of local addresses to listen on. The default is
  3389. 'all local interfaces'.</p>
  3390. </li>
  3391. <li>
  3392. <p><code>type</code>: the type of listener. Normally <code>http</code>, but other valid options are:</p>
  3393. <ul>
  3394. <li>
  3395. <p><code>manhole</code>: (see the docs <a href="usage/configuration/../../manhole.html">here</a>),</p>
  3396. </li>
  3397. <li>
  3398. <p><code>metrics</code>: (see the docs <a href="usage/configuration/../../metrics-howto.html">here</a>),</p>
  3399. </li>
  3400. <li>
  3401. <p><code>replication</code>: (see the docs <a href="usage/configuration/../../workers.html">here</a>).</p>
  3402. </li>
  3403. </ul>
  3404. </li>
  3405. <li>
  3406. <p><code>tls</code>: set to true to enable TLS for this listener. Will use the TLS key/cert specified in tls_private_key_path / tls_certificate_path.</p>
  3407. </li>
  3408. <li>
  3409. <p><code>x_forwarded</code>: Only valid for an 'http' listener. Set to true to use the X-Forwarded-For header as the client IP. Useful when Synapse is
  3410. behind a reverse-proxy.</p>
  3411. </li>
  3412. <li>
  3413. <p><code>resources</code>: Only valid for an 'http' listener. A list of resources to host
  3414. on this port. Sub-options for each resource are:</p>
  3415. <ul>
  3416. <li>
  3417. <p><code>names</code>: a list of names of HTTP resources. See below for a list of valid resource names.</p>
  3418. </li>
  3419. <li>
  3420. <p><code>compress</code>: set to true to enable HTTP compression for this resource.</p>
  3421. </li>
  3422. </ul>
  3423. </li>
  3424. <li>
  3425. <p><code>additional_resources</code>: Only valid for an 'http' listener. A map of
  3426. additional endpoints which should be loaded via dynamic modules.</p>
  3427. </li>
  3428. </ul>
  3429. <p>Valid resource names are:</p>
  3430. <ul>
  3431. <li>
  3432. <p><code>client</code>: the client-server API (/_matrix/client), and the synapse admin API (/_synapse/admin). Also implies <code>media</code> and <code>static</code>.</p>
  3433. </li>
  3434. <li>
  3435. <p><code>consent</code>: user consent forms (/_matrix/consent). See <a href="usage/configuration/../../consent_tracking.html">here</a> for more.</p>
  3436. </li>
  3437. <li>
  3438. <p><code>federation</code>: the server-server API (/_matrix/federation). Also implies <code>media</code>, <code>keys</code>, <code>openid</code></p>
  3439. </li>
  3440. <li>
  3441. <p><code>keys</code>: the key discovery API (/_matrix/key).</p>
  3442. </li>
  3443. <li>
  3444. <p><code>media</code>: the media API (/_matrix/media).</p>
  3445. </li>
  3446. <li>
  3447. <p><code>metrics</code>: the metrics interface. See <a href="usage/configuration/../../metrics-howto.html">here</a>.</p>
  3448. </li>
  3449. <li>
  3450. <p><code>openid</code>: OpenID authentication. See <a href="usage/configuration/../../openid.html">here</a>.</p>
  3451. </li>
  3452. <li>
  3453. <p><code>replication</code>: the HTTP replication API (/_synapse/replication). See <a href="usage/configuration/../../workers.html">here</a>.</p>
  3454. </li>
  3455. <li>
  3456. <p><code>static</code>: static resources under synapse/static (/_matrix/static). (Mostly useful for 'fallback authentication'.)</p>
  3457. </li>
  3458. </ul>
  3459. <p>Example configuration #1:</p>
  3460. <pre><code class="language-yaml">listeners:
  3461. # TLS-enabled listener: for when matrix traffic is sent directly to synapse.
  3462. #
  3463. # (Note that you will also need to give Synapse a TLS key and certificate: see the TLS section
  3464. # below.)
  3465. #
  3466. - port: 8448
  3467. type: http
  3468. tls: true
  3469. resources:
  3470. - names: [client, federation]
  3471. </code></pre>
  3472. <p>Example configuration #2:</p>
  3473. <pre><code class="language-yaml">listeners:
  3474. # Unsecure HTTP listener: for when matrix traffic passes through a reverse proxy
  3475. # that unwraps TLS.
  3476. #
  3477. # If you plan to use a reverse proxy, please see
  3478. # https://matrix-org.github.io/synapse/latest/reverse_proxy.html.
  3479. #
  3480. - port: 8008
  3481. tls: false
  3482. type: http
  3483. x_forwarded: true
  3484. bind_addresses: ['::1', '127.0.0.1']
  3485. resources:
  3486. - names: [client, federation]
  3487. compress: false
  3488. # example additional_resources:
  3489. additional_resources:
  3490. &quot;/_matrix/my/custom/endpoint&quot;:
  3491. module: my_module.CustomRequestHandler
  3492. config: {}
  3493. # Turn on the twisted ssh manhole service on localhost on the given
  3494. # port.
  3495. - port: 9000
  3496. bind_addresses: ['::1', '127.0.0.1']
  3497. type: manhole
  3498. </code></pre>
  3499. <hr />
  3500. <p>Config option: <code>manhole_settings</code></p>
  3501. <p>Connection settings for the manhole. You can find more information
  3502. on the manhole <a href="usage/configuration/../../manhole.html">here</a>. Manhole sub-options include:</p>
  3503. <ul>
  3504. <li><code>username</code> : the username for the manhole. This defaults to 'matrix'.</li>
  3505. <li><code>password</code>: The password for the manhole. This defaults to 'rabbithole'.</li>
  3506. <li><code>ssh_priv_key_path</code> and <code>ssh_pub_key_path</code>: The private and public SSH key pair used to encrypt the manhole traffic.
  3507. If these are left unset, then hardcoded and non-secret keys are used,
  3508. which could allow traffic to be intercepted if sent over a public network.</li>
  3509. </ul>
  3510. <p>Example configuration:</p>
  3511. <pre><code class="language-yaml">manhole_settings:
  3512. username: manhole
  3513. password: mypassword
  3514. ssh_priv_key_path: CONFDIR/id_rsa
  3515. ssh_pub_key_path: CONFDIR/id_rsa.pub
  3516. </code></pre>
  3517. <hr />
  3518. <p>Config option: <code>dummy_events_threshold</code></p>
  3519. <p>Forward extremities can build up in a room due to networking delays between
  3520. homeservers. Once this happens in a large room, calculation of the state of
  3521. that room can become quite expensive. To mitigate this, once the number of
  3522. forward extremities reaches a given threshold, Synapse will send an
  3523. <code>org.matrix.dummy_event</code> event, which will reduce the forward extremities
  3524. in the room.</p>
  3525. <p>This setting defines the threshold (i.e. number of forward extremities in the room) at which dummy events are sent.
  3526. The default value is 10.</p>
  3527. <p>Example configuration:</p>
  3528. <pre><code class="language-yaml">dummy_events_threshold: 5
  3529. </code></pre>
  3530. <hr />
  3531. <h2 id="homeserver-blocking"><a class="header" href="#homeserver-blocking">Homeserver blocking</a></h2>
  3532. <p>Useful options for Synapse admins.</p>
  3533. <hr />
  3534. <p>Config option: <code>admin_contact</code></p>
  3535. <p>How to reach the server admin, used in <code>ResourceLimitError</code>. Defaults to none. </p>
  3536. <p>Example configuration:</p>
  3537. <pre><code class="language-yaml">admin_contact: 'mailto:admin@server.com'
  3538. </code></pre>
  3539. <hr />
  3540. <p>Config option: <code>hs_disabled</code> and <code>hs_disabled_message</code></p>
  3541. <p>Blocks users from connecting to the homeserver and provides a human-readable reason
  3542. why the connection was blocked. Defaults to false. </p>
  3543. <p>Example configuration:</p>
  3544. <pre><code class="language-yaml">hs_disabled: true
  3545. hs_disabled_message: 'Reason for why the HS is blocked'
  3546. </code></pre>
  3547. <hr />
  3548. <p>Config option: <code>limit_usage_by_mau</code></p>
  3549. <p>This option disables/enables monthly active user blocking. Used in cases where the admin or
  3550. server owner wants to limit to the number of monthly active users. When enabled and a limit is
  3551. reached the server returns a <code>ResourceLimitError</code> with error type <code>Codes.RESOURCE_LIMIT_EXCEEDED</code>.
  3552. Defaults to false. If this is enabled, a value for <code>max_mau_value</code> must also be set.</p>
  3553. <p>Example configuration:</p>
  3554. <pre><code class="language-yaml">limit_usage_by_mau: true
  3555. </code></pre>
  3556. <hr />
  3557. <p>Config option: <code>max_mau_value</code></p>
  3558. <p>This option sets the hard limit of monthly active users above which the server will start
  3559. blocking user actions if <code>limit_usage_by_mau</code> is enabled. Defaults to 0.</p>
  3560. <p>Example configuration:</p>
  3561. <pre><code class="language-yaml">max_mau_value: 50
  3562. </code></pre>
  3563. <hr />
  3564. <p>Config option: <code>mau_trial_days</code></p>
  3565. <p>The option <code>mau_trial_days</code> is a means to add a grace period for active users. It
  3566. means that users must be active for the specified number of days before they
  3567. can be considered active and guards against the case where lots of users
  3568. sign up in a short space of time never to return after their initial
  3569. session. Defaults to 0. </p>
  3570. <p>Example configuration:</p>
  3571. <pre><code class="language-yaml">mau_trial_days: 5
  3572. </code></pre>
  3573. <hr />
  3574. <p>Config option: <code>mau_appservice_trial_days</code></p>
  3575. <p>The option <code>mau_appservice_trial_days</code> is similar to <code>mau_trial_days</code>, but applies a different
  3576. trial number if the user was registered by an appservice. A value
  3577. of 0 means no trial days are applied. Appservices not listed in this dictionary
  3578. use the value of <code>mau_trial_days</code> instead.</p>
  3579. <p>Example configuration:</p>
  3580. <pre><code class="language-yaml">mau_appservice_trial_days:
  3581. my_appservice_id: 3
  3582. another_appservice_id: 6
  3583. </code></pre>
  3584. <hr />
  3585. <p>Config option: <code>mau_limit_alerting</code></p>
  3586. <p>The option <code>mau_limit_alerting</code> is a means of limiting client-side alerting
  3587. should the mau limit be reached. This is useful for small instances
  3588. where the admin has 5 mau seats (say) for 5 specific people and no
  3589. interest increasing the mau limit further. Defaults to true, which
  3590. means that alerting is enabled.</p>
  3591. <p>Example configuration:</p>
  3592. <pre><code class="language-yaml">mau_limit_alerting: false
  3593. </code></pre>
  3594. <hr />
  3595. <p>Config option: <code>mau_stats_only</code></p>
  3596. <p>If enabled, the metrics for the number of monthly active users will
  3597. be populated, however no one will be limited based on these numbers. If <code>limit_usage_by_mau</code>
  3598. is true, this is implied to be true. Defaults to false. </p>
  3599. <p>Example configuration:</p>
  3600. <pre><code class="language-yaml">mau_stats_only: true
  3601. </code></pre>
  3602. <hr />
  3603. <p>Config option: <code>mau_limit_reserved_threepids</code></p>
  3604. <p>Sometimes the server admin will want to ensure certain accounts are
  3605. never blocked by mau checking. These accounts are specified by this option.
  3606. Defaults to none. Add accounts by specifying the <code>medium</code> and <code>address</code> of the
  3607. reserved threepid (3rd party identifier).</p>
  3608. <p>Example configuration:</p>
  3609. <pre><code class="language-yaml">mau_limit_reserved_threepids:
  3610. - medium: 'email'
  3611. address: 'reserved_user@example.com'
  3612. </code></pre>
  3613. <hr />
  3614. <p>Config option: <code>server_context</code></p>
  3615. <p>This option is used by phonehome stats to group together related servers.
  3616. Defaults to none. </p>
  3617. <p>Example configuration:</p>
  3618. <pre><code class="language-yaml">server_context: context
  3619. </code></pre>
  3620. <hr />
  3621. <p>Config option: <code>limit_remote_rooms</code></p>
  3622. <p>When this option is enabled, the room &quot;complexity&quot; will be checked before a user
  3623. joins a new remote room. If it is above the complexity limit, the server will
  3624. disallow joining, or will instantly leave. This is useful for homeservers that are
  3625. resource-constrained. Options for this setting include:</p>
  3626. <ul>
  3627. <li><code>enabled</code>: whether this check is enabled. Defaults to false.</li>
  3628. <li><code>complexity</code>: the limit above which rooms cannot be joined. The default is 1.0.</li>
  3629. <li><code>complexity_error</code>: override the error which is returned when the room is too complex with a
  3630. custom message. </li>
  3631. <li><code>admins_can_join</code>: allow server admins to join complex rooms. Default is false.</li>
  3632. </ul>
  3633. <p>Room complexity is an arbitrary measure based on factors such as the number of
  3634. users in the room. </p>
  3635. <p>Example configuration:</p>
  3636. <pre><code class="language-yaml">limit_remote_rooms:
  3637. enabled: true
  3638. complexity: 0.5
  3639. complexity_error: &quot;I can't let you do that, Dave.&quot;
  3640. admins_can_join: true
  3641. </code></pre>
  3642. <hr />
  3643. <p>Config option: <code>require_membership_for_aliases</code></p>
  3644. <p>Whether to require a user to be in the room to add an alias to it.
  3645. Defaults to true.</p>
  3646. <p>Example configuration:</p>
  3647. <pre><code class="language-yaml">require_membership_for_aliases: false
  3648. </code></pre>
  3649. <hr />
  3650. <p>Config option: <code>allow_per_room_profiles</code></p>
  3651. <p>Whether to allow per-room membership profiles through the sending of membership
  3652. events with profile information that differs from the target's global profile.
  3653. Defaults to true.</p>
  3654. <p>Example configuration:</p>
  3655. <pre><code class="language-yaml">allow_per_room_profiles: false
  3656. </code></pre>
  3657. <hr />
  3658. <p>Config option: <code>max_avatar_size</code></p>
  3659. <p>The largest permissible file size in bytes for a user avatar. Defaults to no restriction.
  3660. Use M for MB and K for KB. </p>
  3661. <p>Note that user avatar changes will not work if this is set without using Synapse's media repository.</p>
  3662. <p>Example configuration:</p>
  3663. <pre><code class="language-yaml">max_avatar_size: 10M
  3664. </code></pre>
  3665. <hr />
  3666. <p>Config option: <code>allowed_avatar_mimetypes</code></p>
  3667. <p>The MIME types allowed for user avatars. Defaults to no restriction.</p>
  3668. <p>Note that user avatar changes will not work if this is set without
  3669. using Synapse's media repository.</p>
  3670. <p>Example configuration:</p>
  3671. <pre><code class="language-yaml">allowed_avatar_mimetypes: [&quot;image/png&quot;, &quot;image/jpeg&quot;, &quot;image/gif&quot;]
  3672. </code></pre>
  3673. <hr />
  3674. <p>Config option: <code>redaction_retention_period</code></p>
  3675. <p>How long to keep redacted events in unredacted form in the database. After
  3676. this period redacted events get replaced with their redacted form in the DB.</p>
  3677. <p>Defaults to <code>7d</code>. Set to <code>null</code> to disable.</p>
  3678. <p>Example configuration:</p>
  3679. <pre><code class="language-yaml">redaction_retention_period: 28d
  3680. </code></pre>
  3681. <hr />
  3682. <p>Config option: <code>user_ips_max_age</code> </p>
  3683. <p>How long to track users' last seen time and IPs in the database.</p>
  3684. <p>Defaults to <code>28d</code>. Set to <code>null</code> to disable clearing out of old rows.</p>
  3685. <p>Example configuration:</p>
  3686. <pre><code class="language-yaml">user_ips_max_age: 14d
  3687. </code></pre>
  3688. <hr />
  3689. <p>Config option: <code>request_token_inhibit_3pid_errors</code></p>
  3690. <p>Inhibits the <code>/requestToken</code> endpoints from returning an error that might leak
  3691. information about whether an e-mail address is in use or not on this
  3692. homeserver. Defaults to false.
  3693. Note that for some endpoints the error situation is the e-mail already being
  3694. used, and for others the error is entering the e-mail being unused.
  3695. If this option is enabled, instead of returning an error, these endpoints will
  3696. act as if no error happened and return a fake session ID ('sid') to clients.</p>
  3697. <p>Example configuration:</p>
  3698. <pre><code class="language-yaml">request_token_inhibit_3pid_errors: true
  3699. </code></pre>
  3700. <hr />
  3701. <p>Config option: <code>next_link_domain_whitelist</code></p>
  3702. <p>A list of domains that the domain portion of <code>next_link</code> parameters
  3703. must match.</p>
  3704. <p>This parameter is optionally provided by clients while requesting
  3705. validation of an email or phone number, and maps to a link that
  3706. users will be automatically redirected to after validation
  3707. succeeds. Clients can make use this parameter to aid the validation
  3708. process.</p>
  3709. <p>The whitelist is applied whether the homeserver or an identity server is handling validation.</p>
  3710. <p>The default value is no whitelist functionality; all domains are
  3711. allowed. Setting this value to an empty list will instead disallow
  3712. all domains.</p>
  3713. <p>Example configuration:</p>
  3714. <pre><code class="language-yaml">next_link_domain_whitelist: [&quot;matrix.org&quot;]
  3715. </code></pre>
  3716. <hr />
  3717. <p>Config option: <code>templates</code> and <code>custom_template_directory</code></p>
  3718. <p>These options define templates to use when generating email or HTML page contents.
  3719. The <code>custom_template_directory</code> determines which directory Synapse will try to
  3720. find template files in to use to generate email or HTML page contents.
  3721. If not set, or a file is not found within the template directory, a default
  3722. template from within the Synapse package will be used.</p>
  3723. <p>See <a href="usage/configuration/../../templates.html">here</a> for more
  3724. information about using custom templates.</p>
  3725. <p>Example configuration:</p>
  3726. <pre><code class="language-yaml">templates:
  3727. custom_template_directory: /path/to/custom/templates/
  3728. </code></pre>
  3729. <hr />
  3730. <p>Config option: <code>retention</code></p>
  3731. <p>This option and the associated options determine message retention policy at the
  3732. server level.</p>
  3733. <p>Room admins and mods can define a retention period for their rooms using the
  3734. <code>m.room.retention</code> state event, and server admins can cap this period by setting
  3735. the <code>allowed_lifetime_min</code> and <code>allowed_lifetime_max</code> config options.</p>
  3736. <p>If this feature is enabled, Synapse will regularly look for and purge events
  3737. which are older than the room's maximum retention period. Synapse will also
  3738. filter events received over federation so that events that should have been
  3739. purged are ignored and not stored again. </p>
  3740. <p>The message retention policies feature is disabled by default.</p>
  3741. <p>This setting has the following sub-options:</p>
  3742. <ul>
  3743. <li>
  3744. <p><code>default_policy</code>: Default retention policy. If set, Synapse will apply it to rooms that lack the
  3745. 'm.room.retention' state event. This option is further specified by the
  3746. <code>min_lifetime</code> and <code>max_lifetime</code> sub-options associated with it. Note that the
  3747. value of <code>min_lifetime</code> doesn't matter much because Synapse doesn't take it into account yet. </p>
  3748. </li>
  3749. <li>
  3750. <p><code>allowed_lifetime_min</code> and <code>allowed_lifetime_max</code>: Retention policy limits. If
  3751. set, and the state of a room contains a <code>m.room.retention</code> event in its state
  3752. which contains a <code>min_lifetime</code> or a <code>max_lifetime</code> that's out of these bounds,
  3753. Synapse will cap the room's policy to these limits when running purge jobs.</p>
  3754. </li>
  3755. <li>
  3756. <p><code>purge_jobs</code> and the associated <code>shortest_max_lifetime</code> and <code>longest_max_lifetime</code> sub-options:
  3757. Server admins can define the settings of the background jobs purging the
  3758. events whose lifetime has expired under the <code>purge_jobs</code> section.</p>
  3759. <p>If no configuration is provided for this option, a single job will be set up to delete
  3760. expired events in every room daily.</p>
  3761. <p>Each job's configuration defines which range of message lifetimes the job
  3762. takes care of. For example, if <code>shortest_max_lifetime</code> is '2d' and
  3763. <code>longest_max_lifetime</code> is '3d', the job will handle purging expired events in
  3764. rooms whose state defines a <code>max_lifetime</code> that's both higher than 2 days, and
  3765. lower than or equal to 3 days. Both the minimum and the maximum value of a
  3766. range are optional, e.g. a job with no <code>shortest_max_lifetime</code> and a
  3767. <code>longest_max_lifetime</code> of '3d' will handle every room with a retention policy
  3768. whose <code>max_lifetime</code> is lower than or equal to three days.</p>
  3769. <p>The rationale for this per-job configuration is that some rooms might have a
  3770. retention policy with a low <code>max_lifetime</code>, where history needs to be purged
  3771. of outdated messages on a more frequent basis than for the rest of the rooms
  3772. (e.g. every 12h), but not want that purge to be performed by a job that's
  3773. iterating over every room it knows, which could be heavy on the server.</p>
  3774. <p>If any purge job is configured, it is strongly recommended to have at least
  3775. a single job with neither <code>shortest_max_lifetime</code> nor <code>longest_max_lifetime</code>
  3776. set, or one job without <code>shortest_max_lifetime</code> and one job without
  3777. <code>longest_max_lifetime</code> set. Otherwise some rooms might be ignored, even if
  3778. <code>allowed_lifetime_min</code> and <code>allowed_lifetime_max</code> are set, because capping a
  3779. room's policy to these values is done after the policies are retrieved from
  3780. Synapse's database (which is done using the range specified in a purge job's
  3781. configuration).</p>
  3782. </li>
  3783. </ul>
  3784. <p>Example configuration:</p>
  3785. <pre><code class="language-yaml">retention:
  3786. enabled: true
  3787. default_policy:
  3788. min_lifetime: 1d
  3789. max_lifetime: 1y
  3790. allowed_lifetime_min: 1d
  3791. allowed_lifetime_max: 1y
  3792. purge_jobs:
  3793. - longest_max_lifetime: 3d
  3794. interval: 12h
  3795. - shortest_max_lifetime: 3d
  3796. interval: 1d
  3797. </code></pre>
  3798. <hr />
  3799. <h2 id="tls"><a class="header" href="#tls">TLS</a></h2>
  3800. <p>Options related to TLS.</p>
  3801. <hr />
  3802. <p>Config option: <code>tls_certificate_path</code></p>
  3803. <p>This option specifies a PEM-encoded X509 certificate for TLS.
  3804. This certificate, as of Synapse 1.0, will need to be a valid and verifiable
  3805. certificate, signed by a recognised Certificate Authority. Defaults to none. </p>
  3806. <p>Be sure to use a <code>.pem</code> file that includes the full certificate chain including
  3807. any intermediate certificates (for instance, if using certbot, use
  3808. <code>fullchain.pem</code> as your certificate, not <code>cert.pem</code>). </p>
  3809. <p>Example configuration:</p>
  3810. <pre><code class="language-yaml">tls_certificate_path: &quot;CONFDIR/SERVERNAME.tls.crt&quot;
  3811. </code></pre>
  3812. <hr />
  3813. <p>Config option: <code>tls_private_key_path</code></p>
  3814. <p>PEM-encoded private key for TLS. Defaults to none. </p>
  3815. <p>Example configuration:</p>
  3816. <pre><code class="language-yaml">tls_private_key_path: &quot;CONFDIR/SERVERNAME.tls.key&quot;
  3817. </code></pre>
  3818. <hr />
  3819. <p>Config option: <code>federation_verify_certificates</code>
  3820. Whether to verify TLS server certificates for outbound federation requests.</p>
  3821. <p>Defaults to true. To disable certificate verification, set the option to false.</p>
  3822. <p>Example configuration:</p>
  3823. <pre><code class="language-yaml">federation_verify_certificates: false
  3824. </code></pre>
  3825. <hr />
  3826. <p>Config option: <code>federation_client_minimum_tls_version</code></p>
  3827. <p>The minimum TLS version that will be used for outbound federation requests.</p>
  3828. <p>Defaults to <code>1</code>. Configurable to <code>1</code>, <code>1.1</code>, <code>1.2</code>, or <code>1.3</code>. Note
  3829. that setting this value higher than <code>1.2</code> will prevent federation to most
  3830. of the public Matrix network: only configure it to <code>1.3</code> if you have an
  3831. entirely private federation setup and you can ensure TLS 1.3 support.</p>
  3832. <p>Example configuration:</p>
  3833. <pre><code class="language-yaml">federation_client_minimum_tls_version: 1.2
  3834. </code></pre>
  3835. <hr />
  3836. <p>Config option: <code>federation_certificate_verification_whitelist</code></p>
  3837. <p>Skip federation certificate verification on a given whitelist
  3838. of domains.</p>
  3839. <p>This setting should only be used in very specific cases, such as
  3840. federation over Tor hidden services and similar. For private networks
  3841. of homeservers, you likely want to use a private CA instead.</p>
  3842. <p>Only effective if <code>federation_verify_certicates</code> is <code>true</code>.</p>
  3843. <p>Example configuration:</p>
  3844. <pre><code class="language-yaml">federation_certificate_verification_whitelist:
  3845. - lon.example.com
  3846. - &quot;*.domain.com&quot;
  3847. - &quot;*.onion&quot;
  3848. </code></pre>
  3849. <hr />
  3850. <p>Config option: <code>federation_custom_ca_list</code></p>
  3851. <p>List of custom certificate authorities for federation traffic.</p>
  3852. <p>This setting should only normally be used within a private network of
  3853. homeservers.</p>
  3854. <p>Note that this list will replace those that are provided by your
  3855. operating environment. Certificates must be in PEM format.</p>
  3856. <p>Example configuration:</p>
  3857. <pre><code class="language-yaml">federation_custom_ca_list:
  3858. - myCA1.pem
  3859. - myCA2.pem
  3860. - myCA3.pem
  3861. </code></pre>
  3862. <hr />
  3863. <h2 id="federation"><a class="header" href="#federation">Federation</a></h2>
  3864. <p>Options related to federation.</p>
  3865. <hr />
  3866. <p>Config option: <code>federation_domain_whitelist</code></p>
  3867. <p>Restrict federation to the given whitelist of domains.
  3868. N.B. we recommend also firewalling your federation listener to limit
  3869. inbound federation traffic as early as possible, rather than relying
  3870. purely on this application-layer restriction. If not specified, the
  3871. default is to whitelist everything.</p>
  3872. <p>Example configuration:</p>
  3873. <pre><code class="language-yaml">federation_domain_whitelist:
  3874. - lon.example.com
  3875. - nyc.example.com
  3876. - syd.example.com
  3877. </code></pre>
  3878. <hr />
  3879. <p>Config option: <code>federation_metrics_domains</code></p>
  3880. <p>Report prometheus metrics on the age of PDUs being sent to and received from
  3881. the given domains. This can be used to give an idea of &quot;delay&quot; on inbound
  3882. and outbound federation, though be aware that any delay can be due to problems
  3883. at either end or with the intermediate network.</p>
  3884. <p>By default, no domains are monitored in this way.</p>
  3885. <p>Example configuration:</p>
  3886. <pre><code class="language-yaml">federation_metrics_domains:
  3887. - matrix.org
  3888. - example.com
  3889. </code></pre>
  3890. <hr />
  3891. <p>Config option: <code>allow_profile_lookup_over_federation</code></p>
  3892. <p>Set to false to disable profile lookup over federation. By default, the
  3893. Federation API allows other homeservers to obtain profile data of any user
  3894. on this homeserver.</p>
  3895. <p>Example configuration:</p>
  3896. <pre><code class="language-yaml">allow_profile_lookup_over_federation: false
  3897. </code></pre>
  3898. <hr />
  3899. <p>Config option: <code>allow_device_name_lookup_over_federation</code></p>
  3900. <p>Set this option to true to allow device display name lookup over federation. By default, the
  3901. Federation API prevents other homeservers from obtaining the display names of any user devices
  3902. on this homeserver.</p>
  3903. <p>Example configuration:</p>
  3904. <pre><code class="language-yaml">allow_device_name_lookup_over_federation: true
  3905. </code></pre>
  3906. <hr />
  3907. <h2 id="caching"><a class="header" href="#caching">Caching</a></h2>
  3908. <p>Options related to caching</p>
  3909. <hr />
  3910. <p>Config option: <code>event_cache_size</code></p>
  3911. <p>The number of events to cache in memory. Not affected by
  3912. <code>caches.global_factor</code>. Defaults to 10K.</p>
  3913. <p>Example configuration:</p>
  3914. <pre><code class="language-yaml">event_cache_size: 15K
  3915. </code></pre>
  3916. <hr />
  3917. <p>Config option: <code>cache</code> and associated values</p>
  3918. <p>A cache 'factor' is a multiplier that can be applied to each of
  3919. Synapse's caches in order to increase or decrease the maximum
  3920. number of entries that can be stored.</p>
  3921. <p>Caching can be configured through the following sub-options:</p>
  3922. <ul>
  3923. <li>
  3924. <p><code>global_factor</code>: Controls the global cache factor, which is the default cache factor
  3925. for all caches if a specific factor for that cache is not otherwise
  3926. set.</p>
  3927. <p>This can also be set by the <code>SYNAPSE_CACHE_FACTOR</code> environment
  3928. variable. Setting by environment variable takes priority over
  3929. setting through the config file.</p>
  3930. <p>Defaults to 0.5, which will halve the size of all caches.</p>
  3931. </li>
  3932. <li>
  3933. <p><code>per_cache_factors</code>: A dictionary of cache name to cache factor for that individual
  3934. cache. Overrides the global cache factor for a given cache.</p>
  3935. <p>These can also be set through environment variables comprised
  3936. of <code>SYNAPSE_CACHE_FACTOR_</code> + the name of the cache in capital
  3937. letters and underscores. Setting by environment variable
  3938. takes priority over setting through the config file.
  3939. Ex. <code>SYNAPSE_CACHE_FACTOR_GET_USERS_WHO_SHARE_ROOM_WITH_USER=2.0</code></p>
  3940. <p>Some caches have '*' and other characters that are not
  3941. alphanumeric or underscores. These caches can be named with or
  3942. without the special characters stripped. For example, to specify
  3943. the cache factor for <code>*stateGroupCache*</code> via an environment
  3944. variable would be <code>SYNAPSE_CACHE_FACTOR_STATEGROUPCACHE=2.0</code>.</p>
  3945. </li>
  3946. <li>
  3947. <p><code>expire_caches</code>: Controls whether cache entries are evicted after a specified time
  3948. period. Defaults to true. Set to false to disable this feature. Note that never expiring
  3949. caches may result in excessive memory usage. </p>
  3950. </li>
  3951. <li>
  3952. <p><code>cache_entry_ttl</code>: If <code>expire_caches</code> is enabled, this flag controls how long an entry can
  3953. be in a cache without having been accessed before being evicted.
  3954. Defaults to 30m. </p>
  3955. </li>
  3956. <li>
  3957. <p><code>sync_response_cache_duration</code>: Controls how long the results of a /sync request are
  3958. cached for after a successful response is returned. A higher duration can help clients
  3959. with intermittent connections, at the cost of higher memory usage.
  3960. By default, this is zero, which means that sync responses are not cached
  3961. at all.</p>
  3962. </li>
  3963. <li>
  3964. <p><code>cache_autotuning</code> and its sub-options <code>max_cache_memory_usage</code>, <code>target_cache_memory_usage</code>, and
  3965. <code>min_cache_ttl</code> work in conjunction with each other to maintain a balance between cache memory
  3966. usage and cache entry availability. You must be using <a href="https://github.com/matrix-org/synapse#help-synapse-is-slow-and-eats-all-my-ramcpu">jemalloc</a>
  3967. to utilize this option, and all three of the options must be specified for this feature to work. This option
  3968. defaults to off, enable it by providing values for the sub-options listed below. Please note that the feature will not work
  3969. and may cause unstable behavior (such as excessive emptying of caches or exceptions) if all of the values are not provided.
  3970. Please see the <a href="usage/configuration/config_documentation.html#config-conventions">Config Conventions</a> for information on how to specify memory size and cache expiry
  3971. durations.</p>
  3972. <ul>
  3973. <li><code>max_cache_memory_usage</code> sets a ceiling on how much memory the cache can use before caches begin to be continuously evicted.
  3974. They will continue to be evicted until the memory usage drops below the <code>target_memory_usage</code>, set in
  3975. the setting below, or until the <code>min_cache_ttl</code> is hit. There is no default value for this option.</li>
  3976. <li><code>target_memory_usage</code> sets a rough target for the desired memory usage of the caches. There is no default value
  3977. for this option.</li>
  3978. <li><code>min_cache_ttl</code> sets a limit under which newer cache entries are not evicted and is only applied when
  3979. caches are actively being evicted/<code>max_cache_memory_usage</code> has been exceeded. This is to protect hot caches
  3980. from being emptied while Synapse is evicting due to memory. There is no default value for this option. </li>
  3981. </ul>
  3982. </li>
  3983. </ul>
  3984. <p>Example configuration:</p>
  3985. <pre><code class="language-yaml">caches:
  3986. global_factor: 1.0
  3987. per_cache_factors:
  3988. get_users_who_share_room_with_user: 2.0
  3989. sync_response_cache_duration: 2m
  3990. cache_autotuning:
  3991. max_cache_memory_usage: 1024M
  3992. target_cache_memory_usage: 758M
  3993. min_cache_ttl: 5m
  3994. </code></pre>
  3995. <h3 id="reloading-cache-factors"><a class="header" href="#reloading-cache-factors">Reloading cache factors</a></h3>
  3996. <p>The cache factors (i.e. <code>caches.global_factor</code> and <code>caches.per_cache_factors</code>) may be reloaded at any time by sending a
  3997. <a href="https://en.wikipedia.org/wiki/SIGHUP"><code>SIGHUP</code></a> signal to Synapse using e.g.</p>
  3998. <pre><code class="language-commandline">kill -HUP [PID_OF_SYNAPSE_PROCESS]
  3999. </code></pre>
  4000. <p>If you are running multiple workers, you must individually update the worker
  4001. config file and send this signal to each worker process.</p>
  4002. <p>If you're using the <a href="https://github.com/matrix-org/synapse/blob/develop/contrib/systemd/matrix-synapse.service">example systemd service</a>
  4003. file in Synapse's <code>contrib</code> directory, you can send a <code>SIGHUP</code> signal by using
  4004. <code>systemctl reload matrix-synapse</code>.</p>
  4005. <hr />
  4006. <h2 id="database"><a class="header" href="#database">Database</a></h2>
  4007. <p>Config options related to database settings.</p>
  4008. <hr />
  4009. <p>Config option: <code>database</code></p>
  4010. <p>The <code>database</code> setting defines the database that synapse uses to store all of
  4011. its data.</p>
  4012. <p>Associated sub-options:</p>
  4013. <ul>
  4014. <li>
  4015. <p><code>name</code>: this option specifies the database engine to use: either <code>sqlite3</code> (for SQLite)
  4016. or <code>psycopg2</code> (for PostgreSQL). If no name is specified Synapse will default to SQLite. </p>
  4017. </li>
  4018. <li>
  4019. <p><code>txn_limit</code> gives the maximum number of transactions to run per connection
  4020. before reconnecting. Defaults to 0, which means no limit.</p>
  4021. </li>
  4022. <li>
  4023. <p><code>allow_unsafe_locale</code> is an option specific to Postgres. Under the default behavior, Synapse will refuse to
  4024. start if the postgres db is set to a non-C locale. You can override this behavior (which is <em>not</em> recommended)
  4025. by setting <code>allow_unsafe_locale</code> to true. Note that doing so may corrupt your database. You can find more information
  4026. <a href="usage/configuration/../../postgres.html#fixing-incorrect-collate-or-ctype">here</a> and <a href="https://wiki.postgresql.org/wiki/Locale_data_changes">here</a>.</p>
  4027. </li>
  4028. <li>
  4029. <p><code>args</code> gives options which are passed through to the database engine,
  4030. except for options starting with <code>cp_</code>, which are used to configure the Twisted
  4031. connection pool. For a reference to valid arguments, see:</p>
  4032. <ul>
  4033. <li>for <a href="https://docs.python.org/3/library/sqlite3.html#sqlite3.connect">sqlite</a></li>
  4034. <li>for <a href="https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS">postgres</a></li>
  4035. <li>for <a href="https://twistedmatrix.com/documents/current/api/twisted.enterprise.adbapi.ConnectionPool.html#__init__">the connection pool</a></li>
  4036. </ul>
  4037. </li>
  4038. </ul>
  4039. <p>For more information on using Synapse with Postgres,
  4040. see <a href="usage/configuration/../../postgres.html">here</a>.</p>
  4041. <p>Example SQLite configuration:</p>
  4042. <pre><code class="language-yaml">database:
  4043. name: sqlite3
  4044. args:
  4045. database: /path/to/homeserver.db
  4046. </code></pre>
  4047. <p>Example Postgres configuration:</p>
  4048. <pre><code class="language-yaml">database:
  4049. name: psycopg2
  4050. txn_limit: 10000
  4051. args:
  4052. user: synapse_user
  4053. password: secretpassword
  4054. database: synapse
  4055. host: localhost
  4056. port: 5432
  4057. cp_min: 5
  4058. cp_max: 10
  4059. </code></pre>
  4060. <hr />
  4061. <h2 id="logging"><a class="header" href="#logging">Logging</a></h2>
  4062. <p>Config options related to logging. </p>
  4063. <hr />
  4064. <p>Config option: <code>log_config</code></p>
  4065. <p>This option specifies a yaml python logging config file as described <a href="https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema">here</a>.</p>
  4066. <p>Example configuration:</p>
  4067. <pre><code class="language-yaml">log_config: &quot;CONFDIR/SERVERNAME.log.config&quot;
  4068. </code></pre>
  4069. <hr />
  4070. <h2 id="ratelimiting"><a class="header" href="#ratelimiting">Ratelimiting</a></h2>
  4071. <p>Options related to ratelimiting in Synapse. </p>
  4072. <p>Each ratelimiting configuration is made of two parameters:</p>
  4073. <ul>
  4074. <li><code>per_second</code>: number of requests a client can send per second.</li>
  4075. <li><code>burst_count</code>: number of requests a client can send before being throttled.</li>
  4076. </ul>
  4077. <hr />
  4078. <p>Config option: <code>rc_message</code></p>
  4079. <p>Ratelimiting settings for client messaging.</p>
  4080. <p>This is a ratelimiting option for messages that ratelimits sending based on the account the client
  4081. is using. It defaults to: <code>per_second: 0.2</code>, <code>burst_count: 10</code>.</p>
  4082. <p>Example configuration:</p>
  4083. <pre><code class="language-yaml">rc_message:
  4084. per_second: 0.5
  4085. burst_count: 15
  4086. </code></pre>
  4087. <hr />
  4088. <p>Config option: <code>rc_registration</code></p>
  4089. <p>This option ratelimits registration requests based on the client's IP address.
  4090. It defaults to <code>per_second: 0.17</code>, <code>burst_count: 3</code>. </p>
  4091. <p>Example configuration:</p>
  4092. <pre><code class="language-yaml">rc_registration:
  4093. per_second: 0.15
  4094. burst_count: 2
  4095. </code></pre>
  4096. <hr />
  4097. <p>Config option: <code>rc_registration_token_validity</code></p>
  4098. <p>This option checks the validity of registration tokens that ratelimits requests based on
  4099. the client's IP address.
  4100. Defaults to <code>per_second: 0.1</code>, <code>burst_count: 5</code>.</p>
  4101. <p>Example configuration:</p>
  4102. <pre><code class="language-yaml">rc_registration_token_validity:
  4103. per_second: 0.3
  4104. burst_count: 6
  4105. </code></pre>
  4106. <hr />
  4107. <p>Config option: <code>rc_login</code></p>
  4108. <p>This option specifies several limits for login:</p>
  4109. <ul>
  4110. <li>
  4111. <p><code>address</code> ratelimits login requests based on the client's IP
  4112. address. Defaults to <code>per_second: 0.17</code>, <code>burst_count: 3</code>.</p>
  4113. </li>
  4114. <li>
  4115. <p><code>account</code> ratelimits login requests based on the account the
  4116. client is attempting to log into. Defaults to <code>per_second: 0.17</code>,
  4117. <code>burst_count: 3</code>.</p>
  4118. </li>
  4119. <li>
  4120. <p><code>failted_attempts</code> ratelimits login requests based on the account the
  4121. client is attempting to log into, based on the amount of failed login
  4122. attempts for this account. Defaults to <code>per_second: 0.17</code>, <code>burst_count: 3</code>.</p>
  4123. </li>
  4124. </ul>
  4125. <p>Example configuration:</p>
  4126. <pre><code class="language-yaml">rc_login:
  4127. address:
  4128. per_second: 0.15
  4129. burst_count: 5
  4130. account:
  4131. per_second: 0.18
  4132. burst_count: 4
  4133. failed_attempts:
  4134. per_second: 0.19
  4135. burst_count: 7
  4136. </code></pre>
  4137. <hr />
  4138. <p>Config option: <code>rc_admin_redaction</code></p>
  4139. <p>This option sets ratelimiting redactions by room admins. If this is not explicitly
  4140. set then it uses the same ratelimiting as per <code>rc_message</code>. This is useful
  4141. to allow room admins to deal with abuse quickly. </p>
  4142. <p>Example configuration:</p>
  4143. <pre><code class="language-yaml">rc_admin_redaction:
  4144. per_second: 1
  4145. burst_count: 50
  4146. </code></pre>
  4147. <hr />
  4148. <p>Config option: <code>rc_joins</code></p>
  4149. <p>This option allows for ratelimiting number of rooms a user can join. This setting has the following sub-options:</p>
  4150. <ul>
  4151. <li>
  4152. <p><code>local</code>: ratelimits when users are joining rooms the server is already in.
  4153. Defaults to <code>per_second: 0.1</code>, <code>burst_count: 10</code>.</p>
  4154. </li>
  4155. <li>
  4156. <p><code>remote</code>: ratelimits when users are trying to join rooms not on the server (which
  4157. can be more computationally expensive than restricting locally). Defaults to
  4158. <code>per_second: 0.01</code>, <code>burst_count: 10</code> </p>
  4159. </li>
  4160. </ul>
  4161. <p>Example configuration:</p>
  4162. <pre><code class="language-yaml">rc_joins:
  4163. local:
  4164. per_second: 0.2
  4165. burst_count: 15
  4166. remote:
  4167. per_second: 0.03
  4168. burst_count: 12
  4169. </code></pre>
  4170. <hr />
  4171. <p>Config option: <code>rc_3pid_validation</code></p>
  4172. <p>This option ratelimits how often a user or IP can attempt to validate a 3PID.
  4173. Defaults to <code>per_second: 0.003</code>, <code>burst_count: 5</code>.</p>
  4174. <p>Example configuration:</p>
  4175. <pre><code class="language-yaml">rc_3pid_validation:
  4176. per_second: 0.003
  4177. burst_count: 5
  4178. </code></pre>
  4179. <hr />
  4180. <p>Config option: <code>rc_invites</code></p>
  4181. <p>This option sets ratelimiting how often invites can be sent in a room or to a
  4182. specific user. <code>per_room</code> defaults to <code>per_second: 0.3</code>, <code>burst_count: 10</code> and
  4183. <code>per_user</code> defaults to <code>per_second: 0.003</code>, <code>burst_count: 5</code>. </p>
  4184. <p>Client requests that invite user(s) when <a href="https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3createroom">creating a
  4185. room</a>
  4186. will count against the <code>rc_invites.per_room</code> limit, whereas
  4187. client requests to <a href="https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidinvite">invite a single user to a
  4188. room</a>
  4189. will count against both the <code>rc_invites.per_user</code> and <code>rc_invites.per_room</code> limits.</p>
  4190. <p>Federation requests to invite a user will count against the <code>rc_invites.per_user</code>
  4191. limit only, as Synapse presumes ratelimiting by room will be done by the sending server.</p>
  4192. <p>The <code>rc_invites.per_user</code> limit applies to the <em>receiver</em> of the invite, rather than the
  4193. sender, meaning that a <code>rc_invite.per_user.burst_count</code> of 5 mandates that a single user
  4194. cannot <em>receive</em> more than a burst of 5 invites at a time.</p>
  4195. <p>Example configuration:</p>
  4196. <pre><code class="language-yaml">rc_invites:
  4197. per_room:
  4198. per_second: 0.5
  4199. burst_count: 5
  4200. per_user:
  4201. per_second: 0.004
  4202. burst_count: 3
  4203. </code></pre>
  4204. <hr />
  4205. <p>Config option: <code>rc_third_party_invite</code></p>
  4206. <p>This option ratelimits 3PID invites (i.e. invites sent to a third-party ID
  4207. such as an email address or a phone number) based on the account that's
  4208. sending the invite. Defaults to <code>per_second: 0.2</code>, <code>burst_count: 10</code>.</p>
  4209. <p>Example configuration:</p>
  4210. <pre><code class="language-yaml">rc_third_party_invite:
  4211. per_second: 0.2
  4212. burst_count: 10
  4213. </code></pre>
  4214. <hr />
  4215. <p>Config option: <code>rc_federation</code></p>
  4216. <p>Defines limits on federation requests. </p>
  4217. <p>The <code>rc_federation</code> configuration has the following sub-options:</p>
  4218. <ul>
  4219. <li><code>window_size</code>: window size in milliseconds. Defaults to 1000.</li>
  4220. <li><code>sleep_limit</code>: number of federation requests from a single server in
  4221. a window before the server will delay processing the request. Defaults to 10.</li>
  4222. <li><code>sleep_delay</code>: duration in milliseconds to delay processing events
  4223. from remote servers by if they go over the sleep limit. Defaults to 500.</li>
  4224. <li><code>reject_limit</code>: maximum number of concurrent federation requests
  4225. allowed from a single server. Defaults to 50.</li>
  4226. <li><code>concurrent</code>: number of federation requests to concurrently process
  4227. from a single server. Defaults to 3.</li>
  4228. </ul>
  4229. <p>Example configuration:</p>
  4230. <pre><code class="language-yaml">rc_federation:
  4231. window_size: 750
  4232. sleep_limit: 15
  4233. sleep_delay: 400
  4234. reject_limit: 40
  4235. concurrent: 5
  4236. </code></pre>
  4237. <hr />
  4238. <p>Config option: <code>federation_rr_transactions_per_room_per_second</code></p>
  4239. <p>Sets outgoing federation transaction frequency for sending read-receipts,
  4240. per-room.</p>
  4241. <p>If we end up trying to send out more read-receipts, they will get buffered up
  4242. into fewer transactions. Defaults to 50. </p>
  4243. <p>Example configuration:</p>
  4244. <pre><code class="language-yaml">federation_rr_transactions_per_room_per_second: 40
  4245. </code></pre>
  4246. <hr />
  4247. <h2 id="media-store"><a class="header" href="#media-store">Media Store</a></h2>
  4248. <p>Config options relating to Synapse media store.</p>
  4249. <hr />
  4250. <p>Config option: <code>enable_media_repo</code> </p>
  4251. <p>Enable the media store service in the Synapse master. Defaults to true.
  4252. Set to false if you are using a separate media store worker.</p>
  4253. <p>Example configuration:</p>
  4254. <pre><code class="language-yaml">enable_media_repo: false
  4255. </code></pre>
  4256. <hr />
  4257. <p>Config option: <code>media_store_path</code></p>
  4258. <p>Directory where uploaded images and attachments are stored.</p>
  4259. <p>Example configuration:</p>
  4260. <pre><code class="language-yaml">media_store_path: &quot;DATADIR/media_store&quot;
  4261. </code></pre>
  4262. <hr />
  4263. <p>Config option: <code>media_storage_providers</code></p>
  4264. <p>Media storage providers allow media to be stored in different
  4265. locations. Defaults to none. Associated sub-options are:</p>
  4266. <ul>
  4267. <li><code>module</code>: type of resource, e.g. <code>file_system</code>.</li>
  4268. <li><code>store_local</code>: whether to store newly uploaded local files</li>
  4269. <li><code>store_remote</code>: whether to store newly downloaded local files</li>
  4270. <li><code>store_synchronous</code>: whether to wait for successful storage for local uploads</li>
  4271. <li><code>config</code>: sets a path to the resource through the <code>directory</code> option </li>
  4272. </ul>
  4273. <p>Example configuration:</p>
  4274. <pre><code class="language-yaml">media_storage_providers:
  4275. - module: file_system
  4276. store_local: false
  4277. store_remote: false
  4278. store_synchronous: false
  4279. config:
  4280. directory: /mnt/some/other/directory
  4281. </code></pre>
  4282. <hr />
  4283. <p>Config option: <code>max_upload_size</code></p>
  4284. <p>The largest allowed upload size in bytes.</p>
  4285. <p>If you are using a reverse proxy you may also need to set this value in
  4286. your reverse proxy's config. Defaults to 50M. Notably Nginx has a small max body size by default.
  4287. See <a href="usage/configuration/../../reverse_proxy.html">here</a> for more on using a reverse proxy with Synapse. </p>
  4288. <p>Example configuration:</p>
  4289. <pre><code class="language-yaml">max_upload_size: 60M
  4290. </code></pre>
  4291. <hr />
  4292. <p>Config option: <code>max_image_pixels</code></p>
  4293. <p>Maximum number of pixels that will be thumbnailed. Defaults to 32M.</p>
  4294. <p>Example configuration:</p>
  4295. <pre><code class="language-yaml">max_image_pixels: 35M
  4296. </code></pre>
  4297. <hr />
  4298. <p>Config option: <code>dynamic_thumbnails</code></p>
  4299. <p>Whether to generate new thumbnails on the fly to precisely match
  4300. the resolution requested by the client. If true then whenever
  4301. a new resolution is requested by the client the server will
  4302. generate a new thumbnail. If false the server will pick a thumbnail
  4303. from a precalculated list. Defaults to false. </p>
  4304. <p>Example configuration:</p>
  4305. <pre><code class="language-yaml">dynamic_thumbnails: true
  4306. </code></pre>
  4307. <hr />
  4308. <p>Config option: <code>thumbnail_sizes</code></p>
  4309. <p>List of thumbnails to precalculate when an image is uploaded. Associated sub-options are:</p>
  4310. <ul>
  4311. <li><code>width</code></li>
  4312. <li><code>height</code></li>
  4313. <li><code>method</code>: i.e. <code>crop</code>, <code>scale</code>, etc.</li>
  4314. </ul>
  4315. <p>Example configuration:</p>
  4316. <pre><code class="language-yaml">thumbnail_sizes:
  4317. - width: 32
  4318. height: 32
  4319. method: crop
  4320. - width: 96
  4321. height: 96
  4322. method: crop
  4323. - width: 320
  4324. height: 240
  4325. method: scale
  4326. - width: 640
  4327. height: 480
  4328. method: scale
  4329. - width: 800
  4330. height: 600
  4331. method: scale
  4332. </code></pre>
  4333. <p>Config option: <code>url_preview_enabled</code></p>
  4334. <p>This setting determines whether the preview URL API is enabled.
  4335. It is disabled by default. Set to true to enable. If enabled you must specify a
  4336. <code>url_preview_ip_range_blacklist</code> blacklist.</p>
  4337. <p>Example configuration:</p>
  4338. <pre><code class="language-yaml">url_preview_enabled: true
  4339. </code></pre>
  4340. <hr />
  4341. <p>Config option: <code>url_preview_ip_range_blacklist</code></p>
  4342. <p>List of IP address CIDR ranges that the URL preview spider is denied
  4343. from accessing. There are no defaults: you must explicitly
  4344. specify a list for URL previewing to work. You should specify any
  4345. internal services in your network that you do not want synapse to try
  4346. to connect to, otherwise anyone in any Matrix room could cause your
  4347. synapse to issue arbitrary GET requests to your internal services,
  4348. causing serious security issues.</p>
  4349. <p>(0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
  4350. listed here, since they correspond to unroutable addresses.)</p>
  4351. <p>This must be specified if <code>url_preview_enabled</code> is set. It is recommended that
  4352. you use the following example list as a starting point.</p>
  4353. <p>Note: The value is ignored when an HTTP proxy is in use.</p>
  4354. <p>Example configuration:</p>
  4355. <pre><code class="language-yaml">url_preview_ip_range_blacklist:
  4356. - '127.0.0.0/8'
  4357. - '10.0.0.0/8'
  4358. - '172.16.0.0/12'
  4359. - '192.168.0.0/16'
  4360. - '100.64.0.0/10'
  4361. - '192.0.0.0/24'
  4362. - '169.254.0.0/16'
  4363. - '192.88.99.0/24'
  4364. - '198.18.0.0/15'
  4365. - '192.0.2.0/24'
  4366. - '198.51.100.0/24'
  4367. - '203.0.113.0/24'
  4368. - '224.0.0.0/4'
  4369. - '::1/128'
  4370. - 'fe80::/10'
  4371. - 'fc00::/7'
  4372. - '2001:db8::/32'
  4373. - 'ff00::/8'
  4374. - 'fec0::/10'
  4375. </code></pre>
  4376. <hr />
  4377. <p>Config option: <code>url_preview_ip_range_whitelist</code></p>
  4378. <p>This option sets a list of IP address CIDR ranges that the URL preview spider is allowed
  4379. to access even if they are specified in <code>url_preview_ip_range_blacklist</code>.
  4380. This is useful for specifying exceptions to wide-ranging blacklisted
  4381. target IP ranges - e.g. for enabling URL previews for a specific private
  4382. website only visible in your network. Defaults to none. </p>
  4383. <p>Example configuration:</p>
  4384. <pre><code class="language-yaml">url_preview_ip_range_whitelist:
  4385. - '192.168.1.1'
  4386. </code></pre>
  4387. <hr />
  4388. <p>Config option: <code>url_preview_url_blacklist</code></p>
  4389. <p>Optional list of URL matches that the URL preview spider is
  4390. denied from accessing. You should use <code>url_preview_ip_range_blacklist</code>
  4391. in preference to this, otherwise someone could define a public DNS
  4392. entry that points to a private IP address and circumvent the blacklist.
  4393. This is more useful if you know there is an entire shape of URL that
  4394. you know that will never want synapse to try to spider.</p>
  4395. <p>Each list entry is a dictionary of url component attributes as returned
  4396. by urlparse.urlsplit as applied to the absolute form of the URL. See
  4397. <a href="https://docs.python.org/2/library/urlparse.html#urlparse.urlsplit">here</a> for more
  4398. information. Some examples are:</p>
  4399. <ul>
  4400. <li><code>username</code></li>
  4401. <li><code>netloc</code></li>
  4402. <li><code>scheme</code></li>
  4403. <li><code>path</code></li>
  4404. </ul>
  4405. <p>The values of the dictionary are treated as a filename match pattern
  4406. applied to that component of URLs, unless they start with a ^ in which
  4407. case they are treated as a regular expression match. If all the
  4408. specified component matches for a given list item succeed, the URL is
  4409. blacklisted.</p>
  4410. <p>Example configuration:</p>
  4411. <pre><code class="language-yaml">url_preview_url_blacklist:
  4412. # blacklist any URL with a username in its URI
  4413. - username: '*'
  4414. # blacklist all *.google.com URLs
  4415. - netloc: 'google.com'
  4416. - netloc: '*.google.com'
  4417. # blacklist all plain HTTP URLs
  4418. - scheme: 'http'
  4419. # blacklist http(s)://www.acme.com/foo
  4420. - netloc: 'www.acme.com'
  4421. path: '/foo'
  4422. # blacklist any URL with a literal IPv4 address
  4423. - netloc: '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
  4424. </code></pre>
  4425. <hr />
  4426. <p>Config option: <code>max_spider_size</code></p>
  4427. <p>The largest allowed URL preview spidering size in bytes. Defaults to 10M.</p>
  4428. <p>Example configuration:</p>
  4429. <pre><code class="language-yaml">max_spider_size: 8M
  4430. </code></pre>
  4431. <hr />
  4432. <p>Config option: <code>url_preview_language</code></p>
  4433. <p>A list of values for the Accept-Language HTTP header used when
  4434. downloading webpages during URL preview generation. This allows
  4435. Synapse to specify the preferred languages that URL previews should
  4436. be in when communicating with remote servers.</p>
  4437. <p>Each value is a IETF language tag; a 2-3 letter identifier for a
  4438. language, optionally followed by subtags separated by '-', specifying
  4439. a country or region variant.</p>
  4440. <p>Multiple values can be provided, and a weight can be added to each by
  4441. using quality value syntax (;q=). '*' translates to any language.</p>
  4442. <p>Defaults to &quot;en&quot;.</p>
  4443. <p>Example configuration:</p>
  4444. <pre><code class="language-yaml"> url_preview_accept_language:
  4445. - 'en-UK'
  4446. - 'en-US;q=0.9'
  4447. - 'fr;q=0.8'
  4448. - '*;q=0.7'
  4449. </code></pre>
  4450. <hr />
  4451. <p>Config option: <code>oembed</code></p>
  4452. <p>oEmbed allows for easier embedding content from a website. It can be
  4453. used for generating URLs previews of services which support it. A default list of oEmbed providers
  4454. is included with Synapse. Set <code>disable_default_providers</code> to true to disable using
  4455. these default oEmbed URLs. Use <code>additional_providers</code> to specify additional files with oEmbed configuration (each
  4456. should be in the form of providers.json). By default this list is empty. </p>
  4457. <p>Example configuration:</p>
  4458. <pre><code class="language-yaml">oembed:
  4459. disable_default_providers: true
  4460. additional_providers:
  4461. - oembed/my_providers.json
  4462. </code></pre>
  4463. <hr />
  4464. <h2 id="captcha"><a class="header" href="#captcha">Captcha</a></h2>
  4465. <p>See <a href="usage/configuration/../../CAPTCHA_SETUP.html">here</a> for full details on setting up captcha.</p>
  4466. <hr />
  4467. <p>Config option: <code>recaptcha_public_key</code></p>
  4468. <p>This homeserver's ReCAPTCHA public key. Must be specified if <code>enable_registration_captcha</code> is
  4469. enabled.</p>
  4470. <p>Example configuration:</p>
  4471. <pre><code class="language-yaml">recaptcha_public_key: &quot;YOUR_PUBLIC_KEY&quot;
  4472. </code></pre>
  4473. <hr />
  4474. <p>Config option: <code>recaptcha_private_key</code> </p>
  4475. <p>This homeserver's ReCAPTCHA private key. Must be specified if <code>enable_registration_captcha</code> is
  4476. enabled.</p>
  4477. <p>Example configuration:</p>
  4478. <pre><code class="language-yaml">recaptcha_private_key: &quot;YOUR_PRIVATE_KEY&quot;
  4479. </code></pre>
  4480. <hr />
  4481. <p>Config option: <code>enable_registration_captcha</code></p>
  4482. <p>Set to true to enable ReCaptcha checks when registering, preventing signup
  4483. unless a captcha is answered. Requires a valid ReCaptcha public/private key.
  4484. Defaults to false.</p>
  4485. <p>Example configuration:</p>
  4486. <pre><code class="language-yaml">enable_registration_captcha: true
  4487. </code></pre>
  4488. <hr />
  4489. <p>Config option: <code>recaptcha_siteverify_api</code></p>
  4490. <p>The API endpoint to use for verifying <code>m.login.recaptcha</code> responses.
  4491. Defaults to <code>https://www.recaptcha.net/recaptcha/api/siteverify</code>.</p>
  4492. <p>Example configuration:</p>
  4493. <pre><code class="language-yaml">recaptcha_siteverify_api: &quot;https://my.recaptcha.site&quot;
  4494. </code></pre>
  4495. <hr />
  4496. <h2 id="turn"><a class="header" href="#turn">TURN</a></h2>
  4497. <p>Options related to adding a TURN server to Synapse.</p>
  4498. <hr />
  4499. <p>Config option: <code>turn_uris</code></p>
  4500. <p>The public URIs of the TURN server to give to clients.</p>
  4501. <p>Example configuration:</p>
  4502. <pre><code class="language-yaml">turn_uris: [turn:example.org]
  4503. </code></pre>
  4504. <hr />
  4505. <p>Config option: <code>turn_shared_secret</code></p>
  4506. <p>The shared secret used to compute passwords for the TURN server.</p>
  4507. <p>Example configuration:</p>
  4508. <pre><code class="language-yaml">turn_shared_secret: &quot;YOUR_SHARED_SECRET&quot;
  4509. </code></pre>
  4510. <hr />
  4511. <p>Config options: <code>turn_username</code> and <code>turn_password</code></p>
  4512. <p>The Username and password if the TURN server needs them and does not use a token.</p>
  4513. <p>Example configuration:</p>
  4514. <pre><code class="language-yaml">turn_username: &quot;TURNSERVER_USERNAME&quot;
  4515. turn_password: &quot;TURNSERVER_PASSWORD&quot;
  4516. </code></pre>
  4517. <hr />
  4518. <p>Config option: <code>turn_user_lifetime</code></p>
  4519. <p>How long generated TURN credentials last. Defaults to 1h.</p>
  4520. <p>Example configuration:</p>
  4521. <pre><code class="language-yaml">turn_user_lifetime: 2h
  4522. </code></pre>
  4523. <hr />
  4524. <p>Config option: <code>turn_allow_guests</code></p>
  4525. <p>Whether guests should be allowed to use the TURN server. This defaults to true, otherwise
  4526. VoIP will be unreliable for guests. However, it does introduce a slight security risk as
  4527. it allows users to connect to arbitrary endpoints without having first signed up for a valid account (e.g. by passing a CAPTCHA).</p>
  4528. <p>Example configuration:</p>
  4529. <pre><code class="language-yaml">turn_allow_guests: false
  4530. </code></pre>
  4531. <hr />
  4532. <h2 id="registration"><a class="header" href="#registration">Registration</a></h2>
  4533. <p>Registration can be rate-limited using the parameters in the <a href="usage/configuration/config_documentation.html#ratelimiting">Ratelimiting</a> section of this manual.</p>
  4534. <hr />
  4535. <p>Config option: <code>enable_registration</code></p>
  4536. <p>Enable registration for new users. Defaults to false. It is highly recommended that if you enable registration,
  4537. you use either captcha, email, or token-based verification to verify that new users are not bots. In order to enable registration
  4538. without any verification, you must also set <code>enable_registration_without_verification</code> to true.</p>
  4539. <p>Example configuration:</p>
  4540. <pre><code class="language-yaml">enable_registration: true
  4541. </code></pre>
  4542. <hr />
  4543. <p>Config option: <code>enable_registration_without_verification</code>
  4544. Enable registration without email or captcha verification. Note: this option is <em>not</em> recommended,
  4545. as registration without verification is a known vector for spam and abuse. Defaults to false. Has no effect
  4546. unless <code>enable_registration</code> is also enabled.</p>
  4547. <p>Example configuration:</p>
  4548. <pre><code class="language-yaml">enable_registration_without_verification: true
  4549. </code></pre>
  4550. <hr />
  4551. <p>Config option: <code>session_lifetime</code></p>
  4552. <p>Time that a user's session remains valid for, after they log in.</p>
  4553. <p>Note that this is not currently compatible with guest logins.</p>
  4554. <p>Note also that this is calculated at login time: changes are not applied retrospectively to users who have already
  4555. logged in.</p>
  4556. <p>By default, this is infinite.</p>
  4557. <p>Example configuration:</p>
  4558. <pre><code class="language-yaml">session_lifetime: 24h
  4559. </code></pre>
  4560. <hr />
  4561. <p>Config option: <code>refresh_access_token_lifetime</code></p>
  4562. <p>Time that an access token remains valid for, if the session is using refresh tokens.</p>
  4563. <p>For more information about refresh tokens, please see the <a href="usage/configuration/user_authentication/refresh_tokens.html">manual</a>.</p>
  4564. <p>Note that this only applies to clients which advertise support for refresh tokens.</p>
  4565. <p>Note also that this is calculated at login time and refresh time: changes are not applied to
  4566. existing sessions until they are refreshed.</p>
  4567. <p>By default, this is 5 minutes.</p>
  4568. <p>Example configuration:</p>
  4569. <pre><code class="language-yaml">refreshable_access_token_lifetime: 10m
  4570. </code></pre>
  4571. <hr />
  4572. <p>Config option: <code>refresh_token_lifetime: 24h</code></p>
  4573. <p>Time that a refresh token remains valid for (provided that it is not
  4574. exchanged for another one first).
  4575. This option can be used to automatically log-out inactive sessions.
  4576. Please see the manual for more information.</p>
  4577. <p>Note also that this is calculated at login time and refresh time:
  4578. changes are not applied to existing sessions until they are refreshed.</p>
  4579. <p>By default, this is infinite.</p>
  4580. <p>Example configuration:</p>
  4581. <pre><code class="language-yaml">refresh_token_lifetime: 24h
  4582. </code></pre>
  4583. <hr />
  4584. <p>Config option: <code>nonrefreshable_access_token_lifetime</code></p>
  4585. <p>Time that an access token remains valid for, if the session is NOT
  4586. using refresh tokens.</p>
  4587. <p>Please note that not all clients support refresh tokens, so setting
  4588. this to a short value may be inconvenient for some users who will
  4589. then be logged out frequently.</p>
  4590. <p>Note also that this is calculated at login time: changes are not applied
  4591. retrospectively to existing sessions for users that have already logged in.</p>
  4592. <p>By default, this is infinite.</p>
  4593. <p>Example configuration:</p>
  4594. <pre><code class="language-yaml">nonrefreshable_access_token_lifetime: 24h
  4595. </code></pre>
  4596. <hr />
  4597. <p>Config option: <code>registrations_require_3pid</code></p>
  4598. <p>If this is set, the user must provide all of the specified types of 3PID when registering.</p>
  4599. <p>Example configuration:</p>
  4600. <pre><code class="language-yaml">registrations_require_3pid:
  4601. - email
  4602. - msisdn
  4603. </code></pre>
  4604. <hr />
  4605. <p>Config option: <code>disable_msisdn_registration</code></p>
  4606. <p>Explicitly disable asking for MSISDNs from the registration
  4607. flow (overrides <code>registrations_require_3pid</code> if MSISDNs are set as required).</p>
  4608. <p>Example configuration:</p>
  4609. <pre><code class="language-yaml">disable_msisdn_registration: true
  4610. </code></pre>
  4611. <hr />
  4612. <p>Config option: <code>allowed_local_3pids</code></p>
  4613. <p>Mandate that users are only allowed to associate certain formats of
  4614. 3PIDs with accounts on this server, as specified by the <code>medium</code> and <code>pattern</code> sub-options.</p>
  4615. <p>Example configuration:</p>
  4616. <pre><code class="language-yaml">allowed_local_3pids:
  4617. - medium: email
  4618. pattern: '^[^@]+@matrix\.org$'
  4619. - medium: email
  4620. pattern: '^[^@]+@vector\.im$'
  4621. - medium: msisdn
  4622. pattern: '\+44'
  4623. </code></pre>
  4624. <hr />
  4625. <p>Config option: <code>enable_3pid_lookup</code></p>
  4626. <p>Enable 3PIDs lookup requests to identity servers from this server. Defaults to true.</p>
  4627. <p>Example configuration:</p>
  4628. <pre><code class="language-yaml">enable_3pid_lookup: false
  4629. </code></pre>
  4630. <hr />
  4631. <p>Config option: <code>registration_requires_token</code></p>
  4632. <p>Require users to submit a token during registration.
  4633. Tokens can be managed using the admin <a href="usage/configuration/../administration/admin_api/registration_tokens.html">API</a>.
  4634. Note that <code>enable_registration</code> must be set to true.
  4635. Disabling this option will not delete any tokens previously generated.
  4636. Defaults to false. Set to true to enable.</p>
  4637. <p>Example configuration:</p>
  4638. <pre><code class="language-yaml">registration_requires_token: true
  4639. </code></pre>
  4640. <hr />
  4641. <p>Config option: <code>registration_shared_secret</code></p>
  4642. <p>If set, allows registration of standard or admin accounts by anyone who
  4643. has the shared secret, even if registration is otherwise disabled.</p>
  4644. <p>Example configuration:</p>
  4645. <pre><code class="language-yaml">registration_shared_secret: &lt;PRIVATE STRING&gt;
  4646. </code></pre>
  4647. <hr />
  4648. <p>Config option: <code>bcrypt_rounds</code></p>
  4649. <p>Set the number of bcrypt rounds used to generate password hash.
  4650. Larger numbers increase the work factor needed to generate the hash.
  4651. The default number is 12 (which equates to 2^12 rounds).
  4652. N.B. that increasing this will exponentially increase the time required
  4653. to register or login - e.g. 24 =&gt; 2^24 rounds which will take &gt;20 mins.
  4654. Example configuration:</p>
  4655. <pre><code class="language-yaml">bcrypt_rounds: 14
  4656. </code></pre>
  4657. <hr />
  4658. <p>Config option: <code>allow_guest_access</code></p>
  4659. <p>Allows users to register as guests without a password/email/etc, and
  4660. participate in rooms hosted on this server which have been made
  4661. accessible to anonymous users. Defaults to false.</p>
  4662. <p>Example configuration:</p>
  4663. <pre><code class="language-yaml">allow_guest_access: true
  4664. </code></pre>
  4665. <hr />
  4666. <p>Config option: <code>default_identity_server</code></p>
  4667. <p>The identity server which we suggest that clients should use when users log
  4668. in on this server.</p>
  4669. <p>(By default, no suggestion is made, so it is left up to the client.
  4670. This setting is ignored unless <code>public_baseurl</code> is also explicitly set.)</p>
  4671. <p>Example configuration:</p>
  4672. <pre><code class="language-yaml">default_identity_server: https://matrix.org
  4673. </code></pre>
  4674. <hr />
  4675. <p>Config option: <code>account_threepid_delegates</code></p>
  4676. <p>Handle threepid (email/phone etc) registration and password resets through a set of
  4677. <em>trusted</em> identity servers. Note that this allows the configured identity server to
  4678. reset passwords for accounts!</p>
  4679. <p>Be aware that if <code>email</code> is not set, and SMTP options have not been
  4680. configured in the email config block, registration and user password resets via
  4681. email will be globally disabled.</p>
  4682. <p>Additionally, if <code>msisdn</code> is not set, registration and password resets via msisdn
  4683. will be disabled regardless, and users will not be able to associate an msisdn
  4684. identifier to their account. This is due to Synapse currently not supporting
  4685. any method of sending SMS messages on its own.</p>
  4686. <p>To enable using an identity server for operations regarding a particular third-party
  4687. identifier type, set the value to the URL of that identity server as shown in the
  4688. examples below.</p>
  4689. <p>Servers handling the these requests must answer the <code>/requestToken</code> endpoints defined
  4690. by the Matrix Identity Service API <a href="https://matrix.org/docs/spec/identity_service/latest">specification</a>.</p>
  4691. <p>Example configuration:</p>
  4692. <pre><code class="language-yaml">account_threepid_delegates:
  4693. email: https://example.com # Delegate email sending to example.com
  4694. msisdn: http://localhost:8090 # Delegate SMS sending to this local process
  4695. </code></pre>
  4696. <hr />
  4697. <p>Config option: <code>enable_set_displayname</code></p>
  4698. <p>Whether users are allowed to change their displayname after it has
  4699. been initially set. Useful when provisioning users based on the
  4700. contents of a third-party directory.</p>
  4701. <p>Does not apply to server administrators. Defaults to true.</p>
  4702. <p>Example configuration:</p>
  4703. <pre><code class="language-yaml">enable_set_displayname: false
  4704. </code></pre>
  4705. <hr />
  4706. <p>Config option: <code>enable_set_avatar_url</code></p>
  4707. <p>Whether users are allowed to change their avatar after it has been
  4708. initially set. Useful when provisioning users based on the contents
  4709. of a third-party directory.</p>
  4710. <p>Does not apply to server administrators. Defaults to true.</p>
  4711. <p>Example configuration:</p>
  4712. <pre><code class="language-yaml">enable_set_avatar_url: false
  4713. </code></pre>
  4714. <hr />
  4715. <p>Config option: <code>enable_3pid_changes</code></p>
  4716. <p>Whether users can change the third-party IDs associated with their accounts
  4717. (email address and msisdn).</p>
  4718. <p>Defaults to true.</p>
  4719. <p>Example configuration:</p>
  4720. <pre><code class="language-yaml">enable_3pid_changes: false
  4721. </code></pre>
  4722. <hr />
  4723. <p>Config option: <code>auto_join_rooms</code></p>
  4724. <p>Users who register on this homeserver will automatically be joined
  4725. to the rooms listed under this option.</p>
  4726. <p>By default, any room aliases included in this list will be created
  4727. as a publicly joinable room when the first user registers for the
  4728. homeserver. If the room already exists, make certain it is a publicly joinable
  4729. room, i.e. the join rule of the room must be set to 'public'. You can find more options
  4730. relating to auto-joining rooms below. </p>
  4731. <p>Example configuration:</p>
  4732. <pre><code class="language-yaml">auto_join_rooms:
  4733. - &quot;#exampleroom:example.com&quot;
  4734. - &quot;#anotherexampleroom:example.com&quot;
  4735. </code></pre>
  4736. <hr />
  4737. <p>Config option: <code>autocreate_auto_join_rooms</code></p>
  4738. <p>Where <code>auto_join_rooms</code> are specified, setting this flag ensures that
  4739. the rooms exist by creating them when the first user on the
  4740. homeserver registers.</p>
  4741. <p>By default the auto-created rooms are publicly joinable from any federated
  4742. server. Use the <code>autocreate_auto_join_rooms_federated</code> and
  4743. <code>autocreate_auto_join_room_preset</code> settings to customise this behaviour.</p>
  4744. <p>Setting to false means that if the rooms are not manually created,
  4745. users cannot be auto-joined since they do not exist.</p>
  4746. <p>Defaults to true.</p>
  4747. <p>Example configuration:</p>
  4748. <pre><code class="language-yaml">autocreate_auto_join_rooms: false
  4749. </code></pre>
  4750. <hr />
  4751. <p>Config option: <code>autocreate_auto_join_rooms_federated</code></p>
  4752. <p>Whether the rooms listen in <code>auto_join_rooms</code> that are auto-created are available
  4753. via federation. Only has an effect if <code>autocreate_auto_join_rooms</code> is true.</p>
  4754. <p>Note that whether a room is federated cannot be modified after
  4755. creation.</p>
  4756. <p>Defaults to true: the room will be joinable from other servers.
  4757. Set to false to prevent users from other homeservers from
  4758. joining these rooms.</p>
  4759. <p>Example configuration:</p>
  4760. <pre><code class="language-yaml">autocreate_auto_join_rooms_federated: false
  4761. </code></pre>
  4762. <hr />
  4763. <p>Config option: <code>autocreate_auto_join_room_preset</code></p>
  4764. <p>The room preset to use when auto-creating one of <code>auto_join_rooms</code>. Only has an
  4765. effect if <code>autocreate_auto_join_rooms</code> is true.</p>
  4766. <p>Possible values for this option are:</p>
  4767. <ul>
  4768. <li>&quot;public_chat&quot;: the room is joinable by anyone, including
  4769. federated servers if <code>autocreate_auto_join_rooms_federated</code> is true (the default).</li>
  4770. <li>&quot;private_chat&quot;: an invitation is required to join these rooms. </li>
  4771. <li>&quot;trusted_private_chat&quot;: an invitation is required to join this room and the invitee is
  4772. assigned a power level of 100 upon joining the room. </li>
  4773. </ul>
  4774. <p>If a value of &quot;private_chat&quot; or &quot;trusted_private_chat&quot; is used then
  4775. <code>auto_join_mxid_localpart</code> must also be configured.</p>
  4776. <p>Defaults to &quot;public_chat&quot;.</p>
  4777. <p>Example configuration:</p>
  4778. <pre><code class="language-yaml">autocreate_auto_join_room_preset: private_chat
  4779. </code></pre>
  4780. <hr />
  4781. <p>Config option: <code>auto_join_mxid_localpart</code></p>
  4782. <p>The local part of the user id which is used to create <code>auto_join_rooms</code> if
  4783. <code>autocreate_auto_join_rooms</code> is true. If this is not provided then the
  4784. initial user account that registers will be used to create the rooms.</p>
  4785. <p>The user id is also used to invite new users to any auto-join rooms which
  4786. are set to invite-only.</p>
  4787. <p>It <em>must</em> be configured if <code>autocreate_auto_join_room_preset</code> is set to
  4788. &quot;private_chat&quot; or &quot;trusted_private_chat&quot;.</p>
  4789. <p>Note that this must be specified in order for new users to be correctly
  4790. invited to any auto-join rooms which have been set to invite-only (either
  4791. at the time of creation or subsequently).</p>
  4792. <p>Note that, if the room already exists, this user must be joined and
  4793. have the appropriate permissions to invite new members.</p>
  4794. <p>Example configuration:</p>
  4795. <pre><code class="language-yaml">auto_join_mxid_localpart: system
  4796. </code></pre>
  4797. <hr />
  4798. <p>Config option: <code>auto_join_rooms_for_guests</code></p>
  4799. <p>When <code>auto_join_rooms</code> is specified, setting this flag to false prevents
  4800. guest accounts from being automatically joined to the rooms.</p>
  4801. <p>Defaults to true.</p>
  4802. <p>Example configuration:</p>
  4803. <pre><code class="language-yaml">auto_join_rooms_for_guests: false
  4804. </code></pre>
  4805. <hr />
  4806. <p>Config option: <code>inhibit_user_in_use_error</code></p>
  4807. <p>Whether to inhibit errors raised when registering a new account if the user ID
  4808. already exists. If turned on, requests to <code>/register/available</code> will always
  4809. show a user ID as available, and Synapse won't raise an error when starting
  4810. a registration with a user ID that already exists. However, Synapse will still
  4811. raise an error if the registration completes and the username conflicts.</p>
  4812. <p>Defaults to false.</p>
  4813. <p>Example configuration:</p>
  4814. <pre><code class="language-yaml">inhibit_user_in_use_error: true
  4815. </code></pre>
  4816. <hr />
  4817. <h2 id="metrics"><a class="header" href="#metrics">Metrics</a></h2>
  4818. <p>Config options related to metrics.</p>
  4819. <hr />
  4820. <p>Config option: <code>enable_metrics</code></p>
  4821. <p>Set to true to enable collection and rendering of performance metrics.
  4822. Defaults to false.</p>
  4823. <p>Example configuration:</p>
  4824. <pre><code class="language-yaml">enable_metrics: true
  4825. </code></pre>
  4826. <hr />
  4827. <p>Config option: <code>sentry</code></p>
  4828. <p>Use this option to enable sentry integration. Provide the DSN assigned to you by sentry
  4829. with the <code>dsn</code> setting. </p>
  4830. <p>NOTE: While attempts are made to ensure that the logs don't contain
  4831. any sensitive information, this cannot be guaranteed. By enabling
  4832. this option the sentry server may therefore receive sensitive
  4833. information, and it in turn may then disseminate sensitive information
  4834. through insecure notification channels if so configured.</p>
  4835. <p>Example configuration:</p>
  4836. <pre><code class="language-yaml">sentry:
  4837. dsn: &quot;...&quot;
  4838. </code></pre>
  4839. <hr />
  4840. <p>Config option: <code>metrics_flags</code></p>
  4841. <p>Flags to enable Prometheus metrics which are not suitable to be
  4842. enabled by default, either for performance reasons or limited use.
  4843. Currently the only option is <code>known_servers</code>, which publishes
  4844. <code>synapse_federation_known_servers</code>, a gauge of the number of
  4845. servers this homeserver knows about, including itself. May cause
  4846. performance problems on large homeservers.</p>
  4847. <p>Example configuration:</p>
  4848. <pre><code class="language-yaml">metrics_flags:
  4849. known_servers: true
  4850. </code></pre>
  4851. <hr />
  4852. <p>Config option: <code>report_stats</code></p>
  4853. <p>Whether or not to report anonymized homeserver usage statistics. This is originally
  4854. set when generating the config. Set this option to true or false to change the current
  4855. behavior. </p>
  4856. <p>Example configuration:</p>
  4857. <pre><code class="language-yaml">report_stats: true
  4858. </code></pre>
  4859. <hr />
  4860. <p>Config option: <code>report_stats_endpoint</code></p>
  4861. <p>The endpoint to report the anonymized homeserver usage statistics to.
  4862. Defaults to https://matrix.org/report-usage-stats/push</p>
  4863. <p>Example configuration:</p>
  4864. <pre><code class="language-yaml">report_stats_endpoint: https://example.com/report-usage-stats/push
  4865. </code></pre>
  4866. <hr />
  4867. <h2 id="api-configuration"><a class="header" href="#api-configuration">API Configuration</a></h2>
  4868. <p>Config settings related to the client/server API</p>
  4869. <hr />
  4870. <p>Config option: <code>room_prejoin_state:</code></p>
  4871. <p>Controls for the state that is shared with users who receive an invite
  4872. to a room. By default, the following state event types are shared with users who
  4873. receive invites to the room:</p>
  4874. <ul>
  4875. <li>m.room.join_rules</li>
  4876. <li>m.room.canonical_alias</li>
  4877. <li>m.room.avatar</li>
  4878. <li>m.room.encryption</li>
  4879. <li>m.room.name</li>
  4880. <li>m.room.create</li>
  4881. <li>m.room.topic</li>
  4882. </ul>
  4883. <p>To change the default behavior, use the following sub-options:</p>
  4884. <ul>
  4885. <li><code>disable_default_event_types</code>: set to true to disable the above defaults. If this
  4886. is enabled, only the event types listed in <code>additional_event_types</code> are shared.
  4887. Defaults to false.</li>
  4888. <li><code>additional_event_types</code>: Additional state event types to share with users when they are invited
  4889. to a room. By default, this list is empty (so only the default event types are shared).</li>
  4890. </ul>
  4891. <p>Example configuration:</p>
  4892. <pre><code class="language-yaml">room_prejoin_state:
  4893. disable_default_event_types: true
  4894. additional_event_types:
  4895. - org.example.custom.event.type
  4896. - m.room.join_rules
  4897. </code></pre>
  4898. <hr />
  4899. <p>Config option: <code>track_puppeted_user_ips</code></p>
  4900. <p>We record the IP address of clients used to access the API for various
  4901. reasons, including displaying it to the user in the &quot;Where you're signed in&quot;
  4902. dialog.</p>
  4903. <p>By default, when puppeting another user via the admin API, the client IP
  4904. address is recorded against the user who created the access token (ie, the
  4905. admin user), and <em>not</em> the puppeted user.</p>
  4906. <p>Set this option to true to also record the IP address against the puppeted
  4907. user. (This also means that the puppeted user will count as an &quot;active&quot; user
  4908. for the purpose of monthly active user tracking - see <code>limit_usage_by_mau</code> etc
  4909. above.)</p>
  4910. <p>Example configuration:</p>
  4911. <pre><code class="language-yaml">track_puppeted_user_ips: true
  4912. </code></pre>
  4913. <hr />
  4914. <p>Config option: <code>app_service_config_files</code></p>
  4915. <p>A list of application service config files to use.</p>
  4916. <p>Example configuration:</p>
  4917. <pre><code class="language-yaml">app_service_config_files:
  4918. - app_service_1.yaml
  4919. - app_service_2.yaml
  4920. </code></pre>
  4921. <hr />
  4922. <p>Config option: <code>track_appservice_user_ips</code></p>
  4923. <p>Defaults to false. Set to true to enable tracking of application service IP addresses.
  4924. Implicitly enables MAU tracking for application service users.</p>
  4925. <p>Example configuration:</p>
  4926. <pre><code class="language-yaml">track_appservice_user_ips: true
  4927. </code></pre>
  4928. <hr />
  4929. <p>Config option: <code>macaroon_secret_key</code></p>
  4930. <p>A secret which is used to sign access tokens. If none is specified,
  4931. the <code>registration_shared_secret</code> is used, if one is given; otherwise,
  4932. a secret key is derived from the signing key.</p>
  4933. <p>Example configuration:</p>
  4934. <pre><code class="language-yaml">macaroon_secret_key: &lt;PRIVATE STRING&gt;
  4935. </code></pre>
  4936. <hr />
  4937. <p>Config option: <code>form_secret</code></p>
  4938. <p>A secret which is used to calculate HMACs for form values, to stop
  4939. falsification of values. Must be specified for the User Consent
  4940. forms to work.</p>
  4941. <p>Example configuration:</p>
  4942. <pre><code class="language-yaml">form_secret: &lt;PRIVATE STRING&gt;
  4943. </code></pre>
  4944. <hr />
  4945. <h2 id="signing-keys"><a class="header" href="#signing-keys">Signing Keys</a></h2>
  4946. <p>Config options relating to signing keys</p>
  4947. <hr />
  4948. <p>Config option: <code>signing_key_path</code></p>
  4949. <p>Path to the signing key to sign messages with.</p>
  4950. <p>Example configuration:</p>
  4951. <pre><code class="language-yaml">signing_key_path: &quot;CONFDIR/SERVERNAME.signing.key&quot;
  4952. </code></pre>
  4953. <hr />
  4954. <p>Config option: <code>old_signing_keys</code></p>
  4955. <p>The keys that the server used to sign messages with but won't use
  4956. to sign new messages. For each key, <code>key</code> should be the base64-encoded public key, and
  4957. <code>expired_ts</code>should be the time (in milliseconds since the unix epoch) that
  4958. it was last used.</p>
  4959. <p>It is possible to build an entry from an old <code>signing.key</code> file using the
  4960. <code>export_signing_key</code> script which is provided with synapse.</p>
  4961. <p>Example configuration:</p>
  4962. <pre><code class="language-yaml">old_signing_keys:
  4963. &quot;ed25519:id&quot;: { key: &quot;base64string&quot;, expired_ts: 123456789123 }
  4964. </code></pre>
  4965. <hr />
  4966. <p>Config option: <code>key_refresh_interval</code></p>
  4967. <p>How long key response published by this server is valid for.
  4968. Used to set the <code>valid_until_ts</code> in <code>/key/v2</code> APIs.
  4969. Determines how quickly servers will query to check which keys
  4970. are still valid. Defaults to 1d.</p>
  4971. <p>Example configuration:</p>
  4972. <pre><code class="language-yaml">key_refresh_interval: 2d
  4973. </code></pre>
  4974. <hr />
  4975. <p>Config option: <code>trusted_key_servers:</code></p>
  4976. <p>The trusted servers to download signing keys from.</p>
  4977. <p>When we need to fetch a signing key, each server is tried in parallel.</p>
  4978. <p>Normally, the connection to the key server is validated via TLS certificates.
  4979. Additional security can be provided by configuring a <code>verify key</code>, which
  4980. will make synapse check that the response is signed by that key.</p>
  4981. <p>This setting supercedes an older setting named <code>perspectives</code>. The old format
  4982. is still supported for backwards-compatibility, but it is deprecated.</p>
  4983. <p><code>trusted_key_servers</code> defaults to matrix.org, but using it will generate a
  4984. warning on start-up. To suppress this warning, set
  4985. <code>suppress_key_server_warning</code> to true.</p>
  4986. <p>Options for each entry in the list include:</p>
  4987. <ul>
  4988. <li><code>server_name</code>: the name of the server. Required.</li>
  4989. <li><code>verify_keys</code>: an optional map from key id to base64-encoded public key.
  4990. If specified, we will check that the response is signed by at least
  4991. one of the given keys.</li>
  4992. <li><code>accept_keys_insecurely</code>: a boolean. Normally, if <code>verify_keys</code> is unset,
  4993. and <code>federation_verify_certificates</code> is not <code>true</code>, synapse will refuse
  4994. to start, because this would allow anyone who can spoof DNS responses
  4995. to masquerade as the trusted key server. If you know what you are doing
  4996. and are sure that your network environment provides a secure connection
  4997. to the key server, you can set this to <code>true</code> to override this behaviour.</li>
  4998. </ul>
  4999. <p>Example configuration #1:</p>
  5000. <pre><code class="language-yaml">trusted_key_servers:
  5001. - server_name: &quot;my_trusted_server.example.com&quot;
  5002. verify_keys:
  5003. &quot;ed25519:auto&quot;: &quot;abcdefghijklmnopqrstuvwxyzabcdefghijklmopqr&quot;
  5004. - server_name: &quot;my_other_trusted_server.example.com&quot;
  5005. </code></pre>
  5006. <p>Example configuration #2:</p>
  5007. <pre><code class="language-yaml">trusted_key_servers:
  5008. - server_name: &quot;matrix.org&quot;
  5009. </code></pre>
  5010. <hr />
  5011. <p>Config option: <code>suppress_key_server_warning</code></p>
  5012. <p>Set the following to true to disable the warning that is emitted when the
  5013. <code>trusted_key_servers</code> include 'matrix.org'. See above.</p>
  5014. <p>Example configuration:</p>
  5015. <pre><code class="language-yaml">suppress_key_server_warning: true
  5016. </code></pre>
  5017. <hr />
  5018. <p>Config option: <code>key_server_signing_keys_path</code></p>
  5019. <p>The signing keys to use when acting as a trusted key server. If not specified
  5020. defaults to the server signing key.</p>
  5021. <p>Can contain multiple keys, one per line.</p>
  5022. <p>Example configuration:</p>
  5023. <pre><code class="language-yaml">key_server_signing_keys_path: &quot;key_server_signing_keys.key&quot;
  5024. </code></pre>
  5025. <hr />
  5026. <h2 id="single-sign-on-integration"><a class="header" href="#single-sign-on-integration">Single sign-on integration</a></h2>
  5027. <p>The following settings can be used to make Synapse use a single sign-on
  5028. provider for authentication, instead of its internal password database.</p>
  5029. <p>You will probably also want to set the following options to false to
  5030. disable the regular login/registration flows:</p>
  5031. <ul>
  5032. <li><code>enable_registration</code></li>
  5033. <li><code>password_config.enabled</code></li>
  5034. </ul>
  5035. <p>You will also want to investigate the settings under the &quot;sso&quot; configuration
  5036. section below.</p>
  5037. <hr />
  5038. <p>Config option: <code>saml2_config</code></p>
  5039. <p>Enable SAML2 for registration and login. Uses pysaml2. To learn more about pysaml and
  5040. to find a full list options for configuring pysaml, read the docs <a href="https://pysaml2.readthedocs.io/en/latest/">here</a>.</p>
  5041. <p>At least one of <code>sp_config</code> or <code>config_path</code> must be set in this section to
  5042. enable SAML login. You can either put your entire pysaml config inline using the <code>sp_config</code>
  5043. option, or you can specify a path to a psyaml config file with the sub-option <code>config_path</code>.
  5044. This setting has the following sub-options:</p>
  5045. <ul>
  5046. <li><code>sp_config</code>: the configuration for the pysaml2 Service Provider. See pysaml2 docs for format of config.
  5047. Default values will be used for the <code>entityid</code> and <code>service</code> settings,
  5048. so it is not normally necessary to specify them unless you need to
  5049. override them. Here are a few useful sub-options for configuring pysaml:
  5050. <ul>
  5051. <li><code>metadata</code>: Point this to the IdP's metadata. You must provide either a local
  5052. file via the <code>local</code> attribute or (preferably) a URL via the
  5053. <code>remote</code> attribute.</li>
  5054. <li><code>accepted_time_diff: 3</code>: Allowed clock difference in seconds between the homeserver and IdP.
  5055. Defaults to 0.</li>
  5056. <li><code>service</code>: By default, the user has to go to our login page first. If you'd like
  5057. to allow IdP-initiated login, set <code>allow_unsolicited</code> to true under <code>sp</code> in the <code>service</code>
  5058. section.</li>
  5059. </ul>
  5060. </li>
  5061. <li><code>config_path</code>: specify a separate pysaml2 configuration file thusly:
  5062. <code>config_path: &quot;CONFDIR/sp_conf.py&quot;</code></li>
  5063. <li><code>saml_session_lifetime</code>: The lifetime of a SAML session. This defines how long a user has to
  5064. complete the authentication process, if <code>allow_unsolicited</code> is unset. The default is 15 minutes.</li>
  5065. <li><code>user_mapping_provider</code>: Using this option, an external module can be provided as a
  5066. custom solution to mapping attributes returned from a saml provider onto a matrix user. The
  5067. <code>user_mapping_provider</code> has the following attributes:
  5068. <ul>
  5069. <li><code>module</code>: The custom module's class. </li>
  5070. <li><code>config</code>: Custom configuration values for the module. Use the values provided in the
  5071. example if you are using the built-in user_mapping_provider, or provide your own
  5072. config values for a custom class if you are using one. This section will be passed as a Python
  5073. dictionary to the module's <code>parse_config</code> method. The built-in provider takes the following two
  5074. options:
  5075. <ul>
  5076. <li><code>mxid_source_attribute</code>: The SAML attribute (after mapping via the attribute maps) to use
  5077. to derive the Matrix ID from. It is 'uid' by default. Note: This used to be configured by the
  5078. <code>saml2_config.mxid_source_attribute option</code>. If that is still defined, its value will be used instead.</li>
  5079. <li><code>mxid_mapping</code>: The mapping system to use for mapping the saml attribute onto a
  5080. matrix ID. Options include: <code>hexencode</code> (which maps unpermitted characters to '=xx')
  5081. and <code>dotreplace</code> (which replaces unpermitted characters with '.').
  5082. The default is <code>hexencode</code>. Note: This used to be configured by the
  5083. <code>saml2_config.mxid_mapping option</code>. If that is still defined, its value will be used instead.</li>
  5084. </ul>
  5085. </li>
  5086. </ul>
  5087. </li>
  5088. <li><code>grandfathered_mxid_source_attribute</code>: In previous versions of synapse, the mapping from SAML attribute to
  5089. MXID was always calculated dynamically rather than stored in a table. For backwards- compatibility, we will look for <code>user_ids</code>
  5090. matching such a pattern before creating a new account. This setting controls the SAML attribute which will be used for this
  5091. backwards-compatibility lookup. Typically it should be 'uid', but if the attribute maps are changed, it may be necessary to change it.
  5092. The default is 'uid'. </li>
  5093. <li><code>attribute_requirements</code>: It is possible to configure Synapse to only allow logins if SAML attributes
  5094. match particular values. The requirements can be listed under
  5095. <code>attribute_requirements</code> as shown in the example. All of the listed attributes must
  5096. match for the login to be permitted.</li>
  5097. <li><code>idp_entityid</code>: If the metadata XML contains multiple IdP entities then the <code>idp_entityid</code>
  5098. option must be set to the entity to redirect users to.
  5099. Most deployments only have a single IdP entity and so should omit this option.</li>
  5100. </ul>
  5101. <p>Once SAML support is enabled, a metadata file will be exposed at
  5102. <code>https://&lt;server&gt;:&lt;port&gt;/_synapse/client/saml2/metadata.xml</code>, which you may be able to
  5103. use to configure your SAML IdP with. Alternatively, you can manually configure
  5104. the IdP to use an ACS location of
  5105. <code>https://&lt;server&gt;:&lt;port&gt;/_synapse/client/saml2/authn_response</code>.</p>
  5106. <p>Example configuration:</p>
  5107. <pre><code class="language-yaml">saml2_config:
  5108. sp_config:
  5109. metadata:
  5110. local: [&quot;saml2/idp.xml&quot;]
  5111. remote:
  5112. - url: https://our_idp/metadata.xml
  5113. accepted_time_diff: 3
  5114. service:
  5115. sp:
  5116. allow_unsolicited: true
  5117. # The examples below are just used to generate our metadata xml, and you
  5118. # may well not need them, depending on your setup. Alternatively you
  5119. # may need a whole lot more detail - see the pysaml2 docs!
  5120. description: [&quot;My awesome SP&quot;, &quot;en&quot;]
  5121. name: [&quot;Test SP&quot;, &quot;en&quot;]
  5122. ui_info:
  5123. display_name:
  5124. - lang: en
  5125. text: &quot;Display Name is the descriptive name of your service.&quot;
  5126. description:
  5127. - lang: en
  5128. text: &quot;Description should be a short paragraph explaining the purpose of the service.&quot;
  5129. information_url:
  5130. - lang: en
  5131. text: &quot;https://example.com/terms-of-service&quot;
  5132. privacy_statement_url:
  5133. - lang: en
  5134. text: &quot;https://example.com/privacy-policy&quot;
  5135. keywords:
  5136. - lang: en
  5137. text: [&quot;Matrix&quot;, &quot;Element&quot;]
  5138. logo:
  5139. - lang: en
  5140. text: &quot;https://example.com/logo.svg&quot;
  5141. width: &quot;200&quot;
  5142. height: &quot;80&quot;
  5143. organization:
  5144. name: Example com
  5145. display_name:
  5146. - [&quot;Example co&quot;, &quot;en&quot;]
  5147. url: &quot;http://example.com&quot;
  5148. contact_person:
  5149. - given_name: Bob
  5150. sur_name: &quot;the Sysadmin&quot;
  5151. email_address&quot;: [&quot;admin@example.com&quot;]
  5152. contact_type&quot;: technical
  5153. saml_session_lifetime: 5m
  5154. user_mapping_provider:
  5155. # Below options are intended for the built-in provider, they should be
  5156. # changed if using a custom module.
  5157. config:
  5158. mxid_source_attribute: displayName
  5159. mxid_mapping: dotreplace
  5160. grandfathered_mxid_source_attribute: upn
  5161. attribute_requirements:
  5162. - attribute: userGroup
  5163. value: &quot;staff&quot;
  5164. - attribute: department
  5165. value: &quot;sales&quot;
  5166. idp_entityid: 'https://our_idp/entityid'
  5167. </code></pre>
  5168. <hr />
  5169. <p>Config option: <code>oidc_providers</code></p>
  5170. <p>List of OpenID Connect (OIDC) / OAuth 2.0 identity providers, for registration
  5171. and login. See <a href="usage/configuration/../../openid.html">here</a>
  5172. for information on how to configure these options.</p>
  5173. <p>For backwards compatibility, it is also possible to configure a single OIDC
  5174. provider via an <code>oidc_config</code> setting. This is now deprecated and admins are
  5175. advised to migrate to the <code>oidc_providers</code> format. (When doing that migration,
  5176. use <code>oidc</code> for the <code>idp_id</code> to ensure that existing users continue to be
  5177. recognised.)</p>
  5178. <p>Options for each entry include:</p>
  5179. <ul>
  5180. <li>
  5181. <p><code>idp_id</code>: a unique identifier for this identity provider. Used internally
  5182. by Synapse; should be a single word such as 'github'.
  5183. Note that, if this is changed, users authenticating via that provider
  5184. will no longer be recognised as the same user!
  5185. (Use &quot;oidc&quot; here if you are migrating from an old <code>oidc_config</code> configuration.)</p>
  5186. </li>
  5187. <li>
  5188. <p><code>idp_name</code>: A user-facing name for this identity provider, which is used to
  5189. offer the user a choice of login mechanisms.</p>
  5190. </li>
  5191. <li>
  5192. <p><code>idp_icon</code>: An optional icon for this identity provider, which is presented
  5193. by clients and Synapse's own IdP picker page. If given, must be an
  5194. MXC URI of the format mxc://<server-name>/<media-id>. (An easy way to
  5195. obtain such an MXC URI is to upload an image to an (unencrypted) room
  5196. and then copy the &quot;url&quot; from the source of the event.)</p>
  5197. </li>
  5198. <li>
  5199. <p><code>idp_brand</code>: An optional brand for this identity provider, allowing clients
  5200. to style the login flow according to the identity provider in question.
  5201. See the <a href="https://spec.matrix.org/latest/">spec</a> for possible options here.</p>
  5202. </li>
  5203. <li>
  5204. <p><code>discover</code>: set to false to disable the use of the OIDC discovery mechanism
  5205. to discover endpoints. Defaults to true.</p>
  5206. </li>
  5207. <li>
  5208. <p><code>issuer</code>: Required. The OIDC issuer. Used to validate tokens and (if discovery
  5209. is enabled) to discover the provider's endpoints.</p>
  5210. </li>
  5211. <li>
  5212. <p><code>client_id</code>: Required. oauth2 client id to use.</p>
  5213. </li>
  5214. <li>
  5215. <p><code>client_secret</code>: oauth2 client secret to use. May be omitted if
  5216. <code>client_secret_jwt_key</code> is given, or if <code>client_auth_method</code> is 'none'.</p>
  5217. </li>
  5218. <li>
  5219. <p><code>client_secret_jwt_key</code>: Alternative to client_secret: details of a key used
  5220. to create a JSON Web Token to be used as an OAuth2 client secret. If
  5221. given, must be a dictionary with the following properties:</p>
  5222. <ul>
  5223. <li>
  5224. <p><code>key</code>: a pem-encoded signing key. Must be a suitable key for the
  5225. algorithm specified. Required unless <code>key_file</code> is given.</p>
  5226. </li>
  5227. <li>
  5228. <p><code>key_file</code>: the path to file containing a pem-encoded signing key file.
  5229. Required unless <code>key</code> is given.</p>
  5230. </li>
  5231. <li>
  5232. <p><code>jwt_header</code>: a dictionary giving properties to include in the JWT
  5233. header. Must include the key <code>alg</code>, giving the algorithm used to
  5234. sign the JWT, such as &quot;ES256&quot;, using the JWA identifiers in
  5235. RFC7518.</p>
  5236. </li>
  5237. <li>
  5238. <p><code>jwt_payload</code>: an optional dictionary giving properties to include in
  5239. the JWT payload. Normally this should include an <code>iss</code> key.</p>
  5240. </li>
  5241. </ul>
  5242. </li>
  5243. <li>
  5244. <p><code>client_auth_method</code>: auth method to use when exchanging the token. Valid
  5245. values are <code>client_secret_basic</code> (default), <code>client_secret_post</code> and
  5246. <code>none</code>.</p>
  5247. </li>
  5248. <li>
  5249. <p><code>scopes</code>: list of scopes to request. This should normally include the &quot;openid&quot;
  5250. scope. Defaults to [&quot;openid&quot;].</p>
  5251. </li>
  5252. <li>
  5253. <p><code>authorization_endpoint</code>: the oauth2 authorization endpoint. Required if
  5254. provider discovery is disabled.</p>
  5255. </li>
  5256. <li>
  5257. <p><code>token_endpoint</code>: the oauth2 token endpoint. Required if provider discovery is
  5258. disabled.</p>
  5259. </li>
  5260. <li>
  5261. <p><code>userinfo_endpoint</code>: the OIDC userinfo endpoint. Required if discovery is
  5262. disabled and the 'openid' scope is not requested.</p>
  5263. </li>
  5264. <li>
  5265. <p><code>jwks_uri</code>: URI where to fetch the JWKS. Required if discovery is disabled and
  5266. the 'openid' scope is used.</p>
  5267. </li>
  5268. <li>
  5269. <p><code>skip_verification</code>: set to 'true' to skip metadata verification. Use this if
  5270. you are connecting to a provider that is not OpenID Connect compliant.
  5271. Defaults to false. Avoid this in production.</p>
  5272. </li>
  5273. <li>
  5274. <p><code>user_profile_method</code>: Whether to fetch the user profile from the userinfo
  5275. endpoint, or to rely on the data returned in the id_token from the <code>token_endpoint</code>.
  5276. Valid values are: <code>auto</code> or <code>userinfo_endpoint</code>.
  5277. Defaults to <code>auto</code>, which uses the userinfo endpoint if <code>openid</code> is
  5278. not included in <code>scopes</code>. Set to <code>userinfo_endpoint</code> to always use the
  5279. userinfo endpoint.</p>
  5280. </li>
  5281. <li>
  5282. <p><code>allow_existing_users</code>: set to true to allow a user logging in via OIDC to
  5283. match a pre-existing account instead of failing. This could be used if
  5284. switching from password logins to OIDC. Defaults to false.</p>
  5285. </li>
  5286. <li>
  5287. <p><code>user_mapping_provider</code>: Configuration for how attributes returned from a OIDC
  5288. provider are mapped onto a matrix user. This setting has the following
  5289. sub-properties:</p>
  5290. <ul>
  5291. <li>
  5292. <p><code>module</code>: The class name of a custom mapping module. Default is
  5293. <code>synapse.handlers.oidc.JinjaOidcMappingProvider</code>.
  5294. See https://matrix-org.github.io/synapse/latest/sso_mapping_providers.html#openid-mapping-providers
  5295. for information on implementing a custom mapping provider.</p>
  5296. </li>
  5297. <li>
  5298. <p><code>config</code>: Configuration for the mapping provider module. This section will
  5299. be passed as a Python dictionary to the user mapping provider
  5300. module's <code>parse_config</code> method.</p>
  5301. <p>For the default provider, the following settings are available:</p>
  5302. <ul>
  5303. <li>
  5304. <p>subject_claim: name of the claim containing a unique identifier
  5305. for the user. Defaults to 'sub', which OpenID Connect
  5306. compliant providers should provide.</p>
  5307. </li>
  5308. <li>
  5309. <p><code>localpart_template</code>: Jinja2 template for the localpart of the MXID.
  5310. If this is not set, the user will be prompted to choose their
  5311. own username (see the documentation for the <code>sso_auth_account_details.html</code>
  5312. template). This template can use the <code>localpart_from_email</code> filter.</p>
  5313. </li>
  5314. <li>
  5315. <p><code>confirm_localpart</code>: Whether to prompt the user to validate (or
  5316. change) the generated localpart (see the documentation for the
  5317. 'sso_auth_account_details.html' template), instead of
  5318. registering the account right away.</p>
  5319. </li>
  5320. <li>
  5321. <p><code>display_name_template</code>: Jinja2 template for the display name to set
  5322. on first login. If unset, no displayname will be set.</p>
  5323. </li>
  5324. <li>
  5325. <p><code>email_template</code>: Jinja2 template for the email address of the user.
  5326. If unset, no email address will be added to the account.</p>
  5327. </li>
  5328. <li>
  5329. <p><code>extra_attributes</code>: a map of Jinja2 templates for extra attributes
  5330. to send back to the client during login. Note that these are non-standard and clients will ignore them
  5331. without modifications.</p>
  5332. </li>
  5333. </ul>
  5334. </li>
  5335. </ul>
  5336. <p>When rendering, the Jinja2 templates are given a 'user' variable,
  5337. which is set to the claims returned by the UserInfo Endpoint and/or
  5338. in the ID Token.</p>
  5339. </li>
  5340. </ul>
  5341. <p>It is possible to configure Synapse to only allow logins if certain attributes
  5342. match particular values in the OIDC userinfo. The requirements can be listed under
  5343. <code>attribute_requirements</code> as shown here:</p>
  5344. <pre><code class="language-yaml">attribute_requirements:
  5345. - attribute: family_name
  5346. value: &quot;Stephensson&quot;
  5347. - attribute: groups
  5348. value: &quot;admin&quot;
  5349. </code></pre>
  5350. <p>All of the listed attributes must match for the login to be permitted. Additional attributes can be added to
  5351. userinfo by expanding the <code>scopes</code> section of the OIDC config to retrieve
  5352. additional information from the OIDC provider.</p>
  5353. <p>If the OIDC claim is a list, then the attribute must match any value in the list.
  5354. Otherwise, it must exactly match the value of the claim. Using the example
  5355. above, the <code>family_name</code> claim MUST be &quot;Stephensson&quot;, but the <code>groups</code>
  5356. claim MUST contain &quot;admin&quot;.</p>
  5357. <p>Example configuration:</p>
  5358. <pre><code class="language-yaml">oidc_providers:
  5359. # Generic example
  5360. #
  5361. - idp_id: my_idp
  5362. idp_name: &quot;My OpenID provider&quot;
  5363. idp_icon: &quot;mxc://example.com/mediaid&quot;
  5364. discover: false
  5365. issuer: &quot;https://accounts.example.com/&quot;
  5366. client_id: &quot;provided-by-your-issuer&quot;
  5367. client_secret: &quot;provided-by-your-issuer&quot;
  5368. client_auth_method: client_secret_post
  5369. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5370. authorization_endpoint: &quot;https://accounts.example.com/oauth2/auth&quot;
  5371. token_endpoint: &quot;https://accounts.example.com/oauth2/token&quot;
  5372. userinfo_endpoint: &quot;https://accounts.example.com/userinfo&quot;
  5373. jwks_uri: &quot;https://accounts.example.com/.well-known/jwks.json&quot;
  5374. skip_verification: true
  5375. user_mapping_provider:
  5376. config:
  5377. subject_claim: &quot;id&quot;
  5378. localpart_template: &quot;{{ user.login }}&quot;
  5379. display_name_template: &quot;{{ user.name }}&quot;
  5380. email_template: &quot;{{ user.email }}&quot;
  5381. attribute_requirements:
  5382. - attribute: userGroup
  5383. value: &quot;synapseUsers&quot;
  5384. </code></pre>
  5385. <hr />
  5386. <p>Config option: <code>cas_config</code></p>
  5387. <p>Enable Central Authentication Service (CAS) for registration and login.
  5388. Has the following sub-options:</p>
  5389. <ul>
  5390. <li><code>enabled</code>: Set this to true to enable authorization against a CAS server.
  5391. Defaults to false.</li>
  5392. <li><code>server_url</code>: The URL of the CAS authorization endpoint.</li>
  5393. <li><code>displayname_attribute</code>: The attribute of the CAS response to use as the display name.
  5394. If no name is given here, no displayname will be set.</li>
  5395. <li><code>required_attributes</code>: It is possible to configure Synapse to only allow logins if CAS attributes
  5396. match particular values. All of the keys given below must exist
  5397. and the values must match the given value. Alternately if the given value
  5398. is <code>None</code> then any value is allowed (the attribute just must exist).
  5399. All of the listed attributes must match for the login to be permitted.</li>
  5400. </ul>
  5401. <p>Example configuration:</p>
  5402. <pre><code class="language-yaml">cas_config:
  5403. enabled: true
  5404. server_url: &quot;https://cas-server.com&quot;
  5405. displayname_attribute: name
  5406. required_attributes:
  5407. userGroup: &quot;staff&quot;
  5408. department: None
  5409. </code></pre>
  5410. <hr />
  5411. <p>Config option: <code>sso</code></p>
  5412. <p>Additional settings to use with single-sign on systems such as OpenID Connect,
  5413. SAML2 and CAS.</p>
  5414. <p>Server admins can configure custom templates for pages related to SSO. See
  5415. <a href="usage/configuration/../../templates.html">here</a> for more information.</p>
  5416. <p>Options include:</p>
  5417. <ul>
  5418. <li><code>client_whitelist</code>: A list of client URLs which are whitelisted so that the user does not
  5419. have to confirm giving access to their account to the URL. Any client
  5420. whose URL starts with an entry in the following list will not be subject
  5421. to an additional confirmation step after the SSO login is completed.
  5422. WARNING: An entry such as &quot;https://my.client&quot; is insecure, because it
  5423. will also match &quot;https://my.client.evil.site&quot;, exposing your users to
  5424. phishing attacks from evil.site. To avoid this, include a slash after the
  5425. hostname: &quot;https://my.client/&quot;.
  5426. The login fallback page (used by clients that don't natively support the
  5427. required login flows) is whitelisted in addition to any URLs in this list.
  5428. By default, this list contains only the login fallback page.</li>
  5429. <li><code>update_profile_information</code>: Use this setting to keep a user's profile fields in sync with information from
  5430. the identity provider. Currently only syncing the displayname is supported. Fields
  5431. are checked on every SSO login, and are updated if necessary.
  5432. Note that enabling this option will override user profile information,
  5433. regardless of whether users have opted-out of syncing that
  5434. information when first signing in. Defaults to false.</li>
  5435. </ul>
  5436. <p>Example configuration:</p>
  5437. <pre><code class="language-yaml">sso:
  5438. client_whitelist:
  5439. - https://riot.im/develop
  5440. - https://my.custom.client/
  5441. update_profile_information: true
  5442. </code></pre>
  5443. <hr />
  5444. <p>Config option: <code>jwt_config</code></p>
  5445. <p>JSON web token integration. The following settings can be used to make
  5446. Synapse JSON web tokens for authentication, instead of its internal
  5447. password database.</p>
  5448. <p>Each JSON Web Token needs to contain a &quot;sub&quot; (subject) claim, which is
  5449. used as the localpart of the mxid.</p>
  5450. <p>Additionally, the expiration time (&quot;exp&quot;), not before time (&quot;nbf&quot;),
  5451. and issued at (&quot;iat&quot;) claims are validated if present.</p>
  5452. <p>Note that this is a non-standard login type and client support is
  5453. expected to be non-existent.</p>
  5454. <p>See <a href="usage/configuration/../../jwt.html">here</a> for more.</p>
  5455. <p>Additional sub-options for this setting include:</p>
  5456. <ul>
  5457. <li><code>enabled</code>: Set to true to enable authorization using JSON web
  5458. tokens. Defaults to false.</li>
  5459. <li><code>secret</code>: This is either the private shared secret or the public key used to
  5460. decode the contents of the JSON web token. Required if <code>enabled</code> is set to true.</li>
  5461. <li><code>algorithm</code>: The algorithm used to sign the JSON web token. Supported algorithms are listed at
  5462. https://pyjwt.readthedocs.io/en/latest/algorithms.html Required if <code>enabled</code> is set to true.</li>
  5463. <li><code>subject_claim</code>: Name of the claim containing a unique identifier for the user.
  5464. Optional, defaults to <code>sub</code>.</li>
  5465. <li><code>issuer</code>: The issuer to validate the &quot;iss&quot; claim against. Optional. If provided the
  5466. &quot;iss&quot; claim will be required and validated for all JSON web tokens.</li>
  5467. <li><code>audiences</code>: A list of audiences to validate the &quot;aud&quot; claim against. Optional.
  5468. If provided the &quot;aud&quot; claim will be required and validated for all JSON web tokens.
  5469. Note that if the &quot;aud&quot; claim is included in a JSON web token then
  5470. validation will fail without configuring audiences.</li>
  5471. </ul>
  5472. <p>Example configuration:</p>
  5473. <pre><code class="language-yaml">jwt_config:
  5474. enabled: true
  5475. secret: &quot;provided-by-your-issuer&quot;
  5476. algorithm: &quot;provided-by-your-issuer&quot;
  5477. subject_claim: &quot;name_of_claim&quot;
  5478. issuer: &quot;provided-by-your-issuer&quot;
  5479. audiences:
  5480. - &quot;provided-by-your-issuer&quot;
  5481. </code></pre>
  5482. <hr />
  5483. <p>Config option: <code>password_config</code></p>
  5484. <p>Use this setting to enable password-based logins. </p>
  5485. <p>This setting has the following sub-options:</p>
  5486. <ul>
  5487. <li><code>enabled</code>: Defaults to true.
  5488. Set to false to disable password authentication.
  5489. Set to <code>only_for_reauth</code> to allow users with existing passwords to use them
  5490. to log in and reauthenticate, whilst preventing new users from setting passwords.</li>
  5491. <li><code>localdb_enabled</code>: Set to false to disable authentication against the local password
  5492. database. This is ignored if <code>enabled</code> is false, and is only useful
  5493. if you have other <code>password_providers</code>. Defaults to true. </li>
  5494. <li><code>pepper</code>: Set the value here to a secret random string for extra security. # Uncomment and change to a secret random string for extra security.
  5495. DO NOT CHANGE THIS AFTER INITIAL SETUP!</li>
  5496. <li><code>policy</code>: Define and enforce a password policy, such as minimum lengths for passwords, etc.
  5497. Each parameter is optional. This is an implementation of MSC2000. Parameters are as follows:
  5498. <ul>
  5499. <li><code>enabled</code>: Defaults to false. Set to true to enable.</li>
  5500. <li><code>minimum_length</code>: Minimum accepted length for a password. Defaults to 0.</li>
  5501. <li><code>require_digit</code>: Whether a password must contain at least one digit.
  5502. Defaults to false.</li>
  5503. <li><code>require_symbol</code>: Whether a password must contain at least one symbol.
  5504. A symbol is any character that's not a number or a letter. Defaults to false.</li>
  5505. <li><code>require_lowercase</code>: Whether a password must contain at least one lowercase letter.
  5506. Defaults to false.</li>
  5507. <li><code>require_uppercase</code>: Whether a password must contain at least one uppercase letter.
  5508. Defaults to false.</li>
  5509. </ul>
  5510. </li>
  5511. </ul>
  5512. <p>Example configuration:</p>
  5513. <pre><code class="language-yaml">password_config:
  5514. enabled: false
  5515. localdb_enabled: false
  5516. pepper: &quot;EVEN_MORE_SECRET&quot;
  5517. policy:
  5518. enabled: true
  5519. minimum_length: 15
  5520. require_digit: true
  5521. require_symbol: true
  5522. require_lowercase: true
  5523. require_uppercase: true
  5524. </code></pre>
  5525. <hr />
  5526. <p>Config option: <code>ui_auth</code></p>
  5527. <p>The amount of time to allow a user-interactive authentication session to be active.</p>
  5528. <p>This defaults to 0, meaning the user is queried for their credentials
  5529. before every action, but this can be overridden to allow a single
  5530. validation to be re-used. This weakens the protections afforded by
  5531. the user-interactive authentication process, by allowing for multiple
  5532. (and potentially different) operations to use the same validation session.</p>
  5533. <p>This is ignored for potentially &quot;dangerous&quot; operations (including
  5534. deactivating an account, modifying an account password, and
  5535. adding a 3PID).</p>
  5536. <p>Use the <code>session_timeout</code> sub-option here to change the time allowed for credential validation.</p>
  5537. <p>Example configuration:</p>
  5538. <pre><code class="language-yaml">ui_auth:
  5539. session_timeout: &quot;15s&quot;
  5540. </code></pre>
  5541. <hr />
  5542. <p>Config option: <code>email</code></p>
  5543. <p>Configuration for sending emails from Synapse.</p>
  5544. <p>Server admins can configure custom templates for email content. See
  5545. <a href="usage/configuration/../../templates.html">here</a> for more information.</p>
  5546. <p>This setting has the following sub-options:</p>
  5547. <ul>
  5548. <li>
  5549. <p><code>smtp_host</code>: The hostname of the outgoing SMTP server to use. Defaults to 'localhost'.</p>
  5550. </li>
  5551. <li>
  5552. <p><code>smtp_port</code>: The port on the mail server for outgoing SMTP. Defaults to 25.</p>
  5553. </li>
  5554. <li>
  5555. <p><code>smtp_user</code> and <code>smtp_pass</code>: Username/password for authentication to the SMTP server. By default, no
  5556. authentication is attempted.</p>
  5557. </li>
  5558. <li>
  5559. <p><code>require_transport_security</code>: Set to true to require TLS transport security for SMTP.
  5560. By default, Synapse will connect over plain text, and will then switch to
  5561. TLS via STARTTLS <em>if the SMTP server supports it</em>. If this option is set,
  5562. Synapse will refuse to connect unless the server supports STARTTLS.</p>
  5563. </li>
  5564. <li>
  5565. <p><code>enable_tls</code>: By default, if the server supports TLS, it will be used, and the server
  5566. must present a certificate that is valid for 'smtp_host'. If this option
  5567. is set to false, TLS will not be used.</p>
  5568. </li>
  5569. <li>
  5570. <p><code>notif_from</code>: defines the &quot;From&quot; address to use when sending emails.
  5571. It must be set if email sending is enabled. The placeholder '%(app)s' will be replaced by the application name,
  5572. which is normally set in <code>app_name</code>, but may be overridden by the
  5573. Matrix client application. Note that the placeholder must be written '%(app)s', including the
  5574. trailing 's'.</p>
  5575. </li>
  5576. <li>
  5577. <p><code>app_name</code>: <code>app_name</code> defines the default value for '%(app)s' in <code>notif_from</code> and email
  5578. subjects. It defaults to 'Matrix'.</p>
  5579. </li>
  5580. <li>
  5581. <p><code>enable_notifs</code>: Set to true to enable sending emails for messages that the user
  5582. has missed. Disabled by default.</p>
  5583. </li>
  5584. <li>
  5585. <p><code>notif_for_new_users</code>: Set to false to disable automatic subscription to email
  5586. notifications for new users. Enabled by default.</p>
  5587. </li>
  5588. <li>
  5589. <p><code>client_base_url</code>: Custom URL for client links within the email notifications. By default
  5590. links will be based on &quot;https://matrix.to&quot;. (This setting used to be called <code>riot_base_url</code>;
  5591. the old name is still supported for backwards-compatibility but is now deprecated.)</p>
  5592. </li>
  5593. <li>
  5594. <p><code>validation_token_lifetime</code>: Configures the time that a validation email will expire after sending.
  5595. Defaults to 1h.</p>
  5596. </li>
  5597. <li>
  5598. <p><code>invite_client_location</code>: The web client location to direct users to during an invite. This is passed
  5599. to the identity server as the <code>org.matrix.web_client_location</code> key. Defaults
  5600. to unset, giving no guidance to the identity server.</p>
  5601. </li>
  5602. <li>
  5603. <p><code>subjects</code>: Subjects to use when sending emails from Synapse. The placeholder '%(app)s' will
  5604. be replaced with the value of the <code>app_name</code> setting, or by a value dictated by the Matrix client application.
  5605. In addition, each subject can use the following placeholders: '%(person)s', which will be replaced by the displayname
  5606. of the user(s) that sent the message(s), e.g. &quot;Alice and Bob&quot;, and '%(room)s', which will be replaced by the name of the room the
  5607. message(s) have been sent to, e.g. &quot;My super room&quot;. In addition, emails related to account administration will
  5608. can use the '%(server_name)s' placeholder, which will be replaced by the value of the
  5609. <code>server_name</code> setting in your Synapse configuration.</p>
  5610. <p>Here is a list of subjects for notification emails that can be set: </p>
  5611. <ul>
  5612. <li><code>message_from_person_in_room</code>: Subject to use to notify about one message from one or more user(s) in a
  5613. room which has a name. Defaults to &quot;[%(app)s] You have a message on %(app)s from %(person)s in the %(room)s room...&quot;</li>
  5614. <li><code>message_from_person</code>: Subject to use to notify about one message from one or more user(s) in a
  5615. room which doesn't have a name. Defaults to &quot;[%(app)s] You have a message on %(app)s from %(person)s...&quot;</li>
  5616. <li><code>messages_from_person</code>: Subject to use to notify about multiple messages from one or more users in
  5617. a room which doesn't have a name. Defaults to &quot;[%(app)s] You have messages on %(app)s from %(person)s...&quot;</li>
  5618. <li><code>messages_in_room</code>: Subject to use to notify about multiple messages in a room which has a
  5619. name. Defaults to &quot;[%(app)s] You have messages on %(app)s in the %(room)s room...&quot;</li>
  5620. <li><code>messages_in_room_and_others</code>: Subject to use to notify about multiple messages in multiple rooms.
  5621. Defaults to &quot;[%(app)s] You have messages on %(app)s in the %(room)s room and others...&quot;</li>
  5622. <li><code>messages_from_person_and_others</code>: Subject to use to notify about multiple messages from multiple persons in
  5623. multiple rooms. This is similar to the setting above except it's used when
  5624. the room in which the notification was triggered has no name. Defaults to
  5625. &quot;[%(app)s] You have messages on %(app)s from %(person)s and others...&quot;</li>
  5626. <li><code>invite_from_person_to_room</code>: Subject to use to notify about an invite to a room which has a name.
  5627. Defaults to &quot;[%(app)s] %(person)s has invited you to join the %(room)s room on %(app)s...&quot;</li>
  5628. <li><code>invite_from_person</code>: Subject to use to notify about an invite to a room which doesn't have a
  5629. name. Defaults to &quot;[%(app)s] %(person)s has invited you to chat on %(app)s...&quot;</li>
  5630. <li><code>password_reset</code>: Subject to use when sending a password reset email. Defaults to &quot;[%(server_name)s] Password reset&quot;</li>
  5631. <li><code>email_validation</code>: Subject to use when sending a verification email to assert an address's
  5632. ownership. Defaults to &quot;[%(server_name)s] Validate your email&quot;</li>
  5633. </ul>
  5634. </li>
  5635. </ul>
  5636. <p>Example configuration:</p>
  5637. <pre><code class="language-yaml">email:
  5638. smtp_host: mail.server
  5639. smtp_port: 587
  5640. smtp_user: &quot;exampleusername&quot;
  5641. smtp_pass: &quot;examplepassword&quot;
  5642. require_transport_security: true
  5643. enable_tls: false
  5644. notif_from: &quot;Your Friendly %(app)s homeserver &lt;noreply@example.com&gt;&quot;
  5645. app_name: my_branded_matrix_server
  5646. enable_notifs: true
  5647. notif_for_new_users: false
  5648. client_base_url: &quot;http://localhost/riot&quot;
  5649. validation_token_lifetime: 15m
  5650. invite_client_location: https://app.element.io
  5651. subjects:
  5652. message_from_person_in_room: &quot;[%(app)s] You have a message on %(app)s from %(person)s in the %(room)s room...&quot;
  5653. message_from_person: &quot;[%(app)s] You have a message on %(app)s from %(person)s...&quot;
  5654. messages_from_person: &quot;[%(app)s] You have messages on %(app)s from %(person)s...&quot;
  5655. messages_in_room: &quot;[%(app)s] You have messages on %(app)s in the %(room)s room...&quot;
  5656. messages_in_room_and_others: &quot;[%(app)s] You have messages on %(app)s in the %(room)s room and others...&quot;
  5657. messages_from_person_and_others: &quot;[%(app)s] You have messages on %(app)s from %(person)s and others...&quot;
  5658. invite_from_person_to_room: &quot;[%(app)s] %(person)s has invited you to join the %(room)s room on %(app)s...&quot;
  5659. invite_from_person: &quot;[%(app)s] %(person)s has invited you to chat on %(app)s...&quot;
  5660. password_reset: &quot;[%(server_name)s] Password reset&quot;
  5661. email_validation: &quot;[%(server_name)s] Validate your email&quot;
  5662. </code></pre>
  5663. <hr />
  5664. <h2 id="push"><a class="header" href="#push">Push</a></h2>
  5665. <p>Configuration settings related to push notifications</p>
  5666. <hr />
  5667. <p>Config option: <code>push</code></p>
  5668. <p>This setting defines options for push notifications. </p>
  5669. <p>This option has a number of sub-options. They are as follows:</p>
  5670. <ul>
  5671. <li><code>include_content</code>: Clients requesting push notifications can either have the body of
  5672. the message sent in the notification poke along with other details
  5673. like the sender, or just the event ID and room ID (<code>event_id_only</code>).
  5674. If clients choose the to have the body sent, this option controls whether the
  5675. notification request includes the content of the event (other details
  5676. like the sender are still included). If <code>event_id_only</code> is enabled, it
  5677. has no effect.
  5678. For modern android devices the notification content will still appear
  5679. because it is loaded by the app. iPhone, however will send a
  5680. notification saying only that a message arrived and who it came from.
  5681. Defaults to true. Set to false to only include the event ID and room ID in push notification payloads.</li>
  5682. <li><code>group_unread_count_by_room: false</code>: When a push notification is received, an unread count is also sent.
  5683. This number can either be calculated as the number of unread messages for the user, or the number of <em>rooms</em> the
  5684. user has unread messages in. Defaults to true, meaning push clients will see the number of
  5685. rooms with unread messages in them. Set to false to instead send the number
  5686. of unread messages.</li>
  5687. </ul>
  5688. <p>Example configuration:</p>
  5689. <pre><code class="language-yaml">push:
  5690. include_content: false
  5691. group_unread_count_by_room: false
  5692. </code></pre>
  5693. <hr />
  5694. <h2 id="rooms"><a class="header" href="#rooms">Rooms</a></h2>
  5695. <p>Config options relating to rooms.</p>
  5696. <hr />
  5697. <p>Config option: <code>encryption_enabled_by_default</code></p>
  5698. <p>Controls whether locally-created rooms should be end-to-end encrypted by
  5699. default.</p>
  5700. <p>Possible options are &quot;all&quot;, &quot;invite&quot;, and &quot;off&quot;. They are defined as:</p>
  5701. <ul>
  5702. <li>&quot;all&quot;: any locally-created room</li>
  5703. <li>&quot;invite&quot;: any room created with the <code>private_chat</code> or <code>trusted_private_chat</code>
  5704. room creation presets</li>
  5705. <li>&quot;off&quot;: this option will take no effect</li>
  5706. </ul>
  5707. <p>The default value is &quot;off&quot;.</p>
  5708. <p>Note that this option will only affect rooms created after it is set. It
  5709. will also not affect rooms created by other servers.</p>
  5710. <p>Example configuration:</p>
  5711. <pre><code class="language-yaml">encryption_enabled_by_default_for_room_type: invite
  5712. </code></pre>
  5713. <hr />
  5714. <p>Config option: <code>enable_group_creation</code></p>
  5715. <p>Set to true to allow non-server-admin users to create groups on this server</p>
  5716. <p>Example configuration:</p>
  5717. <pre><code class="language-yaml">enable_group_creation: true
  5718. </code></pre>
  5719. <hr />
  5720. <p>Config option: <code>group_creation_prefix</code></p>
  5721. <p>If enabled/present, non-server admins can only create groups with local parts
  5722. starting with this prefix.</p>
  5723. <p>Example configuration:</p>
  5724. <pre><code class="language-yaml">group_creation_prefix: &quot;unofficial_&quot;
  5725. </code></pre>
  5726. <hr />
  5727. <p>Config option: <code>user_directory</code></p>
  5728. <p>This setting defines options related to the user directory. </p>
  5729. <p>This option has the following sub-options:</p>
  5730. <ul>
  5731. <li><code>enabled</code>: Defines whether users can search the user directory. If false then
  5732. empty responses are returned to all queries. Defaults to true.</li>
  5733. <li><code>search_all_users</code>: Defines whether to search all users visible to your HS when searching
  5734. the user directory. If false, search results will only contain users
  5735. visible in public rooms and users sharing a room with the requester.
  5736. Defaults to false.
  5737. NB. If you set this to true, and the last time the user_directory search
  5738. indexes were (re)built was before Synapse 1.44, you'll have to
  5739. rebuild the indexes in order to search through all known users.
  5740. These indexes are built the first time Synapse starts; admins can
  5741. manually trigger a rebuild via API following the instructions at
  5742. https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/background_updates.html#run
  5743. Set to true to return search results containing all known users, even if that
  5744. user does not share a room with the requester.</li>
  5745. <li><code>prefer_local_users</code>: Defines whether to prefer local users in search query results.
  5746. If set to true, local users are more likely to appear above remote users when searching the
  5747. user directory. Defaults to false.</li>
  5748. </ul>
  5749. <p>Example configuration:</p>
  5750. <pre><code class="language-yaml">user_directory:
  5751. enabled: false
  5752. search_all_users: true
  5753. prefer_local_users: true
  5754. </code></pre>
  5755. <hr />
  5756. <p>Config option: <code>user_consent</code></p>
  5757. <p>For detailed instructions on user consent configuration, see <a href="usage/configuration/../../consent_tracking.html">here</a>.</p>
  5758. <p>Parts of this section are required if enabling the <code>consent</code> resource under
  5759. <code>listeners</code>, in particular <code>template_dir</code> and <code>version</code>. # TODO: link <code>listeners</code></p>
  5760. <ul>
  5761. <li>
  5762. <p><code>template_dir</code>: gives the location of the templates for the HTML forms.
  5763. This directory should contain one subdirectory per language (eg, <code>en</code>, <code>fr</code>),
  5764. and each language directory should contain the policy document (named as
  5765. <version>.html) and a success page (success.html).</p>
  5766. </li>
  5767. <li>
  5768. <p><code>version</code>: specifies the 'current' version of the policy document. It defines
  5769. the version to be served by the consent resource if there is no 'v'
  5770. parameter.</p>
  5771. </li>
  5772. <li>
  5773. <p><code>server_notice_content</code>: if enabled, will send a user a &quot;Server Notice&quot;
  5774. asking them to consent to the privacy policy. The <code>server_notices</code> section ##TODO: link
  5775. must also be configured for this to work. Notices will <em>not</em> be sent to
  5776. guest users unless <code>send_server_notice_to_guests</code> is set to true.</p>
  5777. </li>
  5778. <li>
  5779. <p><code>block_events_error</code>, if set, will block any attempts to send events
  5780. until the user consents to the privacy policy. The value of the setting is
  5781. used as the text of the error.</p>
  5782. </li>
  5783. <li>
  5784. <p><code>require_at_registration</code>, if enabled, will add a step to the registration
  5785. process, similar to how captcha works. Users will be required to accept the
  5786. policy before their account is created.</p>
  5787. </li>
  5788. <li>
  5789. <p><code>policy_name</code> is the display name of the policy users will see when registering
  5790. for an account. Has no effect unless <code>require_at_registration</code> is enabled.
  5791. Defaults to &quot;Privacy Policy&quot;.</p>
  5792. </li>
  5793. </ul>
  5794. <p>Example configuration:</p>
  5795. <pre><code class="language-yaml">user_consent:
  5796. template_dir: res/templates/privacy
  5797. version: 1.0
  5798. server_notice_content:
  5799. msgtype: m.text
  5800. body: &gt;-
  5801. To continue using this homeserver you must review and agree to the
  5802. terms and conditions at %(consent_uri)s
  5803. send_server_notice_to_guests: true
  5804. block_events_error: &gt;-
  5805. To continue using this homeserver you must review and agree to the
  5806. terms and conditions at %(consent_uri)s
  5807. require_at_registration: false
  5808. policy_name: Privacy Policy
  5809. </code></pre>
  5810. <hr />
  5811. <p>Config option: <code>stats</code></p>
  5812. <p>Settings for local room and user statistics collection. See <a href="usage/configuration/../../room_and_user_statistics.html">here</a>
  5813. for more. </p>
  5814. <ul>
  5815. <li><code>enabled</code>: Set to false to disable room and user statistics. Note that doing
  5816. so may cause certain features (such as the room directory) not to work
  5817. correctly. Defaults to true. </li>
  5818. </ul>
  5819. <p>Example configuration:</p>
  5820. <pre><code class="language-yaml">stats:
  5821. enabled: false
  5822. </code></pre>
  5823. <hr />
  5824. <p>Config option: <code>server_notices</code></p>
  5825. <p>Use this setting to enable a room which can be used to send notices
  5826. from the server to users. It is a special room which users cannot leave; notices
  5827. in the room come from a special &quot;notices&quot; user id.</p>
  5828. <p>If you use this setting, you <em>must</em> define the <code>system_mxid_localpart</code>
  5829. sub-setting, which defines the id of the user which will be used to send the
  5830. notices.</p>
  5831. <p>Sub-options for this setting include:</p>
  5832. <ul>
  5833. <li><code>system_mxid_display_name</code>: set the display name of the &quot;notices&quot; user</li>
  5834. <li><code>system_mxid_avatar_url</code>: set the avatar for the &quot;notices&quot; user</li>
  5835. <li><code>room_name</code>: set the room name of the server notices room</li>
  5836. </ul>
  5837. <p>Example configuration:</p>
  5838. <pre><code class="language-yaml">server_notices:
  5839. system_mxid_localpart: notices
  5840. system_mxid_display_name: &quot;Server Notices&quot;
  5841. system_mxid_avatar_url: &quot;mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ&quot;
  5842. room_name: &quot;Server Notices&quot;
  5843. </code></pre>
  5844. <hr />
  5845. <p>Config option: <code>enable_room_list_search</code></p>
  5846. <p>Set to false to disable searching the public room list. When disabled
  5847. blocks searching local and remote room lists for local and remote
  5848. users by always returning an empty list for all queries. Defaults to true. </p>
  5849. <p>Example configuration:</p>
  5850. <pre><code class="language-yaml">enable_room_list_search: false
  5851. </code></pre>
  5852. <hr />
  5853. <p>Config option: <code>alias_creation</code></p>
  5854. <p>The <code>alias_creation</code> option controls who is allowed to create aliases
  5855. on this server.</p>
  5856. <p>The format of this option is a list of rules that contain globs that
  5857. match against user_id, room_id and the new alias (fully qualified with
  5858. server name). The action in the first rule that matches is taken,
  5859. which can currently either be &quot;allow&quot; or &quot;deny&quot;.</p>
  5860. <p>Missing user_id/room_id/alias fields default to &quot;*&quot;.</p>
  5861. <p>If no rules match the request is denied. An empty list means no one
  5862. can create aliases.</p>
  5863. <p>Options for the rules include:</p>
  5864. <ul>
  5865. <li><code>user_id</code>: Matches against the creator of the alias. Defaults to &quot;*&quot;.</li>
  5866. <li><code>alias</code>: Matches against the alias being created. Defaults to &quot;*&quot;.</li>
  5867. <li><code>room_id</code>: Matches against the room ID the alias is being pointed at. Defaults to &quot;*&quot;</li>
  5868. <li><code>action</code>: Whether to &quot;allow&quot; or &quot;deny&quot; the request if the rule matches. Defaults to allow. </li>
  5869. </ul>
  5870. <p>Example configuration:</p>
  5871. <pre><code class="language-yaml">alias_creation_rules:
  5872. - user_id: &quot;bad_user&quot;
  5873. alias: &quot;spammy_alias&quot;
  5874. room_id: &quot;*&quot;
  5875. action: deny
  5876. </code></pre>
  5877. <hr />
  5878. <p>Config options: <code>room_list_publication_rules</code></p>
  5879. <p>The <code>room_list_publication_rules</code> option controls who can publish and
  5880. which rooms can be published in the public room list.</p>
  5881. <p>The format of this option is the same as that for
  5882. <code>alias_creation_rules</code>.</p>
  5883. <p>If the room has one or more aliases associated with it, only one of
  5884. the aliases needs to match the alias rule. If there are no aliases
  5885. then only rules with <code>alias: *</code> match.</p>
  5886. <p>If no rules match the request is denied. An empty list means no one
  5887. can publish rooms.</p>
  5888. <p>Options for the rules include:</p>
  5889. <ul>
  5890. <li><code>user_id</code>: Matches against the creator of the alias. Defaults to &quot;*&quot;.</li>
  5891. <li><code>alias</code>: Matches against any current local or canonical aliases associated with the room. Defaults to &quot;*&quot;.</li>
  5892. <li><code>room_id</code>: Matches against the room ID being published. Defaults to &quot;*&quot;.</li>
  5893. <li><code>action</code>: Whether to &quot;allow&quot; or &quot;deny&quot; the request if the rule matches. Defaults to allow. </li>
  5894. </ul>
  5895. <p>Example configuration:</p>
  5896. <pre><code class="language-yaml">room_list_publication_rules:
  5897. - user_id: &quot;*&quot;
  5898. alias: &quot;*&quot;
  5899. room_id: &quot;*&quot;
  5900. action: allow
  5901. </code></pre>
  5902. <hr />
  5903. <p>Config option: <code>default_power_level_content_override</code></p>
  5904. <p>The <code>default_power_level_content_override</code> option controls the default power
  5905. levels for rooms.</p>
  5906. <p>Useful if you know that your users need special permissions in rooms
  5907. that they create (e.g. to send particular types of state events without
  5908. needing an elevated power level). This takes the same shape as the
  5909. <code>power_level_content_override</code> parameter in the /createRoom API, but
  5910. is applied before that parameter.</p>
  5911. <p>Note that each key provided inside a preset (for example <code>events</code> in the example
  5912. below) will overwrite all existing defaults inside that key. So in the example
  5913. below, newly-created private_chat rooms will have no rules for any event types
  5914. except <code>com.example.foo</code>.</p>
  5915. <p>Example configuration:</p>
  5916. <pre><code class="language-yaml">default_power_level_content_override:
  5917. private_chat: { &quot;events&quot;: { &quot;com.example.foo&quot; : 0 } }
  5918. trusted_private_chat: null
  5919. public_chat: null
  5920. </code></pre>
  5921. <hr />
  5922. <h2 id="opentracing"><a class="header" href="#opentracing">Opentracing</a></h2>
  5923. <p>Configuration options related to Opentracing support.</p>
  5924. <hr />
  5925. <p>Config option: <code>opentracing</code></p>
  5926. <p>These settings enable and configure opentracing, which implements distributed tracing.
  5927. This allows you to observe the causal chains of events across servers
  5928. including requests, key lookups etc., across any server running
  5929. synapse or any other services which support opentracing
  5930. (specifically those implemented with Jaeger).</p>
  5931. <p>Sub-options include:</p>
  5932. <ul>
  5933. <li><code>enabled</code>: whether tracing is enabled. Set to true to enable. Disabled by default.</li>
  5934. <li><code>homeserver_whitelist</code>: The list of homeservers we wish to send and receive span contexts and span baggage.
  5935. See <a href="usage/configuration/../../opentracing.html">here</a> for more.
  5936. This is a list of regexes which are matched against the <code>server_name</code> of the homeserver.
  5937. By default, it is empty, so no servers are matched.</li>
  5938. <li><code>force_tracing_for_users</code>: # A list of the matrix IDs of users whose requests will always be traced,
  5939. even if the tracing system would otherwise drop the traces due to probabilistic sampling.
  5940. By default, the list is empty.</li>
  5941. <li><code>jaeger_config</code>: Jaeger can be configured to sample traces at different rates.
  5942. All configuration options provided by Jaeger can be set here. Jaeger's configuration is
  5943. mostly related to trace sampling which is documented <a href="https://www.jaegertracing.io/docs/latest/sampling/">here</a>.</li>
  5944. </ul>
  5945. <p>Example configuration:</p>
  5946. <pre><code class="language-yaml">opentracing:
  5947. enabled: true
  5948. homeserver_whitelist:
  5949. - &quot;.*&quot;
  5950. force_tracing_for_users:
  5951. - &quot;@user1:server_name&quot;
  5952. - &quot;@user2:server_name&quot;
  5953. jaeger_config:
  5954. sampler:
  5955. type: const
  5956. param: 1
  5957. logging:
  5958. false
  5959. </code></pre>
  5960. <hr />
  5961. <h2 id="workers"><a class="header" href="#workers">Workers</a></h2>
  5962. <p>Configuration options related to workers.</p>
  5963. <hr />
  5964. <p>Config option: <code>send_federation</code></p>
  5965. <p>Controls sending of outbound federation transactions on the main process.
  5966. Set to false if using a federation sender worker. Defaults to true. </p>
  5967. <p>Example configuration:</p>
  5968. <pre><code class="language-yaml">send_federation: false
  5969. </code></pre>
  5970. <hr />
  5971. <p>Config option: <code>federation_sender_instances</code></p>
  5972. <p>It is possible to run multiple federation sender workers, in which case the
  5973. work is balanced across them. Use this setting to list the senders. </p>
  5974. <p>This configuration setting must be shared between all federation sender workers, and if
  5975. changed all federation sender workers must be stopped at the same time and then
  5976. started, to ensure that all instances are running with the same config (otherwise
  5977. events may be dropped). </p>
  5978. <p>Example configuration:</p>
  5979. <pre><code class="language-yaml">federation_sender_instances:
  5980. - federation_sender1
  5981. </code></pre>
  5982. <hr />
  5983. <p>Config option: <code>instance_map</code></p>
  5984. <p>When using workers this should be a map from worker name to the
  5985. HTTP replication listener of the worker, if configured. </p>
  5986. <p>Example configuration:</p>
  5987. <pre><code class="language-yaml">instance_map:
  5988. worker1:
  5989. host: localhost
  5990. port: 8034
  5991. </code></pre>
  5992. <hr />
  5993. <p>Config option: <code>stream_writers</code></p>
  5994. <p>Experimental: When using workers you can define which workers should
  5995. handle event persistence and typing notifications. Any worker
  5996. specified here must also be in the <code>instance_map</code>.</p>
  5997. <p>Example configuration:</p>
  5998. <pre><code class="language-yaml">stream_writers:
  5999. events: worker1
  6000. typing: worker1
  6001. </code></pre>
  6002. <hr />
  6003. <p>Config option: <code>run_background_tasks_on</code></p>
  6004. <p>The worker that is used to run background tasks (e.g. cleaning up expired
  6005. data). If not provided this defaults to the main process.</p>
  6006. <p>Example configuration:</p>
  6007. <pre><code class="language-yaml">run_background_tasks_on: worker1
  6008. </code></pre>
  6009. <hr />
  6010. <p>Config option: <code>worker_replication_secret</code></p>
  6011. <p>A shared secret used by the replication APIs to authenticate HTTP requests
  6012. from workers.</p>
  6013. <p>By default this is unused and traffic is not authenticated.</p>
  6014. <p>Example configuration:</p>
  6015. <pre><code class="language-yaml">worker_replication_secret: &quot;secret_secret&quot;
  6016. </code></pre>
  6017. <p>Config option: <code>redis</code></p>
  6018. <p>Configuration for Redis when using workers. This <em>must</em> be enabled when
  6019. using workers (unless using old style direct TCP configuration).
  6020. This setting has the following sub-options:</p>
  6021. <ul>
  6022. <li><code>enabled</code>: whether to use Redis support. Defaults to false. </li>
  6023. <li><code>host</code> and <code>port</code>: Optional host and port to use to connect to redis. Defaults to
  6024. localhost and 6379</li>
  6025. <li><code>password</code>: Optional password if configured on the Redis instance.</li>
  6026. </ul>
  6027. <p>Example configuration:</p>
  6028. <pre><code class="language-yaml">redis:
  6029. enabled: true
  6030. host: localhost
  6031. port: 6379
  6032. password: &lt;secret_password&gt;
  6033. </code></pre>
  6034. <h2 id="background-updates"><a class="header" href="#background-updates">Background Updates</a></h2>
  6035. <p>Configuration settings related to background updates. </p>
  6036. <hr />
  6037. <p>Config option: <code>background_updates</code></p>
  6038. <p>Background updates are database updates that are run in the background in batches.
  6039. The duration, minimum batch size, default batch size, whether to sleep between batches and if so, how long to
  6040. sleep can all be configured. This is helpful to speed up or slow down the updates.
  6041. This setting has the following sub-options:</p>
  6042. <ul>
  6043. <li><code>background_update_duration_ms</code>: How long in milliseconds to run a batch of background updates for. Defaults to 100.
  6044. Set a different time to change the default.</li>
  6045. <li><code>sleep_enabled</code>: Whether to sleep between updates. Defaults to true. Set to false to change the default.</li>
  6046. <li><code>sleep_duration_ms</code>: If sleeping between updates, how long in milliseconds to sleep for. Defaults to 1000.
  6047. Set a duration to change the default.</li>
  6048. <li><code>min_batch_size</code>: Minimum size a batch of background updates can be. Must be greater than 0. Defaults to 1.
  6049. Set a size to change the default.</li>
  6050. <li><code>default_batch_size</code>: The batch size to use for the first iteration of a new background update. The default is 100.
  6051. Set a size to change the default.</li>
  6052. </ul>
  6053. <p>Example configuration: </p>
  6054. <pre><code class="language-yaml">background_updates:
  6055. background_update_duration_ms: 500
  6056. sleep_enabled: false
  6057. sleep_duration_ms: 300
  6058. min_batch_size: 10
  6059. default_batch_size: 50
  6060. </code></pre>
  6061. <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>
  6062. <p>Below is a sample homeserver configuration file. The homeserver configuration file
  6063. can be tweaked to change the behaviour of your homeserver. A restart of the server is
  6064. generally required to apply any changes made to this file.</p>
  6065. <p>Note that the contents below are <em>not</em> intended to be copied and used as the basis for
  6066. a real homeserver.yaml. Instead, if you are starting from scratch, please generate
  6067. a fresh config using Synapse by following the instructions in
  6068. <a href="usage/configuration/../../setup/installation.html">Installation</a>.</p>
  6069. <pre><code class="language-yaml"># This file is maintained as an up-to-date snapshot of the default
  6070. # homeserver.yaml configuration generated by Synapse.
  6071. #
  6072. # It is intended to act as a reference for the default configuration,
  6073. # helping admins keep track of new options and other changes, and compare
  6074. # their configs with the current default. As such, many of the actual
  6075. # config values shown are placeholders.
  6076. #
  6077. # It is *not* intended to be copied and used as the basis for a real
  6078. # homeserver.yaml. Instead, if you are starting from scratch, please generate
  6079. # a fresh config using Synapse by following the instructions in
  6080. # https://matrix-org.github.io/synapse/latest/setup/installation.html.
  6081. # Configuration options that take a time period can be set using a number
  6082. # followed by a letter. Letters have the following meanings:
  6083. # s = second
  6084. # m = minute
  6085. # h = hour
  6086. # d = day
  6087. # w = week
  6088. # y = year
  6089. # For example, setting redaction_retention_period: 5m would remove redacted
  6090. # messages from the database after 5 minutes, rather than 5 months.
  6091. ################################################################################
  6092. # Configuration file for Synapse.
  6093. #
  6094. # This is a YAML file: see [1] for a quick introduction. Note in particular
  6095. # that *indentation is important*: all the elements of a list or dictionary
  6096. # should have the same indentation.
  6097. #
  6098. # [1] https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
  6099. ## Modules ##
  6100. # Server admins can expand Synapse's functionality with external modules.
  6101. #
  6102. # See https://matrix-org.github.io/synapse/latest/modules/index.html for more
  6103. # documentation on how to configure or create custom modules for Synapse.
  6104. #
  6105. modules:
  6106. #- module: my_super_module.MySuperClass
  6107. # config:
  6108. # do_thing: true
  6109. #- module: my_other_super_module.SomeClass
  6110. # config: {}
  6111. ## Server ##
  6112. # The public-facing domain of the server
  6113. #
  6114. # The server_name name will appear at the end of usernames and room addresses
  6115. # created on this server. For example if the server_name was example.com,
  6116. # usernames on this server would be in the format @user:example.com
  6117. #
  6118. # In most cases you should avoid using a matrix specific subdomain such as
  6119. # matrix.example.com or synapse.example.com as the server_name for the same
  6120. # reasons you wouldn't use user@email.example.com as your email address.
  6121. # See https://matrix-org.github.io/synapse/latest/delegate.html
  6122. # for information on how to host Synapse on a subdomain while preserving
  6123. # a clean server_name.
  6124. #
  6125. # The server_name cannot be changed later so it is important to
  6126. # configure this correctly before you start Synapse. It should be all
  6127. # lowercase and may contain an explicit port.
  6128. # Examples: matrix.org, localhost:8080
  6129. #
  6130. server_name: &quot;SERVERNAME&quot;
  6131. # When running as a daemon, the file to store the pid in
  6132. #
  6133. pid_file: DATADIR/homeserver.pid
  6134. # The absolute URL to the web client which / will redirect to.
  6135. #
  6136. #web_client_location: https://riot.example.com/
  6137. # The public-facing base URL that clients use to access this Homeserver (not
  6138. # including _matrix/...). This is the same URL a user might enter into the
  6139. # 'Custom Homeserver URL' field on their client. If you use Synapse with a
  6140. # reverse proxy, this should be the URL to reach Synapse via the proxy.
  6141. # Otherwise, it should be the URL to reach Synapse's client HTTP listener (see
  6142. # 'listeners' below).
  6143. #
  6144. # Defaults to 'https://&lt;server_name&gt;/'.
  6145. #
  6146. #public_baseurl: https://example.com/
  6147. # Uncomment the following to tell other servers to send federation traffic on
  6148. # port 443.
  6149. #
  6150. # By default, other servers will try to reach our server on port 8448, which can
  6151. # be inconvenient in some environments.
  6152. #
  6153. # Provided 'https://&lt;server_name&gt;/' on port 443 is routed to Synapse, this
  6154. # option configures Synapse to serve a file at
  6155. # 'https://&lt;server_name&gt;/.well-known/matrix/server'. This will tell other
  6156. # servers to send traffic to port 443 instead.
  6157. #
  6158. # See https://matrix-org.github.io/synapse/latest/delegate.html for more
  6159. # information.
  6160. #
  6161. # Defaults to 'false'.
  6162. #
  6163. #serve_server_wellknown: true
  6164. # Set the soft limit on the number of file descriptors synapse can use
  6165. # Zero is used to indicate synapse should set the soft limit to the
  6166. # hard limit.
  6167. #
  6168. #soft_file_limit: 0
  6169. # Presence tracking allows users to see the state (e.g online/offline)
  6170. # of other local and remote users.
  6171. #
  6172. presence:
  6173. # Uncomment to disable presence tracking on this homeserver. This option
  6174. # replaces the previous top-level 'use_presence' option.
  6175. #
  6176. #enabled: false
  6177. # Whether to require authentication to retrieve profile data (avatars,
  6178. # display names) of other users through the client API. Defaults to
  6179. # 'false'. Note that profile data is also available via the federation
  6180. # API, unless allow_profile_lookup_over_federation is set to false.
  6181. #
  6182. #require_auth_for_profile_requests: true
  6183. # Uncomment to require a user to share a room with another user in order
  6184. # to retrieve their profile information. Only checked on Client-Server
  6185. # requests. Profile requests from other servers should be checked by the
  6186. # requesting server. Defaults to 'false'.
  6187. #
  6188. #limit_profile_requests_to_users_who_share_rooms: true
  6189. # Uncomment to prevent a user's profile data from being retrieved and
  6190. # displayed in a room until they have joined it. By default, a user's
  6191. # profile data is included in an invite event, regardless of the values
  6192. # of the above two settings, and whether or not the users share a server.
  6193. # Defaults to 'true'.
  6194. #
  6195. #include_profile_data_on_invite: false
  6196. # If set to 'true', removes the need for authentication to access the server's
  6197. # public rooms directory through the client API, meaning that anyone can
  6198. # query the room directory. Defaults to 'false'.
  6199. #
  6200. #allow_public_rooms_without_auth: true
  6201. # If set to 'true', allows any other homeserver to fetch the server's public
  6202. # rooms directory via federation. Defaults to 'false'.
  6203. #
  6204. #allow_public_rooms_over_federation: true
  6205. # The default room version for newly created rooms.
  6206. #
  6207. # Known room versions are listed here:
  6208. # https://spec.matrix.org/latest/rooms/#complete-list-of-room-versions
  6209. #
  6210. # For example, for room version 1, default_room_version should be set
  6211. # to &quot;1&quot;.
  6212. #
  6213. #default_room_version: &quot;9&quot;
  6214. # The GC threshold parameters to pass to `gc.set_threshold`, if defined
  6215. #
  6216. #gc_thresholds: [700, 10, 10]
  6217. # The minimum time in seconds between each GC for a generation, regardless of
  6218. # the GC thresholds. This ensures that we don't do GC too frequently.
  6219. #
  6220. # A value of `[1s, 10s, 30s]` indicates that a second must pass between consecutive
  6221. # generation 0 GCs, etc.
  6222. #
  6223. # Defaults to `[1s, 10s, 30s]`.
  6224. #
  6225. #gc_min_interval: [0.5s, 30s, 1m]
  6226. # Set the limit on the returned events in the timeline in the get
  6227. # and sync operations. The default value is 100. -1 means no upper limit.
  6228. #
  6229. # Uncomment the following to increase the limit to 5000.
  6230. #
  6231. #filter_timeline_limit: 5000
  6232. # Whether room invites to users on this server should be blocked
  6233. # (except those sent by local server admins). The default is False.
  6234. #
  6235. #block_non_admin_invites: true
  6236. # Room searching
  6237. #
  6238. # If disabled, new messages will not be indexed for searching and users
  6239. # will receive errors when searching for messages. Defaults to enabled.
  6240. #
  6241. #enable_search: false
  6242. # Prevent outgoing requests from being sent to the following blacklisted IP address
  6243. # CIDR ranges. If this option is not specified then it defaults to private IP
  6244. # address ranges (see the example below).
  6245. #
  6246. # The blacklist applies to the outbound requests for federation, identity servers,
  6247. # push servers, and for checking key validity for third-party invite events.
  6248. #
  6249. # (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
  6250. # listed here, since they correspond to unroutable addresses.)
  6251. #
  6252. # This option replaces federation_ip_range_blacklist in Synapse v1.25.0.
  6253. #
  6254. # Note: The value is ignored when an HTTP proxy is in use
  6255. #
  6256. #ip_range_blacklist:
  6257. # - '127.0.0.0/8'
  6258. # - '10.0.0.0/8'
  6259. # - '172.16.0.0/12'
  6260. # - '192.168.0.0/16'
  6261. # - '100.64.0.0/10'
  6262. # - '192.0.0.0/24'
  6263. # - '169.254.0.0/16'
  6264. # - '192.88.99.0/24'
  6265. # - '198.18.0.0/15'
  6266. # - '192.0.2.0/24'
  6267. # - '198.51.100.0/24'
  6268. # - '203.0.113.0/24'
  6269. # - '224.0.0.0/4'
  6270. # - '::1/128'
  6271. # - 'fe80::/10'
  6272. # - 'fc00::/7'
  6273. # - '2001:db8::/32'
  6274. # - 'ff00::/8'
  6275. # - 'fec0::/10'
  6276. # List of IP address CIDR ranges that should be allowed for federation,
  6277. # identity servers, push servers, and for checking key validity for
  6278. # third-party invite events. This is useful for specifying exceptions to
  6279. # wide-ranging blacklisted target IP ranges - e.g. for communication with
  6280. # a push server only visible in your network.
  6281. #
  6282. # This whitelist overrides ip_range_blacklist and defaults to an empty
  6283. # list.
  6284. #
  6285. #ip_range_whitelist:
  6286. # - '192.168.1.1'
  6287. # List of ports that Synapse should listen on, their purpose and their
  6288. # configuration.
  6289. #
  6290. # Options for each listener include:
  6291. #
  6292. # port: the TCP port to bind to
  6293. #
  6294. # bind_addresses: a list of local addresses to listen on. The default is
  6295. # 'all local interfaces'.
  6296. #
  6297. # type: the type of listener. Normally 'http', but other valid options are:
  6298. # 'manhole' (see https://matrix-org.github.io/synapse/latest/manhole.html),
  6299. # 'metrics' (see https://matrix-org.github.io/synapse/latest/metrics-howto.html),
  6300. # 'replication' (see https://matrix-org.github.io/synapse/latest/workers.html).
  6301. #
  6302. # tls: set to true to enable TLS for this listener. Will use the TLS
  6303. # key/cert specified in tls_private_key_path / tls_certificate_path.
  6304. #
  6305. # x_forwarded: Only valid for an 'http' listener. Set to true to use the
  6306. # X-Forwarded-For header as the client IP. Useful when Synapse is
  6307. # behind a reverse-proxy.
  6308. #
  6309. # resources: Only valid for an 'http' listener. A list of resources to host
  6310. # on this port. Options for each resource are:
  6311. #
  6312. # names: a list of names of HTTP resources. See below for a list of
  6313. # valid resource names.
  6314. #
  6315. # compress: set to true to enable HTTP compression for this resource.
  6316. #
  6317. # additional_resources: Only valid for an 'http' listener. A map of
  6318. # additional endpoints which should be loaded via dynamic modules.
  6319. #
  6320. # Valid resource names are:
  6321. #
  6322. # client: the client-server API (/_matrix/client), and the synapse admin
  6323. # API (/_synapse/admin). Also implies 'media' and 'static'.
  6324. #
  6325. # consent: user consent forms (/_matrix/consent).
  6326. # See https://matrix-org.github.io/synapse/latest/consent_tracking.html.
  6327. #
  6328. # federation: the server-server API (/_matrix/federation). Also implies
  6329. # 'media', 'keys', 'openid'
  6330. #
  6331. # keys: the key discovery API (/_matrix/key).
  6332. #
  6333. # media: the media API (/_matrix/media).
  6334. #
  6335. # metrics: the metrics interface.
  6336. # See https://matrix-org.github.io/synapse/latest/metrics-howto.html.
  6337. #
  6338. # openid: OpenID authentication.
  6339. #
  6340. # replication: the HTTP replication API (/_synapse/replication).
  6341. # See https://matrix-org.github.io/synapse/latest/workers.html.
  6342. #
  6343. # static: static resources under synapse/static (/_matrix/static). (Mostly
  6344. # useful for 'fallback authentication'.)
  6345. #
  6346. listeners:
  6347. # TLS-enabled listener: for when matrix traffic is sent directly to synapse.
  6348. #
  6349. # Disabled by default. To enable it, uncomment the following. (Note that you
  6350. # will also need to give Synapse a TLS key and certificate: see the TLS section
  6351. # below.)
  6352. #
  6353. #- port: 8448
  6354. # type: http
  6355. # tls: true
  6356. # resources:
  6357. # - names: [client, federation]
  6358. # Unsecure HTTP listener: for when matrix traffic passes through a reverse proxy
  6359. # that unwraps TLS.
  6360. #
  6361. # If you plan to use a reverse proxy, please see
  6362. # https://matrix-org.github.io/synapse/latest/reverse_proxy.html.
  6363. #
  6364. - port: 8008
  6365. tls: false
  6366. type: http
  6367. x_forwarded: true
  6368. bind_addresses: ['::1', '127.0.0.1']
  6369. resources:
  6370. - names: [client, federation]
  6371. compress: false
  6372. # example additional_resources:
  6373. #
  6374. #additional_resources:
  6375. # &quot;/_matrix/my/custom/endpoint&quot;:
  6376. # module: my_module.CustomRequestHandler
  6377. # config: {}
  6378. # Turn on the twisted ssh manhole service on localhost on the given
  6379. # port.
  6380. #
  6381. #- port: 9000
  6382. # bind_addresses: ['::1', '127.0.0.1']
  6383. # type: manhole
  6384. # Connection settings for the manhole
  6385. #
  6386. manhole_settings:
  6387. # The username for the manhole. This defaults to 'matrix'.
  6388. #
  6389. #username: manhole
  6390. # The password for the manhole. This defaults to 'rabbithole'.
  6391. #
  6392. #password: mypassword
  6393. # The private and public SSH key pair used to encrypt the manhole traffic.
  6394. # If these are left unset, then hardcoded and non-secret keys are used,
  6395. # which could allow traffic to be intercepted if sent over a public network.
  6396. #
  6397. #ssh_priv_key_path: CONFDIR/id_rsa
  6398. #ssh_pub_key_path: CONFDIR/id_rsa.pub
  6399. # Forward extremities can build up in a room due to networking delays between
  6400. # homeservers. Once this happens in a large room, calculation of the state of
  6401. # that room can become quite expensive. To mitigate this, once the number of
  6402. # forward extremities reaches a given threshold, Synapse will send an
  6403. # org.matrix.dummy_event event, which will reduce the forward extremities
  6404. # in the room.
  6405. #
  6406. # This setting defines the threshold (i.e. number of forward extremities in the
  6407. # room) at which dummy events are sent. The default value is 10.
  6408. #
  6409. #dummy_events_threshold: 5
  6410. ## Homeserver blocking ##
  6411. # How to reach the server admin, used in ResourceLimitError
  6412. #
  6413. #admin_contact: 'mailto:admin@server.com'
  6414. # Global blocking
  6415. #
  6416. #hs_disabled: false
  6417. #hs_disabled_message: 'Human readable reason for why the HS is blocked'
  6418. # Monthly Active User Blocking
  6419. #
  6420. # Used in cases where the admin or server owner wants to limit to the
  6421. # number of monthly active users.
  6422. #
  6423. # 'limit_usage_by_mau' disables/enables monthly active user blocking. When
  6424. # enabled and a limit is reached the server returns a 'ResourceLimitError'
  6425. # with error type Codes.RESOURCE_LIMIT_EXCEEDED
  6426. #
  6427. # 'max_mau_value' is the hard limit of monthly active users above which
  6428. # the server will start blocking user actions.
  6429. #
  6430. # 'mau_trial_days' is a means to add a grace period for active users. It
  6431. # means that users must be active for this number of days before they
  6432. # can be considered active and guards against the case where lots of users
  6433. # sign up in a short space of time never to return after their initial
  6434. # session.
  6435. #
  6436. # The option `mau_appservice_trial_days` is similar to `mau_trial_days`, but
  6437. # applies a different trial number if the user was registered by an appservice.
  6438. # A value of 0 means no trial days are applied. Appservices not listed in this
  6439. # dictionary use the value of `mau_trial_days` instead.
  6440. #
  6441. # 'mau_limit_alerting' is a means of limiting client side alerting
  6442. # should the mau limit be reached. This is useful for small instances
  6443. # where the admin has 5 mau seats (say) for 5 specific people and no
  6444. # interest increasing the mau limit further. Defaults to True, which
  6445. # means that alerting is enabled
  6446. #
  6447. #limit_usage_by_mau: false
  6448. #max_mau_value: 50
  6449. #mau_trial_days: 2
  6450. #mau_limit_alerting: false
  6451. #mau_appservice_trial_days:
  6452. # &quot;appservice-id&quot;: 1
  6453. # If enabled, the metrics for the number of monthly active users will
  6454. # be populated, however no one will be limited. If limit_usage_by_mau
  6455. # is true, this is implied to be true.
  6456. #
  6457. #mau_stats_only: false
  6458. # Sometimes the server admin will want to ensure certain accounts are
  6459. # never blocked by mau checking. These accounts are specified here.
  6460. #
  6461. #mau_limit_reserved_threepids:
  6462. # - medium: 'email'
  6463. # address: 'reserved_user@example.com'
  6464. # Used by phonehome stats to group together related servers.
  6465. #server_context: context
  6466. # Resource-constrained homeserver settings
  6467. #
  6468. # When this is enabled, the room &quot;complexity&quot; will be checked before a user
  6469. # joins a new remote room. If it is above the complexity limit, the server will
  6470. # disallow joining, or will instantly leave.
  6471. #
  6472. # Room complexity is an arbitrary measure based on factors such as the number of
  6473. # users in the room.
  6474. #
  6475. limit_remote_rooms:
  6476. # Uncomment to enable room complexity checking.
  6477. #
  6478. #enabled: true
  6479. # the limit above which rooms cannot be joined. The default is 1.0.
  6480. #
  6481. #complexity: 0.5
  6482. # override the error which is returned when the room is too complex.
  6483. #
  6484. #complexity_error: &quot;This room is too complex.&quot;
  6485. # allow server admins to join complex rooms. Default is false.
  6486. #
  6487. #admins_can_join: true
  6488. # Whether to require a user to be in the room to add an alias to it.
  6489. # Defaults to 'true'.
  6490. #
  6491. #require_membership_for_aliases: false
  6492. # Whether to allow per-room membership profiles through the send of membership
  6493. # events with profile information that differ from the target's global profile.
  6494. # Defaults to 'true'.
  6495. #
  6496. #allow_per_room_profiles: false
  6497. # The largest allowed file size for a user avatar. Defaults to no restriction.
  6498. #
  6499. # Note that user avatar changes will not work if this is set without
  6500. # using Synapse's media repository.
  6501. #
  6502. #max_avatar_size: 10M
  6503. # The MIME types allowed for user avatars. Defaults to no restriction.
  6504. #
  6505. # Note that user avatar changes will not work if this is set without
  6506. # using Synapse's media repository.
  6507. #
  6508. #allowed_avatar_mimetypes: [&quot;image/png&quot;, &quot;image/jpeg&quot;, &quot;image/gif&quot;]
  6509. # How long to keep redacted events in unredacted form in the database. After
  6510. # this period redacted events get replaced with their redacted form in the DB.
  6511. #
  6512. # Defaults to `7d`. Set to `null` to disable.
  6513. #
  6514. #redaction_retention_period: 28d
  6515. # How long to track users' last seen time and IPs in the database.
  6516. #
  6517. # Defaults to `28d`. Set to `null` to disable clearing out of old rows.
  6518. #
  6519. #user_ips_max_age: 14d
  6520. # Inhibits the /requestToken endpoints from returning an error that might leak
  6521. # information about whether an e-mail address is in use or not on this
  6522. # homeserver.
  6523. # Note that for some endpoints the error situation is the e-mail already being
  6524. # used, and for others the error is entering the e-mail being unused.
  6525. # If this option is enabled, instead of returning an error, these endpoints will
  6526. # act as if no error happened and return a fake session ID ('sid') to clients.
  6527. #
  6528. #request_token_inhibit_3pid_errors: true
  6529. # A list of domains that the domain portion of 'next_link' parameters
  6530. # must match.
  6531. #
  6532. # This parameter is optionally provided by clients while requesting
  6533. # validation of an email or phone number, and maps to a link that
  6534. # users will be automatically redirected to after validation
  6535. # succeeds. Clients can make use this parameter to aid the validation
  6536. # process.
  6537. #
  6538. # The whitelist is applied whether the homeserver or an
  6539. # identity server is handling validation.
  6540. #
  6541. # The default value is no whitelist functionality; all domains are
  6542. # allowed. Setting this value to an empty list will instead disallow
  6543. # all domains.
  6544. #
  6545. #next_link_domain_whitelist: [&quot;matrix.org&quot;]
  6546. # Templates to use when generating email or HTML page contents.
  6547. #
  6548. templates:
  6549. # Directory in which Synapse will try to find template files to use to generate
  6550. # email or HTML page contents.
  6551. # If not set, or a file is not found within the template directory, a default
  6552. # template from within the Synapse package will be used.
  6553. #
  6554. # See https://matrix-org.github.io/synapse/latest/templates.html for more
  6555. # information about using custom templates.
  6556. #
  6557. #custom_template_directory: /path/to/custom/templates/
  6558. # List of rooms to exclude from sync responses. This is useful for server
  6559. # administrators wishing to group users into a room without these users being able
  6560. # to see it from their client.
  6561. #
  6562. # By default, no room is excluded.
  6563. #
  6564. #exclude_rooms_from_sync:
  6565. # - !foo:example.com
  6566. # Message retention policy at the server level.
  6567. #
  6568. # Room admins and mods can define a retention period for their rooms using the
  6569. # 'm.room.retention' state event, and server admins can cap this period by setting
  6570. # the 'allowed_lifetime_min' and 'allowed_lifetime_max' config options.
  6571. #
  6572. # If this feature is enabled, Synapse will regularly look for and purge events
  6573. # which are older than the room's maximum retention period. Synapse will also
  6574. # filter events received over federation so that events that should have been
  6575. # purged are ignored and not stored again.
  6576. #
  6577. retention:
  6578. # The message retention policies feature is disabled by default. Uncomment the
  6579. # following line to enable it.
  6580. #
  6581. #enabled: true
  6582. # Default retention policy. If set, Synapse will apply it to rooms that lack the
  6583. # 'm.room.retention' state event. Currently, the value of 'min_lifetime' doesn't
  6584. # matter much because Synapse doesn't take it into account yet.
  6585. #
  6586. #default_policy:
  6587. # min_lifetime: 1d
  6588. # max_lifetime: 1y
  6589. # Retention policy limits. If set, and the state of a room contains a
  6590. # 'm.room.retention' event in its state which contains a 'min_lifetime' or a
  6591. # 'max_lifetime' that's out of these bounds, Synapse will cap the room's policy
  6592. # to these limits when running purge jobs.
  6593. #
  6594. #allowed_lifetime_min: 1d
  6595. #allowed_lifetime_max: 1y
  6596. # Server admins can define the settings of the background jobs purging the
  6597. # events which lifetime has expired under the 'purge_jobs' section.
  6598. #
  6599. # If no configuration is provided, a single job will be set up to delete expired
  6600. # events in every room daily.
  6601. #
  6602. # Each job's configuration defines which range of message lifetimes the job
  6603. # takes care of. For example, if 'shortest_max_lifetime' is '2d' and
  6604. # 'longest_max_lifetime' is '3d', the job will handle purging expired events in
  6605. # rooms whose state defines a 'max_lifetime' that's both higher than 2 days, and
  6606. # lower than or equal to 3 days. Both the minimum and the maximum value of a
  6607. # range are optional, e.g. a job with no 'shortest_max_lifetime' and a
  6608. # 'longest_max_lifetime' of '3d' will handle every room with a retention policy
  6609. # which 'max_lifetime' is lower than or equal to three days.
  6610. #
  6611. # The rationale for this per-job configuration is that some rooms might have a
  6612. # retention policy with a low 'max_lifetime', where history needs to be purged
  6613. # of outdated messages on a more frequent basis than for the rest of the rooms
  6614. # (e.g. every 12h), but not want that purge to be performed by a job that's
  6615. # iterating over every room it knows, which could be heavy on the server.
  6616. #
  6617. # If any purge job is configured, it is strongly recommended to have at least
  6618. # a single job with neither 'shortest_max_lifetime' nor 'longest_max_lifetime'
  6619. # set, or one job without 'shortest_max_lifetime' and one job without
  6620. # 'longest_max_lifetime' set. Otherwise some rooms might be ignored, even if
  6621. # 'allowed_lifetime_min' and 'allowed_lifetime_max' are set, because capping a
  6622. # room's policy to these values is done after the policies are retrieved from
  6623. # Synapse's database (which is done using the range specified in a purge job's
  6624. # configuration).
  6625. #
  6626. #purge_jobs:
  6627. # - longest_max_lifetime: 3d
  6628. # interval: 12h
  6629. # - shortest_max_lifetime: 3d
  6630. # interval: 1d
  6631. ## TLS ##
  6632. # PEM-encoded X509 certificate for TLS.
  6633. # This certificate, as of Synapse 1.0, will need to be a valid and verifiable
  6634. # certificate, signed by a recognised Certificate Authority.
  6635. #
  6636. # Be sure to use a `.pem` file that includes the full certificate chain including
  6637. # any intermediate certificates (for instance, if using certbot, use
  6638. # `fullchain.pem` as your certificate, not `cert.pem`).
  6639. #
  6640. #tls_certificate_path: &quot;CONFDIR/SERVERNAME.tls.crt&quot;
  6641. # PEM-encoded private key for TLS
  6642. #
  6643. #tls_private_key_path: &quot;CONFDIR/SERVERNAME.tls.key&quot;
  6644. # Whether to verify TLS server certificates for outbound federation requests.
  6645. #
  6646. # Defaults to `true`. To disable certificate verification, uncomment the
  6647. # following line.
  6648. #
  6649. #federation_verify_certificates: false
  6650. # The minimum TLS version that will be used for outbound federation requests.
  6651. #
  6652. # Defaults to `1`. Configurable to `1`, `1.1`, `1.2`, or `1.3`. Note
  6653. # that setting this value higher than `1.2` will prevent federation to most
  6654. # of the public Matrix network: only configure it to `1.3` if you have an
  6655. # entirely private federation setup and you can ensure TLS 1.3 support.
  6656. #
  6657. #federation_client_minimum_tls_version: 1.2
  6658. # Skip federation certificate verification on the following whitelist
  6659. # of domains.
  6660. #
  6661. # This setting should only be used in very specific cases, such as
  6662. # federation over Tor hidden services and similar. For private networks
  6663. # of homeservers, you likely want to use a private CA instead.
  6664. #
  6665. # Only effective if federation_verify_certicates is `true`.
  6666. #
  6667. #federation_certificate_verification_whitelist:
  6668. # - lon.example.com
  6669. # - &quot;*.domain.com&quot;
  6670. # - &quot;*.onion&quot;
  6671. # List of custom certificate authorities for federation traffic.
  6672. #
  6673. # This setting should only normally be used within a private network of
  6674. # homeservers.
  6675. #
  6676. # Note that this list will replace those that are provided by your
  6677. # operating environment. Certificates must be in PEM format.
  6678. #
  6679. #federation_custom_ca_list:
  6680. # - myCA1.pem
  6681. # - myCA2.pem
  6682. # - myCA3.pem
  6683. ## Federation ##
  6684. # Restrict federation to the following whitelist of domains.
  6685. # N.B. we recommend also firewalling your federation listener to limit
  6686. # inbound federation traffic as early as possible, rather than relying
  6687. # purely on this application-layer restriction. If not specified, the
  6688. # default is to whitelist everything.
  6689. #
  6690. #federation_domain_whitelist:
  6691. # - lon.example.com
  6692. # - nyc.example.com
  6693. # - syd.example.com
  6694. # Report prometheus metrics on the age of PDUs being sent to and received from
  6695. # the following domains. This can be used to give an idea of &quot;delay&quot; on inbound
  6696. # and outbound federation, though be aware that any delay can be due to problems
  6697. # at either end or with the intermediate network.
  6698. #
  6699. # By default, no domains are monitored in this way.
  6700. #
  6701. #federation_metrics_domains:
  6702. # - matrix.org
  6703. # - example.com
  6704. # Uncomment to disable profile lookup over federation. By default, the
  6705. # Federation API allows other homeservers to obtain profile data of any user
  6706. # on this homeserver. Defaults to 'true'.
  6707. #
  6708. #allow_profile_lookup_over_federation: false
  6709. # Uncomment to allow device display name lookup over federation. By default, the
  6710. # Federation API prevents other homeservers from obtaining the display names of
  6711. # user devices on this homeserver. Defaults to 'false'.
  6712. #
  6713. #allow_device_name_lookup_over_federation: true
  6714. ## Caching ##
  6715. # Caching can be configured through the following options.
  6716. #
  6717. # A cache 'factor' is a multiplier that can be applied to each of
  6718. # Synapse's caches in order to increase or decrease the maximum
  6719. # number of entries that can be stored.
  6720. #
  6721. # The configuration for cache factors (caches.global_factor and
  6722. # caches.per_cache_factors) can be reloaded while the application is running,
  6723. # by sending a SIGHUP signal to the Synapse process. Changes to other parts of
  6724. # the caching config will NOT be applied after a SIGHUP is received; a restart
  6725. # is necessary.
  6726. # The number of events to cache in memory. Not affected by
  6727. # caches.global_factor.
  6728. #
  6729. #event_cache_size: 10K
  6730. caches:
  6731. # Controls the global cache factor, which is the default cache factor
  6732. # for all caches if a specific factor for that cache is not otherwise
  6733. # set.
  6734. #
  6735. # This can also be set by the &quot;SYNAPSE_CACHE_FACTOR&quot; environment
  6736. # variable. Setting by environment variable takes priority over
  6737. # setting through the config file.
  6738. #
  6739. # Defaults to 0.5, which will half the size of all caches.
  6740. #
  6741. #global_factor: 1.0
  6742. # A dictionary of cache name to cache factor for that individual
  6743. # cache. Overrides the global cache factor for a given cache.
  6744. #
  6745. # These can also be set through environment variables comprised
  6746. # of &quot;SYNAPSE_CACHE_FACTOR_&quot; + the name of the cache in capital
  6747. # letters and underscores. Setting by environment variable
  6748. # takes priority over setting through the config file.
  6749. # Ex. SYNAPSE_CACHE_FACTOR_GET_USERS_WHO_SHARE_ROOM_WITH_USER=2.0
  6750. #
  6751. # Some caches have '*' and other characters that are not
  6752. # alphanumeric or underscores. These caches can be named with or
  6753. # without the special characters stripped. For example, to specify
  6754. # the cache factor for `*stateGroupCache*` via an environment
  6755. # variable would be `SYNAPSE_CACHE_FACTOR_STATEGROUPCACHE=2.0`.
  6756. #
  6757. per_cache_factors:
  6758. #get_users_who_share_room_with_user: 2.0
  6759. # Controls whether cache entries are evicted after a specified time
  6760. # period. Defaults to true. Uncomment to disable this feature.
  6761. #
  6762. #expire_caches: false
  6763. # If expire_caches is enabled, this flag controls how long an entry can
  6764. # be in a cache without having been accessed before being evicted.
  6765. # Defaults to 30m. Uncomment to set a different time to live for cache entries.
  6766. #
  6767. #cache_entry_ttl: 30m
  6768. # This flag enables cache autotuning, and is further specified by the sub-options `max_cache_memory_usage`,
  6769. # `target_cache_memory_usage`, `min_cache_ttl`. These flags work in conjunction with each other to maintain
  6770. # a balance between cache memory usage and cache entry availability. You must be using jemalloc to utilize
  6771. # this option, and all three of the options must be specified for this feature to work.
  6772. #cache_autotuning:
  6773. # This flag sets a ceiling on much memory the cache can use before caches begin to be continuously evicted.
  6774. # They will continue to be evicted until the memory usage drops below the `target_memory_usage`, set in
  6775. # the flag below, or until the `min_cache_ttl` is hit.
  6776. #max_cache_memory_usage: 1024M
  6777. # This flag sets a rough target for the desired memory usage of the caches.
  6778. #target_cache_memory_usage: 758M
  6779. # 'min_cache_ttl` sets a limit under which newer cache entries are not evicted and is only applied when
  6780. # caches are actively being evicted/`max_cache_memory_usage` has been exceeded. This is to protect hot caches
  6781. # from being emptied while Synapse is evicting due to memory.
  6782. #min_cache_ttl: 5m
  6783. # Controls how long the results of a /sync request are cached for after
  6784. # a successful response is returned. A higher duration can help clients with
  6785. # intermittent connections, at the cost of higher memory usage.
  6786. #
  6787. # By default, this is zero, which means that sync responses are not cached
  6788. # at all.
  6789. #
  6790. #sync_response_cache_duration: 2m
  6791. ## Database ##
  6792. # The 'database' setting defines the database that synapse uses to store all of
  6793. # its data.
  6794. #
  6795. # 'name' gives the database engine to use: either 'sqlite3' (for SQLite) or
  6796. # 'psycopg2' (for PostgreSQL).
  6797. #
  6798. # 'txn_limit' gives the maximum number of transactions to run per connection
  6799. # before reconnecting. Defaults to 0, which means no limit.
  6800. #
  6801. # 'allow_unsafe_locale' is an option specific to Postgres. Under the default behavior, Synapse will refuse to
  6802. # start if the postgres db is set to a non-C locale. You can override this behavior (which is *not* recommended)
  6803. # by setting 'allow_unsafe_locale' to true. Note that doing so may corrupt your database. You can find more information
  6804. # here: https://matrix-org.github.io/synapse/latest/postgres.html#fixing-incorrect-collate-or-ctype and here:
  6805. # https://wiki.postgresql.org/wiki/Locale_data_changes
  6806. #
  6807. # 'args' gives options which are passed through to the database engine,
  6808. # except for options starting 'cp_', which are used to configure the Twisted
  6809. # connection pool. For a reference to valid arguments, see:
  6810. # * for sqlite: https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
  6811. # * for postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
  6812. # * for the connection pool: https://twistedmatrix.com/documents/current/api/twisted.enterprise.adbapi.ConnectionPool.html#__init__
  6813. #
  6814. #
  6815. # Example SQLite configuration:
  6816. #
  6817. #database:
  6818. # name: sqlite3
  6819. # args:
  6820. # database: /path/to/homeserver.db
  6821. #
  6822. #
  6823. # Example Postgres configuration:
  6824. #
  6825. #database:
  6826. # name: psycopg2
  6827. # txn_limit: 10000
  6828. # args:
  6829. # user: synapse_user
  6830. # password: secretpassword
  6831. # database: synapse
  6832. # host: localhost
  6833. # port: 5432
  6834. # cp_min: 5
  6835. # cp_max: 10
  6836. #
  6837. # For more information on using Synapse with Postgres,
  6838. # see https://matrix-org.github.io/synapse/latest/postgres.html.
  6839. #
  6840. database:
  6841. name: sqlite3
  6842. args:
  6843. database: DATADIR/homeserver.db
  6844. ## Logging ##
  6845. # A yaml python logging config file as described by
  6846. # https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
  6847. #
  6848. log_config: &quot;CONFDIR/SERVERNAME.log.config&quot;
  6849. ## Ratelimiting ##
  6850. # Ratelimiting settings for client actions (registration, login, messaging).
  6851. #
  6852. # Each ratelimiting configuration is made of two parameters:
  6853. # - per_second: number of requests a client can send per second.
  6854. # - burst_count: number of requests a client can send before being throttled.
  6855. #
  6856. # Synapse currently uses the following configurations:
  6857. # - one for messages that ratelimits sending based on the account the client
  6858. # is using
  6859. # - one for registration that ratelimits registration requests based on the
  6860. # client's IP address.
  6861. # - one for checking the validity of registration tokens that ratelimits
  6862. # requests based on the client's IP address.
  6863. # - one for login that ratelimits login requests based on the client's IP
  6864. # address.
  6865. # - one for login that ratelimits login requests based on the account the
  6866. # client is attempting to log into.
  6867. # - one for login that ratelimits login requests based on the account the
  6868. # client is attempting to log into, based on the amount of failed login
  6869. # attempts for this account.
  6870. # - one for ratelimiting redactions by room admins. If this is not explicitly
  6871. # set then it uses the same ratelimiting as per rc_message. This is useful
  6872. # to allow room admins to deal with abuse quickly.
  6873. # - two for ratelimiting number of rooms a user can join, &quot;local&quot; for when
  6874. # users are joining rooms the server is already in (this is cheap) vs
  6875. # &quot;remote&quot; for when users are trying to join rooms not on the server (which
  6876. # can be more expensive)
  6877. # - one for ratelimiting how often a user or IP can attempt to validate a 3PID.
  6878. # - two for ratelimiting how often invites can be sent in a room or to a
  6879. # specific user.
  6880. # - one for ratelimiting 3PID invites (i.e. invites sent to a third-party ID
  6881. # such as an email address or a phone number) based on the account that's
  6882. # sending the invite.
  6883. #
  6884. # The defaults are as shown below.
  6885. #
  6886. #rc_message:
  6887. # per_second: 0.2
  6888. # burst_count: 10
  6889. #
  6890. #rc_registration:
  6891. # per_second: 0.17
  6892. # burst_count: 3
  6893. #
  6894. #rc_registration_token_validity:
  6895. # per_second: 0.1
  6896. # burst_count: 5
  6897. #
  6898. #rc_login:
  6899. # address:
  6900. # per_second: 0.17
  6901. # burst_count: 3
  6902. # account:
  6903. # per_second: 0.17
  6904. # burst_count: 3
  6905. # failed_attempts:
  6906. # per_second: 0.17
  6907. # burst_count: 3
  6908. #
  6909. #rc_admin_redaction:
  6910. # per_second: 1
  6911. # burst_count: 50
  6912. #
  6913. #rc_joins:
  6914. # local:
  6915. # per_second: 0.1
  6916. # burst_count: 10
  6917. # remote:
  6918. # per_second: 0.01
  6919. # burst_count: 10
  6920. #
  6921. #rc_3pid_validation:
  6922. # per_second: 0.003
  6923. # burst_count: 5
  6924. #
  6925. #rc_invites:
  6926. # per_room:
  6927. # per_second: 0.3
  6928. # burst_count: 10
  6929. # per_user:
  6930. # per_second: 0.003
  6931. # burst_count: 5
  6932. #
  6933. #rc_third_party_invite:
  6934. # per_second: 0.2
  6935. # burst_count: 10
  6936. # Ratelimiting settings for incoming federation
  6937. #
  6938. # The rc_federation configuration is made up of the following settings:
  6939. # - window_size: window size in milliseconds
  6940. # - sleep_limit: number of federation requests from a single server in
  6941. # a window before the server will delay processing the request.
  6942. # - sleep_delay: duration in milliseconds to delay processing events
  6943. # from remote servers by if they go over the sleep limit.
  6944. # - reject_limit: maximum number of concurrent federation requests
  6945. # allowed from a single server
  6946. # - concurrent: number of federation requests to concurrently process
  6947. # from a single server
  6948. #
  6949. # The defaults are as shown below.
  6950. #
  6951. #rc_federation:
  6952. # window_size: 1000
  6953. # sleep_limit: 10
  6954. # sleep_delay: 500
  6955. # reject_limit: 50
  6956. # concurrent: 3
  6957. # Target outgoing federation transaction frequency for sending read-receipts,
  6958. # per-room.
  6959. #
  6960. # If we end up trying to send out more read-receipts, they will get buffered up
  6961. # into fewer transactions.
  6962. #
  6963. #federation_rr_transactions_per_room_per_second: 50
  6964. ## Media Store ##
  6965. # Enable the media store service in the Synapse master. Uncomment the
  6966. # following if you are using a separate media store worker.
  6967. #
  6968. #enable_media_repo: false
  6969. # Directory where uploaded images and attachments are stored.
  6970. #
  6971. media_store_path: &quot;DATADIR/media_store&quot;
  6972. # Media storage providers allow media to be stored in different
  6973. # locations.
  6974. #
  6975. #media_storage_providers:
  6976. # - module: file_system
  6977. # # Whether to store newly uploaded local files
  6978. # store_local: false
  6979. # # Whether to store newly downloaded remote files
  6980. # store_remote: false
  6981. # # Whether to wait for successful storage for local uploads
  6982. # store_synchronous: false
  6983. # config:
  6984. # directory: /mnt/some/other/directory
  6985. # The largest allowed upload size in bytes
  6986. #
  6987. # If you are using a reverse proxy you may also need to set this value in
  6988. # your reverse proxy's config. Notably Nginx has a small max body size by default.
  6989. # See https://matrix-org.github.io/synapse/latest/reverse_proxy.html.
  6990. #
  6991. #max_upload_size: 50M
  6992. # Maximum number of pixels that will be thumbnailed
  6993. #
  6994. #max_image_pixels: 32M
  6995. # Whether to generate new thumbnails on the fly to precisely match
  6996. # the resolution requested by the client. If true then whenever
  6997. # a new resolution is requested by the client the server will
  6998. # generate a new thumbnail. If false the server will pick a thumbnail
  6999. # from a precalculated list.
  7000. #
  7001. #dynamic_thumbnails: false
  7002. # List of thumbnails to precalculate when an image is uploaded.
  7003. #
  7004. #thumbnail_sizes:
  7005. # - width: 32
  7006. # height: 32
  7007. # method: crop
  7008. # - width: 96
  7009. # height: 96
  7010. # method: crop
  7011. # - width: 320
  7012. # height: 240
  7013. # method: scale
  7014. # - width: 640
  7015. # height: 480
  7016. # method: scale
  7017. # - width: 800
  7018. # height: 600
  7019. # method: scale
  7020. # Is the preview URL API enabled?
  7021. #
  7022. # 'false' by default: uncomment the following to enable it (and specify a
  7023. # url_preview_ip_range_blacklist blacklist).
  7024. #
  7025. #url_preview_enabled: true
  7026. # List of IP address CIDR ranges that the URL preview spider is denied
  7027. # from accessing. There are no defaults: you must explicitly
  7028. # specify a list for URL previewing to work. You should specify any
  7029. # internal services in your network that you do not want synapse to try
  7030. # to connect to, otherwise anyone in any Matrix room could cause your
  7031. # synapse to issue arbitrary GET requests to your internal services,
  7032. # causing serious security issues.
  7033. #
  7034. # (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
  7035. # listed here, since they correspond to unroutable addresses.)
  7036. #
  7037. # This must be specified if url_preview_enabled is set. It is recommended that
  7038. # you uncomment the following list as a starting point.
  7039. #
  7040. # Note: The value is ignored when an HTTP proxy is in use
  7041. #
  7042. #url_preview_ip_range_blacklist:
  7043. # - '127.0.0.0/8'
  7044. # - '10.0.0.0/8'
  7045. # - '172.16.0.0/12'
  7046. # - '192.168.0.0/16'
  7047. # - '100.64.0.0/10'
  7048. # - '192.0.0.0/24'
  7049. # - '169.254.0.0/16'
  7050. # - '192.88.99.0/24'
  7051. # - '198.18.0.0/15'
  7052. # - '192.0.2.0/24'
  7053. # - '198.51.100.0/24'
  7054. # - '203.0.113.0/24'
  7055. # - '224.0.0.0/4'
  7056. # - '::1/128'
  7057. # - 'fe80::/10'
  7058. # - 'fc00::/7'
  7059. # - '2001:db8::/32'
  7060. # - 'ff00::/8'
  7061. # - 'fec0::/10'
  7062. # List of IP address CIDR ranges that the URL preview spider is allowed
  7063. # to access even if they are specified in url_preview_ip_range_blacklist.
  7064. # This is useful for specifying exceptions to wide-ranging blacklisted
  7065. # target IP ranges - e.g. for enabling URL previews for a specific private
  7066. # website only visible in your network.
  7067. #
  7068. #url_preview_ip_range_whitelist:
  7069. # - '192.168.1.1'
  7070. # Optional list of URL matches that the URL preview spider is
  7071. # denied from accessing. You should use url_preview_ip_range_blacklist
  7072. # in preference to this, otherwise someone could define a public DNS
  7073. # entry that points to a private IP address and circumvent the blacklist.
  7074. # This is more useful if you know there is an entire shape of URL that
  7075. # you know that will never want synapse to try to spider.
  7076. #
  7077. # Each list entry is a dictionary of url component attributes as returned
  7078. # by urlparse.urlsplit as applied to the absolute form of the URL. See
  7079. # https://docs.python.org/2/library/urlparse.html#urlparse.urlsplit
  7080. # The values of the dictionary are treated as an filename match pattern
  7081. # applied to that component of URLs, unless they start with a ^ in which
  7082. # case they are treated as a regular expression match. If all the
  7083. # specified component matches for a given list item succeed, the URL is
  7084. # blacklisted.
  7085. #
  7086. #url_preview_url_blacklist:
  7087. # # blacklist any URL with a username in its URI
  7088. # - username: '*'
  7089. #
  7090. # # blacklist all *.google.com URLs
  7091. # - netloc: 'google.com'
  7092. # - netloc: '*.google.com'
  7093. #
  7094. # # blacklist all plain HTTP URLs
  7095. # - scheme: 'http'
  7096. #
  7097. # # blacklist http(s)://www.acme.com/foo
  7098. # - netloc: 'www.acme.com'
  7099. # path: '/foo'
  7100. #
  7101. # # blacklist any URL with a literal IPv4 address
  7102. # - netloc: '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
  7103. # The largest allowed URL preview spidering size in bytes
  7104. #
  7105. #max_spider_size: 10M
  7106. # A list of values for the Accept-Language HTTP header used when
  7107. # downloading webpages during URL preview generation. This allows
  7108. # Synapse to specify the preferred languages that URL previews should
  7109. # be in when communicating with remote servers.
  7110. #
  7111. # Each value is a IETF language tag; a 2-3 letter identifier for a
  7112. # language, optionally followed by subtags separated by '-', specifying
  7113. # a country or region variant.
  7114. #
  7115. # Multiple values can be provided, and a weight can be added to each by
  7116. # using quality value syntax (;q=). '*' translates to any language.
  7117. #
  7118. # Defaults to &quot;en&quot;.
  7119. #
  7120. # Example:
  7121. #
  7122. # url_preview_accept_language:
  7123. # - en-UK
  7124. # - en-US;q=0.9
  7125. # - fr;q=0.8
  7126. # - *;q=0.7
  7127. #
  7128. url_preview_accept_language:
  7129. # - en
  7130. # oEmbed allows for easier embedding content from a website. It can be
  7131. # used for generating URLs previews of services which support it.
  7132. #
  7133. oembed:
  7134. # A default list of oEmbed providers is included with Synapse.
  7135. #
  7136. # Uncomment the following to disable using these default oEmbed URLs.
  7137. # Defaults to 'false'.
  7138. #
  7139. #disable_default_providers: true
  7140. # Additional files with oEmbed configuration (each should be in the
  7141. # form of providers.json).
  7142. #
  7143. # By default, this list is empty (so only the default providers.json
  7144. # is used).
  7145. #
  7146. #additional_providers:
  7147. # - oembed/my_providers.json
  7148. ## Captcha ##
  7149. # See docs/CAPTCHA_SETUP.md for full details of configuring this.
  7150. # This homeserver's ReCAPTCHA public key. Must be specified if
  7151. # enable_registration_captcha is enabled.
  7152. #
  7153. #recaptcha_public_key: &quot;YOUR_PUBLIC_KEY&quot;
  7154. # This homeserver's ReCAPTCHA private key. Must be specified if
  7155. # enable_registration_captcha is enabled.
  7156. #
  7157. #recaptcha_private_key: &quot;YOUR_PRIVATE_KEY&quot;
  7158. # Uncomment to enable ReCaptcha checks when registering, preventing signup
  7159. # unless a captcha is answered. Requires a valid ReCaptcha
  7160. # public/private key. Defaults to 'false'.
  7161. #
  7162. #enable_registration_captcha: true
  7163. # The API endpoint to use for verifying m.login.recaptcha responses.
  7164. # Defaults to &quot;https://www.recaptcha.net/recaptcha/api/siteverify&quot;.
  7165. #
  7166. #recaptcha_siteverify_api: &quot;https://my.recaptcha.site&quot;
  7167. ## TURN ##
  7168. # The public URIs of the TURN server to give to clients
  7169. #
  7170. #turn_uris: []
  7171. # The shared secret used to compute passwords for the TURN server
  7172. #
  7173. #turn_shared_secret: &quot;YOUR_SHARED_SECRET&quot;
  7174. # The Username and password if the TURN server needs them and
  7175. # does not use a token
  7176. #
  7177. #turn_username: &quot;TURNSERVER_USERNAME&quot;
  7178. #turn_password: &quot;TURNSERVER_PASSWORD&quot;
  7179. # How long generated TURN credentials last
  7180. #
  7181. #turn_user_lifetime: 1h
  7182. # Whether guests should be allowed to use the TURN server.
  7183. # This defaults to True, otherwise VoIP will be unreliable for guests.
  7184. # However, it does introduce a slight security risk as it allows users to
  7185. # connect to arbitrary endpoints without having first signed up for a
  7186. # valid account (e.g. by passing a CAPTCHA).
  7187. #
  7188. #turn_allow_guests: true
  7189. ## Registration ##
  7190. #
  7191. # Registration can be rate-limited using the parameters in the &quot;Ratelimiting&quot;
  7192. # section of this file.
  7193. # Enable registration for new users. Defaults to 'false'. It is highly recommended that if you enable registration,
  7194. # you use either captcha, email, or token-based verification to verify that new users are not bots. In order to enable registration
  7195. # without any verification, you must also set `enable_registration_without_verification`, found below.
  7196. #
  7197. #enable_registration: false
  7198. # Enable registration without email or captcha verification. Note: this option is *not* recommended,
  7199. # as registration without verification is a known vector for spam and abuse. Defaults to false. Has no effect
  7200. # unless `enable_registration` is also enabled.
  7201. #
  7202. #enable_registration_without_verification: true
  7203. # Time that a user's session remains valid for, after they log in.
  7204. #
  7205. # Note that this is not currently compatible with guest logins.
  7206. #
  7207. # Note also that this is calculated at login time: changes are not applied
  7208. # retrospectively to users who have already logged in.
  7209. #
  7210. # By default, this is infinite.
  7211. #
  7212. #session_lifetime: 24h
  7213. # Time that an access token remains valid for, if the session is
  7214. # using refresh tokens.
  7215. # For more information about refresh tokens, please see the manual.
  7216. # Note that this only applies to clients which advertise support for
  7217. # refresh tokens.
  7218. #
  7219. # Note also that this is calculated at login time and refresh time:
  7220. # changes are not applied to existing sessions until they are refreshed.
  7221. #
  7222. # By default, this is 5 minutes.
  7223. #
  7224. #refreshable_access_token_lifetime: 5m
  7225. # Time that a refresh token remains valid for (provided that it is not
  7226. # exchanged for another one first).
  7227. # This option can be used to automatically log-out inactive sessions.
  7228. # Please see the manual for more information.
  7229. #
  7230. # Note also that this is calculated at login time and refresh time:
  7231. # changes are not applied to existing sessions until they are refreshed.
  7232. #
  7233. # By default, this is infinite.
  7234. #
  7235. #refresh_token_lifetime: 24h
  7236. # Time that an access token remains valid for, if the session is NOT
  7237. # using refresh tokens.
  7238. # Please note that not all clients support refresh tokens, so setting
  7239. # this to a short value may be inconvenient for some users who will
  7240. # then be logged out frequently.
  7241. #
  7242. # Note also that this is calculated at login time: changes are not applied
  7243. # retrospectively to existing sessions for users that have already logged in.
  7244. #
  7245. # By default, this is infinite.
  7246. #
  7247. #nonrefreshable_access_token_lifetime: 24h
  7248. # The user must provide all of the below types of 3PID when registering.
  7249. #
  7250. #registrations_require_3pid:
  7251. # - email
  7252. # - msisdn
  7253. # Explicitly disable asking for MSISDNs from the registration
  7254. # flow (overrides registrations_require_3pid if MSISDNs are set as required)
  7255. #
  7256. #disable_msisdn_registration: true
  7257. # Mandate that users are only allowed to associate certain formats of
  7258. # 3PIDs with accounts on this server.
  7259. #
  7260. #allowed_local_3pids:
  7261. # - medium: email
  7262. # pattern: '^[^@]+@matrix\.org$'
  7263. # - medium: email
  7264. # pattern: '^[^@]+@vector\.im$'
  7265. # - medium: msisdn
  7266. # pattern: '\+44'
  7267. # Enable 3PIDs lookup requests to identity servers from this server.
  7268. #
  7269. #enable_3pid_lookup: true
  7270. # Require users to submit a token during registration.
  7271. # Tokens can be managed using the admin API:
  7272. # https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/registration_tokens.html
  7273. # Note that `enable_registration` must be set to `true`.
  7274. # Disabling this option will not delete any tokens previously generated.
  7275. # Defaults to false. Uncomment the following to require tokens:
  7276. #
  7277. #registration_requires_token: true
  7278. # Allow users to submit a token during registration to bypass any required 3pid
  7279. # steps configured in `registrations_require_3pid`.
  7280. # Defaults to false, requiring that registration tokens (if enabled) complete a 3pid flow.
  7281. #
  7282. #enable_registration_token_3pid_bypass: false
  7283. # If set, allows registration of standard or admin accounts by anyone who
  7284. # has the shared secret, even if registration is otherwise disabled.
  7285. #
  7286. #registration_shared_secret: &lt;PRIVATE STRING&gt;
  7287. # Set the number of bcrypt rounds used to generate password hash.
  7288. # Larger numbers increase the work factor needed to generate the hash.
  7289. # The default number is 12 (which equates to 2^12 rounds).
  7290. # N.B. that increasing this will exponentially increase the time required
  7291. # to register or login - e.g. 24 =&gt; 2^24 rounds which will take &gt;20 mins.
  7292. #
  7293. #bcrypt_rounds: 12
  7294. # Allows users to register as guests without a password/email/etc, and
  7295. # participate in rooms hosted on this server which have been made
  7296. # accessible to anonymous users.
  7297. #
  7298. #allow_guest_access: false
  7299. # The identity server which we suggest that clients should use when users log
  7300. # in on this server.
  7301. #
  7302. # (By default, no suggestion is made, so it is left up to the client.
  7303. # This setting is ignored unless public_baseurl is also explicitly set.)
  7304. #
  7305. #default_identity_server: https://matrix.org
  7306. # Handle threepid (email/phone etc) registration and password resets through a set of
  7307. # *trusted* identity servers. Note that this allows the configured identity server to
  7308. # reset passwords for accounts!
  7309. #
  7310. # Be aware that if `email` is not set, and SMTP options have not been
  7311. # configured in the email config block, registration and user password resets via
  7312. # email will be globally disabled.
  7313. #
  7314. # Additionally, if `msisdn` is not set, registration and password resets via msisdn
  7315. # will be disabled regardless, and users will not be able to associate an msisdn
  7316. # identifier to their account. This is due to Synapse currently not supporting
  7317. # any method of sending SMS messages on its own.
  7318. #
  7319. # To enable using an identity server for operations regarding a particular third-party
  7320. # identifier type, set the value to the URL of that identity server as shown in the
  7321. # examples below.
  7322. #
  7323. # Servers handling the these requests must answer the `/requestToken` endpoints defined
  7324. # by the Matrix Identity Service API specification:
  7325. # https://matrix.org/docs/spec/identity_service/latest
  7326. #
  7327. account_threepid_delegates:
  7328. #email: https://example.com # Delegate email sending to example.com
  7329. #msisdn: http://localhost:8090 # Delegate SMS sending to this local process
  7330. # Whether users are allowed to change their displayname after it has
  7331. # been initially set. Useful when provisioning users based on the
  7332. # contents of a third-party directory.
  7333. #
  7334. # Does not apply to server administrators. Defaults to 'true'
  7335. #
  7336. #enable_set_displayname: false
  7337. # Whether users are allowed to change their avatar after it has been
  7338. # initially set. Useful when provisioning users based on the contents
  7339. # of a third-party directory.
  7340. #
  7341. # Does not apply to server administrators. Defaults to 'true'
  7342. #
  7343. #enable_set_avatar_url: false
  7344. # Whether users can change the 3PIDs associated with their accounts
  7345. # (email address and msisdn).
  7346. #
  7347. # Defaults to 'true'
  7348. #
  7349. #enable_3pid_changes: false
  7350. # Users who register on this homeserver will automatically be joined
  7351. # to these rooms.
  7352. #
  7353. # By default, any room aliases included in this list will be created
  7354. # as a publicly joinable room when the first user registers for the
  7355. # homeserver. This behaviour can be customised with the settings below.
  7356. # If the room already exists, make certain it is a publicly joinable
  7357. # room. The join rule of the room must be set to 'public'.
  7358. #
  7359. #auto_join_rooms:
  7360. # - &quot;#example:example.com&quot;
  7361. # Where auto_join_rooms are specified, setting this flag ensures that the
  7362. # the rooms exist by creating them when the first user on the
  7363. # homeserver registers.
  7364. #
  7365. # By default the auto-created rooms are publicly joinable from any federated
  7366. # server. Use the autocreate_auto_join_rooms_federated and
  7367. # autocreate_auto_join_room_preset settings below to customise this behaviour.
  7368. #
  7369. # Setting to false means that if the rooms are not manually created,
  7370. # users cannot be auto-joined since they do not exist.
  7371. #
  7372. # Defaults to true. Uncomment the following line to disable automatically
  7373. # creating auto-join rooms.
  7374. #
  7375. #autocreate_auto_join_rooms: false
  7376. # Whether the auto_join_rooms that are auto-created are available via
  7377. # federation. Only has an effect if autocreate_auto_join_rooms is true.
  7378. #
  7379. # Note that whether a room is federated cannot be modified after
  7380. # creation.
  7381. #
  7382. # Defaults to true: the room will be joinable from other servers.
  7383. # Uncomment the following to prevent users from other homeservers from
  7384. # joining these rooms.
  7385. #
  7386. #autocreate_auto_join_rooms_federated: false
  7387. # The room preset to use when auto-creating one of auto_join_rooms. Only has an
  7388. # effect if autocreate_auto_join_rooms is true.
  7389. #
  7390. # This can be one of &quot;public_chat&quot;, &quot;private_chat&quot;, or &quot;trusted_private_chat&quot;.
  7391. # If a value of &quot;private_chat&quot; or &quot;trusted_private_chat&quot; is used then
  7392. # auto_join_mxid_localpart must also be configured.
  7393. #
  7394. # Defaults to &quot;public_chat&quot;, meaning that the room is joinable by anyone, including
  7395. # federated servers if autocreate_auto_join_rooms_federated is true (the default).
  7396. # Uncomment the following to require an invitation to join these rooms.
  7397. #
  7398. #autocreate_auto_join_room_preset: private_chat
  7399. # The local part of the user id which is used to create auto_join_rooms if
  7400. # autocreate_auto_join_rooms is true. If this is not provided then the
  7401. # initial user account that registers will be used to create the rooms.
  7402. #
  7403. # The user id is also used to invite new users to any auto-join rooms which
  7404. # are set to invite-only.
  7405. #
  7406. # It *must* be configured if autocreate_auto_join_room_preset is set to
  7407. # &quot;private_chat&quot; or &quot;trusted_private_chat&quot;.
  7408. #
  7409. # Note that this must be specified in order for new users to be correctly
  7410. # invited to any auto-join rooms which have been set to invite-only (either
  7411. # at the time of creation or subsequently).
  7412. #
  7413. # Note that, if the room already exists, this user must be joined and
  7414. # have the appropriate permissions to invite new members.
  7415. #
  7416. #auto_join_mxid_localpart: system
  7417. # When auto_join_rooms is specified, setting this flag to false prevents
  7418. # guest accounts from being automatically joined to the rooms.
  7419. #
  7420. # Defaults to true.
  7421. #
  7422. #auto_join_rooms_for_guests: false
  7423. # Whether to inhibit errors raised when registering a new account if the user ID
  7424. # already exists. If turned on, that requests to /register/available will always
  7425. # show a user ID as available, and Synapse won't raise an error when starting
  7426. # a registration with a user ID that already exists. However, Synapse will still
  7427. # raise an error if the registration completes and the username conflicts.
  7428. #
  7429. # Defaults to false.
  7430. #
  7431. #inhibit_user_in_use_error: true
  7432. ## Metrics ###
  7433. # Enable collection and rendering of performance metrics
  7434. #
  7435. #enable_metrics: false
  7436. # Enable sentry integration
  7437. # NOTE: While attempts are made to ensure that the logs don't contain
  7438. # any sensitive information, this cannot be guaranteed. By enabling
  7439. # this option the sentry server may therefore receive sensitive
  7440. # information, and it in turn may then diseminate sensitive information
  7441. # through insecure notification channels if so configured.
  7442. #
  7443. #sentry:
  7444. # dsn: &quot;...&quot;
  7445. # Flags to enable Prometheus metrics which are not suitable to be
  7446. # enabled by default, either for performance reasons or limited use.
  7447. #
  7448. metrics_flags:
  7449. # Publish synapse_federation_known_servers, a gauge of the number of
  7450. # servers this homeserver knows about, including itself. May cause
  7451. # performance problems on large homeservers.
  7452. #
  7453. #known_servers: true
  7454. # Whether or not to report anonymized homeserver usage statistics.
  7455. #
  7456. #report_stats: true|false
  7457. # The endpoint to report the anonymized homeserver usage statistics to.
  7458. # Defaults to https://matrix.org/report-usage-stats/push
  7459. #
  7460. #report_stats_endpoint: https://example.com/report-usage-stats/push
  7461. ## API Configuration ##
  7462. # Controls for the state that is shared with users who receive an invite
  7463. # to a room
  7464. #
  7465. room_prejoin_state:
  7466. # By default, the following state event types are shared with users who
  7467. # receive invites to the room:
  7468. #
  7469. # - m.room.join_rules
  7470. # - m.room.canonical_alias
  7471. # - m.room.avatar
  7472. # - m.room.encryption
  7473. # - m.room.name
  7474. # - m.room.create
  7475. # - m.room.topic
  7476. #
  7477. # Uncomment the following to disable these defaults (so that only the event
  7478. # types listed in 'additional_event_types' are shared). Defaults to 'false'.
  7479. #
  7480. #disable_default_event_types: true
  7481. # Additional state event types to share with users when they are invited
  7482. # to a room.
  7483. #
  7484. # By default, this list is empty (so only the default event types are shared).
  7485. #
  7486. #additional_event_types:
  7487. # - org.example.custom.event.type
  7488. # We record the IP address of clients used to access the API for various
  7489. # reasons, including displaying it to the user in the &quot;Where you're signed in&quot;
  7490. # dialog.
  7491. #
  7492. # By default, when puppeting another user via the admin API, the client IP
  7493. # address is recorded against the user who created the access token (ie, the
  7494. # admin user), and *not* the puppeted user.
  7495. #
  7496. # Uncomment the following to also record the IP address against the puppeted
  7497. # user. (This also means that the puppeted user will count as an &quot;active&quot; user
  7498. # for the purpose of monthly active user tracking - see 'limit_usage_by_mau' etc
  7499. # above.)
  7500. #
  7501. #track_puppeted_user_ips: true
  7502. # A list of application service config files to use
  7503. #
  7504. #app_service_config_files:
  7505. # - app_service_1.yaml
  7506. # - app_service_2.yaml
  7507. # Uncomment to enable tracking of application service IP addresses. Implicitly
  7508. # enables MAU tracking for application service users.
  7509. #
  7510. #track_appservice_user_ips: true
  7511. # a secret which is used to sign access tokens. If none is specified,
  7512. # the registration_shared_secret is used, if one is given; otherwise,
  7513. # a secret key is derived from the signing key.
  7514. #
  7515. #macaroon_secret_key: &lt;PRIVATE STRING&gt;
  7516. # a secret which is used to calculate HMACs for form values, to stop
  7517. # falsification of values. Must be specified for the User Consent
  7518. # forms to work.
  7519. #
  7520. #form_secret: &lt;PRIVATE STRING&gt;
  7521. ## Signing Keys ##
  7522. # Path to the signing key to sign messages with
  7523. #
  7524. signing_key_path: &quot;CONFDIR/SERVERNAME.signing.key&quot;
  7525. # The keys that the server used to sign messages with but won't use
  7526. # to sign new messages.
  7527. #
  7528. old_signing_keys:
  7529. # For each key, `key` should be the base64-encoded public key, and
  7530. # `expired_ts`should be the time (in milliseconds since the unix epoch) that
  7531. # it was last used.
  7532. #
  7533. # It is possible to build an entry from an old signing.key file using the
  7534. # `export_signing_key` script which is provided with synapse.
  7535. #
  7536. # For example:
  7537. #
  7538. #&quot;ed25519:id&quot;: { key: &quot;base64string&quot;, expired_ts: 123456789123 }
  7539. # How long key response published by this server is valid for.
  7540. # Used to set the valid_until_ts in /key/v2 APIs.
  7541. # Determines how quickly servers will query to check which keys
  7542. # are still valid.
  7543. #
  7544. #key_refresh_interval: 1d
  7545. # The trusted servers to download signing keys from.
  7546. #
  7547. # When we need to fetch a signing key, each server is tried in parallel.
  7548. #
  7549. # Normally, the connection to the key server is validated via TLS certificates.
  7550. # Additional security can be provided by configuring a `verify key`, which
  7551. # will make synapse check that the response is signed by that key.
  7552. #
  7553. # This setting supercedes an older setting named `perspectives`. The old format
  7554. # is still supported for backwards-compatibility, but it is deprecated.
  7555. #
  7556. # 'trusted_key_servers' defaults to matrix.org, but using it will generate a
  7557. # warning on start-up. To suppress this warning, set
  7558. # 'suppress_key_server_warning' to true.
  7559. #
  7560. # Options for each entry in the list include:
  7561. #
  7562. # server_name: the name of the server. required.
  7563. #
  7564. # verify_keys: an optional map from key id to base64-encoded public key.
  7565. # If specified, we will check that the response is signed by at least
  7566. # one of the given keys.
  7567. #
  7568. # accept_keys_insecurely: a boolean. Normally, if `verify_keys` is unset,
  7569. # and federation_verify_certificates is not `true`, synapse will refuse
  7570. # to start, because this would allow anyone who can spoof DNS responses
  7571. # to masquerade as the trusted key server. If you know what you are doing
  7572. # and are sure that your network environment provides a secure connection
  7573. # to the key server, you can set this to `true` to override this
  7574. # behaviour.
  7575. #
  7576. # An example configuration might look like:
  7577. #
  7578. #trusted_key_servers:
  7579. # - server_name: &quot;my_trusted_server.example.com&quot;
  7580. # verify_keys:
  7581. # &quot;ed25519:auto&quot;: &quot;abcdefghijklmnopqrstuvwxyzabcdefghijklmopqr&quot;
  7582. # - server_name: &quot;my_other_trusted_server.example.com&quot;
  7583. #
  7584. trusted_key_servers:
  7585. - server_name: &quot;matrix.org&quot;
  7586. # Uncomment the following to disable the warning that is emitted when the
  7587. # trusted_key_servers include 'matrix.org'. See above.
  7588. #
  7589. #suppress_key_server_warning: true
  7590. # The signing keys to use when acting as a trusted key server. If not specified
  7591. # defaults to the server signing key.
  7592. #
  7593. # Can contain multiple keys, one per line.
  7594. #
  7595. #key_server_signing_keys_path: &quot;key_server_signing_keys.key&quot;
  7596. ## Single sign-on integration ##
  7597. # The following settings can be used to make Synapse use a single sign-on
  7598. # provider for authentication, instead of its internal password database.
  7599. #
  7600. # You will probably also want to set the following options to `false` to
  7601. # disable the regular login/registration flows:
  7602. # * enable_registration
  7603. # * password_config.enabled
  7604. #
  7605. # You will also want to investigate the settings under the &quot;sso&quot; configuration
  7606. # section below.
  7607. # Enable SAML2 for registration and login. Uses pysaml2.
  7608. #
  7609. # At least one of `sp_config` or `config_path` must be set in this section to
  7610. # enable SAML login.
  7611. #
  7612. # Once SAML support is enabled, a metadata file will be exposed at
  7613. # https://&lt;server&gt;:&lt;port&gt;/_synapse/client/saml2/metadata.xml, which you may be able to
  7614. # use to configure your SAML IdP with. Alternatively, you can manually configure
  7615. # the IdP to use an ACS location of
  7616. # https://&lt;server&gt;:&lt;port&gt;/_synapse/client/saml2/authn_response.
  7617. #
  7618. saml2_config:
  7619. # `sp_config` is the configuration for the pysaml2 Service Provider.
  7620. # See pysaml2 docs for format of config.
  7621. #
  7622. # Default values will be used for the 'entityid' and 'service' settings,
  7623. # so it is not normally necessary to specify them unless you need to
  7624. # override them.
  7625. #
  7626. sp_config:
  7627. # Point this to the IdP's metadata. You must provide either a local
  7628. # file via the `local` attribute or (preferably) a URL via the
  7629. # `remote` attribute.
  7630. #
  7631. #metadata:
  7632. # local: [&quot;saml2/idp.xml&quot;]
  7633. # remote:
  7634. # - url: https://our_idp/metadata.xml
  7635. # Allowed clock difference in seconds between the homeserver and IdP.
  7636. #
  7637. # Uncomment the below to increase the accepted time difference from 0 to 3 seconds.
  7638. #
  7639. #accepted_time_diff: 3
  7640. # By default, the user has to go to our login page first. If you'd like
  7641. # to allow IdP-initiated login, set 'allow_unsolicited: true' in a
  7642. # 'service.sp' section:
  7643. #
  7644. #service:
  7645. # sp:
  7646. # allow_unsolicited: true
  7647. # The examples below are just used to generate our metadata xml, and you
  7648. # may well not need them, depending on your setup. Alternatively you
  7649. # may need a whole lot more detail - see the pysaml2 docs!
  7650. #description: [&quot;My awesome SP&quot;, &quot;en&quot;]
  7651. #name: [&quot;Test SP&quot;, &quot;en&quot;]
  7652. #ui_info:
  7653. # display_name:
  7654. # - lang: en
  7655. # text: &quot;Display Name is the descriptive name of your service.&quot;
  7656. # description:
  7657. # - lang: en
  7658. # text: &quot;Description should be a short paragraph explaining the purpose of the service.&quot;
  7659. # information_url:
  7660. # - lang: en
  7661. # text: &quot;https://example.com/terms-of-service&quot;
  7662. # privacy_statement_url:
  7663. # - lang: en
  7664. # text: &quot;https://example.com/privacy-policy&quot;
  7665. # keywords:
  7666. # - lang: en
  7667. # text: [&quot;Matrix&quot;, &quot;Element&quot;]
  7668. # logo:
  7669. # - lang: en
  7670. # text: &quot;https://example.com/logo.svg&quot;
  7671. # width: &quot;200&quot;
  7672. # height: &quot;80&quot;
  7673. #organization:
  7674. # name: Example com
  7675. # display_name:
  7676. # - [&quot;Example co&quot;, &quot;en&quot;]
  7677. # url: &quot;http://example.com&quot;
  7678. #contact_person:
  7679. # - given_name: Bob
  7680. # sur_name: &quot;the Sysadmin&quot;
  7681. # email_address&quot;: [&quot;admin@example.com&quot;]
  7682. # contact_type&quot;: technical
  7683. # Instead of putting the config inline as above, you can specify a
  7684. # separate pysaml2 configuration file:
  7685. #
  7686. #config_path: &quot;CONFDIR/sp_conf.py&quot;
  7687. # The lifetime of a SAML session. This defines how long a user has to
  7688. # complete the authentication process, if allow_unsolicited is unset.
  7689. # The default is 15 minutes.
  7690. #
  7691. #saml_session_lifetime: 5m
  7692. # An external module can be provided here as a custom solution to
  7693. # mapping attributes returned from a saml provider onto a matrix user.
  7694. #
  7695. user_mapping_provider:
  7696. # The custom module's class. Uncomment to use a custom module.
  7697. #
  7698. #module: mapping_provider.SamlMappingProvider
  7699. # Custom configuration values for the module. Below options are
  7700. # intended for the built-in provider, they should be changed if
  7701. # using a custom module. This section will be passed as a Python
  7702. # dictionary to the module's `parse_config` method.
  7703. #
  7704. config:
  7705. # The SAML attribute (after mapping via the attribute maps) to use
  7706. # to derive the Matrix ID from. 'uid' by default.
  7707. #
  7708. # Note: This used to be configured by the
  7709. # saml2_config.mxid_source_attribute option. If that is still
  7710. # defined, its value will be used instead.
  7711. #
  7712. #mxid_source_attribute: displayName
  7713. # The mapping system to use for mapping the saml attribute onto a
  7714. # matrix ID.
  7715. #
  7716. # Options include:
  7717. # * 'hexencode' (which maps unpermitted characters to '=xx')
  7718. # * 'dotreplace' (which replaces unpermitted characters with
  7719. # '.').
  7720. # The default is 'hexencode'.
  7721. #
  7722. # Note: This used to be configured by the
  7723. # saml2_config.mxid_mapping option. If that is still defined, its
  7724. # value will be used instead.
  7725. #
  7726. #mxid_mapping: dotreplace
  7727. # In previous versions of synapse, the mapping from SAML attribute to
  7728. # MXID was always calculated dynamically rather than stored in a
  7729. # table. For backwards- compatibility, we will look for user_ids
  7730. # matching such a pattern before creating a new account.
  7731. #
  7732. # This setting controls the SAML attribute which will be used for this
  7733. # backwards-compatibility lookup. Typically it should be 'uid', but if
  7734. # the attribute maps are changed, it may be necessary to change it.
  7735. #
  7736. # The default is 'uid'.
  7737. #
  7738. #grandfathered_mxid_source_attribute: upn
  7739. # It is possible to configure Synapse to only allow logins if SAML attributes
  7740. # match particular values. The requirements can be listed under
  7741. # `attribute_requirements` as shown below. All of the listed attributes must
  7742. # match for the login to be permitted.
  7743. #
  7744. #attribute_requirements:
  7745. # - attribute: userGroup
  7746. # value: &quot;staff&quot;
  7747. # - attribute: department
  7748. # value: &quot;sales&quot;
  7749. # If the metadata XML contains multiple IdP entities then the `idp_entityid`
  7750. # option must be set to the entity to redirect users to.
  7751. #
  7752. # Most deployments only have a single IdP entity and so should omit this
  7753. # option.
  7754. #
  7755. #idp_entityid: 'https://our_idp/entityid'
  7756. # List of OpenID Connect (OIDC) / OAuth 2.0 identity providers, for registration
  7757. # and login.
  7758. #
  7759. # Options for each entry include:
  7760. #
  7761. # idp_id: a unique identifier for this identity provider. Used internally
  7762. # by Synapse; should be a single word such as 'github'.
  7763. #
  7764. # Note that, if this is changed, users authenticating via that provider
  7765. # will no longer be recognised as the same user!
  7766. #
  7767. # (Use &quot;oidc&quot; here if you are migrating from an old &quot;oidc_config&quot;
  7768. # configuration.)
  7769. #
  7770. # idp_name: A user-facing name for this identity provider, which is used to
  7771. # offer the user a choice of login mechanisms.
  7772. #
  7773. # idp_icon: An optional icon for this identity provider, which is presented
  7774. # by clients and Synapse's own IdP picker page. If given, must be an
  7775. # MXC URI of the format mxc://&lt;server-name&gt;/&lt;media-id&gt;. (An easy way to
  7776. # obtain such an MXC URI is to upload an image to an (unencrypted) room
  7777. # and then copy the &quot;url&quot; from the source of the event.)
  7778. #
  7779. # idp_brand: An optional brand for this identity provider, allowing clients
  7780. # to style the login flow according to the identity provider in question.
  7781. # See the spec for possible options here.
  7782. #
  7783. # discover: set to 'false' to disable the use of the OIDC discovery mechanism
  7784. # to discover endpoints. Defaults to true.
  7785. #
  7786. # issuer: Required. The OIDC issuer. Used to validate tokens and (if discovery
  7787. # is enabled) to discover the provider's endpoints.
  7788. #
  7789. # client_id: Required. oauth2 client id to use.
  7790. #
  7791. # client_secret: oauth2 client secret to use. May be omitted if
  7792. # client_secret_jwt_key is given, or if client_auth_method is 'none'.
  7793. #
  7794. # client_secret_jwt_key: Alternative to client_secret: details of a key used
  7795. # to create a JSON Web Token to be used as an OAuth2 client secret. If
  7796. # given, must be a dictionary with the following properties:
  7797. #
  7798. # key: a pem-encoded signing key. Must be a suitable key for the
  7799. # algorithm specified. Required unless 'key_file' is given.
  7800. #
  7801. # key_file: the path to file containing a pem-encoded signing key file.
  7802. # Required unless 'key' is given.
  7803. #
  7804. # jwt_header: a dictionary giving properties to include in the JWT
  7805. # header. Must include the key 'alg', giving the algorithm used to
  7806. # sign the JWT, such as &quot;ES256&quot;, using the JWA identifiers in
  7807. # RFC7518.
  7808. #
  7809. # jwt_payload: an optional dictionary giving properties to include in
  7810. # the JWT payload. Normally this should include an 'iss' key.
  7811. #
  7812. # client_auth_method: auth method to use when exchanging the token. Valid
  7813. # values are 'client_secret_basic' (default), 'client_secret_post' and
  7814. # 'none'.
  7815. #
  7816. # scopes: list of scopes to request. This should normally include the &quot;openid&quot;
  7817. # scope. Defaults to [&quot;openid&quot;].
  7818. #
  7819. # authorization_endpoint: the oauth2 authorization endpoint. Required if
  7820. # provider discovery is disabled.
  7821. #
  7822. # token_endpoint: the oauth2 token endpoint. Required if provider discovery is
  7823. # disabled.
  7824. #
  7825. # userinfo_endpoint: the OIDC userinfo endpoint. Required if discovery is
  7826. # disabled and the 'openid' scope is not requested.
  7827. #
  7828. # jwks_uri: URI where to fetch the JWKS. Required if discovery is disabled and
  7829. # the 'openid' scope is used.
  7830. #
  7831. # skip_verification: set to 'true' to skip metadata verification. Use this if
  7832. # you are connecting to a provider that is not OpenID Connect compliant.
  7833. # Defaults to false. Avoid this in production.
  7834. #
  7835. # user_profile_method: Whether to fetch the user profile from the userinfo
  7836. # endpoint, or to rely on the data returned in the id_token from the
  7837. # token_endpoint.
  7838. #
  7839. # Valid values are: 'auto' or 'userinfo_endpoint'.
  7840. #
  7841. # Defaults to 'auto', which uses the userinfo endpoint if 'openid' is
  7842. # not included in 'scopes'. Set to 'userinfo_endpoint' to always use the
  7843. # userinfo endpoint.
  7844. #
  7845. # allow_existing_users: set to 'true' to allow a user logging in via OIDC to
  7846. # match a pre-existing account instead of failing. This could be used if
  7847. # switching from password logins to OIDC. Defaults to false.
  7848. #
  7849. # user_mapping_provider: Configuration for how attributes returned from a OIDC
  7850. # provider are mapped onto a matrix user. This setting has the following
  7851. # sub-properties:
  7852. #
  7853. # module: The class name of a custom mapping module. Default is
  7854. # 'synapse.handlers.oidc.JinjaOidcMappingProvider'.
  7855. # See https://matrix-org.github.io/synapse/latest/sso_mapping_providers.html#openid-mapping-providers
  7856. # for information on implementing a custom mapping provider.
  7857. #
  7858. # config: Configuration for the mapping provider module. This section will
  7859. # be passed as a Python dictionary to the user mapping provider
  7860. # module's `parse_config` method.
  7861. #
  7862. # For the default provider, the following settings are available:
  7863. #
  7864. # subject_claim: name of the claim containing a unique identifier
  7865. # for the user. Defaults to 'sub', which OpenID Connect
  7866. # compliant providers should provide.
  7867. #
  7868. # localpart_template: Jinja2 template for the localpart of the MXID.
  7869. # If this is not set, the user will be prompted to choose their
  7870. # own username (see the documentation for the
  7871. # 'sso_auth_account_details.html' template). This template can
  7872. # use the 'localpart_from_email' filter.
  7873. #
  7874. # confirm_localpart: Whether to prompt the user to validate (or
  7875. # change) the generated localpart (see the documentation for the
  7876. # 'sso_auth_account_details.html' template), instead of
  7877. # registering the account right away.
  7878. #
  7879. # display_name_template: Jinja2 template for the display name to set
  7880. # on first login. If unset, no displayname will be set.
  7881. #
  7882. # email_template: Jinja2 template for the email address of the user.
  7883. # If unset, no email address will be added to the account.
  7884. #
  7885. # extra_attributes: a map of Jinja2 templates for extra attributes
  7886. # to send back to the client during login.
  7887. # Note that these are non-standard and clients will ignore them
  7888. # without modifications.
  7889. #
  7890. # When rendering, the Jinja2 templates are given a 'user' variable,
  7891. # which is set to the claims returned by the UserInfo Endpoint and/or
  7892. # in the ID Token.
  7893. #
  7894. # It is possible to configure Synapse to only allow logins if certain attributes
  7895. # match particular values in the OIDC userinfo. The requirements can be listed under
  7896. # `attribute_requirements` as shown below. All of the listed attributes must
  7897. # match for the login to be permitted. Additional attributes can be added to
  7898. # userinfo by expanding the `scopes` section of the OIDC config to retrieve
  7899. # additional information from the OIDC provider.
  7900. #
  7901. # If the OIDC claim is a list, then the attribute must match any value in the list.
  7902. # Otherwise, it must exactly match the value of the claim. Using the example
  7903. # below, the `family_name` claim MUST be &quot;Stephensson&quot;, but the `groups`
  7904. # claim MUST contain &quot;admin&quot;.
  7905. #
  7906. # attribute_requirements:
  7907. # - attribute: family_name
  7908. # value: &quot;Stephensson&quot;
  7909. # - attribute: groups
  7910. # value: &quot;admin&quot;
  7911. #
  7912. # See https://matrix-org.github.io/synapse/latest/openid.html
  7913. # for information on how to configure these options.
  7914. #
  7915. # For backwards compatibility, it is also possible to configure a single OIDC
  7916. # provider via an 'oidc_config' setting. This is now deprecated and admins are
  7917. # advised to migrate to the 'oidc_providers' format. (When doing that migration,
  7918. # use 'oidc' for the idp_id to ensure that existing users continue to be
  7919. # recognised.)
  7920. #
  7921. oidc_providers:
  7922. # Generic example
  7923. #
  7924. #- idp_id: my_idp
  7925. # idp_name: &quot;My OpenID provider&quot;
  7926. # idp_icon: &quot;mxc://example.com/mediaid&quot;
  7927. # discover: false
  7928. # issuer: &quot;https://accounts.example.com/&quot;
  7929. # client_id: &quot;provided-by-your-issuer&quot;
  7930. # client_secret: &quot;provided-by-your-issuer&quot;
  7931. # client_auth_method: client_secret_post
  7932. # scopes: [&quot;openid&quot;, &quot;profile&quot;]
  7933. # authorization_endpoint: &quot;https://accounts.example.com/oauth2/auth&quot;
  7934. # token_endpoint: &quot;https://accounts.example.com/oauth2/token&quot;
  7935. # userinfo_endpoint: &quot;https://accounts.example.com/userinfo&quot;
  7936. # jwks_uri: &quot;https://accounts.example.com/.well-known/jwks.json&quot;
  7937. # skip_verification: true
  7938. # user_mapping_provider:
  7939. # config:
  7940. # subject_claim: &quot;id&quot;
  7941. # localpart_template: &quot;{{ user.login }}&quot;
  7942. # display_name_template: &quot;{{ user.name }}&quot;
  7943. # email_template: &quot;{{ user.email }}&quot;
  7944. # attribute_requirements:
  7945. # - attribute: userGroup
  7946. # value: &quot;synapseUsers&quot;
  7947. # Enable Central Authentication Service (CAS) for registration and login.
  7948. #
  7949. cas_config:
  7950. # Uncomment the following to enable authorization against a CAS server.
  7951. # Defaults to false.
  7952. #
  7953. #enabled: true
  7954. # The URL of the CAS authorization endpoint.
  7955. #
  7956. #server_url: &quot;https://cas-server.com&quot;
  7957. # The attribute of the CAS response to use as the display name.
  7958. #
  7959. # If unset, no displayname will be set.
  7960. #
  7961. #displayname_attribute: name
  7962. # It is possible to configure Synapse to only allow logins if CAS attributes
  7963. # match particular values. All of the keys in the mapping below must exist
  7964. # and the values must match the given value. Alternately if the given value
  7965. # is None then any value is allowed (the attribute just must exist).
  7966. # All of the listed attributes must match for the login to be permitted.
  7967. #
  7968. #required_attributes:
  7969. # userGroup: &quot;staff&quot;
  7970. # department: None
  7971. # Additional settings to use with single-sign on systems such as OpenID Connect,
  7972. # SAML2 and CAS.
  7973. #
  7974. # Server admins can configure custom templates for pages related to SSO. See
  7975. # https://matrix-org.github.io/synapse/latest/templates.html for more information.
  7976. #
  7977. sso:
  7978. # A list of client URLs which are whitelisted so that the user does not
  7979. # have to confirm giving access to their account to the URL. Any client
  7980. # whose URL starts with an entry in the following list will not be subject
  7981. # to an additional confirmation step after the SSO login is completed.
  7982. #
  7983. # WARNING: An entry such as &quot;https://my.client&quot; is insecure, because it
  7984. # will also match &quot;https://my.client.evil.site&quot;, exposing your users to
  7985. # phishing attacks from evil.site. To avoid this, include a slash after the
  7986. # hostname: &quot;https://my.client/&quot;.
  7987. #
  7988. # The login fallback page (used by clients that don't natively support the
  7989. # required login flows) is whitelisted in addition to any URLs in this list.
  7990. #
  7991. # By default, this list contains only the login fallback page.
  7992. #
  7993. #client_whitelist:
  7994. # - https://riot.im/develop
  7995. # - https://my.custom.client/
  7996. # Uncomment to keep a user's profile fields in sync with information from
  7997. # the identity provider. Currently only syncing the displayname is
  7998. # supported. Fields are checked on every SSO login, and are updated
  7999. # if necessary.
  8000. #
  8001. # Note that enabling this option will override user profile information,
  8002. # regardless of whether users have opted-out of syncing that
  8003. # information when first signing in. Defaults to false.
  8004. #
  8005. #update_profile_information: true
  8006. # JSON web token integration. The following settings can be used to make
  8007. # Synapse JSON web tokens for authentication, instead of its internal
  8008. # password database.
  8009. #
  8010. # Each JSON Web Token needs to contain a &quot;sub&quot; (subject) claim, which is
  8011. # used as the localpart of the mxid.
  8012. #
  8013. # Additionally, the expiration time (&quot;exp&quot;), not before time (&quot;nbf&quot;),
  8014. # and issued at (&quot;iat&quot;) claims are validated if present.
  8015. #
  8016. # Note that this is a non-standard login type and client support is
  8017. # expected to be non-existent.
  8018. #
  8019. # See https://matrix-org.github.io/synapse/latest/jwt.html.
  8020. #
  8021. #jwt_config:
  8022. # Uncomment the following to enable authorization using JSON web
  8023. # tokens. Defaults to false.
  8024. #
  8025. #enabled: true
  8026. # This is either the private shared secret or the public key used to
  8027. # decode the contents of the JSON web token.
  8028. #
  8029. # Required if 'enabled' is true.
  8030. #
  8031. #secret: &quot;provided-by-your-issuer&quot;
  8032. # The algorithm used to sign the JSON web token.
  8033. #
  8034. # Supported algorithms are listed at
  8035. # https://pyjwt.readthedocs.io/en/latest/algorithms.html
  8036. #
  8037. # Required if 'enabled' is true.
  8038. #
  8039. #algorithm: &quot;provided-by-your-issuer&quot;
  8040. # Name of the claim containing a unique identifier for the user.
  8041. #
  8042. # Optional, defaults to `sub`.
  8043. #
  8044. #subject_claim: &quot;sub&quot;
  8045. # The issuer to validate the &quot;iss&quot; claim against.
  8046. #
  8047. # Optional, if provided the &quot;iss&quot; claim will be required and
  8048. # validated for all JSON web tokens.
  8049. #
  8050. #issuer: &quot;provided-by-your-issuer&quot;
  8051. # A list of audiences to validate the &quot;aud&quot; claim against.
  8052. #
  8053. # Optional, if provided the &quot;aud&quot; claim will be required and
  8054. # validated for all JSON web tokens.
  8055. #
  8056. # Note that if the &quot;aud&quot; claim is included in a JSON web token then
  8057. # validation will fail without configuring audiences.
  8058. #
  8059. #audiences:
  8060. # - &quot;provided-by-your-issuer&quot;
  8061. password_config:
  8062. # Uncomment to disable password login.
  8063. # Set to `only_for_reauth` to permit reauthentication for users that
  8064. # have passwords and are already logged in.
  8065. #
  8066. #enabled: false
  8067. # Uncomment to disable authentication against the local password
  8068. # database. This is ignored if `enabled` is false, and is only useful
  8069. # if you have other password_providers.
  8070. #
  8071. #localdb_enabled: false
  8072. # Uncomment and change to a secret random string for extra security.
  8073. # DO NOT CHANGE THIS AFTER INITIAL SETUP!
  8074. #
  8075. #pepper: &quot;EVEN_MORE_SECRET&quot;
  8076. # Define and enforce a password policy. Each parameter is optional.
  8077. # This is an implementation of MSC2000.
  8078. #
  8079. policy:
  8080. # Whether to enforce the password policy.
  8081. # Defaults to 'false'.
  8082. #
  8083. #enabled: true
  8084. # Minimum accepted length for a password.
  8085. # Defaults to 0.
  8086. #
  8087. #minimum_length: 15
  8088. # Whether a password must contain at least one digit.
  8089. # Defaults to 'false'.
  8090. #
  8091. #require_digit: true
  8092. # Whether a password must contain at least one symbol.
  8093. # A symbol is any character that's not a number or a letter.
  8094. # Defaults to 'false'.
  8095. #
  8096. #require_symbol: true
  8097. # Whether a password must contain at least one lowercase letter.
  8098. # Defaults to 'false'.
  8099. #
  8100. #require_lowercase: true
  8101. # Whether a password must contain at least one uppercase letter.
  8102. # Defaults to 'false'.
  8103. #
  8104. #require_uppercase: true
  8105. ui_auth:
  8106. # The amount of time to allow a user-interactive authentication session
  8107. # to be active.
  8108. #
  8109. # This defaults to 0, meaning the user is queried for their credentials
  8110. # before every action, but this can be overridden to allow a single
  8111. # validation to be re-used. This weakens the protections afforded by
  8112. # the user-interactive authentication process, by allowing for multiple
  8113. # (and potentially different) operations to use the same validation session.
  8114. #
  8115. # This is ignored for potentially &quot;dangerous&quot; operations (including
  8116. # deactivating an account, modifying an account password, and
  8117. # adding a 3PID).
  8118. #
  8119. # Uncomment below to allow for credential validation to last for 15
  8120. # seconds.
  8121. #
  8122. #session_timeout: &quot;15s&quot;
  8123. # Configuration for sending emails from Synapse.
  8124. #
  8125. # Server admins can configure custom templates for email content. See
  8126. # https://matrix-org.github.io/synapse/latest/templates.html for more information.
  8127. #
  8128. email:
  8129. # The hostname of the outgoing SMTP server to use. Defaults to 'localhost'.
  8130. #
  8131. #smtp_host: mail.server
  8132. # The port on the mail server for outgoing SMTP. Defaults to 25.
  8133. #
  8134. #smtp_port: 587
  8135. # Username/password for authentication to the SMTP server. By default, no
  8136. # authentication is attempted.
  8137. #
  8138. #smtp_user: &quot;exampleusername&quot;
  8139. #smtp_pass: &quot;examplepassword&quot;
  8140. # Uncomment the following to require TLS transport security for SMTP.
  8141. # By default, Synapse will connect over plain text, and will then switch to
  8142. # TLS via STARTTLS *if the SMTP server supports it*. If this option is set,
  8143. # Synapse will refuse to connect unless the server supports STARTTLS.
  8144. #
  8145. #require_transport_security: true
  8146. # Uncomment the following to disable TLS for SMTP.
  8147. #
  8148. # By default, if the server supports TLS, it will be used, and the server
  8149. # must present a certificate that is valid for 'smtp_host'. If this option
  8150. # is set to false, TLS will not be used.
  8151. #
  8152. #enable_tls: false
  8153. # notif_from defines the &quot;From&quot; address to use when sending emails.
  8154. # It must be set if email sending is enabled.
  8155. #
  8156. # The placeholder '%(app)s' will be replaced by the application name,
  8157. # which is normally 'app_name' (below), but may be overridden by the
  8158. # Matrix client application.
  8159. #
  8160. # Note that the placeholder must be written '%(app)s', including the
  8161. # trailing 's'.
  8162. #
  8163. #notif_from: &quot;Your Friendly %(app)s homeserver &lt;noreply@example.com&gt;&quot;
  8164. # app_name defines the default value for '%(app)s' in notif_from and email
  8165. # subjects. It defaults to 'Matrix'.
  8166. #
  8167. #app_name: my_branded_matrix_server
  8168. # Uncomment the following to enable sending emails for messages that the user
  8169. # has missed. Disabled by default.
  8170. #
  8171. #enable_notifs: true
  8172. # Uncomment the following to disable automatic subscription to email
  8173. # notifications for new users. Enabled by default.
  8174. #
  8175. #notif_for_new_users: false
  8176. # Custom URL for client links within the email notifications. By default
  8177. # links will be based on &quot;https://matrix.to&quot;.
  8178. #
  8179. # (This setting used to be called riot_base_url; the old name is still
  8180. # supported for backwards-compatibility but is now deprecated.)
  8181. #
  8182. #client_base_url: &quot;http://localhost/riot&quot;
  8183. # Configure the time that a validation email will expire after sending.
  8184. # Defaults to 1h.
  8185. #
  8186. #validation_token_lifetime: 15m
  8187. # The web client location to direct users to during an invite. This is passed
  8188. # to the identity server as the org.matrix.web_client_location key. Defaults
  8189. # to unset, giving no guidance to the identity server.
  8190. #
  8191. #invite_client_location: https://app.element.io
  8192. # Subjects to use when sending emails from Synapse.
  8193. #
  8194. # The placeholder '%(app)s' will be replaced with the value of the 'app_name'
  8195. # setting above, or by a value dictated by the Matrix client application.
  8196. #
  8197. # If a subject isn't overridden in this configuration file, the value used as
  8198. # its example will be used.
  8199. #
  8200. #subjects:
  8201. # Subjects for notification emails.
  8202. #
  8203. # On top of the '%(app)s' placeholder, these can use the following
  8204. # placeholders:
  8205. #
  8206. # * '%(person)s', which will be replaced by the display name of the user(s)
  8207. # that sent the message(s), e.g. &quot;Alice and Bob&quot;.
  8208. # * '%(room)s', which will be replaced by the name of the room the
  8209. # message(s) have been sent to, e.g. &quot;My super room&quot;.
  8210. #
  8211. # See the example provided for each setting to see which placeholder can be
  8212. # used and how to use them.
  8213. #
  8214. # Subject to use to notify about one message from one or more user(s) in a
  8215. # room which has a name.
  8216. #message_from_person_in_room: &quot;[%(app)s] You have a message on %(app)s from %(person)s in the %(room)s room...&quot;
  8217. #
  8218. # Subject to use to notify about one message from one or more user(s) in a
  8219. # room which doesn't have a name.
  8220. #message_from_person: &quot;[%(app)s] You have a message on %(app)s from %(person)s...&quot;
  8221. #
  8222. # Subject to use to notify about multiple messages from one or more users in
  8223. # a room which doesn't have a name.
  8224. #messages_from_person: &quot;[%(app)s] You have messages on %(app)s from %(person)s...&quot;
  8225. #
  8226. # Subject to use to notify about multiple messages in a room which has a
  8227. # name.
  8228. #messages_in_room: &quot;[%(app)s] You have messages on %(app)s in the %(room)s room...&quot;
  8229. #
  8230. # Subject to use to notify about multiple messages in multiple rooms.
  8231. #messages_in_room_and_others: &quot;[%(app)s] You have messages on %(app)s in the %(room)s room and others...&quot;
  8232. #
  8233. # Subject to use to notify about multiple messages from multiple persons in
  8234. # multiple rooms. This is similar to the setting above except it's used when
  8235. # the room in which the notification was triggered has no name.
  8236. #messages_from_person_and_others: &quot;[%(app)s] You have messages on %(app)s from %(person)s and others...&quot;
  8237. #
  8238. # Subject to use to notify about an invite to a room which has a name.
  8239. #invite_from_person_to_room: &quot;[%(app)s] %(person)s has invited you to join the %(room)s room on %(app)s...&quot;
  8240. #
  8241. # Subject to use to notify about an invite to a room which doesn't have a
  8242. # name.
  8243. #invite_from_person: &quot;[%(app)s] %(person)s has invited you to chat on %(app)s...&quot;
  8244. # Subject for emails related to account administration.
  8245. #
  8246. # On top of the '%(app)s' placeholder, these one can use the
  8247. # '%(server_name)s' placeholder, which will be replaced by the value of the
  8248. # 'server_name' setting in your Synapse configuration.
  8249. #
  8250. # Subject to use when sending a password reset email.
  8251. #password_reset: &quot;[%(server_name)s] Password reset&quot;
  8252. #
  8253. # Subject to use when sending a verification email to assert an address's
  8254. # ownership.
  8255. #email_validation: &quot;[%(server_name)s] Validate your email&quot;
  8256. ## Push ##
  8257. push:
  8258. # Clients requesting push notifications can either have the body of
  8259. # the message sent in the notification poke along with other details
  8260. # like the sender, or just the event ID and room ID (`event_id_only`).
  8261. # If clients choose the former, this option controls whether the
  8262. # notification request includes the content of the event (other details
  8263. # like the sender are still included). For `event_id_only` push, it
  8264. # has no effect.
  8265. #
  8266. # For modern android devices the notification content will still appear
  8267. # because it is loaded by the app. iPhone, however will send a
  8268. # notification saying only that a message arrived and who it came from.
  8269. #
  8270. # The default value is &quot;true&quot; to include message details. Uncomment to only
  8271. # include the event ID and room ID in push notification payloads.
  8272. #
  8273. #include_content: false
  8274. # When a push notification is received, an unread count is also sent.
  8275. # This number can either be calculated as the number of unread messages
  8276. # for the user, or the number of *rooms* the user has unread messages in.
  8277. #
  8278. # The default value is &quot;true&quot;, meaning push clients will see the number of
  8279. # rooms with unread messages in them. Uncomment to instead send the number
  8280. # of unread messages.
  8281. #
  8282. #group_unread_count_by_room: false
  8283. ## Rooms ##
  8284. # Controls whether locally-created rooms should be end-to-end encrypted by
  8285. # default.
  8286. #
  8287. # Possible options are &quot;all&quot;, &quot;invite&quot;, and &quot;off&quot;. They are defined as:
  8288. #
  8289. # * &quot;all&quot;: any locally-created room
  8290. # * &quot;invite&quot;: any room created with the &quot;private_chat&quot; or &quot;trusted_private_chat&quot;
  8291. # room creation presets
  8292. # * &quot;off&quot;: this option will take no effect
  8293. #
  8294. # The default value is &quot;off&quot;.
  8295. #
  8296. # Note that this option will only affect rooms created after it is set. It
  8297. # will also not affect rooms created by other servers.
  8298. #
  8299. #encryption_enabled_by_default_for_room_type: invite
  8300. # Override the default power levels for rooms created on this server, per
  8301. # room creation preset.
  8302. #
  8303. # The appropriate dictionary for the room preset will be applied on top
  8304. # of the existing power levels content.
  8305. #
  8306. # Useful if you know that your users need special permissions in rooms
  8307. # that they create (e.g. to send particular types of state events without
  8308. # needing an elevated power level). This takes the same shape as the
  8309. # `power_level_content_override` parameter in the /createRoom API, but
  8310. # is applied before that parameter.
  8311. #
  8312. # Valid keys are some or all of `private_chat`, `trusted_private_chat`
  8313. # and `public_chat`. Inside each of those should be any of the
  8314. # properties allowed in `power_level_content_override` in the
  8315. # /createRoom API. If any property is missing, its default value will
  8316. # continue to be used. If any property is present, it will overwrite
  8317. # the existing default completely (so if the `events` property exists,
  8318. # the default event power levels will be ignored).
  8319. #
  8320. #default_power_level_content_override:
  8321. # private_chat:
  8322. # &quot;events&quot;:
  8323. # &quot;com.example.myeventtype&quot; : 0
  8324. # &quot;m.room.avatar&quot;: 50
  8325. # &quot;m.room.canonical_alias&quot;: 50
  8326. # &quot;m.room.encryption&quot;: 100
  8327. # &quot;m.room.history_visibility&quot;: 100
  8328. # &quot;m.room.name&quot;: 50
  8329. # &quot;m.room.power_levels&quot;: 100
  8330. # &quot;m.room.server_acl&quot;: 100
  8331. # &quot;m.room.tombstone&quot;: 100
  8332. # &quot;events_default&quot;: 1
  8333. # Uncomment to allow non-server-admin users to create groups on this server
  8334. #
  8335. #enable_group_creation: true
  8336. # If enabled, non server admins can only create groups with local parts
  8337. # starting with this prefix
  8338. #
  8339. #group_creation_prefix: &quot;unofficial_&quot;
  8340. # User Directory configuration
  8341. #
  8342. user_directory:
  8343. # Defines whether users can search the user directory. If false then
  8344. # empty responses are returned to all queries. Defaults to true.
  8345. #
  8346. # Uncomment to disable the user directory.
  8347. #
  8348. #enabled: false
  8349. # Defines whether to search all users visible to your HS when searching
  8350. # the user directory. If false, search results will only contain users
  8351. # visible in public rooms and users sharing a room with the requester.
  8352. # Defaults to false.
  8353. #
  8354. # NB. If you set this to true, and the last time the user_directory search
  8355. # indexes were (re)built was before Synapse 1.44, you'll have to
  8356. # rebuild the indexes in order to search through all known users.
  8357. # These indexes are built the first time Synapse starts; admins can
  8358. # manually trigger a rebuild via API following the instructions at
  8359. # https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/background_updates.html#run
  8360. #
  8361. # Uncomment to return search results containing all known users, even if that
  8362. # user does not share a room with the requester.
  8363. #
  8364. #search_all_users: true
  8365. # Defines whether to prefer local users in search query results.
  8366. # If True, local users are more likely to appear above remote users
  8367. # when searching the user directory. Defaults to false.
  8368. #
  8369. # Uncomment to prefer local over remote users in user directory search
  8370. # results.
  8371. #
  8372. #prefer_local_users: true
  8373. # User Consent configuration
  8374. #
  8375. # for detailed instructions, see
  8376. # https://matrix-org.github.io/synapse/latest/consent_tracking.html
  8377. #
  8378. # Parts of this section are required if enabling the 'consent' resource under
  8379. # 'listeners', in particular 'template_dir' and 'version'.
  8380. #
  8381. # 'template_dir' gives the location of the templates for the HTML forms.
  8382. # This directory should contain one subdirectory per language (eg, 'en', 'fr'),
  8383. # and each language directory should contain the policy document (named as
  8384. # '&lt;version&gt;.html') and a success page (success.html).
  8385. #
  8386. # 'version' specifies the 'current' version of the policy document. It defines
  8387. # the version to be served by the consent resource if there is no 'v'
  8388. # parameter.
  8389. #
  8390. # 'server_notice_content', if enabled, will send a user a &quot;Server Notice&quot;
  8391. # asking them to consent to the privacy policy. The 'server_notices' section
  8392. # must also be configured for this to work. Notices will *not* be sent to
  8393. # guest users unless 'send_server_notice_to_guests' is set to true.
  8394. #
  8395. # 'block_events_error', if set, will block any attempts to send events
  8396. # until the user consents to the privacy policy. The value of the setting is
  8397. # used as the text of the error.
  8398. #
  8399. # 'require_at_registration', if enabled, will add a step to the registration
  8400. # process, similar to how captcha works. Users will be required to accept the
  8401. # policy before their account is created.
  8402. #
  8403. # 'policy_name' is the display name of the policy users will see when registering
  8404. # for an account. Has no effect unless `require_at_registration` is enabled.
  8405. # Defaults to &quot;Privacy Policy&quot;.
  8406. #
  8407. #user_consent:
  8408. # template_dir: res/templates/privacy
  8409. # version: 1.0
  8410. # server_notice_content:
  8411. # msgtype: m.text
  8412. # body: &gt;-
  8413. # To continue using this homeserver you must review and agree to the
  8414. # terms and conditions at %(consent_uri)s
  8415. # send_server_notice_to_guests: true
  8416. # block_events_error: &gt;-
  8417. # To continue using this homeserver you must review and agree to the
  8418. # terms and conditions at %(consent_uri)s
  8419. # require_at_registration: false
  8420. # policy_name: Privacy Policy
  8421. #
  8422. # Settings for local room and user statistics collection. See
  8423. # https://matrix-org.github.io/synapse/latest/room_and_user_statistics.html.
  8424. #
  8425. stats:
  8426. # Uncomment the following to disable room and user statistics. Note that doing
  8427. # so may cause certain features (such as the room directory) not to work
  8428. # correctly.
  8429. #
  8430. #enabled: false
  8431. # Server Notices room configuration
  8432. #
  8433. # Uncomment this section to enable a room which can be used to send notices
  8434. # from the server to users. It is a special room which cannot be left; notices
  8435. # come from a special &quot;notices&quot; user id.
  8436. #
  8437. # If you uncomment this section, you *must* define the system_mxid_localpart
  8438. # setting, which defines the id of the user which will be used to send the
  8439. # notices.
  8440. #
  8441. # It's also possible to override the room name, the display name of the
  8442. # &quot;notices&quot; user, and the avatar for the user.
  8443. #
  8444. #server_notices:
  8445. # system_mxid_localpart: notices
  8446. # system_mxid_display_name: &quot;Server Notices&quot;
  8447. # system_mxid_avatar_url: &quot;mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ&quot;
  8448. # room_name: &quot;Server Notices&quot;
  8449. # Uncomment to disable searching the public room list. When disabled
  8450. # blocks searching local and remote room lists for local and remote
  8451. # users by always returning an empty list for all queries.
  8452. #
  8453. #enable_room_list_search: false
  8454. # The `alias_creation` option controls who's allowed to create aliases
  8455. # on this server.
  8456. #
  8457. # The format of this option is a list of rules that contain globs that
  8458. # match against user_id, room_id and the new alias (fully qualified with
  8459. # server name). The action in the first rule that matches is taken,
  8460. # which can currently either be &quot;allow&quot; or &quot;deny&quot;.
  8461. #
  8462. # Missing user_id/room_id/alias fields default to &quot;*&quot;.
  8463. #
  8464. # If no rules match the request is denied. An empty list means no one
  8465. # can create aliases.
  8466. #
  8467. # Options for the rules include:
  8468. #
  8469. # user_id: Matches against the creator of the alias
  8470. # alias: Matches against the alias being created
  8471. # room_id: Matches against the room ID the alias is being pointed at
  8472. # action: Whether to &quot;allow&quot; or &quot;deny&quot; the request if the rule matches
  8473. #
  8474. # The default is:
  8475. #
  8476. #alias_creation_rules:
  8477. # - user_id: &quot;*&quot;
  8478. # alias: &quot;*&quot;
  8479. # room_id: &quot;*&quot;
  8480. # action: allow
  8481. # The `room_list_publication_rules` option controls who can publish and
  8482. # which rooms can be published in the public room list.
  8483. #
  8484. # The format of this option is the same as that for
  8485. # `alias_creation_rules`.
  8486. #
  8487. # If the room has one or more aliases associated with it, only one of
  8488. # the aliases needs to match the alias rule. If there are no aliases
  8489. # then only rules with `alias: *` match.
  8490. #
  8491. # If no rules match the request is denied. An empty list means no one
  8492. # can publish rooms.
  8493. #
  8494. # Options for the rules include:
  8495. #
  8496. # user_id: Matches against the creator of the alias
  8497. # room_id: Matches against the room ID being published
  8498. # alias: Matches against any current local or canonical aliases
  8499. # associated with the room
  8500. # action: Whether to &quot;allow&quot; or &quot;deny&quot; the request if the rule matches
  8501. #
  8502. # The default is:
  8503. #
  8504. #room_list_publication_rules:
  8505. # - user_id: &quot;*&quot;
  8506. # alias: &quot;*&quot;
  8507. # room_id: &quot;*&quot;
  8508. # action: allow
  8509. ## Opentracing ##
  8510. # These settings enable opentracing, which implements distributed tracing.
  8511. # This allows you to observe the causal chains of events across servers
  8512. # including requests, key lookups etc., across any server running
  8513. # synapse or any other other services which supports opentracing
  8514. # (specifically those implemented with Jaeger).
  8515. #
  8516. opentracing:
  8517. # tracing is disabled by default. Uncomment the following line to enable it.
  8518. #
  8519. #enabled: true
  8520. # The list of homeservers we wish to send and receive span contexts and span baggage.
  8521. # See https://matrix-org.github.io/synapse/latest/opentracing.html.
  8522. #
  8523. # This is a list of regexes which are matched against the server_name of the
  8524. # homeserver.
  8525. #
  8526. # By default, it is empty, so no servers are matched.
  8527. #
  8528. #homeserver_whitelist:
  8529. # - &quot;.*&quot;
  8530. # A list of the matrix IDs of users whose requests will always be traced,
  8531. # even if the tracing system would otherwise drop the traces due to
  8532. # probabilistic sampling.
  8533. #
  8534. # By default, the list is empty.
  8535. #
  8536. #force_tracing_for_users:
  8537. # - &quot;@user1:server_name&quot;
  8538. # - &quot;@user2:server_name&quot;
  8539. # Jaeger can be configured to sample traces at different rates.
  8540. # All configuration options provided by Jaeger can be set here.
  8541. # Jaeger's configuration is mostly related to trace sampling which
  8542. # is documented here:
  8543. # https://www.jaegertracing.io/docs/latest/sampling/.
  8544. #
  8545. #jaeger_config:
  8546. # sampler:
  8547. # type: const
  8548. # param: 1
  8549. # logging:
  8550. # false
  8551. ## Workers ##
  8552. # Disables sending of outbound federation transactions on the main process.
  8553. # Uncomment if using a federation sender worker.
  8554. #
  8555. #send_federation: false
  8556. # It is possible to run multiple federation sender workers, in which case the
  8557. # work is balanced across them.
  8558. #
  8559. # This configuration must be shared between all federation sender workers, and if
  8560. # changed all federation sender workers must be stopped at the same time and then
  8561. # started, to ensure that all instances are running with the same config (otherwise
  8562. # events may be dropped).
  8563. #
  8564. #federation_sender_instances:
  8565. # - federation_sender1
  8566. # When using workers this should be a map from `worker_name` to the
  8567. # HTTP replication listener of the worker, if configured.
  8568. #
  8569. #instance_map:
  8570. # worker1:
  8571. # host: localhost
  8572. # port: 8034
  8573. # Experimental: When using workers you can define which workers should
  8574. # handle event persistence and typing notifications. Any worker
  8575. # specified here must also be in the `instance_map`.
  8576. #
  8577. #stream_writers:
  8578. # events: worker1
  8579. # typing: worker1
  8580. # The worker that is used to run background tasks (e.g. cleaning up expired
  8581. # data). If not provided this defaults to the main process.
  8582. #
  8583. #run_background_tasks_on: worker1
  8584. # A shared secret used by the replication APIs to authenticate HTTP requests
  8585. # from workers.
  8586. #
  8587. # By default this is unused and traffic is not authenticated.
  8588. #
  8589. #worker_replication_secret: &quot;&quot;
  8590. # Configuration for Redis when using workers. This *must* be enabled when
  8591. # using workers (unless using old style direct TCP configuration).
  8592. #
  8593. redis:
  8594. # Uncomment the below to enable Redis support.
  8595. #
  8596. #enabled: true
  8597. # Optional host and port to use to connect to redis. Defaults to
  8598. # localhost and 6379
  8599. #
  8600. #host: localhost
  8601. #port: 6379
  8602. # Optional password if configured on the Redis instance
  8603. #
  8604. #password: &lt;secret_password&gt;
  8605. ## Background Updates ##
  8606. # Background updates are database updates that are run in the background in batches.
  8607. # The duration, minimum batch size, default batch size, whether to sleep between batches and if so, how long to
  8608. # sleep can all be configured. This is helpful to speed up or slow down the updates.
  8609. #
  8610. background_updates:
  8611. # How long in milliseconds to run a batch of background updates for. Defaults to 100. Uncomment and set
  8612. # a time to change the default.
  8613. #
  8614. #background_update_duration_ms: 500
  8615. # Whether to sleep between updates. Defaults to True. Uncomment to change the default.
  8616. #
  8617. #sleep_enabled: false
  8618. # If sleeping between updates, how long in milliseconds to sleep for. Defaults to 1000. Uncomment
  8619. # and set a duration to change the default.
  8620. #
  8621. #sleep_duration_ms: 300
  8622. # Minimum size a batch of background updates can be. Must be greater than 0. Defaults to 1. Uncomment and
  8623. # set a size to change the default.
  8624. #
  8625. #min_batch_size: 10
  8626. # The batch size to use for the first iteration of a new background update. The default is 100.
  8627. # Uncomment and set a size to change the default.
  8628. #
  8629. #default_batch_size: 50
  8630. </code></pre>
  8631. <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>
  8632. <p>Below is a sample logging configuration file. This file can be tweaked to control how your
  8633. homeserver will output logs. A restart of the server is generally required to apply any
  8634. changes made to this file. The value of the <code>log_config</code> option in your homeserver
  8635. config should be the path to this file.</p>
  8636. <p>Note that a default logging configuration (shown below) is created automatically alongside
  8637. the homeserver config when following the <a href="usage/configuration/../../setup/installation.html">installation instructions</a>.
  8638. It should be named <code>&lt;SERVERNAME&gt;.log.config</code> by default.</p>
  8639. <pre><code class="language-yaml"># Log configuration for Synapse.
  8640. #
  8641. # This is a YAML file containing a standard Python logging configuration
  8642. # dictionary. See [1] for details on the valid settings.
  8643. #
  8644. # Synapse also supports structured logging for machine readable logs which can
  8645. # be ingested by ELK stacks. See [2] for details.
  8646. #
  8647. # [1]: https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
  8648. # [2]: https://matrix-org.github.io/synapse/latest/structured_logging.html
  8649. version: 1
  8650. formatters:
  8651. precise:
  8652. format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'
  8653. handlers:
  8654. file:
  8655. class: logging.handlers.TimedRotatingFileHandler
  8656. formatter: precise
  8657. filename: /var/log/matrix-synapse/homeserver.log
  8658. when: midnight
  8659. backupCount: 3 # Does not include the current log file.
  8660. encoding: utf8
  8661. # Default to buffering writes to log file for efficiency.
  8662. # WARNING/ERROR logs will still be flushed immediately, but there will be a
  8663. # delay (of up to `period` seconds, or until the buffer is full with
  8664. # `capacity` messages) before INFO/DEBUG logs get written.
  8665. buffer:
  8666. class: synapse.logging.handlers.PeriodicallyFlushingMemoryHandler
  8667. target: file
  8668. # The capacity is the maximum number of log lines that are buffered
  8669. # before being written to disk. Increasing this will lead to better
  8670. # performance, at the expensive of it taking longer for log lines to
  8671. # be written to disk.
  8672. # This parameter is required.
  8673. capacity: 10
  8674. # Logs with a level at or above the flush level will cause the buffer to
  8675. # be flushed immediately.
  8676. # Default value: 40 (ERROR)
  8677. # Other values: 50 (CRITICAL), 30 (WARNING), 20 (INFO), 10 (DEBUG)
  8678. flushLevel: 30 # Flush immediately for WARNING logs and higher
  8679. # The period of time, in seconds, between forced flushes.
  8680. # Messages will not be delayed for longer than this time.
  8681. # Default value: 5 seconds
  8682. period: 5
  8683. # A handler that writes logs to stderr. Unused by default, but can be used
  8684. # instead of &quot;buffer&quot; and &quot;file&quot; in the logger handlers.
  8685. console:
  8686. class: logging.StreamHandler
  8687. formatter: precise
  8688. loggers:
  8689. synapse.storage.SQL:
  8690. # beware: increasing this to DEBUG will make synapse log sensitive
  8691. # information such as access tokens.
  8692. level: INFO
  8693. root:
  8694. level: INFO
  8695. # Write logs to the `buffer` handler, which will buffer them together in memory,
  8696. # then write them to a file.
  8697. #
  8698. # Replace &quot;buffer&quot; with &quot;console&quot; to log to stderr instead. (Note that you'll
  8699. # also need to update the configuration for the `twisted` logger above, in
  8700. # this case.)
  8701. #
  8702. handlers: [buffer]
  8703. disable_existing_loggers: false
  8704. </code></pre>
  8705. <div style="break-before: page; page-break-before: always;"></div><h1 id="structured-logging"><a class="header" href="#structured-logging">Structured Logging</a></h1>
  8706. <p>A structured logging system can be useful when your logs are destined for a
  8707. machine to parse and process. By maintaining its machine-readable characteristics,
  8708. it enables more efficient searching and aggregations when consumed by software
  8709. such as the &quot;ELK stack&quot;.</p>
  8710. <p>Synapse's structured logging system is configured via the file that Synapse's
  8711. <code>log_config</code> config option points to. The file should include a formatter which
  8712. uses the <code>synapse.logging.TerseJsonFormatter</code> class included with Synapse and a
  8713. handler which uses the above formatter.</p>
  8714. <p>There is also a <code>synapse.logging.JsonFormatter</code> option which does not include
  8715. a timestamp in the resulting JSON. This is useful if the log ingester adds its
  8716. own timestamp.</p>
  8717. <p>A structured logging configuration looks similar to the following:</p>
  8718. <pre><code class="language-yaml">version: 1
  8719. formatters:
  8720. structured:
  8721. class: synapse.logging.TerseJsonFormatter
  8722. handlers:
  8723. file:
  8724. class: logging.handlers.TimedRotatingFileHandler
  8725. formatter: structured
  8726. filename: /path/to/my/logs/homeserver.log
  8727. when: midnight
  8728. backupCount: 3 # Does not include the current log file.
  8729. encoding: utf8
  8730. loggers:
  8731. synapse:
  8732. level: INFO
  8733. handlers: [remote]
  8734. synapse.storage.SQL:
  8735. level: WARNING
  8736. </code></pre>
  8737. <p>The above logging config will set Synapse as 'INFO' logging level by default,
  8738. with the SQL layer at 'WARNING', and will log to a file, stored as JSON.</p>
  8739. <p>It is also possible to figure Synapse to log to a remote endpoint by using the
  8740. <code>synapse.logging.RemoteHandler</code> class included with Synapse. It takes the
  8741. following arguments:</p>
  8742. <ul>
  8743. <li><code>host</code>: Hostname or IP address of the log aggregator.</li>
  8744. <li><code>port</code>: Numerical port to contact on the host.</li>
  8745. <li><code>maximum_buffer</code>: (Optional, defaults to 1000) The maximum buffer size to allow.</li>
  8746. </ul>
  8747. <p>A remote structured logging configuration looks similar to the following:</p>
  8748. <pre><code class="language-yaml">version: 1
  8749. formatters:
  8750. structured:
  8751. class: synapse.logging.TerseJsonFormatter
  8752. handlers:
  8753. remote:
  8754. class: synapse.logging.RemoteHandler
  8755. formatter: structured
  8756. host: 10.1.2.3
  8757. port: 9999
  8758. loggers:
  8759. synapse:
  8760. level: INFO
  8761. handlers: [remote]
  8762. synapse.storage.SQL:
  8763. level: WARNING
  8764. </code></pre>
  8765. <p>The above logging config will set Synapse as 'INFO' logging level by default,
  8766. with the SQL layer at 'WARNING', and will log JSON formatted messages to a
  8767. remote endpoint at 10.1.2.3:9999.</p>
  8768. <div style="break-before: page; page-break-before: always;"></div><h1 id="templates"><a class="header" href="#templates">Templates</a></h1>
  8769. <p>Synapse uses parametrised templates to generate the content of emails it sends and
  8770. webpages it shows to users.</p>
  8771. <p>By default, Synapse will use the templates listed <a href="https://github.com/matrix-org/synapse/tree/master/synapse/res/templates">here</a>.
  8772. Server admins can configure an additional directory for Synapse to look for templates
  8773. in, allowing them to specify custom templates:</p>
  8774. <pre><code class="language-yaml">templates:
  8775. custom_templates_directory: /path/to/custom/templates/
  8776. </code></pre>
  8777. <p>If this setting is not set, or the files named below are not found within the directory,
  8778. default templates from within the Synapse package will be used.</p>
  8779. <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>.
  8780. Templates rendered by Jinja 2 can also access two functions on top of the functions
  8781. already available as part of Jinja 2:</p>
  8782. <pre><code class="language-python">format_ts(value: int, format: str) -&gt; str
  8783. </code></pre>
  8784. <p>Formats a timestamp in milliseconds.</p>
  8785. <p>Example: <code>reason.last_sent_ts|format_ts(&quot;%c&quot;)</code></p>
  8786. <pre><code class="language-python">mxc_to_http(value: str, width: int, height: int, resize_method: str = &quot;crop&quot;) -&gt; str
  8787. </code></pre>
  8788. <p>Turns a <code>mxc://</code> URL for media content into an HTTP(S) one using the homeserver's
  8789. <code>public_baseurl</code> configuration setting as the URL's base.</p>
  8790. <p>Example: <code>message.sender_avatar_url|mxc_to_http(32,32)</code></p>
  8791. <pre><code class="language-python">localpart_from_email(address: str) -&gt; str
  8792. </code></pre>
  8793. <p>Returns the local part of an email address (e.g. <code>alice</code> in <code>alice@example.com</code>).</p>
  8794. <p>Example: <code>user.email_address|localpart_from_email</code></p>
  8795. <h2 id="email-templates"><a class="header" href="#email-templates">Email templates</a></h2>
  8796. <p>Below are the templates Synapse will look for when generating the content of an email:</p>
  8797. <ul>
  8798. <li><code>notif_mail.html</code> and <code>notif_mail.txt</code>: The contents of email notifications of missed
  8799. events.
  8800. When rendering, this template is given the following variables:
  8801. <ul>
  8802. <li><code>user_display_name</code>: the display name for the user receiving the notification</li>
  8803. <li><code>unsubscribe_link</code>: the link users can click to unsubscribe from email notifications</li>
  8804. <li><code>summary_text</code>: a summary of the notification(s). The text used can be customised
  8805. by configuring the various settings in the <code>email.subjects</code> section of the
  8806. configuration file.</li>
  8807. <li><code>rooms</code>: a list of rooms containing events to include in the email. Each element is
  8808. an object with the following attributes:
  8809. <ul>
  8810. <li><code>title</code>: a human-readable name for the room</li>
  8811. <li><code>hash</code>: a hash of the ID of the room</li>
  8812. <li><code>invite</code>: a boolean, which is <code>True</code> if the room is an invite the user hasn't
  8813. accepted yet, <code>False</code> otherwise</li>
  8814. <li><code>notifs</code>: a list of events, or an empty list if <code>invite</code> is <code>True</code>. Each element
  8815. is an object with the following attributes:
  8816. <ul>
  8817. <li><code>link</code>: a <code>matrix.to</code> link to the event</li>
  8818. <li><code>ts</code>: the time in milliseconds at which the event was received</li>
  8819. <li><code>messages</code>: a list of messages containing one message before the event, the
  8820. message in the event, and one message after the event. Each element is an
  8821. object with the following attributes:
  8822. <ul>
  8823. <li><code>event_type</code>: the type of the event</li>
  8824. <li><code>is_historical</code>: a boolean, which is <code>False</code> if the message is the one
  8825. that triggered the notification, <code>True</code> otherwise</li>
  8826. <li><code>id</code>: the ID of the event</li>
  8827. <li><code>ts</code>: the time in milliseconds at which the event was sent</li>
  8828. <li><code>sender_name</code>: the display name for the event's sender</li>
  8829. <li><code>sender_avatar_url</code>: the avatar URL (as a <code>mxc://</code> URL) for the event's
  8830. sender</li>
  8831. <li><code>sender_hash</code>: a hash of the user ID of the sender</li>
  8832. <li><code>msgtype</code>: the type of the message</li>
  8833. <li><code>body_text_html</code>: html representation of the message</li>
  8834. <li><code>body_text_plain</code>: plaintext representation of the message</li>
  8835. <li><code>image_url</code>: mxc url of an image, when &quot;msgtype&quot; is &quot;m.image&quot;</li>
  8836. </ul>
  8837. </li>
  8838. </ul>
  8839. </li>
  8840. <li><code>link</code>: a <code>matrix.to</code> link to the room</li>
  8841. <li><code>avator_url</code>: url to the room's avator</li>
  8842. </ul>
  8843. </li>
  8844. <li><code>reason</code>: information on the event that triggered the email to be sent. It's an
  8845. object with the following attributes:
  8846. <ul>
  8847. <li><code>room_id</code>: the ID of the room the event was sent in</li>
  8848. <li><code>room_name</code>: a human-readable name for the room the event was sent in</li>
  8849. <li><code>now</code>: the current time in milliseconds</li>
  8850. <li><code>received_at</code>: the time in milliseconds at which the event was received</li>
  8851. <li><code>delay_before_mail_ms</code>: the amount of time in milliseconds Synapse always waits
  8852. before ever emailing about a notification (to give the user a chance to respond
  8853. to other push or notice the window)</li>
  8854. <li><code>last_sent_ts</code>: the time in milliseconds at which a notification was last sent
  8855. for an event in this room</li>
  8856. <li><code>throttle_ms</code>: the minimum amount of time in milliseconds between two
  8857. notifications can be sent for this room</li>
  8858. </ul>
  8859. </li>
  8860. </ul>
  8861. </li>
  8862. <li><code>password_reset.html</code> and <code>password_reset.txt</code>: The contents of password reset emails
  8863. sent by the homeserver.
  8864. When rendering, these templates are given a <code>link</code> variable which contains the link the
  8865. user must click in order to reset their password.</li>
  8866. <li><code>registration.html</code> and <code>registration.txt</code>: The contents of address verification emails
  8867. sent during registration.
  8868. When rendering, these templates are given a <code>link</code> variable which contains the link the
  8869. user must click in order to validate their email address.</li>
  8870. <li><code>add_threepid.html</code> and <code>add_threepid.txt</code>: The contents of address verification emails
  8871. sent when an address is added to a Matrix account.
  8872. When rendering, these templates are given a <code>link</code> variable which contains the link the
  8873. user must click in order to validate their email address.</li>
  8874. </ul>
  8875. <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>
  8876. <p>Below are the templates Synapse will look for when generating pages related to
  8877. registration and password reset:</p>
  8878. <ul>
  8879. <li><code>password_reset_confirmation.html</code>: An HTML page that a user will see when they follow
  8880. the link in the password reset email. The user will be asked to confirm the action
  8881. before their password is reset.
  8882. When rendering, this template is given the following variables:
  8883. <ul>
  8884. <li><code>sid</code>: the session ID for the password reset</li>
  8885. <li><code>token</code>: the token for the password reset</li>
  8886. <li><code>client_secret</code>: the client secret for the password reset</li>
  8887. </ul>
  8888. </li>
  8889. <li><code>password_reset_success.html</code> and <code>password_reset_failure.html</code>: HTML pages for success
  8890. and failure that a user will see when they confirm the password reset flow using the
  8891. page above.
  8892. When rendering, <code>password_reset_success.html</code> is given no variable, and
  8893. <code>password_reset_failure.html</code> is given a <code>failure_reason</code>, which contains the reason
  8894. for the password reset failure. </li>
  8895. <li><code>registration_success.html</code> and <code>registration_failure.html</code>: HTML pages for success and
  8896. failure that a user will see when they follow the link in an address verification email
  8897. sent during registration.
  8898. When rendering, <code>registration_success.html</code> is given no variable, and
  8899. <code>registration_failure.html</code> is given a <code>failure_reason</code>, which contains the reason
  8900. for the registration failure.</li>
  8901. <li><code>add_threepid_success.html</code> and <code>add_threepid_failure.html</code>: HTML pages for success and
  8902. failure that a user will see when they follow the link in an address verification email
  8903. sent when an address is added to a Matrix account.
  8904. When rendering, <code>add_threepid_success.html</code> is given no variable, and
  8905. <code>add_threepid_failure.html</code> is given a <code>failure_reason</code>, which contains the reason
  8906. for the registration failure.</li>
  8907. </ul>
  8908. <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>
  8909. <p>Below are the templates Synapse will look for when generating pages related to SSO:</p>
  8910. <ul>
  8911. <li><code>sso_login_idp_picker.html</code>: HTML page to prompt the user to choose an
  8912. Identity Provider during login.
  8913. This is only used if multiple SSO Identity Providers are configured.
  8914. When rendering, this template is given the following variables:
  8915. <ul>
  8916. <li><code>redirect_url</code>: the URL that the user will be redirected to after
  8917. login.</li>
  8918. <li><code>server_name</code>: the homeserver's name.</li>
  8919. <li><code>providers</code>: a list of available Identity Providers. Each element is
  8920. an object with the following attributes:
  8921. <ul>
  8922. <li><code>idp_id</code>: unique identifier for the IdP</li>
  8923. <li><code>idp_name</code>: user-facing name for the IdP</li>
  8924. <li><code>idp_icon</code>: if specified in the IdP config, an MXC URI for an icon
  8925. for the IdP</li>
  8926. <li><code>idp_brand</code>: if specified in the IdP config, a textual identifier
  8927. for the brand of the IdP
  8928. The rendered HTML page should contain a form which submits its results
  8929. back as a GET request, with the following query parameters:</li>
  8930. </ul>
  8931. </li>
  8932. <li><code>redirectUrl</code>: the client redirect URI (ie, the <code>redirect_url</code> passed
  8933. to the template)</li>
  8934. <li><code>idp</code>: the 'idp_id' of the chosen IDP.</li>
  8935. </ul>
  8936. </li>
  8937. <li><code>sso_auth_account_details.html</code>: HTML page to prompt new users to enter a
  8938. userid and confirm other details. This is only shown if the
  8939. SSO implementation (with any <code>user_mapping_provider</code>) does not return
  8940. a localpart.
  8941. When rendering, this template is given the following variables:
  8942. <ul>
  8943. <li><code>server_name</code>: the homeserver's name.</li>
  8944. <li><code>idp</code>: details of the SSO Identity Provider that the user logged in
  8945. with: an object with the following attributes:
  8946. <ul>
  8947. <li><code>idp_id</code>: unique identifier for the IdP</li>
  8948. <li><code>idp_name</code>: user-facing name for the IdP</li>
  8949. <li><code>idp_icon</code>: if specified in the IdP config, an MXC URI for an icon
  8950. for the IdP</li>
  8951. <li><code>idp_brand</code>: if specified in the IdP config, a textual identifier
  8952. for the brand of the IdP</li>
  8953. </ul>
  8954. </li>
  8955. <li><code>user_attributes</code>: an object containing details about the user that
  8956. we received from the IdP. May have the following attributes:
  8957. <ul>
  8958. <li><code>display_name</code>: the user's display name</li>
  8959. <li><code>emails</code>: a list of email addresses</li>
  8960. <li><code>localpart</code>: the local part of the Matrix user ID to register,
  8961. if <code>localpart_template</code> is set in the mapping provider configuration (empty
  8962. string if not)
  8963. The template should render a form which submits the following fields:</li>
  8964. </ul>
  8965. </li>
  8966. <li><code>username</code>: the localpart of the user's chosen user id</li>
  8967. </ul>
  8968. </li>
  8969. <li><code>sso_new_user_consent.html</code>: HTML page allowing the user to consent to the
  8970. server's terms and conditions. This is only shown for new users, and only if
  8971. <code>user_consent.require_at_registration</code> is set.
  8972. When rendering, this template is given the following variables:
  8973. <ul>
  8974. <li><code>server_name</code>: the homeserver's name.</li>
  8975. <li><code>user_id</code>: the user's matrix proposed ID.</li>
  8976. <li><code>user_profile.display_name</code>: the user's proposed display name, if any.</li>
  8977. <li>consent_version: the version of the terms that the user will be
  8978. shown</li>
  8979. <li><code>terms_url</code>: a link to the page showing the terms.
  8980. The template should render a form which submits the following fields:</li>
  8981. <li><code>accepted_version</code>: the version of the terms accepted by the user
  8982. (ie, 'consent_version' from the input variables).</li>
  8983. </ul>
  8984. </li>
  8985. <li><code>sso_redirect_confirm.html</code>: HTML page for a confirmation step before redirecting back
  8986. to the client with the login token.
  8987. When rendering, this template is given the following variables:
  8988. <ul>
  8989. <li><code>redirect_url</code>: the URL the user is about to be redirected to.</li>
  8990. <li><code>display_url</code>: the same as <code>redirect_url</code>, but with the query
  8991. parameters stripped. The intention is to have a
  8992. human-readable URL to show to users, not to use it as
  8993. the final address to redirect to.</li>
  8994. <li><code>server_name</code>: the homeserver's name.</li>
  8995. <li><code>new_user</code>: a boolean indicating whether this is the user's first time
  8996. logging in.</li>
  8997. <li><code>user_id</code>: the user's matrix ID.</li>
  8998. <li><code>user_profile.avatar_url</code>: an MXC URI for the user's avatar, if any.
  8999. <code>None</code> if the user has not set an avatar.</li>
  9000. <li><code>user_profile.display_name</code>: the user's display name. <code>None</code> if the user
  9001. has not set a display name.</li>
  9002. </ul>
  9003. </li>
  9004. <li><code>sso_auth_confirm.html</code>: HTML page which notifies the user that they are authenticating
  9005. to confirm an operation on their account during the user interactive authentication
  9006. process.
  9007. When rendering, this template is given the following variables:
  9008. <ul>
  9009. <li><code>redirect_url</code>: the URL the user is about to be redirected to.</li>
  9010. <li><code>description</code>: the operation which the user is being asked to confirm</li>
  9011. <li><code>idp</code>: details of the Identity Provider that we will use to confirm
  9012. the user's identity: an object with the following attributes:
  9013. <ul>
  9014. <li><code>idp_id</code>: unique identifier for the IdP</li>
  9015. <li><code>idp_name</code>: user-facing name for the IdP</li>
  9016. <li><code>idp_icon</code>: if specified in the IdP config, an MXC URI for an icon
  9017. for the IdP</li>
  9018. <li><code>idp_brand</code>: if specified in the IdP config, a textual identifier
  9019. for the brand of the IdP</li>
  9020. </ul>
  9021. </li>
  9022. </ul>
  9023. </li>
  9024. <li><code>sso_auth_success.html</code>: HTML page shown after a successful user interactive
  9025. authentication session.
  9026. Note that this page must include the JavaScript which notifies of a successful
  9027. authentication (see https://matrix.org/docs/spec/client_server/r0.6.0#fallback).
  9028. This template has no additional variables.</li>
  9029. <li><code>sso_auth_bad_user.html</code>: HTML page shown after a user-interactive authentication
  9030. session which does not map correctly onto the expected user.
  9031. When rendering, this template is given the following variables:
  9032. <ul>
  9033. <li><code>server_name</code>: the homeserver's name.</li>
  9034. <li><code>user_id_to_verify</code>: the MXID of the user that we are trying to
  9035. validate.</li>
  9036. </ul>
  9037. </li>
  9038. <li><code>sso_account_deactivated.html</code>: HTML page shown during single sign-on if a deactivated
  9039. user (according to Synapse's database) attempts to login.
  9040. This template has no additional variables.</li>
  9041. <li><code>sso_error.html</code>: HTML page to display to users if something goes wrong during the
  9042. OpenID Connect authentication process.
  9043. When rendering, this template is given two variables:
  9044. <ul>
  9045. <li><code>error</code>: the technical name of the error</li>
  9046. <li><code>error_description</code>: a human-readable message for the error</li>
  9047. </ul>
  9048. </li>
  9049. </ul>
  9050. <div style="break-before: page; page-break-before: always;"></div><h1 id="user-authentication"><a class="header" href="#user-authentication">User Authentication</a></h1>
  9051. <p>Synapse supports multiple methods of authenticating users, either out-of-the-box or through custom pluggable
  9052. authentication modules.</p>
  9053. <p>Included in Synapse is support for authenticating users via:</p>
  9054. <ul>
  9055. <li>A username and password.</li>
  9056. <li>An email address and password.</li>
  9057. <li>Single Sign-On through the SAML, Open ID Connect or CAS protocols.</li>
  9058. <li>JSON Web Tokens.</li>
  9059. <li>An administrator's shared secret.</li>
  9060. </ul>
  9061. <p>Synapse can additionally be extended to support custom authentication schemes through optional &quot;password auth provider&quot;
  9062. modules.</p>
  9063. <div style="break-before: page; page-break-before: always;"></div><h1 id="single-sign-on"><a class="header" href="#single-sign-on">Single Sign-On</a></h1>
  9064. <p>Synapse supports single sign-on through the SAML, Open ID Connect or CAS protocols.
  9065. LDAP and other login methods are supported through first and third-party password
  9066. auth provider modules.</p>
  9067. <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>
  9068. <p>Synapse can be configured to use an OpenID Connect Provider (OP) for
  9069. authentication, instead of its own local password database.</p>
  9070. <p>Any OP should work with Synapse, as long as it supports the authorization code
  9071. flow. There are a few options for that:</p>
  9072. <ul>
  9073. <li>
  9074. <p>start a local OP. Synapse has been tested with <a href="https://www.ory.sh/docs/hydra/">Hydra</a> and
  9075. <a href="https://github.com/dexidp/dex">Dex</a>. Note that for an OP to work, it should be served under a
  9076. secure (HTTPS) origin. A certificate signed with a self-signed, locally
  9077. trusted CA should work. In that case, start Synapse with a <code>SSL_CERT_FILE</code>
  9078. environment variable set to the path of the CA.</p>
  9079. </li>
  9080. <li>
  9081. <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
  9082. <a href="https://www.okta.com/">Okta</a>. Synapse has been tested with Auth0 and Google.</p>
  9083. </li>
  9084. </ul>
  9085. <p>It may also be possible to use other OAuth2 providers which provide the
  9086. <a href="https://tools.ietf.org/html/rfc6749#section-4.1">authorization code grant type</a>,
  9087. such as <a href="https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps">Github</a>.</p>
  9088. <h2 id="preparing-synapse"><a class="header" href="#preparing-synapse">Preparing Synapse</a></h2>
  9089. <p>The OpenID integration in Synapse uses the
  9090. <a href="https://pypi.org/project/Authlib/"><code>authlib</code></a> library, which must be installed
  9091. as follows:</p>
  9092. <ul>
  9093. <li>
  9094. <p>The relevant libraries are included in the Docker images and Debian packages
  9095. provided by <code>matrix.org</code> so no further action is needed.</p>
  9096. </li>
  9097. <li>
  9098. <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>
  9099. </li>
  9100. <li>
  9101. <p>For other installation mechanisms, see the documentation provided by the
  9102. maintainer.</p>
  9103. </li>
  9104. </ul>
  9105. <p>To enable the OpenID integration, you should then add a section to the <code>oidc_providers</code>
  9106. setting in your configuration file (or uncomment one of the existing examples).
  9107. See <a href="./sample_config.yaml">sample_config.yaml</a> for some sample settings, as well as
  9108. the text below for example configurations for specific providers.</p>
  9109. <h2 id="sample-configs"><a class="header" href="#sample-configs">Sample configs</a></h2>
  9110. <p>Here are a few configs for providers that should work with Synapse.</p>
  9111. <h3 id="microsoft-azure-active-directory"><a class="header" href="#microsoft-azure-active-directory">Microsoft Azure Active Directory</a></h3>
  9112. <p>Azure AD can act as an OpenID Connect Provider. Register a new application under
  9113. <em>App registrations</em> in the Azure AD management console. The RedirectURI for your
  9114. application should point to your matrix server:
  9115. <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></p>
  9116. <p>Go to <em>Certificates &amp; secrets</em> and register a new client secret. Make note of your
  9117. Directory (tenant) ID as it will be used in the Azure links.
  9118. Edit your Synapse config file and change the <code>oidc_config</code> section:</p>
  9119. <pre><code class="language-yaml">oidc_providers:
  9120. - idp_id: microsoft
  9121. idp_name: Microsoft
  9122. issuer: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/v2.0&quot;
  9123. client_id: &quot;&lt;client id&gt;&quot;
  9124. client_secret: &quot;&lt;client secret&gt;&quot;
  9125. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  9126. authorization_endpoint: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/oauth2/v2.0/authorize&quot;
  9127. token_endpoint: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/oauth2/v2.0/token&quot;
  9128. userinfo_endpoint: &quot;https://graph.microsoft.com/oidc/userinfo&quot;
  9129. user_mapping_provider:
  9130. config:
  9131. localpart_template: &quot;{{ user.preferred_username.split('@')[0] }}&quot;
  9132. display_name_template: &quot;{{ user.name }}&quot;
  9133. </code></pre>
  9134. <h3 id="dex"><a class="header" href="#dex">Dex</a></h3>
  9135. <p><a href="https://github.com/dexidp/dex">Dex</a> is a simple, open-source OpenID Connect Provider.
  9136. Although it is designed to help building a full-blown provider with an
  9137. external database, it can be configured with static passwords in a config file.</p>
  9138. <p>Follow the <a href="https://dexidp.io/docs/getting-started/">Getting Started guide</a>
  9139. to install Dex.</p>
  9140. <p>Edit <code>examples/config-dev.yaml</code> config file from the Dex repo to add a client:</p>
  9141. <pre><code class="language-yaml">staticClients:
  9142. - id: synapse
  9143. secret: secret
  9144. redirectURIs:
  9145. - '[synapse public baseurl]/_synapse/client/oidc/callback'
  9146. name: 'Synapse'
  9147. </code></pre>
  9148. <p>Run with <code>dex serve examples/config-dev.yaml</code>.</p>
  9149. <p>Synapse config:</p>
  9150. <pre><code class="language-yaml">oidc_providers:
  9151. - idp_id: dex
  9152. idp_name: &quot;My Dex server&quot;
  9153. skip_verification: true # This is needed as Dex is served on an insecure endpoint
  9154. issuer: &quot;http://127.0.0.1:5556/dex&quot;
  9155. client_id: &quot;synapse&quot;
  9156. client_secret: &quot;secret&quot;
  9157. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  9158. user_mapping_provider:
  9159. config:
  9160. localpart_template: &quot;{{ user.name }}&quot;
  9161. display_name_template: &quot;{{ user.name|capitalize }}&quot;
  9162. </code></pre>
  9163. <h3 id="keycloak"><a class="header" href="#keycloak">Keycloak</a></h3>
  9164. <p><a href="https://www.keycloak.org/docs/latest/server_admin/#sso-protocols">Keycloak</a> is an opensource IdP maintained by Red Hat.</p>
  9165. <p>Follow the <a href="https://www.keycloak.org/getting-started">Getting Started Guide</a> to install Keycloak and set up a realm.</p>
  9166. <ol>
  9167. <li>
  9168. <p>Click <code>Clients</code> in the sidebar and click <code>Create</code></p>
  9169. </li>
  9170. <li>
  9171. <p>Fill in the fields as below:</p>
  9172. </li>
  9173. </ol>
  9174. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  9175. <tr><td>Client ID</td><td><code>synapse</code></td></tr>
  9176. <tr><td>Client Protocol</td><td><code>openid-connect</code></td></tr>
  9177. </tbody></table>
  9178. <ol start="3">
  9179. <li>Click <code>Save</code></li>
  9180. <li>Fill in the fields as below:</li>
  9181. </ol>
  9182. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  9183. <tr><td>Client ID</td><td><code>synapse</code></td></tr>
  9184. <tr><td>Enabled</td><td><code>On</code></td></tr>
  9185. <tr><td>Client Protocol</td><td><code>openid-connect</code></td></tr>
  9186. <tr><td>Access Type</td><td><code>confidential</code></td></tr>
  9187. <tr><td>Valid Redirect URIs</td><td><code>[synapse public baseurl]/_synapse/client/oidc/callback</code></td></tr>
  9188. </tbody></table>
  9189. <ol start="5">
  9190. <li>Click <code>Save</code></li>
  9191. <li>On the Credentials tab, update the fields:</li>
  9192. </ol>
  9193. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  9194. <tr><td>Client Authenticator</td><td><code>Client ID and Secret</code></td></tr>
  9195. </tbody></table>
  9196. <ol start="7">
  9197. <li>Click <code>Regenerate Secret</code></li>
  9198. <li>Copy Secret</li>
  9199. </ol>
  9200. <pre><code class="language-yaml">oidc_providers:
  9201. - idp_id: keycloak
  9202. idp_name: &quot;My KeyCloak server&quot;
  9203. issuer: &quot;https://127.0.0.1:8443/realms/{realm_name}&quot;
  9204. client_id: &quot;synapse&quot;
  9205. client_secret: &quot;copy secret generated from above&quot;
  9206. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  9207. user_mapping_provider:
  9208. config:
  9209. localpart_template: &quot;{{ user.preferred_username }}&quot;
  9210. display_name_template: &quot;{{ user.name }}&quot;
  9211. </code></pre>
  9212. <h3 id="auth0"><a class="header" href="#auth0">Auth0</a></h3>
  9213. <p><a href="https://auth0.com/">Auth0</a> is a hosted SaaS IdP solution.</p>
  9214. <ol>
  9215. <li>
  9216. <p>Create a regular web application for Synapse</p>
  9217. </li>
  9218. <li>
  9219. <p>Set the Allowed Callback URLs to <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></p>
  9220. </li>
  9221. <li>
  9222. <p>Add a rule to add the <code>preferred_username</code> claim.</p>
  9223. <details>
  9224. <summary>Code sample</summary>
  9225. <pre><code class="language-js">function addPersistenceAttribute(user, context, callback) {
  9226. user.user_metadata = user.user_metadata || {};
  9227. user.user_metadata.preferred_username = user.user_metadata.preferred_username || user.user_id;
  9228. context.idToken.preferred_username = user.user_metadata.preferred_username;
  9229. auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
  9230. .then(function(){
  9231. callback(null, user, context);
  9232. })
  9233. .catch(function(err){
  9234. callback(err);
  9235. });
  9236. }
  9237. </code></pre>
  9238. </li>
  9239. </ol>
  9240. </details>
  9241. <p>Synapse config:</p>
  9242. <pre><code class="language-yaml">oidc_providers:
  9243. - idp_id: auth0
  9244. idp_name: Auth0
  9245. issuer: &quot;https://your-tier.eu.auth0.com/&quot; # TO BE FILLED
  9246. client_id: &quot;your-client-id&quot; # TO BE FILLED
  9247. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  9248. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  9249. user_mapping_provider:
  9250. config:
  9251. localpart_template: &quot;{{ user.preferred_username }}&quot;
  9252. display_name_template: &quot;{{ user.name }}&quot;
  9253. </code></pre>
  9254. <h3 id="authentik"><a class="header" href="#authentik">Authentik</a></h3>
  9255. <p><a href="https://goauthentik.io/">Authentik</a> is an open-source IdP solution.</p>
  9256. <ol>
  9257. <li>Create a provider in Authentik, with type OAuth2/OpenID.</li>
  9258. <li>The parameters are:</li>
  9259. </ol>
  9260. <ul>
  9261. <li>Client Type: Confidential</li>
  9262. <li>JWT Algorithm: RS256</li>
  9263. <li>Scopes: OpenID, Email and Profile</li>
  9264. <li>RSA Key: Select any available key</li>
  9265. <li>Redirect URIs: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  9266. </ul>
  9267. <ol start="3">
  9268. <li>Create an application for synapse in Authentik and link it to the provider.</li>
  9269. <li>Note the slug of your application, Client ID and Client Secret.</li>
  9270. </ol>
  9271. <p>Note: RSA keys must be used for signing for Authentik, ECC keys do not work.</p>
  9272. <p>Synapse config:</p>
  9273. <pre><code class="language-yaml">oidc_providers:
  9274. - idp_id: authentik
  9275. idp_name: authentik
  9276. discover: true
  9277. issuer: &quot;https://your.authentik.example.org/application/o/your-app-slug/&quot; # TO BE FILLED: domain and slug
  9278. client_id: &quot;your client id&quot; # TO BE FILLED
  9279. client_secret: &quot;your client secret&quot; # TO BE FILLED
  9280. scopes:
  9281. - &quot;openid&quot;
  9282. - &quot;profile&quot;
  9283. - &quot;email&quot;
  9284. user_mapping_provider:
  9285. config:
  9286. localpart_template: &quot;{{ user.preferred_username }}&quot;
  9287. 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.
  9288. </code></pre>
  9289. <h3 id="lemonldap"><a class="header" href="#lemonldap">LemonLDAP</a></h3>
  9290. <p><a href="https://lemonldap-ng.org/">LemonLDAP::NG</a> is an open-source IdP solution.</p>
  9291. <ol>
  9292. <li>Create an OpenID Connect Relying Parties in LemonLDAP::NG</li>
  9293. <li>The parameters are:</li>
  9294. </ol>
  9295. <ul>
  9296. <li>Client ID under the basic menu of the new Relying Parties (<code>Options &gt; Basic &gt; Client ID</code>)</li>
  9297. <li>Client secret (<code>Options &gt; Basic &gt; Client secret</code>)</li>
  9298. <li>JWT Algorithm: RS256 within the security menu of the new Relying Parties
  9299. (<code>Options &gt; Security &gt; ID Token signature algorithm</code> and <code>Options &gt; Security &gt; Access Token signature algorithm</code>)</li>
  9300. <li>Scopes: OpenID, Email and Profile</li>
  9301. <li>Allowed redirection addresses for login (<code>Options &gt; Basic &gt; Allowed redirection addresses for login</code> ) :
  9302. <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  9303. </ul>
  9304. <p>Synapse config:</p>
  9305. <pre><code class="language-yaml">oidc_providers:
  9306. - idp_id: lemonldap
  9307. idp_name: lemonldap
  9308. discover: true
  9309. issuer: &quot;https://auth.example.org/&quot; # TO BE FILLED: replace with your domain
  9310. client_id: &quot;your client id&quot; # TO BE FILLED
  9311. client_secret: &quot;your client secret&quot; # TO BE FILLED
  9312. scopes:
  9313. - &quot;openid&quot;
  9314. - &quot;profile&quot;
  9315. - &quot;email&quot;
  9316. user_mapping_provider:
  9317. config:
  9318. localpart_template: &quot;{{ user.preferred_username }}}&quot;
  9319. # 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.
  9320. display_name_template: &quot;{{ user.preferred_username|capitalize }}&quot;
  9321. </code></pre>
  9322. <h3 id="github"><a class="header" href="#github">GitHub</a></h3>
  9323. <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
  9324. just a regular OAuth2 provider.</p>
  9325. <p>The <a href="https://developer.github.com/v3/users/#get-the-authenticated-user"><code>/user</code> API endpoint</a>
  9326. can be used to retrieve information on the authenticated user. As the Synapse
  9327. login mechanism needs an attribute to uniquely identify users, and that endpoint
  9328. does not return a <code>sub</code> property, an alternative <code>subject_claim</code> has to be set.</p>
  9329. <ol>
  9330. <li>Create a new OAuth application: <a href="https://github.com/settings/applications/new">https://github.com/settings/applications/new</a>.</li>
  9331. <li>Set the callback URL to <code>[synapse public baseurl]/_synapse/client/oidc/callback</code>.</li>
  9332. </ol>
  9333. <p>Synapse config:</p>
  9334. <pre><code class="language-yaml">oidc_providers:
  9335. - idp_id: github
  9336. idp_name: Github
  9337. idp_brand: &quot;github&quot; # optional: styling hint for clients
  9338. discover: false
  9339. issuer: &quot;https://github.com/&quot;
  9340. client_id: &quot;your-client-id&quot; # TO BE FILLED
  9341. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  9342. authorization_endpoint: &quot;https://github.com/login/oauth/authorize&quot;
  9343. token_endpoint: &quot;https://github.com/login/oauth/access_token&quot;
  9344. userinfo_endpoint: &quot;https://api.github.com/user&quot;
  9345. scopes: [&quot;read:user&quot;]
  9346. user_mapping_provider:
  9347. config:
  9348. subject_claim: &quot;id&quot;
  9349. localpart_template: &quot;{{ user.login }}&quot;
  9350. display_name_template: &quot;{{ user.name }}&quot;
  9351. </code></pre>
  9352. <h3 id="google"><a class="header" href="#google">Google</a></h3>
  9353. <p><a href="https://developers.google.com/identity/protocols/oauth2/openid-connect">Google</a> is an OpenID certified authentication and authorisation provider.</p>
  9354. <ol>
  9355. <li>Set up a project in the Google API Console (see
  9356. <a href="https://developers.google.com/identity/protocols/oauth2/openid-connect#appsetup">documentation</a>).</li>
  9357. <li>Add an &quot;OAuth Client ID&quot; for a Web Application under &quot;Credentials&quot;.</li>
  9358. <li>Copy the Client ID and Client Secret, and add the following to your synapse config:
  9359. <pre><code class="language-yaml">oidc_providers:
  9360. - idp_id: google
  9361. idp_name: Google
  9362. idp_brand: &quot;google&quot; # optional: styling hint for clients
  9363. issuer: &quot;https://accounts.google.com/&quot;
  9364. client_id: &quot;your-client-id&quot; # TO BE FILLED
  9365. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  9366. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  9367. user_mapping_provider:
  9368. config:
  9369. localpart_template: &quot;{{ user.given_name|lower }}&quot;
  9370. display_name_template: &quot;{{ user.name }}&quot;
  9371. </code></pre>
  9372. </li>
  9373. <li>Back in the Google console, add this Authorized redirect URI: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code>.</li>
  9374. </ol>
  9375. <h3 id="twitch"><a class="header" href="#twitch">Twitch</a></h3>
  9376. <ol>
  9377. <li>Setup a developer account on <a href="https://dev.twitch.tv/">Twitch</a></li>
  9378. <li>Obtain the OAuth 2.0 credentials by <a href="https://dev.twitch.tv/console/apps/">creating an app</a></li>
  9379. <li>Add this OAuth Redirect URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  9380. </ol>
  9381. <p>Synapse config:</p>
  9382. <pre><code class="language-yaml">oidc_providers:
  9383. - idp_id: twitch
  9384. idp_name: Twitch
  9385. issuer: &quot;https://id.twitch.tv/oauth2/&quot;
  9386. client_id: &quot;your-client-id&quot; # TO BE FILLED
  9387. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  9388. client_auth_method: &quot;client_secret_post&quot;
  9389. user_mapping_provider:
  9390. config:
  9391. localpart_template: &quot;{{ user.preferred_username }}&quot;
  9392. display_name_template: &quot;{{ user.name }}&quot;
  9393. </code></pre>
  9394. <h3 id="gitlab"><a class="header" href="#gitlab">GitLab</a></h3>
  9395. <ol>
  9396. <li>Create a <a href="https://gitlab.com/profile/applications">new application</a>.</li>
  9397. <li>Add the <code>read_user</code> and <code>openid</code> scopes.</li>
  9398. <li>Add this Callback URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  9399. </ol>
  9400. <p>Synapse config:</p>
  9401. <pre><code class="language-yaml">oidc_providers:
  9402. - idp_id: gitlab
  9403. idp_name: Gitlab
  9404. idp_brand: &quot;gitlab&quot; # optional: styling hint for clients
  9405. issuer: &quot;https://gitlab.com/&quot;
  9406. client_id: &quot;your-client-id&quot; # TO BE FILLED
  9407. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  9408. client_auth_method: &quot;client_secret_post&quot;
  9409. scopes: [&quot;openid&quot;, &quot;read_user&quot;]
  9410. user_profile_method: &quot;userinfo_endpoint&quot;
  9411. user_mapping_provider:
  9412. config:
  9413. localpart_template: '{{ user.nickname }}'
  9414. display_name_template: '{{ user.name }}'
  9415. </code></pre>
  9416. <h3 id="facebook"><a class="header" href="#facebook">Facebook</a></h3>
  9417. <ol start="0">
  9418. <li>You will need a Facebook developer account. You can register for one
  9419. <a href="https://developers.facebook.com/async/registration/">here</a>.</li>
  9420. <li>On the <a href="https://developers.facebook.com/apps/">apps</a> page of the developer
  9421. console, &quot;Create App&quot;, and choose &quot;Build Connected Experiences&quot;.</li>
  9422. <li>Once the app is created, add &quot;Facebook Login&quot; and choose &quot;Web&quot;. You don't
  9423. need to go through the whole form here.</li>
  9424. <li>In the left-hand menu, open &quot;Products&quot;/&quot;Facebook Login&quot;/&quot;Settings&quot;.
  9425. <ul>
  9426. <li>Add <code>[synapse public baseurl]/_synapse/client/oidc/callback</code> as an OAuth Redirect
  9427. URL.</li>
  9428. </ul>
  9429. </li>
  9430. <li>In the left-hand menu, open &quot;Settings/Basic&quot;. Here you can copy the &quot;App ID&quot;
  9431. and &quot;App Secret&quot; for use below.</li>
  9432. </ol>
  9433. <p>Synapse config:</p>
  9434. <pre><code class="language-yaml"> - idp_id: facebook
  9435. idp_name: Facebook
  9436. idp_brand: &quot;facebook&quot; # optional: styling hint for clients
  9437. discover: false
  9438. issuer: &quot;https://www.facebook.com&quot;
  9439. client_id: &quot;your-client-id&quot; # TO BE FILLED
  9440. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  9441. scopes: [&quot;openid&quot;, &quot;email&quot;]
  9442. authorization_endpoint: &quot;https://facebook.com/dialog/oauth&quot;
  9443. token_endpoint: &quot;https://graph.facebook.com/v9.0/oauth/access_token&quot;
  9444. jwks_uri: &quot;https://www.facebook.com/.well-known/oauth/openid/jwks/&quot;
  9445. user_mapping_provider:
  9446. config:
  9447. display_name_template: &quot;{{ user.name }}&quot;
  9448. email_template: &quot;{{ '{{ user.email }}' }}&quot;
  9449. </code></pre>
  9450. <p>Relevant documents:</p>
  9451. <ul>
  9452. <li><a href="https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow">Manually Build a Login Flow</a></li>
  9453. <li><a href="https://developers.facebook.com/docs/graph-api/using-graph-api/">Using Facebook's Graph API</a></li>
  9454. <li><a href="https://developers.facebook.com/docs/graph-api/reference/user">Reference to the User endpoint</a></li>
  9455. </ul>
  9456. <p>Facebook do have an <a href="https://www.facebook.com/.well-known/openid-configuration">OIDC discovery endpoint</a>,
  9457. but it has a <code>response_types_supported</code> which excludes &quot;code&quot; (which we rely on, and
  9458. is even mentioned in their <a href="https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#login">documentation</a>),
  9459. so we have to disable discovery and configure the URIs manually.</p>
  9460. <h3 id="gitea"><a class="header" href="#gitea">Gitea</a></h3>
  9461. <p>Gitea is, like Github, not an OpenID provider, but just an OAuth2 provider.</p>
  9462. <p>The <a href="https://try.gitea.io/api/swagger#/user/userGetCurrent"><code>/user</code> API endpoint</a>
  9463. can be used to retrieve information on the authenticated user. As the Synapse
  9464. login mechanism needs an attribute to uniquely identify users, and that endpoint
  9465. does not return a <code>sub</code> property, an alternative <code>subject_claim</code> has to be set.</p>
  9466. <ol>
  9467. <li>Create a new application.</li>
  9468. <li>Add this Callback URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  9469. </ol>
  9470. <p>Synapse config:</p>
  9471. <pre><code class="language-yaml">oidc_providers:
  9472. - idp_id: gitea
  9473. idp_name: Gitea
  9474. discover: false
  9475. issuer: &quot;https://your-gitea.com/&quot;
  9476. client_id: &quot;your-client-id&quot; # TO BE FILLED
  9477. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  9478. client_auth_method: client_secret_post
  9479. scopes: [] # Gitea doesn't support Scopes
  9480. authorization_endpoint: &quot;https://your-gitea.com/login/oauth/authorize&quot;
  9481. token_endpoint: &quot;https://your-gitea.com/login/oauth/access_token&quot;
  9482. userinfo_endpoint: &quot;https://your-gitea.com/api/v1/user&quot;
  9483. user_mapping_provider:
  9484. config:
  9485. subject_claim: &quot;id&quot;
  9486. localpart_template: &quot;{{ user.login }}&quot;
  9487. display_name_template: &quot;{{ user.full_name }}&quot;
  9488. </code></pre>
  9489. <h3 id="xwiki"><a class="header" href="#xwiki">XWiki</a></h3>
  9490. <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>
  9491. <p>Synapse config:</p>
  9492. <pre><code class="language-yaml">oidc_providers:
  9493. - idp_id: xwiki
  9494. idp_name: &quot;XWiki&quot;
  9495. issuer: &quot;https://myxwikihost/xwiki/oidc/&quot;
  9496. client_id: &quot;your-client-id&quot; # TO BE FILLED
  9497. client_auth_method: none
  9498. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  9499. user_profile_method: &quot;userinfo_endpoint&quot;
  9500. user_mapping_provider:
  9501. config:
  9502. localpart_template: &quot;{{ user.preferred_username }}&quot;
  9503. display_name_template: &quot;{{ user.name }}&quot;
  9504. </code></pre>
  9505. <h3 id="apple"><a class="header" href="#apple">Apple</a></h3>
  9506. <p>Configuring &quot;Sign in with Apple&quot; (SiWA) requires an Apple Developer account.</p>
  9507. <p>You will need to create a new &quot;Services ID&quot; for SiWA, and create and download a
  9508. private key with &quot;SiWA&quot; enabled.</p>
  9509. <p>As well as the private key file, you will need:</p>
  9510. <ul>
  9511. <li>Client ID: the &quot;identifier&quot; you gave the &quot;Services ID&quot;</li>
  9512. <li>Team ID: a 10-character ID associated with your developer account.</li>
  9513. <li>Key ID: the 10-character identifier for the key.</li>
  9514. </ul>
  9515. <p><a href="https://help.apple.com/developer-account/?lang=en#/dev77c875b7e">Apple's developer documentation</a>
  9516. has more information on setting up SiWA.</p>
  9517. <p>The synapse config will look like this:</p>
  9518. <pre><code class="language-yaml"> - idp_id: apple
  9519. idp_name: Apple
  9520. issuer: &quot;https://appleid.apple.com&quot;
  9521. client_id: &quot;your-client-id&quot; # Set to the &quot;identifier&quot; for your &quot;ServicesID&quot;
  9522. client_auth_method: &quot;client_secret_post&quot;
  9523. client_secret_jwt_key:
  9524. key_file: &quot;/path/to/AuthKey_KEYIDCODE.p8&quot; # point to your key file
  9525. jwt_header:
  9526. alg: ES256
  9527. kid: &quot;KEYIDCODE&quot; # Set to the 10-char Key ID
  9528. jwt_payload:
  9529. iss: TEAMIDCODE # Set to the 10-char Team ID
  9530. scopes: [&quot;name&quot;, &quot;email&quot;, &quot;openid&quot;]
  9531. authorization_endpoint: https://appleid.apple.com/auth/authorize?response_mode=form_post
  9532. user_mapping_provider:
  9533. config:
  9534. email_template: &quot;{{ user.email }}&quot;
  9535. </code></pre>
  9536. <h3 id="django-oauth-toolkit"><a class="header" href="#django-oauth-toolkit">Django OAuth Toolkit</a></h3>
  9537. <p><a href="https://github.com/jazzband/django-oauth-toolkit">django-oauth-toolkit</a> is a
  9538. Django application providing out of the box all the endpoints, data and logic
  9539. needed to add OAuth2 capabilities to your Django projects. It supports
  9540. <a href="https://django-oauth-toolkit.readthedocs.io/en/latest/oidc.html">OpenID Connect too</a>.</p>
  9541. <p>Configuration on Django's side:</p>
  9542. <ol>
  9543. <li>Add an application: <code>https://example.com/admin/oauth2_provider/application/add/</code> and choose parameters like this:</li>
  9544. </ol>
  9545. <ul>
  9546. <li><code>Redirect uris</code>: <code>https://synapse.example.com/_synapse/client/oidc/callback</code></li>
  9547. <li><code>Client type</code>: <code>Confidential</code></li>
  9548. <li><code>Authorization grant type</code>: <code>Authorization code</code></li>
  9549. <li><code>Algorithm</code>: <code>HMAC with SHA-2 256</code></li>
  9550. </ul>
  9551. <ol start="2">
  9552. <li>
  9553. <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>
  9554. <details>
  9555. <summary>Code sample</summary>
  9556. <pre><code class="language-python">class CustomOAuth2Validator(OAuth2Validator):
  9557. def get_additional_claims(self, request):
  9558. return {
  9559. &quot;sub&quot;: request.user.email,
  9560. &quot;email&quot;: request.user.email,
  9561. &quot;first_name&quot;: request.user.first_name,
  9562. &quot;last_name&quot;: request.user.last_name,
  9563. }
  9564. </code></pre>
  9565. </details>
  9566. </li>
  9567. </ol>
  9568. <p>Your synapse config is then:</p>
  9569. <pre><code class="language-yaml">oidc_providers:
  9570. - idp_id: django_example
  9571. idp_name: &quot;Django Example&quot;
  9572. issuer: &quot;https://example.com/o/&quot;
  9573. client_id: &quot;your-client-id&quot; # CHANGE ME
  9574. client_secret: &quot;your-client-secret&quot; # CHANGE ME
  9575. scopes: [&quot;openid&quot;]
  9576. user_profile_method: &quot;userinfo_endpoint&quot; # needed because oauth-toolkit does not include user information in the authorization response
  9577. user_mapping_provider:
  9578. config:
  9579. localpart_template: &quot;{{ user.email.split('@')[0] }}&quot;
  9580. display_name_template: &quot;{{ user.first_name }} {{ user.last_name }}&quot;
  9581. email_template: &quot;{{ user.email }}&quot;
  9582. </code></pre>
  9583. <div style="break-before: page; page-break-before: always;"></div><h1 id="saml"><a class="header" href="#saml">SAML</a></h1>
  9584. <p>Synapse supports authenticating users via the <a href="https://en.wikipedia.org/wiki/Security_Assertion_Markup_Language">Security Assertion
  9585. Markup Language</a>
  9586. (SAML) protocol natively.</p>
  9587. <p>Please see the <code>saml2_config</code> and <code>sso</code> sections of the <a href="usage/configuration/user_authentication/single_sign_on/../../../configuration/homeserver_sample_config.html">Synapse configuration
  9588. file</a> for more details.</p>
  9589. <div style="break-before: page; page-break-before: always;"></div><h1 id="cas"><a class="header" href="#cas">CAS</a></h1>
  9590. <p>Synapse supports authenticating users via the <a href="https://en.wikipedia.org/wiki/Central_Authentication_Service">Central Authentication
  9591. Service protocol</a>
  9592. (CAS) natively.</p>
  9593. <p>Please see the <code>cas_config</code> and <code>sso</code> sections of the <a href="usage/configuration/user_authentication/single_sign_on/../../../configuration/homeserver_sample_config.html">Synapse configuration
  9594. file</a> for more details.</p>
  9595. <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>
  9596. <p>A mapping provider is a Python class (loaded via a Python module) that
  9597. works out how to map attributes of a SSO response to Matrix-specific
  9598. user attributes. Details such as user ID localpart, displayname, and even avatar
  9599. URLs are all things that can be mapped from talking to a SSO service.</p>
  9600. <p>As an example, a SSO service may return the email address
  9601. &quot;john.smith@example.com&quot; for a user, whereas Synapse will need to figure out how
  9602. to turn that into a displayname when creating a Matrix user for this individual.
  9603. It may choose <code>John Smith</code>, or <code>Smith, John [Example.com]</code> or any number of
  9604. variations. As each Synapse configuration may want something different, this is
  9605. where SAML mapping providers come into play.</p>
  9606. <p>SSO mapping providers are currently supported for OpenID and SAML SSO
  9607. configurations. Please see the details below for how to implement your own.</p>
  9608. <p>It is up to the mapping provider whether the user should be assigned a predefined
  9609. Matrix ID based on the SSO attributes, or if the user should be allowed to
  9610. choose their own username.</p>
  9611. <p>In the first case - where users are automatically allocated a Matrix ID - it is
  9612. the responsibility of the mapping provider to normalise the SSO attributes and
  9613. map them to a valid Matrix ID. The <a href="https://matrix.org/docs/spec/appendices#user-identifiers">specification for Matrix
  9614. IDs</a> has some
  9615. information about what is considered valid.</p>
  9616. <p>If the mapping provider does not assign a Matrix ID, then Synapse will
  9617. automatically serve an HTML page allowing the user to pick their own username.</p>
  9618. <p>External mapping providers are provided to Synapse in the form of an external
  9619. Python module. You can retrieve this module from <a href="https://pypi.org">PyPI</a> or elsewhere,
  9620. but it must be importable via Synapse (e.g. it must be in the same virtualenv
  9621. as Synapse). The Synapse config is then modified to point to the mapping provider
  9622. (and optionally provide additional configuration for it).</p>
  9623. <h2 id="openid-mapping-providers"><a class="header" href="#openid-mapping-providers">OpenID Mapping Providers</a></h2>
  9624. <p>The OpenID mapping provider can be customized by editing the
  9625. <code>oidc_config.user_mapping_provider.module</code> config option.</p>
  9626. <p><code>oidc_config.user_mapping_provider.config</code> allows you to provide custom
  9627. configuration options to the module. Check with the module's documentation for
  9628. what options it provides (if any). The options listed by default are for the
  9629. user mapping provider built in to Synapse. If using a custom module, you should
  9630. comment these options out and use those specified by the module instead.</p>
  9631. <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>
  9632. <p>A custom mapping provider must specify the following methods:</p>
  9633. <ul>
  9634. <li><code>def __init__(self, parsed_config)</code>
  9635. <ul>
  9636. <li>Arguments:
  9637. <ul>
  9638. <li><code>parsed_config</code> - A configuration object that is the return value of the
  9639. <code>parse_config</code> method. You should set any configuration options needed by
  9640. the module here.</li>
  9641. </ul>
  9642. </li>
  9643. </ul>
  9644. </li>
  9645. <li><code>def parse_config(config)</code>
  9646. <ul>
  9647. <li>This method should have the <code>@staticmethod</code> decoration.</li>
  9648. <li>Arguments:
  9649. <ul>
  9650. <li><code>config</code> - A <code>dict</code> representing the parsed content of the
  9651. <code>oidc_config.user_mapping_provider.config</code> homeserver config option.
  9652. Runs on homeserver startup. Providers should extract and validate
  9653. any option values they need here.</li>
  9654. </ul>
  9655. </li>
  9656. <li>Whatever is returned will be passed back to the user mapping provider module's
  9657. <code>__init__</code> method during construction.</li>
  9658. </ul>
  9659. </li>
  9660. <li><code>def get_remote_user_id(self, userinfo)</code>
  9661. <ul>
  9662. <li>Arguments:
  9663. <ul>
  9664. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  9665. information from.</li>
  9666. </ul>
  9667. </li>
  9668. <li>This method must return a string, which is the unique, immutable identifier
  9669. for the user. Commonly the <code>sub</code> claim of the response.</li>
  9670. </ul>
  9671. </li>
  9672. <li><code>async def map_user_attributes(self, userinfo, token, failures)</code>
  9673. <ul>
  9674. <li>This method must be async.</li>
  9675. <li>Arguments:
  9676. <ul>
  9677. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  9678. information from.</li>
  9679. <li><code>token</code> - A dictionary which includes information necessary to make
  9680. further requests to the OpenID provider.</li>
  9681. <li><code>failures</code> - An <code>int</code> that represents the amount of times the returned
  9682. mxid localpart mapping has failed. This should be used
  9683. to create a deduplicated mxid localpart which should be
  9684. returned instead. For example, if this method returns
  9685. <code>john.doe</code> as the value of <code>localpart</code> in the returned
  9686. dict, and that is already taken on the homeserver, this
  9687. method will be called again with the same parameters but
  9688. with failures=1. The method should then return a different
  9689. <code>localpart</code> value, such as <code>john.doe1</code>.</li>
  9690. </ul>
  9691. </li>
  9692. <li>Returns a dictionary with two keys:
  9693. <ul>
  9694. <li><code>localpart</code>: A string, used to generate the Matrix ID. If this is
  9695. <code>None</code>, the user is prompted to pick their own username. This is only used
  9696. during a user's first login. Once a localpart has been associated with a
  9697. remote user ID (see <code>get_remote_user_id</code>) it cannot be updated.</li>
  9698. <li><code>displayname</code>: An optional string, the display name for the user.</li>
  9699. </ul>
  9700. </li>
  9701. </ul>
  9702. </li>
  9703. <li><code>async def get_extra_attributes(self, userinfo, token)</code>
  9704. <ul>
  9705. <li>
  9706. <p>This method must be async.</p>
  9707. </li>
  9708. <li>
  9709. <p>Arguments:</p>
  9710. <ul>
  9711. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  9712. information from.</li>
  9713. <li><code>token</code> - A dictionary which includes information necessary to make
  9714. further requests to the OpenID provider.</li>
  9715. </ul>
  9716. </li>
  9717. <li>
  9718. <p>Returns a dictionary that is suitable to be serialized to JSON. This
  9719. will be returned as part of the response during a successful login.</p>
  9720. <p>Note that care should be taken to not overwrite any of the parameters
  9721. 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>
  9722. </li>
  9723. </ul>
  9724. </li>
  9725. </ul>
  9726. <h3 id="default-openid-mapping-provider"><a class="header" href="#default-openid-mapping-provider">Default OpenID Mapping Provider</a></h3>
  9727. <p>Synapse has a built-in OpenID mapping provider if a custom provider isn't
  9728. specified in the config. It is located at
  9729. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/handlers/oidc.py"><code>synapse.handlers.oidc.JinjaOidcMappingProvider</code></a>.</p>
  9730. <h2 id="saml-mapping-providers"><a class="header" href="#saml-mapping-providers">SAML Mapping Providers</a></h2>
  9731. <p>The SAML mapping provider can be customized by editing the
  9732. <code>saml2_config.user_mapping_provider.module</code> config option.</p>
  9733. <p><code>saml2_config.user_mapping_provider.config</code> allows you to provide custom
  9734. configuration options to the module. Check with the module's documentation for
  9735. what options it provides (if any). The options listed by default are for the
  9736. user mapping provider built in to Synapse. If using a custom module, you should
  9737. comment these options out and use those specified by the module instead.</p>
  9738. <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>
  9739. <p>A custom mapping provider must specify the following methods:</p>
  9740. <ul>
  9741. <li><code>def __init__(self, parsed_config, module_api)</code>
  9742. <ul>
  9743. <li>Arguments:
  9744. <ul>
  9745. <li><code>parsed_config</code> - A configuration object that is the return value of the
  9746. <code>parse_config</code> method. You should set any configuration options needed by
  9747. the module here.</li>
  9748. <li><code>module_api</code> - a <code>synapse.module_api.ModuleApi</code> object which provides the
  9749. stable API available for extension modules.</li>
  9750. </ul>
  9751. </li>
  9752. </ul>
  9753. </li>
  9754. <li><code>def parse_config(config)</code>
  9755. <ul>
  9756. <li><strong>This method should have the <code>@staticmethod</code> decoration.</strong></li>
  9757. <li>Arguments:
  9758. <ul>
  9759. <li><code>config</code> - A <code>dict</code> representing the parsed content of the
  9760. <code>saml_config.user_mapping_provider.config</code> homeserver config option.
  9761. Runs on homeserver startup. Providers should extract and validate
  9762. any option values they need here.</li>
  9763. </ul>
  9764. </li>
  9765. <li>Whatever is returned will be passed back to the user mapping provider module's
  9766. <code>__init__</code> method during construction.</li>
  9767. </ul>
  9768. </li>
  9769. <li><code>def get_saml_attributes(config)</code>
  9770. <ul>
  9771. <li><strong>This method should have the <code>@staticmethod</code> decoration.</strong></li>
  9772. <li>Arguments:
  9773. <ul>
  9774. <li><code>config</code> - A object resulting from a call to <code>parse_config</code>.</li>
  9775. </ul>
  9776. </li>
  9777. <li>Returns a tuple of two sets. The first set equates to the SAML auth
  9778. response attributes that are required for the module to function, whereas
  9779. the second set consists of those attributes which can be used if available,
  9780. but are not necessary.</li>
  9781. </ul>
  9782. </li>
  9783. <li><code>def get_remote_user_id(self, saml_response, client_redirect_url)</code>
  9784. <ul>
  9785. <li>Arguments:
  9786. <ul>
  9787. <li><code>saml_response</code> - A <code>saml2.response.AuthnResponse</code> object to extract user
  9788. information from.</li>
  9789. <li><code>client_redirect_url</code> - A string, the URL that the client will be
  9790. redirected to.</li>
  9791. </ul>
  9792. </li>
  9793. <li>This method must return a string, which is the unique, immutable identifier
  9794. for the user. Commonly the <code>uid</code> claim of the response.</li>
  9795. </ul>
  9796. </li>
  9797. <li><code>def saml_response_to_user_attributes(self, saml_response, failures, client_redirect_url)</code>
  9798. <ul>
  9799. <li>
  9800. <p>Arguments:</p>
  9801. <ul>
  9802. <li><code>saml_response</code> - A <code>saml2.response.AuthnResponse</code> object to extract user
  9803. information from.</li>
  9804. <li><code>failures</code> - An <code>int</code> that represents the amount of times the returned
  9805. mxid localpart mapping has failed. This should be used
  9806. to create a deduplicated mxid localpart which should be
  9807. returned instead. For example, if this method returns
  9808. <code>john.doe</code> as the value of <code>mxid_localpart</code> in the returned
  9809. dict, and that is already taken on the homeserver, this
  9810. method will be called again with the same parameters but
  9811. with failures=1. The method should then return a different
  9812. <code>mxid_localpart</code> value, such as <code>john.doe1</code>.</li>
  9813. <li><code>client_redirect_url</code> - A string, the URL that the client will be
  9814. redirected to.</li>
  9815. </ul>
  9816. </li>
  9817. <li>
  9818. <p>This method must return a dictionary, which will then be used by Synapse
  9819. to build a new user. The following keys are allowed:</p>
  9820. <ul>
  9821. <li><code>mxid_localpart</code> - A string, the mxid localpart of the new user. If this is
  9822. <code>None</code>, the user is prompted to pick their own username. This is only used
  9823. during a user's first login. Once a localpart has been associated with a
  9824. remote user ID (see <code>get_remote_user_id</code>) it cannot be updated.</li>
  9825. <li><code>displayname</code> - The displayname of the new user. If not provided, will default to
  9826. the value of <code>mxid_localpart</code>.</li>
  9827. <li><code>emails</code> - A list of emails for the new user. If not provided, will
  9828. default to an empty list.</li>
  9829. </ul>
  9830. <p>Alternatively it can raise a <code>synapse.api.errors.RedirectException</code> to
  9831. redirect the user to another page. This is useful to prompt the user for
  9832. additional information, e.g. if you want them to provide their own username.
  9833. It is the responsibility of the mapping provider to either redirect back
  9834. to <code>client_redirect_url</code> (including any additional information) or to
  9835. complete registration using methods from the <code>ModuleApi</code>.</p>
  9836. </li>
  9837. </ul>
  9838. </li>
  9839. </ul>
  9840. <h3 id="default-saml-mapping-provider"><a class="header" href="#default-saml-mapping-provider">Default SAML Mapping Provider</a></h3>
  9841. <p>Synapse has a built-in SAML mapping provider if a custom provider isn't
  9842. specified in the config. It is located at
  9843. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/handlers/saml.py"><code>synapse.handlers.saml.DefaultSamlMappingProvider</code></a>.</p>
  9844. <div style="break-before: page; page-break-before: always;"></div><h2 style="color:red">
  9845. This page of the Synapse documentation is now deprecated. For up to date
  9846. documentation on setting up or writing a password auth provider module, please see
  9847. <a href="modules/index.html">this page</a>.
  9848. </h2>
  9849. <h1 id="password-auth-provider-modules"><a class="header" href="#password-auth-provider-modules">Password auth provider modules</a></h1>
  9850. <p>Password auth providers offer a way for server administrators to
  9851. integrate their Synapse installation with an existing authentication
  9852. system.</p>
  9853. <p>A password auth provider is a Python class which is dynamically loaded
  9854. into Synapse, and provides a number of methods by which it can integrate
  9855. with the authentication system.</p>
  9856. <p>This document serves as a reference for those looking to implement their
  9857. own password auth providers. Additionally, here is a list of known
  9858. password auth provider module implementations:</p>
  9859. <ul>
  9860. <li><a href="https://github.com/matrix-org/matrix-synapse-ldap3/">matrix-synapse-ldap3</a></li>
  9861. <li><a href="https://github.com/devture/matrix-synapse-shared-secret-auth">matrix-synapse-shared-secret-auth</a></li>
  9862. <li><a href="https://github.com/ma1uta/matrix-synapse-rest-password-provider">matrix-synapse-rest-password-provider</a></li>
  9863. </ul>
  9864. <h2 id="required-methods"><a class="header" href="#required-methods">Required methods</a></h2>
  9865. <p>Password auth provider classes must provide the following methods:</p>
  9866. <ul>
  9867. <li>
  9868. <p><code>parse_config(config)</code>
  9869. This method is passed the <code>config</code> object for this module from the
  9870. homeserver configuration file.</p>
  9871. <p>It should perform any appropriate sanity checks on the provided
  9872. configuration, and return an object which is then passed into
  9873. <code>__init__</code>.</p>
  9874. <p>This method should have the <code>@staticmethod</code> decoration.</p>
  9875. </li>
  9876. <li>
  9877. <p><code>__init__(self, config, account_handler)</code></p>
  9878. <p>The constructor is passed the config object returned by
  9879. <code>parse_config</code>, and a <code>synapse.module_api.ModuleApi</code> object which
  9880. allows the password provider to check if accounts exist and/or create
  9881. new ones.</p>
  9882. </li>
  9883. </ul>
  9884. <h2 id="optional-methods"><a class="header" href="#optional-methods">Optional methods</a></h2>
  9885. <p>Password auth provider classes may optionally provide the following methods:</p>
  9886. <ul>
  9887. <li>
  9888. <p><code>get_db_schema_files(self)</code></p>
  9889. <p>This method, if implemented, should return an Iterable of
  9890. <code>(name, stream)</code> pairs of database schema files. Each file is applied
  9891. in turn at initialisation, and a record is then made in the database
  9892. so that it is not re-applied on the next start.</p>
  9893. </li>
  9894. <li>
  9895. <p><code>get_supported_login_types(self)</code></p>
  9896. <p>This method, if implemented, should return a <code>dict</code> mapping from a
  9897. login type identifier (such as <code>m.login.password</code>) to an iterable
  9898. giving the fields which must be provided by the user in the submission
  9899. to <a href="https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login">the <code>/login</code> API</a>.
  9900. These fields are passed in the <code>login_dict</code> dictionary to <code>check_auth</code>.</p>
  9901. <p>For example, if a password auth provider wants to implement a custom
  9902. login type of <code>com.example.custom_login</code>, where the client is expected
  9903. to pass the fields <code>secret1</code> and <code>secret2</code>, the provider should
  9904. implement this method and return the following dict:</p>
  9905. <pre><code class="language-python">{&quot;com.example.custom_login&quot;: (&quot;secret1&quot;, &quot;secret2&quot;)}
  9906. </code></pre>
  9907. </li>
  9908. <li>
  9909. <p><code>check_auth(self, username, login_type, login_dict)</code></p>
  9910. <p>This method does the real work. If implemented, it
  9911. will be called for each login attempt where the login type matches one
  9912. of the keys returned by <code>get_supported_login_types</code>.</p>
  9913. <p>It is passed the (possibly unqualified) <code>user</code> field provided by the client,
  9914. the login type, and a dictionary of login secrets passed by the
  9915. client.</p>
  9916. <p>The method should return an <code>Awaitable</code> object, which resolves
  9917. to the canonical <code>@localpart:domain</code> user ID if authentication is
  9918. successful, and <code>None</code> if not.</p>
  9919. <p>Alternatively, the <code>Awaitable</code> can resolve to a <code>(str, func)</code> tuple, in
  9920. which case the second field is a callback which will be called with
  9921. the result from the <code>/login</code> call (including <code>access_token</code>,
  9922. <code>device_id</code>, etc.)</p>
  9923. </li>
  9924. <li>
  9925. <p><code>check_3pid_auth(self, medium, address, password)</code></p>
  9926. <p>This method, if implemented, is called when a user attempts to
  9927. register or log in with a third party identifier, such as email. It is
  9928. passed the medium (ex. &quot;email&quot;), an address (ex.
  9929. &quot;<a href="mailto:jdoe@example.com">jdoe@example.com</a>&quot;) and the user's password.</p>
  9930. <p>The method should return an <code>Awaitable</code> object, which resolves
  9931. to a <code>str</code> containing the user's (canonical) User id if
  9932. authentication was successful, and <code>None</code> if not.</p>
  9933. <p>As with <code>check_auth</code>, the <code>Awaitable</code> may alternatively resolve to a
  9934. <code>(user_id, callback)</code> tuple.</p>
  9935. </li>
  9936. <li>
  9937. <p><code>check_password(self, user_id, password)</code></p>
  9938. <p>This method provides a simpler interface than
  9939. <code>get_supported_login_types</code> and <code>check_auth</code> for password auth
  9940. providers that just want to provide a mechanism for validating
  9941. <code>m.login.password</code> logins.</p>
  9942. <p>If implemented, it will be called to check logins with an
  9943. <code>m.login.password</code> login type. It is passed a qualified
  9944. <code>@localpart:domain</code> user id, and the password provided by the user.</p>
  9945. <p>The method should return an <code>Awaitable</code> object, which resolves
  9946. to <code>True</code> if authentication is successful, and <code>False</code> if not.</p>
  9947. </li>
  9948. <li>
  9949. <p><code>on_logged_out(self, user_id, device_id, access_token)</code></p>
  9950. <p>This method, if implemented, is called when a user logs out. It is
  9951. passed the qualified user ID, the ID of the deactivated device (if
  9952. any: access tokens are occasionally created without an associated
  9953. device ID), and the (now deactivated) access token.</p>
  9954. <p>It may return an <code>Awaitable</code> object; the logout request will
  9955. wait for the <code>Awaitable</code> to complete, but the result is ignored.</p>
  9956. </li>
  9957. </ul>
  9958. <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>
  9959. <p>Synapse comes with a non-standard login type to support
  9960. <a href="https://en.wikipedia.org/wiki/JSON_Web_Token">JSON Web Tokens</a>. In general the
  9961. documentation for
  9962. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#login">the login endpoint</a>
  9963. is still valid (and the mechanism works similarly to the
  9964. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#token-based">token based login</a>).</p>
  9965. <p>To log in using a JSON Web Token, clients should submit a <code>/login</code> request as
  9966. follows:</p>
  9967. <pre><code class="language-json">{
  9968. &quot;type&quot;: &quot;org.matrix.login.jwt&quot;,
  9969. &quot;token&quot;: &quot;&lt;jwt&gt;&quot;
  9970. }
  9971. </code></pre>
  9972. <p>The <code>token</code> field should include the JSON web token with the following claims:</p>
  9973. <ul>
  9974. <li>A claim that encodes the local part of the user ID is required. By default,
  9975. the <code>sub</code> (subject) claim is used, or a custom claim can be set in the
  9976. configuration file.</li>
  9977. <li>The expiration time (<code>exp</code>), not before time (<code>nbf</code>), and issued at (<code>iat</code>)
  9978. claims are optional, but validated if present.</li>
  9979. <li>The issuer (<code>iss</code>) claim is optional, but required and validated if configured.</li>
  9980. <li>The audience (<code>aud</code>) claim is optional, but required and validated if configured.
  9981. Providing the audience claim when not configured will cause validation to fail.</li>
  9982. </ul>
  9983. <p>In the case that the token is not valid, the homeserver must respond with
  9984. <code>403 Forbidden</code> and an error code of <code>M_FORBIDDEN</code>.</p>
  9985. <p>As with other login types, there are additional fields (e.g. <code>device_id</code> and
  9986. <code>initial_device_display_name</code>) which can be included in the above request.</p>
  9987. <h2 id="preparing-synapse-1"><a class="header" href="#preparing-synapse-1">Preparing Synapse</a></h2>
  9988. <p>The JSON Web Token integration in Synapse uses the
  9989. <a href="https://pypi.org/project/pyjwt/"><code>PyJWT</code></a> library, which must be installed
  9990. as follows:</p>
  9991. <ul>
  9992. <li>
  9993. <p>The relevant libraries are included in the Docker images and Debian packages
  9994. provided by <code>matrix.org</code> so no further action is needed.</p>
  9995. </li>
  9996. <li>
  9997. <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>
  9998. </li>
  9999. <li>
  10000. <p>For other installation mechanisms, see the documentation provided by the
  10001. maintainer.</p>
  10002. </li>
  10003. </ul>
  10004. <p>To enable the JSON web token integration, you should then add an <code>jwt_config</code> section
  10005. to your configuration file (or uncomment the <code>enabled: true</code> line in the
  10006. existing section). See <a href="./sample_config.yaml">sample_config.yaml</a> for some
  10007. sample settings.</p>
  10008. <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>
  10009. <p>Although JSON Web Tokens are typically generated from an external server, the
  10010. examples below use <a href="https://pyjwt.readthedocs.io/en/latest/">PyJWT</a> directly.</p>
  10011. <ol>
  10012. <li>
  10013. <p>Configure Synapse with JWT logins, note that this example uses a pre-shared
  10014. secret and an algorithm of HS256:</p>
  10015. <pre><code class="language-yaml">jwt_config:
  10016. enabled: true
  10017. secret: &quot;my-secret-token&quot;
  10018. algorithm: &quot;HS256&quot;
  10019. </code></pre>
  10020. </li>
  10021. <li>
  10022. <p>Generate a JSON web token:</p>
  10023. <pre><code class="language-bash">$ pyjwt --key=my-secret-token --alg=HS256 encode sub=test-user
  10024. eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc
  10025. </code></pre>
  10026. </li>
  10027. <li>
  10028. <p>Query for the login types and ensure <code>org.matrix.login.jwt</code> is there:</p>
  10029. <pre><code class="language-bash">curl http://localhost:8080/_matrix/client/r0/login
  10030. </code></pre>
  10031. </li>
  10032. <li>
  10033. <p>Login used the generated JSON web token from above:</p>
  10034. <pre><code class="language-bash">$ curl http://localhost:8082/_matrix/client/r0/login -X POST \
  10035. --data '{&quot;type&quot;:&quot;org.matrix.login.jwt&quot;,&quot;token&quot;:&quot;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc&quot;}'
  10036. {
  10037. &quot;access_token&quot;: &quot;&lt;access token&gt;&quot;,
  10038. &quot;device_id&quot;: &quot;ACBDEFGHI&quot;,
  10039. &quot;home_server&quot;: &quot;localhost:8080&quot;,
  10040. &quot;user_id&quot;: &quot;@test-user:localhost:8480&quot;
  10041. }
  10042. </code></pre>
  10043. </li>
  10044. </ol>
  10045. <p>You should now be able to use the returned access token to query the client API.</p>
  10046. <div style="break-before: page; page-break-before: always;"></div><h1 id="refresh-tokens"><a class="header" href="#refresh-tokens">Refresh Tokens</a></h1>
  10047. <p>Synapse supports refresh tokens since version 1.49 (some earlier versions had support for an earlier, experimental draft of <a href="https://github.com/matrix-org/matrix-doc/blob/main/proposals/2918-refreshtokens.md#msc2918-refresh-tokens">MSC2918</a> which is not compatible).</p>
  10048. <h2 id="background-and-motivation"><a class="header" href="#background-and-motivation">Background and motivation</a></h2>
  10049. <p>Synapse users' sessions are identified by <strong>access tokens</strong>; access tokens are
  10050. issued to users on login. Each session gets a unique access token which identifies
  10051. it; the access token must be kept secret as it grants access to the user's account.</p>
  10052. <p>Traditionally, these access tokens were eternally valid (at least until the user
  10053. explicitly chose to log out).</p>
  10054. <p>In some cases, it may be desirable for these access tokens to expire so that the
  10055. potential damage caused by leaking an access token is reduced.
  10056. On the other hand, forcing a user to re-authenticate (log in again) often might
  10057. be too much of an inconvenience.</p>
  10058. <p><strong>Refresh tokens</strong> are a mechanism to avoid some of this inconvenience whilst
  10059. still getting most of the benefits of short access token lifetimes.
  10060. Refresh tokens are also a concept present in OAuth 2 — further reading is available
  10061. <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-1.5">here</a>.</p>
  10062. <p>When refresh tokens are in use, both an access token and a refresh token will be
  10063. issued to users on login. The access token will expire after a predetermined amount
  10064. of time, but otherwise works in the same way as before. When the access token is
  10065. close to expiring (or has expired), the user's client should present the homeserver
  10066. (Synapse) with the refresh token.</p>
  10067. <p>The homeserver will then generate a new access token and refresh token for the user
  10068. and return them. The old refresh token is invalidated and can not be used again*.</p>
  10069. <p>Finally, refresh tokens also make it possible for sessions to be logged out if they
  10070. are inactive for too long, before the session naturally ends; see the configuration
  10071. guide below.</p>
  10072. <p>*To prevent issues if clients lose connection half-way through refreshing a token,
  10073. the refresh token is only invalidated once the new access token has been used at
  10074. least once. For all intents and purposes, the above simplification is sufficient.</p>
  10075. <h2 id="caveats"><a class="header" href="#caveats">Caveats</a></h2>
  10076. <p>There are some caveats:</p>
  10077. <ul>
  10078. <li>If a third party gets both your access token and refresh token, they will be able to
  10079. continue to enjoy access to your session.
  10080. <ul>
  10081. <li>This is still an improvement because you (the user) will notice when <em>your</em>
  10082. session expires and you're not able to use your refresh token.
  10083. That would be a giveaway that someone else has compromised your session.
  10084. You would be able to log in again and terminate that session.
  10085. Previously (with long-lived access tokens), a third party that has your access
  10086. token could go undetected for a very long time.</li>
  10087. </ul>
  10088. </li>
  10089. <li>Clients need to implement support for refresh tokens in order for them to be a
  10090. useful mechanism.
  10091. <ul>
  10092. <li>It is up to homeserver administrators if they want to issue long-lived access
  10093. tokens to clients not implementing refresh tokens.
  10094. <ul>
  10095. <li>For compatibility, it is likely that they should, at least until client support
  10096. is widespread.
  10097. <ul>
  10098. <li>Users with clients that support refresh tokens will still benefit from the
  10099. added security; it's not possible to downgrade a session to using long-lived
  10100. access tokens so this effectively gives users the choice.</li>
  10101. </ul>
  10102. </li>
  10103. <li>In a closed environment where all users use known clients, this may not be
  10104. an issue as the homeserver administrator can know if the clients have refresh
  10105. token support. In that case, the non-refreshable access token lifetime
  10106. may be set to a short duration so that a similar level of security is provided.</li>
  10107. </ul>
  10108. </li>
  10109. </ul>
  10110. </li>
  10111. </ul>
  10112. <h2 id="configuration-guide"><a class="header" href="#configuration-guide">Configuration Guide</a></h2>
  10113. <p>The following configuration options, in the <code>registration</code> section, are related:</p>
  10114. <ul>
  10115. <li><code>session_lifetime</code>: maximum length of a session, even if it's refreshed.
  10116. In other words, the client must log in again after this time period.
  10117. In most cases, this can be unset (infinite) or set to a long time (years or months).</li>
  10118. <li><code>refreshable_access_token_lifetime</code>: lifetime of access tokens that are created
  10119. by clients supporting refresh tokens.
  10120. This should be short; a good value might be 5 minutes (<code>5m</code>).</li>
  10121. <li><code>nonrefreshable_access_token_lifetime</code>: lifetime of access tokens that are created
  10122. by clients which don't support refresh tokens.
  10123. Make this short if you want to effectively force use of refresh tokens.
  10124. Make this long if you don't want to inconvenience users of clients which don't
  10125. support refresh tokens (by forcing them to frequently re-authenticate using
  10126. login credentials).</li>
  10127. <li><code>refresh_token_lifetime</code>: lifetime of refresh tokens.
  10128. In other words, the client must refresh within this time period to maintain its session.
  10129. Unless you want to log inactive sessions out, it is often fine to use a long
  10130. value here or even leave it unset (infinite).
  10131. Beware that making it too short will inconvenience clients that do not connect
  10132. very often, including mobile clients and clients of infrequent users (by making
  10133. it more difficult for them to refresh in time, which may force them to need to
  10134. re-authenticate using login credentials).</li>
  10135. </ul>
  10136. <p><strong>Note:</strong> All four options above only apply when tokens are created (by logging in or refreshing).
  10137. Changes to these settings do not apply retroactively.</p>
  10138. <h3 id="using-refresh-token-expiry-to-log-out-inactive-sessions"><a class="header" href="#using-refresh-token-expiry-to-log-out-inactive-sessions">Using refresh token expiry to log out inactive sessions</a></h3>
  10139. <p>If you'd like to force sessions to be logged out upon inactivity, you can enable
  10140. refreshable access token expiry and refresh token expiry.</p>
  10141. <p>This works because a client must refresh at least once within a period of
  10142. <code>refresh_token_lifetime</code> in order to maintain valid credentials to access the
  10143. account.</p>
  10144. <p>(It's suggested that <code>refresh_token_lifetime</code> should be longer than
  10145. <code>refreshable_access_token_lifetime</code> and this section assumes that to be the case
  10146. for simplicity.)</p>
  10147. <p>Note: this will only affect sessions using refresh tokens. You may wish to
  10148. set a short <code>nonrefreshable_access_token_lifetime</code> to prevent this being bypassed
  10149. by clients that do not support refresh tokens.</p>
  10150. <h4 id="choosing-values-that-guarantee-permitting-some-inactivity"><a class="header" href="#choosing-values-that-guarantee-permitting-some-inactivity">Choosing values that guarantee permitting some inactivity</a></h4>
  10151. <p>It may be desirable to permit some short periods of inactivity, for example to
  10152. accommodate brief outages in client connectivity.</p>
  10153. <p>The following model aims to provide guidance for choosing <code>refresh_token_lifetime</code>
  10154. and <code>refreshable_access_token_lifetime</code> to satisfy requirements of the form:</p>
  10155. <ol>
  10156. <li>inactivity longer than <code>L</code> <strong>MUST</strong> cause the session to be logged out; and</li>
  10157. <li>inactivity shorter than <code>S</code> <strong>MUST NOT</strong> cause the session to be logged out.</li>
  10158. </ol>
  10159. <p>This model makes the weakest assumption that all active clients will refresh as
  10160. needed to maintain an active access token, but no sooner.
  10161. <em>In reality, clients may refresh more often than this model assumes, but the
  10162. above requirements will still hold.</em></p>
  10163. <p>To satisfy the above model,</p>
  10164. <ul>
  10165. <li><code>refresh_token_lifetime</code> should be set to <code>L</code>; and</li>
  10166. <li><code>refreshable_access_token_lifetime</code> should be set to <code>L - S</code>.</li>
  10167. </ul>
  10168. <div style="break-before: page; page-break-before: always;"></div><h1 id="overview-2"><a class="header" href="#overview-2">Overview</a></h1>
  10169. <p>A captcha can be enabled on your homeserver to help prevent bots from registering
  10170. accounts. Synapse currently uses Google's reCAPTCHA service which requires API keys
  10171. from Google.</p>
  10172. <h2 id="getting-api-keys"><a class="header" href="#getting-api-keys">Getting API keys</a></h2>
  10173. <ol>
  10174. <li>Create a new site at <a href="https://www.google.com/recaptcha/admin/create">https://www.google.com/recaptcha/admin/create</a></li>
  10175. <li>Set the label to anything you want</li>
  10176. <li>Set the type to reCAPTCHA v2 using the &quot;I'm not a robot&quot; Checkbox option.
  10177. This is the only type of captcha that works with Synapse.</li>
  10178. <li>Add the public hostname for your server, as set in <code>public_baseurl</code>
  10179. in <code>homeserver.yaml</code>, to the list of authorized domains. If you have not set
  10180. <code>public_baseurl</code>, use <code>server_name</code>.</li>
  10181. <li>Agree to the terms of service and submit.</li>
  10182. <li>Copy your site key and secret key and add them to your <code>homeserver.yaml</code>
  10183. configuration file
  10184. <pre><code class="language-yaml">recaptcha_public_key: YOUR_SITE_KEY
  10185. recaptcha_private_key: YOUR_SECRET_KEY
  10186. </code></pre>
  10187. </li>
  10188. <li>Enable the CAPTCHA for new registrations
  10189. <pre><code class="language-yaml">enable_registration_captcha: true
  10190. </code></pre>
  10191. </li>
  10192. <li>Go to the settings page for the CAPTCHA you just created</li>
  10193. <li>Uncheck the &quot;Verify the origin of reCAPTCHA solutions&quot; checkbox so that the
  10194. captcha can be displayed in any client. If you do not disable this option then you
  10195. must specify the domains of every client that is allowed to display the CAPTCHA.</li>
  10196. </ol>
  10197. <h2 id="configuring-ip-used-for-auth"><a class="header" href="#configuring-ip-used-for-auth">Configuring IP used for auth</a></h2>
  10198. <p>The reCAPTCHA API requires that the IP address of the user who solved the
  10199. CAPTCHA is sent. If the client is connecting through a proxy or load balancer,
  10200. it may be required to use the <code>X-Forwarded-For</code> (XFF) header instead of the origin
  10201. IP address. This can be configured using the <code>x_forwarded</code> directive in the
  10202. listeners section of the <code>homeserver.yaml</code> configuration file.</p>
  10203. <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>
  10204. <p>The registration of new application services depends on the homeserver used.
  10205. In synapse, you need to create a new configuration file for your AS and add it
  10206. to the list specified under the <code>app_service_config_files</code> config
  10207. option in your synapse config.</p>
  10208. <p>For example:</p>
  10209. <pre><code class="language-yaml">app_service_config_files:
  10210. - /home/matrix/.synapse/&lt;your-AS&gt;.yaml
  10211. </code></pre>
  10212. <p>The format of the AS configuration file is as follows:</p>
  10213. <pre><code class="language-yaml">url: &lt;base url of AS&gt;
  10214. as_token: &lt;token AS will add to requests to HS&gt;
  10215. hs_token: &lt;token HS will add to requests to AS&gt;
  10216. sender_localpart: &lt;localpart of AS user&gt;
  10217. namespaces:
  10218. users: # List of users we're interested in
  10219. - exclusive: &lt;bool&gt;
  10220. regex: &lt;regex&gt;
  10221. group_id: &lt;group&gt;
  10222. - ...
  10223. aliases: [] # List of aliases we're interested in
  10224. rooms: [] # List of room ids we're interested in
  10225. </code></pre>
  10226. <p><code>exclusive</code>: If enabled, only this application service is allowed to register users in its namespace(s).
  10227. <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>
  10228. <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>
  10229. <div style="break-before: page; page-break-before: always;"></div><h1 id="server-notices"><a class="header" href="#server-notices">Server Notices</a></h1>
  10230. <p>'Server Notices' are a new feature introduced in Synapse 0.30. They provide a
  10231. channel whereby server administrators can send messages to users on the server.</p>
  10232. <p>They are used as part of communication of the server polices (see
  10233. <a href="consent_tracking.html">Consent Tracking</a>), however the intention is that
  10234. they may also find a use for features such as &quot;Message of the day&quot;.</p>
  10235. <p>This is a feature specific to Synapse, but it uses standard Matrix
  10236. communication mechanisms, so should work with any Matrix client.</p>
  10237. <h2 id="user-experience"><a class="header" href="#user-experience">User experience</a></h2>
  10238. <p>When the user is first sent a server notice, they will get an invitation to a
  10239. room (typically called 'Server Notices', though this is configurable in
  10240. <code>homeserver.yaml</code>). They will be <strong>unable to reject</strong> this invitation -
  10241. attempts to do so will receive an error.</p>
  10242. <p>Once they accept the invitation, they will see the notice message in the room
  10243. history; it will appear to have come from the 'server notices user' (see
  10244. below).</p>
  10245. <p>The user is prevented from sending any messages in this room by the power
  10246. levels.</p>
  10247. <p>Having joined the room, the user can leave the room if they want. Subsequent
  10248. server notices will then cause a new room to be created.</p>
  10249. <h2 id="synapse-configuration"><a class="header" href="#synapse-configuration">Synapse configuration</a></h2>
  10250. <p>Server notices come from a specific user id on the server. Server
  10251. administrators are free to choose the user id - something like <code>server</code> is
  10252. suggested, meaning the notices will come from
  10253. <code>@server:&lt;your_server_name&gt;</code>. Once the Server Notices user is configured, that
  10254. user id becomes a special, privileged user, so administrators should ensure
  10255. that <strong>it is not already allocated</strong>.</p>
  10256. <p>In order to support server notices, it is necessary to add some configuration
  10257. to the <code>homeserver.yaml</code> file. In particular, you should add a <code>server_notices</code>
  10258. section, which should look like this:</p>
  10259. <pre><code class="language-yaml">server_notices:
  10260. system_mxid_localpart: server
  10261. system_mxid_display_name: &quot;Server Notices&quot;
  10262. system_mxid_avatar_url: &quot;mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ&quot;
  10263. room_name: &quot;Server Notices&quot;
  10264. </code></pre>
  10265. <p>The only compulsory setting is <code>system_mxid_localpart</code>, which defines the user
  10266. id of the Server Notices user, as above. <code>room_name</code> defines the name of the
  10267. room which will be created.</p>
  10268. <p><code>system_mxid_display_name</code> and <code>system_mxid_avatar_url</code> can be used to set the
  10269. displayname and avatar of the Server Notices user.</p>
  10270. <h2 id="sending-notices"><a class="header" href="#sending-notices">Sending notices</a></h2>
  10271. <p>To send server notices to users you can use the
  10272. <a href="admin_api/server_notices.html">admin_api</a>.</p>
  10273. <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>
  10274. <p>Synapse 0.30 introduces support for tracking whether users have agreed to the
  10275. terms and conditions set by the administrator of a server - and blocking access
  10276. to the server until they have.</p>
  10277. <p>There are several parts to this functionality; each requires some specific
  10278. configuration in <code>homeserver.yaml</code> to be enabled.</p>
  10279. <p>Note that various parts of the configuation and this document refer to the
  10280. &quot;privacy policy&quot;: agreement with a privacy policy is one particular use of this
  10281. feature, but of course adminstrators can specify other terms and conditions
  10282. unrelated to &quot;privacy&quot; per se.</p>
  10283. <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>
  10284. <p>Synapse can be configured to serve the user a simple policy form with an
  10285. &quot;accept&quot; button. Clicking &quot;Accept&quot; records the user's acceptance in the
  10286. database and shows a success page.</p>
  10287. <p>To enable this, first create templates for the policy and success pages.
  10288. These should be stored on the local filesystem.</p>
  10289. <p>These templates use the <a href="http://jinja.pocoo.org">Jinja2</a> templating language,
  10290. and <a href="https://github.com/matrix-org/synapse/tree/develop/docs/privacy_policy_templates/">docs/privacy_policy_templates</a>
  10291. gives examples of the sort of thing that can be done.</p>
  10292. <p>Note that the templates must be stored under a name giving the language of the
  10293. template - currently this must always be <code>en</code> (for &quot;English&quot;);
  10294. internationalisation support is intended for the future.</p>
  10295. <p>The template for the policy itself should be versioned and named according to
  10296. the version: for example <code>1.0.html</code>. The version of the policy which the user
  10297. has agreed to is stored in the database.</p>
  10298. <p>Once the templates are in place, make the following changes to <code>homeserver.yaml</code>:</p>
  10299. <ol>
  10300. <li>
  10301. <p>Add a <code>user_consent</code> section, which should look like:</p>
  10302. <pre><code class="language-yaml">user_consent:
  10303. template_dir: privacy_policy_templates
  10304. version: 1.0
  10305. </code></pre>
  10306. <p><code>template_dir</code> points to the directory containing the policy
  10307. templates. <code>version</code> defines the version of the policy which will be served
  10308. to the user. In the example above, Synapse will serve
  10309. <code>privacy_policy_templates/en/1.0.html</code>.</p>
  10310. </li>
  10311. <li>
  10312. <p>Add a <code>form_secret</code> setting at the top level:</p>
  10313. <pre><code class="language-yaml">form_secret: &quot;&lt;unique secret&gt;&quot;
  10314. </code></pre>
  10315. <p>This should be set to an arbitrary secret string (try <code>pwgen -y 30</code> to
  10316. generate suitable secrets).</p>
  10317. <p>More on what this is used for below.</p>
  10318. </li>
  10319. <li>
  10320. <p>Add <code>consent</code> wherever the <code>client</code> resource is currently enabled in the
  10321. <code>listeners</code> configuration. For example:</p>
  10322. <pre><code class="language-yaml">listeners:
  10323. - port: 8008
  10324. resources:
  10325. - names:
  10326. - client
  10327. - consent
  10328. </code></pre>
  10329. </li>
  10330. </ol>
  10331. <p>Finally, ensure that <code>jinja2</code> is installed. If you are using a virtualenv, this
  10332. should be a matter of <code>pip install Jinja2</code>. On debian, try <code>apt-get install python-jinja2</code>.</p>
  10333. <p>Once this is complete, and the server has been restarted, try visiting
  10334. <code>https://&lt;server&gt;/_matrix/consent</code>. If correctly configured, this should give
  10335. an error &quot;Missing string query parameter 'u'&quot;. It is now possible to manually
  10336. construct URIs where users can give their consent.</p>
  10337. <h3 id="enabling-consent-tracking-at-registration"><a class="header" href="#enabling-consent-tracking-at-registration">Enabling consent tracking at registration</a></h3>
  10338. <ol>
  10339. <li>
  10340. <p>Add the following to your configuration:</p>
  10341. <pre><code class="language-yaml">user_consent:
  10342. require_at_registration: true
  10343. policy_name: &quot;Privacy Policy&quot; # or whatever you'd like to call the policy
  10344. </code></pre>
  10345. </li>
  10346. <li>
  10347. <p>In your consent templates, make use of the <code>public_version</code> variable to
  10348. see if an unauthenticated user is viewing the page. This is typically
  10349. wrapped around the form that would be used to actually agree to the document:</p>
  10350. <pre><code class="language-html">{% if not public_version %}
  10351. &lt;!-- The variables used here are only provided when the 'u' param is given to the homeserver --&gt;
  10352. &lt;form method=&quot;post&quot; action=&quot;consent&quot;&gt;
  10353. &lt;input type=&quot;hidden&quot; name=&quot;v&quot; value=&quot;{{version}}&quot;/&gt;
  10354. &lt;input type=&quot;hidden&quot; name=&quot;u&quot; value=&quot;{{user}}&quot;/&gt;
  10355. &lt;input type=&quot;hidden&quot; name=&quot;h&quot; value=&quot;{{userhmac}}&quot;/&gt;
  10356. &lt;input type=&quot;submit&quot; value=&quot;Sure thing!&quot;/&gt;
  10357. &lt;/form&gt;
  10358. {% endif %}
  10359. </code></pre>
  10360. </li>
  10361. <li>
  10362. <p>Restart Synapse to apply the changes.</p>
  10363. </li>
  10364. </ol>
  10365. <p>Visiting <code>https://&lt;server&gt;/_matrix/consent</code> should now give you a view of the privacy
  10366. document. This is what users will be able to see when registering for accounts.</p>
  10367. <h3 id="constructing-the-consent-uri"><a class="header" href="#constructing-the-consent-uri">Constructing the consent URI</a></h3>
  10368. <p>It may be useful to manually construct the &quot;consent URI&quot; for a given user - for
  10369. instance, in order to send them an email asking them to consent. To do this,
  10370. take the base <code>https://&lt;server&gt;/_matrix/consent</code> URL and add the following
  10371. query parameters:</p>
  10372. <ul>
  10373. <li>
  10374. <p><code>u</code>: the user id of the user. This can either be a full MXID
  10375. (<code>@user:server.com</code>) or just the localpart (<code>user</code>).</p>
  10376. </li>
  10377. <li>
  10378. <p><code>h</code>: hex-encoded HMAC-SHA256 of <code>u</code> using the <code>form_secret</code> as a key. It is
  10379. possible to calculate this on the commandline with something like:</p>
  10380. <pre><code class="language-bash">echo -n '&lt;user&gt;' | openssl sha256 -hmac '&lt;form_secret&gt;'
  10381. </code></pre>
  10382. <p>This should result in a URI which looks something like:
  10383. <code>https://&lt;server&gt;/_matrix/consent?u=&lt;user&gt;&amp;h=68a152465a4d...</code>.</p>
  10384. </li>
  10385. </ul>
  10386. <p>Note that not providing a <code>u</code> parameter will be interpreted as wanting to view
  10387. the document from an unauthenticated perspective, such as prior to registration.
  10388. Therefore, the <code>h</code> parameter is not required in this scenario. To enable this
  10389. behaviour, set <code>require_at_registration</code> to <code>true</code> in your <code>user_consent</code> config.</p>
  10390. <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>
  10391. <p>It is possible to configure Synapse to send a <a href="server_notices.html">server
  10392. notice</a> to anybody who has not yet agreed to the current
  10393. version of the policy. To do so:</p>
  10394. <ul>
  10395. <li>
  10396. <p>ensure that the consent resource is configured, as in the previous section</p>
  10397. </li>
  10398. <li>
  10399. <p>ensure that server notices are configured, as in <a href="server_notices.html">the server notice documentation</a>.</p>
  10400. </li>
  10401. <li>
  10402. <p>Add <code>server_notice_content</code> under <code>user_consent</code> in <code>homeserver.yaml</code>. For
  10403. example:</p>
  10404. <pre><code class="language-yaml">user_consent:
  10405. server_notice_content:
  10406. msgtype: m.text
  10407. body: &gt;-
  10408. Please give your consent to the privacy policy at %(consent_uri)s.
  10409. </code></pre>
  10410. <p>Synapse automatically replaces the placeholder <code>%(consent_uri)s</code> with the
  10411. consent uri for that user.</p>
  10412. </li>
  10413. <li>
  10414. <p>ensure that <code>public_baseurl</code> is set in <code>homeserver.yaml</code>, and gives the base
  10415. URI that clients use to connect to the server. (It is used to construct
  10416. <code>consent_uri</code> in the server notice.)</p>
  10417. </li>
  10418. </ul>
  10419. <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>
  10420. <p>Synapse can be configured to block any attempts to join rooms or send messages
  10421. until the user has given their agreement to the policy. (Joining the server
  10422. notices room is exempted from this).</p>
  10423. <p>To enable this, add <code>block_events_error</code> under <code>user_consent</code>. For example:</p>
  10424. <pre><code class="language-yaml">user_consent:
  10425. block_events_error: &gt;-
  10426. You can't send any messages until you consent to the privacy policy at
  10427. %(consent_uri)s.
  10428. </code></pre>
  10429. <p>Synapse automatically replaces the placeholder <code>%(consent_uri)s</code> with the
  10430. consent uri for that user.</p>
  10431. <p>ensure that <code>public_baseurl</code> is set in <code>homeserver.yaml</code>, and gives the base
  10432. URI that clients use to connect to the server. (It is used to construct
  10433. <code>consent_uri</code> in the error.)</p>
  10434. <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>
  10435. <p>The <code>GET /_matrix/media/r0/preview_url</code> endpoint provides a generic preview API
  10436. for URLs which outputs <a href="https://ogp.me/">Open Graph</a> responses (with some Matrix
  10437. specific additions).</p>
  10438. <p>This does have trade-offs compared to other designs:</p>
  10439. <ul>
  10440. <li>Pros:
  10441. <ul>
  10442. <li>Simple and flexible; can be used by any clients at any point</li>
  10443. </ul>
  10444. </li>
  10445. <li>Cons:
  10446. <ul>
  10447. <li>If each homeserver provides one of these independently, all the HSes in a
  10448. room may needlessly DoS the target URI</li>
  10449. <li>The URL metadata must be stored somewhere, rather than just using Matrix
  10450. itself to store the media.</li>
  10451. <li>Matrix cannot be used to distribute the metadata between homeservers.</li>
  10452. </ul>
  10453. </li>
  10454. </ul>
  10455. <p>When Synapse is asked to preview a URL it does the following:</p>
  10456. <ol>
  10457. <li>Checks against a URL blacklist (defined as <code>url_preview_url_blacklist</code> in the
  10458. config).</li>
  10459. <li>Checks the in-memory cache by URLs and returns the result if it exists. (This
  10460. is also used to de-duplicate processing of multiple in-flight requests at once.)</li>
  10461. <li>Kicks off a background process to generate a preview:
  10462. <ol>
  10463. <li>Checks the database cache by URL and timestamp and returns the result if it
  10464. has not expired and was successful (a 2xx return code).</li>
  10465. <li>Checks if the URL matches an <a href="https://oembed.com/">oEmbed</a> pattern. If it
  10466. does, update the URL to download.</li>
  10467. <li>Downloads the URL and stores it into a file via the media storage provider
  10468. and saves the local media metadata.</li>
  10469. <li>If the media is an image:
  10470. <ol>
  10471. <li>Generates thumbnails.</li>
  10472. <li>Generates an Open Graph response based on image properties.</li>
  10473. </ol>
  10474. </li>
  10475. <li>If the media is HTML:
  10476. <ol>
  10477. <li>Decodes the HTML via the stored file.</li>
  10478. <li>Generates an Open Graph response from the HTML.</li>
  10479. <li>If a JSON oEmbed URL was found in the HTML via autodiscovery:
  10480. <ol>
  10481. <li>Downloads the URL and stores it into a file via the media storage provider
  10482. and saves the local media metadata.</li>
  10483. <li>Convert the oEmbed response to an Open Graph response.</li>
  10484. <li>Override any Open Graph data from the HTML with data from oEmbed.</li>
  10485. </ol>
  10486. </li>
  10487. <li>If an image exists in the Open Graph response:
  10488. <ol>
  10489. <li>Downloads the URL and stores it into a file via the media storage
  10490. provider and saves the local media metadata.</li>
  10491. <li>Generates thumbnails.</li>
  10492. <li>Updates the Open Graph response based on image properties.</li>
  10493. </ol>
  10494. </li>
  10495. </ol>
  10496. </li>
  10497. <li>If the media is JSON and an oEmbed URL was found:
  10498. <ol>
  10499. <li>Convert the oEmbed response to an Open Graph response.</li>
  10500. <li>If a thumbnail or image is in the oEmbed response:
  10501. <ol>
  10502. <li>Downloads the URL and stores it into a file via the media storage
  10503. provider and saves the local media metadata.</li>
  10504. <li>Generates thumbnails.</li>
  10505. <li>Updates the Open Graph response based on image properties.</li>
  10506. </ol>
  10507. </li>
  10508. </ol>
  10509. </li>
  10510. <li>Stores the result in the database cache.</li>
  10511. </ol>
  10512. </li>
  10513. <li>Returns the result.</li>
  10514. </ol>
  10515. <p>The in-memory cache expires after 1 hour.</p>
  10516. <p>Expired entries in the database cache (and their associated media files) are
  10517. deleted every 10 seconds. The default expiration time is 1 hour from download.</p>
  10518. <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>
  10519. <p>The user directory is currently maintained based on the 'visible' users
  10520. on this particular server - i.e. ones which your account shares a room with, or
  10521. who are present in a publicly viewable room present on the server.</p>
  10522. <p>The directory info is stored in various tables, which can (typically after
  10523. DB corruption) get stale or out of sync. If this happens, for now the
  10524. solution to fix it is to use the <a href="usage/administration/admin_api/background_updates.html#run">admin API</a>
  10525. and execute the job <code>regenerate_directory</code>. This should then start a background task to
  10526. flush the current tables and regenerate the directory.</p>
  10527. <h2 id="data-model"><a class="header" href="#data-model">Data model</a></h2>
  10528. <p>There are five relevant tables that collectively form the &quot;user directory&quot;.
  10529. Three of them track a master list of all the users we could search for.
  10530. The last two (collectively called the &quot;search tables&quot;) track who can
  10531. see who.</p>
  10532. <p>From all of these tables we exclude three types of local user:</p>
  10533. <ul>
  10534. <li>support users</li>
  10535. <li>appservice users</li>
  10536. <li>deactivated users</li>
  10537. </ul>
  10538. <ul>
  10539. <li>
  10540. <p><code>user_directory</code>. This contains the user_id, display name and avatar we'll
  10541. return when you search the directory.</p>
  10542. <ul>
  10543. <li>Because there's only one directory entry per user, it's important that we only
  10544. ever put publicly visible names here. Otherwise we might leak a private
  10545. nickname or avatar used in a private room.</li>
  10546. <li>Indexed on rooms. Indexed on users.</li>
  10547. </ul>
  10548. </li>
  10549. <li>
  10550. <p><code>user_directory_search</code>. To be joined to <code>user_directory</code>. It contains an extra
  10551. column that enables full text search based on user ids and display names.
  10552. Different schemas for SQLite and Postgres with different code paths to match.</p>
  10553. <ul>
  10554. <li>Indexed on the full text search data. Indexed on users.</li>
  10555. </ul>
  10556. </li>
  10557. <li>
  10558. <p><code>user_directory_stream_pos</code>. When the initial background update to populate
  10559. the directory is complete, we record a stream position here. This indicates
  10560. that synapse should now listen for room changes and incrementally update
  10561. the directory where necessary.</p>
  10562. </li>
  10563. <li>
  10564. <p><code>users_in_public_rooms</code>. Contains associations between users and the public rooms they're in.
  10565. Used to determine which users are in public rooms and should be publicly visible in the directory.</p>
  10566. </li>
  10567. <li>
  10568. <p><code>users_who_share_private_rooms</code>. Rows are triples <code>(L, M, room id)</code> where <code>L</code>
  10569. is a local user and <code>M</code> is a local or remote user. <code>L</code> and <code>M</code> should be
  10570. different, but this isn't enforced by a constraint.</p>
  10571. </li>
  10572. </ul>
  10573. <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>
  10574. <p>Synapse admins can enable support for message retention policies on
  10575. their homeserver. Message retention policies exist at a room level,
  10576. follow the semantics described in
  10577. <a href="https://github.com/matrix-org/matrix-doc/blob/matthew/msc1763/proposals/1763-configurable-retention-periods.md">MSC1763</a>,
  10578. and allow server and room admins to configure how long messages should
  10579. be kept in a homeserver's database before being purged from it.
  10580. <strong>Please note that, as this feature isn't part of the Matrix
  10581. specification yet, this implementation is to be considered as
  10582. experimental.</strong> </p>
  10583. <p>A message retention policy is mainly defined by its <code>max_lifetime</code>
  10584. parameter, which defines how long a message can be kept around after
  10585. it was sent to the room. If a room doesn't have a message retention
  10586. policy, and there's no default one for a given server, then no message
  10587. sent in that room is ever purged on that server.</p>
  10588. <p>MSC1763 also specifies semantics for a <code>min_lifetime</code> parameter which
  10589. defines the amount of time after which an event <em>can</em> get purged (after
  10590. it was sent to the room), but Synapse doesn't currently support it
  10591. beyond registering it.</p>
  10592. <p>Both <code>max_lifetime</code> and <code>min_lifetime</code> are optional parameters.</p>
  10593. <p>Note that message retention policies don't apply to state events.</p>
  10594. <p>Once an event reaches its expiry date (defined as the time it was sent
  10595. plus the value for <code>max_lifetime</code> in the room), two things happen:</p>
  10596. <ul>
  10597. <li>Synapse stops serving the event to clients via any endpoint.</li>
  10598. <li>The message gets picked up by the next purge job (see the &quot;Purge jobs&quot;
  10599. section) and is removed from Synapse's database.</li>
  10600. </ul>
  10601. <p>Since purge jobs don't run continuously, this means that an event might
  10602. stay in a server's database for longer than the value for <code>max_lifetime</code>
  10603. in the room would allow, though hidden from clients.</p>
  10604. <p>Similarly, if a server (with support for message retention policies
  10605. enabled) receives from another server an event that should have been
  10606. purged according to its room's policy, then the receiving server will
  10607. process and store that event until it's picked up by the next purge job,
  10608. though it will always hide it from clients.</p>
  10609. <p>Synapse requires at least one message in each room, so it will never
  10610. delete the last message in a room. It will, however, hide it from
  10611. clients.</p>
  10612. <h2 id="server-configuration"><a class="header" href="#server-configuration">Server configuration</a></h2>
  10613. <p>Support for this feature can be enabled and configured in the
  10614. <code>retention</code> section of the Synapse configuration file (see the
  10615. <a href="https://github.com/matrix-org/synapse/blob/v1.36.0/docs/sample_config.yaml#L451-L518">sample file</a>).</p>
  10616. <p>To enable support for message retention policies, set the setting
  10617. <code>enabled</code> in this section to <code>true</code>.</p>
  10618. <h3 id="default-policy"><a class="header" href="#default-policy">Default policy</a></h3>
  10619. <p>A default message retention policy is a policy defined in Synapse's
  10620. configuration that is used by Synapse for every room that doesn't have a
  10621. message retention policy configured in its state. This allows server
  10622. admins to ensure that messages are never kept indefinitely in a server's
  10623. database. </p>
  10624. <p>A default policy can be defined as such, in the <code>retention</code> section of
  10625. the configuration file:</p>
  10626. <pre><code class="language-yaml">default_policy:
  10627. min_lifetime: 1d
  10628. max_lifetime: 1y
  10629. </code></pre>
  10630. <p>Here, <code>min_lifetime</code> and <code>max_lifetime</code> have the same meaning and level
  10631. of support as previously described. They can be expressed either as a
  10632. duration (using the units <code>s</code> (seconds), <code>m</code> (minutes), <code>h</code> (hours),
  10633. <code>d</code> (days), <code>w</code> (weeks) and <code>y</code> (years)) or as a number of milliseconds.</p>
  10634. <h3 id="purge-jobs"><a class="header" href="#purge-jobs">Purge jobs</a></h3>
  10635. <p>Purge jobs are the jobs that Synapse runs in the background to purge
  10636. expired events from the database. They are only run if support for
  10637. message retention policies is enabled in the server's configuration. If
  10638. no configuration for purge jobs is configured by the server admin,
  10639. Synapse will use a default configuration, which is described in the
  10640. <a href="https://github.com/matrix-org/synapse/blob/v1.36.0/docs/sample_config.yaml#L451-L518">sample configuration file</a>.</p>
  10641. <p>Some server admins might want a finer control on when events are removed
  10642. depending on an event's room's policy. This can be done by setting the
  10643. <code>purge_jobs</code> sub-section in the <code>retention</code> section of the configuration
  10644. file. An example of such configuration could be:</p>
  10645. <pre><code class="language-yaml">purge_jobs:
  10646. - longest_max_lifetime: 3d
  10647. interval: 12h
  10648. - shortest_max_lifetime: 3d
  10649. longest_max_lifetime: 1w
  10650. interval: 1d
  10651. - shortest_max_lifetime: 1w
  10652. interval: 2d
  10653. </code></pre>
  10654. <p>In this example, we define three jobs:</p>
  10655. <ul>
  10656. <li>one that runs twice a day (every 12 hours) and purges events in rooms
  10657. which policy's <code>max_lifetime</code> is lower or equal to 3 days.</li>
  10658. <li>one that runs once a day and purges events in rooms which policy's
  10659. <code>max_lifetime</code> is between 3 days and a week.</li>
  10660. <li>one that runs once every 2 days and purges events in rooms which
  10661. policy's <code>max_lifetime</code> is greater than a week.</li>
  10662. </ul>
  10663. <p>Note that this example is tailored to show different configurations and
  10664. features slightly more jobs than it's probably necessary (in practice, a
  10665. server admin would probably consider it better to replace the two last
  10666. jobs with one that runs once a day and handles rooms which which
  10667. policy's <code>max_lifetime</code> is greater than 3 days).</p>
  10668. <p>Keep in mind, when configuring these jobs, that a purge job can become
  10669. quite heavy on the server if it targets many rooms, therefore prefer
  10670. having jobs with a low interval that target a limited set of rooms. Also
  10671. make sure to include a job with no minimum and one with no maximum to
  10672. make sure your configuration handles every policy.</p>
  10673. <p>As previously mentioned in this documentation, while a purge job that
  10674. runs e.g. every day means that an expired event might stay in the
  10675. database for up to a day after its expiry, Synapse hides expired events
  10676. from clients as soon as they expire, so the event is not visible to
  10677. local users between its expiry date and the moment it gets purged from
  10678. the server's database.</p>
  10679. <h3 id="lifetime-limits"><a class="header" href="#lifetime-limits">Lifetime limits</a></h3>
  10680. <p>Server admins can set limits on the values of <code>max_lifetime</code> to use when
  10681. purging old events in a room. These limits can be defined as such in the
  10682. <code>retention</code> section of the configuration file:</p>
  10683. <pre><code class="language-yaml">allowed_lifetime_min: 1d
  10684. allowed_lifetime_max: 1y
  10685. </code></pre>
  10686. <p>The limits are considered when running purge jobs. If necessary, the
  10687. effective value of <code>max_lifetime</code> will be brought between
  10688. <code>allowed_lifetime_min</code> and <code>allowed_lifetime_max</code> (inclusive).
  10689. This means that, if the value of <code>max_lifetime</code> defined in the room's state
  10690. is lower than <code>allowed_lifetime_min</code>, the value of <code>allowed_lifetime_min</code>
  10691. will be used instead. Likewise, if the value of <code>max_lifetime</code> is higher
  10692. than <code>allowed_lifetime_max</code>, the value of <code>allowed_lifetime_max</code> will be
  10693. used instead.</p>
  10694. <p>In the example above, we ensure Synapse never deletes events that are less
  10695. than one day old, and that it always deletes events that are over a year
  10696. old.</p>
  10697. <p>If a default policy is set, and its <code>max_lifetime</code> value is lower than
  10698. <code>allowed_lifetime_min</code> or higher than <code>allowed_lifetime_max</code>, the same
  10699. process applies.</p>
  10700. <p>Both parameters are optional; if one is omitted Synapse won't use it to
  10701. adjust the effective value of <code>max_lifetime</code>.</p>
  10702. <p>Like other settings in this section, these parameters can be expressed
  10703. either as a duration or as a number of milliseconds.</p>
  10704. <h2 id="room-configuration"><a class="header" href="#room-configuration">Room configuration</a></h2>
  10705. <p>To configure a room's message retention policy, a room's admin or
  10706. moderator needs to send a state event in that room with the type
  10707. <code>m.room.retention</code> and the following content:</p>
  10708. <pre><code class="language-json">{
  10709. &quot;max_lifetime&quot;: ...
  10710. }
  10711. </code></pre>
  10712. <p>In this event's content, the <code>max_lifetime</code> parameter has the same
  10713. meaning as previously described, and needs to be expressed in
  10714. milliseconds. The event's content can also include a <code>min_lifetime</code>
  10715. parameter, which has the same meaning and limited support as previously
  10716. described.</p>
  10717. <p>Note that over every server in the room, only the ones with support for
  10718. message retention policies will actually remove expired events. This
  10719. support is currently not enabled by default in Synapse.</p>
  10720. <h2 id="note-on-reclaiming-disk-space"><a class="header" href="#note-on-reclaiming-disk-space">Note on reclaiming disk space</a></h2>
  10721. <p>While purge jobs actually delete data from the database, the disk space
  10722. used by the database might not decrease immediately on the database's
  10723. host. However, even though the database engine won't free up the disk
  10724. space, it will start writing new data into where the purged data was.</p>
  10725. <p>If you want to reclaim the freed disk space anyway and return it to the
  10726. operating system, the server admin needs to run <code>VACUUM FULL;</code> (or
  10727. <code>VACUUM;</code> for SQLite databases) on Synapse's database (see the related
  10728. <a href="https://www.postgresql.org/docs/current/sql-vacuum.html">PostgreSQL documentation</a>).</p>
  10729. <div style="break-before: page; page-break-before: always;"></div><h1 id="modules-1"><a class="header" href="#modules-1">Modules</a></h1>
  10730. <p>Synapse supports extending its functionality by configuring external modules.</p>
  10731. <p><strong>Note</strong>: When using third-party modules, you effectively allow someone else to run
  10732. custom code on your Synapse homeserver. Server admins are encouraged to verify the
  10733. provenance of the modules they use on their homeserver and make sure the modules aren't
  10734. running malicious code on their instance.</p>
  10735. <h2 id="using-modules"><a class="header" href="#using-modules">Using modules</a></h2>
  10736. <p>To use a module on Synapse, add it to the <code>modules</code> section of the configuration file:</p>
  10737. <pre><code class="language-yaml">modules:
  10738. - module: my_super_module.MySuperClass
  10739. config:
  10740. do_thing: true
  10741. - module: my_other_super_module.SomeClass
  10742. config: {}
  10743. </code></pre>
  10744. <p>Each module is defined by a path to a Python class as well as a configuration. This
  10745. information for a given module should be available in the module's own documentation.</p>
  10746. <h2 id="using-multiple-modules"><a class="header" href="#using-multiple-modules">Using multiple modules</a></h2>
  10747. <p>The order in which modules are listed in this section is important. When processing an
  10748. action that can be handled by several modules, Synapse will always prioritise the module
  10749. that appears first (i.e. is the highest in the list). This means:</p>
  10750. <ul>
  10751. <li>If several modules register the same callback, the callback registered by the module
  10752. that appears first is used.</li>
  10753. <li>If several modules try to register a handler for the same HTTP path, only the handler
  10754. registered by the module that appears first is used. Handlers registered by the other
  10755. module(s) are ignored and Synapse will log a warning message about them.</li>
  10756. </ul>
  10757. <p>Note that Synapse doesn't allow multiple modules implementing authentication checkers via
  10758. the password auth provider feature for the same login type with different fields. If this
  10759. happens, Synapse will refuse to start.</p>
  10760. <h2 id="current-status"><a class="header" href="#current-status">Current status</a></h2>
  10761. <p>We are currently in the process of migrating module interfaces to this system. While some
  10762. interfaces might be compatible with it, others still require configuring modules in
  10763. another part of Synapse's configuration file.</p>
  10764. <p>Currently, only the following pre-existing interfaces are compatible with this new system:</p>
  10765. <ul>
  10766. <li>spam checker</li>
  10767. <li>third-party rules</li>
  10768. <li>presence router</li>
  10769. <li>password auth providers</li>
  10770. </ul>
  10771. <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>
  10772. <p>A module is a Python class that uses Synapse's module API to interact with the
  10773. homeserver. It can register callbacks that Synapse will call on specific operations, as
  10774. well as web resources to attach to Synapse's web server.</p>
  10775. <p>When instantiated, a module is given its parsed configuration as well as an instance of
  10776. the <code>synapse.module_api.ModuleApi</code> class. The configuration is a dictionary, and is
  10777. either the output of the module's <code>parse_config</code> static method (see below), or the
  10778. configuration associated with the module in Synapse's configuration file.</p>
  10779. <p>See the documentation for the <code>ModuleApi</code> class
  10780. <a href="https://github.com/matrix-org/synapse/blob/master/synapse/module_api/__init__.py">here</a>.</p>
  10781. <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>
  10782. <p>If Synapse is running with other modules configured, the order each module appears in
  10783. within the <code>modules</code> section of the Synapse configuration file might restrict what it can
  10784. or cannot register. See <a href="modules/index.html#using-multiple-modules">this section</a> for more
  10785. information.</p>
  10786. <p>On top of the rules listed in the link above, if a callback returns a value that should
  10787. cause the current operation to fail (e.g. if a callback checking an event returns with a
  10788. value that should cause the event to be denied), Synapse will fail the operation and
  10789. ignore any subsequent callbacks that should have been run after this one.</p>
  10790. <p>The documentation for each callback mentions how Synapse behaves when
  10791. multiple modules implement it.</p>
  10792. <h2 id="handling-the-modules-configuration"><a class="header" href="#handling-the-modules-configuration">Handling the module's configuration</a></h2>
  10793. <p>A module can implement the following static method:</p>
  10794. <pre><code class="language-python">@staticmethod
  10795. def parse_config(config: dict) -&gt; Any
  10796. </code></pre>
  10797. <p>This method is given a dictionary resulting from parsing the YAML configuration for the
  10798. module. It may modify it (for example by parsing durations expressed as strings (e.g.
  10799. &quot;5d&quot;) into milliseconds, etc.), and return the modified dictionary. It may also verify
  10800. that the configuration is correct, and raise an instance of
  10801. <code>synapse.module_api.errors.ConfigError</code> if not.</p>
  10802. <h2 id="registering-a-web-resource"><a class="header" href="#registering-a-web-resource">Registering a web resource</a></h2>
  10803. <p>Modules can register web resources onto Synapse's web server using the following module
  10804. API method:</p>
  10805. <pre><code class="language-python">def ModuleApi.register_web_resource(path: str, resource: IResource) -&gt; None
  10806. </code></pre>
  10807. <p>The path is the full absolute path to register the resource at. For example, if you
  10808. register a resource for the path <code>/_synapse/client/my_super_module/say_hello</code>, Synapse
  10809. will serve it at <code>http(s)://[HS_URL]/_synapse/client/my_super_module/say_hello</code>. Note
  10810. that Synapse does not allow registering resources for several sub-paths in the <code>/_matrix</code>
  10811. namespace (such as anything under <code>/_matrix/client</code> for example). It is strongly
  10812. recommended that modules register their web resources under the <code>/_synapse/client</code>
  10813. namespace.</p>
  10814. <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>
  10815. interface (such as <a href="https://twistedmatrix.com/documents/current/api/twisted.web.resource.Resource.html">Resource</a>).</p>
  10816. <p>Only one resource can be registered for a given path. If several modules attempt to
  10817. register a resource for the same path, the module that appears first in Synapse's
  10818. configuration file takes priority.</p>
  10819. <p>Modules <strong>must</strong> register their web resources in their <code>__init__</code> method.</p>
  10820. <h2 id="registering-a-callback"><a class="header" href="#registering-a-callback">Registering a callback</a></h2>
  10821. <p>Modules can use Synapse's module API to register callbacks. Callbacks are functions that
  10822. Synapse will call when performing specific actions. Callbacks must be asynchronous (unless
  10823. specified otherwise), and are split in categories. A single module may implement callbacks
  10824. from multiple categories, and is under no obligation to implement all callbacks from the
  10825. categories it registers callbacks for.</p>
  10826. <p>Modules can register callbacks using one of the module API's <code>register_[...]_callbacks</code>
  10827. methods. The callback functions are passed to these methods as keyword arguments, with
  10828. the callback name as the argument name and the function as its value. A
  10829. <code>register_[...]_callbacks</code> method exists for each category.</p>
  10830. <p>Callbacks for each category can be found on their respective page of the
  10831. <a href="https://matrix-org.github.io/synapse">Synapse documentation website</a>.</p>
  10832. <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>
  10833. <p>Spam checker callbacks allow module developers to implement spam mitigation actions for
  10834. Synapse instances. Spam checker callbacks can be registered using the module API's
  10835. <code>register_spam_checker_callbacks</code> method.</p>
  10836. <h2 id="callbacks"><a class="header" href="#callbacks">Callbacks</a></h2>
  10837. <p>The available spam checker callbacks are:</p>
  10838. <h3 id="check_event_for_spam"><a class="header" href="#check_event_for_spam"><code>check_event_for_spam</code></a></h3>
  10839. <p><em>First introduced in Synapse v1.37.0</em></p>
  10840. <p><em>Changed in Synapse v1.60.0: <code>synapse.module_api.NOT_SPAM</code> and <code>synapse.module_api.errors.Codes</code> can be returned by this callback. Returning a boolean or a string is now deprecated.</em> </p>
  10841. <pre><code class="language-python">async def check_event_for_spam(event: &quot;synapse.module_api.EventBase&quot;) -&gt; Union[&quot;synapse.module_api.NOT_SPAM&quot;, &quot;synapse.module_api.errors.Codes&quot;, str, bool]
  10842. </code></pre>
  10843. <p>Called when receiving an event from a client or via federation. The callback must return one of:</p>
  10844. <ul>
  10845. <li><code>synapse.module_api.NOT_SPAM</code>, to allow the operation. Other callbacks may still
  10846. decide to reject it.</li>
  10847. <li><code>synapse.module_api.errors.Codes</code> to reject the operation with an error code. In case
  10848. of doubt, <code>synapse.module_api.errors.Codes.FORBIDDEN</code> is a good error code.</li>
  10849. <li>(deprecated) a non-<code>Codes</code> <code>str</code> to reject the operation and specify an error message. Note that clients
  10850. typically will not localize the error message to the user's preferred locale.</li>
  10851. <li>(deprecated) <code>False</code>, which is the same as returning <code>synapse.module_api.NOT_SPAM</code>.</li>
  10852. <li>(deprecated) <code>True</code>, which is the same as returning <code>synapse.module_api.errors.Codes.FORBIDDEN</code>.</li>
  10853. </ul>
  10854. <p>If multiple modules implement this callback, they will be considered in order. If a
  10855. callback returns <code>synapse.module_api.NOT_SPAM</code>, Synapse falls through to the next one.
  10856. The value of the first callback that does not return <code>synapse.module_api.NOT_SPAM</code> will
  10857. be used. If this happens, Synapse will not call any of the subsequent implementations of
  10858. this callback.</p>
  10859. <h3 id="user_may_join_room"><a class="header" href="#user_may_join_room"><code>user_may_join_room</code></a></h3>
  10860. <p><em>First introduced in Synapse v1.37.0</em></p>
  10861. <pre><code class="language-python">async def user_may_join_room(user: str, room: str, is_invited: bool) -&gt; bool
  10862. </code></pre>
  10863. <p>Called when a user is trying to join a room. The module must return a <code>bool</code> to indicate
  10864. whether the user can join the room. Return <code>False</code> to prevent the user from joining the
  10865. room; otherwise return <code>True</code> to permit the joining.</p>
  10866. <p>The user is represented by their Matrix user ID (e.g.
  10867. <code>@alice:example.com</code>) and the room is represented by its Matrix ID (e.g.
  10868. <code>!room:example.com</code>). The module is also given a boolean to indicate whether the user
  10869. currently has a pending invite in the room.</p>
  10870. <p>This callback isn't called if the join is performed by a server administrator, or in the
  10871. context of a room creation.</p>
  10872. <p>If multiple modules implement this callback, they will be considered in order. If a
  10873. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  10874. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  10875. any of the subsequent implementations of this callback.</p>
  10876. <h3 id="user_may_invite"><a class="header" href="#user_may_invite"><code>user_may_invite</code></a></h3>
  10877. <p><em>First introduced in Synapse v1.37.0</em></p>
  10878. <pre><code class="language-python">async def user_may_invite(inviter: str, invitee: str, room_id: str) -&gt; bool
  10879. </code></pre>
  10880. <p>Called when processing an invitation. The module must return a <code>bool</code> indicating whether
  10881. the inviter can invite the invitee to the given room. Both inviter and invitee are
  10882. represented by their Matrix user ID (e.g. <code>@alice:example.com</code>). Return <code>False</code> to prevent
  10883. the invitation; otherwise return <code>True</code> to permit it.</p>
  10884. <p>If multiple modules implement this callback, they will be considered in order. If a
  10885. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  10886. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  10887. any of the subsequent implementations of this callback.</p>
  10888. <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>
  10889. <p><em>First introduced in Synapse v1.45.0</em></p>
  10890. <pre><code class="language-python">async def user_may_send_3pid_invite(
  10891. inviter: str,
  10892. medium: str,
  10893. address: str,
  10894. room_id: str,
  10895. ) -&gt; bool
  10896. </code></pre>
  10897. <p>Called when processing an invitation using a third-party identifier (also called a 3PID,
  10898. e.g. an email address or a phone number). The module must return a <code>bool</code> indicating
  10899. whether the inviter can invite the invitee to the given room. Return <code>False</code> to prevent
  10900. the invitation; otherwise return <code>True</code> to permit it.</p>
  10901. <p>The inviter is represented by their Matrix user ID (e.g. <code>@alice:example.com</code>), and the
  10902. invitee is represented by its medium (e.g. &quot;email&quot;) and its address
  10903. (e.g. <code>alice@example.com</code>). See <a href="https://matrix.org/docs/spec/appendices#pid-types">the Matrix specification</a>
  10904. for more information regarding third-party identifiers.</p>
  10905. <p>For example, a call to this callback to send an invitation to the email address
  10906. <code>alice@example.com</code> would look like this:</p>
  10907. <pre><code class="language-python">await user_may_send_3pid_invite(
  10908. &quot;@bob:example.com&quot;, # The inviter's user ID
  10909. &quot;email&quot;, # The medium of the 3PID to invite
  10910. &quot;alice@example.com&quot;, # The address of the 3PID to invite
  10911. &quot;!some_room:example.com&quot;, # The ID of the room to send the invite into
  10912. )
  10913. </code></pre>
  10914. <p><strong>Note</strong>: If the third-party identifier is already associated with a matrix user ID,
  10915. <a href="modules/spam_checker_callbacks.html#user_may_invite"><code>user_may_invite</code></a> will be used instead.</p>
  10916. <p>If multiple modules implement this callback, they will be considered in order. If a
  10917. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  10918. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  10919. any of the subsequent implementations of this callback.</p>
  10920. <h3 id="user_may_create_room"><a class="header" href="#user_may_create_room"><code>user_may_create_room</code></a></h3>
  10921. <p><em>First introduced in Synapse v1.37.0</em></p>
  10922. <pre><code class="language-python">async def user_may_create_room(user: str) -&gt; bool
  10923. </code></pre>
  10924. <p>Called when processing a room creation request. The module must return a <code>bool</code> indicating
  10925. whether the given user (represented by their Matrix user ID) is allowed to create a room.
  10926. Return <code>False</code> to prevent room creation; otherwise return <code>True</code> to permit it.</p>
  10927. <p>If multiple modules implement this callback, they will be considered in order. If a
  10928. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  10929. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  10930. any of the subsequent implementations of this callback.</p>
  10931. <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>
  10932. <p><em>First introduced in Synapse v1.37.0</em></p>
  10933. <pre><code class="language-python">async def user_may_create_room_alias(user: str, room_alias: &quot;synapse.types.RoomAlias&quot;) -&gt; bool
  10934. </code></pre>
  10935. <p>Called when trying to associate an alias with an existing room. The module must return a
  10936. <code>bool</code> indicating whether the given user (represented by their Matrix user ID) is allowed
  10937. to set the given alias. Return <code>False</code> to prevent the alias creation; otherwise return
  10938. <code>True</code> to permit it.</p>
  10939. <p>If multiple modules implement this callback, they will be considered in order. If a
  10940. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  10941. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  10942. any of the subsequent implementations of this callback.</p>
  10943. <h3 id="user_may_publish_room"><a class="header" href="#user_may_publish_room"><code>user_may_publish_room</code></a></h3>
  10944. <p><em>First introduced in Synapse v1.37.0</em></p>
  10945. <pre><code class="language-python">async def user_may_publish_room(user: str, room_id: str) -&gt; bool
  10946. </code></pre>
  10947. <p>Called when trying to publish a room to the homeserver's public rooms directory. The
  10948. module must return a <code>bool</code> indicating whether the given user (represented by their
  10949. Matrix user ID) is allowed to publish the given room. Return <code>False</code> to prevent the
  10950. room from being published; otherwise return <code>True</code> to permit its publication.</p>
  10951. <p>If multiple modules implement this callback, they will be considered in order. If a
  10952. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  10953. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  10954. any of the subsequent implementations of this callback.</p>
  10955. <h3 id="check_username_for_spam"><a class="header" href="#check_username_for_spam"><code>check_username_for_spam</code></a></h3>
  10956. <p><em>First introduced in Synapse v1.37.0</em></p>
  10957. <pre><code class="language-python">async def check_username_for_spam(user_profile: synapse.module_api.UserProfile) -&gt; bool
  10958. </code></pre>
  10959. <p>Called when computing search results in the user directory. The module must return a
  10960. <code>bool</code> indicating whether the given user should be excluded from user directory
  10961. searches. Return <code>True</code> to indicate that the user is spammy and exclude them from
  10962. search results; otherwise return <code>False</code>.</p>
  10963. <p>The profile is represented as a dictionary with the following keys:</p>
  10964. <ul>
  10965. <li><code>user_id: str</code>. The Matrix ID for this user.</li>
  10966. <li><code>display_name: Optional[str]</code>. The user's display name, or <code>None</code> if this user
  10967. has not set a display name.</li>
  10968. <li><code>avatar_url: Optional[str]</code>. The <code>mxc://</code> URL to the user's avatar, or <code>None</code>
  10969. if this user has not set an avatar.</li>
  10970. </ul>
  10971. <p>The module is given a copy of the original dictionary, so modifying it from within the
  10972. module cannot modify a user's profile when included in user directory search results.</p>
  10973. <p>If multiple modules implement this callback, they will be considered in order. If a
  10974. callback returns <code>False</code>, Synapse falls through to the next one. The value of the first
  10975. callback that does not return <code>False</code> will be used. If this happens, Synapse will not call
  10976. any of the subsequent implementations of this callback.</p>
  10977. <h3 id="check_registration_for_spam"><a class="header" href="#check_registration_for_spam"><code>check_registration_for_spam</code></a></h3>
  10978. <p><em>First introduced in Synapse v1.37.0</em></p>
  10979. <pre><code class="language-python">async def check_registration_for_spam(
  10980. email_threepid: Optional[dict],
  10981. username: Optional[str],
  10982. request_info: Collection[Tuple[str, str]],
  10983. auth_provider_id: Optional[str] = None,
  10984. ) -&gt; &quot;synapse.spam_checker_api.RegistrationBehaviour&quot;
  10985. </code></pre>
  10986. <p>Called when registering a new user. The module must return a <code>RegistrationBehaviour</code>
  10987. indicating whether the registration can go through or must be denied, or whether the user
  10988. may be allowed to register but will be shadow banned.</p>
  10989. <p>The arguments passed to this callback are:</p>
  10990. <ul>
  10991. <li><code>email_threepid</code>: The email address used for registering, if any.</li>
  10992. <li><code>username</code>: The username the user would like to register. Can be <code>None</code>, meaning that
  10993. Synapse will generate one later.</li>
  10994. <li><code>request_info</code>: A collection of tuples, which first item is a user agent, and which
  10995. second item is an IP address. These user agents and IP addresses are the ones that were
  10996. used during the registration process.</li>
  10997. <li><code>auth_provider_id</code>: The identifier of the SSO authentication provider, if any.</li>
  10998. </ul>
  10999. <p>If multiple modules implement this callback, they will be considered in order. If a
  11000. callback returns <code>RegistrationBehaviour.ALLOW</code>, Synapse falls through to the next one.
  11001. The value of the first callback that does not return <code>RegistrationBehaviour.ALLOW</code> will
  11002. be used. If this happens, Synapse will not call any of the subsequent implementations of
  11003. this callback.</p>
  11004. <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>
  11005. <p><em>First introduced in Synapse v1.37.0</em></p>
  11006. <pre><code class="language-python">async def check_media_file_for_spam(
  11007. file_wrapper: &quot;synapse.rest.media.v1.media_storage.ReadableFileWrapper&quot;,
  11008. file_info: &quot;synapse.rest.media.v1._base.FileInfo&quot;,
  11009. ) -&gt; bool
  11010. </code></pre>
  11011. <p>Called when storing a local or remote file. The module must return a <code>bool</code> indicating
  11012. whether the given file should be excluded from the homeserver's media store. Return
  11013. <code>True</code> to prevent this file from being stored; otherwise return <code>False</code>.</p>
  11014. <p>If multiple modules implement this callback, they will be considered in order. If a
  11015. callback returns <code>False</code>, Synapse falls through to the next one. The value of the first
  11016. callback that does not return <code>False</code> will be used. If this happens, Synapse will not call
  11017. any of the subsequent implementations of this callback.</p>
  11018. <h3 id="should_drop_federated_event"><a class="header" href="#should_drop_federated_event"><code>should_drop_federated_event</code></a></h3>
  11019. <p><em>First introduced in Synapse v1.60.0</em></p>
  11020. <pre><code class="language-python">async def should_drop_federated_event(event: &quot;synapse.events.EventBase&quot;) -&gt; bool
  11021. </code></pre>
  11022. <p>Called when checking whether a remote server can federate an event with us. <strong>Returning
  11023. <code>True</code> from this function will silently drop a federated event and split-brain our view
  11024. of a room's DAG, and thus you shouldn't use this callback unless you know what you are
  11025. doing.</strong></p>
  11026. <p>If multiple modules implement this callback, they will be considered in order. If a
  11027. callback returns <code>False</code>, Synapse falls through to the next one. The value of the first
  11028. callback that does not return <code>False</code> will be used. If this happens, Synapse will not call
  11029. any of the subsequent implementations of this callback.</p>
  11030. <h2 id="example"><a class="header" href="#example">Example</a></h2>
  11031. <p>The example below is a module that implements the spam checker callback
  11032. <code>check_event_for_spam</code> to deny any message sent by users whose Matrix user IDs are
  11033. mentioned in a configured list, and registers a web resource to the path
  11034. <code>/_synapse/client/list_spam_checker/is_evil</code> that returns a JSON object indicating
  11035. whether the provided user appears in that list.</p>
  11036. <pre><code class="language-python">import json
  11037. from typing import Union
  11038. from twisted.web.resource import Resource
  11039. from twisted.web.server import Request
  11040. from synapse.module_api import ModuleApi
  11041. class IsUserEvilResource(Resource):
  11042. def __init__(self, config):
  11043. super(IsUserEvilResource, self).__init__()
  11044. self.evil_users = config.get(&quot;evil_users&quot;) or []
  11045. def render_GET(self, request: Request):
  11046. user = request.args.get(b&quot;user&quot;)[0].decode()
  11047. request.setHeader(b&quot;Content-Type&quot;, b&quot;application/json&quot;)
  11048. return json.dumps({&quot;evil&quot;: user in self.evil_users}).encode()
  11049. class ListSpamChecker:
  11050. def __init__(self, config: dict, api: ModuleApi):
  11051. self.api = api
  11052. self.evil_users = config.get(&quot;evil_users&quot;) or []
  11053. self.api.register_spam_checker_callbacks(
  11054. check_event_for_spam=self.check_event_for_spam,
  11055. )
  11056. self.api.register_web_resource(
  11057. path=&quot;/_synapse/client/list_spam_checker/is_evil&quot;,
  11058. resource=IsUserEvilResource(config),
  11059. )
  11060. async def check_event_for_spam(self, event: &quot;synapse.events.EventBase&quot;) -&gt; Union[bool, str]:
  11061. return event.sender not in self.evil_users
  11062. </code></pre>
  11063. <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>
  11064. <p>Third party rules callbacks allow module developers to add extra checks to verify the
  11065. validity of incoming events. Third party event rules callbacks can be registered using
  11066. the module API's <code>register_third_party_rules_callbacks</code> method.</p>
  11067. <h2 id="callbacks-1"><a class="header" href="#callbacks-1">Callbacks</a></h2>
  11068. <p>The available third party rules callbacks are:</p>
  11069. <h3 id="check_event_allowed"><a class="header" href="#check_event_allowed"><code>check_event_allowed</code></a></h3>
  11070. <p><em>First introduced in Synapse v1.39.0</em></p>
  11071. <pre><code class="language-python">async def check_event_allowed(
  11072. event: &quot;synapse.events.EventBase&quot;,
  11073. state_events: &quot;synapse.types.StateMap&quot;,
  11074. ) -&gt; Tuple[bool, Optional[dict]]
  11075. </code></pre>
  11076. <p><strong><span style="color:red">
  11077. This callback is very experimental and can and will break without notice. Module developers
  11078. are encouraged to implement <code>check_event_for_spam</code> from the spam checker category instead.
  11079. </span></strong></p>
  11080. <p>Called when processing any incoming event, with the event and a <code>StateMap</code>
  11081. representing the current state of the room the event is being sent into. A <code>StateMap</code> is
  11082. a dictionary that maps tuples containing an event type and a state key to the
  11083. corresponding state event. For example retrieving the room's <code>m.room.create</code> event from
  11084. the <code>state_events</code> argument would look like this: <code>state_events.get((&quot;m.room.create&quot;, &quot;&quot;))</code>.
  11085. The module must return a boolean indicating whether the event can be allowed.</p>
  11086. <p>Note that this callback function processes incoming events coming via federation
  11087. traffic (on top of client traffic). This means denying an event might cause the local
  11088. copy of the room's history to diverge from that of remote servers. This may cause
  11089. federation issues in the room. It is strongly recommended to only deny events using this
  11090. callback function if the sender is a local user, or in a private federation in which all
  11091. servers are using the same module, with the same configuration.</p>
  11092. <p>If the boolean returned by the module is <code>True</code>, it may also tell Synapse to replace the
  11093. event with new data by returning the new event's data as a dictionary. In order to do
  11094. that, it is recommended the module calls <code>event.get_dict()</code> to get the current event as a
  11095. dictionary, and modify the returned dictionary accordingly.</p>
  11096. <p>If <code>check_event_allowed</code> raises an exception, the module is assumed to have failed.
  11097. The event will not be accepted but is not treated as explicitly rejected, either.
  11098. An HTTP request causing the module check will likely result in a 500 Internal
  11099. Server Error.</p>
  11100. <p>When the boolean returned by the module is <code>False</code>, the event is rejected.
  11101. (Module developers should not use exceptions for rejection.)</p>
  11102. <p>Note that replacing the event only works for events sent by local users, not for events
  11103. received over federation.</p>
  11104. <p>If multiple modules implement this callback, they will be considered in order. If a
  11105. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  11106. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  11107. any of the subsequent implementations of this callback.</p>
  11108. <h3 id="on_create_room"><a class="header" href="#on_create_room"><code>on_create_room</code></a></h3>
  11109. <p><em>First introduced in Synapse v1.39.0</em></p>
  11110. <pre><code class="language-python">async def on_create_room(
  11111. requester: &quot;synapse.types.Requester&quot;,
  11112. request_content: dict,
  11113. is_requester_admin: bool,
  11114. ) -&gt; None
  11115. </code></pre>
  11116. <p>Called when processing a room creation request, with the <code>Requester</code> object for the user
  11117. performing the request, a dictionary representing the room creation request's JSON body
  11118. (see <a href="https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-createroom">the spec</a>
  11119. for a list of possible parameters), and a boolean indicating whether the user performing
  11120. the request is a server admin.</p>
  11121. <p>Modules can modify the <code>request_content</code> (by e.g. adding events to its <code>initial_state</code>),
  11122. or deny the room's creation by raising a <code>module_api.errors.SynapseError</code>.</p>
  11123. <p>If multiple modules implement this callback, they will be considered in order. If a
  11124. callback returns without raising an exception, Synapse falls through to the next one. The
  11125. room creation will be forbidden as soon as one of the callbacks raises an exception. If
  11126. this happens, Synapse will not call any of the subsequent implementations of this
  11127. callback.</p>
  11128. <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>
  11129. <p><em>First introduced in Synapse v1.39.0</em></p>
  11130. <pre><code class="language-python">async def check_threepid_can_be_invited(
  11131. medium: str,
  11132. address: str,
  11133. state_events: &quot;synapse.types.StateMap&quot;,
  11134. ) -&gt; bool:
  11135. </code></pre>
  11136. <p>Called when processing an invite via a third-party identifier (i.e. email or phone number).
  11137. The module must return a boolean indicating whether the invite can go through.</p>
  11138. <p>If multiple modules implement this callback, they will be considered in order. If a
  11139. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  11140. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  11141. any of the subsequent implementations of this callback.</p>
  11142. <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>
  11143. <p><em>First introduced in Synapse v1.39.0</em></p>
  11144. <pre><code class="language-python">async def check_visibility_can_be_modified(
  11145. room_id: str,
  11146. state_events: &quot;synapse.types.StateMap&quot;,
  11147. new_visibility: str,
  11148. ) -&gt; bool:
  11149. </code></pre>
  11150. <p>Called when changing the visibility of a room in the local public room directory. The
  11151. visibility is a string that's either &quot;public&quot; or &quot;private&quot;. The module must return a
  11152. boolean indicating whether the change can go through.</p>
  11153. <p>If multiple modules implement this callback, they will be considered in order. If a
  11154. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  11155. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  11156. any of the subsequent implementations of this callback.</p>
  11157. <h3 id="on_new_event"><a class="header" href="#on_new_event"><code>on_new_event</code></a></h3>
  11158. <p><em>First introduced in Synapse v1.47.0</em></p>
  11159. <pre><code class="language-python">async def on_new_event(
  11160. event: &quot;synapse.events.EventBase&quot;,
  11161. state_events: &quot;synapse.types.StateMap&quot;,
  11162. ) -&gt; None:
  11163. </code></pre>
  11164. <p>Called after sending an event into a room. The module is passed the event, as well
  11165. as the state of the room <em>after</em> the event. This means that if the event is a state event,
  11166. it will be included in this state.</p>
  11167. <p>Note that this callback is called when the event has already been processed and stored
  11168. into the room, which means this callback cannot be used to deny persisting the event. To
  11169. 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>
  11170. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  11171. <h3 id="check_can_shutdown_room"><a class="header" href="#check_can_shutdown_room"><code>check_can_shutdown_room</code></a></h3>
  11172. <p><em>First introduced in Synapse v1.55.0</em></p>
  11173. <pre><code class="language-python">async def check_can_shutdown_room(
  11174. user_id: str, room_id: str,
  11175. ) -&gt; bool:
  11176. </code></pre>
  11177. <p>Called when an admin user requests the shutdown of a room. The module must return a
  11178. boolean indicating whether the shutdown can go through. If the callback returns <code>False</code>,
  11179. the shutdown will not proceed and the caller will see a <code>M_FORBIDDEN</code> error.</p>
  11180. <p>If multiple modules implement this callback, they will be considered in order. If a
  11181. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  11182. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  11183. any of the subsequent implementations of this callback.</p>
  11184. <h3 id="check_can_deactivate_user"><a class="header" href="#check_can_deactivate_user"><code>check_can_deactivate_user</code></a></h3>
  11185. <p><em>First introduced in Synapse v1.55.0</em></p>
  11186. <pre><code class="language-python">async def check_can_deactivate_user(
  11187. user_id: str, by_admin: bool,
  11188. ) -&gt; bool:
  11189. </code></pre>
  11190. <p>Called when the deactivation of a user is requested. User deactivation can be
  11191. performed by an admin or the user themselves, so developers are encouraged to check the
  11192. requester when implementing this callback. The module must return a
  11193. boolean indicating whether the deactivation can go through. If the callback returns <code>False</code>,
  11194. the deactivation will not proceed and the caller will see a <code>M_FORBIDDEN</code> error.</p>
  11195. <p>The module is passed two parameters, <code>user_id</code> which is the ID of the user being deactivated, and <code>by_admin</code> which is <code>True</code> if the request is made by a serve admin, and <code>False</code> otherwise.</p>
  11196. <p>If multiple modules implement this callback, they will be considered in order. If a
  11197. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  11198. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  11199. any of the subsequent implementations of this callback.</p>
  11200. <h3 id="on_profile_update"><a class="header" href="#on_profile_update"><code>on_profile_update</code></a></h3>
  11201. <p><em>First introduced in Synapse v1.54.0</em></p>
  11202. <pre><code class="language-python">async def on_profile_update(
  11203. user_id: str,
  11204. new_profile: &quot;synapse.module_api.ProfileInfo&quot;,
  11205. by_admin: bool,
  11206. deactivation: bool,
  11207. ) -&gt; None:
  11208. </code></pre>
  11209. <p>Called after updating a local user's profile. The update can be triggered either by the
  11210. user themselves or a server admin. The update can also be triggered by a user being
  11211. deactivated (in which case their display name is set to an empty string (<code>&quot;&quot;</code>) and the
  11212. avatar URL is set to <code>None</code>). The module is passed the Matrix ID of the user whose profile
  11213. has been updated, their new profile, as well as a <code>by_admin</code> boolean that is <code>True</code> if the
  11214. update was triggered by a server admin (and <code>False</code> otherwise), and a <code>deactivated</code>
  11215. boolean that is <code>True</code> if the update is a result of the user being deactivated.</p>
  11216. <p>Note that the <code>by_admin</code> boolean is also <code>True</code> if the profile change happens as a result
  11217. of the user logging in through Single Sign-On, or if a server admin updates their own
  11218. profile.</p>
  11219. <p>Per-room profile changes do not trigger this callback to be called. Synapse administrators
  11220. wishing this callback to be called on every profile change are encouraged to disable
  11221. per-room profiles globally using the <code>allow_per_room_profiles</code> configuration setting in
  11222. Synapse's configuration file.
  11223. This callback is not called when registering a user, even when setting it through the
  11224. <a href="https://matrix-org.github.io/synapse/latest/modules/password_auth_provider_callbacks.html#get_displayname_for_registration"><code>get_displayname_for_registration</code></a>
  11225. module callback.</p>
  11226. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  11227. <h3 id="on_user_deactivation_status_changed"><a class="header" href="#on_user_deactivation_status_changed"><code>on_user_deactivation_status_changed</code></a></h3>
  11228. <p><em>First introduced in Synapse v1.54.0</em></p>
  11229. <pre><code class="language-python">async def on_user_deactivation_status_changed(
  11230. user_id: str, deactivated: bool, by_admin: bool
  11231. ) -&gt; None:
  11232. </code></pre>
  11233. <p>Called after deactivating a local user, or reactivating them through the admin API. The
  11234. deactivation can be triggered either by the user themselves or a server admin. The module
  11235. is passed the Matrix ID of the user whose status is changed, as well as a <code>deactivated</code>
  11236. boolean that is <code>True</code> if the user is being deactivated and <code>False</code> if they're being
  11237. reactivated, and a <code>by_admin</code> boolean that is <code>True</code> if the deactivation was triggered by
  11238. a server admin (and <code>False</code> otherwise). This latter <code>by_admin</code> boolean is always <code>True</code>
  11239. if the user is being reactivated, as this operation can only be performed through the
  11240. admin API.</p>
  11241. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  11242. <h3 id="on_threepid_bind"><a class="header" href="#on_threepid_bind"><code>on_threepid_bind</code></a></h3>
  11243. <p><em>First introduced in Synapse v1.56.0</em></p>
  11244. <pre><code class="language-python">async def on_threepid_bind(user_id: str, medium: str, address: str) -&gt; None:
  11245. </code></pre>
  11246. <p>Called after creating an association between a local user and a third-party identifier
  11247. (email address, phone number). The module is given the Matrix ID of the user the
  11248. association is for, as well as the medium (<code>email</code> or <code>msisdn</code>) and address of the
  11249. third-party identifier.</p>
  11250. <p>Note that this callback is <em>not</em> called after a successful association on an <em>identity
  11251. server</em>.</p>
  11252. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  11253. <h2 id="example-1"><a class="header" href="#example-1">Example</a></h2>
  11254. <p>The example below is a module that implements the third-party rules callback
  11255. <code>check_event_allowed</code> to censor incoming messages as dictated by a third-party service.</p>
  11256. <pre><code class="language-python">from typing import Optional, Tuple
  11257. from synapse.module_api import ModuleApi
  11258. _DEFAULT_CENSOR_ENDPOINT = &quot;https://my-internal-service.local/censor-event&quot;
  11259. class EventCensorer:
  11260. def __init__(self, config: dict, api: ModuleApi):
  11261. self.api = api
  11262. self._endpoint = config.get(&quot;endpoint&quot;, _DEFAULT_CENSOR_ENDPOINT)
  11263. self.api.register_third_party_rules_callbacks(
  11264. check_event_allowed=self.check_event_allowed,
  11265. )
  11266. async def check_event_allowed(
  11267. self,
  11268. event: &quot;synapse.events.EventBase&quot;,
  11269. state_events: &quot;synapse.types.StateMap&quot;,
  11270. ) -&gt; Tuple[bool, Optional[dict]]:
  11271. event_dict = event.get_dict()
  11272. new_event_content = await self.api.http_client.post_json_get_json(
  11273. uri=self._endpoint, post_json=event_dict,
  11274. )
  11275. event_dict[&quot;content&quot;] = new_event_content
  11276. return event_dict
  11277. </code></pre>
  11278. <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>
  11279. <p>Presence router callbacks allow module developers to specify additional users (local or remote)
  11280. to receive certain presence updates from local users. Presence router callbacks can be
  11281. registered using the module API's <code>register_presence_router_callbacks</code> method.</p>
  11282. <h2 id="callbacks-2"><a class="header" href="#callbacks-2">Callbacks</a></h2>
  11283. <p>The available presence router callbacks are:</p>
  11284. <h3 id="get_users_for_states"><a class="header" href="#get_users_for_states"><code>get_users_for_states</code></a></h3>
  11285. <p><em>First introduced in Synapse v1.42.0</em></p>
  11286. <pre><code class="language-python">async def get_users_for_states(
  11287. state_updates: Iterable[&quot;synapse.api.UserPresenceState&quot;],
  11288. ) -&gt; Dict[str, Set[&quot;synapse.api.UserPresenceState&quot;]]
  11289. </code></pre>
  11290. <p><strong>Requires</strong> <code>get_interested_users</code> to also be registered</p>
  11291. <p>Called when processing updates to the presence state of one or more users. This callback can
  11292. be used to instruct the server to forward that presence state to specific users. The module
  11293. must return a dictionary that maps from Matrix user IDs (which can be local or remote) to the
  11294. <code>UserPresenceState</code> changes that they should be forwarded.</p>
  11295. <p>Synapse will then attempt to send the specified presence updates to each user when possible.</p>
  11296. <p>If multiple modules implement this callback, Synapse merges all the dictionaries returned
  11297. by the callbacks. If multiple callbacks return a dictionary containing the same key,
  11298. Synapse concatenates the sets associated with this key from each dictionary. </p>
  11299. <h3 id="get_interested_users"><a class="header" href="#get_interested_users"><code>get_interested_users</code></a></h3>
  11300. <p><em>First introduced in Synapse v1.42.0</em></p>
  11301. <pre><code class="language-python">async def get_interested_users(
  11302. user_id: str
  11303. ) -&gt; Union[Set[str], &quot;synapse.module_api.PRESENCE_ALL_USERS&quot;]
  11304. </code></pre>
  11305. <p><strong>Requires</strong> <code>get_users_for_states</code> to also be registered</p>
  11306. <p>Called when determining which users someone should be able to see the presence state of. This
  11307. callback should return complementary results to <code>get_users_for_state</code> or the presence information
  11308. may not be properly forwarded.</p>
  11309. <p>The callback is given the Matrix user ID for a local user that is requesting presence data and
  11310. should return the Matrix user IDs of the users whose presence state they are allowed to
  11311. query. The returned users can be local or remote. </p>
  11312. <p>Alternatively the callback can return <code>synapse.module_api.PRESENCE_ALL_USERS</code>
  11313. to indicate that the user should receive updates from all known users.</p>
  11314. <p>If multiple modules implement this callback, they will be considered in order. Synapse
  11315. calls each callback one by one, and use a concatenation of all the <code>set</code>s returned by the
  11316. callbacks. If one callback returns <code>synapse.module_api.PRESENCE_ALL_USERS</code>, Synapse uses
  11317. this value instead. If this happens, Synapse does not call any of the subsequent
  11318. implementations of this callback.</p>
  11319. <h2 id="example-2"><a class="header" href="#example-2">Example</a></h2>
  11320. <p>The example below is a module that implements both presence router callbacks, and ensures
  11321. that <code>@alice:example.org</code> receives all presence updates from <code>@bob:example.com</code> and
  11322. <code>@charlie:somewhere.org</code>, regardless of whether Alice shares a room with any of them.</p>
  11323. <pre><code class="language-python">from typing import Dict, Iterable, Set, Union
  11324. from synapse.module_api import ModuleApi
  11325. class CustomPresenceRouter:
  11326. def __init__(self, config: dict, api: ModuleApi):
  11327. self.api = api
  11328. self.api.register_presence_router_callbacks(
  11329. get_users_for_states=self.get_users_for_states,
  11330. get_interested_users=self.get_interested_users,
  11331. )
  11332. async def get_users_for_states(
  11333. self,
  11334. state_updates: Iterable[&quot;synapse.api.UserPresenceState&quot;],
  11335. ) -&gt; Dict[str, Set[&quot;synapse.api.UserPresenceState&quot;]]:
  11336. res = {}
  11337. for update in state_updates:
  11338. if (
  11339. update.user_id == &quot;@bob:example.com&quot;
  11340. or update.user_id == &quot;@charlie:somewhere.org&quot;
  11341. ):
  11342. res.setdefault(&quot;@alice:example.com&quot;, set()).add(update)
  11343. return res
  11344. async def get_interested_users(
  11345. self,
  11346. user_id: str,
  11347. ) -&gt; Union[Set[str], &quot;synapse.module_api.PRESENCE_ALL_USERS&quot;]:
  11348. if user_id == &quot;@alice:example.com&quot;:
  11349. return {&quot;@bob:example.com&quot;, &quot;@charlie:somewhere.org&quot;}
  11350. return set()
  11351. </code></pre>
  11352. <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>
  11353. <p>Account validity callbacks allow module developers to add extra steps to verify the
  11354. validity on an account, i.e. see if a user can be granted access to their account on the
  11355. Synapse instance. Account validity callbacks can be registered using the module API's
  11356. <code>register_account_validity_callbacks</code> method.</p>
  11357. <p>The available account validity callbacks are:</p>
  11358. <h3 id="is_user_expired"><a class="header" href="#is_user_expired"><code>is_user_expired</code></a></h3>
  11359. <p><em>First introduced in Synapse v1.39.0</em></p>
  11360. <pre><code class="language-python">async def is_user_expired(user: str) -&gt; Optional[bool]
  11361. </code></pre>
  11362. <p>Called when processing any authenticated request (except for logout requests). The module
  11363. can return a <code>bool</code> to indicate whether the user has expired and should be locked out of
  11364. their account, or <code>None</code> if the module wasn't able to figure it out. The user is
  11365. represented by their Matrix user ID (e.g. <code>@alice:example.com</code>).</p>
  11366. <p>If the module returns <code>True</code>, the current request will be denied with the error code
  11367. <code>ORG_MATRIX_EXPIRED_ACCOUNT</code> and the HTTP status code 403. Note that this doesn't
  11368. invalidate the user's access token.</p>
  11369. <p>If multiple modules implement this callback, they will be considered in order. If a
  11370. callback returns <code>None</code>, Synapse falls through to the next one. The value of the first
  11371. callback that does not return <code>None</code> will be used. If this happens, Synapse will not call
  11372. any of the subsequent implementations of this callback.</p>
  11373. <h3 id="on_user_registration"><a class="header" href="#on_user_registration"><code>on_user_registration</code></a></h3>
  11374. <p><em>First introduced in Synapse v1.39.0</em></p>
  11375. <pre><code class="language-python">async def on_user_registration(user: str) -&gt; None
  11376. </code></pre>
  11377. <p>Called after successfully registering a user, in case the module needs to perform extra
  11378. operations to keep track of them. (e.g. add them to a database table). The user is
  11379. represented by their Matrix user ID.</p>
  11380. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  11381. <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>
  11382. <p>Password auth providers offer a way for server administrators to integrate
  11383. their Synapse installation with an external authentication system. The callbacks can be
  11384. registered by using the Module API's <code>register_password_auth_provider_callbacks</code> method.</p>
  11385. <h2 id="callbacks-3"><a class="header" href="#callbacks-3">Callbacks</a></h2>
  11386. <h3 id="auth_checkers"><a class="header" href="#auth_checkers"><code>auth_checkers</code></a></h3>
  11387. <p><em>First introduced in Synapse v1.46.0</em></p>
  11388. <pre><code class="language-python">auth_checkers: Dict[Tuple[str, Tuple[str, ...]], Callable]
  11389. </code></pre>
  11390. <p>A dict mapping from tuples of a login type identifier (such as <code>m.login.password</code>) and a
  11391. tuple of field names (such as <code>(&quot;password&quot;, &quot;secret_thing&quot;)</code>) to authentication checking
  11392. callbacks, which should be of the following form:</p>
  11393. <pre><code class="language-python">async def check_auth(
  11394. user: str,
  11395. login_type: str,
  11396. login_dict: &quot;synapse.module_api.JsonDict&quot;,
  11397. ) -&gt; Optional[
  11398. Tuple[
  11399. str,
  11400. Optional[Callable[[&quot;synapse.module_api.LoginResponse&quot;], Awaitable[None]]]
  11401. ]
  11402. ]
  11403. </code></pre>
  11404. <p>The login type and field names should be provided by the user in the
  11405. request to the <code>/login</code> API. <a href="https://matrix.org/docs/spec/client_server/latest#authentication-types">The Matrix specification</a>
  11406. defines some types, however user defined ones are also allowed.</p>
  11407. <p>The callback is passed the <code>user</code> field provided by the client (which might not be in
  11408. <code>@username:server</code> form), the login type, and a dictionary of login secrets passed by
  11409. the client.</p>
  11410. <p>If the authentication is successful, the module must return the user's Matrix ID (e.g.
  11411. <code>@alice:example.com</code>) and optionally a callback to be called with the response to the
  11412. <code>/login</code> request. If the module doesn't wish to return a callback, it must return <code>None</code>
  11413. instead.</p>
  11414. <p>If the authentication is unsuccessful, the module must return <code>None</code>.</p>
  11415. <p>If multiple modules register an auth checker for the same login type but with different
  11416. fields, Synapse will refuse to start.</p>
  11417. <p>If multiple modules register an auth checker for the same login type with the same fields,
  11418. then the callbacks will be executed in order, until one returns a Matrix User ID (and
  11419. optionally a callback). In that case, the return value of that callback will be accepted
  11420. and subsequent callbacks will not be fired. If every callback returns <code>None</code>, then the
  11421. authentication fails.</p>
  11422. <h3 id="check_3pid_auth"><a class="header" href="#check_3pid_auth"><code>check_3pid_auth</code></a></h3>
  11423. <p><em>First introduced in Synapse v1.46.0</em></p>
  11424. <pre><code class="language-python">async def check_3pid_auth(
  11425. medium: str,
  11426. address: str,
  11427. password: str,
  11428. ) -&gt; Optional[
  11429. Tuple[
  11430. str,
  11431. Optional[Callable[[&quot;synapse.module_api.LoginResponse&quot;], Awaitable[None]]]
  11432. ]
  11433. ]
  11434. </code></pre>
  11435. <p>Called when a user attempts to register or log in with a third party identifier,
  11436. such as email. It is passed the medium (eg. <code>email</code>), an address (eg. <code>jdoe@example.com</code>)
  11437. and the user's password.</p>
  11438. <p>If the authentication is successful, the module must return the user's Matrix ID (e.g.
  11439. <code>@alice:example.com</code>) and optionally a callback to be called with the response to the <code>/login</code> request.
  11440. If the module doesn't wish to return a callback, it must return None instead.</p>
  11441. <p>If the authentication is unsuccessful, the module must return <code>None</code>.</p>
  11442. <p>If multiple modules implement this callback, they will be considered in order. If a
  11443. callback returns <code>None</code>, Synapse falls through to the next one. The value of the first
  11444. callback that does not return <code>None</code> will be used. If this happens, Synapse will not call
  11445. any of the subsequent implementations of this callback. If every callback returns <code>None</code>,
  11446. the authentication is denied.</p>
  11447. <h3 id="on_logged_out"><a class="header" href="#on_logged_out"><code>on_logged_out</code></a></h3>
  11448. <p><em>First introduced in Synapse v1.46.0</em></p>
  11449. <pre><code class="language-python">async def on_logged_out(
  11450. user_id: str,
  11451. device_id: Optional[str],
  11452. access_token: str
  11453. ) -&gt; None
  11454. </code></pre>
  11455. <p>Called during a logout request for a user. It is passed the qualified user ID, the ID of the
  11456. deactivated device (if any: access tokens are occasionally created without an associated
  11457. device ID), and the (now deactivated) access token.</p>
  11458. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  11459. <h3 id="get_username_for_registration"><a class="header" href="#get_username_for_registration"><code>get_username_for_registration</code></a></h3>
  11460. <p><em>First introduced in Synapse v1.52.0</em></p>
  11461. <pre><code class="language-python">async def get_username_for_registration(
  11462. uia_results: Dict[str, Any],
  11463. params: Dict[str, Any],
  11464. ) -&gt; Optional[str]
  11465. </code></pre>
  11466. <p>Called when registering a new user. The module can return a username to set for the user
  11467. being registered by returning it as a string, or <code>None</code> if it doesn't wish to force a
  11468. username for this user. If a username is returned, it will be used as the local part of a
  11469. user's full Matrix ID (e.g. it's <code>alice</code> in <code>@alice:example.com</code>).</p>
  11470. <p>This callback is called once <a href="https://spec.matrix.org/latest/client-server-api/#user-interactive-authentication-api">User-Interactive Authentication</a>
  11471. has been completed by the user. It is not called when registering a user via SSO. It is
  11472. passed two dictionaries, which include the information that the user has provided during
  11473. the registration process.</p>
  11474. <p>The first dictionary contains the results of the <a href="https://spec.matrix.org/latest/client-server-api/#user-interactive-authentication-api">User-Interactive Authentication</a>
  11475. flow followed by the user. Its keys are the identifiers of every step involved in the flow,
  11476. associated with either a boolean value indicating whether the step was correctly completed,
  11477. or additional information (e.g. email address, phone number...). A list of most existing
  11478. identifiers can be found in the <a href="https://spec.matrix.org/v1.1/client-server-api/#authentication-types">Matrix specification</a>.
  11479. Here's an example featuring all currently supported keys:</p>
  11480. <pre><code class="language-python">{
  11481. &quot;m.login.dummy&quot;: True, # Dummy authentication
  11482. &quot;m.login.terms&quot;: True, # User has accepted the terms of service for the homeserver
  11483. &quot;m.login.recaptcha&quot;: True, # User has completed the recaptcha challenge
  11484. &quot;m.login.email.identity&quot;: { # User has provided and verified an email address
  11485. &quot;medium&quot;: &quot;email&quot;,
  11486. &quot;address&quot;: &quot;alice@example.com&quot;,
  11487. &quot;validated_at&quot;: 1642701357084,
  11488. },
  11489. &quot;m.login.msisdn&quot;: { # User has provided and verified a phone number
  11490. &quot;medium&quot;: &quot;msisdn&quot;,
  11491. &quot;address&quot;: &quot;33123456789&quot;,
  11492. &quot;validated_at&quot;: 1642701357084,
  11493. },
  11494. &quot;m.login.registration_token&quot;: &quot;sometoken&quot;, # User has registered through a registration token
  11495. }
  11496. </code></pre>
  11497. <p>The second dictionary contains the parameters provided by the user's client in the request
  11498. to <code>/_matrix/client/v3/register</code>. See the <a href="https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3register">Matrix specification</a>
  11499. for a complete list of these parameters.</p>
  11500. <p>If the module cannot, or does not wish to, generate a username for this user, it must
  11501. return <code>None</code>.</p>
  11502. <p>If multiple modules implement this callback, they will be considered in order. If a
  11503. callback returns <code>None</code>, Synapse falls through to the next one. The value of the first
  11504. callback that does not return <code>None</code> will be used. If this happens, Synapse will not call
  11505. any of the subsequent implementations of this callback. If every callback returns <code>None</code>,
  11506. the username provided by the user is used, if any (otherwise one is automatically
  11507. generated).</p>
  11508. <h3 id="get_displayname_for_registration"><a class="header" href="#get_displayname_for_registration"><code>get_displayname_for_registration</code></a></h3>
  11509. <p><em>First introduced in Synapse v1.54.0</em></p>
  11510. <pre><code class="language-python">async def get_displayname_for_registration(
  11511. uia_results: Dict[str, Any],
  11512. params: Dict[str, Any],
  11513. ) -&gt; Optional[str]
  11514. </code></pre>
  11515. <p>Called when registering a new user. The module can return a display name to set for the
  11516. user being registered by returning it as a string, or <code>None</code> if it doesn't wish to force a
  11517. display name for this user.</p>
  11518. <p>This callback is called once <a href="https://spec.matrix.org/latest/client-server-api/#user-interactive-authentication-api">User-Interactive Authentication</a>
  11519. has been completed by the user. It is not called when registering a user via SSO. It is
  11520. passed two dictionaries, which include the information that the user has provided during
  11521. the registration process. These dictionaries are identical to the ones passed to
  11522. <a href="modules/password_auth_provider_callbacks.html#get_username_for_registration"><code>get_username_for_registration</code></a>, so refer to the
  11523. documentation of this callback for more information about them.</p>
  11524. <p>If multiple modules implement this callback, they will be considered in order. If a
  11525. callback returns <code>None</code>, Synapse falls through to the next one. The value of the first
  11526. callback that does not return <code>None</code> will be used. If this happens, Synapse will not call
  11527. any of the subsequent implementations of this callback. If every callback returns <code>None</code>,
  11528. the username will be used (e.g. <code>alice</code> if the user being registered is <code>@alice:example.com</code>).</p>
  11529. <h2 id="is_3pid_allowed"><a class="header" href="#is_3pid_allowed"><code>is_3pid_allowed</code></a></h2>
  11530. <p><em>First introduced in Synapse v1.53.0</em></p>
  11531. <pre><code class="language-python">async def is_3pid_allowed(self, medium: str, address: str, registration: bool) -&gt; bool
  11532. </code></pre>
  11533. <p>Called when attempting to bind a third-party identifier (i.e. an email address or a phone
  11534. number). The module is given the medium of the third-party identifier (which is <code>email</code> if
  11535. the identifier is an email address, or <code>msisdn</code> if the identifier is a phone number) and
  11536. its address, as well as a boolean indicating whether the attempt to bind is happening as
  11537. part of registering a new user. The module must return a boolean indicating whether the
  11538. identifier can be allowed to be bound to an account on the local homeserver.</p>
  11539. <p>If multiple modules implement this callback, they will be considered in order. If a
  11540. callback returns <code>True</code>, Synapse falls through to the next one. The value of the first
  11541. callback that does not return <code>True</code> will be used. If this happens, Synapse will not call
  11542. any of the subsequent implementations of this callback.</p>
  11543. <h2 id="example-3"><a class="header" href="#example-3">Example</a></h2>
  11544. <p>The example module below implements authentication checkers for two different login types: </p>
  11545. <ul>
  11546. <li><code>my.login.type</code>
  11547. <ul>
  11548. <li>Expects a <code>my_field</code> field to be sent to <code>/login</code></li>
  11549. <li>Is checked by the method: <code>self.check_my_login</code></li>
  11550. </ul>
  11551. </li>
  11552. <li><code>m.login.password</code> (defined in <a href="https://matrix.org/docs/spec/client_server/latest#password-based">the spec</a>)
  11553. <ul>
  11554. <li>Expects a <code>password</code> field to be sent to <code>/login</code></li>
  11555. <li>Is checked by the method: <code>self.check_pass</code></li>
  11556. </ul>
  11557. </li>
  11558. </ul>
  11559. <pre><code class="language-python">from typing import Awaitable, Callable, Optional, Tuple
  11560. import synapse
  11561. from synapse import module_api
  11562. class MyAuthProvider:
  11563. def __init__(self, config: dict, api: module_api):
  11564. self.api = api
  11565. self.credentials = {
  11566. &quot;bob&quot;: &quot;building&quot;,
  11567. &quot;@scoop:matrix.org&quot;: &quot;digging&quot;,
  11568. }
  11569. api.register_password_auth_provider_callbacks(
  11570. auth_checkers={
  11571. (&quot;my.login_type&quot;, (&quot;my_field&quot;,)): self.check_my_login,
  11572. (&quot;m.login.password&quot;, (&quot;password&quot;,)): self.check_pass,
  11573. },
  11574. )
  11575. async def check_my_login(
  11576. self,
  11577. username: str,
  11578. login_type: str,
  11579. login_dict: &quot;synapse.module_api.JsonDict&quot;,
  11580. ) -&gt; Optional[
  11581. Tuple[
  11582. str,
  11583. Optional[Callable[[&quot;synapse.module_api.LoginResponse&quot;], Awaitable[None]]],
  11584. ]
  11585. ]:
  11586. if login_type != &quot;my.login_type&quot;:
  11587. return None
  11588. if self.credentials.get(username) == login_dict.get(&quot;my_field&quot;):
  11589. return self.api.get_qualified_user_id(username)
  11590. async def check_pass(
  11591. self,
  11592. username: str,
  11593. login_type: str,
  11594. login_dict: &quot;synapse.module_api.JsonDict&quot;,
  11595. ) -&gt; Optional[
  11596. Tuple[
  11597. str,
  11598. Optional[Callable[[&quot;synapse.module_api.LoginResponse&quot;], Awaitable[None]]],
  11599. ]
  11600. ]:
  11601. if login_type != &quot;m.login.password&quot;:
  11602. return None
  11603. if self.credentials.get(username) == login_dict.get(&quot;password&quot;):
  11604. return self.api.get_qualified_user_id(username)
  11605. </code></pre>
  11606. <div style="break-before: page; page-break-before: always;"></div><h1 id="background-update-controller-callbacks"><a class="header" href="#background-update-controller-callbacks">Background update controller callbacks</a></h1>
  11607. <p>Background update controller callbacks allow module developers to control (e.g. rate-limit)
  11608. how database background updates are run. A database background update is an operation
  11609. Synapse runs on its database in the background after it starts. It's usually used to run
  11610. database operations that would take too long if they were run at the same time as schema
  11611. updates (which are run on startup) and delay Synapse's startup too much: populating a
  11612. table with a big amount of data, adding an index on a big table, deleting superfluous data,
  11613. etc.</p>
  11614. <p>Background update controller callbacks can be registered using the module API's
  11615. <code>register_background_update_controller_callbacks</code> method. Only the first module (in order
  11616. of appearance in Synapse's configuration file) calling this method can register background
  11617. update controller callbacks, subsequent calls are ignored.</p>
  11618. <p>The available background update controller callbacks are:</p>
  11619. <h3 id="on_update"><a class="header" href="#on_update"><code>on_update</code></a></h3>
  11620. <p><em>First introduced in Synapse v1.49.0</em></p>
  11621. <pre><code class="language-python">def on_update(update_name: str, database_name: str, one_shot: bool) -&gt; AsyncContextManager[int]
  11622. </code></pre>
  11623. <p>Called when about to do an iteration of a background update. The module is given the name
  11624. of the update, the name of the database, and a flag to indicate whether the background
  11625. update will happen in one go and may take a long time (e.g. creating indices). If this last
  11626. argument is set to <code>False</code>, the update will be run in batches.</p>
  11627. <p>The module must return an async context manager. It will be entered before Synapse runs a
  11628. background update; this should return the desired duration of the iteration, in
  11629. milliseconds.</p>
  11630. <p>The context manager will be exited when the iteration completes. Note that the duration
  11631. returned by the context manager is a target, and an iteration may take substantially longer
  11632. or shorter. If the <code>one_shot</code> flag is set to <code>True</code>, the duration returned is ignored.</p>
  11633. <p><strong>Note</strong>: Unlike most module callbacks in Synapse, this one is <em>synchronous</em>. This is
  11634. because asynchronous operations are expected to be run by the async context manager.</p>
  11635. <p>This callback is required when registering any other background update controller callback.</p>
  11636. <h3 id="default_batch_size"><a class="header" href="#default_batch_size"><code>default_batch_size</code></a></h3>
  11637. <p><em>First introduced in Synapse v1.49.0</em></p>
  11638. <pre><code class="language-python">async def default_batch_size(update_name: str, database_name: str) -&gt; int
  11639. </code></pre>
  11640. <p>Called before the first iteration of a background update, with the name of the update and
  11641. of the database. The module must return the number of elements to process in this first
  11642. iteration.</p>
  11643. <p>If this callback is not defined, Synapse will use a default value of 100.</p>
  11644. <h3 id="min_batch_size"><a class="header" href="#min_batch_size"><code>min_batch_size</code></a></h3>
  11645. <p><em>First introduced in Synapse v1.49.0</em></p>
  11646. <pre><code class="language-python">async def min_batch_size(update_name: str, database_name: str) -&gt; int
  11647. </code></pre>
  11648. <p>Called before running a new batch for a background update, with the name of the update and
  11649. of the database. The module must return an integer representing the minimum number of
  11650. elements to process in this iteration. This number must be at least 1, and is used to
  11651. ensure that progress is always made.</p>
  11652. <p>If this callback is not defined, Synapse will use a default value of 100.</p>
  11653. <div style="break-before: page; page-break-before: always;"></div><h1 id="account-data-callbacks"><a class="header" href="#account-data-callbacks">Account data callbacks</a></h1>
  11654. <p>Account data callbacks allow module developers to react to changes of the account data
  11655. of local users. Account data callbacks can be registered using the module API's
  11656. <code>register_account_data_callbacks</code> method.</p>
  11657. <h2 id="callbacks-4"><a class="header" href="#callbacks-4">Callbacks</a></h2>
  11658. <p>The available account data callbacks are:</p>
  11659. <h3 id="on_account_data_updated"><a class="header" href="#on_account_data_updated"><code>on_account_data_updated</code></a></h3>
  11660. <p><em>First introduced in Synapse v1.57.0</em></p>
  11661. <pre><code class="language-python">async def on_account_data_updated(
  11662. user_id: str,
  11663. room_id: Optional[str],
  11664. account_data_type: str,
  11665. content: &quot;synapse.module_api.JsonDict&quot;,
  11666. ) -&gt; None:
  11667. </code></pre>
  11668. <p>Called after user's account data has been updated. The module is given the
  11669. Matrix ID of the user whose account data is changing, the room ID the data is associated
  11670. with, the type associated with the change, as well as the new content. If the account
  11671. data is not associated with a specific room, then the room ID is <code>None</code>.</p>
  11672. <p>This callback is triggered when new account data is added or when the data associated with
  11673. a given type (and optionally room) changes. This includes deletion, since in Matrix,
  11674. deleting account data consists of replacing the data associated with a given type
  11675. (and optionally room) with an empty dictionary (<code>{}</code>).</p>
  11676. <p>Note that this doesn't trigger when changing the tags associated with a room, as these are
  11677. processed separately by Synapse.</p>
  11678. <p>If multiple modules implement this callback, Synapse runs them all in order.</p>
  11679. <h2 id="example-4"><a class="header" href="#example-4">Example</a></h2>
  11680. <p>The example below is a module that implements the <code>on_account_data_updated</code> callback, and
  11681. sends an event to an audit room when a user changes their account data.</p>
  11682. <pre><code class="language-python">import json
  11683. import attr
  11684. from typing import Any, Dict, Optional
  11685. from synapse.module_api import JsonDict, ModuleApi
  11686. from synapse.module_api.errors import ConfigError
  11687. @attr.s(auto_attribs=True)
  11688. class CustomAccountDataConfig:
  11689. audit_room: str
  11690. sender: str
  11691. class CustomAccountDataModule:
  11692. def __init__(self, config: CustomAccountDataConfig, api: ModuleApi):
  11693. self.api = api
  11694. self.config = config
  11695. self.api.register_account_data_callbacks(
  11696. on_account_data_updated=self.log_new_account_data,
  11697. )
  11698. @staticmethod
  11699. def parse_config(config: Dict[str, Any]) -&gt; CustomAccountDataConfig:
  11700. def check_in_config(param: str):
  11701. if param not in config:
  11702. raise ConfigError(f&quot;'{param}' is required&quot;)
  11703. check_in_config(&quot;audit_room&quot;)
  11704. check_in_config(&quot;sender&quot;)
  11705. return CustomAccountDataConfig(
  11706. audit_room=config[&quot;audit_room&quot;],
  11707. sender=config[&quot;sender&quot;],
  11708. )
  11709. async def log_new_account_data(
  11710. self,
  11711. user_id: str,
  11712. room_id: Optional[str],
  11713. account_data_type: str,
  11714. content: JsonDict,
  11715. ) -&gt; None:
  11716. content_raw = json.dumps(content)
  11717. msg_content = f&quot;{user_id} has changed their account data for type {account_data_type} to: {content_raw}&quot;
  11718. if room_id is not None:
  11719. msg_content += f&quot; (in room {room_id})&quot;
  11720. await self.api.create_and_send_event_into_room(
  11721. {
  11722. &quot;room_id&quot;: self.config.audit_room,
  11723. &quot;sender&quot;: self.config.sender,
  11724. &quot;type&quot;: &quot;m.room.message&quot;,
  11725. &quot;content&quot;: {
  11726. &quot;msgtype&quot;: &quot;m.text&quot;,
  11727. &quot;body&quot;: msg_content
  11728. }
  11729. }
  11730. )
  11731. </code></pre>
  11732. <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>
  11733. <p>In order to port a module that uses Synapse's old module interface, its author needs to:</p>
  11734. <ul>
  11735. <li>ensure the module's callbacks are all asynchronous.</li>
  11736. <li>register their callbacks using one or more of the <code>register_[...]_callbacks</code> methods
  11737. 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>
  11738. for more info).</li>
  11739. </ul>
  11740. <p>Additionally, if the module is packaged with an additional web resource, the module
  11741. should register this resource in its <code>__init__</code> method using the <code>register_web_resource</code>
  11742. method from the <code>ModuleApi</code> class (see <a href="modules/writing_a_module.html#registering-a-web-resource">this section</a> for
  11743. more info).</p>
  11744. <p>There is no longer a <code>get_db_schema_files</code> callback provided for password auth provider modules. Any
  11745. changes to the database should now be made by the module using the module API class.</p>
  11746. <p>The module's author should also update any example in the module's configuration to only
  11747. use the new <code>modules</code> section in Synapse's configuration file (see <a href="modules/index.html#using-modules">this section</a>
  11748. for more info).</p>
  11749. <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>
  11750. <p>For small instances it recommended to run Synapse in the default monolith mode.
  11751. For larger instances where performance is a concern it can be helpful to split
  11752. out functionality into multiple separate python processes. These processes are
  11753. called 'workers', and are (eventually) intended to scale horizontally
  11754. independently.</p>
  11755. <p>Synapse's worker support is under active development and subject to change as
  11756. we attempt to rapidly scale ever larger Synapse instances. However we are
  11757. documenting it here to help admins needing a highly scalable Synapse instance
  11758. similar to the one running <code>matrix.org</code>.</p>
  11759. <p>All processes continue to share the same database instance, and as such,
  11760. workers only work with PostgreSQL-based Synapse deployments. SQLite should only
  11761. be used for demo purposes and any admin considering workers should already be
  11762. running PostgreSQL.</p>
  11763. <p>See also <a href="https://matrix.org/blog/2020/11/03/how-we-fixed-synapses-scalability">Matrix.org blog post</a>
  11764. for a higher level overview.</p>
  11765. <h2 id="main-processworker-communication"><a class="header" href="#main-processworker-communication">Main process/worker communication</a></h2>
  11766. <p>The processes communicate with each other via a Synapse-specific protocol called
  11767. 'replication' (analogous to MySQL- or Postgres-style database replication) which
  11768. feeds streams of newly written data between processes so they can be kept in
  11769. sync with the database state.</p>
  11770. <p>When configured to do so, Synapse uses a
  11771. <a href="https://redis.io/docs/manual/pubsub/">Redis pub/sub channel</a> to send the replication
  11772. stream between all configured Synapse processes. Additionally, processes may
  11773. make HTTP requests to each other, primarily for operations which need to wait
  11774. for a reply ─ such as sending an event.</p>
  11775. <p>Redis support was added in v1.13.0 with it becoming the recommended method in
  11776. v1.18.0. It replaced the old direct TCP connections (which is deprecated as of
  11777. v1.18.0) to the main process. With Redis, rather than all the workers connecting
  11778. to the main process, all the workers and the main process connect to Redis,
  11779. which relays replication commands between processes. This can give a significant
  11780. cpu saving on the main process and will be a prerequisite for upcoming
  11781. performance improvements.</p>
  11782. <p>If Redis support is enabled Synapse will use it as a shared cache, as well as a
  11783. pub/sub mechanism.</p>
  11784. <p>See the <a href="workers.html#architectural-diagram">Architectural diagram</a> section at the end for
  11785. a visualisation of what this looks like.</p>
  11786. <h2 id="setting-up-workers"><a class="header" href="#setting-up-workers">Setting up workers</a></h2>
  11787. <p>A Redis server is required to manage the communication between the processes.
  11788. The Redis server should be installed following the normal procedure for your
  11789. distribution (e.g. <code>apt install redis-server</code> on Debian). It is safe to use an
  11790. existing Redis deployment if you have one.</p>
  11791. <p>Once installed, check that Redis is running and accessible from the host running
  11792. Synapse, for example by executing <code>echo PING | nc -q1 localhost 6379</code> and seeing
  11793. a response of <code>+PONG</code>.</p>
  11794. <p>The appropriate dependencies must also be installed for Synapse. If using a
  11795. virtualenv, these can be installed with:</p>
  11796. <pre><code class="language-sh">pip install &quot;matrix-synapse[redis]&quot;
  11797. </code></pre>
  11798. <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
  11799. <code>matrix.org</code> and in the docker images at
  11800. https://hub.docker.com/r/matrixdotorg/synapse/.</p>
  11801. <p>To make effective use of the workers, you will need to configure an HTTP
  11802. reverse-proxy such as nginx or haproxy, which will direct incoming requests to
  11803. the correct worker, or to the main synapse instance. See
  11804. <a href="reverse_proxy.html">the reverse proxy documentation</a> for information on setting up a reverse
  11805. proxy.</p>
  11806. <p>When using workers, each worker process has its own configuration file which
  11807. contains settings specific to that worker, such as the HTTP listener that it
  11808. provides (if any), logging configuration, etc.</p>
  11809. <p>Normally, the worker processes are configured to read from a shared
  11810. configuration file as well as the worker-specific configuration files. This
  11811. makes it easier to keep common configuration settings synchronised across all
  11812. the processes.</p>
  11813. <p>The main process is somewhat special in this respect: it does not normally
  11814. need its own configuration file and can take all of its configuration from the
  11815. shared configuration file.</p>
  11816. <h3 id="shared-configuration"><a class="header" href="#shared-configuration">Shared configuration</a></h3>
  11817. <p>Normally, only a couple of changes are needed to make an existing configuration
  11818. file suitable for use with workers. First, you need to enable an &quot;HTTP replication
  11819. listener&quot; for the main process; and secondly, you need to enable redis-based
  11820. replication. Optionally, a shared secret can be used to authenticate HTTP
  11821. traffic between workers. For example:</p>
  11822. <pre><code class="language-yaml"># extend the existing `listeners` section. This defines the ports that the
  11823. # main process will listen on.
  11824. listeners:
  11825. # The HTTP replication port
  11826. - port: 9093
  11827. bind_address: '127.0.0.1'
  11828. type: http
  11829. resources:
  11830. - names: [replication]
  11831. # Add a random shared secret to authenticate traffic.
  11832. worker_replication_secret: &quot;&quot;
  11833. redis:
  11834. enabled: true
  11835. </code></pre>
  11836. <p>See the sample config for the full documentation of each option.</p>
  11837. <p>Under <strong>no circumstances</strong> should the replication listener be exposed to the
  11838. public internet; it has no authentication and is unencrypted.</p>
  11839. <h3 id="worker-configuration"><a class="header" href="#worker-configuration">Worker configuration</a></h3>
  11840. <p>In the config file for each worker, you must specify the type of worker
  11841. application (<code>worker_app</code>), and you should specify a unique name for the worker
  11842. (<code>worker_name</code>). The currently available worker applications are listed below.
  11843. You must also specify the HTTP replication endpoint that it should talk to on
  11844. the main synapse process. <code>worker_replication_host</code> should specify the host of
  11845. the main synapse and <code>worker_replication_http_port</code> should point to the HTTP
  11846. replication port. If the worker will handle HTTP requests then the
  11847. <code>worker_listeners</code> option should be set with a <code>http</code> listener, in the same way
  11848. as the <code>listeners</code> option in the shared config.</p>
  11849. <p>For example:</p>
  11850. <pre><code class="language-yaml">worker_app: synapse.app.generic_worker
  11851. worker_name: generic_worker1
  11852. # The replication listener on the main synapse process.
  11853. worker_replication_host: 127.0.0.1
  11854. worker_replication_http_port: 9093
  11855. worker_listeners:
  11856. - type: http
  11857. port: 8083
  11858. resources:
  11859. - names: [client, federation]
  11860. worker_log_config: /etc/matrix-synapse/generic-worker-log.yaml
  11861. </code></pre>
  11862. <p>...is a full configuration for a generic worker instance, which will expose a
  11863. plain HTTP endpoint on port 8083 separately serving various endpoints, e.g.
  11864. <code>/sync</code>, which are listed below.</p>
  11865. <p>Obviously you should configure your reverse-proxy to route the relevant
  11866. endpoints to the worker (<code>localhost:8083</code> in the above example).</p>
  11867. <h3 id="running-synapse-with-workers"><a class="header" href="#running-synapse-with-workers">Running Synapse with workers</a></h3>
  11868. <p>Finally, you need to start your worker processes. This can be done with either
  11869. <code>synctl</code> or your distribution's preferred service manager such as <code>systemd</code>. We
  11870. recommend the use of <code>systemd</code> where available: for information on setting up
  11871. <code>systemd</code> to start synapse workers, see
  11872. <a href="systemd-with-workers">Systemd with Workers</a>. To use <code>synctl</code>, see
  11873. <a href="synctl_workers.html">Using synctl with Workers</a>.</p>
  11874. <h2 id="available-worker-applications"><a class="header" href="#available-worker-applications">Available worker applications</a></h2>
  11875. <h3 id="synapseappgeneric_worker"><a class="header" href="#synapseappgeneric_worker"><code>synapse.app.generic_worker</code></a></h3>
  11876. <p>This worker can handle API requests matching the following regular expressions.
  11877. These endpoints can be routed to any worker. If a worker is set up to handle a
  11878. stream then, for maximum efficiency, additional endpoints should be routed to that
  11879. worker: refer to the <a href="workers.html#stream-writers">stream writers</a> section below for further
  11880. information.</p>
  11881. <pre><code># Sync requests
  11882. ^/_matrix/client/(r0|v3)/sync$
  11883. ^/_matrix/client/(api/v1|r0|v3)/events$
  11884. ^/_matrix/client/(api/v1|r0|v3)/initialSync$
  11885. ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$
  11886. # Federation requests
  11887. ^/_matrix/federation/v1/event/
  11888. ^/_matrix/federation/v1/state/
  11889. ^/_matrix/federation/v1/state_ids/
  11890. ^/_matrix/federation/v1/backfill/
  11891. ^/_matrix/federation/v1/get_missing_events/
  11892. ^/_matrix/federation/v1/publicRooms
  11893. ^/_matrix/federation/v1/query/
  11894. ^/_matrix/federation/v1/make_join/
  11895. ^/_matrix/federation/v1/make_leave/
  11896. ^/_matrix/federation/(v1|v2)/send_join/
  11897. ^/_matrix/federation/(v1|v2)/send_leave/
  11898. ^/_matrix/federation/(v1|v2)/invite/
  11899. ^/_matrix/federation/v1/event_auth/
  11900. ^/_matrix/federation/v1/exchange_third_party_invite/
  11901. ^/_matrix/federation/v1/user/devices/
  11902. ^/_matrix/federation/v1/get_groups_publicised$
  11903. ^/_matrix/key/v2/query
  11904. ^/_matrix/federation/(v1|unstable/org.matrix.msc2946)/hierarchy/
  11905. # Inbound federation transaction request
  11906. ^/_matrix/federation/v1/send/
  11907. # Client API requests
  11908. ^/_matrix/client/(api/v1|r0|v3|unstable)/createRoom$
  11909. ^/_matrix/client/(api/v1|r0|v3|unstable)/publicRooms$
  11910. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/joined_members$
  11911. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/context/.*$
  11912. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/members$
  11913. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state$
  11914. ^/_matrix/client/(v1|unstable/org.matrix.msc2946)/rooms/.*/hierarchy$
  11915. ^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary$
  11916. ^/_matrix/client/(r0|v3|unstable)/account/3pid$
  11917. ^/_matrix/client/(r0|v3|unstable)/devices$
  11918. ^/_matrix/client/versions$
  11919. ^/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer$
  11920. ^/_matrix/client/(r0|v3|unstable)/joined_groups$
  11921. ^/_matrix/client/(r0|v3|unstable)/publicised_groups$
  11922. ^/_matrix/client/(r0|v3|unstable)/publicised_groups/
  11923. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/
  11924. ^/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms$
  11925. ^/_matrix/client/(api/v1|r0|v3|unstable)/search$
  11926. # Encryption requests
  11927. ^/_matrix/client/(r0|v3|unstable)/keys/query$
  11928. ^/_matrix/client/(r0|v3|unstable)/keys/changes$
  11929. ^/_matrix/client/(r0|v3|unstable)/keys/claim$
  11930. ^/_matrix/client/(r0|v3|unstable)/room_keys/
  11931. # Registration/login requests
  11932. ^/_matrix/client/(api/v1|r0|v3|unstable)/login$
  11933. ^/_matrix/client/(r0|v3|unstable)/register$
  11934. ^/_matrix/client/v1/register/m.login.registration_token/validity$
  11935. # Event sending requests
  11936. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact
  11937. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send
  11938. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/
  11939. ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$
  11940. ^/_matrix/client/(api/v1|r0|v3|unstable)/join/
  11941. ^/_matrix/client/(api/v1|r0|v3|unstable)/profile/
  11942. # Device requests
  11943. ^/_matrix/client/(r0|v3|unstable)/sendToDevice/
  11944. # Account data requests
  11945. ^/_matrix/client/(r0|v3|unstable)/.*/tags
  11946. ^/_matrix/client/(r0|v3|unstable)/.*/account_data
  11947. # Receipts requests
  11948. ^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt
  11949. ^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers
  11950. # Presence requests
  11951. ^/_matrix/client/(api/v1|r0|v3|unstable)/presence/
  11952. # User directory search requests
  11953. ^/_matrix/client/(r0|v3|unstable)/user_directory/search$
  11954. </code></pre>
  11955. <p>Additionally, the following REST endpoints can be handled for GET requests:</p>
  11956. <pre><code>^/_matrix/federation/v1/groups/
  11957. ^/_matrix/client/(api/v1|r0|v3|unstable)/pushrules/
  11958. ^/_matrix/client/(r0|v3|unstable)/groups/
  11959. </code></pre>
  11960. <p>Pagination requests can also be handled, but all requests for a given
  11961. room must be routed to the same instance. Additionally, care must be taken to
  11962. ensure that the purge history admin API is not used while pagination requests
  11963. for the room are in flight:</p>
  11964. <pre><code>^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/messages$
  11965. </code></pre>
  11966. <p>Additionally, the following endpoints should be included if Synapse is configured
  11967. to use SSO (you only need to include the ones for whichever SSO provider you're
  11968. using):</p>
  11969. <pre><code># for all SSO providers
  11970. ^/_matrix/client/(api/v1|r0|v3|unstable)/login/sso/redirect
  11971. ^/_synapse/client/pick_idp$
  11972. ^/_synapse/client/pick_username
  11973. ^/_synapse/client/new_user_consent$
  11974. ^/_synapse/client/sso_register$
  11975. # OpenID Connect requests.
  11976. ^/_synapse/client/oidc/callback$
  11977. # SAML requests.
  11978. ^/_synapse/client/saml2/authn_response$
  11979. # CAS requests.
  11980. ^/_matrix/client/(api/v1|r0|v3|unstable)/login/cas/ticket$
  11981. </code></pre>
  11982. <p>Ensure that all SSO logins go to a single process.
  11983. For multiple workers not handling the SSO endpoints properly, see
  11984. <a href="https://github.com/matrix-org/synapse/issues/7530">#7530</a> and
  11985. <a href="https://github.com/matrix-org/synapse/issues/9427">#9427</a>.</p>
  11986. <p>Note that a HTTP listener with <code>client</code> and <code>federation</code> resources must be
  11987. configured in the <code>worker_listeners</code> option in the worker config.</p>
  11988. <h4 id="load-balancing"><a class="header" href="#load-balancing">Load balancing</a></h4>
  11989. <p>It is possible to run multiple instances of this worker app, with incoming requests
  11990. being load-balanced between them by the reverse-proxy. However, different endpoints
  11991. have different characteristics and so admins
  11992. may wish to run multiple groups of workers handling different endpoints so that
  11993. load balancing can be done in different ways.</p>
  11994. <p>For <code>/sync</code> and <code>/initialSync</code> requests it will be more efficient if all
  11995. requests from a particular user are routed to a single instance. Extracting a
  11996. user ID from the access token or <code>Authorization</code> header is currently left as an
  11997. exercise for the reader. Admins may additionally wish to separate out <code>/sync</code>
  11998. requests that have a <code>since</code> query parameter from those that don't (and
  11999. <code>/initialSync</code>), as requests that don't are known as &quot;initial sync&quot; that happens
  12000. when a user logs in on a new device and can be <em>very</em> resource intensive, so
  12001. isolating these requests will stop them from interfering with other users ongoing
  12002. syncs.</p>
  12003. <p>Federation and client requests can be balanced via simple round robin.</p>
  12004. <p>The inbound federation transaction request <code>^/_matrix/federation/v1/send/</code>
  12005. should be balanced by source IP so that transactions from the same remote server
  12006. go to the same process.</p>
  12007. <p>Registration/login requests can be handled separately purely to help ensure that
  12008. unexpected load doesn't affect new logins and sign ups.</p>
  12009. <p>Finally, event sending requests can be balanced by the room ID in the URI (or
  12010. the full URI, or even just round robin), the room ID is the path component after
  12011. <code>/rooms/</code>. If there is a large bridge connected that is sending or may send lots
  12012. of events, then a dedicated set of workers can be provisioned to limit the
  12013. effects of bursts of events from that bridge on events sent by normal users.</p>
  12014. <h4 id="stream-writers"><a class="header" href="#stream-writers">Stream writers</a></h4>
  12015. <p>Additionally, the writing of specific streams (such as events) can be moved off
  12016. of the main process to a particular worker.
  12017. (This is only supported with Redis-based replication.)</p>
  12018. <p>To enable this, the worker must have a HTTP replication listener configured,
  12019. have a <code>worker_name</code> and be listed in the <code>instance_map</code> config. The same worker
  12020. can handle multiple streams, but unless otherwise documented, each stream can only
  12021. have a single writer.</p>
  12022. <p>For example, to move event persistence off to a dedicated worker, the shared
  12023. configuration would include:</p>
  12024. <pre><code class="language-yaml">instance_map:
  12025. event_persister1:
  12026. host: localhost
  12027. port: 8034
  12028. stream_writers:
  12029. events: event_persister1
  12030. </code></pre>
  12031. <p>An example for a stream writer instance:</p>
  12032. <pre><code class="language-yaml">worker_app: synapse.app.generic_worker
  12033. worker_name: event_persister1
  12034. # The replication listener on the main synapse process.
  12035. worker_replication_host: 127.0.0.1
  12036. worker_replication_http_port: 9093
  12037. worker_listeners:
  12038. - type: http
  12039. port: 8034
  12040. resources:
  12041. - names: [replication]
  12042. # Enable listener if this stream writer handles endpoints for the `typing` or
  12043. # `to_device` streams. Uses a different port to the `replication` listener to
  12044. # avoid exposing the `replication` listener publicly.
  12045. #
  12046. #- type: http
  12047. # port: 8035
  12048. # resources:
  12049. # - names: [client]
  12050. worker_log_config: /etc/matrix-synapse/event-persister-log.yaml
  12051. </code></pre>
  12052. <p>Some of the streams have associated endpoints which, for maximum efficiency, should
  12053. be routed to the workers handling that stream. See below for the currently supported
  12054. streams and the endpoints associated with them:</p>
  12055. <h5 id="the-events-stream"><a class="header" href="#the-events-stream">The <code>events</code> stream</a></h5>
  12056. <p>The <code>events</code> stream experimentally supports having multiple writers, where work
  12057. is sharded between them by room ID. Note that you <em>must</em> restart all worker
  12058. instances when adding or removing event persisters. An example <code>stream_writers</code>
  12059. configuration with multiple writers:</p>
  12060. <pre><code class="language-yaml">stream_writers:
  12061. events:
  12062. - event_persister1
  12063. - event_persister2
  12064. </code></pre>
  12065. <h5 id="the-typing-stream"><a class="header" href="#the-typing-stream">The <code>typing</code> stream</a></h5>
  12066. <p>The following endpoints should be routed directly to the worker configured as
  12067. the stream writer for the <code>typing</code> stream:</p>
  12068. <pre><code>^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/typing
  12069. </code></pre>
  12070. <h5 id="the-to_device-stream"><a class="header" href="#the-to_device-stream">The <code>to_device</code> stream</a></h5>
  12071. <p>The following endpoints should be routed directly to the worker configured as
  12072. the stream writer for the <code>to_device</code> stream:</p>
  12073. <pre><code>^/_matrix/client/(r0|v3|unstable)/sendToDevice/
  12074. </code></pre>
  12075. <h5 id="the-account_data-stream"><a class="header" href="#the-account_data-stream">The <code>account_data</code> stream</a></h5>
  12076. <p>The following endpoints should be routed directly to the worker configured as
  12077. the stream writer for the <code>account_data</code> stream:</p>
  12078. <pre><code>^/_matrix/client/(r0|v3|unstable)/.*/tags
  12079. ^/_matrix/client/(r0|v3|unstable)/.*/account_data
  12080. </code></pre>
  12081. <h5 id="the-receipts-stream"><a class="header" href="#the-receipts-stream">The <code>receipts</code> stream</a></h5>
  12082. <p>The following endpoints should be routed directly to the worker configured as
  12083. the stream writer for the <code>receipts</code> stream:</p>
  12084. <pre><code>^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt
  12085. ^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers
  12086. </code></pre>
  12087. <h5 id="the-presence-stream"><a class="header" href="#the-presence-stream">The <code>presence</code> stream</a></h5>
  12088. <p>The following endpoints should be routed directly to the worker configured as
  12089. the stream writer for the <code>presence</code> stream:</p>
  12090. <pre><code>^/_matrix/client/(api/v1|r0|v3|unstable)/presence/
  12091. </code></pre>
  12092. <h4 id="background-tasks"><a class="header" href="#background-tasks">Background tasks</a></h4>
  12093. <p>There is also support for moving background tasks to a separate
  12094. worker. Background tasks are run periodically or started via replication. Exactly
  12095. which tasks are configured to run depends on your Synapse configuration (e.g. if
  12096. stats is enabled).</p>
  12097. <p>To enable this, the worker must have a <code>worker_name</code> and can be configured to run
  12098. background tasks. For example, to move background tasks to a dedicated worker,
  12099. the shared configuration would include:</p>
  12100. <pre><code class="language-yaml">run_background_tasks_on: background_worker
  12101. </code></pre>
  12102. <p>You might also wish to investigate the <code>update_user_directory_from_worker</code> and
  12103. <code>media_instance_running_background_jobs</code> settings.</p>
  12104. <p>An example for a dedicated background worker instance:</p>
  12105. <pre><code class="language-yaml">worker_app: synapse.app.generic_worker
  12106. worker_name: background_worker
  12107. # The replication listener on the main synapse process.
  12108. worker_replication_host: 127.0.0.1
  12109. worker_replication_http_port: 9093
  12110. worker_log_config: /etc/matrix-synapse/background-worker-log.yaml
  12111. </code></pre>
  12112. <h4 id="updating-the-user-directory"><a class="header" href="#updating-the-user-directory">Updating the User Directory</a></h4>
  12113. <p>You can designate one generic worker to update the user directory.</p>
  12114. <p>Specify its name in the shared configuration as follows:</p>
  12115. <pre><code class="language-yaml">update_user_directory_from_worker: worker_name
  12116. </code></pre>
  12117. <p>This work cannot be load-balanced; please ensure the main process is restarted
  12118. after setting this option in the shared configuration!</p>
  12119. <p>User directory updates allow REST endpoints matching the following regular
  12120. expressions to work:</p>
  12121. <pre><code>^/_matrix/client/(r0|v3|unstable)/user_directory/search$
  12122. </code></pre>
  12123. <p>The above endpoints can be routed to any worker, though you may choose to route
  12124. it to the chosen user directory worker.</p>
  12125. <p>This style of configuration supersedes the legacy <code>synapse.app.user_dir</code>
  12126. worker application type.</p>
  12127. <h4 id="notifying-application-services"><a class="header" href="#notifying-application-services">Notifying Application Services</a></h4>
  12128. <p>You can designate one generic worker to send output traffic to Application Services.</p>
  12129. <p>Specify its name in the shared configuration as follows:</p>
  12130. <pre><code class="language-yaml">notify_appservices_from_worker: worker_name
  12131. </code></pre>
  12132. <p>This work cannot be load-balanced; please ensure the main process is restarted
  12133. after setting this option in the shared configuration!</p>
  12134. <p>This style of configuration supersedes the legacy <code>synapse.app.appservice</code>
  12135. worker application type.</p>
  12136. <h3 id="synapseapppusher"><a class="header" href="#synapseapppusher"><code>synapse.app.pusher</code></a></h3>
  12137. <p>Handles sending push notifications to sygnal and email. Doesn't handle any
  12138. REST endpoints itself, but you should set <code>start_pushers: False</code> in the
  12139. shared configuration file to stop the main synapse sending push notifications.</p>
  12140. <p>To run multiple instances at once the <code>pusher_instances</code> option should list all
  12141. pusher instances by their worker name, e.g.:</p>
  12142. <pre><code class="language-yaml">pusher_instances:
  12143. - pusher_worker1
  12144. - pusher_worker2
  12145. </code></pre>
  12146. <h3 id="synapseappappservice"><a class="header" href="#synapseappappservice"><code>synapse.app.appservice</code></a></h3>
  12147. <p><strong>Deprecated as of Synapse v1.59.</strong> <a href="workers.html#notifying-application-services">Use <code>synapse.app.generic_worker</code> with the
  12148. <code>notify_appservices_from_worker</code> option instead.</a></p>
  12149. <p>Handles sending output traffic to Application Services. Doesn't handle any
  12150. REST endpoints itself, but you should set <code>notify_appservices: False</code> in the
  12151. shared configuration file to stop the main synapse sending appservice notifications.</p>
  12152. <p>Note this worker cannot be load-balanced: only one instance should be active.</p>
  12153. <h3 id="synapseappfederation_sender"><a class="header" href="#synapseappfederation_sender"><code>synapse.app.federation_sender</code></a></h3>
  12154. <p>Handles sending federation traffic to other servers. Doesn't handle any
  12155. REST endpoints itself, but you should set <code>send_federation: False</code> in the
  12156. shared configuration file to stop the main synapse sending this traffic.</p>
  12157. <p>If running multiple federation senders then you must list each
  12158. instance in the <code>federation_sender_instances</code> option by their <code>worker_name</code>.
  12159. All instances must be stopped and started when adding or removing instances.
  12160. For example:</p>
  12161. <pre><code class="language-yaml">federation_sender_instances:
  12162. - federation_sender1
  12163. - federation_sender2
  12164. </code></pre>
  12165. <h3 id="synapseappmedia_repository"><a class="header" href="#synapseappmedia_repository"><code>synapse.app.media_repository</code></a></h3>
  12166. <p>Handles the media repository. It can handle all endpoints starting with:</p>
  12167. <pre><code>/_matrix/media/
  12168. </code></pre>
  12169. <p>... and the following regular expressions matching media-specific administration APIs:</p>
  12170. <pre><code>^/_synapse/admin/v1/purge_media_cache$
  12171. ^/_synapse/admin/v1/room/.*/media.*$
  12172. ^/_synapse/admin/v1/user/.*/media.*$
  12173. ^/_synapse/admin/v1/media/.*$
  12174. ^/_synapse/admin/v1/quarantine_media/.*$
  12175. ^/_synapse/admin/v1/users/.*/media$
  12176. </code></pre>
  12177. <p>You should also set <code>enable_media_repo: False</code> in the shared configuration
  12178. file to stop the main synapse running background jobs related to managing the
  12179. media repository. Note that doing so will prevent the main process from being
  12180. able to handle the above endpoints.</p>
  12181. <p>In the <code>media_repository</code> worker configuration file, configure the http listener to
  12182. expose the <code>media</code> resource. For example:</p>
  12183. <pre><code class="language-yaml">worker_listeners:
  12184. - type: http
  12185. port: 8085
  12186. resources:
  12187. - names:
  12188. - media
  12189. </code></pre>
  12190. <p>Note that if running multiple media repositories they must be on the same server
  12191. and you must configure a single instance to run the background tasks, e.g.:</p>
  12192. <pre><code class="language-yaml">media_instance_running_background_jobs: &quot;media-repository-1&quot;
  12193. </code></pre>
  12194. <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>
  12195. <h3 id="synapseappuser_dir"><a class="header" href="#synapseappuser_dir"><code>synapse.app.user_dir</code></a></h3>
  12196. <p><strong>Deprecated as of Synapse v1.59.</strong> <a href="workers.html#updating-the-user-directory">Use <code>synapse.app.generic_worker</code> with the
  12197. <code>update_user_directory_from_worker</code> option instead.</a></p>
  12198. <p>Handles searches in the user directory. It can handle REST endpoints matching
  12199. the following regular expressions:</p>
  12200. <pre><code>^/_matrix/client/(r0|v3|unstable)/user_directory/search$
  12201. </code></pre>
  12202. <p>When using this worker you must also set <code>update_user_directory: false</code> in the
  12203. shared configuration file to stop the main synapse running background
  12204. jobs related to updating the user directory.</p>
  12205. <p>Above endpoint is not <em>required</em> to be routed to this worker. By default,
  12206. <code>update_user_directory</code> is set to <code>true</code>, which means the main process
  12207. will handle updates. All workers configured with <code>client</code> can handle the above
  12208. endpoint as long as either this worker or the main process are configured to
  12209. handle it, and are online.</p>
  12210. <p>If <code>update_user_directory</code> is set to <code>false</code>, and this worker is not running,
  12211. the above endpoint may give outdated results.</p>
  12212. <h3 id="synapseappfrontend_proxy"><a class="header" href="#synapseappfrontend_proxy"><code>synapse.app.frontend_proxy</code></a></h3>
  12213. <p>Proxies some frequently-requested client endpoints to add caching and remove
  12214. load from the main synapse. It can handle REST endpoints matching the following
  12215. regular expressions:</p>
  12216. <pre><code>^/_matrix/client/(r0|v3|unstable)/keys/upload
  12217. </code></pre>
  12218. <p>If <code>use_presence</code> is False in the homeserver config, it can also handle REST
  12219. endpoints matching the following regular expressions:</p>
  12220. <pre><code>^/_matrix/client/(api/v1|r0|v3|unstable)/presence/[^/]+/status
  12221. </code></pre>
  12222. <p>This &quot;stub&quot; presence handler will pass through <code>GET</code> request but make the
  12223. <code>PUT</code> effectively a no-op.</p>
  12224. <p>It will proxy any requests it cannot handle to the main synapse instance. It
  12225. must therefore be configured with the location of the main instance, via
  12226. the <code>worker_main_http_uri</code> setting in the <code>frontend_proxy</code> worker configuration
  12227. file. For example:</p>
  12228. <pre><code class="language-yaml">worker_main_http_uri: http://127.0.0.1:8008
  12229. </code></pre>
  12230. <h3 id="historical-apps"><a class="header" href="#historical-apps">Historical apps</a></h3>
  12231. <p><em>Note:</em> Historically there used to be more apps, however they have been
  12232. amalgamated into a single <code>synapse.app.generic_worker</code> app. The remaining apps
  12233. are ones that do specific processing unrelated to requests, e.g. the <code>pusher</code>
  12234. that handles sending out push notifications for new events. The intention is for
  12235. all these to be folded into the <code>generic_worker</code> app and to use config to define
  12236. which processes handle the various proccessing such as push notifications.</p>
  12237. <h2 id="migration-from-old-config"><a class="header" href="#migration-from-old-config">Migration from old config</a></h2>
  12238. <p>There are two main independent changes that have been made: introducing Redis
  12239. support and merging apps into <code>synapse.app.generic_worker</code>. Both these changes
  12240. are backwards compatible and so no changes to the config are required, however
  12241. server admins are encouraged to plan to migrate to Redis as the old style direct
  12242. TCP replication config is deprecated.</p>
  12243. <p>To migrate to Redis add the <code>redis</code> config as above, and optionally remove the
  12244. TCP <code>replication</code> listener from master and <code>worker_replication_port</code> from worker
  12245. config.</p>
  12246. <p>To migrate apps to use <code>synapse.app.generic_worker</code> simply update the
  12247. <code>worker_app</code> option in the worker configs, and where worker are started (e.g.
  12248. in systemd service files, but not required for synctl).</p>
  12249. <h2 id="architectural-diagram"><a class="header" href="#architectural-diagram">Architectural diagram</a></h2>
  12250. <p>The following shows an example setup using Redis and a reverse proxy:</p>
  12251. <pre><code> Clients &amp; Federation
  12252. |
  12253. v
  12254. +-----------+
  12255. | |
  12256. | Reverse |
  12257. | Proxy |
  12258. | |
  12259. +-----------+
  12260. | | |
  12261. | | | HTTP requests
  12262. +-------------------+ | +-----------+
  12263. | +---+ |
  12264. | | |
  12265. v v v
  12266. +--------------+ +--------------+ +--------------+ +--------------+
  12267. | Main | | Generic | | Generic | | Event |
  12268. | Process | | Worker 1 | | Worker 2 | | Persister |
  12269. +--------------+ +--------------+ +--------------+ +--------------+
  12270. ^ ^ | ^ | | ^ | | ^ ^
  12271. | | | | | | | | | | |
  12272. | | | | | HTTP | | | | | |
  12273. | +----------+&lt;--|---|---------+&lt;--|---|---------+ | |
  12274. | | +-------------|--&gt;+-------------+ |
  12275. | | | |
  12276. | | | |
  12277. v v v v
  12278. ======================================================================
  12279. Redis pub/sub channel
  12280. </code></pre>
  12281. <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>
  12282. <p>If you want to use <code>synctl</code> to manage your synapse processes, you will need to
  12283. create an an additional configuration file for the main synapse process. That
  12284. configuration should look like this:</p>
  12285. <pre><code class="language-yaml">worker_app: synapse.app.homeserver
  12286. </code></pre>
  12287. <p>Additionally, each worker app must be configured with the name of a &quot;pid file&quot;,
  12288. to which it will write its process ID when it starts. For example, for a
  12289. synchrotron, you might write:</p>
  12290. <pre><code class="language-yaml">worker_pid_file: /home/matrix/synapse/worker1.pid
  12291. </code></pre>
  12292. <p>Finally, to actually run your worker-based synapse, you must pass synctl the <code>-a</code>
  12293. commandline option to tell it to operate on all the worker configurations found
  12294. in the given directory, e.g.:</p>
  12295. <pre><code class="language-sh">synctl -a $CONFIG/workers start
  12296. </code></pre>
  12297. <p>Currently one should always restart all workers when restarting or upgrading
  12298. synapse, unless you explicitly know it's safe not to. For instance, restarting
  12299. synapse without restarting all the synchrotrons may result in broken typing
  12300. notifications.</p>
  12301. <p>To manipulate a specific worker, you pass the -w option to synctl:</p>
  12302. <pre><code class="language-sh">synctl -w $CONFIG/workers/worker1.yaml restart
  12303. </code></pre>
  12304. <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>
  12305. <p>This is a setup for managing synapse with systemd, including support for
  12306. managing workers. It provides a <code>matrix-synapse</code> service for the master, as
  12307. well as a <code>matrix-synapse-worker@</code> service template for any workers you
  12308. require. Additionally, to group the required services, it sets up a
  12309. <code>matrix-synapse.target</code>.</p>
  12310. <p>See the folder <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/system/">system</a>
  12311. for the systemd unit files.</p>
  12312. <p>The folder <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/workers/">workers</a>
  12313. contains an example configuration for the <code>generic_worker</code> worker.</p>
  12314. <h2 id="synapse-configuration-files"><a class="header" href="#synapse-configuration-files">Synapse configuration files</a></h2>
  12315. <p>See <a href="systemd-with-workers/../workers.html">the worker documentation</a> for information on how to set up the
  12316. configuration files and reverse-proxy correctly.
  12317. Below is a sample <code>generic_worker</code> worker configuration file.</p>
  12318. <pre><code class="language-yaml">worker_app: synapse.app.generic_worker
  12319. worker_name: generic_worker1
  12320. # The replication listener on the main synapse process.
  12321. worker_replication_host: 127.0.0.1
  12322. worker_replication_http_port: 9093
  12323. worker_listeners:
  12324. - type: http
  12325. port: 8083
  12326. resources:
  12327. - names: [client, federation]
  12328. worker_log_config: /etc/matrix-synapse/generic-worker-log.yaml
  12329. </code></pre>
  12330. <p>Systemd manages daemonization itself, so ensure that none of the configuration
  12331. files set either <code>daemonize</code> or <code>worker_daemonize</code>.</p>
  12332. <p>The config files of all workers are expected to be located in
  12333. <code>/etc/matrix-synapse/workers</code>. If you want to use a different location, edit
  12334. the provided <code>*.service</code> files accordingly.</p>
  12335. <p>There is no need for a separate configuration file for the master process.</p>
  12336. <h2 id="set-up"><a class="header" href="#set-up">Set up</a></h2>
  12337. <ol>
  12338. <li>Adjust synapse configuration files as above.</li>
  12339. <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>
  12340. to <code>/etc/systemd/system</code>.</li>
  12341. <li>Run <code>systemctl daemon-reload</code> to tell systemd to load the new unit files.</li>
  12342. <li>Run <code>systemctl enable matrix-synapse.service</code>. This will configure the
  12343. synapse master process to be started as part of the <code>matrix-synapse.target</code>
  12344. target.</li>
  12345. <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
  12346. should be a corresponding configuration file.
  12347. <code>/etc/matrix-synapse/workers/&lt;worker_name&gt;.yaml</code>.</li>
  12348. <li>Start all the synapse processes with <code>systemctl start matrix-synapse.target</code>.</li>
  12349. <li>Tell systemd to start synapse on boot with <code>systemctl enable matrix-synapse.target</code>.</li>
  12350. </ol>
  12351. <h2 id="usage"><a class="header" href="#usage">Usage</a></h2>
  12352. <p>Once the services are correctly set up, you can use the following commands
  12353. to manage your synapse installation:</p>
  12354. <pre><code class="language-sh"># Restart Synapse master and all workers
  12355. systemctl restart matrix-synapse.target
  12356. # Stop Synapse and all workers
  12357. systemctl stop matrix-synapse.target
  12358. # Restart the master alone
  12359. systemctl start matrix-synapse.service
  12360. # Restart a specific worker (eg. generic_worker); the master is
  12361. # unaffected by this.
  12362. systemctl restart matrix-synapse-worker@generic_worker.service
  12363. # Add a new worker (assuming all configs are set up already)
  12364. systemctl enable matrix-synapse-worker@federation_writer.service
  12365. systemctl restart matrix-synapse.target
  12366. </code></pre>
  12367. <h2 id="hardening"><a class="header" href="#hardening">Hardening</a></h2>
  12368. <p><strong>Optional:</strong> If further hardening is desired, the file
  12369. <code>override-hardened.conf</code> may be copied from
  12370. <a href="https://github.com/matrix-org/synapse/tree/develop/contrib/systemd/">contrib/systemd/override-hardened.conf</a>
  12371. in this repository to the location
  12372. <code>/etc/systemd/system/matrix-synapse.service.d/override-hardened.conf</code> (the
  12373. directory may have to be created). It enables certain sandboxing features in
  12374. systemd to further secure the synapse service. You may read the comments to
  12375. understand what the override file is doing. The same file will need to be copied to
  12376. <code>/etc/systemd/system/matrix-synapse-worker@.service.d/override-hardened-worker.conf</code>
  12377. (this directory may also have to be created) in order to apply the same
  12378. hardening options to any worker processes.</p>
  12379. <p>Once these files have been copied to their appropriate locations, simply reload
  12380. systemd's manager config files and restart all Synapse services to apply the hardening options. They will automatically
  12381. be applied at every restart as long as the override files are present at the
  12382. specified locations.</p>
  12383. <pre><code class="language-sh">systemctl daemon-reload
  12384. # Restart services
  12385. systemctl restart matrix-synapse.target
  12386. </code></pre>
  12387. <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
  12388. the changes being applied at a glance.</p>
  12389. <div style="break-before: page; page-break-before: always;"></div><h1 id="administration"><a class="header" href="#administration">Administration</a></h1>
  12390. <p>This section contains information on managing your Synapse homeserver. This includes:</p>
  12391. <ul>
  12392. <li>Managing users, rooms and media via the Admin API.</li>
  12393. <li>Setting up metrics and monitoring to give you insight into your homeserver's health.</li>
  12394. <li>Configuring structured logging.</li>
  12395. </ul>
  12396. <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>
  12397. <h2 id="authenticate-as-a-server-admin"><a class="header" href="#authenticate-as-a-server-admin">Authenticate as a server admin</a></h2>
  12398. <p>Many of the API calls in the admin api will require an <code>access_token</code> for a
  12399. server admin. (Note that a server admin is distinct from a room admin.)</p>
  12400. <p>A user can be marked as a server admin by updating the database directly, e.g.:</p>
  12401. <pre><code class="language-sql">UPDATE users SET admin = 1 WHERE name = '@foo:bar.com';
  12402. </code></pre>
  12403. <p>A new server admin user can also be created using the <code>register_new_matrix_user</code>
  12404. command. This is a script that is distributed as part of synapse. It is possibly
  12405. already on your <code>$PATH</code> depending on how Synapse was installed.</p>
  12406. <p>Finding your user's <code>access_token</code> is client-dependent, but will usually be shown in the client's settings.</p>
  12407. <h2 id="making-an-admin-api-request"><a class="header" href="#making-an-admin-api-request">Making an Admin API request</a></h2>
  12408. <p>Once you have your <code>access_token</code>, you will need to authenticate each request to an Admin API endpoint by
  12409. providing the token as either a query parameter or a request header. To add it as a request header in cURL:</p>
  12410. <pre><code class="language-sh">curl --header &quot;Authorization: Bearer &lt;access_token&gt;&quot; &lt;the_rest_of_your_API_request&gt;
  12411. </code></pre>
  12412. <p>For more details on access tokens in Matrix, please refer to the complete
  12413. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#using-access-tokens">matrix spec documentation</a>.</p>
  12414. <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>
  12415. <p>This API allows a server administrator to manage the validity of an account. To
  12416. use it, you must enable the account validity feature (under
  12417. <code>account_validity</code>) in Synapse's configuration.</p>
  12418. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  12419. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  12420. <h2 id="renew-account"><a class="header" href="#renew-account">Renew account</a></h2>
  12421. <p>This API extends the validity of an account by as much time as configured in the
  12422. <code>period</code> parameter from the <code>account_validity</code> configuration.</p>
  12423. <p>The API is:</p>
  12424. <pre><code>POST /_synapse/admin/v1/account_validity/validity
  12425. </code></pre>
  12426. <p>with the following body:</p>
  12427. <pre><code class="language-json">{
  12428. &quot;user_id&quot;: &quot;&lt;user ID for the account to renew&gt;&quot;,
  12429. &quot;expiration_ts&quot;: 0,
  12430. &quot;enable_renewal_emails&quot;: true
  12431. }
  12432. </code></pre>
  12433. <p><code>expiration_ts</code> is an optional parameter and overrides the expiration date,
  12434. which otherwise defaults to now + validity period.</p>
  12435. <p><code>enable_renewal_emails</code> is also an optional parameter and enables/disables
  12436. sending renewal emails to the user. Defaults to true.</p>
  12437. <p>The API returns with the new expiration date for this account, as a timestamp in
  12438. milliseconds since epoch:</p>
  12439. <pre><code class="language-json">{
  12440. &quot;expiration_ts&quot;: 0
  12441. }
  12442. </code></pre>
  12443. <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>
  12444. <p>This API allows a server administrator to manage the background updates being
  12445. run against the database.</p>
  12446. <h2 id="status"><a class="header" href="#status">Status</a></h2>
  12447. <p>This API gets the current status of the background updates.</p>
  12448. <p>The API is:</p>
  12449. <pre><code>GET /_synapse/admin/v1/background_updates/status
  12450. </code></pre>
  12451. <p>Returning:</p>
  12452. <pre><code class="language-json">{
  12453. &quot;enabled&quot;: true,
  12454. &quot;current_updates&quot;: {
  12455. &quot;&lt;db_name&gt;&quot;: {
  12456. &quot;name&quot;: &quot;&lt;background_update_name&gt;&quot;,
  12457. &quot;total_item_count&quot;: 50,
  12458. &quot;total_duration_ms&quot;: 10000.0,
  12459. &quot;average_items_per_ms&quot;: 2.2,
  12460. },
  12461. }
  12462. }
  12463. </code></pre>
  12464. <p><code>enabled</code> whether the background updates are enabled or disabled.</p>
  12465. <p><code>db_name</code> the database name (usually Synapse is configured with a single database named 'master').</p>
  12466. <p>For each update:</p>
  12467. <p><code>name</code> the name of the update.
  12468. <code>total_item_count</code> total number of &quot;items&quot; processed (the meaning of 'items' depends on the update in question).
  12469. <code>total_duration_ms</code> how long the background process has been running, not including time spent sleeping.
  12470. <code>average_items_per_ms</code> how many items are processed per millisecond based on an exponential average.</p>
  12471. <h2 id="enabled"><a class="header" href="#enabled">Enabled</a></h2>
  12472. <p>This API allow pausing background updates.</p>
  12473. <p>Background updates should <em>not</em> be paused for significant periods of time, as
  12474. this can affect the performance of Synapse.</p>
  12475. <p><em>Note</em>: This won't persist over restarts.</p>
  12476. <p><em>Note</em>: This won't cancel any update query that is currently running. This is
  12477. usually fine since most queries are short lived, except for <code>CREATE INDEX</code>
  12478. background updates which won't be cancelled once started.</p>
  12479. <p>The API is:</p>
  12480. <pre><code>POST /_synapse/admin/v1/background_updates/enabled
  12481. </code></pre>
  12482. <p>with the following body:</p>
  12483. <pre><code class="language-json">{
  12484. &quot;enabled&quot;: false
  12485. }
  12486. </code></pre>
  12487. <p><code>enabled</code> sets whether the background updates are enabled or disabled.</p>
  12488. <p>The API returns the <code>enabled</code> param.</p>
  12489. <pre><code class="language-json">{
  12490. &quot;enabled&quot;: false
  12491. }
  12492. </code></pre>
  12493. <p>There is also a <code>GET</code> version which returns the <code>enabled</code> state.</p>
  12494. <h2 id="run"><a class="header" href="#run">Run</a></h2>
  12495. <p>This API schedules a specific background update to run. The job starts immediately after calling the API.</p>
  12496. <p>The API is:</p>
  12497. <pre><code>POST /_synapse/admin/v1/background_updates/start_job
  12498. </code></pre>
  12499. <p>with the following body:</p>
  12500. <pre><code class="language-json">{
  12501. &quot;job_name&quot;: &quot;populate_stats_process_rooms&quot;
  12502. }
  12503. </code></pre>
  12504. <p>The following JSON body parameters are available:</p>
  12505. <ul>
  12506. <li><code>job_name</code> - A string which job to run. Valid values are:
  12507. <ul>
  12508. <li><code>populate_stats_process_rooms</code> - Recalculate the stats for all rooms.</li>
  12509. <li><code>regenerate_directory</code> - Recalculate the <a href="usage/administration/admin_api/../../../user_directory.html">user directory</a> if it is stale or out of sync.</li>
  12510. </ul>
  12511. </li>
  12512. </ul>
  12513. <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>
  12514. <p>This API lets a server admin delete a local group. Doing so will kick all
  12515. users out of the group so that their clients will correctly handle the group
  12516. being deleted.</p>
  12517. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  12518. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  12519. <p>The API is:</p>
  12520. <pre><code>POST /_synapse/admin/v1/delete_group/&lt;group_id&gt;
  12521. </code></pre>
  12522. <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>
  12523. <p>This API returns information about reported events.</p>
  12524. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  12525. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  12526. <p>The api is:</p>
  12527. <pre><code>GET /_synapse/admin/v1/event_reports?from=0&amp;limit=10
  12528. </code></pre>
  12529. <p>It returns a JSON body like the following:</p>
  12530. <pre><code class="language-json">{
  12531. &quot;event_reports&quot;: [
  12532. {
  12533. &quot;event_id&quot;: &quot;$bNUFCwGzWca1meCGkjp-zwslF-GfVcXukvRLI1_FaVY&quot;,
  12534. &quot;id&quot;: 2,
  12535. &quot;reason&quot;: &quot;foo&quot;,
  12536. &quot;score&quot;: -100,
  12537. &quot;received_ts&quot;: 1570897107409,
  12538. &quot;canonical_alias&quot;: &quot;#alias1:matrix.org&quot;,
  12539. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  12540. &quot;name&quot;: &quot;Matrix HQ&quot;,
  12541. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  12542. &quot;user_id&quot;: &quot;@foo:matrix.org&quot;
  12543. },
  12544. {
  12545. &quot;event_id&quot;: &quot;$3IcdZsDaN_En-S1DF4EMCy3v4gNRKeOJs8W5qTOKj4I&quot;,
  12546. &quot;id&quot;: 3,
  12547. &quot;reason&quot;: &quot;bar&quot;,
  12548. &quot;score&quot;: -100,
  12549. &quot;received_ts&quot;: 1598889612059,
  12550. &quot;canonical_alias&quot;: &quot;#alias2:matrix.org&quot;,
  12551. &quot;room_id&quot;: &quot;!eGvUQuTCkHGVwNMOjv:matrix.org&quot;,
  12552. &quot;name&quot;: &quot;Your room name here&quot;,
  12553. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  12554. &quot;user_id&quot;: &quot;@bar:matrix.org&quot;
  12555. }
  12556. ],
  12557. &quot;next_token&quot;: 2,
  12558. &quot;total&quot;: 4
  12559. }
  12560. </code></pre>
  12561. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again with <code>from</code>
  12562. set to the value of <code>next_token</code>. This will return a new page.</p>
  12563. <p>If the endpoint does not return a <code>next_token</code> then there are no more reports to
  12564. paginate through.</p>
  12565. <p><strong>URL parameters:</strong></p>
  12566. <ul>
  12567. <li><code>limit</code>: integer - Is optional but is used for pagination, denoting the maximum number
  12568. of items to return in this call. Defaults to <code>100</code>.</li>
  12569. <li><code>from</code>: integer - Is optional but used for pagination, denoting the offset in the
  12570. returned results. This should be treated as an opaque value and not explicitly set to
  12571. anything other than the return value of <code>next_token</code> from a previous call. Defaults to <code>0</code>.</li>
  12572. <li><code>dir</code>: string - Direction of event report order. Whether to fetch the most recent
  12573. first (<code>b</code>) or the oldest first (<code>f</code>). Defaults to <code>b</code>.</li>
  12574. <li><code>user_id</code>: string - Is optional and filters to only return users with user IDs that
  12575. contain this value. This is the user who reported the event and wrote the reason.</li>
  12576. <li><code>room_id</code>: string - Is optional and filters to only return rooms with room IDs that
  12577. contain this value.</li>
  12578. </ul>
  12579. <p><strong>Response</strong></p>
  12580. <p>The following fields are returned in the JSON response body:</p>
  12581. <ul>
  12582. <li><code>id</code>: integer - ID of event report.</li>
  12583. <li><code>received_ts</code>: integer - The timestamp (in milliseconds since the unix epoch) when this
  12584. report was sent.</li>
  12585. <li><code>room_id</code>: string - The ID of the room in which the event being reported is located.</li>
  12586. <li><code>name</code>: string - The name of the room.</li>
  12587. <li><code>event_id</code>: string - The ID of the reported event.</li>
  12588. <li><code>user_id</code>: string - This is the user who reported the event and wrote the reason.</li>
  12589. <li><code>reason</code>: string - Comment made by the <code>user_id</code> in this report. May be blank or <code>null</code>.</li>
  12590. <li><code>score</code>: integer - Content is reported based upon a negative score, where -100 is
  12591. &quot;most offensive&quot; and 0 is &quot;inoffensive&quot;. May be <code>null</code>.</li>
  12592. <li><code>sender</code>: string - This is the ID of the user who sent the original message/event that
  12593. was reported.</li>
  12594. <li><code>canonical_alias</code>: string - The canonical alias of the room. <code>null</code> if the room does not
  12595. have a canonical alias set.</li>
  12596. <li><code>next_token</code>: integer - Indication for pagination. See above.</li>
  12597. <li><code>total</code>: integer - Total number of event reports related to the query
  12598. (<code>user_id</code> and <code>room_id</code>).</li>
  12599. </ul>
  12600. <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>
  12601. <p>This API returns information about a specific event report.</p>
  12602. <p>The api is:</p>
  12603. <pre><code>GET /_synapse/admin/v1/event_reports/&lt;report_id&gt;
  12604. </code></pre>
  12605. <p>It returns a JSON body like the following:</p>
  12606. <pre><code class="language-json">{
  12607. &quot;event_id&quot;: &quot;$bNUFCwGzWca1meCGkjp-zwslF-GfVcXukvRLI1_FaVY&quot;,
  12608. &quot;event_json&quot;: {
  12609. &quot;auth_events&quot;: [
  12610. &quot;$YK4arsKKcc0LRoe700pS8DSjOvUT4NDv0HfInlMFw2M&quot;,
  12611. &quot;$oggsNXxzPFRE3y53SUNd7nsj69-QzKv03a1RucHu-ws&quot;
  12612. ],
  12613. &quot;content&quot;: {
  12614. &quot;body&quot;: &quot;matrix.org: This Week in Matrix&quot;,
  12615. &quot;format&quot;: &quot;org.matrix.custom.html&quot;,
  12616. &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;,
  12617. &quot;msgtype&quot;: &quot;m.notice&quot;
  12618. },
  12619. &quot;depth&quot;: 546,
  12620. &quot;hashes&quot;: {
  12621. &quot;sha256&quot;: &quot;xK1//xnmvHJIOvbgXlkI8eEqdvoMmihVDJ9J4SNlsAw&quot;
  12622. },
  12623. &quot;origin&quot;: &quot;matrix.org&quot;,
  12624. &quot;origin_server_ts&quot;: 1592291711430,
  12625. &quot;prev_events&quot;: [
  12626. &quot;$YK4arsKKcc0LRoe700pS8DSjOvUT4NDv0HfInlMFw2M&quot;
  12627. ],
  12628. &quot;prev_state&quot;: [],
  12629. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  12630. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  12631. &quot;signatures&quot;: {
  12632. &quot;matrix.org&quot;: {
  12633. &quot;ed25519:a_JaEG&quot;: &quot;cs+OUKW/iHx5pEidbWxh0UiNNHwe46Ai9LwNz+Ah16aWDNszVIe2gaAcVZfvNsBhakQTew51tlKmL2kspXk/Dg&quot;
  12634. }
  12635. },
  12636. &quot;type&quot;: &quot;m.room.message&quot;,
  12637. &quot;unsigned&quot;: {
  12638. &quot;age_ts&quot;: 1592291711430
  12639. }
  12640. },
  12641. &quot;id&quot;: &lt;report_id&gt;,
  12642. &quot;reason&quot;: &quot;foo&quot;,
  12643. &quot;score&quot;: -100,
  12644. &quot;received_ts&quot;: 1570897107409,
  12645. &quot;canonical_alias&quot;: &quot;#alias1:matrix.org&quot;,
  12646. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  12647. &quot;name&quot;: &quot;Matrix HQ&quot;,
  12648. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  12649. &quot;user_id&quot;: &quot;@foo:matrix.org&quot;
  12650. }
  12651. </code></pre>
  12652. <p><strong>URL parameters:</strong></p>
  12653. <ul>
  12654. <li><code>report_id</code>: string - The ID of the event report.</li>
  12655. </ul>
  12656. <p><strong>Response</strong></p>
  12657. <p>The following fields are returned in the JSON response body:</p>
  12658. <ul>
  12659. <li><code>id</code>: integer - ID of event report.</li>
  12660. <li><code>received_ts</code>: integer - The timestamp (in milliseconds since the unix epoch) when this
  12661. report was sent.</li>
  12662. <li><code>room_id</code>: string - The ID of the room in which the event being reported is located.</li>
  12663. <li><code>name</code>: string - The name of the room.</li>
  12664. <li><code>event_id</code>: string - The ID of the reported event.</li>
  12665. <li><code>user_id</code>: string - This is the user who reported the event and wrote the reason.</li>
  12666. <li><code>reason</code>: string - Comment made by the <code>user_id</code> in this report. May be blank.</li>
  12667. <li><code>score</code>: integer - Content is reported based upon a negative score, where -100 is
  12668. &quot;most offensive&quot; and 0 is &quot;inoffensive&quot;.</li>
  12669. <li><code>sender</code>: string - This is the ID of the user who sent the original message/event that
  12670. was reported.</li>
  12671. <li><code>canonical_alias</code>: string - The canonical alias of the room. <code>null</code> if the room does not
  12672. have a canonical alias set.</li>
  12673. <li><code>event_json</code>: object - Details of the original event that was reported.</li>
  12674. </ul>
  12675. <div style="break-before: page; page-break-before: always;"></div><h1 id="querying-media"><a class="header" href="#querying-media">Querying media</a></h1>
  12676. <p>These APIs allow extracting media information from the homeserver.</p>
  12677. <p>Details about the format of the <code>media_id</code> and storage of the media in the file system
  12678. are documented under <a href="admin_api/../media_repository.html">media repository</a>.</p>
  12679. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  12680. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  12681. <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>
  12682. <p>This API gets a list of known media in a room.
  12683. However, it only shows media from unencrypted events or rooms.</p>
  12684. <p>The API is:</p>
  12685. <pre><code>GET /_synapse/admin/v1/room/&lt;room_id&gt;/media
  12686. </code></pre>
  12687. <p>The API returns a JSON body like the following:</p>
  12688. <pre><code class="language-json">{
  12689. &quot;local&quot;: [
  12690. &quot;mxc://localhost/xwvutsrqponmlkjihgfedcba&quot;,
  12691. &quot;mxc://localhost/abcdefghijklmnopqrstuvwx&quot;
  12692. ],
  12693. &quot;remote&quot;: [
  12694. &quot;mxc://matrix.org/xwvutsrqponmlkjihgfedcba&quot;,
  12695. &quot;mxc://matrix.org/abcdefghijklmnopqrstuvwx&quot;
  12696. ]
  12697. }
  12698. </code></pre>
  12699. <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>
  12700. <p>Listing all media that has been uploaded by a local user can be achieved through
  12701. the use of the
  12702. <a href="admin_api/user_admin_api.html#list-media-uploaded-by-a-user">List media uploaded by a user</a>
  12703. Admin API.</p>
  12704. <h1 id="quarantine-media"><a class="header" href="#quarantine-media">Quarantine media</a></h1>
  12705. <p>Quarantining media means that it is marked as inaccessible by users. It applies
  12706. to any local media, and any locally-cached copies of remote media.</p>
  12707. <p>The media file itself (and any thumbnails) is not deleted from the server.</p>
  12708. <h2 id="quarantining-media-by-id"><a class="header" href="#quarantining-media-by-id">Quarantining media by ID</a></h2>
  12709. <p>This API quarantines a single piece of local or remote media.</p>
  12710. <p>Request:</p>
  12711. <pre><code>POST /_synapse/admin/v1/media/quarantine/&lt;server_name&gt;/&lt;media_id&gt;
  12712. {}
  12713. </code></pre>
  12714. <p>Where <code>server_name</code> is in the form of <code>example.org</code>, and <code>media_id</code> is in the
  12715. form of <code>abcdefg12345...</code>.</p>
  12716. <p>Response:</p>
  12717. <pre><code class="language-json">{}
  12718. </code></pre>
  12719. <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>
  12720. <p>This API removes a single piece of local or remote media from quarantine.</p>
  12721. <p>Request:</p>
  12722. <pre><code>POST /_synapse/admin/v1/media/unquarantine/&lt;server_name&gt;/&lt;media_id&gt;
  12723. {}
  12724. </code></pre>
  12725. <p>Where <code>server_name</code> is in the form of <code>example.org</code>, and <code>media_id</code> is in the
  12726. form of <code>abcdefg12345...</code>.</p>
  12727. <p>Response:</p>
  12728. <pre><code class="language-json">{}
  12729. </code></pre>
  12730. <h2 id="quarantining-media-in-a-room"><a class="header" href="#quarantining-media-in-a-room">Quarantining media in a room</a></h2>
  12731. <p>This API quarantines all local and remote media in a room.</p>
  12732. <p>Request:</p>
  12733. <pre><code>POST /_synapse/admin/v1/room/&lt;room_id&gt;/media/quarantine
  12734. {}
  12735. </code></pre>
  12736. <p>Where <code>room_id</code> is in the form of <code>!roomid12345:example.org</code>.</p>
  12737. <p>Response:</p>
  12738. <pre><code class="language-json">{
  12739. &quot;num_quarantined&quot;: 10
  12740. }
  12741. </code></pre>
  12742. <p>The following fields are returned in the JSON response body:</p>
  12743. <ul>
  12744. <li><code>num_quarantined</code>: integer - The number of media items successfully quarantined</li>
  12745. </ul>
  12746. <p>Note that there is a legacy endpoint, <code>POST /_synapse/admin/v1/quarantine_media/&lt;room_id&gt;</code>, that operates the same.
  12747. However, it is deprecated and may be removed in a future release.</p>
  12748. <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>
  12749. <p>This API quarantines all <em>local</em> media that a <em>local</em> user has uploaded. That is to say, if
  12750. you would like to quarantine media uploaded by a user on a remote homeserver, you should
  12751. instead use one of the other APIs.</p>
  12752. <p>Request:</p>
  12753. <pre><code>POST /_synapse/admin/v1/user/&lt;user_id&gt;/media/quarantine
  12754. {}
  12755. </code></pre>
  12756. <p>URL Parameters</p>
  12757. <ul>
  12758. <li><code>user_id</code>: string - User ID in the form of <code>@bob:example.org</code></li>
  12759. </ul>
  12760. <p>Response:</p>
  12761. <pre><code class="language-json">{
  12762. &quot;num_quarantined&quot;: 10
  12763. }
  12764. </code></pre>
  12765. <p>The following fields are returned in the JSON response body:</p>
  12766. <ul>
  12767. <li><code>num_quarantined</code>: integer - The number of media items successfully quarantined</li>
  12768. </ul>
  12769. <h2 id="protecting-media-from-being-quarantined"><a class="header" href="#protecting-media-from-being-quarantined">Protecting media from being quarantined</a></h2>
  12770. <p>This API protects a single piece of local media from being quarantined using the
  12771. above APIs. This is useful for sticker packs and other shared media which you do
  12772. not want to get quarantined, especially when
  12773. <a href="admin_api/media_admin_api.html#quarantining-media-in-a-room">quarantining media in a room</a>.</p>
  12774. <p>Request:</p>
  12775. <pre><code>POST /_synapse/admin/v1/media/protect/&lt;media_id&gt;
  12776. {}
  12777. </code></pre>
  12778. <p>Where <code>media_id</code> is in the form of <code>abcdefg12345...</code>.</p>
  12779. <p>Response:</p>
  12780. <pre><code class="language-json">{}
  12781. </code></pre>
  12782. <h2 id="unprotecting-media-from-being-quarantined"><a class="header" href="#unprotecting-media-from-being-quarantined">Unprotecting media from being quarantined</a></h2>
  12783. <p>This API reverts the protection of a media.</p>
  12784. <p>Request:</p>
  12785. <pre><code>POST /_synapse/admin/v1/media/unprotect/&lt;media_id&gt;
  12786. {}
  12787. </code></pre>
  12788. <p>Where <code>media_id</code> is in the form of <code>abcdefg12345...</code>.</p>
  12789. <p>Response:</p>
  12790. <pre><code class="language-json">{}
  12791. </code></pre>
  12792. <h1 id="delete-local-media"><a class="header" href="#delete-local-media">Delete local media</a></h1>
  12793. <p>This API deletes the <em>local</em> media from the disk of your own server.
  12794. This includes any local thumbnails and copies of media downloaded from
  12795. remote homeservers.
  12796. This API will not affect media that has been uploaded to external
  12797. media repositories (e.g https://github.com/turt2live/matrix-media-repo/).
  12798. See also <a href="admin_api/media_admin_api.html#purge-remote-media-api">Purge Remote Media API</a>.</p>
  12799. <h2 id="delete-a-specific-local-media"><a class="header" href="#delete-a-specific-local-media">Delete a specific local media</a></h2>
  12800. <p>Delete a specific <code>media_id</code>.</p>
  12801. <p>Request:</p>
  12802. <pre><code>DELETE /_synapse/admin/v1/media/&lt;server_name&gt;/&lt;media_id&gt;
  12803. {}
  12804. </code></pre>
  12805. <p>URL Parameters</p>
  12806. <ul>
  12807. <li><code>server_name</code>: string - The name of your local server (e.g <code>matrix.org</code>)</li>
  12808. <li><code>media_id</code>: string - The ID of the media (e.g <code>abcdefghijklmnopqrstuvwx</code>)</li>
  12809. </ul>
  12810. <p>Response:</p>
  12811. <pre><code class="language-json">{
  12812. &quot;deleted_media&quot;: [
  12813. &quot;abcdefghijklmnopqrstuvwx&quot;
  12814. ],
  12815. &quot;total&quot;: 1
  12816. }
  12817. </code></pre>
  12818. <p>The following fields are returned in the JSON response body:</p>
  12819. <ul>
  12820. <li><code>deleted_media</code>: an array of strings - List of deleted <code>media_id</code></li>
  12821. <li><code>total</code>: integer - Total number of deleted <code>media_id</code></li>
  12822. </ul>
  12823. <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>
  12824. <p>Request:</p>
  12825. <pre><code>POST /_synapse/admin/v1/media/&lt;server_name&gt;/delete?before_ts=&lt;before_ts&gt;
  12826. {}
  12827. </code></pre>
  12828. <p>URL Parameters</p>
  12829. <ul>
  12830. <li><code>server_name</code>: string - The name of your local server (e.g <code>matrix.org</code>).</li>
  12831. <li><code>before_ts</code>: string representing a positive integer - Unix timestamp in milliseconds.
  12832. Files that were last used before this timestamp will be deleted. It is the timestamp of
  12833. last access, not the timestamp when the file was created.</li>
  12834. <li><code>size_gt</code>: Optional - string representing a positive integer - Size of the media in bytes.
  12835. Files that are larger will be deleted. Defaults to <code>0</code>.</li>
  12836. <li><code>keep_profiles</code>: Optional - string representing a boolean - Switch to also delete files
  12837. that are still used in image data (e.g user profile, room avatar).
  12838. If <code>false</code> these files will be deleted. Defaults to <code>true</code>.</li>
  12839. </ul>
  12840. <p>Response:</p>
  12841. <pre><code class="language-json">{
  12842. &quot;deleted_media&quot;: [
  12843. &quot;abcdefghijklmnopqrstuvwx&quot;,
  12844. &quot;abcdefghijklmnopqrstuvwz&quot;
  12845. ],
  12846. &quot;total&quot;: 2
  12847. }
  12848. </code></pre>
  12849. <p>The following fields are returned in the JSON response body:</p>
  12850. <ul>
  12851. <li><code>deleted_media</code>: an array of strings - List of deleted <code>media_id</code></li>
  12852. <li><code>total</code>: integer - Total number of deleted <code>media_id</code></li>
  12853. </ul>
  12854. <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>
  12855. <p>You can find details of how to delete multiple media uploaded by a user in
  12856. <a href="admin_api/user_admin_api.html#delete-media-uploaded-by-a-user">User Admin API</a>.</p>
  12857. <h1 id="purge-remote-media-api"><a class="header" href="#purge-remote-media-api">Purge Remote Media API</a></h1>
  12858. <p>The purge remote media API allows server admins to purge old cached remote media.</p>
  12859. <p>The API is:</p>
  12860. <pre><code>POST /_synapse/admin/v1/purge_media_cache?before_ts=&lt;unix_timestamp_in_ms&gt;
  12861. {}
  12862. </code></pre>
  12863. <p>URL Parameters</p>
  12864. <ul>
  12865. <li><code>before_ts</code>: string representing a positive integer - Unix timestamp in milliseconds.
  12866. All cached media that was last accessed before this timestamp will be removed.</li>
  12867. </ul>
  12868. <p>Response:</p>
  12869. <pre><code class="language-json">{
  12870. &quot;deleted&quot;: 10
  12871. }
  12872. </code></pre>
  12873. <p>The following fields are returned in the JSON response body:</p>
  12874. <ul>
  12875. <li><code>deleted</code>: integer - The number of media items successfully deleted</li>
  12876. </ul>
  12877. <p>If the user re-requests purged remote media, synapse will re-request the media
  12878. from the originating server.</p>
  12879. <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>
  12880. <p>The purge history API allows server admins to purge historic events from their
  12881. database, reclaiming disk space.</p>
  12882. <p>Depending on the amount of history being purged a call to the API may take
  12883. several minutes or longer. During this period users will not be able to
  12884. paginate further back in the room from the point being purged from.</p>
  12885. <p>Note that Synapse requires at least one message in each room, so it will never
  12886. delete the last message in a room.</p>
  12887. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  12888. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  12889. <p>The API is:</p>
  12890. <pre><code>POST /_synapse/admin/v1/purge_history/&lt;room_id&gt;[/&lt;event_id&gt;]
  12891. </code></pre>
  12892. <p>By default, events sent by local users are not deleted, as they may represent
  12893. the only copies of this content in existence. (Events sent by remote users are
  12894. deleted.)</p>
  12895. <p>Room state data (such as joins, leaves, topic) is always preserved.</p>
  12896. <p>To delete local message events as well, set <code>delete_local_events</code> in the body:</p>
  12897. <pre><code class="language-json">{
  12898. &quot;delete_local_events&quot;: true
  12899. }
  12900. </code></pre>
  12901. <p>The caller must specify the point in the room to purge up to. This can be
  12902. specified by including an event_id in the URI, or by setting a
  12903. <code>purge_up_to_event_id</code> or <code>purge_up_to_ts</code> in the request body. If an event
  12904. id is given, that event (and others at the same graph depth) will be retained.
  12905. If <code>purge_up_to_ts</code> is given, it should be a timestamp since the unix epoch,
  12906. in milliseconds.</p>
  12907. <p>The API starts the purge running, and returns immediately with a JSON body with
  12908. a purge id:</p>
  12909. <pre><code class="language-json">{
  12910. &quot;purge_id&quot;: &quot;&lt;opaque id&gt;&quot;
  12911. }
  12912. </code></pre>
  12913. <h2 id="purge-status-query"><a class="header" href="#purge-status-query">Purge status query</a></h2>
  12914. <p>It is possible to poll for updates on recent purges with a second API;</p>
  12915. <pre><code>GET /_synapse/admin/v1/purge_history_status/&lt;purge_id&gt;
  12916. </code></pre>
  12917. <p>This API returns a JSON body like the following:</p>
  12918. <pre><code class="language-json">{
  12919. &quot;status&quot;: &quot;active&quot;
  12920. }
  12921. </code></pre>
  12922. <p>The status will be one of <code>active</code>, <code>complete</code>, or <code>failed</code>.</p>
  12923. <p>If <code>status</code> is <code>failed</code> there will be a string <code>error</code> with the error message.</p>
  12924. <h2 id="reclaim-disk-space-postgres"><a class="header" href="#reclaim-disk-space-postgres">Reclaim disk space (Postgres)</a></h2>
  12925. <p>To reclaim the disk space and return it to the operating system, you need to run
  12926. <code>VACUUM FULL;</code> on the database.</p>
  12927. <p><a href="https://www.postgresql.org/docs/current/sql-vacuum.html">https://www.postgresql.org/docs/current/sql-vacuum.html</a></p>
  12928. <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>
  12929. <p>This API allows for the creation of users in an administrative and
  12930. non-interactive way. This is generally used for bootstrapping a Synapse
  12931. instance with administrator accounts.</p>
  12932. <p>To authenticate yourself to the server, you will need both the shared secret
  12933. (<code>registration_shared_secret</code> in the homeserver configuration), and a
  12934. one-time nonce. If the registration shared secret is not configured, this API
  12935. is not enabled.</p>
  12936. <p>To fetch the nonce, you need to request one from the API:</p>
  12937. <pre><code>&gt; GET /_synapse/admin/v1/register
  12938. &lt; {&quot;nonce&quot;: &quot;thisisanonce&quot;}
  12939. </code></pre>
  12940. <p>Once you have the nonce, you can make a <code>POST</code> to the same URL with a JSON
  12941. body containing the nonce, username, password, whether they are an admin
  12942. (optional, False by default), and a HMAC digest of the content. Also you can
  12943. set the displayname (optional, <code>username</code> by default).</p>
  12944. <p>As an example:</p>
  12945. <pre><code>&gt; POST /_synapse/admin/v1/register
  12946. &gt; {
  12947. &quot;nonce&quot;: &quot;thisisanonce&quot;,
  12948. &quot;username&quot;: &quot;pepper_roni&quot;,
  12949. &quot;displayname&quot;: &quot;Pepper Roni&quot;,
  12950. &quot;password&quot;: &quot;pizza&quot;,
  12951. &quot;admin&quot;: true,
  12952. &quot;mac&quot;: &quot;mac_digest_here&quot;
  12953. }
  12954. &lt; {
  12955. &quot;access_token&quot;: &quot;token_here&quot;,
  12956. &quot;user_id&quot;: &quot;@pepper_roni:localhost&quot;,
  12957. &quot;home_server&quot;: &quot;test&quot;,
  12958. &quot;device_id&quot;: &quot;device_id_here&quot;
  12959. }
  12960. </code></pre>
  12961. <p>The MAC is the hex digest output of the HMAC-SHA1 algorithm, with the key being
  12962. the shared secret and the content being the nonce, user, password, either the
  12963. string &quot;admin&quot; or &quot;notadmin&quot;, and optionally the user_type
  12964. each separated by NULs. For an example of generation in Python:</p>
  12965. <pre><code class="language-python">import hmac, hashlib
  12966. def generate_mac(nonce, user, password, admin=False, user_type=None):
  12967. mac = hmac.new(
  12968. key=shared_secret,
  12969. digestmod=hashlib.sha1,
  12970. )
  12971. mac.update(nonce.encode('utf8'))
  12972. mac.update(b&quot;\x00&quot;)
  12973. mac.update(user.encode('utf8'))
  12974. mac.update(b&quot;\x00&quot;)
  12975. mac.update(password.encode('utf8'))
  12976. mac.update(b&quot;\x00&quot;)
  12977. mac.update(b&quot;admin&quot; if admin else b&quot;notadmin&quot;)
  12978. if user_type:
  12979. mac.update(b&quot;\x00&quot;)
  12980. mac.update(user_type.encode('utf8'))
  12981. return mac.hexdigest()
  12982. </code></pre>
  12983. <div style="break-before: page; page-break-before: always;"></div><h1 id="registration-tokens"><a class="header" href="#registration-tokens">Registration Tokens</a></h1>
  12984. <p>This API allows you to manage tokens which can be used to authenticate
  12985. registration requests, as proposed in
  12986. <a href="https://github.com/matrix-org/matrix-doc/blob/main/proposals/3231-token-authenticated-registration.md">MSC3231</a>.
  12987. To use it, you will need to enable the <code>registration_requires_token</code> config
  12988. option, and authenticate by providing an <code>access_token</code> for a server admin:
  12989. see <a href="usage/administration/admin_api/../../usage/administration/admin_api">Admin API</a>.
  12990. Note that this API is still experimental; not all clients may support it yet.</p>
  12991. <h2 id="registration-token-objects"><a class="header" href="#registration-token-objects">Registration token objects</a></h2>
  12992. <p>Most endpoints make use of JSON objects that contain details about tokens.
  12993. These objects have the following fields:</p>
  12994. <ul>
  12995. <li><code>token</code>: The token which can be used to authenticate registration.</li>
  12996. <li><code>uses_allowed</code>: The number of times the token can be used to complete a
  12997. registration before it becomes invalid.</li>
  12998. <li><code>pending</code>: The number of pending uses the token has. When someone uses
  12999. the token to authenticate themselves, the pending counter is incremented
  13000. so that the token is not used more than the permitted number of times.
  13001. When the person completes registration the pending counter is decremented,
  13002. and the completed counter is incremented.</li>
  13003. <li><code>completed</code>: The number of times the token has been used to successfully
  13004. complete a registration.</li>
  13005. <li><code>expiry_time</code>: The latest time the token is valid. Given as the number of
  13006. milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch).
  13007. To convert this into a human-readable form you can remove the milliseconds
  13008. and use the <code>date</code> command. For example, <code>date -d '@1625394937'</code>.</li>
  13009. </ul>
  13010. <h2 id="list-all-tokens"><a class="header" href="#list-all-tokens">List all tokens</a></h2>
  13011. <p>Lists all tokens and details about them. If the request is successful, the top
  13012. level JSON object will have a <code>registration_tokens</code> key which is an array of
  13013. registration token objects.</p>
  13014. <pre><code>GET /_synapse/admin/v1/registration_tokens
  13015. </code></pre>
  13016. <p>Optional query parameters:</p>
  13017. <ul>
  13018. <li><code>valid</code>: <code>true</code> or <code>false</code>. If <code>true</code>, only valid tokens are returned.
  13019. If <code>false</code>, only tokens that have expired or have had all uses exhausted are
  13020. returned. If omitted, all tokens are returned regardless of validity.</li>
  13021. </ul>
  13022. <p>Example:</p>
  13023. <pre><code>GET /_synapse/admin/v1/registration_tokens
  13024. </code></pre>
  13025. <pre><code>200 OK
  13026. {
  13027. &quot;registration_tokens&quot;: [
  13028. {
  13029. &quot;token&quot;: &quot;abcd&quot;,
  13030. &quot;uses_allowed&quot;: 3,
  13031. &quot;pending&quot;: 0,
  13032. &quot;completed&quot;: 1,
  13033. &quot;expiry_time&quot;: null
  13034. },
  13035. {
  13036. &quot;token&quot;: &quot;pqrs&quot;,
  13037. &quot;uses_allowed&quot;: 2,
  13038. &quot;pending&quot;: 1,
  13039. &quot;completed&quot;: 1,
  13040. &quot;expiry_time&quot;: null
  13041. },
  13042. {
  13043. &quot;token&quot;: &quot;wxyz&quot;,
  13044. &quot;uses_allowed&quot;: null,
  13045. &quot;pending&quot;: 0,
  13046. &quot;completed&quot;: 9,
  13047. &quot;expiry_time&quot;: 1625394937000 // 2021-07-04 10:35:37 UTC
  13048. }
  13049. ]
  13050. }
  13051. </code></pre>
  13052. <p>Example using the <code>valid</code> query parameter:</p>
  13053. <pre><code>GET /_synapse/admin/v1/registration_tokens?valid=false
  13054. </code></pre>
  13055. <pre><code>200 OK
  13056. {
  13057. &quot;registration_tokens&quot;: [
  13058. {
  13059. &quot;token&quot;: &quot;pqrs&quot;,
  13060. &quot;uses_allowed&quot;: 2,
  13061. &quot;pending&quot;: 1,
  13062. &quot;completed&quot;: 1,
  13063. &quot;expiry_time&quot;: null
  13064. },
  13065. {
  13066. &quot;token&quot;: &quot;wxyz&quot;,
  13067. &quot;uses_allowed&quot;: null,
  13068. &quot;pending&quot;: 0,
  13069. &quot;completed&quot;: 9,
  13070. &quot;expiry_time&quot;: 1625394937000 // 2021-07-04 10:35:37 UTC
  13071. }
  13072. ]
  13073. }
  13074. </code></pre>
  13075. <h2 id="get-one-token"><a class="header" href="#get-one-token">Get one token</a></h2>
  13076. <p>Get details about a single token. If the request is successful, the response
  13077. body will be a registration token object.</p>
  13078. <pre><code>GET /_synapse/admin/v1/registration_tokens/&lt;token&gt;
  13079. </code></pre>
  13080. <p>Path parameters:</p>
  13081. <ul>
  13082. <li><code>token</code>: The registration token to return details of.</li>
  13083. </ul>
  13084. <p>Example:</p>
  13085. <pre><code>GET /_synapse/admin/v1/registration_tokens/abcd
  13086. </code></pre>
  13087. <pre><code>200 OK
  13088. {
  13089. &quot;token&quot;: &quot;abcd&quot;,
  13090. &quot;uses_allowed&quot;: 3,
  13091. &quot;pending&quot;: 0,
  13092. &quot;completed&quot;: 1,
  13093. &quot;expiry_time&quot;: null
  13094. }
  13095. </code></pre>
  13096. <h2 id="create-token"><a class="header" href="#create-token">Create token</a></h2>
  13097. <p>Create a new registration token. If the request is successful, the newly created
  13098. token will be returned as a registration token object in the response body.</p>
  13099. <pre><code>POST /_synapse/admin/v1/registration_tokens/new
  13100. </code></pre>
  13101. <p>The request body must be a JSON object and can contain the following fields:</p>
  13102. <ul>
  13103. <li><code>token</code>: The registration token. A string of no more than 64 characters that
  13104. consists only of characters matched by the regex <code>[A-Za-z0-9._~-]</code>.
  13105. Default: randomly generated.</li>
  13106. <li><code>uses_allowed</code>: The integer number of times the token can be used to complete
  13107. a registration before it becomes invalid.
  13108. Default: <code>null</code> (unlimited uses).</li>
  13109. <li><code>expiry_time</code>: The latest time the token is valid. Given as the number of
  13110. milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch).
  13111. You could use, for example, <code>date '+%s000' -d 'tomorrow'</code>.
  13112. Default: <code>null</code> (token does not expire).</li>
  13113. <li><code>length</code>: The length of the token randomly generated if <code>token</code> is not
  13114. specified. Must be between 1 and 64 inclusive. Default: <code>16</code>.</li>
  13115. </ul>
  13116. <p>If a field is omitted the default is used.</p>
  13117. <p>Example using defaults:</p>
  13118. <pre><code>POST /_synapse/admin/v1/registration_tokens/new
  13119. {}
  13120. </code></pre>
  13121. <pre><code>200 OK
  13122. {
  13123. &quot;token&quot;: &quot;0M-9jbkf2t_Tgiw1&quot;,
  13124. &quot;uses_allowed&quot;: null,
  13125. &quot;pending&quot;: 0,
  13126. &quot;completed&quot;: 0,
  13127. &quot;expiry_time&quot;: null
  13128. }
  13129. </code></pre>
  13130. <p>Example specifying some fields:</p>
  13131. <pre><code>POST /_synapse/admin/v1/registration_tokens/new
  13132. {
  13133. &quot;token&quot;: &quot;defg&quot;,
  13134. &quot;uses_allowed&quot;: 1
  13135. }
  13136. </code></pre>
  13137. <pre><code>200 OK
  13138. {
  13139. &quot;token&quot;: &quot;defg&quot;,
  13140. &quot;uses_allowed&quot;: 1,
  13141. &quot;pending&quot;: 0,
  13142. &quot;completed&quot;: 0,
  13143. &quot;expiry_time&quot;: null
  13144. }
  13145. </code></pre>
  13146. <h2 id="update-token"><a class="header" href="#update-token">Update token</a></h2>
  13147. <p>Update the number of allowed uses or expiry time of a token. If the request is
  13148. successful, the updated token will be returned as a registration token object
  13149. in the response body.</p>
  13150. <pre><code>PUT /_synapse/admin/v1/registration_tokens/&lt;token&gt;
  13151. </code></pre>
  13152. <p>Path parameters:</p>
  13153. <ul>
  13154. <li><code>token</code>: The registration token to update.</li>
  13155. </ul>
  13156. <p>The request body must be a JSON object and can contain the following fields:</p>
  13157. <ul>
  13158. <li><code>uses_allowed</code>: The integer number of times the token can be used to complete
  13159. a registration before it becomes invalid. By setting <code>uses_allowed</code> to <code>0</code>
  13160. the token can be easily made invalid without deleting it.
  13161. If <code>null</code> the token will have an unlimited number of uses.</li>
  13162. <li><code>expiry_time</code>: The latest time the token is valid. Given as the number of
  13163. milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch).
  13164. If <code>null</code> the token will not expire.</li>
  13165. </ul>
  13166. <p>If a field is omitted its value is not modified.</p>
  13167. <p>Example:</p>
  13168. <pre><code>PUT /_synapse/admin/v1/registration_tokens/defg
  13169. {
  13170. &quot;expiry_time&quot;: 4781243146000 // 2121-07-06 11:05:46 UTC
  13171. }
  13172. </code></pre>
  13173. <pre><code>200 OK
  13174. {
  13175. &quot;token&quot;: &quot;defg&quot;,
  13176. &quot;uses_allowed&quot;: 1,
  13177. &quot;pending&quot;: 0,
  13178. &quot;completed&quot;: 0,
  13179. &quot;expiry_time&quot;: 4781243146000
  13180. }
  13181. </code></pre>
  13182. <h2 id="delete-token"><a class="header" href="#delete-token">Delete token</a></h2>
  13183. <p>Delete a registration token. If the request is successful, the response body
  13184. will be an empty JSON object.</p>
  13185. <pre><code>DELETE /_synapse/admin/v1/registration_tokens/&lt;token&gt;
  13186. </code></pre>
  13187. <p>Path parameters:</p>
  13188. <ul>
  13189. <li><code>token</code>: The registration token to delete.</li>
  13190. </ul>
  13191. <p>Example:</p>
  13192. <pre><code>DELETE /_synapse/admin/v1/registration_tokens/wxyz
  13193. </code></pre>
  13194. <pre><code>200 OK
  13195. {}
  13196. </code></pre>
  13197. <h2 id="errors"><a class="header" href="#errors">Errors</a></h2>
  13198. <p>If a request fails a &quot;standard error response&quot; will be returned as defined in
  13199. the <a href="https://matrix.org/docs/spec/client_server/r0.6.1#api-standards">Matrix Client-Server API specification</a>.</p>
  13200. <p>For example, if the token specified in a path parameter does not exist a
  13201. <code>404 Not Found</code> error will be returned.</p>
  13202. <pre><code>GET /_synapse/admin/v1/registration_tokens/1234
  13203. </code></pre>
  13204. <pre><code>404 Not Found
  13205. {
  13206. &quot;errcode&quot;: &quot;M_NOT_FOUND&quot;,
  13207. &quot;error&quot;: &quot;No such registration token: 1234&quot;
  13208. }
  13209. </code></pre>
  13210. <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>
  13211. <p>This API allows an administrator to join an user account with a given <code>user_id</code>
  13212. to a room with a given <code>room_id_or_alias</code>. You can only modify the membership of
  13213. local users. The server administrator must be in the room and have permission to
  13214. invite users.</p>
  13215. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  13216. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  13217. <h2 id="parameters"><a class="header" href="#parameters">Parameters</a></h2>
  13218. <p>The following parameters are available:</p>
  13219. <ul>
  13220. <li><code>user_id</code> - Fully qualified user: for example, <code>@user:server.com</code>.</li>
  13221. <li><code>room_id_or_alias</code> - The room identifier or alias to join: for example,
  13222. <code>!636q39766251:server.com</code>.</li>
  13223. </ul>
  13224. <h2 id="usage-1"><a class="header" href="#usage-1">Usage</a></h2>
  13225. <pre><code>POST /_synapse/admin/v1/join/&lt;room_id_or_alias&gt;
  13226. {
  13227. &quot;user_id&quot;: &quot;@user:server.com&quot;
  13228. }
  13229. </code></pre>
  13230. <p>Response:</p>
  13231. <pre><code class="language-json">{
  13232. &quot;room_id&quot;: &quot;!636q39766251:server.com&quot;
  13233. }
  13234. </code></pre>
  13235. <div style="break-before: page; page-break-before: always;"></div><h1 id="list-room-api"><a class="header" href="#list-room-api">List Room API</a></h1>
  13236. <p>The List Room admin API allows server admins to get a list of rooms on their
  13237. server. There are various parameters available that allow for filtering and
  13238. sorting the returned list. This API supports pagination.</p>
  13239. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  13240. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  13241. <p><strong>Parameters</strong></p>
  13242. <p>The following query parameters are available:</p>
  13243. <ul>
  13244. <li>
  13245. <p><code>from</code> - Offset in the returned list. Defaults to <code>0</code>.</p>
  13246. </li>
  13247. <li>
  13248. <p><code>limit</code> - Maximum amount of rooms to return. Defaults to <code>100</code>.</p>
  13249. </li>
  13250. <li>
  13251. <p><code>order_by</code> - The method in which to sort the returned list of rooms. Valid values are:</p>
  13252. <ul>
  13253. <li><code>alphabetical</code> - Same as <code>name</code>. This is deprecated.</li>
  13254. <li><code>size</code> - Same as <code>joined_members</code>. This is deprecated.</li>
  13255. <li><code>name</code> - Rooms are ordered alphabetically by room name. This is the default.</li>
  13256. <li><code>canonical_alias</code> - Rooms are ordered alphabetically by main alias address of the room.</li>
  13257. <li><code>joined_members</code> - Rooms are ordered by the number of members. Largest to smallest.</li>
  13258. <li><code>joined_local_members</code> - Rooms are ordered by the number of local members. Largest to smallest.</li>
  13259. <li><code>version</code> - Rooms are ordered by room version. Largest to smallest.</li>
  13260. <li><code>creator</code> - Rooms are ordered alphabetically by creator of the room.</li>
  13261. <li><code>encryption</code> - Rooms are ordered alphabetically by the end-to-end encryption algorithm.</li>
  13262. <li><code>federatable</code> - Rooms are ordered by whether the room is federatable.</li>
  13263. <li><code>public</code> - Rooms are ordered by visibility in room list.</li>
  13264. <li><code>join_rules</code> - Rooms are ordered alphabetically by join rules of the room.</li>
  13265. <li><code>guest_access</code> - Rooms are ordered alphabetically by guest access option of the room.</li>
  13266. <li><code>history_visibility</code> - Rooms are ordered alphabetically by visibility of history of the room.</li>
  13267. <li><code>state_events</code> - Rooms are ordered by number of state events. Largest to smallest.</li>
  13268. </ul>
  13269. </li>
  13270. <li>
  13271. <p><code>dir</code> - Direction of room order. Either <code>f</code> for forwards or <code>b</code> for backwards. Setting
  13272. this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</p>
  13273. </li>
  13274. <li>
  13275. <p><code>search_term</code> - Filter rooms by their room name, canonical alias and room id.
  13276. Specifically, rooms are selected if the search term is contained in</p>
  13277. <ul>
  13278. <li>the room's name,</li>
  13279. <li>the local part of the room's canonical alias, or</li>
  13280. <li>the complete (local and server part) room's id (case sensitive).</li>
  13281. </ul>
  13282. <p>Defaults to no filtering.</p>
  13283. </li>
  13284. </ul>
  13285. <p><strong>Response</strong></p>
  13286. <p>The following fields are possible in the JSON response body:</p>
  13287. <ul>
  13288. <li><code>rooms</code> - An array of objects, each containing information about a room.
  13289. <ul>
  13290. <li>Room objects contain the following fields:
  13291. <ul>
  13292. <li><code>room_id</code> - The ID of the room.</li>
  13293. <li><code>name</code> - The name of the room.</li>
  13294. <li><code>canonical_alias</code> - The canonical (main) alias address of the room.</li>
  13295. <li><code>joined_members</code> - How many users are currently in the room.</li>
  13296. <li><code>joined_local_members</code> - How many local users are currently in the room.</li>
  13297. <li><code>version</code> - The version of the room as a string.</li>
  13298. <li><code>creator</code> - The <code>user_id</code> of the room creator.</li>
  13299. <li><code>encryption</code> - Algorithm of end-to-end encryption of messages. Is <code>null</code> if encryption is not active.</li>
  13300. <li><code>federatable</code> - Whether users on other servers can join this room.</li>
  13301. <li><code>public</code> - Whether the room is visible in room directory.</li>
  13302. <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>
  13303. <li><code>guest_access</code> - Whether guests can join the room. One of: [&quot;can_join&quot;, &quot;forbidden&quot;].</li>
  13304. <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>
  13305. <li><code>state_events</code> - Total number of state_events of a room. Complexity of the room.</li>
  13306. </ul>
  13307. </li>
  13308. </ul>
  13309. </li>
  13310. <li><code>offset</code> - The current pagination offset in rooms. This parameter should be
  13311. used instead of <code>next_token</code> for room offset as <code>next_token</code> is
  13312. not intended to be parsed.</li>
  13313. <li><code>total_rooms</code> - The total number of rooms this query can return. Using this
  13314. and <code>offset</code>, you have enough information to know the current
  13315. progression through the list.</li>
  13316. <li><code>next_batch</code> - If this field is present, we know that there are potentially
  13317. more rooms on the server that did not all fit into this response.
  13318. We can use <code>next_batch</code> to get the &quot;next page&quot; of results. To do
  13319. so, simply repeat your request, setting the <code>from</code> parameter to
  13320. the value of <code>next_batch</code>.</li>
  13321. <li><code>prev_batch</code> - If this field is present, it is possible to paginate backwards.
  13322. Use <code>prev_batch</code> for the <code>from</code> value in the next request to
  13323. get the &quot;previous page&quot; of results.</li>
  13324. </ul>
  13325. <p>The API is:</p>
  13326. <p>A standard request with no filtering:</p>
  13327. <pre><code>GET /_synapse/admin/v1/rooms
  13328. </code></pre>
  13329. <p>A response body like the following is returned:</p>
  13330. <pre><code class="language-json">{
  13331. &quot;rooms&quot;: [
  13332. {
  13333. &quot;room_id&quot;: &quot;!OGEhHVWSdvArJzumhm:matrix.org&quot;,
  13334. &quot;name&quot;: &quot;Matrix HQ&quot;,
  13335. &quot;canonical_alias&quot;: &quot;#matrix:matrix.org&quot;,
  13336. &quot;joined_members&quot;: 8326,
  13337. &quot;joined_local_members&quot;: 2,
  13338. &quot;version&quot;: &quot;1&quot;,
  13339. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  13340. &quot;encryption&quot;: null,
  13341. &quot;federatable&quot;: true,
  13342. &quot;public&quot;: true,
  13343. &quot;join_rules&quot;: &quot;invite&quot;,
  13344. &quot;guest_access&quot;: null,
  13345. &quot;history_visibility&quot;: &quot;shared&quot;,
  13346. &quot;state_events&quot;: 93534
  13347. },
  13348. ... (8 hidden items) ...
  13349. {
  13350. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  13351. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  13352. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  13353. &quot;joined_members&quot;: 314,
  13354. &quot;joined_local_members&quot;: 20,
  13355. &quot;version&quot;: &quot;4&quot;,
  13356. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  13357. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  13358. &quot;federatable&quot;: true,
  13359. &quot;public&quot;: false,
  13360. &quot;join_rules&quot;: &quot;invite&quot;,
  13361. &quot;guest_access&quot;: null,
  13362. &quot;history_visibility&quot;: &quot;shared&quot;,
  13363. &quot;state_events&quot;: 8345
  13364. }
  13365. ],
  13366. &quot;offset&quot;: 0,
  13367. &quot;total_rooms&quot;: 10
  13368. }
  13369. </code></pre>
  13370. <p>Filtering by room name:</p>
  13371. <pre><code>GET /_synapse/admin/v1/rooms?search_term=TWIM
  13372. </code></pre>
  13373. <p>A response body like the following is returned:</p>
  13374. <pre><code class="language-json">{
  13375. &quot;rooms&quot;: [
  13376. {
  13377. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  13378. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  13379. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  13380. &quot;joined_members&quot;: 314,
  13381. &quot;joined_local_members&quot;: 20,
  13382. &quot;version&quot;: &quot;4&quot;,
  13383. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  13384. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  13385. &quot;federatable&quot;: true,
  13386. &quot;public&quot;: false,
  13387. &quot;join_rules&quot;: &quot;invite&quot;,
  13388. &quot;guest_access&quot;: null,
  13389. &quot;history_visibility&quot;: &quot;shared&quot;,
  13390. &quot;state_events&quot;: 8
  13391. }
  13392. ],
  13393. &quot;offset&quot;: 0,
  13394. &quot;total_rooms&quot;: 1
  13395. }
  13396. </code></pre>
  13397. <p>Paginating through a list of rooms:</p>
  13398. <pre><code>GET /_synapse/admin/v1/rooms?order_by=size
  13399. </code></pre>
  13400. <p>A response body like the following is returned:</p>
  13401. <pre><code class="language-json">{
  13402. &quot;rooms&quot;: [
  13403. {
  13404. &quot;room_id&quot;: &quot;!OGEhHVWSdvArJzumhm:matrix.org&quot;,
  13405. &quot;name&quot;: &quot;Matrix HQ&quot;,
  13406. &quot;canonical_alias&quot;: &quot;#matrix:matrix.org&quot;,
  13407. &quot;joined_members&quot;: 8326,
  13408. &quot;joined_local_members&quot;: 2,
  13409. &quot;version&quot;: &quot;1&quot;,
  13410. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  13411. &quot;encryption&quot;: null,
  13412. &quot;federatable&quot;: true,
  13413. &quot;public&quot;: true,
  13414. &quot;join_rules&quot;: &quot;invite&quot;,
  13415. &quot;guest_access&quot;: null,
  13416. &quot;history_visibility&quot;: &quot;shared&quot;,
  13417. &quot;state_events&quot;: 93534
  13418. },
  13419. ... (98 hidden items) ...
  13420. {
  13421. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  13422. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  13423. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  13424. &quot;joined_members&quot;: 314,
  13425. &quot;joined_local_members&quot;: 20,
  13426. &quot;version&quot;: &quot;4&quot;,
  13427. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  13428. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  13429. &quot;federatable&quot;: true,
  13430. &quot;public&quot;: false,
  13431. &quot;join_rules&quot;: &quot;invite&quot;,
  13432. &quot;guest_access&quot;: null,
  13433. &quot;history_visibility&quot;: &quot;shared&quot;,
  13434. &quot;state_events&quot;: 8345
  13435. }
  13436. ],
  13437. &quot;offset&quot;: 0,
  13438. &quot;total_rooms&quot;: 150,
  13439. &quot;next_token&quot;: 100
  13440. }
  13441. </code></pre>
  13442. <p>The presence of the <code>next_token</code> parameter tells us that there are more rooms
  13443. than returned in this request, and we need to make another request to get them.
  13444. To get the next batch of room results, we repeat our request, setting the <code>from</code>
  13445. parameter to the value of <code>next_token</code>.</p>
  13446. <pre><code>GET /_synapse/admin/v1/rooms?order_by=size&amp;from=100
  13447. </code></pre>
  13448. <p>A response body like the following is returned:</p>
  13449. <pre><code class="language-json">{
  13450. &quot;rooms&quot;: [
  13451. {
  13452. &quot;room_id&quot;: &quot;!mscvqgqpHYjBGDxNym:matrix.org&quot;,
  13453. &quot;name&quot;: &quot;Music Theory&quot;,
  13454. &quot;canonical_alias&quot;: &quot;#musictheory:matrix.org&quot;,
  13455. &quot;joined_members&quot;: 127,
  13456. &quot;joined_local_members&quot;: 2,
  13457. &quot;version&quot;: &quot;1&quot;,
  13458. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  13459. &quot;encryption&quot;: null,
  13460. &quot;federatable&quot;: true,
  13461. &quot;public&quot;: true,
  13462. &quot;join_rules&quot;: &quot;invite&quot;,
  13463. &quot;guest_access&quot;: null,
  13464. &quot;history_visibility&quot;: &quot;shared&quot;,
  13465. &quot;state_events&quot;: 93534
  13466. },
  13467. ... (48 hidden items) ...
  13468. {
  13469. &quot;room_id&quot;: &quot;!twcBhHVdZlQWuuxBhN:termina.org.uk&quot;,
  13470. &quot;name&quot;: &quot;weechat-matrix&quot;,
  13471. &quot;canonical_alias&quot;: &quot;#weechat-matrix:termina.org.uk&quot;,
  13472. &quot;joined_members&quot;: 137,
  13473. &quot;joined_local_members&quot;: 20,
  13474. &quot;version&quot;: &quot;4&quot;,
  13475. &quot;creator&quot;: &quot;@foo:termina.org.uk&quot;,
  13476. &quot;encryption&quot;: null,
  13477. &quot;federatable&quot;: true,
  13478. &quot;public&quot;: true,
  13479. &quot;join_rules&quot;: &quot;invite&quot;,
  13480. &quot;guest_access&quot;: null,
  13481. &quot;history_visibility&quot;: &quot;shared&quot;,
  13482. &quot;state_events&quot;: 8345
  13483. }
  13484. ],
  13485. &quot;offset&quot;: 100,
  13486. &quot;prev_batch&quot;: 0,
  13487. &quot;total_rooms&quot;: 150
  13488. }
  13489. </code></pre>
  13490. <p>Once the <code>next_token</code> parameter is no longer present, we know we've reached the
  13491. end of the list.</p>
  13492. <h1 id="room-details-api"><a class="header" href="#room-details-api">Room Details API</a></h1>
  13493. <p>The Room Details admin API allows server admins to get all details of a room.</p>
  13494. <p>The following fields are possible in the JSON response body:</p>
  13495. <ul>
  13496. <li><code>room_id</code> - The ID of the room.</li>
  13497. <li><code>name</code> - The name of the room.</li>
  13498. <li><code>topic</code> - The topic of the room.</li>
  13499. <li><code>avatar</code> - The <code>mxc</code> URI to the avatar of the room.</li>
  13500. <li><code>canonical_alias</code> - The canonical (main) alias address of the room.</li>
  13501. <li><code>joined_members</code> - How many users are currently in the room.</li>
  13502. <li><code>joined_local_members</code> - How many local users are currently in the room.</li>
  13503. <li><code>joined_local_devices</code> - How many local devices are currently in the room.</li>
  13504. <li><code>version</code> - The version of the room as a string.</li>
  13505. <li><code>creator</code> - The <code>user_id</code> of the room creator.</li>
  13506. <li><code>encryption</code> - Algorithm of end-to-end encryption of messages. Is <code>null</code> if encryption is not active.</li>
  13507. <li><code>federatable</code> - Whether users on other servers can join this room.</li>
  13508. <li><code>public</code> - Whether the room is visible in room directory.</li>
  13509. <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>
  13510. <li><code>guest_access</code> - Whether guests can join the room. One of: [&quot;can_join&quot;, &quot;forbidden&quot;].</li>
  13511. <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>
  13512. <li><code>state_events</code> - Total number of state_events of a room. Complexity of the room.</li>
  13513. </ul>
  13514. <p>The API is:</p>
  13515. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;
  13516. </code></pre>
  13517. <p>A response body like the following is returned:</p>
  13518. <pre><code class="language-json">{
  13519. &quot;room_id&quot;: &quot;!mscvqgqpHYjBGDxNym:matrix.org&quot;,
  13520. &quot;name&quot;: &quot;Music Theory&quot;,
  13521. &quot;avatar&quot;: &quot;mxc://matrix.org/AQDaVFlbkQoErdOgqWRgiGSV&quot;,
  13522. &quot;topic&quot;: &quot;Theory, Composition, Notation, Analysis&quot;,
  13523. &quot;canonical_alias&quot;: &quot;#musictheory:matrix.org&quot;,
  13524. &quot;joined_members&quot;: 127,
  13525. &quot;joined_local_members&quot;: 2,
  13526. &quot;joined_local_devices&quot;: 2,
  13527. &quot;version&quot;: &quot;1&quot;,
  13528. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  13529. &quot;encryption&quot;: null,
  13530. &quot;federatable&quot;: true,
  13531. &quot;public&quot;: true,
  13532. &quot;join_rules&quot;: &quot;invite&quot;,
  13533. &quot;guest_access&quot;: null,
  13534. &quot;history_visibility&quot;: &quot;shared&quot;,
  13535. &quot;state_events&quot;: 93534
  13536. }
  13537. </code></pre>
  13538. <h1 id="room-members-api"><a class="header" href="#room-members-api">Room Members API</a></h1>
  13539. <p>The Room Members admin API allows server admins to get a list of all members of a room.</p>
  13540. <p>The response includes the following fields:</p>
  13541. <ul>
  13542. <li><code>members</code> - A list of all the members that are present in the room, represented by their ids.</li>
  13543. <li><code>total</code> - Total number of members in the room.</li>
  13544. </ul>
  13545. <p>The API is:</p>
  13546. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/members
  13547. </code></pre>
  13548. <p>A response body like the following is returned:</p>
  13549. <pre><code class="language-json">{
  13550. &quot;members&quot;: [
  13551. &quot;@foo:matrix.org&quot;,
  13552. &quot;@bar:matrix.org&quot;,
  13553. &quot;@foobar:matrix.org&quot;
  13554. ],
  13555. &quot;total&quot;: 3
  13556. }
  13557. </code></pre>
  13558. <h1 id="room-state-api"><a class="header" href="#room-state-api">Room State API</a></h1>
  13559. <p>The Room State admin API allows server admins to get a list of all state events in a room.</p>
  13560. <p>The response includes the following fields:</p>
  13561. <ul>
  13562. <li><code>state</code> - The current state of the room at the time of request.</li>
  13563. </ul>
  13564. <p>The API is:</p>
  13565. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/state
  13566. </code></pre>
  13567. <p>A response body like the following is returned:</p>
  13568. <pre><code class="language-json">{
  13569. &quot;state&quot;: [
  13570. {&quot;type&quot;: &quot;m.room.create&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true},
  13571. {&quot;type&quot;: &quot;m.room.power_levels&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true},
  13572. {&quot;type&quot;: &quot;m.room.name&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true}
  13573. ]
  13574. }
  13575. </code></pre>
  13576. <h1 id="block-room-api"><a class="header" href="#block-room-api">Block Room API</a></h1>
  13577. <p>The Block Room admin API allows server admins to block and unblock rooms,
  13578. and query to see if a given room is blocked.
  13579. This API can be used to pre-emptively block a room, even if it's unknown to this
  13580. homeserver. Users will be prevented from joining a blocked room.</p>
  13581. <h2 id="block-or-unblock-a-room"><a class="header" href="#block-or-unblock-a-room">Block or unblock a room</a></h2>
  13582. <p>The API is:</p>
  13583. <pre><code>PUT /_synapse/admin/v1/rooms/&lt;room_id&gt;/block
  13584. </code></pre>
  13585. <p>with a body of:</p>
  13586. <pre><code class="language-json">{
  13587. &quot;block&quot;: true
  13588. }
  13589. </code></pre>
  13590. <p>A response body like the following is returned:</p>
  13591. <pre><code class="language-json">{
  13592. &quot;block&quot;: true
  13593. }
  13594. </code></pre>
  13595. <p><strong>Parameters</strong></p>
  13596. <p>The following parameters should be set in the URL:</p>
  13597. <ul>
  13598. <li><code>room_id</code> - The ID of the room.</li>
  13599. </ul>
  13600. <p>The following JSON body parameters are available:</p>
  13601. <ul>
  13602. <li><code>block</code> - If <code>true</code> the room will be blocked and if <code>false</code> the room will be unblocked.</li>
  13603. </ul>
  13604. <p><strong>Response</strong></p>
  13605. <p>The following fields are possible in the JSON response body:</p>
  13606. <ul>
  13607. <li><code>block</code> - A boolean. <code>true</code> if the room is blocked, otherwise <code>false</code></li>
  13608. </ul>
  13609. <h2 id="get-block-status"><a class="header" href="#get-block-status">Get block status</a></h2>
  13610. <p>The API is:</p>
  13611. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/block
  13612. </code></pre>
  13613. <p>A response body like the following is returned:</p>
  13614. <pre><code class="language-json">{
  13615. &quot;block&quot;: true,
  13616. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  13617. }
  13618. </code></pre>
  13619. <p><strong>Parameters</strong></p>
  13620. <p>The following parameters should be set in the URL:</p>
  13621. <ul>
  13622. <li><code>room_id</code> - The ID of the room.</li>
  13623. </ul>
  13624. <p><strong>Response</strong></p>
  13625. <p>The following fields are possible in the JSON response body:</p>
  13626. <ul>
  13627. <li><code>block</code> - A boolean. <code>true</code> if the room is blocked, otherwise <code>false</code></li>
  13628. <li><code>user_id</code> - An optional string. If the room is blocked (<code>block</code> is <code>true</code>) shows
  13629. the user who has add the room to blocking list. Otherwise it is not displayed.</li>
  13630. </ul>
  13631. <h1 id="delete-room-api"><a class="header" href="#delete-room-api">Delete Room API</a></h1>
  13632. <p>The Delete Room admin API allows server admins to remove rooms from the server
  13633. and block these rooms.</p>
  13634. <p>Shuts down a room. Moves all local users and room aliases automatically to a
  13635. new room if <code>new_room_user_id</code> is set. Otherwise local users only
  13636. leave the room without any information.</p>
  13637. <p>The new room will be created with the user specified by the <code>new_room_user_id</code> parameter
  13638. as room administrator and will contain a message explaining what happened. Users invited
  13639. to the new room will have power level <code>-10</code> by default, and thus be unable to speak.</p>
  13640. <p>If <code>block</code> is <code>true</code>, users will be prevented from joining the old room.
  13641. This option can in <a href="admin_api/rooms.html#version-1-old-version">Version 1</a> also be used to pre-emptively
  13642. block a room, even if it's unknown to this homeserver. In this case, the room will be
  13643. blocked, and no further action will be taken. If <code>block</code> is <code>false</code>, attempting to
  13644. delete an unknown room is invalid and will be rejected as a bad request.</p>
  13645. <p>This API will remove all trace of the old room from your database after removing
  13646. all local users. If <code>purge</code> is <code>true</code> (the default), all traces of the old room will
  13647. be removed from your database after removing all local users. If you do not want
  13648. this to happen, set <code>purge</code> to <code>false</code>.
  13649. Depending on the amount of history being purged, a call to the API may take
  13650. several minutes or longer.</p>
  13651. <p>The local server will only have the power to move local user and room aliases to
  13652. the new room. Users on other servers will be unaffected.</p>
  13653. <h2 id="version-1-old-version"><a class="header" href="#version-1-old-version">Version 1 (old version)</a></h2>
  13654. <p>This version works synchronously. That means you only get the response once the server has
  13655. finished the action, which may take a long time. If you request the same action
  13656. a second time, and the server has not finished the first one, the second request will block.
  13657. This is fixed in version 2 of this API. The parameters are the same in both APIs.
  13658. This API will become deprecated in the future.</p>
  13659. <p>The API is:</p>
  13660. <pre><code>DELETE /_synapse/admin/v1/rooms/&lt;room_id&gt;
  13661. </code></pre>
  13662. <p>with a body of:</p>
  13663. <pre><code class="language-json">{
  13664. &quot;new_room_user_id&quot;: &quot;@someuser:example.com&quot;,
  13665. &quot;room_name&quot;: &quot;Content Violation Notification&quot;,
  13666. &quot;message&quot;: &quot;Bad Room has been shutdown due to content violations on this server. Please review our Terms of Service.&quot;,
  13667. &quot;block&quot;: true,
  13668. &quot;purge&quot;: true
  13669. }
  13670. </code></pre>
  13671. <p>A response body like the following is returned:</p>
  13672. <pre><code class="language-json">{
  13673. &quot;kicked_users&quot;: [
  13674. &quot;@foobar:example.com&quot;
  13675. ],
  13676. &quot;failed_to_kick_users&quot;: [],
  13677. &quot;local_aliases&quot;: [
  13678. &quot;#badroom:example.com&quot;,
  13679. &quot;#evilsaloon:example.com&quot;
  13680. ],
  13681. &quot;new_room_id&quot;: &quot;!newroomid:example.com&quot;
  13682. }
  13683. </code></pre>
  13684. <p>The parameters and response values have the same format as
  13685. <a href="admin_api/rooms.html#version-2-new-version">version 2</a> of the API.</p>
  13686. <h2 id="version-2-new-version"><a class="header" href="#version-2-new-version">Version 2 (new version)</a></h2>
  13687. <p><strong>Note</strong>: This API is new, experimental and &quot;subject to change&quot;.</p>
  13688. <p>This version works asynchronously, meaning you get the response from server immediately
  13689. while the server works on that task in background. You can then request the status of the action
  13690. to check if it has completed.</p>
  13691. <p>The API is:</p>
  13692. <pre><code>DELETE /_synapse/admin/v2/rooms/&lt;room_id&gt;
  13693. </code></pre>
  13694. <p>with a body of:</p>
  13695. <pre><code class="language-json">{
  13696. &quot;new_room_user_id&quot;: &quot;@someuser:example.com&quot;,
  13697. &quot;room_name&quot;: &quot;Content Violation Notification&quot;,
  13698. &quot;message&quot;: &quot;Bad Room has been shutdown due to content violations on this server. Please review our Terms of Service.&quot;,
  13699. &quot;block&quot;: true,
  13700. &quot;purge&quot;: true
  13701. }
  13702. </code></pre>
  13703. <p>The API starts the shut down and purge running, and returns immediately with a JSON body with
  13704. a purge id:</p>
  13705. <pre><code class="language-json">{
  13706. &quot;delete_id&quot;: &quot;&lt;opaque id&gt;&quot;
  13707. }
  13708. </code></pre>
  13709. <p><strong>Parameters</strong></p>
  13710. <p>The following parameters should be set in the URL:</p>
  13711. <ul>
  13712. <li><code>room_id</code> - The ID of the room.</li>
  13713. </ul>
  13714. <p>The following JSON body parameters are available:</p>
  13715. <ul>
  13716. <li><code>new_room_user_id</code> - Optional. If set, a new room will be created with this user ID
  13717. as the creator and admin, and all users in the old room will be moved into that
  13718. room. If not set, no new room will be created and the users will just be removed
  13719. from the old room. The user ID must be on the local server, but does not necessarily
  13720. have to belong to a registered user.</li>
  13721. <li><code>room_name</code> - Optional. A string representing the name of the room that new users will be
  13722. invited to. Defaults to <code>Content Violation Notification</code></li>
  13723. <li><code>message</code> - Optional. A string containing the first message that will be sent as
  13724. <code>new_room_user_id</code> in the new room. Ideally this will clearly convey why the
  13725. 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>
  13726. <li><code>block</code> - Optional. If set to <code>true</code>, this room will be added to a blocking list,
  13727. preventing future attempts to join the room. Rooms can be blocked
  13728. even if they're not yet known to the homeserver (only with
  13729. <a href="admin_api/rooms.html#version-1-old-version">Version 1</a> of the API). Defaults to <code>false</code>.</li>
  13730. <li><code>purge</code> - Optional. If set to <code>true</code>, it will remove all traces of the room from your database.
  13731. Defaults to <code>true</code>.</li>
  13732. <li><code>force_purge</code> - Optional, and ignored unless <code>purge</code> is <code>true</code>. If set to <code>true</code>, it
  13733. will force a purge to go ahead even if there are local users still in the room. Do not
  13734. use this unless a regular <code>purge</code> operation fails, as it could leave those users'
  13735. clients in a confused state.</li>
  13736. </ul>
  13737. <p>The JSON body must not be empty. The body must be at least <code>{}</code>.</p>
  13738. <h2 id="status-of-deleting-rooms"><a class="header" href="#status-of-deleting-rooms">Status of deleting rooms</a></h2>
  13739. <p><strong>Note</strong>: This API is new, experimental and &quot;subject to change&quot;.</p>
  13740. <p>It is possible to query the status of the background task for deleting rooms.
  13741. The status can be queried up to 24 hours after completion of the task,
  13742. or until Synapse is restarted (whichever happens first).</p>
  13743. <h3 id="query-by-room_id"><a class="header" href="#query-by-room_id">Query by <code>room_id</code></a></h3>
  13744. <p>With this API you can get the status of all active deletion tasks, and all those completed in the last 24h,
  13745. for the given <code>room_id</code>.</p>
  13746. <p>The API is:</p>
  13747. <pre><code>GET /_synapse/admin/v2/rooms/&lt;room_id&gt;/delete_status
  13748. </code></pre>
  13749. <p>A response body like the following is returned:</p>
  13750. <pre><code class="language-json">{
  13751. &quot;results&quot;: [
  13752. {
  13753. &quot;delete_id&quot;: &quot;delete_id1&quot;,
  13754. &quot;status&quot;: &quot;failed&quot;,
  13755. &quot;error&quot;: &quot;error message&quot;,
  13756. &quot;shutdown_room&quot;: {
  13757. &quot;kicked_users&quot;: [],
  13758. &quot;failed_to_kick_users&quot;: [],
  13759. &quot;local_aliases&quot;: [],
  13760. &quot;new_room_id&quot;: null
  13761. }
  13762. }, {
  13763. &quot;delete_id&quot;: &quot;delete_id2&quot;,
  13764. &quot;status&quot;: &quot;purging&quot;,
  13765. &quot;shutdown_room&quot;: {
  13766. &quot;kicked_users&quot;: [
  13767. &quot;@foobar:example.com&quot;
  13768. ],
  13769. &quot;failed_to_kick_users&quot;: [],
  13770. &quot;local_aliases&quot;: [
  13771. &quot;#badroom:example.com&quot;,
  13772. &quot;#evilsaloon:example.com&quot;
  13773. ],
  13774. &quot;new_room_id&quot;: &quot;!newroomid:example.com&quot;
  13775. }
  13776. }
  13777. ]
  13778. }
  13779. </code></pre>
  13780. <p><strong>Parameters</strong></p>
  13781. <p>The following parameters should be set in the URL:</p>
  13782. <ul>
  13783. <li><code>room_id</code> - The ID of the room.</li>
  13784. </ul>
  13785. <h3 id="query-by-delete_id"><a class="header" href="#query-by-delete_id">Query by <code>delete_id</code></a></h3>
  13786. <p>With this API you can get the status of one specific task by <code>delete_id</code>.</p>
  13787. <p>The API is:</p>
  13788. <pre><code>GET /_synapse/admin/v2/rooms/delete_status/&lt;delete_id&gt;
  13789. </code></pre>
  13790. <p>A response body like the following is returned:</p>
  13791. <pre><code class="language-json">{
  13792. &quot;status&quot;: &quot;purging&quot;,
  13793. &quot;shutdown_room&quot;: {
  13794. &quot;kicked_users&quot;: [
  13795. &quot;@foobar:example.com&quot;
  13796. ],
  13797. &quot;failed_to_kick_users&quot;: [],
  13798. &quot;local_aliases&quot;: [
  13799. &quot;#badroom:example.com&quot;,
  13800. &quot;#evilsaloon:example.com&quot;
  13801. ],
  13802. &quot;new_room_id&quot;: &quot;!newroomid:example.com&quot;
  13803. }
  13804. }
  13805. </code></pre>
  13806. <p><strong>Parameters</strong></p>
  13807. <p>The following parameters should be set in the URL:</p>
  13808. <ul>
  13809. <li><code>delete_id</code> - The ID for this delete.</li>
  13810. </ul>
  13811. <h3 id="response"><a class="header" href="#response">Response</a></h3>
  13812. <p>The following fields are returned in the JSON response body:</p>
  13813. <ul>
  13814. <li><code>results</code> - An array of objects, each containing information about one task.
  13815. This field is omitted from the result when you query by <code>delete_id</code>.
  13816. Task objects contain the following fields:
  13817. <ul>
  13818. <li><code>delete_id</code> - The ID for this purge if you query by <code>room_id</code>.</li>
  13819. <li><code>status</code> - The status will be one of:
  13820. <ul>
  13821. <li><code>shutting_down</code> - The process is removing users from the room.</li>
  13822. <li><code>purging</code> - The process is purging the room and event data from database.</li>
  13823. <li><code>complete</code> - The process has completed successfully.</li>
  13824. <li><code>failed</code> - The process is aborted, an error has occurred.</li>
  13825. </ul>
  13826. </li>
  13827. <li><code>error</code> - A string that shows an error message if <code>status</code> is <code>failed</code>.
  13828. Otherwise this field is hidden.</li>
  13829. <li><code>shutdown_room</code> - An object containing information about the result of shutting down the room.
  13830. <em>Note:</em> The result is shown after removing the room members.
  13831. The delete process can still be running. Please pay attention to the <code>status</code>.
  13832. <ul>
  13833. <li><code>kicked_users</code> - An array of users (<code>user_id</code>) that were kicked.</li>
  13834. <li><code>failed_to_kick_users</code> - An array of users (<code>user_id</code>) that that were not kicked.</li>
  13835. <li><code>local_aliases</code> - An array of strings representing the local aliases that were
  13836. migrated from the old room to the new.</li>
  13837. <li><code>new_room_id</code> - A string representing the room ID of the new room, or <code>null</code> if
  13838. no such room was created.</li>
  13839. </ul>
  13840. </li>
  13841. </ul>
  13842. </li>
  13843. </ul>
  13844. <h2 id="undoing-room-deletions"><a class="header" href="#undoing-room-deletions">Undoing room deletions</a></h2>
  13845. <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,
  13846. the structure can and does change without notice.</p>
  13847. <p>First, it's important to understand that a room deletion is very destructive. Undoing a deletion is not as simple as pretending it
  13848. 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
  13849. to recover at all:</p>
  13850. <ul>
  13851. <li>If the room was invite-only, your users will need to be re-invited.</li>
  13852. <li>If the room no longer has any members at all, it'll be impossible to rejoin.</li>
  13853. <li>The first user to rejoin will have to do so via an alias on a different
  13854. server (or receive an invite from a user on a different server).</li>
  13855. </ul>
  13856. <p>With all that being said, if you still want to try and recover the room:</p>
  13857. <ol>
  13858. <li>
  13859. <p>If the room was <code>block</code>ed, you must unblock it on your server. This can be
  13860. accomplished as follows:</p>
  13861. <ol>
  13862. <li>For safety reasons, shut down Synapse.</li>
  13863. <li>In the database, run <code>DELETE FROM blocked_rooms WHERE room_id = '!example:example.org';</code>
  13864. <ul>
  13865. <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>
  13866. <li>The room ID is the same one supplied to the delete room API, not the Content Violation room.</li>
  13867. </ul>
  13868. </li>
  13869. <li>Restart Synapse.</li>
  13870. </ol>
  13871. <p>This step is unnecessary if <code>block</code> was not set.</p>
  13872. </li>
  13873. <li>
  13874. <p>Any room aliases on your server that pointed to the deleted room may have
  13875. been deleted, or redirected to the Content Violation room. These will need
  13876. to be restored manually.</p>
  13877. </li>
  13878. <li>
  13879. <p>Users on your server that were in the deleted room will have been kicked
  13880. from the room. Consider whether you want to update their membership
  13881. (possibly via the <a href="admin_api/room_membership.html">Edit Room Membership API</a>) or let
  13882. them handle rejoining themselves.</p>
  13883. </li>
  13884. <li>
  13885. <p>If <code>new_room_user_id</code> was given, a 'Content Violation' will have been
  13886. created. Consider whether you want to delete that roomm.</p>
  13887. </li>
  13888. </ol>
  13889. <h1 id="make-room-admin-api"><a class="header" href="#make-room-admin-api">Make Room Admin API</a></h1>
  13890. <p>Grants another user the highest power available to a local user who is in the room.
  13891. If the user is not in the room, and it is not publicly joinable, then invite the user.</p>
  13892. <p>By default the server admin (the caller) is granted power, but another user can
  13893. optionally be specified, e.g.:</p>
  13894. <pre><code>POST /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/make_room_admin
  13895. {
  13896. &quot;user_id&quot;: &quot;@foo:example.com&quot;
  13897. }
  13898. </code></pre>
  13899. <h1 id="forward-extremities-admin-api"><a class="header" href="#forward-extremities-admin-api">Forward Extremities Admin API</a></h1>
  13900. <p>Enables querying and deleting forward extremities from rooms. When a lot of forward
  13901. extremities accumulate in a room, performance can become degraded. For details, see
  13902. <a href="https://github.com/matrix-org/synapse/issues/1760">#1760</a>.</p>
  13903. <h2 id="check-for-forward-extremities"><a class="header" href="#check-for-forward-extremities">Check for forward extremities</a></h2>
  13904. <p>To check the status of forward extremities for a room:</p>
  13905. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/forward_extremities
  13906. </code></pre>
  13907. <p>A response as follows will be returned:</p>
  13908. <pre><code class="language-json">{
  13909. &quot;count&quot;: 1,
  13910. &quot;results&quot;: [
  13911. {
  13912. &quot;event_id&quot;: &quot;$M5SP266vsnxctfwFgFLNceaCo3ujhRtg_NiiHabcdefgh&quot;,
  13913. &quot;state_group&quot;: 439,
  13914. &quot;depth&quot;: 123,
  13915. &quot;received_ts&quot;: 1611263016761
  13916. }
  13917. ]
  13918. }
  13919. </code></pre>
  13920. <h2 id="deleting-forward-extremities"><a class="header" href="#deleting-forward-extremities">Deleting forward extremities</a></h2>
  13921. <p><strong>WARNING</strong>: Please ensure you know what you're doing and have read
  13922. the related issue <a href="https://github.com/matrix-org/synapse/issues/1760">#1760</a>.
  13923. Under no situations should this API be executed as an automated maintenance task!</p>
  13924. <p>If a room has lots of forward extremities, the extra can be
  13925. deleted as follows:</p>
  13926. <pre><code>DELETE /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/forward_extremities
  13927. </code></pre>
  13928. <p>A response as follows will be returned, indicating the amount of forward extremities
  13929. that were deleted.</p>
  13930. <pre><code class="language-json">{
  13931. &quot;deleted&quot;: 1
  13932. }
  13933. </code></pre>
  13934. <h1 id="event-context-api"><a class="header" href="#event-context-api">Event Context API</a></h1>
  13935. <p>This API lets a client find the context of an event. This is designed primarily to investigate abuse reports.</p>
  13936. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/context/&lt;event_id&gt;
  13937. </code></pre>
  13938. <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>
  13939. <p>Example response:</p>
  13940. <pre><code class="language-json">{
  13941. &quot;end&quot;: &quot;t29-57_2_0_2&quot;,
  13942. &quot;events_after&quot;: [
  13943. {
  13944. &quot;content&quot;: {
  13945. &quot;body&quot;: &quot;This is an example text message&quot;,
  13946. &quot;msgtype&quot;: &quot;m.text&quot;,
  13947. &quot;format&quot;: &quot;org.matrix.custom.html&quot;,
  13948. &quot;formatted_body&quot;: &quot;&lt;b&gt;This is an example text message&lt;/b&gt;&quot;
  13949. },
  13950. &quot;type&quot;: &quot;m.room.message&quot;,
  13951. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  13952. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  13953. &quot;sender&quot;: &quot;@example:example.org&quot;,
  13954. &quot;origin_server_ts&quot;: 1432735824653,
  13955. &quot;unsigned&quot;: {
  13956. &quot;age&quot;: 1234
  13957. }
  13958. }
  13959. ],
  13960. &quot;event&quot;: {
  13961. &quot;content&quot;: {
  13962. &quot;body&quot;: &quot;filename.jpg&quot;,
  13963. &quot;info&quot;: {
  13964. &quot;h&quot;: 398,
  13965. &quot;w&quot;: 394,
  13966. &quot;mimetype&quot;: &quot;image/jpeg&quot;,
  13967. &quot;size&quot;: 31037
  13968. },
  13969. &quot;url&quot;: &quot;mxc://example.org/JWEIFJgwEIhweiWJE&quot;,
  13970. &quot;msgtype&quot;: &quot;m.image&quot;
  13971. },
  13972. &quot;type&quot;: &quot;m.room.message&quot;,
  13973. &quot;event_id&quot;: &quot;$f3h4d129462ha:example.com&quot;,
  13974. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  13975. &quot;sender&quot;: &quot;@example:example.org&quot;,
  13976. &quot;origin_server_ts&quot;: 1432735824653,
  13977. &quot;unsigned&quot;: {
  13978. &quot;age&quot;: 1234
  13979. }
  13980. },
  13981. &quot;events_before&quot;: [
  13982. {
  13983. &quot;content&quot;: {
  13984. &quot;body&quot;: &quot;something-important.doc&quot;,
  13985. &quot;filename&quot;: &quot;something-important.doc&quot;,
  13986. &quot;info&quot;: {
  13987. &quot;mimetype&quot;: &quot;application/msword&quot;,
  13988. &quot;size&quot;: 46144
  13989. },
  13990. &quot;msgtype&quot;: &quot;m.file&quot;,
  13991. &quot;url&quot;: &quot;mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe&quot;
  13992. },
  13993. &quot;type&quot;: &quot;m.room.message&quot;,
  13994. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  13995. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  13996. &quot;sender&quot;: &quot;@example:example.org&quot;,
  13997. &quot;origin_server_ts&quot;: 1432735824653,
  13998. &quot;unsigned&quot;: {
  13999. &quot;age&quot;: 1234
  14000. }
  14001. }
  14002. ],
  14003. &quot;start&quot;: &quot;t27-54_2_0_2&quot;,
  14004. &quot;state&quot;: [
  14005. {
  14006. &quot;content&quot;: {
  14007. &quot;creator&quot;: &quot;@example:example.org&quot;,
  14008. &quot;room_version&quot;: &quot;1&quot;,
  14009. &quot;m.federate&quot;: true,
  14010. &quot;predecessor&quot;: {
  14011. &quot;event_id&quot;: &quot;$something:example.org&quot;,
  14012. &quot;room_id&quot;: &quot;!oldroom:example.org&quot;
  14013. }
  14014. },
  14015. &quot;type&quot;: &quot;m.room.create&quot;,
  14016. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  14017. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  14018. &quot;sender&quot;: &quot;@example:example.org&quot;,
  14019. &quot;origin_server_ts&quot;: 1432735824653,
  14020. &quot;unsigned&quot;: {
  14021. &quot;age&quot;: 1234
  14022. },
  14023. &quot;state_key&quot;: &quot;&quot;
  14024. },
  14025. {
  14026. &quot;content&quot;: {
  14027. &quot;membership&quot;: &quot;join&quot;,
  14028. &quot;avatar_url&quot;: &quot;mxc://example.org/SEsfnsuifSDFSSEF&quot;,
  14029. &quot;displayname&quot;: &quot;Alice Margatroid&quot;
  14030. },
  14031. &quot;type&quot;: &quot;m.room.member&quot;,
  14032. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  14033. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  14034. &quot;sender&quot;: &quot;@example:example.org&quot;,
  14035. &quot;origin_server_ts&quot;: 1432735824653,
  14036. &quot;unsigned&quot;: {
  14037. &quot;age&quot;: 1234
  14038. },
  14039. &quot;state_key&quot;: &quot;@alice:example.org&quot;
  14040. }
  14041. ]
  14042. }
  14043. </code></pre>
  14044. <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>
  14045. <p>The API to send notices is as follows:</p>
  14046. <pre><code>POST /_synapse/admin/v1/send_server_notice
  14047. </code></pre>
  14048. <p>or:</p>
  14049. <pre><code>PUT /_synapse/admin/v1/send_server_notice/{txnId}
  14050. </code></pre>
  14051. <p>You will need to authenticate with an access token for an admin user.</p>
  14052. <p>When using the <code>PUT</code> form, retransmissions with the same transaction ID will be
  14053. ignored in the same way as with <code>PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}</code>.</p>
  14054. <p>The request body should look something like the following:</p>
  14055. <pre><code class="language-json">{
  14056. &quot;user_id&quot;: &quot;@target_user:server_name&quot;,
  14057. &quot;content&quot;: {
  14058. &quot;msgtype&quot;: &quot;m.text&quot;,
  14059. &quot;body&quot;: &quot;This is my message&quot;
  14060. }
  14061. }
  14062. </code></pre>
  14063. <p>You can optionally include the following additional parameters:</p>
  14064. <ul>
  14065. <li><code>type</code>: the type of event. Defaults to <code>m.room.message</code>.</li>
  14066. <li><code>state_key</code>: Setting this will result in a state event being sent.</li>
  14067. </ul>
  14068. <p>Once the notice has been sent, the API will return the following response:</p>
  14069. <pre><code class="language-json">{
  14070. &quot;event_id&quot;: &quot;&lt;event_id&gt;&quot;
  14071. }
  14072. </code></pre>
  14073. <p>Note that server notices must be enabled in <code>homeserver.yaml</code> before this API
  14074. can be used. See <a href="admin_api/../server_notices.html">the server notices documentation</a> for more information.</p>
  14075. <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>
  14076. <p>Returns information about all local media usage of users. Gives the
  14077. possibility to filter them by time and user.</p>
  14078. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  14079. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  14080. <p>The API is:</p>
  14081. <pre><code>GET /_synapse/admin/v1/statistics/users/media
  14082. </code></pre>
  14083. <p>A response body like the following is returned:</p>
  14084. <pre><code class="language-json">{
  14085. &quot;users&quot;: [
  14086. {
  14087. &quot;displayname&quot;: &quot;foo_user_0&quot;,
  14088. &quot;media_count&quot;: 2,
  14089. &quot;media_length&quot;: 134,
  14090. &quot;user_id&quot;: &quot;@foo_user_0:test&quot;
  14091. },
  14092. {
  14093. &quot;displayname&quot;: &quot;foo_user_1&quot;,
  14094. &quot;media_count&quot;: 2,
  14095. &quot;media_length&quot;: 134,
  14096. &quot;user_id&quot;: &quot;@foo_user_1:test&quot;
  14097. }
  14098. ],
  14099. &quot;next_token&quot;: 3,
  14100. &quot;total&quot;: 10
  14101. }
  14102. </code></pre>
  14103. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint
  14104. again with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  14105. <p>If the endpoint does not return a <code>next_token</code> then there are no more
  14106. reports to paginate through.</p>
  14107. <p><strong>Parameters</strong></p>
  14108. <p>The following parameters should be set in the URL:</p>
  14109. <ul>
  14110. <li><code>limit</code>: string representing a positive integer - Is optional but is
  14111. used for pagination, denoting the maximum number of items to return
  14112. in this call. Defaults to <code>100</code>.</li>
  14113. <li><code>from</code>: string representing a positive integer - Is optional but used for pagination,
  14114. denoting the offset in the returned results. This should be treated as an opaque value
  14115. and not explicitly set to anything other than the return value of <code>next_token</code> from a
  14116. previous call. Defaults to <code>0</code>.</li>
  14117. <li><code>order_by</code> - string - The method in which to sort the returned list of users. Valid values are:
  14118. <ul>
  14119. <li><code>user_id</code> - Users are ordered alphabetically by <code>user_id</code>. This is the default.</li>
  14120. <li><code>displayname</code> - Users are ordered alphabetically by <code>displayname</code>.</li>
  14121. <li><code>media_length</code> - Users are ordered by the total size of uploaded media in bytes.
  14122. Smallest to largest.</li>
  14123. <li><code>media_count</code> - Users are ordered by number of uploaded media. Smallest to largest.</li>
  14124. </ul>
  14125. </li>
  14126. <li><code>from_ts</code> - string representing a positive integer - Considers only
  14127. files created at this timestamp or later. Unix timestamp in ms.</li>
  14128. <li><code>until_ts</code> - string representing a positive integer - Considers only
  14129. files created at this timestamp or earlier. Unix timestamp in ms.</li>
  14130. <li><code>search_term</code> - string - Filter users by their user ID localpart <strong>or</strong> displayname.
  14131. The search term can be found in any part of the string.
  14132. Defaults to no filtering.</li>
  14133. <li><code>dir</code> - string - Direction of order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  14134. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</li>
  14135. </ul>
  14136. <p><strong>Response</strong></p>
  14137. <p>The following fields are returned in the JSON response body:</p>
  14138. <ul>
  14139. <li><code>users</code> - An array of objects, each containing information
  14140. about the user and their local media. Objects contain the following fields:
  14141. <ul>
  14142. <li><code>displayname</code> - string - Displayname of this user.</li>
  14143. <li><code>media_count</code> - integer - Number of uploaded media by this user.</li>
  14144. <li><code>media_length</code> - integer - Size of uploaded media in bytes by this user.</li>
  14145. <li><code>user_id</code> - string - Fully-qualified user ID (ex. <code>@user:server.com</code>).</li>
  14146. </ul>
  14147. </li>
  14148. <li><code>next_token</code> - integer - Opaque value used for pagination. See above.</li>
  14149. <li><code>total</code> - integer - Total number of users after filtering.</li>
  14150. </ul>
  14151. <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>
  14152. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  14153. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  14154. <h2 id="query-user-account"><a class="header" href="#query-user-account">Query User Account</a></h2>
  14155. <p>This API returns information about a specific user account.</p>
  14156. <p>The api is:</p>
  14157. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;
  14158. </code></pre>
  14159. <p>It returns a JSON body like the following:</p>
  14160. <pre><code class="language-jsonc">{
  14161. &quot;name&quot;: &quot;@user:example.com&quot;,
  14162. &quot;displayname&quot;: &quot;User&quot;, // can be null if not set
  14163. &quot;threepids&quot;: [
  14164. {
  14165. &quot;medium&quot;: &quot;email&quot;,
  14166. &quot;address&quot;: &quot;&lt;user_mail_1&gt;&quot;,
  14167. &quot;added_at&quot;: 1586458409743,
  14168. &quot;validated_at&quot;: 1586458409743
  14169. },
  14170. {
  14171. &quot;medium&quot;: &quot;email&quot;,
  14172. &quot;address&quot;: &quot;&lt;user_mail_2&gt;&quot;,
  14173. &quot;added_at&quot;: 1586458409743,
  14174. &quot;validated_at&quot;: 1586458409743
  14175. }
  14176. ],
  14177. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;, // can be null if not set
  14178. &quot;is_guest&quot;: 0,
  14179. &quot;admin&quot;: 0,
  14180. &quot;deactivated&quot;: 0,
  14181. &quot;shadow_banned&quot;: 0,
  14182. &quot;creation_ts&quot;: 1560432506,
  14183. &quot;appservice_id&quot;: null,
  14184. &quot;consent_server_notice_sent&quot;: null,
  14185. &quot;consent_version&quot;: null,
  14186. &quot;external_ids&quot;: [
  14187. {
  14188. &quot;auth_provider&quot;: &quot;&lt;provider1&gt;&quot;,
  14189. &quot;external_id&quot;: &quot;&lt;user_id_provider_1&gt;&quot;
  14190. },
  14191. {
  14192. &quot;auth_provider&quot;: &quot;&lt;provider2&gt;&quot;,
  14193. &quot;external_id&quot;: &quot;&lt;user_id_provider_2&gt;&quot;
  14194. }
  14195. ],
  14196. &quot;user_type&quot;: null
  14197. }
  14198. </code></pre>
  14199. <p>URL parameters:</p>
  14200. <ul>
  14201. <li><code>user_id</code>: fully-qualified user id: for example, <code>@user:server.com</code>.</li>
  14202. </ul>
  14203. <h2 id="create-or-modify-account"><a class="header" href="#create-or-modify-account">Create or modify Account</a></h2>
  14204. <p>This API allows an administrator to create or modify a user account with a
  14205. specific <code>user_id</code>.</p>
  14206. <p>This api is:</p>
  14207. <pre><code>PUT /_synapse/admin/v2/users/&lt;user_id&gt;
  14208. </code></pre>
  14209. <p>with a body of:</p>
  14210. <pre><code class="language-json">{
  14211. &quot;password&quot;: &quot;user_password&quot;,
  14212. &quot;displayname&quot;: &quot;User&quot;,
  14213. &quot;threepids&quot;: [
  14214. {
  14215. &quot;medium&quot;: &quot;email&quot;,
  14216. &quot;address&quot;: &quot;&lt;user_mail_1&gt;&quot;
  14217. },
  14218. {
  14219. &quot;medium&quot;: &quot;email&quot;,
  14220. &quot;address&quot;: &quot;&lt;user_mail_2&gt;&quot;
  14221. }
  14222. ],
  14223. &quot;external_ids&quot;: [
  14224. {
  14225. &quot;auth_provider&quot;: &quot;&lt;provider1&gt;&quot;,
  14226. &quot;external_id&quot;: &quot;&lt;user_id_provider_1&gt;&quot;
  14227. },
  14228. {
  14229. &quot;auth_provider&quot;: &quot;&lt;provider2&gt;&quot;,
  14230. &quot;external_id&quot;: &quot;&lt;user_id_provider_2&gt;&quot;
  14231. }
  14232. ],
  14233. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;,
  14234. &quot;admin&quot;: false,
  14235. &quot;deactivated&quot;: false,
  14236. &quot;user_type&quot;: null
  14237. }
  14238. </code></pre>
  14239. <p>Returns HTTP status code:</p>
  14240. <ul>
  14241. <li><code>201</code> - When a new user object was created.</li>
  14242. <li><code>200</code> - When a user was modified.</li>
  14243. </ul>
  14244. <p>URL parameters:</p>
  14245. <ul>
  14246. <li><code>user_id</code>: fully-qualified user id: for example, <code>@user:server.com</code>.</li>
  14247. </ul>
  14248. <p>Body parameters:</p>
  14249. <ul>
  14250. <li><code>password</code> - string, optional. If provided, the user's password is updated and all
  14251. devices are logged out.</li>
  14252. <li><code>displayname</code> - string, optional, defaults to the value of <code>user_id</code>.</li>
  14253. <li><code>threepids</code> - array, optional, allows setting the third-party IDs (email, msisdn)
  14254. <ul>
  14255. <li><code>medium</code> - string. Kind of third-party ID, either <code>email</code> or <code>msisdn</code>.</li>
  14256. <li><code>address</code> - string. Value of third-party ID.
  14257. belonging to a user.</li>
  14258. </ul>
  14259. </li>
  14260. <li><code>external_ids</code> - array, optional. Allow setting the identifier of the external identity
  14261. provider for SSO (Single sign-on). Details in
  14262. <a href="admin_api/../usage/configuration/homeserver_sample_config.html">Sample Configuration File</a>
  14263. section <code>sso</code> and <code>oidc_providers</code>.
  14264. <ul>
  14265. <li><code>auth_provider</code> - string. ID of the external identity provider. Value of <code>idp_id</code>
  14266. in the homeserver configuration. Note that no error is raised if the provided
  14267. value is not in the homeserver configuration.</li>
  14268. <li><code>external_id</code> - string, user ID in the external identity provider.</li>
  14269. </ul>
  14270. </li>
  14271. <li><code>avatar_url</code> - string, optional, must be a
  14272. <a href="https://matrix.org/docs/spec/client_server/r0.6.0#matrix-content-mxc-uris">MXC URI</a>.</li>
  14273. <li><code>admin</code> - bool, optional, defaults to <code>false</code>.</li>
  14274. <li><code>deactivated</code> - bool, optional. If unspecified, deactivation state will be left
  14275. unchanged on existing accounts and set to <code>false</code> for new accounts.
  14276. A user cannot be erased by deactivating with this API. For details on
  14277. deactivating users see <a href="admin_api/user_admin_api.html#deactivate-account">Deactivate Account</a>.</li>
  14278. <li><code>user_type</code> - string or null, optional. If provided, the user type will be
  14279. adjusted. If <code>null</code> given, the user type will be cleared. Other
  14280. allowed options are: <code>bot</code> and <code>support</code>.</li>
  14281. </ul>
  14282. <p>If the user already exists then optional parameters default to the current value.</p>
  14283. <p>In order to re-activate an account <code>deactivated</code> must be set to <code>false</code>. If
  14284. users do not login via single-sign-on, a new <code>password</code> must be provided.</p>
  14285. <h2 id="list-accounts"><a class="header" href="#list-accounts">List Accounts</a></h2>
  14286. <p>This API returns all local user accounts.
  14287. By default, the response is ordered by ascending user ID.</p>
  14288. <pre><code>GET /_synapse/admin/v2/users?from=0&amp;limit=10&amp;guests=false
  14289. </code></pre>
  14290. <p>A response body like the following is returned:</p>
  14291. <pre><code class="language-json">{
  14292. &quot;users&quot;: [
  14293. {
  14294. &quot;name&quot;: &quot;&lt;user_id1&gt;&quot;,
  14295. &quot;is_guest&quot;: 0,
  14296. &quot;admin&quot;: 0,
  14297. &quot;user_type&quot;: null,
  14298. &quot;deactivated&quot;: 0,
  14299. &quot;shadow_banned&quot;: 0,
  14300. &quot;displayname&quot;: &quot;&lt;User One&gt;&quot;,
  14301. &quot;avatar_url&quot;: null,
  14302. &quot;creation_ts&quot;: 1560432668000
  14303. }, {
  14304. &quot;name&quot;: &quot;&lt;user_id2&gt;&quot;,
  14305. &quot;is_guest&quot;: 0,
  14306. &quot;admin&quot;: 1,
  14307. &quot;user_type&quot;: null,
  14308. &quot;deactivated&quot;: 0,
  14309. &quot;shadow_banned&quot;: 0,
  14310. &quot;displayname&quot;: &quot;&lt;User Two&gt;&quot;,
  14311. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;,
  14312. &quot;creation_ts&quot;: 1561550621000
  14313. }
  14314. ],
  14315. &quot;next_token&quot;: &quot;100&quot;,
  14316. &quot;total&quot;: 200
  14317. }
  14318. </code></pre>
  14319. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again
  14320. with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  14321. <p>If the endpoint does not return a <code>next_token</code> then there are no more users
  14322. to paginate through.</p>
  14323. <p><strong>Parameters</strong></p>
  14324. <p>The following parameters should be set in the URL:</p>
  14325. <ul>
  14326. <li>
  14327. <p><code>user_id</code> - Is optional and filters to only return users with user IDs
  14328. that contain this value. This parameter is ignored when using the <code>name</code> parameter.</p>
  14329. </li>
  14330. <li>
  14331. <p><code>name</code> - Is optional and filters to only return users with user ID localparts
  14332. <strong>or</strong> displaynames that contain this value.</p>
  14333. </li>
  14334. <li>
  14335. <p><code>guests</code> - string representing a bool - Is optional and if <code>false</code> will <strong>exclude</strong> guest users.
  14336. Defaults to <code>true</code> to include guest users.</p>
  14337. </li>
  14338. <li>
  14339. <p><code>deactivated</code> - string representing a bool - Is optional and if <code>true</code> will <strong>include</strong> deactivated users.
  14340. Defaults to <code>false</code> to exclude deactivated users.</p>
  14341. </li>
  14342. <li>
  14343. <p><code>limit</code> - string representing a positive integer - Is optional but is used for pagination,
  14344. denoting the maximum number of items to return in this call. Defaults to <code>100</code>.</p>
  14345. </li>
  14346. <li>
  14347. <p><code>from</code> - string representing a positive integer - Is optional but used for pagination,
  14348. denoting the offset in the returned results. This should be treated as an opaque value and
  14349. not explicitly set to anything other than the return value of <code>next_token</code> from a previous call.
  14350. Defaults to <code>0</code>.</p>
  14351. </li>
  14352. <li>
  14353. <p><code>order_by</code> - The method by which to sort the returned list of users.
  14354. If the ordered field has duplicates, the second order is always by ascending <code>name</code>,
  14355. which guarantees a stable ordering. Valid values are:</p>
  14356. <ul>
  14357. <li><code>name</code> - Users are ordered alphabetically by <code>name</code>. This is the default.</li>
  14358. <li><code>is_guest</code> - Users are ordered by <code>is_guest</code> status.</li>
  14359. <li><code>admin</code> - Users are ordered by <code>admin</code> status.</li>
  14360. <li><code>user_type</code> - Users are ordered alphabetically by <code>user_type</code>.</li>
  14361. <li><code>deactivated</code> - Users are ordered by <code>deactivated</code> status.</li>
  14362. <li><code>shadow_banned</code> - Users are ordered by <code>shadow_banned</code> status.</li>
  14363. <li><code>displayname</code> - Users are ordered alphabetically by <code>displayname</code>.</li>
  14364. <li><code>avatar_url</code> - Users are ordered alphabetically by avatar URL.</li>
  14365. <li><code>creation_ts</code> - Users are ordered by when the users was created in ms.</li>
  14366. </ul>
  14367. </li>
  14368. <li>
  14369. <p><code>dir</code> - Direction of media order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  14370. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</p>
  14371. </li>
  14372. </ul>
  14373. <p>Caution. The database only has indexes on the columns <code>name</code> and <code>creation_ts</code>.
  14374. This means that if a different sort order is used (<code>is_guest</code>, <code>admin</code>,
  14375. <code>user_type</code>, <code>deactivated</code>, <code>shadow_banned</code>, <code>avatar_url</code> or <code>displayname</code>),
  14376. this can cause a large load on the database, especially for large environments.</p>
  14377. <p><strong>Response</strong></p>
  14378. <p>The following fields are returned in the JSON response body:</p>
  14379. <ul>
  14380. <li>
  14381. <p><code>users</code> - An array of objects, each containing information about an user.
  14382. User objects contain the following fields:</p>
  14383. <ul>
  14384. <li><code>name</code> - string - Fully-qualified user ID (ex. <code>@user:server.com</code>).</li>
  14385. <li><code>is_guest</code> - bool - Status if that user is a guest account.</li>
  14386. <li><code>admin</code> - bool - Status if that user is a server administrator.</li>
  14387. <li><code>user_type</code> - string - Type of the user. Normal users are type <code>None</code>.
  14388. This allows user type specific behaviour. There are also types <code>support</code> and <code>bot</code>. </li>
  14389. <li><code>deactivated</code> - bool - Status if that user has been marked as deactivated.</li>
  14390. <li><code>shadow_banned</code> - bool - Status if that user has been marked as shadow banned.</li>
  14391. <li><code>displayname</code> - string - The user's display name if they have set one.</li>
  14392. <li><code>avatar_url</code> - string - The user's avatar URL if they have set one.</li>
  14393. <li><code>creation_ts</code> - integer - The user's creation timestamp in ms.</li>
  14394. </ul>
  14395. </li>
  14396. <li>
  14397. <p><code>next_token</code>: string representing a positive integer - Indication for pagination. See above.</p>
  14398. </li>
  14399. <li>
  14400. <p><code>total</code> - integer - Total number of media.</p>
  14401. </li>
  14402. </ul>
  14403. <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>
  14404. <p>This API returns information about the active sessions for a specific user.</p>
  14405. <p>The endpoints are:</p>
  14406. <pre><code>GET /_synapse/admin/v1/whois/&lt;user_id&gt;
  14407. </code></pre>
  14408. <p>and:</p>
  14409. <pre><code>GET /_matrix/client/r0/admin/whois/&lt;userId&gt;
  14410. </code></pre>
  14411. <p>See also: <a href="https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-admin-whois-userid">Client Server
  14412. API Whois</a>.</p>
  14413. <p>It returns a JSON body like the following:</p>
  14414. <pre><code class="language-json">{
  14415. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;,
  14416. &quot;devices&quot;: {
  14417. &quot;&quot;: {
  14418. &quot;sessions&quot;: [
  14419. {
  14420. &quot;connections&quot;: [
  14421. {
  14422. &quot;ip&quot;: &quot;1.2.3.4&quot;,
  14423. &quot;last_seen&quot;: 1417222374433,
  14424. &quot;user_agent&quot;: &quot;Mozilla/5.0 ...&quot;
  14425. },
  14426. {
  14427. &quot;ip&quot;: &quot;1.2.3.10&quot;,
  14428. &quot;last_seen&quot;: 1417222374500,
  14429. &quot;user_agent&quot;: &quot;Dalvik/2.1.0 ...&quot;
  14430. }
  14431. ]
  14432. }
  14433. ]
  14434. }
  14435. }
  14436. }
  14437. </code></pre>
  14438. <p><code>last_seen</code> is measured in milliseconds since the Unix epoch.</p>
  14439. <h2 id="deactivate-account"><a class="header" href="#deactivate-account">Deactivate Account</a></h2>
  14440. <p>This API deactivates an account. It removes active access tokens, resets the
  14441. password, and deletes third-party IDs (to prevent the user requesting a
  14442. password reset).</p>
  14443. <p>It can also mark the user as GDPR-erased. This means messages sent by the
  14444. user will still be visible by anyone that was in the room when these messages
  14445. were sent, but hidden from users joining the room afterwards.</p>
  14446. <p>The api is:</p>
  14447. <pre><code>POST /_synapse/admin/v1/deactivate/&lt;user_id&gt;
  14448. </code></pre>
  14449. <p>with a body of:</p>
  14450. <pre><code class="language-json">{
  14451. &quot;erase&quot;: true
  14452. }
  14453. </code></pre>
  14454. <p>The erase parameter is optional and defaults to <code>false</code>.
  14455. An empty body may be passed for backwards compatibility.</p>
  14456. <p>The following actions are performed when deactivating an user:</p>
  14457. <ul>
  14458. <li>Try to unbind 3PIDs from the identity server</li>
  14459. <li>Remove all 3PIDs from the homeserver</li>
  14460. <li>Delete all devices and E2EE keys</li>
  14461. <li>Delete all access tokens</li>
  14462. <li>Delete all pushers</li>
  14463. <li>Delete the password hash</li>
  14464. <li>Removal from all rooms the user is a member of</li>
  14465. <li>Remove the user from the user directory</li>
  14466. <li>Reject all pending invites</li>
  14467. <li>Remove all account validity information related to the user</li>
  14468. <li>Remove the arbitrary data store known as <em>account data</em>. For example, this includes:
  14469. <ul>
  14470. <li>list of ignored users;</li>
  14471. <li>push rules;</li>
  14472. <li>secret storage keys; and</li>
  14473. <li>cross-signing keys.</li>
  14474. </ul>
  14475. </li>
  14476. </ul>
  14477. <p>The following additional actions are performed during deactivation if <code>erase</code>
  14478. is set to <code>true</code>:</p>
  14479. <ul>
  14480. <li>Remove the user's display name</li>
  14481. <li>Remove the user's avatar URL</li>
  14482. <li>Mark the user as erased</li>
  14483. </ul>
  14484. <p>The following actions are <strong>NOT</strong> performed. The list may be incomplete.</p>
  14485. <ul>
  14486. <li>Remove mappings of SSO IDs</li>
  14487. <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>
  14488. <li>Delete sent and received messages</li>
  14489. <li>Remove the user's creation (registration) timestamp</li>
  14490. <li><a href="admin_api/user_admin_api.html#override-ratelimiting-for-users">Remove rate limit overrides</a></li>
  14491. <li>Remove from monthly active users</li>
  14492. </ul>
  14493. <h2 id="reset-password"><a class="header" href="#reset-password">Reset password</a></h2>
  14494. <p>Changes the password of another user. This will automatically log the user out of all their devices.</p>
  14495. <p>The api is:</p>
  14496. <pre><code>POST /_synapse/admin/v1/reset_password/&lt;user_id&gt;
  14497. </code></pre>
  14498. <p>with a body of:</p>
  14499. <pre><code class="language-json">{
  14500. &quot;new_password&quot;: &quot;&lt;secret&gt;&quot;,
  14501. &quot;logout_devices&quot;: true
  14502. }
  14503. </code></pre>
  14504. <p>The parameter <code>new_password</code> is required.
  14505. The parameter <code>logout_devices</code> is optional and defaults to <code>true</code>.</p>
  14506. <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>
  14507. <p>The api is:</p>
  14508. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/admin
  14509. </code></pre>
  14510. <p>A response body like the following is returned:</p>
  14511. <pre><code class="language-json">{
  14512. &quot;admin&quot;: true
  14513. }
  14514. </code></pre>
  14515. <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>
  14516. <p>Note that you cannot demote yourself.</p>
  14517. <p>The api is:</p>
  14518. <pre><code>PUT /_synapse/admin/v1/users/&lt;user_id&gt;/admin
  14519. </code></pre>
  14520. <p>with a body of:</p>
  14521. <pre><code class="language-json">{
  14522. &quot;admin&quot;: true
  14523. }
  14524. </code></pre>
  14525. <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>
  14526. <p>Gets a list of all <code>room_id</code> that a specific <code>user_id</code> is member.</p>
  14527. <p>The API is:</p>
  14528. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/joined_rooms
  14529. </code></pre>
  14530. <p>A response body like the following is returned:</p>
  14531. <pre><code class="language-json"> {
  14532. &quot;joined_rooms&quot;: [
  14533. &quot;!DuGcnbhHGaSZQoNQR:matrix.org&quot;,
  14534. &quot;!ZtSaPCawyWtxfWiIy:matrix.org&quot;
  14535. ],
  14536. &quot;total&quot;: 2
  14537. }
  14538. </code></pre>
  14539. <p>The server returns the list of rooms of which the user and the server
  14540. are member. If the user is local, all the rooms of which the user is
  14541. member are returned.</p>
  14542. <p><strong>Parameters</strong></p>
  14543. <p>The following parameters should be set in the URL:</p>
  14544. <ul>
  14545. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  14546. </ul>
  14547. <p><strong>Response</strong></p>
  14548. <p>The following fields are returned in the JSON response body:</p>
  14549. <ul>
  14550. <li><code>joined_rooms</code> - An array of <code>room_id</code>.</li>
  14551. <li><code>total</code> - Number of rooms.</li>
  14552. </ul>
  14553. <h2 id="account-data"><a class="header" href="#account-data">Account Data</a></h2>
  14554. <p>Gets information about account data for a specific <code>user_id</code>.</p>
  14555. <p>The API is:</p>
  14556. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/accountdata
  14557. </code></pre>
  14558. <p>A response body like the following is returned:</p>
  14559. <pre><code class="language-json">{
  14560. &quot;account_data&quot;: {
  14561. &quot;global&quot;: {
  14562. &quot;m.secret_storage.key.LmIGHTg5W&quot;: {
  14563. &quot;algorithm&quot;: &quot;m.secret_storage.v1.aes-hmac-sha2&quot;,
  14564. &quot;iv&quot;: &quot;fwjNZatxg==&quot;,
  14565. &quot;mac&quot;: &quot;eWh9kNnLWZUNOgnc=&quot;
  14566. },
  14567. &quot;im.vector.hide_profile&quot;: {
  14568. &quot;hide_profile&quot;: true
  14569. },
  14570. &quot;org.matrix.preview_urls&quot;: {
  14571. &quot;disable&quot;: false
  14572. },
  14573. &quot;im.vector.riot.breadcrumb_rooms&quot;: {
  14574. &quot;rooms&quot;: [
  14575. &quot;!LxcBDAsDUVAfJDEo:matrix.org&quot;,
  14576. &quot;!MAhRxqasbItjOqxu:matrix.org&quot;
  14577. ]
  14578. },
  14579. &quot;m.accepted_terms&quot;: {
  14580. &quot;accepted&quot;: [
  14581. &quot;https://example.org/somewhere/privacy-1.2-en.html&quot;,
  14582. &quot;https://example.org/somewhere/terms-2.0-en.html&quot;
  14583. ]
  14584. },
  14585. &quot;im.vector.setting.breadcrumbs&quot;: {
  14586. &quot;recent_rooms&quot;: [
  14587. &quot;!MAhRxqasbItqxuEt:matrix.org&quot;,
  14588. &quot;!ZtSaPCawyWtxiImy:matrix.org&quot;
  14589. ]
  14590. }
  14591. },
  14592. &quot;rooms&quot;: {
  14593. &quot;!GUdfZSHUJibpiVqHYd:matrix.org&quot;: {
  14594. &quot;m.fully_read&quot;: {
  14595. &quot;event_id&quot;: &quot;$156334540fYIhZ:matrix.org&quot;
  14596. }
  14597. },
  14598. &quot;!tOZwOOiqwCYQkLhV:matrix.org&quot;: {
  14599. &quot;m.fully_read&quot;: {
  14600. &quot;event_id&quot;: &quot;$xjsIyp4_NaVl2yPvIZs_k1Jl8tsC_Sp23wjqXPno&quot;
  14601. }
  14602. }
  14603. }
  14604. }
  14605. }
  14606. </code></pre>
  14607. <p><strong>Parameters</strong></p>
  14608. <p>The following parameters should be set in the URL:</p>
  14609. <ul>
  14610. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  14611. </ul>
  14612. <p><strong>Response</strong></p>
  14613. <p>The following fields are returned in the JSON response body:</p>
  14614. <ul>
  14615. <li><code>account_data</code> - A map containing the account data for the user
  14616. <ul>
  14617. <li><code>global</code> - A map containing the global account data for the user</li>
  14618. <li><code>rooms</code> - A map containing the account data per room for the user</li>
  14619. </ul>
  14620. </li>
  14621. </ul>
  14622. <h2 id="user-media"><a class="header" href="#user-media">User media</a></h2>
  14623. <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>
  14624. <p>Gets a list of all local media that a specific <code>user_id</code> has created.
  14625. These are media that the user has uploaded themselves
  14626. (<a href="admin_api/../media_repository.html#local-media">local media</a>), as well as
  14627. <a href="admin_api/../media_repository.html#url-previews">URL preview images</a> requested by the user if the
  14628. <a href="admin_api/../development/url_previews.html">feature is enabled</a>.</p>
  14629. <p>By default, the response is ordered by descending creation date and ascending media ID.
  14630. The newest media is on top. You can change the order with parameters
  14631. <code>order_by</code> and <code>dir</code>.</p>
  14632. <p>The API is:</p>
  14633. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/media
  14634. </code></pre>
  14635. <p>A response body like the following is returned:</p>
  14636. <pre><code class="language-json">{
  14637. &quot;media&quot;: [
  14638. {
  14639. &quot;created_ts&quot;: 100400,
  14640. &quot;last_access_ts&quot;: null,
  14641. &quot;media_id&quot;: &quot;qXhyRzulkwLsNHTbpHreuEgo&quot;,
  14642. &quot;media_length&quot;: 67,
  14643. &quot;media_type&quot;: &quot;image/png&quot;,
  14644. &quot;quarantined_by&quot;: null,
  14645. &quot;safe_from_quarantine&quot;: false,
  14646. &quot;upload_name&quot;: &quot;test1.png&quot;
  14647. },
  14648. {
  14649. &quot;created_ts&quot;: 200400,
  14650. &quot;last_access_ts&quot;: null,
  14651. &quot;media_id&quot;: &quot;FHfiSnzoINDatrXHQIXBtahw&quot;,
  14652. &quot;media_length&quot;: 67,
  14653. &quot;media_type&quot;: &quot;image/png&quot;,
  14654. &quot;quarantined_by&quot;: null,
  14655. &quot;safe_from_quarantine&quot;: false,
  14656. &quot;upload_name&quot;: &quot;test2.png&quot;
  14657. }
  14658. ],
  14659. &quot;next_token&quot;: 3,
  14660. &quot;total&quot;: 2
  14661. }
  14662. </code></pre>
  14663. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again
  14664. with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  14665. <p>If the endpoint does not return a <code>next_token</code> then there are no more
  14666. reports to paginate through.</p>
  14667. <p><strong>Parameters</strong></p>
  14668. <p>The following parameters should be set in the URL:</p>
  14669. <ul>
  14670. <li>
  14671. <p><code>user_id</code> - string - fully qualified: for example, <code>@user:server.com</code>.</p>
  14672. </li>
  14673. <li>
  14674. <p><code>limit</code>: string representing a positive integer - Is optional but is used for pagination,
  14675. denoting the maximum number of items to return in this call. Defaults to <code>100</code>.</p>
  14676. </li>
  14677. <li>
  14678. <p><code>from</code>: string representing a positive integer - Is optional but used for pagination,
  14679. denoting the offset in the returned results. This should be treated as an opaque value and
  14680. not explicitly set to anything other than the return value of <code>next_token</code> from a previous call.
  14681. Defaults to <code>0</code>.</p>
  14682. </li>
  14683. <li>
  14684. <p><code>order_by</code> - The method by which to sort the returned list of media.
  14685. If the ordered field has duplicates, the second order is always by ascending <code>media_id</code>,
  14686. which guarantees a stable ordering. Valid values are:</p>
  14687. <ul>
  14688. <li><code>media_id</code> - Media are ordered alphabetically by <code>media_id</code>.</li>
  14689. <li><code>upload_name</code> - Media are ordered alphabetically by name the media was uploaded with.</li>
  14690. <li><code>created_ts</code> - Media are ordered by when the content was uploaded in ms.
  14691. Smallest to largest. This is the default.</li>
  14692. <li><code>last_access_ts</code> - Media are ordered by when the content was last accessed in ms.
  14693. Smallest to largest.</li>
  14694. <li><code>media_length</code> - Media are ordered by length of the media in bytes.
  14695. Smallest to largest.</li>
  14696. <li><code>media_type</code> - Media are ordered alphabetically by MIME-type.</li>
  14697. <li><code>quarantined_by</code> - Media are ordered alphabetically by the user ID that
  14698. initiated the quarantine request for this media.</li>
  14699. <li><code>safe_from_quarantine</code> - Media are ordered by the status if this media is safe
  14700. from quarantining.</li>
  14701. </ul>
  14702. </li>
  14703. <li>
  14704. <p><code>dir</code> - Direction of media order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  14705. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</p>
  14706. </li>
  14707. </ul>
  14708. <p>If neither <code>order_by</code> nor <code>dir</code> is set, the default order is newest media on top
  14709. (corresponds to <code>order_by</code> = <code>created_ts</code> and <code>dir</code> = <code>b</code>).</p>
  14710. <p>Caution. The database only has indexes on the columns <code>media_id</code>,
  14711. <code>user_id</code> and <code>created_ts</code>. This means that if a different sort order is used
  14712. (<code>upload_name</code>, <code>last_access_ts</code>, <code>media_length</code>, <code>media_type</code>,
  14713. <code>quarantined_by</code> or <code>safe_from_quarantine</code>), this can cause a large load on the
  14714. database, especially for large environments.</p>
  14715. <p><strong>Response</strong></p>
  14716. <p>The following fields are returned in the JSON response body:</p>
  14717. <ul>
  14718. <li><code>media</code> - An array of objects, each containing information about a media.
  14719. Media objects contain the following fields:
  14720. <ul>
  14721. <li><code>created_ts</code> - integer - Timestamp when the content was uploaded in ms.</li>
  14722. <li><code>last_access_ts</code> - integer - Timestamp when the content was last accessed in ms.</li>
  14723. <li><code>media_id</code> - string - The id used to refer to the media. Details about the format
  14724. are documented under
  14725. <a href="admin_api/../media_repository.html">media repository</a>.</li>
  14726. <li><code>media_length</code> - integer - Length of the media in bytes.</li>
  14727. <li><code>media_type</code> - string - The MIME-type of the media.</li>
  14728. <li><code>quarantined_by</code> - string - The user ID that initiated the quarantine request
  14729. for this media.</li>
  14730. <li><code>safe_from_quarantine</code> - bool - Status if this media is safe from quarantining.</li>
  14731. <li><code>upload_name</code> - string - The name the media was uploaded with.</li>
  14732. </ul>
  14733. </li>
  14734. <li><code>next_token</code>: integer - Indication for pagination. See above.</li>
  14735. <li><code>total</code> - integer - Total number of media.</li>
  14736. </ul>
  14737. <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>
  14738. <p>This API deletes the <em>local</em> media from the disk of your own server
  14739. that a specific <code>user_id</code> has created. This includes any local thumbnails.</p>
  14740. <p>This API will not affect media that has been uploaded to external
  14741. media repositories (e.g https://github.com/turt2live/matrix-media-repo/).</p>
  14742. <p>By default, the API deletes media ordered by descending creation date and ascending media ID.
  14743. The newest media is deleted first. You can change the order with parameters
  14744. <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>
  14745. <p>The API is:</p>
  14746. <pre><code>DELETE /_synapse/admin/v1/users/&lt;user_id&gt;/media
  14747. </code></pre>
  14748. <p>A response body like the following is returned:</p>
  14749. <pre><code class="language-json">{
  14750. &quot;deleted_media&quot;: [
  14751. &quot;abcdefghijklmnopqrstuvwx&quot;
  14752. ],
  14753. &quot;total&quot;: 1
  14754. }
  14755. </code></pre>
  14756. <p>The following fields are returned in the JSON response body:</p>
  14757. <ul>
  14758. <li><code>deleted_media</code>: an array of strings - List of deleted <code>media_id</code></li>
  14759. <li><code>total</code>: integer - Total number of deleted <code>media_id</code></li>
  14760. </ul>
  14761. <p><strong>Note</strong>: There is no <code>next_token</code>. This is not useful for deleting media, because
  14762. after deleting media the remaining media have a new order.</p>
  14763. <p><strong>Parameters</strong></p>
  14764. <p>This API has the same parameters as
  14765. <a href="admin_api/user_admin_api.html#list-media-uploaded-by-a-user">List media uploaded by a user</a>.
  14766. With the parameters you can for example limit the number of files to delete at once or
  14767. delete largest/smallest or newest/oldest files first.</p>
  14768. <h2 id="login-as-a-user"><a class="header" href="#login-as-a-user">Login as a user</a></h2>
  14769. <p>Get an access token that can be used to authenticate as that user. Useful for
  14770. when admins wish to do actions on behalf of a user.</p>
  14771. <p>The API is:</p>
  14772. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/login
  14773. {}
  14774. </code></pre>
  14775. <p>An optional <code>valid_until_ms</code> field can be specified in the request body as an
  14776. integer timestamp that specifies when the token should expire. By default tokens
  14777. do not expire.</p>
  14778. <p>A response body like the following is returned:</p>
  14779. <pre><code class="language-json">{
  14780. &quot;access_token&quot;: &quot;&lt;opaque_access_token_string&gt;&quot;
  14781. }
  14782. </code></pre>
  14783. <p>This API does <em>not</em> generate a new device for the user, and so will not appear
  14784. their <code>/devices</code> list, and in general the target user should not be able to
  14785. tell they have been logged in as.</p>
  14786. <p>To expire the token call the standard <code>/logout</code> API with the token.</p>
  14787. <p>Note: The token will expire if the <em>admin</em> user calls <code>/logout/all</code> from any
  14788. of their devices, but the token will <em>not</em> expire if the target user does the
  14789. same.</p>
  14790. <h2 id="user-devices"><a class="header" href="#user-devices">User devices</a></h2>
  14791. <h3 id="list-all-devices"><a class="header" href="#list-all-devices">List all devices</a></h3>
  14792. <p>Gets information about all devices for a specific <code>user_id</code>.</p>
  14793. <p>The API is:</p>
  14794. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;/devices
  14795. </code></pre>
  14796. <p>A response body like the following is returned:</p>
  14797. <pre><code class="language-json">{
  14798. &quot;devices&quot;: [
  14799. {
  14800. &quot;device_id&quot;: &quot;QBUAZIFURK&quot;,
  14801. &quot;display_name&quot;: &quot;android&quot;,
  14802. &quot;last_seen_ip&quot;: &quot;1.2.3.4&quot;,
  14803. &quot;last_seen_ts&quot;: 1474491775024,
  14804. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  14805. },
  14806. {
  14807. &quot;device_id&quot;: &quot;AUIECTSRND&quot;,
  14808. &quot;display_name&quot;: &quot;ios&quot;,
  14809. &quot;last_seen_ip&quot;: &quot;1.2.3.5&quot;,
  14810. &quot;last_seen_ts&quot;: 1474491775025,
  14811. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  14812. }
  14813. ],
  14814. &quot;total&quot;: 2
  14815. }
  14816. </code></pre>
  14817. <p><strong>Parameters</strong></p>
  14818. <p>The following parameters should be set in the URL:</p>
  14819. <ul>
  14820. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  14821. </ul>
  14822. <p><strong>Response</strong></p>
  14823. <p>The following fields are returned in the JSON response body:</p>
  14824. <ul>
  14825. <li>
  14826. <p><code>devices</code> - An array of objects, each containing information about a device.
  14827. Device objects contain the following fields:</p>
  14828. <ul>
  14829. <li><code>device_id</code> - Identifier of device.</li>
  14830. <li><code>display_name</code> - Display name set by the user for this device.
  14831. Absent if no name has been set.</li>
  14832. <li><code>last_seen_ip</code> - The IP address where this device was last seen.
  14833. (May be a few minutes out of date, for efficiency reasons).</li>
  14834. <li><code>last_seen_ts</code> - The timestamp (in milliseconds since the unix epoch) when this
  14835. devices was last seen. (May be a few minutes out of date, for efficiency reasons).</li>
  14836. <li><code>user_id</code> - Owner of device.</li>
  14837. </ul>
  14838. </li>
  14839. <li>
  14840. <p><code>total</code> - Total number of user's devices.</p>
  14841. </li>
  14842. </ul>
  14843. <h3 id="delete-multiple-devices"><a class="header" href="#delete-multiple-devices">Delete multiple devices</a></h3>
  14844. <p>Deletes the given devices for a specific <code>user_id</code>, and invalidates
  14845. any access token associated with them.</p>
  14846. <p>The API is:</p>
  14847. <pre><code>POST /_synapse/admin/v2/users/&lt;user_id&gt;/delete_devices
  14848. {
  14849. &quot;devices&quot;: [
  14850. &quot;QBUAZIFURK&quot;,
  14851. &quot;AUIECTSRND&quot;
  14852. ]
  14853. }
  14854. </code></pre>
  14855. <p>An empty JSON dict is returned.</p>
  14856. <p><strong>Parameters</strong></p>
  14857. <p>The following parameters should be set in the URL:</p>
  14858. <ul>
  14859. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  14860. </ul>
  14861. <p>The following fields are required in the JSON request body:</p>
  14862. <ul>
  14863. <li><code>devices</code> - The list of device IDs to delete.</li>
  14864. </ul>
  14865. <h3 id="show-a-device"><a class="header" href="#show-a-device">Show a device</a></h3>
  14866. <p>Gets information on a single device, by <code>device_id</code> for a specific <code>user_id</code>.</p>
  14867. <p>The API is:</p>
  14868. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  14869. </code></pre>
  14870. <p>A response body like the following is returned:</p>
  14871. <pre><code class="language-json">{
  14872. &quot;device_id&quot;: &quot;&lt;device_id&gt;&quot;,
  14873. &quot;display_name&quot;: &quot;android&quot;,
  14874. &quot;last_seen_ip&quot;: &quot;1.2.3.4&quot;,
  14875. &quot;last_seen_ts&quot;: 1474491775024,
  14876. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  14877. }
  14878. </code></pre>
  14879. <p><strong>Parameters</strong></p>
  14880. <p>The following parameters should be set in the URL:</p>
  14881. <ul>
  14882. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  14883. <li><code>device_id</code> - The device to retrieve.</li>
  14884. </ul>
  14885. <p><strong>Response</strong></p>
  14886. <p>The following fields are returned in the JSON response body:</p>
  14887. <ul>
  14888. <li><code>device_id</code> - Identifier of device.</li>
  14889. <li><code>display_name</code> - Display name set by the user for this device.
  14890. Absent if no name has been set.</li>
  14891. <li><code>last_seen_ip</code> - The IP address where this device was last seen.
  14892. (May be a few minutes out of date, for efficiency reasons).</li>
  14893. <li><code>last_seen_ts</code> - The timestamp (in milliseconds since the unix epoch) when this
  14894. devices was last seen. (May be a few minutes out of date, for efficiency reasons).</li>
  14895. <li><code>user_id</code> - Owner of device.</li>
  14896. </ul>
  14897. <h3 id="update-a-device"><a class="header" href="#update-a-device">Update a device</a></h3>
  14898. <p>Updates the metadata on the given <code>device_id</code> for a specific <code>user_id</code>.</p>
  14899. <p>The API is:</p>
  14900. <pre><code>PUT /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  14901. {
  14902. &quot;display_name&quot;: &quot;My other phone&quot;
  14903. }
  14904. </code></pre>
  14905. <p>An empty JSON dict is returned.</p>
  14906. <p><strong>Parameters</strong></p>
  14907. <p>The following parameters should be set in the URL:</p>
  14908. <ul>
  14909. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  14910. <li><code>device_id</code> - The device to update.</li>
  14911. </ul>
  14912. <p>The following fields are required in the JSON request body:</p>
  14913. <ul>
  14914. <li><code>display_name</code> - The new display name for this device. If not given,
  14915. the display name is unchanged.</li>
  14916. </ul>
  14917. <h3 id="delete-a-device"><a class="header" href="#delete-a-device">Delete a device</a></h3>
  14918. <p>Deletes the given <code>device_id</code> for a specific <code>user_id</code>,
  14919. and invalidates any access token associated with it.</p>
  14920. <p>The API is:</p>
  14921. <pre><code>DELETE /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  14922. {}
  14923. </code></pre>
  14924. <p>An empty JSON dict is returned.</p>
  14925. <p><strong>Parameters</strong></p>
  14926. <p>The following parameters should be set in the URL:</p>
  14927. <ul>
  14928. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  14929. <li><code>device_id</code> - The device to delete.</li>
  14930. </ul>
  14931. <h2 id="list-all-pushers"><a class="header" href="#list-all-pushers">List all pushers</a></h2>
  14932. <p>Gets information about all pushers for a specific <code>user_id</code>.</p>
  14933. <p>The API is:</p>
  14934. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/pushers
  14935. </code></pre>
  14936. <p>A response body like the following is returned:</p>
  14937. <pre><code class="language-json">{
  14938. &quot;pushers&quot;: [
  14939. {
  14940. &quot;app_display_name&quot;:&quot;HTTP Push Notifications&quot;,
  14941. &quot;app_id&quot;:&quot;m.http&quot;,
  14942. &quot;data&quot;: {
  14943. &quot;url&quot;:&quot;example.com&quot;
  14944. },
  14945. &quot;device_display_name&quot;:&quot;pushy push&quot;,
  14946. &quot;kind&quot;:&quot;http&quot;,
  14947. &quot;lang&quot;:&quot;None&quot;,
  14948. &quot;profile_tag&quot;:&quot;&quot;,
  14949. &quot;pushkey&quot;:&quot;a@example.com&quot;
  14950. }
  14951. ],
  14952. &quot;total&quot;: 1
  14953. }
  14954. </code></pre>
  14955. <p><strong>Parameters</strong></p>
  14956. <p>The following parameters should be set in the URL:</p>
  14957. <ul>
  14958. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  14959. </ul>
  14960. <p><strong>Response</strong></p>
  14961. <p>The following fields are returned in the JSON response body:</p>
  14962. <ul>
  14963. <li>
  14964. <p><code>pushers</code> - An array containing the current pushers for the user</p>
  14965. <ul>
  14966. <li>
  14967. <p><code>app_display_name</code> - string - A string that will allow the user to identify
  14968. what application owns this pusher.</p>
  14969. </li>
  14970. <li>
  14971. <p><code>app_id</code> - string - This is a reverse-DNS style identifier for the application.
  14972. Max length, 64 chars.</p>
  14973. </li>
  14974. <li>
  14975. <p><code>data</code> - A dictionary of information for the pusher implementation itself.</p>
  14976. <ul>
  14977. <li>
  14978. <p><code>url</code> - string - Required if <code>kind</code> is <code>http</code>. The URL to use to send
  14979. notifications to.</p>
  14980. </li>
  14981. <li>
  14982. <p><code>format</code> - string - The format to use when sending notifications to the
  14983. Push Gateway.</p>
  14984. </li>
  14985. </ul>
  14986. </li>
  14987. <li>
  14988. <p><code>device_display_name</code> - string - A string that will allow the user to identify
  14989. what device owns this pusher.</p>
  14990. </li>
  14991. <li>
  14992. <p><code>profile_tag</code> - string - This string determines which set of device specific rules
  14993. this pusher executes.</p>
  14994. </li>
  14995. <li>
  14996. <p><code>kind</code> - string - The kind of pusher. &quot;http&quot; is a pusher that sends HTTP pokes.</p>
  14997. </li>
  14998. <li>
  14999. <p><code>lang</code> - string - The preferred language for receiving notifications
  15000. (e.g. 'en' or 'en-US')</p>
  15001. </li>
  15002. <li>
  15003. <p><code>profile_tag</code> - string - This string determines which set of device specific rules
  15004. this pusher executes.</p>
  15005. </li>
  15006. <li>
  15007. <p><code>pushkey</code> - string - This is a unique identifier for this pusher.
  15008. Max length, 512 bytes.</p>
  15009. </li>
  15010. </ul>
  15011. </li>
  15012. <li>
  15013. <p><code>total</code> - integer - Number of pushers.</p>
  15014. </li>
  15015. </ul>
  15016. <p>See also the
  15017. <a href="https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushers">Client-Server API Spec on pushers</a>.</p>
  15018. <h2 id="controlling-whether-a-user-is-shadow-banned"><a class="header" href="#controlling-whether-a-user-is-shadow-banned">Controlling whether a user is shadow-banned</a></h2>
  15019. <p>Shadow-banning is a useful tool for moderating malicious or egregiously abusive users.
  15020. A shadow-banned users receives successful responses to their client-server API requests,
  15021. but the events are not propagated into rooms. This can be an effective tool as it
  15022. (hopefully) takes longer for the user to realise they are being moderated before
  15023. pivoting to another account.</p>
  15024. <p>Shadow-banning a user should be used as a tool of last resort and may lead to confusing
  15025. or broken behaviour for the client. A shadow-banned user will not receive any
  15026. notification and it is generally more appropriate to ban or kick abusive users.
  15027. A shadow-banned user will be unable to contact anyone on the server.</p>
  15028. <p>To shadow-ban a user the API is:</p>
  15029. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/shadow_ban
  15030. </code></pre>
  15031. <p>To un-shadow-ban a user the API is:</p>
  15032. <pre><code>DELETE /_synapse/admin/v1/users/&lt;user_id&gt;/shadow_ban
  15033. </code></pre>
  15034. <p>An empty JSON dict is returned in both cases.</p>
  15035. <p><strong>Parameters</strong></p>
  15036. <p>The following parameters should be set in the URL:</p>
  15037. <ul>
  15038. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  15039. be local.</li>
  15040. </ul>
  15041. <h2 id="override-ratelimiting-for-users"><a class="header" href="#override-ratelimiting-for-users">Override ratelimiting for users</a></h2>
  15042. <p>This API allows to override or disable ratelimiting for a specific user.
  15043. There are specific APIs to set, get and delete a ratelimit.</p>
  15044. <h3 id="get-status-of-ratelimit"><a class="header" href="#get-status-of-ratelimit">Get status of ratelimit</a></h3>
  15045. <p>The API is:</p>
  15046. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  15047. </code></pre>
  15048. <p>A response body like the following is returned:</p>
  15049. <pre><code class="language-json">{
  15050. &quot;messages_per_second&quot;: 0,
  15051. &quot;burst_count&quot;: 0
  15052. }
  15053. </code></pre>
  15054. <p><strong>Parameters</strong></p>
  15055. <p>The following parameters should be set in the URL:</p>
  15056. <ul>
  15057. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  15058. be local.</li>
  15059. </ul>
  15060. <p><strong>Response</strong></p>
  15061. <p>The following fields are returned in the JSON response body:</p>
  15062. <ul>
  15063. <li><code>messages_per_second</code> - integer - The number of actions that can
  15064. be performed in a second. <code>0</code> mean that ratelimiting is disabled for this user.</li>
  15065. <li><code>burst_count</code> - integer - How many actions that can be performed before
  15066. being limited.</li>
  15067. </ul>
  15068. <p>If <strong>no</strong> custom ratelimit is set, an empty JSON dict is returned.</p>
  15069. <pre><code class="language-json">{}
  15070. </code></pre>
  15071. <h3 id="set-ratelimit"><a class="header" href="#set-ratelimit">Set ratelimit</a></h3>
  15072. <p>The API is:</p>
  15073. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  15074. </code></pre>
  15075. <p>A response body like the following is returned:</p>
  15076. <pre><code class="language-json">{
  15077. &quot;messages_per_second&quot;: 0,
  15078. &quot;burst_count&quot;: 0
  15079. }
  15080. </code></pre>
  15081. <p><strong>Parameters</strong></p>
  15082. <p>The following parameters should be set in the URL:</p>
  15083. <ul>
  15084. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  15085. be local.</li>
  15086. </ul>
  15087. <p>Body parameters:</p>
  15088. <ul>
  15089. <li><code>messages_per_second</code> - positive integer, optional. The number of actions that can
  15090. be performed in a second. Defaults to <code>0</code>.</li>
  15091. <li><code>burst_count</code> - positive integer, optional. How many actions that can be performed
  15092. before being limited. Defaults to <code>0</code>.</li>
  15093. </ul>
  15094. <p>To disable users' ratelimit set both values to <code>0</code>.</p>
  15095. <p><strong>Response</strong></p>
  15096. <p>The following fields are returned in the JSON response body:</p>
  15097. <ul>
  15098. <li><code>messages_per_second</code> - integer - The number of actions that can
  15099. be performed in a second.</li>
  15100. <li><code>burst_count</code> - integer - How many actions that can be performed before
  15101. being limited.</li>
  15102. </ul>
  15103. <h3 id="delete-ratelimit"><a class="header" href="#delete-ratelimit">Delete ratelimit</a></h3>
  15104. <p>The API is:</p>
  15105. <pre><code>DELETE /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  15106. </code></pre>
  15107. <p>An empty JSON dict is returned.</p>
  15108. <pre><code class="language-json">{}
  15109. </code></pre>
  15110. <p><strong>Parameters</strong></p>
  15111. <p>The following parameters should be set in the URL:</p>
  15112. <ul>
  15113. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  15114. be local.</li>
  15115. </ul>
  15116. <h3 id="check-username-availability"><a class="header" href="#check-username-availability">Check username availability</a></h3>
  15117. <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
  15118. API</a>
  15119. for more information.</p>
  15120. <p>This endpoint will work even if registration is disabled on the server, unlike
  15121. <code>/_matrix/client/r0/register/available</code>.</p>
  15122. <p>The API is:</p>
  15123. <pre><code>GET /_synapse/admin/v1/username_available?username=$localpart
  15124. </code></pre>
  15125. <p>The request and response format is the same as the
  15126. <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>
  15127. <div style="break-before: page; page-break-before: always;"></div><h1 id="version-api"><a class="header" href="#version-api">Version API</a></h1>
  15128. <p>This API returns the running Synapse version and the Python version
  15129. on which Synapse is being run. This is useful when a Synapse instance
  15130. is behind a proxy that does not forward the 'Server' header (which also
  15131. contains Synapse version information).</p>
  15132. <p>The api is:</p>
  15133. <pre><code>GET /_synapse/admin/v1/server_version
  15134. </code></pre>
  15135. <p>It returns a JSON body like the following:</p>
  15136. <pre><code class="language-json">{
  15137. &quot;server_version&quot;: &quot;0.99.2rc1 (b=develop, abcdef123)&quot;,
  15138. &quot;python_version&quot;: &quot;3.7.8&quot;
  15139. }
  15140. </code></pre>
  15141. <div style="break-before: page; page-break-before: always;"></div><h1 id="federation-api"><a class="header" href="#federation-api">Federation API</a></h1>
  15142. <p>This API allows a server administrator to manage Synapse's federation with other homeservers.</p>
  15143. <p>Note: This API is new, experimental and &quot;subject to change&quot;.</p>
  15144. <h2 id="list-of-destinations"><a class="header" href="#list-of-destinations">List of destinations</a></h2>
  15145. <p>This API gets the current destination retry timing info for all remote servers.</p>
  15146. <p>The list contains all the servers with which the server federates,
  15147. regardless of whether an error occurred or not.
  15148. If an error occurs, it may take up to 20 minutes for the error to be displayed here,
  15149. as a complete retry must have failed.</p>
  15150. <p>The API is:</p>
  15151. <p>A standard request with no filtering:</p>
  15152. <pre><code>GET /_synapse/admin/v1/federation/destinations
  15153. </code></pre>
  15154. <p>A response body like the following is returned:</p>
  15155. <pre><code class="language-json">{
  15156. &quot;destinations&quot;:[
  15157. {
  15158. &quot;destination&quot;: &quot;matrix.org&quot;,
  15159. &quot;retry_last_ts&quot;: 1557332397936,
  15160. &quot;retry_interval&quot;: 3000000,
  15161. &quot;failure_ts&quot;: 1557329397936,
  15162. &quot;last_successful_stream_ordering&quot;: null
  15163. }
  15164. ],
  15165. &quot;total&quot;: 1
  15166. }
  15167. </code></pre>
  15168. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again
  15169. with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  15170. <p>If the endpoint does not return a <code>next_token</code> then there are no more destinations
  15171. to paginate through.</p>
  15172. <p><strong>Parameters</strong></p>
  15173. <p>The following query parameters are available:</p>
  15174. <ul>
  15175. <li><code>from</code> - Offset in the returned list. Defaults to <code>0</code>.</li>
  15176. <li><code>limit</code> - Maximum amount of destinations to return. Defaults to <code>100</code>.</li>
  15177. <li><code>order_by</code> - The method in which to sort the returned list of destinations.
  15178. Valid values are:
  15179. <ul>
  15180. <li><code>destination</code> - Destinations are ordered alphabetically by remote server name.
  15181. This is the default.</li>
  15182. <li><code>retry_last_ts</code> - Destinations are ordered by time of last retry attempt in ms.</li>
  15183. <li><code>retry_interval</code> - Destinations are ordered by how long until next retry in ms.</li>
  15184. <li><code>failure_ts</code> - Destinations are ordered by when the server started failing in ms.</li>
  15185. <li><code>last_successful_stream_ordering</code> - Destinations are ordered by the stream ordering
  15186. of the most recent successfully-sent PDU.</li>
  15187. </ul>
  15188. </li>
  15189. <li><code>dir</code> - Direction of room order. Either <code>f</code> for forwards or <code>b</code> for backwards. Setting
  15190. this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</li>
  15191. </ul>
  15192. <p><em>Caution:</em> The database only has an index on the column <code>destination</code>.
  15193. This means that if a different sort order is used,
  15194. this can cause a large load on the database, especially for large environments.</p>
  15195. <p><strong>Response</strong></p>
  15196. <p>The following fields are returned in the JSON response body:</p>
  15197. <ul>
  15198. <li><code>destinations</code> - An array of objects, each containing information about a destination.
  15199. Destination objects contain the following fields:
  15200. <ul>
  15201. <li><code>destination</code> - string - Name of the remote server to federate.</li>
  15202. <li><code>retry_last_ts</code> - integer - The last time Synapse tried and failed to reach the
  15203. remote server, in ms. This is <code>0</code> if the last attempt to communicate with the
  15204. remote server was successful.</li>
  15205. <li><code>retry_interval</code> - integer - How long since the last time Synapse tried to reach
  15206. the remote server before trying again, in ms. This is <code>0</code> if no further retrying occuring.</li>
  15207. <li><code>failure_ts</code> - nullable integer - The first time Synapse tried and failed to reach the
  15208. remote server, in ms. This is <code>null</code> if communication with the remote server has never failed.</li>
  15209. <li><code>last_successful_stream_ordering</code> - nullable integer - The stream ordering of the most
  15210. recent successfully-sent <a href="usage/administration/admin_api/understanding_synapse_through_grafana_graphs.html#federation">PDU</a>
  15211. to this destination, or <code>null</code> if this information has not been tracked yet.</li>
  15212. </ul>
  15213. </li>
  15214. <li><code>next_token</code>: string representing a positive integer - Indication for pagination. See above.</li>
  15215. <li><code>total</code> - integer - Total number of destinations.</li>
  15216. </ul>
  15217. <h2 id="destination-details-api"><a class="header" href="#destination-details-api">Destination Details API</a></h2>
  15218. <p>This API gets the retry timing info for a specific remote server.</p>
  15219. <p>The API is:</p>
  15220. <pre><code>GET /_synapse/admin/v1/federation/destinations/&lt;destination&gt;
  15221. </code></pre>
  15222. <p>A response body like the following is returned:</p>
  15223. <pre><code class="language-json">{
  15224. &quot;destination&quot;: &quot;matrix.org&quot;,
  15225. &quot;retry_last_ts&quot;: 1557332397936,
  15226. &quot;retry_interval&quot;: 3000000,
  15227. &quot;failure_ts&quot;: 1557329397936,
  15228. &quot;last_successful_stream_ordering&quot;: null
  15229. }
  15230. </code></pre>
  15231. <p><strong>Parameters</strong></p>
  15232. <p>The following parameters should be set in the URL:</p>
  15233. <ul>
  15234. <li><code>destination</code> - Name of the remote server.</li>
  15235. </ul>
  15236. <p><strong>Response</strong></p>
  15237. <p>The response fields are the same like in the <code>destinations</code> array in
  15238. <a href="usage/administration/admin_api/federation.html#list-of-destinations">List of destinations</a> response.</p>
  15239. <h2 id="destination-rooms"><a class="header" href="#destination-rooms">Destination rooms</a></h2>
  15240. <p>This API gets the rooms that federate with a specific remote server.</p>
  15241. <p>The API is:</p>
  15242. <pre><code>GET /_synapse/admin/v1/federation/destinations/&lt;destination&gt;/rooms
  15243. </code></pre>
  15244. <p>A response body like the following is returned:</p>
  15245. <pre><code class="language-json">{
  15246. &quot;rooms&quot;:[
  15247. {
  15248. &quot;room_id&quot;: &quot;!OGEhHVWSdvArJzumhm:matrix.org&quot;,
  15249. &quot;stream_ordering&quot;: 8326
  15250. },
  15251. {
  15252. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  15253. &quot;stream_ordering&quot;: 93534
  15254. }
  15255. ],
  15256. &quot;total&quot;: 2
  15257. }
  15258. </code></pre>
  15259. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again
  15260. with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  15261. <p>If the endpoint does not return a <code>next_token</code> then there are no more destinations
  15262. to paginate through.</p>
  15263. <p><strong>Parameters</strong></p>
  15264. <p>The following parameters should be set in the URL:</p>
  15265. <ul>
  15266. <li><code>destination</code> - Name of the remote server.</li>
  15267. </ul>
  15268. <p>The following query parameters are available:</p>
  15269. <ul>
  15270. <li><code>from</code> - Offset in the returned list. Defaults to <code>0</code>.</li>
  15271. <li><code>limit</code> - Maximum amount of destinations to return. Defaults to <code>100</code>.</li>
  15272. <li><code>dir</code> - Direction of room order by <code>room_id</code>. Either <code>f</code> for forwards or <code>b</code> for
  15273. backwards. Defaults to <code>f</code>.</li>
  15274. </ul>
  15275. <p><strong>Response</strong></p>
  15276. <p>The following fields are returned in the JSON response body:</p>
  15277. <ul>
  15278. <li><code>rooms</code> - An array of objects, each containing information about a room.
  15279. Room objects contain the following fields:
  15280. <ul>
  15281. <li><code>room_id</code> - string - The ID of the room.</li>
  15282. <li><code>stream_ordering</code> - integer - The stream ordering of the most recent
  15283. successfully-sent <a href="usage/administration/admin_api/understanding_synapse_through_grafana_graphs.html#federation">PDU</a>
  15284. to this destination in this room.</li>
  15285. </ul>
  15286. </li>
  15287. <li><code>next_token</code>: string representing a positive integer - Indication for pagination. See above.</li>
  15288. <li><code>total</code> - integer - Total number of destinations.</li>
  15289. </ul>
  15290. <h2 id="reset-connection-timeout"><a class="header" href="#reset-connection-timeout">Reset connection timeout</a></h2>
  15291. <p>Synapse makes federation requests to other homeservers. If a federation request fails,
  15292. Synapse will mark the destination homeserver as offline, preventing any future requests
  15293. to that server for a &quot;cooldown&quot; period. This period grows over time if the server
  15294. continues to fail its responses
  15295. (<a href="https://en.wikipedia.org/wiki/Exponential_backoff">exponential backoff</a>).</p>
  15296. <p>Admins can cancel the cooldown period with this API.</p>
  15297. <p>This API resets the retry timing for a specific remote server and tries to connect to
  15298. the remote server again. It does not wait for the next <code>retry_interval</code>.
  15299. The connection must have previously run into an error and <code>retry_last_ts</code>
  15300. (<a href="usage/administration/admin_api/federation.html#destination-details-api">Destination Details API</a>) must not be equal to <code>0</code>.</p>
  15301. <p>The connection attempt is carried out in the background and can take a while
  15302. even if the API already returns the http status 200.</p>
  15303. <p>The API is:</p>
  15304. <pre><code>POST /_synapse/admin/v1/federation/destinations/&lt;destination&gt;/reset_connection
  15305. {}
  15306. </code></pre>
  15307. <p><strong>Parameters</strong></p>
  15308. <p>The following parameters should be set in the URL:</p>
  15309. <ul>
  15310. <li><code>destination</code> - Name of the remote server.</li>
  15311. </ul>
  15312. <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>
  15313. <p>The &quot;manhole&quot; allows server administrators to access a Python shell on a running
  15314. Synapse installation. This is a very powerful mechanism for administration and
  15315. debugging.</p>
  15316. <p><strong><em>Security Warning</em></strong></p>
  15317. <p>Note that this will give administrative access to synapse to <strong>all users</strong> with
  15318. shell access to the server. It should therefore <strong>not</strong> be enabled in
  15319. environments where untrusted users have shell access.</p>
  15320. <h2 id="configuring-the-manhole"><a class="header" href="#configuring-the-manhole">Configuring the manhole</a></h2>
  15321. <p>To enable it, first uncomment the <code>manhole</code> listener configuration in
  15322. <code>homeserver.yaml</code>. The configuration is slightly different if you're using docker.</p>
  15323. <h4 id="docker-config"><a class="header" href="#docker-config">Docker config</a></h4>
  15324. <p>If you are using Docker, set <code>bind_addresses</code> to <code>['0.0.0.0']</code> as shown:</p>
  15325. <pre><code class="language-yaml">listeners:
  15326. - port: 9000
  15327. bind_addresses: ['0.0.0.0']
  15328. type: manhole
  15329. </code></pre>
  15330. <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
  15331. <code>manhole</code> port forwarding. The <code>-p 127.0.0.1:9000:9000</code> below is important: it
  15332. ensures that access to the <code>manhole</code> is only possible for local users.</p>
  15333. <pre><code class="language-bash">docker run -d --name synapse \
  15334. --mount type=volume,src=synapse-data,dst=/data \
  15335. -p 8008:8008 \
  15336. -p 127.0.0.1:9000:9000 \
  15337. matrixdotorg/synapse:latest
  15338. </code></pre>
  15339. <h4 id="native-config"><a class="header" href="#native-config">Native config</a></h4>
  15340. <p>If you are not using docker, set <code>bind_addresses</code> to <code>['::1', '127.0.0.1']</code> as shown.
  15341. The <code>bind_addresses</code> in the example below is important: it ensures that access to the
  15342. <code>manhole</code> is only possible for local users).</p>
  15343. <pre><code class="language-yaml">listeners:
  15344. - port: 9000
  15345. bind_addresses: ['::1', '127.0.0.1']
  15346. type: manhole
  15347. </code></pre>
  15348. <h3 id="security-settings"><a class="header" href="#security-settings">Security settings</a></h3>
  15349. <p>The following config options are available:</p>
  15350. <ul>
  15351. <li><code>username</code> - The username for the manhole (defaults to <code>matrix</code>)</li>
  15352. <li><code>password</code> - The password for the manhole (defaults to <code>rabbithole</code>)</li>
  15353. <li><code>ssh_priv_key</code> - The path to a private SSH key (defaults to a hardcoded value)</li>
  15354. <li><code>ssh_pub_key</code> - The path to a public SSH key (defaults to a hardcoded value)</li>
  15355. </ul>
  15356. <p>For example:</p>
  15357. <pre><code class="language-yaml">manhole_settings:
  15358. username: manhole
  15359. password: mypassword
  15360. ssh_priv_key: &quot;/home/synapse/manhole_keys/id_rsa&quot;
  15361. ssh_pub_key: &quot;/home/synapse/manhole_keys/id_rsa.pub&quot;
  15362. </code></pre>
  15363. <h2 id="accessing-synapse-manhole"><a class="header" href="#accessing-synapse-manhole">Accessing synapse manhole</a></h2>
  15364. <p>Then restart synapse, and point an ssh client at port 9000 on localhost, using
  15365. the username and password configured in <code>homeserver.yaml</code> - with the default
  15366. configuration, this would be:</p>
  15367. <pre><code class="language-bash">ssh -p9000 matrix@localhost
  15368. </code></pre>
  15369. <p>Then enter the password when prompted (the default is <code>rabbithole</code>).</p>
  15370. <p>This gives a Python REPL in which <code>hs</code> gives access to the
  15371. <code>synapse.server.HomeServer</code> object - which in turn gives access to many other
  15372. parts of the process.</p>
  15373. <p>Note that, prior to Synapse 1.41, any call which returns a coroutine will need to be wrapped in <code>ensureDeferred</code>.</p>
  15374. <p>As a simple example, retrieving an event from the database:</p>
  15375. <pre><code class="language-pycon">&gt;&gt;&gt; from twisted.internet import defer
  15376. &gt;&gt;&gt; defer.ensureDeferred(hs.get_datastores().main.get_event('$1416420717069yeQaw:matrix.org'))
  15377. &lt;Deferred at 0x7ff253fc6998 current result: &lt;FrozenEvent event_id='$1416420717069yeQaw:matrix.org', type='m.room.create', state_key=''&gt;&gt;
  15378. </code></pre>
  15379. <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>
  15380. <ol>
  15381. <li>
  15382. <p>Install Prometheus:</p>
  15383. <p>Follow instructions at
  15384. <a href="http://prometheus.io/docs/introduction/install/">http://prometheus.io/docs/introduction/install/</a></p>
  15385. </li>
  15386. <li>
  15387. <p>Enable Synapse metrics:</p>
  15388. <p>There are two methods of enabling metrics in Synapse.</p>
  15389. <p>The first serves the metrics as a part of the usual web server and
  15390. can be enabled by adding the &quot;metrics&quot; resource to the existing
  15391. listener as such:</p>
  15392. <pre><code class="language-yaml"> resources:
  15393. - names:
  15394. - client
  15395. - metrics
  15396. </code></pre>
  15397. <p>This provides a simple way of adding metrics to your Synapse
  15398. installation, and serves under <code>/_synapse/metrics</code>. If you do not
  15399. wish your metrics be publicly exposed, you will need to either
  15400. filter it out at your load balancer, or use the second method.</p>
  15401. <p>The second method runs the metrics server on a different port, in a
  15402. different thread to Synapse. This can make it more resilient to
  15403. heavy load meaning metrics cannot be retrieved, and can be exposed
  15404. to just internal networks easier. The served metrics are available
  15405. over HTTP only, and will be available at <code>/_synapse/metrics</code>.</p>
  15406. <p>Add a new listener to homeserver.yaml:</p>
  15407. <pre><code class="language-yaml"> listeners:
  15408. - type: metrics
  15409. port: 9000
  15410. bind_addresses:
  15411. - '0.0.0.0'
  15412. </code></pre>
  15413. <p>For both options, you will need to ensure that <code>enable_metrics</code> is
  15414. set to <code>True</code>.</p>
  15415. </li>
  15416. <li>
  15417. <p>Restart Synapse.</p>
  15418. </li>
  15419. <li>
  15420. <p>Add a Prometheus target for Synapse.</p>
  15421. <p>It needs to set the <code>metrics_path</code> to a non-default value (under
  15422. <code>scrape_configs</code>):</p>
  15423. <pre><code class="language-yaml"> - job_name: &quot;synapse&quot;
  15424. scrape_interval: 15s
  15425. metrics_path: &quot;/_synapse/metrics&quot;
  15426. static_configs:
  15427. - targets: [&quot;my.server.here:port&quot;]
  15428. </code></pre>
  15429. <p>where <code>my.server.here</code> is the IP address of Synapse, and <code>port</code> is
  15430. the listener port configured with the <code>metrics</code> resource.</p>
  15431. <p>If your prometheus is older than 1.5.2, you will need to replace
  15432. <code>static_configs</code> in the above with <code>target_groups</code>.</p>
  15433. </li>
  15434. <li>
  15435. <p>Restart Prometheus.</p>
  15436. </li>
  15437. <li>
  15438. <p>Consider using the <a href="https://github.com/matrix-org/synapse/tree/master/contrib/grafana/">grafana dashboard</a>
  15439. and required <a href="https://github.com/matrix-org/synapse/tree/master/contrib/prometheus/">recording rules</a> </p>
  15440. </li>
  15441. </ol>
  15442. <h2 id="monitoring-workers"><a class="header" href="#monitoring-workers">Monitoring workers</a></h2>
  15443. <p>To monitor a Synapse installation using <a href="workers.html">workers</a>,
  15444. every worker needs to be monitored independently, in addition to
  15445. the main homeserver process. This is because workers don't send
  15446. their metrics to the main homeserver process, but expose them
  15447. directly (if they are configured to do so).</p>
  15448. <p>To allow collecting metrics from a worker, you need to add a
  15449. <code>metrics</code> listener to its configuration, by adding the following
  15450. under <code>worker_listeners</code>:</p>
  15451. <pre><code class="language-yaml"> - type: metrics
  15452. bind_address: ''
  15453. port: 9101
  15454. </code></pre>
  15455. <p>The <code>bind_address</code> and <code>port</code> parameters should be set so that
  15456. the resulting listener can be reached by prometheus, and they
  15457. don't clash with an existing worker.
  15458. With this example, the worker's metrics would then be available
  15459. on <code>http://127.0.0.1:9101</code>.</p>
  15460. <p>Example Prometheus target for Synapse with workers:</p>
  15461. <pre><code class="language-yaml"> - job_name: &quot;synapse&quot;
  15462. scrape_interval: 15s
  15463. metrics_path: &quot;/_synapse/metrics&quot;
  15464. static_configs:
  15465. - targets: [&quot;my.server.here:port&quot;]
  15466. labels:
  15467. instance: &quot;my.server&quot;
  15468. job: &quot;master&quot;
  15469. index: 1
  15470. - targets: [&quot;my.workerserver.here:port&quot;]
  15471. labels:
  15472. instance: &quot;my.server&quot;
  15473. job: &quot;generic_worker&quot;
  15474. index: 1
  15475. - targets: [&quot;my.workerserver.here:port&quot;]
  15476. labels:
  15477. instance: &quot;my.server&quot;
  15478. job: &quot;generic_worker&quot;
  15479. index: 2
  15480. - targets: [&quot;my.workerserver.here:port&quot;]
  15481. labels:
  15482. instance: &quot;my.server&quot;
  15483. job: &quot;media_repository&quot;
  15484. index: 1
  15485. </code></pre>
  15486. <p>Labels (<code>instance</code>, <code>job</code>, <code>index</code>) can be defined as anything.
  15487. The labels are used to group graphs in grafana.</p>
  15488. <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>
  15489. <p>Synapse 1.2 updates the Prometheus metrics to match the naming
  15490. convention of the upstream <code>prometheus_client</code>. The old names are
  15491. considered deprecated and will be removed in a future version of
  15492. Synapse.</p>
  15493. <table><thead><tr><th>New Name</th><th>Old Name</th></tr></thead><tbody>
  15494. <tr><td>python_gc_objects_collected_total</td><td>python_gc_objects_collected</td></tr>
  15495. <tr><td>python_gc_objects_uncollectable_total</td><td>python_gc_objects_uncollectable</td></tr>
  15496. <tr><td>python_gc_collections_total</td><td>python_gc_collections</td></tr>
  15497. <tr><td>process_cpu_seconds_total</td><td>process_cpu_seconds</td></tr>
  15498. <tr><td>synapse_federation_client_sent_transactions_total</td><td>synapse_federation_client_sent_transactions</td></tr>
  15499. <tr><td>synapse_federation_client_events_processed_total</td><td>synapse_federation_client_events_processed</td></tr>
  15500. <tr><td>synapse_event_processing_loop_count_total</td><td>synapse_event_processing_loop_count</td></tr>
  15501. <tr><td>synapse_event_processing_loop_room_count_total</td><td>synapse_event_processing_loop_room_count</td></tr>
  15502. <tr><td>synapse_util_metrics_block_count_total</td><td>synapse_util_metrics_block_count</td></tr>
  15503. <tr><td>synapse_util_metrics_block_time_seconds_total</td><td>synapse_util_metrics_block_time_seconds</td></tr>
  15504. <tr><td>synapse_util_metrics_block_ru_utime_seconds_total</td><td>synapse_util_metrics_block_ru_utime_seconds</td></tr>
  15505. <tr><td>synapse_util_metrics_block_ru_stime_seconds_total</td><td>synapse_util_metrics_block_ru_stime_seconds</td></tr>
  15506. <tr><td>synapse_util_metrics_block_db_txn_count_total</td><td>synapse_util_metrics_block_db_txn_count</td></tr>
  15507. <tr><td>synapse_util_metrics_block_db_txn_duration_seconds_total</td><td>synapse_util_metrics_block_db_txn_duration_seconds</td></tr>
  15508. <tr><td>synapse_util_metrics_block_db_sched_duration_seconds_total</td><td>synapse_util_metrics_block_db_sched_duration_seconds</td></tr>
  15509. <tr><td>synapse_background_process_start_count_total</td><td>synapse_background_process_start_count</td></tr>
  15510. <tr><td>synapse_background_process_ru_utime_seconds_total</td><td>synapse_background_process_ru_utime_seconds</td></tr>
  15511. <tr><td>synapse_background_process_ru_stime_seconds_total</td><td>synapse_background_process_ru_stime_seconds</td></tr>
  15512. <tr><td>synapse_background_process_db_txn_count_total</td><td>synapse_background_process_db_txn_count</td></tr>
  15513. <tr><td>synapse_background_process_db_txn_duration_seconds_total</td><td>synapse_background_process_db_txn_duration_seconds</td></tr>
  15514. <tr><td>synapse_background_process_db_sched_duration_seconds_total</td><td>synapse_background_process_db_sched_duration_seconds</td></tr>
  15515. <tr><td>synapse_storage_events_persisted_events_total</td><td>synapse_storage_events_persisted_events</td></tr>
  15516. <tr><td>synapse_storage_events_persisted_events_sep_total</td><td>synapse_storage_events_persisted_events_sep</td></tr>
  15517. <tr><td>synapse_storage_events_state_delta_total</td><td>synapse_storage_events_state_delta</td></tr>
  15518. <tr><td>synapse_storage_events_state_delta_single_event_total</td><td>synapse_storage_events_state_delta_single_event</td></tr>
  15519. <tr><td>synapse_storage_events_state_delta_reuse_delta_total</td><td>synapse_storage_events_state_delta_reuse_delta</td></tr>
  15520. <tr><td>synapse_federation_server_received_pdus_total</td><td>synapse_federation_server_received_pdus</td></tr>
  15521. <tr><td>synapse_federation_server_received_edus_total</td><td>synapse_federation_server_received_edus</td></tr>
  15522. <tr><td>synapse_handler_presence_notified_presence_total</td><td>synapse_handler_presence_notified_presence</td></tr>
  15523. <tr><td>synapse_handler_presence_federation_presence_out_total</td><td>synapse_handler_presence_federation_presence_out</td></tr>
  15524. <tr><td>synapse_handler_presence_presence_updates_total</td><td>synapse_handler_presence_presence_updates</td></tr>
  15525. <tr><td>synapse_handler_presence_timers_fired_total</td><td>synapse_handler_presence_timers_fired</td></tr>
  15526. <tr><td>synapse_handler_presence_federation_presence_total</td><td>synapse_handler_presence_federation_presence</td></tr>
  15527. <tr><td>synapse_handler_presence_bump_active_time_total</td><td>synapse_handler_presence_bump_active_time</td></tr>
  15528. <tr><td>synapse_federation_client_sent_edus_total</td><td>synapse_federation_client_sent_edus</td></tr>
  15529. <tr><td>synapse_federation_client_sent_pdu_destinations_count_total</td><td>synapse_federation_client_sent_pdu_destinations:count</td></tr>
  15530. <tr><td>synapse_federation_client_sent_pdu_destinations_total</td><td>synapse_federation_client_sent_pdu_destinations:total</td></tr>
  15531. <tr><td>synapse_handlers_appservice_events_processed_total</td><td>synapse_handlers_appservice_events_processed</td></tr>
  15532. <tr><td>synapse_notifier_notified_events_total</td><td>synapse_notifier_notified_events</td></tr>
  15533. <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>
  15534. <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>
  15535. <tr><td>synapse_http_httppusher_http_pushes_processed_total</td><td>synapse_http_httppusher_http_pushes_processed</td></tr>
  15536. <tr><td>synapse_http_httppusher_http_pushes_failed_total</td><td>synapse_http_httppusher_http_pushes_failed</td></tr>
  15537. <tr><td>synapse_http_httppusher_badge_updates_processed_total</td><td>synapse_http_httppusher_badge_updates_processed</td></tr>
  15538. <tr><td>synapse_http_httppusher_badge_updates_failed_total</td><td>synapse_http_httppusher_badge_updates_failed</td></tr>
  15539. </tbody></table>
  15540. <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>
  15541. <p>The duplicated metrics deprecated in Synapse 0.27.0 have been removed.</p>
  15542. <p>All time duration-based metrics have been changed to be seconds. This
  15543. affects:</p>
  15544. <table><thead><tr><th>msec -&gt; sec metrics</th></tr></thead><tbody>
  15545. <tr><td>python_gc_time</td></tr>
  15546. <tr><td>python_twisted_reactor_tick_time</td></tr>
  15547. <tr><td>synapse_storage_query_time</td></tr>
  15548. <tr><td>synapse_storage_schedule_time</td></tr>
  15549. <tr><td>synapse_storage_transaction_time</td></tr>
  15550. </tbody></table>
  15551. <p>Several metrics have been changed to be histograms, which sort entries
  15552. into buckets and allow better analysis. The following metrics are now
  15553. histograms:</p>
  15554. <table><thead><tr><th>Altered metrics</th></tr></thead><tbody>
  15555. <tr><td>python_gc_time</td></tr>
  15556. <tr><td>python_twisted_reactor_pending_calls</td></tr>
  15557. <tr><td>python_twisted_reactor_tick_time</td></tr>
  15558. <tr><td>synapse_http_server_response_time_seconds</td></tr>
  15559. <tr><td>synapse_storage_query_time</td></tr>
  15560. <tr><td>synapse_storage_schedule_time</td></tr>
  15561. <tr><td>synapse_storage_transaction_time</td></tr>
  15562. </tbody></table>
  15563. <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>
  15564. <p>Synapse 0.27.0 begins the process of rationalising the duplicate
  15565. <code>*:count</code> metrics reported for the resource tracking for code blocks and
  15566. HTTP requests.</p>
  15567. <p>At the same time, the corresponding <code>*:total</code> metrics are being renamed,
  15568. as the <code>:total</code> suffix no longer makes sense in the absence of a
  15569. corresponding <code>:count</code> metric.</p>
  15570. <p>To enable a graceful migration path, this release just adds new names
  15571. for the metrics being renamed. A future release will remove the old
  15572. ones.</p>
  15573. <p>The following table shows the new metrics, and the old metrics which
  15574. they are replacing.</p>
  15575. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  15576. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_timer:count</td></tr>
  15577. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_ru_utime:count</td></tr>
  15578. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_ru_stime:count</td></tr>
  15579. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_db_txn_count:count</td></tr>
  15580. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_db_txn_duration:count</td></tr>
  15581. <tr><td>synapse_util_metrics_block_time_seconds</td><td>synapse_util_metrics_block_timer:total</td></tr>
  15582. <tr><td>synapse_util_metrics_block_ru_utime_seconds</td><td>synapse_util_metrics_block_ru_utime:total</td></tr>
  15583. <tr><td>synapse_util_metrics_block_ru_stime_seconds</td><td>synapse_util_metrics_block_ru_stime:total</td></tr>
  15584. <tr><td>synapse_util_metrics_block_db_txn_count</td><td>synapse_util_metrics_block_db_txn_count:total</td></tr>
  15585. <tr><td>synapse_util_metrics_block_db_txn_duration_seconds</td><td>synapse_util_metrics_block_db_txn_duration:total</td></tr>
  15586. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_requests</td></tr>
  15587. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_time:count</td></tr>
  15588. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_ru_utime:count</td></tr>
  15589. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_ru_stime:count</td></tr>
  15590. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_db_txn_count:count</td></tr>
  15591. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_db_txn_duration:count</td></tr>
  15592. <tr><td>synapse_http_server_response_time_seconds</td><td>synapse_http_server_response_time:total</td></tr>
  15593. <tr><td>synapse_http_server_response_ru_utime_seconds</td><td>synapse_http_server_response_ru_utime:total</td></tr>
  15594. <tr><td>synapse_http_server_response_ru_stime_seconds</td><td>synapse_http_server_response_ru_stime:total</td></tr>
  15595. <tr><td>synapse_http_server_response_db_txn_count</td><td>synapse_http_server_response_db_txn_count:total</td></tr>
  15596. <tr><td>synapse_http_server_response_db_txn_duration_seconds</td><td>synapse_http_server_response_db_txn_duration:total</td></tr>
  15597. </tbody></table>
  15598. <h2 id="standard-metric-names"><a class="header" href="#standard-metric-names">Standard Metric Names</a></h2>
  15599. <p>As of synapse version 0.18.2, the format of the process-wide metrics has
  15600. been changed to fit prometheus standard naming conventions. Additionally
  15601. the units have been changed to seconds, from miliseconds.</p>
  15602. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  15603. <tr><td>process_cpu_user_seconds_total</td><td>process_resource_utime / 1000</td></tr>
  15604. <tr><td>process_cpu_system_seconds_total</td><td>process_resource_stime / 1000</td></tr>
  15605. <tr><td>process_open_fds (no 'type' label)</td><td>process_fds</td></tr>
  15606. </tbody></table>
  15607. <p>The python-specific counts of garbage collector performance have been
  15608. renamed.</p>
  15609. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  15610. <tr><td>python_gc_time</td><td>reactor_gc_time</td></tr>
  15611. <tr><td>python_gc_unreachable_total</td><td>reactor_gc_unreachable</td></tr>
  15612. <tr><td>python_gc_counts</td><td>reactor_gc_counts</td></tr>
  15613. </tbody></table>
  15614. <p>The twisted-specific reactor metrics have been renamed.</p>
  15615. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  15616. <tr><td>python_twisted_reactor_pending_calls</td><td>reactor_pending_calls</td></tr>
  15617. <tr><td>python_twisted_reactor_tick_time</td><td>reactor_tick_time</td></tr>
  15618. </tbody></table>
  15619. <div style="break-before: page; page-break-before: always;"></div><h2 id="understanding-synapse-through-grafana-graphs"><a class="header" href="#understanding-synapse-through-grafana-graphs">Understanding Synapse through Grafana graphs</a></h2>
  15620. <p>It is possible to monitor much of the internal state of Synapse using <a href="https://prometheus.io">Prometheus</a>
  15621. metrics and <a href="https://grafana.com/">Grafana</a>.
  15622. A guide for configuring Synapse to provide metrics is available <a href="usage/administration/../../metrics-howto.html">here</a>
  15623. and information on setting up Grafana is <a href="https://github.com/matrix-org/synapse/tree/master/contrib/grafana">here</a>.
  15624. In this setup, Prometheus will periodically scrape the information Synapse provides and
  15625. store a record of it over time. Grafana is then used as an interface to query and
  15626. present this information through a series of pretty graphs.</p>
  15627. <p>Once you have grafana set up, and assuming you're using <a href="https://github.com/matrix-org/synapse/blob/master/contrib/grafana/synapse.json">our grafana dashboard template</a>, look for the following graphs when debugging a slow/overloaded Synapse:</p>
  15628. <h2 id="message-event-send-time"><a class="header" href="#message-event-send-time">Message Event Send Time</a></h2>
  15629. <p><img src="https://user-images.githubusercontent.com/1342360/82239409-a1c8e900-9930-11ea-8081-e4614e0c63f4.png" alt="image" /></p>
  15630. <p>This, along with the CPU and Memory graphs, is a good way to check the general health of your Synapse instance. It represents how long it takes for a user on your homeserver to send a message.</p>
  15631. <h2 id="transaction-count-and-transaction-duration"><a class="header" href="#transaction-count-and-transaction-duration">Transaction Count and Transaction Duration</a></h2>
  15632. <p><img src="https://user-images.githubusercontent.com/1342360/82239985-8d392080-9931-11ea-80d0-843ab2f22e1e.png" alt="image" /></p>
  15633. <p><img src="https://user-images.githubusercontent.com/1342360/82240050-ab068580-9931-11ea-98f1-f94671cbac9a.png" alt="image" /></p>
  15634. <p>These graphs show the database transactions that are occurring the most frequently, as well as those are that are taking the most amount of time to execute.</p>
  15635. <p><img src="https://user-images.githubusercontent.com/1342360/82240192-e86b1300-9931-11ea-9aac-3e2c9bfa6fdc.png" alt="image" /></p>
  15636. <p>In the first graph, we can see obvious spikes corresponding to lots of <code>get_user_by_id</code> transactions. This would be useful information to figure out which part of the Synapse codebase is potentially creating a heavy load on the system. However, be sure to cross-reference this with Transaction Duration, which states that <code>get_users_by_id</code> is actually a very quick database transaction and isn't causing as much load as others, like <code>persist_events</code>:</p>
  15637. <p><img src="https://user-images.githubusercontent.com/1342360/82240467-62030100-9932-11ea-8db9-917f2d977fe1.png" alt="image" /></p>
  15638. <p>Still, it's probably worth investigating why we're getting users from the database that often, and whether it's possible to reduce the amount of queries we make by adjusting our cache factor(s).</p>
  15639. <p>The <code>persist_events</code> transaction is responsible for saving new room events to the Synapse database, so can often show a high transaction duration.</p>
  15640. <h2 id="federation-1"><a class="header" href="#federation-1">Federation</a></h2>
  15641. <p>The charts in the &quot;Federation&quot; section show information about incoming and outgoing federation requests. Federation data can be divided into two basic types:</p>
  15642. <ul>
  15643. <li>PDU (Persistent Data Unit) - room events: messages, state events (join/leave), etc. These are permanently stored in the database.</li>
  15644. <li>EDU (Ephemeral Data Unit) - other data, which need not be stored permanently, such as read receipts, typing notifications.</li>
  15645. </ul>
  15646. <p>The &quot;Outgoing EDUs by type&quot; chart shows the EDUs within outgoing federation requests by type: <code>m.device_list_update</code>, <code>m.direct_to_device</code>, <code>m.presence</code>, <code>m.receipt</code>, <code>m.typing</code>.</p>
  15647. <p>If you see a large number of <code>m.presence</code> EDUs and are having trouble with too much CPU load, you can disable <code>presence</code> in the Synapse config. See also <a href="https://github.com/matrix-org/synapse/issues/3971">#3971</a>.</p>
  15648. <h2 id="caches"><a class="header" href="#caches">Caches</a></h2>
  15649. <p><img src="https://user-images.githubusercontent.com/1342360/82240572-8b239180-9932-11ea-96ff-6b5f0e57ebe5.png" alt="image" /></p>
  15650. <p><img src="https://user-images.githubusercontent.com/1342360/82240666-b8703f80-9932-11ea-86af-9f663988d8da.png" alt="image" /></p>
  15651. <p>This is quite a useful graph. It shows how many times Synapse attempts to retrieve a piece of data from a cache which the cache did not contain, thus resulting in a call to the database. We can see here that the <code>_get_joined_profile_from_event_id</code> cache is being requested a lot, and often the data we're after is not cached.</p>
  15652. <p>Cross-referencing this with the Eviction Rate graph, which shows that entries are being evicted from <code>_get_joined_profile_from_event_id</code> quite often:</p>
  15653. <p><img src="https://user-images.githubusercontent.com/1342360/82240766-de95df80-9932-11ea-8c15-5acfc57c48da.png" alt="image" /></p>
  15654. <p>we should probably consider raising the size of that cache by raising its cache factor (a multiplier value for the size of an individual cache). Information on doing so is available <a href="https://github.com/matrix-org/synapse/blob/ee421e524478c1ad8d43741c27379499c2f6135c/docs/sample_config.yaml#L608-L642">here</a> (note that the configuration of individual cache factors through the configuration file is available in Synapse v1.14.0+, whereas doing so through environment variables has been supported for a very long time). Note that this will increase Synapse's overall memory usage.</p>
  15655. <h2 id="forward-extremities"><a class="header" href="#forward-extremities">Forward Extremities</a></h2>
  15656. <p><img src="https://user-images.githubusercontent.com/1342360/82241440-13566680-9934-11ea-8b88-ba468db937ed.png" alt="image" /></p>
  15657. <p>Forward extremities are the leaf events at the end of a DAG in a room, aka events that have no children. The more that exist in a room, the more <a href="https://spec.matrix.org/v1.1/server-server-api/#room-state-resolution">state resolution</a> that Synapse needs to perform (hint: it's an expensive operation). While Synapse has code to prevent too many of these existing at one time in a room, bugs can sometimes make them crop up again.</p>
  15658. <p>If a room has &gt;10 forward extremities, it's worth checking which room is the culprit and potentially removing them using the SQL queries mentioned in <a href="https://github.com/matrix-org/synapse/issues/1760">#1760</a>.</p>
  15659. <h2 id="garbage-collection"><a class="header" href="#garbage-collection">Garbage Collection</a></h2>
  15660. <p><img src="https://user-images.githubusercontent.com/1342360/82241911-da6ac180-9934-11ea-9a0d-a311fe22acd0.png" alt="image" /></p>
  15661. <p>Large spikes in garbage collection times (bigger than shown here, I'm talking in the
  15662. multiple seconds range), can cause lots of problems in Synapse performance. It's more an
  15663. indicator of problems, and a symptom of other problems though, so check other graphs for what might be causing it.</p>
  15664. <h2 id="final-thoughts"><a class="header" href="#final-thoughts">Final Thoughts</a></h2>
  15665. <p>If you're still having performance problems with your Synapse instance and you've
  15666. tried everything you can, it may just be a lack of system resources. Consider adding
  15667. more CPU and RAM, and make use of <a href="usage/administration/../../workers.html">worker mode</a>
  15668. to make use of multiple CPU cores / multiple machines for your homeserver.</p>
  15669. <div style="break-before: page; page-break-before: always;"></div><h2 id="some-useful-sql-queries-for-synapse-admins"><a class="header" href="#some-useful-sql-queries-for-synapse-admins">Some useful SQL queries for Synapse Admins</a></h2>
  15670. <h2 id="size-of-full-matrix-db"><a class="header" href="#size-of-full-matrix-db">Size of full matrix db</a></h2>
  15671. <pre><code class="language-sql">SELECT pg_size_pretty( pg_database_size( 'matrix' ) );
  15672. </code></pre>
  15673. <h3 id="result-example"><a class="header" href="#result-example">Result example:</a></h3>
  15674. <pre><code>pg_size_pretty
  15675. ----------------
  15676. 6420 MB
  15677. (1 row)
  15678. </code></pre>
  15679. <h2 id="show-top-20-larger-tables-by-row-count"><a class="header" href="#show-top-20-larger-tables-by-row-count">Show top 20 larger tables by row count</a></h2>
  15680. <pre><code class="language-sql">SELECT relname, n_live_tup AS &quot;rows&quot;
  15681. FROM pg_stat_user_tables
  15682. ORDER BY n_live_tup DESC
  15683. LIMIT 20;
  15684. </code></pre>
  15685. <p>This query is quick, but may be very approximate, for exact number of rows use:</p>
  15686. <pre><code class="language-sql">SELECT COUNT(*) FROM &lt;table_name&gt;;
  15687. </code></pre>
  15688. <h3 id="result-example-1"><a class="header" href="#result-example-1">Result example:</a></h3>
  15689. <pre><code>state_groups_state - 161687170
  15690. event_auth - 8584785
  15691. event_edges - 6995633
  15692. event_json - 6585916
  15693. event_reference_hashes - 6580990
  15694. events - 6578879
  15695. received_transactions - 5713989
  15696. event_to_state_groups - 4873377
  15697. stream_ordering_to_exterm - 4136285
  15698. current_state_delta_stream - 3770972
  15699. event_search - 3670521
  15700. state_events - 2845082
  15701. room_memberships - 2785854
  15702. cache_invalidation_stream - 2448218
  15703. state_groups - 1255467
  15704. state_group_edges - 1229849
  15705. current_state_events - 1222905
  15706. users_in_public_rooms - 364059
  15707. device_lists_stream - 326903
  15708. user_directory_search - 316433
  15709. </code></pre>
  15710. <h2 id="show-top-20-larger-tables-by-storage-size"><a class="header" href="#show-top-20-larger-tables-by-storage-size">Show top 20 larger tables by storage size</a></h2>
  15711. <pre><code class="language-sql">SELECT nspname || '.' || relname AS &quot;relation&quot;,
  15712. pg_size_pretty(pg_total_relation_size(c.oid)) AS &quot;total_size&quot;
  15713. FROM pg_class c
  15714. LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
  15715. WHERE nspname NOT IN ('pg_catalog', 'information_schema')
  15716. AND c.relkind &lt;&gt; 'i'
  15717. AND nspname !~ '^pg_toast'
  15718. ORDER BY pg_total_relation_size(c.oid) DESC
  15719. LIMIT 20;
  15720. </code></pre>
  15721. <h3 id="result-example-2"><a class="header" href="#result-example-2">Result example:</a></h3>
  15722. <pre><code>public.state_groups_state - 27 GB
  15723. public.event_json - 9855 MB
  15724. public.events - 3675 MB
  15725. public.event_edges - 3404 MB
  15726. public.received_transactions - 2745 MB
  15727. public.event_reference_hashes - 1864 MB
  15728. public.event_auth - 1775 MB
  15729. public.stream_ordering_to_exterm - 1663 MB
  15730. public.event_search - 1370 MB
  15731. public.room_memberships - 1050 MB
  15732. public.event_to_state_groups - 948 MB
  15733. public.current_state_delta_stream - 711 MB
  15734. public.state_events - 611 MB
  15735. public.presence_stream - 530 MB
  15736. public.current_state_events - 525 MB
  15737. public.cache_invalidation_stream - 466 MB
  15738. public.receipts_linearized - 279 MB
  15739. public.state_groups - 160 MB
  15740. public.device_lists_remote_cache - 124 MB
  15741. public.state_group_edges - 122 MB
  15742. </code></pre>
  15743. <h2 id="show-top-20-larger-rooms-by-state-events-count"><a class="header" href="#show-top-20-larger-rooms-by-state-events-count">Show top 20 larger rooms by state events count</a></h2>
  15744. <p>You get the same information when you use the
  15745. <a href="usage/administration/../../admin_api/rooms.html#list-room-api">admin API</a>
  15746. and set parameter <code>order_by=state_events</code>.</p>
  15747. <pre><code class="language-sql">SELECT r.name, s.room_id, s.current_state_events
  15748. FROM room_stats_current s
  15749. LEFT JOIN room_stats_state r USING (room_id)
  15750. ORDER BY current_state_events DESC
  15751. LIMIT 20;
  15752. </code></pre>
  15753. <p>and by state_group_events count:</p>
  15754. <pre><code class="language-sql">SELECT rss.name, s.room_id, COUNT(s.room_id)
  15755. FROM state_groups_state s
  15756. LEFT JOIN room_stats_state rss USING (room_id)
  15757. GROUP BY s.room_id, rss.name
  15758. ORDER BY COUNT(s.room_id) DESC
  15759. LIMIT 20;
  15760. </code></pre>
  15761. <p>plus same, but with join removed for performance reasons:</p>
  15762. <pre><code class="language-sql">SELECT s.room_id, COUNT(s.room_id)
  15763. FROM state_groups_state s
  15764. GROUP BY s.room_id
  15765. ORDER BY COUNT(s.room_id) DESC
  15766. LIMIT 20;
  15767. </code></pre>
  15768. <h2 id="show-top-20-rooms-by-new-events-count-in-last-1-day"><a class="header" href="#show-top-20-rooms-by-new-events-count-in-last-1-day">Show top 20 rooms by new events count in last 1 day:</a></h2>
  15769. <pre><code class="language-sql">SELECT e.room_id, r.name, COUNT(e.event_id) cnt
  15770. FROM events e
  15771. LEFT JOIN room_stats_state r USING (room_id)
  15772. WHERE e.origin_server_ts &gt;= DATE_PART('epoch', NOW() - INTERVAL '1 day') * 1000
  15773. GROUP BY e.room_id, r.name
  15774. ORDER BY cnt DESC
  15775. LIMIT 20;
  15776. </code></pre>
  15777. <h2 id="show-top-20-users-on-homeserver-by-sent-events-messages-at-last-month"><a class="header" href="#show-top-20-users-on-homeserver-by-sent-events-messages-at-last-month">Show top 20 users on homeserver by sent events (messages) at last month:</a></h2>
  15778. <p>Caution. This query does not use any indexes, can be slow and create load on the database.</p>
  15779. <pre><code class="language-sql">SELECT COUNT(*), sender
  15780. FROM events
  15781. WHERE (type = 'm.room.encrypted' OR type = 'm.room.message')
  15782. AND origin_server_ts &gt;= DATE_PART('epoch', NOW() - INTERVAL '1 month') * 1000
  15783. GROUP BY sender
  15784. ORDER BY COUNT(*) DESC
  15785. LIMIT 20;
  15786. </code></pre>
  15787. <h2 id="show-last-100-messages-from-needed-user-with-room-names"><a class="header" href="#show-last-100-messages-from-needed-user-with-room-names">Show last 100 messages from needed user, with room names:</a></h2>
  15788. <pre><code class="language-sql">SELECT e.room_id, r.name, e.event_id, e.type, e.content, j.json
  15789. FROM events e
  15790. LEFT JOIN event_json j USING (room_id)
  15791. LEFT JOIN room_stats_state r USING (room_id)
  15792. WHERE sender = '@LOGIN:example.com'
  15793. AND e.type = 'm.room.message'
  15794. ORDER BY stream_ordering DESC
  15795. LIMIT 100;
  15796. </code></pre>
  15797. <h2 id="show-rooms-with-names-sorted-by-events-in-this-rooms"><a class="header" href="#show-rooms-with-names-sorted-by-events-in-this-rooms">Show rooms with names, sorted by events in this rooms</a></h2>
  15798. <p><strong>Sort and order with bash</strong></p>
  15799. <pre><code class="language-bash">echo &quot;SELECT event_json.room_id, room_stats_state.name FROM event_json, room_stats_state \
  15800. WHERE room_stats_state.room_id = event_json.room_id&quot; | psql -d synapse -h localhost -U synapse_user -t \
  15801. | sort | uniq -c | sort -n
  15802. </code></pre>
  15803. <p>Documentation for <code>psql</code> command line parameters: https://www.postgresql.org/docs/current/app-psql.html</p>
  15804. <p><strong>Sort and order with SQL</strong></p>
  15805. <pre><code class="language-sql">SELECT COUNT(*), event_json.room_id, room_stats_state.name
  15806. FROM event_json, room_stats_state
  15807. WHERE room_stats_state.room_id = event_json.room_id
  15808. GROUP BY event_json.room_id, room_stats_state.name
  15809. ORDER BY COUNT(*) DESC
  15810. LIMIT 50;
  15811. </code></pre>
  15812. <h3 id="result-example-3"><a class="header" href="#result-example-3">Result example:</a></h3>
  15813. <pre><code> 9459 !FPUfgzXYWTKgIrwKxW:matrix.org | This Week in Matrix
  15814. 9459 !FPUfgzXYWTKgIrwKxW:matrix.org | This Week in Matrix (TWIM)
  15815. 17799 !iDIOImbmXxwNngznsa:matrix.org | Linux in Russian
  15816. 18739 !GnEEPYXUhoaHbkFBNX:matrix.org | Riot Android
  15817. 23373 !QtykxKocfZaZOUrTwp:matrix.org | Matrix HQ
  15818. 39504 !gTQfWzbYncrtNrvEkB:matrix.org | ru.[matrix]
  15819. 43601 !iNmaIQExDMeqdITdHH:matrix.org | Riot
  15820. 43601 !iNmaIQExDMeqdITdHH:matrix.org | Riot Web/Desktop
  15821. </code></pre>
  15822. <h2 id="lookup-room-state-info-by-list-of-room_id"><a class="header" href="#lookup-room-state-info-by-list-of-room_id">Lookup room state info by list of room_id</a></h2>
  15823. <p>You get the same information when you use the
  15824. <a href="usage/administration/../../admin_api/rooms.html#room-details-api">admin API</a>.</p>
  15825. <pre><code class="language-sql">SELECT rss.room_id, rss.name, rss.canonical_alias, rss.topic, rss.encryption,
  15826. rsc.joined_members, rsc.local_users_in_room, rss.join_rules
  15827. FROM room_stats_state rss
  15828. LEFT JOIN room_stats_current rsc USING (room_id)
  15829. WHERE room_id IN ( WHERE room_id IN (
  15830. '!OGEhHVWSdvArJzumhm:matrix.org',
  15831. '!YTvKGNlinIzlkMTVRl:matrix.org'
  15832. );
  15833. </code></pre>
  15834. <h2 id="show-users-and-devices-that-have-not-been-online-for-a-while"><a class="header" href="#show-users-and-devices-that-have-not-been-online-for-a-while">Show users and devices that have not been online for a while</a></h2>
  15835. <pre><code class="language-sql">SELECT user_id, device_id, user_agent, TO_TIMESTAMP(last_seen / 1000) AS &quot;last_seen&quot;
  15836. FROM devices
  15837. WHERE last_seen &lt; DATE_PART('epoch', NOW() - INTERVAL '3 month') * 1000;
  15838. </code></pre>
  15839. <div style="break-before: page; page-break-before: always;"></div><p>This blog post by Victor Berger explains how to use many of the tools listed on this page: https://levans.fr/shrink-synapse-database.html</p>
  15840. <h1 id="list-of-useful-tools-and-scripts-for-maintenance-synapse-database"><a class="header" href="#list-of-useful-tools-and-scripts-for-maintenance-synapse-database">List of useful tools and scripts for maintenance Synapse database:</a></h1>
  15841. <h2 id="purge-remote-media-api-1"><a class="header" href="#purge-remote-media-api-1"><a href="usage/administration/../../admin_api/media_admin_api.html#purge-remote-media-api">Purge Remote Media API</a></a></h2>
  15842. <p>The purge remote media API allows server admins to purge old cached remote media.</p>
  15843. <h2 id="purge-local-media-api"><a class="header" href="#purge-local-media-api"><a href="usage/administration/../../admin_api/media_admin_api.html#delete-local-media">Purge Local Media API</a></a></h2>
  15844. <p>This API deletes the <em>local</em> media from the disk of your own server.</p>
  15845. <h2 id="purge-history-api-1"><a class="header" href="#purge-history-api-1"><a href="usage/administration/../../admin_api/purge_history_api.html">Purge History API</a></a></h2>
  15846. <p>The purge history API allows server admins to purge historic events from their database, reclaiming disk space.</p>
  15847. <h2 id="synapse-compress-state"><a class="header" href="#synapse-compress-state"><a href="https://github.com/matrix-org/rust-synapse-compress-state">synapse-compress-state</a></a></h2>
  15848. <p>Tool for compressing (deduplicating) <code>state_groups_state</code> table.</p>
  15849. <h2 id="sql-for-analyzing-synapse-postgresql-database-stats"><a class="header" href="#sql-for-analyzing-synapse-postgresql-database-stats"><a href="usage/administration/useful_sql_for_admins.html">SQL for analyzing Synapse PostgreSQL database stats</a></a></h2>
  15850. <p>Some easy SQL that reports useful stats about your Synapse database.</p>
  15851. <div style="break-before: page; page-break-before: always;"></div><h1 id="how-do-state-groups-work"><a class="header" href="#how-do-state-groups-work">How do State Groups work?</a></h1>
  15852. <p>As a general rule, I encourage people who want to understand the deepest darkest secrets of the database schema to drop by #synapse-dev:matrix.org and ask questions.</p>
  15853. <p>However, one question that comes up frequently is that of how &quot;state groups&quot; work, and why the <code>state_groups_state</code> table gets so big, so here's an attempt to answer that question.</p>
  15854. <p>We need to be able to relatively quickly calculate the state of a room at any point in that room's history. In other words, we need to know the state of the room at each event in that room. This is done as follows:</p>
  15855. <p>A sequence of events where the state is the same are grouped together into a <code>state_group</code>; the mapping is recorded in <code>event_to_state_groups</code>. (Technically speaking, since a state event usually changes the state in the room, we are recording the state of the room <em>after</em> the given event id: which is to say, to a handwavey simplification, the first event in a state group is normally a state event, and others in the same state group are normally non-state-events.)</p>
  15856. <p><code>state_groups</code> records, for each state group, the id of the room that we're looking at, and also the id of the first event in that group. (I'm not sure if that event id is used much in practice.) </p>
  15857. <p>Now, if we stored all the room state for each <code>state_group</code>, that would be a huge amount of data. Instead, for each state group, we normally store the difference between the state in that group and some other state group, and only occasionally (every 100 state changes or so) record the full state.</p>
  15858. <p>So, most state groups have an entry in <code>state_group_edges</code> (don't ask me why it's not a column in <code>state_groups</code>) which records the previous state group in the room, and <code>state_groups_state</code> records the differences in state since that previous state group.</p>
  15859. <p>A full state group just records the event id for each piece of state in the room at that point.</p>
  15860. <h2 id="known-bugs-with-state-groups"><a class="header" href="#known-bugs-with-state-groups">Known bugs with state groups</a></h2>
  15861. <p>There are various reasons that we can end up creating many more state groups than we need: see https://github.com/matrix-org/synapse/issues/3364 for more details.</p>
  15862. <h2 id="compression-tool"><a class="header" href="#compression-tool">Compression tool</a></h2>
  15863. <p>There is a tool at https://github.com/matrix-org/rust-synapse-compress-state which can compress the <code>state_groups_state</code> on a room by-room basis (essentially, it reduces the number of &quot;full&quot; state groups). This can result in dramatic reductions of the storage used.</p>
  15864. <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>
  15865. <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>
  15866. <p>See the following for how to decode the dense data available from the default logging configuration.</p>
  15867. <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]
  15868. -AAAAAAAAAAAAAAAAAAAAA- -BBBBBBBBBBBBBBBBBBBBBB- -C- -DD- -EEEEEE- -FFFFFFFFF- -GG- -HHHHHHHHHHHHHHHHHHHHHHH- -IIIIII- -JJJJJJJ- -KKKKKK-, -LLLLLL- -MMMMMMM- -NNNNNN- O -P- -QQ- -RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR- -SSSSSSSSSSSS- -TTTTTT-
  15869. </code></pre>
  15870. <table><thead><tr><th>Part</th><th>Explanation</th></tr></thead><tbody>
  15871. <tr><td>AAAA</td><td>Timestamp request was logged (not recieved)</td></tr>
  15872. <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>
  15873. <tr><td>CCCC</td><td>Line number in code</td></tr>
  15874. <tr><td>DDDD</td><td>Log Level</td></tr>
  15875. <tr><td>EEEE</td><td>Request Identifier (This identifier is shared by related log lines)</td></tr>
  15876. <tr><td>FFFF</td><td>Source IP (Or X-Forwarded-For if enabled)</td></tr>
  15877. <tr><td>GGGG</td><td>Server Port</td></tr>
  15878. <tr><td>HHHH</td><td>Federated Server or Local User making request (blank if unauthenticated or not supplied)</td></tr>
  15879. <tr><td>IIII</td><td>Total Time to process the request</td></tr>
  15880. <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>
  15881. <tr><td>KKKK</td><td>Userland CPU time</td></tr>
  15882. <tr><td>LLLL</td><td>System CPU time</td></tr>
  15883. <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>
  15884. <tr><td>NNNN</td><td>Total time waiting for response to DB queries across all parallel DB work from this request</td></tr>
  15885. <tr><td>OOOO</td><td>Count of DB transactions performed</td></tr>
  15886. <tr><td>PPPP</td><td>Response body size</td></tr>
  15887. <tr><td>QQQQ</td><td>Response status code<br/>Suffixed with <code>!</code> if the socket was closed before the response was generated.<br/>A <code>499!</code> status code indicates that Synapse also cancelled request processing after the socket was closed.<br/></td></tr>
  15888. <tr><td>RRRR</td><td>Request</td></tr>
  15889. <tr><td>SSSS</td><td>User-agent</td></tr>
  15890. <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>
  15891. </tbody></table>
  15892. <p>MMMM / NNNN can be greater than IIII if there are multiple slow database queries
  15893. running in parallel.</p>
  15894. <p>Some actions can result in multiple identical http requests, which will return
  15895. the same data, but only the first request will report time/transactions in
  15896. <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
  15897. response and will simultaneously return with the first request, but with very
  15898. small processing times.</p>
  15899. <div style="break-before: page; page-break-before: always;"></div><h2 id="admin-faq"><a class="header" href="#admin-faq">Admin FAQ</a></h2>
  15900. <h2 id="how-do-i-become-a-server-admin"><a class="header" href="#how-do-i-become-a-server-admin">How do I become a server admin?</a></h2>
  15901. <p>If your server already has an admin account you should use the user admin API to promote other accounts to become admins. See <a href="usage/administration/../../admin_api/user_admin_api.html#Change-whether-a-user-is-a-server-administrator-or-not">User Admin API</a></p>
  15902. <p>If you don't have any admin accounts yet you won't be able to use the admin API so you'll have to edit the database manually. Manually editing the database is generally not recommended so once you have an admin account, use the admin APIs to make further changes.</p>
  15903. <pre><code class="language-sql">UPDATE users SET admin = 1 WHERE name = '@foo:bar.com';
  15904. </code></pre>
  15905. <h2 id="what-servers-are-my-server-talking-to"><a class="header" href="#what-servers-are-my-server-talking-to">What servers are my server talking to?</a></h2>
  15906. <p>Run this sql query on your db:</p>
  15907. <pre><code class="language-sql">SELECT * FROM destinations;
  15908. </code></pre>
  15909. <h2 id="what-servers-are-currently-participating-in-this-room"><a class="header" href="#what-servers-are-currently-participating-in-this-room">What servers are currently participating in this room?</a></h2>
  15910. <p>Run this sql query on your db:</p>
  15911. <pre><code class="language-sql">SELECT DISTINCT split_part(state_key, ':', 2)
  15912. FROM current_state_events AS c
  15913. INNER JOIN room_memberships AS m USING (room_id, event_id)
  15914. WHERE room_id = '!cURbafjkfsMDVwdRDQ:matrix.org' AND membership = 'join';
  15915. </code></pre>
  15916. <h2 id="what-users-are-registered-on-my-server"><a class="header" href="#what-users-are-registered-on-my-server">What users are registered on my server?</a></h2>
  15917. <pre><code class="language-sql">SELECT NAME from users;
  15918. </code></pre>
  15919. <h2 id="manually-resetting-passwords"><a class="header" href="#manually-resetting-passwords">Manually resetting passwords:</a></h2>
  15920. <p>See https://github.com/matrix-org/synapse/blob/master/README.rst#password-reset</p>
  15921. <h2 id="i-have-a-problem-with-my-server-can-i-just-delete-my-database-and-start-again"><a class="header" href="#i-have-a-problem-with-my-server-can-i-just-delete-my-database-and-start-again">I have a problem with my server. Can I just delete my database and start again?</a></h2>
  15922. <p>Deleting your database is unlikely to make anything better. </p>
  15923. <p>It's easy to make the mistake of thinking that you can start again from a clean slate by dropping your database, but things don't work like that in a federated network: lots of other servers have information about your server.</p>
  15924. <p>For example: other servers might think that you are in a room, your server will think that you are not, and you'll probably be unable to interact with that room in a sensible way ever again.</p>
  15925. <p>In general, there are better solutions to any problem than dropping the database. Come and seek help in https://matrix.to/#/#synapse:matrix.org.</p>
  15926. <p>There are two exceptions when it might be sensible to delete your database and start again:</p>
  15927. <ul>
  15928. <li>You have <em>never</em> joined any rooms which are federated with other servers. For instance, a local deployment which the outside world can't talk to. </li>
  15929. <li>You are changing the <code>server_name</code> in the homeserver configuration. In effect this makes your server a completely new one from the point of view of the network, so in this case it makes sense to start with a clean database.
  15930. (In both cases you probably also want to clear out the media_store.)</li>
  15931. </ul>
  15932. <h2 id="ive-stuffed-up-access-to-my-room-how-can-i-delete-it-to-free-up-the-alias"><a class="header" href="#ive-stuffed-up-access-to-my-room-how-can-i-delete-it-to-free-up-the-alias">I've stuffed up access to my room, how can I delete it to free up the alias?</a></h2>
  15933. <p>Using the following curl command:</p>
  15934. <pre><code>curl -H 'Authorization: Bearer &lt;access-token&gt;' -X DELETE https://matrix.org/_matrix/client/r0/directory/room/&lt;room-alias&gt;
  15935. </code></pre>
  15936. <p><code>&lt;access-token&gt;</code> - can be obtained in riot by looking in the riot settings, down the bottom is:
  15937. Access Token:&lt;click to reveal&gt; </p>
  15938. <p><code>&lt;room-alias&gt;</code> - the room alias, eg. #my_room:matrix.org this possibly needs to be URL encoded also, for example %23my_room%3Amatrix.org</p>
  15939. <h2 id="how-can-i-find-the-lines-corresponding-to-a-given-http-request-in-my-homeserver-log"><a class="header" href="#how-can-i-find-the-lines-corresponding-to-a-given-http-request-in-my-homeserver-log">How can I find the lines corresponding to a given HTTP request in my homeserver log?</a></h2>
  15940. <p>Synapse tags each log line according to the HTTP request it is processing. When it finishes processing each request, it logs a line containing the words <code>Processed request: </code>. For example:</p>
  15941. <pre><code>2019-02-14 22:35:08,196 - synapse.access.http.8008 - 302 - INFO - GET-37 - ::1 - 8008 - {@richvdh:localhost} Processed request: 0.173sec/0.001sec (0.002sec, 0.000sec) (0.027sec/0.026sec/2) 687B 200 &quot;GET /_matrix/client/r0/sync HTTP/1.1&quot; &quot;Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36&quot; [0 dbevts]&quot;
  15942. </code></pre>
  15943. <p>Here we can see that the request has been tagged with <code>GET-37</code>. (The tag depends on the method of the HTTP request, so might start with <code>GET-</code>, <code>PUT-</code>, <code>POST-</code>, <code>OPTIONS-</code> or <code>DELETE-</code>.) So to find all lines corresponding to this request, we can do:</p>
  15944. <pre><code>grep 'GET-37' homeserver.log
  15945. </code></pre>
  15946. <p>If you want to paste that output into a github issue or matrix room, please remember to surround it with triple-backticks (```) to make it legible (see https://help.github.com/en/articles/basic-writing-and-formatting-syntax#quoting-code).</p>
  15947. <h2 id="what-do-all-those-fields-in-the-processed-line-mean"><a class="header" href="#what-do-all-those-fields-in-the-processed-line-mean">What do all those fields in the 'Processed' line mean?</a></h2>
  15948. <p>See <a href="usage/administration/request_log.html">Request log format</a>.</p>
  15949. <h2 id="what-are-the-biggest-rooms-on-my-server"><a class="header" href="#what-are-the-biggest-rooms-on-my-server">What are the biggest rooms on my server?</a></h2>
  15950. <pre><code class="language-sql">SELECT s.canonical_alias, g.room_id, count(*) AS num_rows
  15951. FROM
  15952. state_groups_state AS g,
  15953. room_stats_state AS s
  15954. WHERE g.room_id = s.room_id
  15955. GROUP BY s.canonical_alias, g.room_id
  15956. ORDER BY num_rows desc
  15957. LIMIT 10;
  15958. </code></pre>
  15959. <p>You can also use the <a href="usage/administration/../../admin_api/rooms.html#list-room-api">List Room API</a>
  15960. and <code>order_by</code> <code>state_events</code>.</p>
  15961. <div style="break-before: page; page-break-before: always;"></div><h1 id="contributing"><a class="header" href="#contributing">Contributing</a></h1>
  15962. <p>This document aims to get you started with contributing to Synapse!</p>
  15963. <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>
  15964. <p>Everyone is welcome to contribute code to <a href="https://github.com/matrix-org">matrix.org
  15965. projects</a>, provided that they are willing to
  15966. license their contributions under the same license as the project itself. We
  15967. follow a simple 'inbound=outbound' model for contributions: the act of
  15968. submitting an 'inbound' contribution means that the contributor agrees to
  15969. license the code under the same terms as the project's overall 'outbound'
  15970. license - in our case, this is almost always Apache Software License v2 (see
  15971. <a href="https://github.com/matrix-org/synapse/blob/develop/LICENSE">LICENSE</a>).</p>
  15972. <h1 id="2-what-do-i-need"><a class="header" href="#2-what-do-i-need">2. What do I need?</a></h1>
  15973. <p>If you are running Windows, the Windows Subsystem for Linux (WSL) is strongly
  15974. recommended for development. More information about WSL can be found at
  15975. <a href="https://docs.microsoft.com/en-us/windows/wsl/install">https://docs.microsoft.com/en-us/windows/wsl/install</a>. Running Synapse natively
  15976. on Windows is not officially supported.</p>
  15977. <p>The code of Synapse is written in Python 3. To do pretty much anything, you'll need <a href="https://www.python.org/downloads/">a recent version of Python 3</a>. Your Python also needs support for <a href="https://docs.python.org/3/library/venv.html">virtual environments</a>. This is usually built-in, but some Linux distributions like Debian and Ubuntu split it out into its own package. Running <code>sudo apt install python3-venv</code> should be enough.</p>
  15978. <p>Synapse can connect to PostgreSQL via the <a href="https://pypi.org/project/psycopg2/">psycopg2</a> Python library. Building this library from source requires access to PostgreSQL's C header files. On Debian or Ubuntu Linux, these can be installed with <code>sudo apt install libpq-dev</code>.</p>
  15979. <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>
  15980. <p>For some tests, you will need <a href="https://docs.docker.com/get-docker/">a recent version of Docker</a>.</p>
  15981. <h1 id="3-get-the-source"><a class="header" href="#3-get-the-source">3. Get the source.</a></h1>
  15982. <p>The preferred and easiest way to contribute changes is to fork the relevant
  15983. 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
  15984. changes into our repo.</p>
  15985. <p>Please base your changes on the <code>develop</code> branch.</p>
  15986. <pre><code class="language-sh">git clone git@github.com:YOUR_GITHUB_USER_NAME/synapse.git
  15987. git checkout develop
  15988. </code></pre>
  15989. <p>If you need help getting started with git, this is beyond the scope of the document, but you
  15990. can find many good git tutorials on the web.</p>
  15991. <h1 id="4-install-the-dependencies"><a class="header" href="#4-install-the-dependencies">4. Install the dependencies</a></h1>
  15992. <p>Synapse uses the <a href="https://python-poetry.org/">poetry</a> project to manage its dependencies
  15993. and development environment. Once you have installed Python 3 and added the
  15994. source, you should install <code>poetry</code>.
  15995. Of their installation methods, we recommend
  15996. <a href="https://python-poetry.org/docs/#installing-with-pipx">installing <code>poetry</code> using <code>pipx</code></a>,</p>
  15997. <pre><code class="language-shell">pip install --user pipx
  15998. pipx install poetry
  15999. </code></pre>
  16000. <p>but see poetry's <a href="https://python-poetry.org/docs/#installation">installation instructions</a>
  16001. for other installation methods.</p>
  16002. <p>Next, open a terminal and install dependencies as follows:</p>
  16003. <pre><code class="language-sh">cd path/where/you/have/cloned/the/repository
  16004. poetry install --extras all
  16005. </code></pre>
  16006. <p>This will install the runtime and developer dependencies for the project.</p>
  16007. <h1 id="5-get-in-touch"><a class="header" href="#5-get-in-touch">5. Get in touch.</a></h1>
  16008. <p>Join our developer community on Matrix: <a href="https://matrix.to/#/#synapse-dev:matrix.org">#synapse-dev:matrix.org</a>!</p>
  16009. <h1 id="6-pick-an-issue"><a class="header" href="#6-pick-an-issue">6. Pick an issue.</a></h1>
  16010. <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>
  16011. to work on.</p>
  16012. <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>
  16013. <p>There is a growing amount of documentation located in the
  16014. <a href="https://github.com/matrix-org/synapse/tree/develop/docs"><code>docs</code></a>
  16015. directory, with a rendered version <a href="https://matrix-org.github.io/synapse">available online</a>.
  16016. This documentation is intended primarily for sysadmins running their
  16017. own Synapse instance, as well as developers interacting externally with
  16018. Synapse.
  16019. <a href="https://github.com/matrix-org/synapse/tree/develop/docs/development"><code>docs/development</code></a>
  16020. exists primarily to house documentation for
  16021. Synapse developers.
  16022. <a href="https://github.com/matrix-org/synapse/tree/develop/docs/admin_api"><code>docs/admin_api</code></a> houses documentation
  16023. regarding Synapse's Admin API, which is used mostly by sysadmins and external
  16024. service developers.</p>
  16025. <p>Synapse's code style is documented <a href="development/../code_style.html">here</a>. Please follow
  16026. it, including the conventions for the <a href="development/../code_style.html#configuration-file-format">sample configuration
  16027. file</a>.</p>
  16028. <p>We welcome improvements and additions to our documentation itself! When
  16029. writing new pages, please
  16030. <a href="https://github.com/matrix-org/synapse/tree/develop/docs#adding-to-the-documentation">build <code>docs</code> to a book</a>
  16031. to check that your contributions render correctly. The docs are written in
  16032. <a href="https://guides.github.com/features/mastering-markdown/">GitHub-Flavoured Markdown</a>.</p>
  16033. <p>Some documentation also exists in <a href="https://github.com/matrix-org/synapse/wiki">Synapse's GitHub
  16034. Wiki</a>, although this is primarily
  16035. contributed to by community authors.</p>
  16036. <h1 id="8-test-test-test"><a class="header" href="#8-test-test-test">8. Test, test, test!</a></h1>
  16037. <p><a name="test-test-test"></a></p>
  16038. <p>While you're developing and before submitting a patch, you'll
  16039. want to test your code.</p>
  16040. <h2 id="run-the-linters"><a class="header" href="#run-the-linters">Run the linters.</a></h2>
  16041. <p>The linters look at your code and do two things:</p>
  16042. <ul>
  16043. <li>ensure that your code follows the coding style adopted by the project;</li>
  16044. <li>catch a number of errors in your code.</li>
  16045. </ul>
  16046. <p>The linter takes no time at all to run as soon as you've <a href="development/contributing_guide.html#4-install-the-dependencies">downloaded the dependencies</a>.</p>
  16047. <pre><code class="language-sh">poetry run ./scripts-dev/lint.sh
  16048. </code></pre>
  16049. <p>Note that this script <em>will modify your files</em> to fix styling errors.
  16050. Make sure that you have saved all your files.</p>
  16051. <p>If you wish to restrict the linters to only the files changed since the last commit
  16052. (much faster!), you can instead run:</p>
  16053. <pre><code class="language-sh">poetry run ./scripts-dev/lint.sh -d
  16054. </code></pre>
  16055. <p>Or if you know exactly which files you wish to lint, you can instead run:</p>
  16056. <pre><code class="language-sh">poetry run ./scripts-dev/lint.sh path/to/file1.py path/to/file2.py path/to/folder
  16057. </code></pre>
  16058. <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>
  16059. <p>The unit tests run parts of Synapse, including your changes, to see if anything
  16060. was broken. They are slower than the linters but will typically catch more errors.</p>
  16061. <pre><code class="language-sh">poetry run trial tests
  16062. </code></pre>
  16063. <p>If you wish to only run <em>some</em> unit tests, you may specify
  16064. another module instead of <code>tests</code> - or a test class or a method:</p>
  16065. <pre><code class="language-sh">poetry run trial tests.rest.admin.test_room tests.handlers.test_admin.ExfiltrateData.test_invite
  16066. </code></pre>
  16067. <p>If your tests fail, you may wish to look at the logs (the default log level is <code>ERROR</code>):</p>
  16068. <pre><code class="language-sh">less _trial_temp/test.log
  16069. </code></pre>
  16070. <p>To increase the log level for the tests, set <code>SYNAPSE_TEST_LOG_LEVEL</code>:</p>
  16071. <pre><code class="language-sh">SYNAPSE_TEST_LOG_LEVEL=DEBUG poetry run trial tests
  16072. </code></pre>
  16073. <p>By default, tests will use an in-memory SQLite database for test data. For additional
  16074. help with debugging, one can use an on-disk SQLite database file instead, in order to
  16075. review database state during and after running tests. This can be done by setting
  16076. the <code>SYNAPSE_TEST_PERSIST_SQLITE_DB</code> environment variable. Doing so will cause the
  16077. database state to be stored in a file named <code>test.db</code> under the trial process'
  16078. working directory. Typically, this ends up being <code>_trial_temp/test.db</code>. For example:</p>
  16079. <pre><code class="language-sh">SYNAPSE_TEST_PERSIST_SQLITE_DB=1 poetry run trial tests
  16080. </code></pre>
  16081. <p>The database file can then be inspected with:</p>
  16082. <pre><code class="language-sh">sqlite3 _trial_temp/test.db
  16083. </code></pre>
  16084. <p>Note that the database file is cleared at the beginning of each test run. Thus it
  16085. will always only contain the data generated by the <em>last run test</em>. Though generally
  16086. when debugging, one is only running a single test anyway.</p>
  16087. <h3 id="running-tests-under-postgresql"><a class="header" href="#running-tests-under-postgresql">Running tests under PostgreSQL</a></h3>
  16088. <p>Invoking <code>trial</code> as above will use an in-memory SQLite database. This is great for
  16089. quick development and testing. However, we recommend using a PostgreSQL database
  16090. in production (and indeed, we have some code paths specific to each database).
  16091. This means that we need to run our unit tests against PostgreSQL too. Our CI does
  16092. this automatically for pull requests and release candidates, but it's sometimes
  16093. useful to reproduce this locally.</p>
  16094. <h4 id="using-docker"><a class="header" href="#using-docker">Using Docker</a></h4>
  16095. <p>The easiest way to do so is to run Postgres via a docker container. In one
  16096. terminal:</p>
  16097. <pre><code class="language-shell">docker run --rm -e POSTGRES_PASSWORD=mysecretpassword -e POSTGRES_USER=postgres -e POSTGRES_DB=postgress -p 5432:5432 postgres:14
  16098. </code></pre>
  16099. <p>If you see an error like</p>
  16100. <pre><code>docker: Error response from daemon: driver failed programming external connectivity on endpoint nice_ride (b57bbe2e251b70015518d00c9981e8cb8346b5c785250341a6c53e3c899875f1): Error starting userland proxy: listen tcp4 0.0.0.0:5432: bind: address already in use.
  16101. </code></pre>
  16102. <p>then something is already bound to port 5432. You're probably already running postgres locally.</p>
  16103. <p>Once you have a postgres server running, invoke <code>trial</code> in a second terminal:</p>
  16104. <pre><code class="language-shell">SYNAPSE_POSTGRES=1 SYNAPSE_POSTGRES_HOST=127.0.0.1 SYNAPSE_POSTGRES_USER=postgres SYNAPSE_POSTGRES_PASSWORD=mysecretpassword poetry run trial tests
  16105. </code></pre>
  16106. <h4 id="using-an-existing-postgres-installation"><a class="header" href="#using-an-existing-postgres-installation">Using an existing Postgres installation</a></h4>
  16107. <p>If you have postgres already installed on your system, you can run <code>trial</code> with the
  16108. following environment variables matching your configuration:</p>
  16109. <ul>
  16110. <li><code>SYNAPSE_POSTGRES</code> to anything nonempty</li>
  16111. <li><code>SYNAPSE_POSTGRES_HOST</code> (optional if it's the default: UNIX socket)</li>
  16112. <li><code>SYNAPSE_POSTGRES_PORT</code> (optional if it's the default: 5432)</li>
  16113. <li><code>SYNAPSE_POSTGRES_USER</code> (optional if using a UNIX socket)</li>
  16114. <li><code>SYNAPSE_POSTGRES_PASSWORD</code> (optional if using a UNIX socket)</li>
  16115. </ul>
  16116. <p>For example:</p>
  16117. <pre><code class="language-shell">export SYNAPSE_POSTGRES=1
  16118. export SYNAPSE_POSTGRES_HOST=localhost
  16119. export SYNAPSE_POSTGRES_USER=postgres
  16120. export SYNAPSE_POSTGRES_PASSWORD=mydevenvpassword
  16121. trial
  16122. </code></pre>
  16123. <p>You don't need to specify the host, user, port or password if your Postgres
  16124. server is set to authenticate you over the UNIX socket (i.e. if the <code>psql</code> command
  16125. works without further arguments).</p>
  16126. <p>Your Postgres account needs to be able to create databases; see the postgres
  16127. docs for <a href="https://www.postgresql.org/docs/current/sql-alterrole.html"><code>ALTER ROLE</code></a>.</p>
  16128. <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>
  16129. <p>The integration tests are a more comprehensive suite of tests. They
  16130. run a full version of Synapse, including your changes, to check if
  16131. anything was broken. They are slower than the unit tests but will
  16132. typically catch more errors.</p>
  16133. <p>The following command will let you run the integration test with the most common
  16134. configuration:</p>
  16135. <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
  16136. </code></pre>
  16137. <p>(Note that the paths must be full paths! You could also write <code>$(realpath relative/path)</code> if needed.)</p>
  16138. <p>This configuration should generally cover your needs.</p>
  16139. <ul>
  16140. <li>To run with Postgres, supply the <code>-e POSTGRES=1 -e MULTI_POSTGRES=1</code> environment flags.</li>
  16141. <li>To run with Synapse in worker mode, supply the <code>-e WORKERS=1 -e REDIS=1</code> environment flags (in addition to the Postgres flags).</li>
  16142. </ul>
  16143. <p>For more details about other configurations, see the <a href="https://github.com/matrix-org/sytest/blob/develop/docker/README.md">Docker-specific documentation in the SyTest repo</a>.</p>
  16144. <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>
  16145. <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>
  16146. <p>It's often nice to develop on Synapse and write Complement tests at the same time.
  16147. Here is how to run your local Synapse checkout against your local Complement checkout.</p>
  16148. <p>(checkout <a href="https://github.com/matrix-org/complement"><code>complement</code></a> alongside your <code>synapse</code> checkout)</p>
  16149. <pre><code class="language-sh">COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh
  16150. </code></pre>
  16151. <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>
  16152. <pre><code class="language-sh">COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -run TestImportHistoricalMessages
  16153. </code></pre>
  16154. <p>To run a specific test, you can specify the whole name structure:</p>
  16155. <pre><code class="language-sh">COMPLEMENT_DIR=../complement ./scripts-dev/complement.sh -run TestImportHistoricalMessages/parallel/Historical_events_resolve_in_the_correct_order
  16156. </code></pre>
  16157. <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>
  16158. <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>
  16159. <ol>
  16160. <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>
  16161. <li>Start the Complement tests</li>
  16162. <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>
  16163. <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>
  16164. <li>Install sqlite (database driver), <code>apt-get update &amp;&amp; apt-get install -y sqlite3</code></li>
  16165. <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>
  16166. </ol>
  16167. <h1 id="9-submit-your-patch"><a class="header" href="#9-submit-your-patch">9. Submit your patch.</a></h1>
  16168. <p>Once you're happy with your patch, it's time to prepare a Pull Request.</p>
  16169. <p>To prepare a Pull Request, please:</p>
  16170. <ol>
  16171. <li>verify that <a href="development/contributing_guide.html#test-test-test">all the tests pass</a>, including the coding style;</li>
  16172. <li><a href="development/contributing_guide.html#sign-off">sign off</a> your contribution;</li>
  16173. <li><code>git push</code> your commit to your fork of Synapse;</li>
  16174. <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>
  16175. <li>add a <a href="development/contributing_guide.html#changelog">changelog entry</a> and push it to your Pull Request;</li>
  16176. <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>
  16177. <li>if you need to update your PR, please avoid rebasing and just add new commits to your branch.</li>
  16178. </ol>
  16179. <h2 id="changelog"><a class="header" href="#changelog">Changelog</a></h2>
  16180. <p>All changes, even minor ones, need a corresponding changelog / newsfragment
  16181. entry. These are managed by <a href="https://github.com/hawkowl/towncrier">Towncrier</a>.</p>
  16182. <p>To create a changelog entry, make a new file in the <code>changelog.d</code> directory named
  16183. in the format of <code>PRnumber.type</code>. The type can be one of the following:</p>
  16184. <ul>
  16185. <li><code>feature</code></li>
  16186. <li><code>bugfix</code></li>
  16187. <li><code>docker</code> (for updates to the Docker image)</li>
  16188. <li><code>doc</code> (for updates to the documentation)</li>
  16189. <li><code>removal</code> (also used for deprecations)</li>
  16190. <li><code>misc</code> (for internal-only changes)</li>
  16191. </ul>
  16192. <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
  16193. release, so the content of the file should be a short description of your
  16194. change in the same style as the rest of the changelog. The file can contain Markdown
  16195. formatting, and should end with a full stop (.) or an exclamation mark (!) for
  16196. consistency.</p>
  16197. <p>Adding credits to the changelog is encouraged, we value your
  16198. contributions and would like to have you shouted out in the release notes!</p>
  16199. <p>For example, a fix in PR #1234 would have its changelog entry in
  16200. <code>changelog.d/1234.bugfix</code>, and contain content like:</p>
  16201. <blockquote>
  16202. <p>The security levels of Florbs are now validated when received
  16203. via the <code>/federation/florb</code> endpoint. Contributed by Jane Matrix.</p>
  16204. </blockquote>
  16205. <p>If there are multiple pull requests involved in a single bugfix/feature/etc,
  16206. then the content for each <code>changelog.d</code> file should be the same. Towncrier will
  16207. merge the matching files together into a single changelog entry when we come to
  16208. release.</p>
  16209. <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>
  16210. <p>Obviously, you don't know if you should call your newsfile
  16211. <code>1234.bugfix</code> or <code>5678.bugfix</code> until you create the PR, which leads to a
  16212. chicken-and-egg problem.</p>
  16213. <p>There are two options for solving this:</p>
  16214. <ol>
  16215. <li>
  16216. <p>Open the PR without a changelog file, see what number you got, and <em>then</em>
  16217. add the changelog file to your branch (see <a href="development/contributing_guide.html#updating-your-pull-request">Updating your pull
  16218. request</a>), or:</p>
  16219. </li>
  16220. <li>
  16221. <p>Look at the <a href="https://github.com/matrix-org/synapse/issues?q=">list of all
  16222. issues/PRs</a>, add one to the
  16223. highest number you see, and quickly open the PR before somebody else claims
  16224. your number.</p>
  16225. <p><a href="https://github.com/richvdh/scripts/blob/master/next_github_number.sh">This
  16226. script</a>
  16227. might be helpful if you find yourself doing this a lot.</p>
  16228. </li>
  16229. </ol>
  16230. <p>Sorry, we know it's a bit fiddly, but it's <em>really</em> helpful for us when we come
  16231. to put together a release!</p>
  16232. <h3 id="debian-changelog"><a class="header" href="#debian-changelog">Debian changelog</a></h3>
  16233. <p>Changes which affect the debian packaging files (in <code>debian</code>) are an
  16234. exception to the rule that all changes require a <code>changelog.d</code> file.</p>
  16235. <p>In this case, you will need to add an entry to the debian changelog for the
  16236. next release. For this, run the following command:</p>
  16237. <pre><code>dch
  16238. </code></pre>
  16239. <p>This will make up a new version number (if there isn't already an unreleased
  16240. version in flight), and open an editor where you can add a new changelog entry.
  16241. (Our release process will ensure that the version number and maintainer name is
  16242. corrected for the release.)</p>
  16243. <p>If your change affects both the debian packaging <em>and</em> files outside the debian
  16244. directory, you will need both a regular newsfragment <em>and</em> an entry in the
  16245. debian changelog. (Though typically such changes should be submitted as two
  16246. separate pull requests.)</p>
  16247. <h2 id="sign-off"><a class="header" href="#sign-off">Sign off</a></h2>
  16248. <p>In order to have a concrete record that your contribution is intentional
  16249. and you agree to license it under the same terms as the project's license, we've adopted the
  16250. same lightweight approach that the Linux Kernel
  16251. <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>,
  16252. <a href="https://github.com/docker/docker/blob/master/CONTRIBUTING.md">Docker</a>, and many other
  16253. projects use: the DCO (<a href="http://developercertificate.org/">Developer Certificate of Origin</a>).
  16254. This is a simple declaration that you wrote
  16255. the contribution or otherwise have the right to contribute it to Matrix:</p>
  16256. <pre><code>Developer Certificate of Origin
  16257. Version 1.1
  16258. Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
  16259. 660 York Street, Suite 102,
  16260. San Francisco, CA 94110 USA
  16261. Everyone is permitted to copy and distribute verbatim copies of this
  16262. license document, but changing it is not allowed.
  16263. Developer's Certificate of Origin 1.1
  16264. By making a contribution to this project, I certify that:
  16265. (a) The contribution was created in whole or in part by me and I
  16266. have the right to submit it under the open source license
  16267. indicated in the file; or
  16268. (b) The contribution is based upon previous work that, to the best
  16269. of my knowledge, is covered under an appropriate open source
  16270. license and I have the right under that license to submit that
  16271. work with modifications, whether created in whole or in part
  16272. by me, under the same open source license (unless I am
  16273. permitted to submit under a different license), as indicated
  16274. in the file; or
  16275. (c) The contribution was provided directly to me by some other
  16276. person who certified (a), (b) or (c) and I have not modified
  16277. it.
  16278. (d) I understand and agree that this project and the contribution
  16279. are public and that a record of the contribution (including all
  16280. personal information I submit with it, including my sign-off) is
  16281. maintained indefinitely and may be redistributed consistent with
  16282. this project or the open source license(s) involved.
  16283. </code></pre>
  16284. <p>If you agree to this for your contribution, then all that's needed is to
  16285. include the line in your commit or pull request comment:</p>
  16286. <pre><code>Signed-off-by: Your Name &lt;your@email.example.org&gt;
  16287. </code></pre>
  16288. <p>We accept contributions under a legally identifiable name, such as
  16289. your name on government documentation or common-law names (names
  16290. claimed by legitimate usage or repute). Unfortunately, we cannot
  16291. accept anonymous contributions at this time.</p>
  16292. <p>Git allows you to add this signoff automatically when using the <code>-s</code>
  16293. flag to <code>git commit</code>, which uses the name and email set in your
  16294. <code>user.name</code> and <code>user.email</code> git configs.</p>
  16295. <h3 id="private-sign-off"><a class="header" href="#private-sign-off">Private Sign off</a></h3>
  16296. <p>If you would like to provide your legal name privately to the Matrix.org
  16297. Foundation (instead of in a public commit or comment), you can do so
  16298. by emailing your legal name and a link to the pull request to
  16299. <a href="mailto:dco@matrix.org?subject=Private%20sign%20off">dco@matrix.org</a>.
  16300. It helps to include &quot;sign off&quot; or similar in the subject line. You will then
  16301. be instructed further.</p>
  16302. <p>Once private sign off is complete, doing so for future contributions will not
  16303. be required.</p>
  16304. <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>
  16305. <p>Once the Pull Request is opened, you will see a few things:</p>
  16306. <ol>
  16307. <li>our automated CI (Continuous Integration) pipeline will run (again) the linters, the unit tests, the integration tests and more;</li>
  16308. <li>one or more of the developers will take a look at your Pull Request and offer feedback.</li>
  16309. </ol>
  16310. <p>From this point, you should:</p>
  16311. <ol>
  16312. <li>Look at the results of the CI pipeline.
  16313. <ul>
  16314. <li>If there is any error, fix the error.</li>
  16315. </ul>
  16316. </li>
  16317. <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>
  16318. <li>Create a new commit with the changes.
  16319. <ul>
  16320. <li>Please do NOT overwrite the history. New commits make the reviewer's life easier.</li>
  16321. <li>Push this commits to your Pull Request.</li>
  16322. </ul>
  16323. </li>
  16324. <li>Back to 1.</li>
  16325. </ol>
  16326. <p>Once both the CI and the developers are happy, the patch will be merged into Synapse and released shortly!</p>
  16327. <h1 id="11-find-a-new-issue"><a class="header" href="#11-find-a-new-issue">11. Find a new issue.</a></h1>
  16328. <p>By now, you know the drill!</p>
  16329. <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>
  16330. <p>There are some notes for those with commit access to the project on how we
  16331. manage git <a href="development/git.html">here</a>.</p>
  16332. <h1 id="conclusion"><a class="header" href="#conclusion">Conclusion</a></h1>
  16333. <p>That's it! Matrix is a very open and collaborative project as you might expect
  16334. given our obsession with open communication. If we're going to successfully
  16335. matrix together all the fragmented communication technologies out there we are
  16336. reliant on contributions and collaboration from the community to do so. So
  16337. please get involved - and we hope you have as much fun hacking on Matrix as we
  16338. do!</p>
  16339. <div style="break-before: page; page-break-before: always;"></div><h1 id="code-style"><a class="header" href="#code-style">Code Style</a></h1>
  16340. <h2 id="formatting-tools"><a class="header" href="#formatting-tools">Formatting tools</a></h2>
  16341. <p>The Synapse codebase uses a number of code formatting tools in order to
  16342. quickly and automatically check for formatting (and sometimes logical)
  16343. errors in code.</p>
  16344. <p>The necessary tools are:</p>
  16345. <ul>
  16346. <li><a href="https://black.readthedocs.io/en/stable/">black</a>, a source code formatter;</li>
  16347. <li><a href="https://pycqa.github.io/isort/">isort</a>, which organises each file's imports;</li>
  16348. <li><a href="https://flake8.pycqa.org/en/latest/">flake8</a>, which can spot common errors; and</li>
  16349. <li><a href="https://mypy.readthedocs.io/en/stable/">mypy</a>, a type checker.</li>
  16350. </ul>
  16351. <p>Install them with:</p>
  16352. <pre><code class="language-sh">pip install -e &quot;.[lint,mypy]&quot;
  16353. </code></pre>
  16354. <p>The easiest way to run the lints is to invoke the linter script as follows.</p>
  16355. <pre><code class="language-sh">scripts-dev/lint.sh
  16356. </code></pre>
  16357. <p>It's worth noting that modern IDEs and text editors can run these tools
  16358. automatically on save. It may be worth looking into whether this
  16359. functionality is supported in your editor for a more convenient
  16360. development workflow. It is not, however, recommended to run <code>flake8</code> or <code>mypy</code>
  16361. on save as they take a while and can be very resource intensive.</p>
  16362. <h2 id="general-rules"><a class="header" href="#general-rules">General rules</a></h2>
  16363. <ul>
  16364. <li><strong>Naming</strong>:
  16365. <ul>
  16366. <li>Use <code>CamelCase</code> for class and type names</li>
  16367. <li>Use underscores for <code>function_names</code> and <code>variable_names</code>.</li>
  16368. </ul>
  16369. </li>
  16370. <li><strong>Docstrings</strong>: should follow the <a href="https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings">google code
  16371. style</a>.
  16372. See the
  16373. <a href="http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html">examples</a>
  16374. in the sphinx documentation.</li>
  16375. <li><strong>Imports</strong>:
  16376. <ul>
  16377. <li>
  16378. <p>Imports should be sorted by <code>isort</code> as described above.</p>
  16379. </li>
  16380. <li>
  16381. <p>Prefer to import classes and functions rather than packages or
  16382. modules.</p>
  16383. <p>Example:</p>
  16384. <pre><code class="language-python">from synapse.types import UserID
  16385. ...
  16386. user_id = UserID(local, server)
  16387. </code></pre>
  16388. <p>is preferred over:</p>
  16389. <pre><code class="language-python">from synapse import types
  16390. ...
  16391. user_id = types.UserID(local, server)
  16392. </code></pre>
  16393. <p>(or any other variant).</p>
  16394. <p>This goes against the advice in the Google style guide, but it
  16395. means that errors in the name are caught early (at import time).</p>
  16396. </li>
  16397. <li>
  16398. <p>Avoid wildcard imports (<code>from synapse.types import *</code>) and
  16399. relative imports (<code>from .types import UserID</code>).</p>
  16400. </li>
  16401. </ul>
  16402. </li>
  16403. </ul>
  16404. <h2 id="configuration-file-format"><a class="header" href="#configuration-file-format">Configuration file format</a></h2>
  16405. <p>The <a href="./sample_config.yaml">sample configuration file</a> acts as a
  16406. reference to Synapse's configuration options for server administrators.
  16407. Remember that many readers will be unfamiliar with YAML and server
  16408. administration in general, so that it is important that the file be as
  16409. easy to understand as possible, which includes following a consistent
  16410. format.</p>
  16411. <p>Some guidelines follow:</p>
  16412. <ul>
  16413. <li>
  16414. <p>Sections should be separated with a heading consisting of a single
  16415. line prefixed and suffixed with <code>##</code>. There should be <strong>two</strong> blank
  16416. lines before the section header, and <strong>one</strong> after.</p>
  16417. </li>
  16418. <li>
  16419. <p>Each option should be listed in the file with the following format:</p>
  16420. <ul>
  16421. <li>
  16422. <p>A comment describing the setting. Each line of this comment
  16423. should be prefixed with a hash (<code>#</code>) and a space.</p>
  16424. <p>The comment should describe the default behaviour (ie, what
  16425. happens if the setting is omitted), as well as what the effect
  16426. will be if the setting is changed.</p>
  16427. <p>Often, the comment end with something like &quot;uncomment the
  16428. following to <do action>&quot;.</p>
  16429. </li>
  16430. <li>
  16431. <p>A line consisting of only <code>#</code>.</p>
  16432. </li>
  16433. <li>
  16434. <p>A commented-out example setting, prefixed with only <code>#</code>.</p>
  16435. <p>For boolean (on/off) options, convention is that this example
  16436. should be the <em>opposite</em> to the default (so the comment will end
  16437. with &quot;Uncomment the following to enable [or disable]
  16438. <feature>.&quot; For other options, the example should give some
  16439. non-default value which is likely to be useful to the reader.</p>
  16440. </li>
  16441. </ul>
  16442. </li>
  16443. <li>
  16444. <p>There should be a blank line between each option.</p>
  16445. </li>
  16446. <li>
  16447. <p>Where several settings are grouped into a single dict, <em>avoid</em> the
  16448. convention where the whole block is commented out, resulting in
  16449. comment lines starting <code># #</code>, as this is hard to read and confusing
  16450. to edit. Instead, leave the top-level config option uncommented, and
  16451. follow the conventions above for sub-options. Ensure that your code
  16452. correctly handles the top-level option being set to <code>None</code> (as it
  16453. will be if no sub-options are enabled).</p>
  16454. </li>
  16455. <li>
  16456. <p>Lines should be wrapped at 80 characters.</p>
  16457. </li>
  16458. <li>
  16459. <p>Use two-space indents.</p>
  16460. </li>
  16461. <li>
  16462. <p><code>true</code> and <code>false</code> are spelt thus (as opposed to <code>True</code>, etc.)</p>
  16463. </li>
  16464. <li>
  16465. <p>Use single quotes (<code>'</code>) rather than double-quotes (<code>&quot;</code>) or backticks
  16466. (<code>`</code>) to refer to configuration options.</p>
  16467. </li>
  16468. </ul>
  16469. <p>Example:</p>
  16470. <pre><code class="language-yaml">## Frobnication ##
  16471. # The frobnicator will ensure that all requests are fully frobnicated.
  16472. # To enable it, uncomment the following.
  16473. #
  16474. #frobnicator_enabled: true
  16475. # By default, the frobnicator will frobnicate with the default frobber.
  16476. # The following will make it use an alternative frobber.
  16477. #
  16478. #frobincator_frobber: special_frobber
  16479. # Settings for the frobber
  16480. #
  16481. frobber:
  16482. # frobbing speed. Defaults to 1.
  16483. #
  16484. #speed: 10
  16485. # frobbing distance. Defaults to 1000.
  16486. #
  16487. #distance: 100
  16488. </code></pre>
  16489. <p>Note that the sample configuration is generated from the synapse code
  16490. and is maintained by a script, <code>scripts-dev/generate_sample_config.sh</code>.
  16491. Making sure that the output from this script matches the desired format
  16492. is left as an exercise for the reader!</p>
  16493. <div style="break-before: page; page-break-before: always;"></div><h1 id="synapse-release-cycle"><a class="header" href="#synapse-release-cycle">Synapse Release Cycle</a></h1>
  16494. <p>Releases of Synapse follow a two week release cycle with new releases usually
  16495. occurring on Tuesdays:</p>
  16496. <ul>
  16497. <li>Day 0: Synapse <code>N - 1</code> is released.</li>
  16498. <li>Day 7: Synapse <code>N</code> release candidate 1 is released.</li>
  16499. <li>Days 7 - 13: Synapse <code>N</code> release candidates 2+ are released, if bugs are found.</li>
  16500. <li>Day 14: Synapse <code>N</code> is released.</li>
  16501. </ul>
  16502. <p>Note that this schedule might be modified depending on the availability of the
  16503. Synapse team, e.g. releases may be skipped to avoid holidays.</p>
  16504. <p>Release announcements can be found in the
  16505. <a href="https://matrix.org/blog/category/releases">release category of the Matrix blog</a>.</p>
  16506. <h2 id="bugfix-releases"><a class="header" href="#bugfix-releases">Bugfix releases</a></h2>
  16507. <p>If a bug is found after release that is deemed severe enough (by a combination
  16508. of the impacted users and the impact on those users) then a bugfix release may
  16509. be issued. This may be at any point in the release cycle.</p>
  16510. <h2 id="security-releases"><a class="header" href="#security-releases">Security releases</a></h2>
  16511. <p>Security will sometimes be backported to the previous version and released
  16512. immediately before the next release candidate. An example of this might be:</p>
  16513. <ul>
  16514. <li>Day 0: Synapse N - 1 is released.</li>
  16515. <li>Day 7: Synapse (N - 1).1 is released as Synapse N - 1 + the security fix.</li>
  16516. <li>Day 7: Synapse N release candidate 1 is released (including the security fix).</li>
  16517. </ul>
  16518. <p>Depending on the impact and complexity of security fixes, multiple fixes might
  16519. be held to be released together.</p>
  16520. <p>In some cases, a pre-disclosure of a security release will be issued as a notice
  16521. to Synapse operators that there is an upcoming security release. These can be
  16522. found in the <a href="https://matrix.org/blog/category/security">security category of the Matrix blog</a>.</p>
  16523. <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>
  16524. <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>
  16525. <p>In an ideal world, our git commit history would be a linear progression of
  16526. commits each of which contains a single change building on what came
  16527. before. Here, by way of an arbitrary example, is the top of <code>git log --graph b2dba0607</code>:</p>
  16528. <img src="development/img/git/clean.png" alt="clean git graph" width="500px">
  16529. <p>Note how the commit comment explains clearly what is changing and why. Also
  16530. note the <em>absence</em> of merge commits, as well as the absence of commits called
  16531. things like (to pick a few culprits):
  16532. <a href="https://github.com/matrix-org/synapse/commit/84691da6c">“pep8”</a>, <a href="https://github.com/matrix-org/synapse/commit/474810d9d">“fix broken
  16533. test”</a>,
  16534. <a href="https://github.com/matrix-org/synapse/commit/c9d72e457">“oops”</a>,
  16535. <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
  16536. the president?”</a>.</p>
  16537. <p>There are a number of reasons why keeping a clean commit history is a good
  16538. thing:</p>
  16539. <ul>
  16540. <li>
  16541. <p>From time to time, after a change lands, it turns out to be necessary to
  16542. revert it, or to backport it to a release branch. Those operations are
  16543. <em>much</em> easier when the change is contained in a single commit.</p>
  16544. </li>
  16545. <li>
  16546. <p>Similarly, it's much easier to answer questions like “is the fix for
  16547. <code>/publicRooms</code> on the release branch?” if that change consists of a single
  16548. commit.</p>
  16549. </li>
  16550. <li>
  16551. <p>Likewise: “what has changed on this branch in the last week?” is much
  16552. clearer without merges and “pep8” commits everywhere.</p>
  16553. </li>
  16554. <li>
  16555. <p>Sometimes we need to figure out where a bug got introduced, or some
  16556. behaviour changed. One way of doing that is with <code>git bisect</code>: pick an
  16557. arbitrary commit between the known good point and the known bad point, and
  16558. see how the code behaves. However, that strategy fails if the commit you
  16559. chose is the middle of someone's epic branch in which they broke the world
  16560. before putting it back together again.</p>
  16561. </li>
  16562. </ul>
  16563. <p>One counterargument is that it is sometimes useful to see how a PR evolved as
  16564. it went through review cycles. This is true, but that information is always
  16565. 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
  16566. namespace</a>).</p>
  16567. <p>Of course, in reality, things are more complicated than that. We have release
  16568. branches as well as <code>develop</code> and <code>master</code>, and we deliberately merge changes
  16569. between them. Bugs often slip through and have to be fixed later. That's all
  16570. fine: this not a cast-iron rule which must be obeyed, but an ideal to aim
  16571. towards.</p>
  16572. <h2 id="merges-squashes-rebases-wtf"><a class="header" href="#merges-squashes-rebases-wtf">Merges, squashes, rebases: wtf?</a></h2>
  16573. <p>Ok, so that's what we'd like to achieve. How do we achieve it?</p>
  16574. <p>The TL;DR is: when you come to merge a pull request, you <em>probably</em> want to
  16575. “squash and merge”:</p>
  16576. <p><img src="development/img/git/squash.png" alt="squash and merge" />.</p>
  16577. <p>(This applies whether you are merging your own PR, or that of another
  16578. contributor.)</p>
  16579. <p>“Squash and merge”<sup id="a1"><a href="development/git.html#f1">1</a></sup> takes all of the changes in the
  16580. PR, and bundles them into a single commit. GitHub gives you the opportunity to
  16581. edit the commit message before you confirm, and normally you should do so,
  16582. because the default will be useless (again: <code>* woops typo</code> is not a useful
  16583. thing to keep in the historical record).</p>
  16584. <p>The main problem with this approach comes when you have a series of pull
  16585. requests which build on top of one another: as soon as you squash-merge the
  16586. first PR, you'll end up with a stack of conflicts to resolve in all of the
  16587. others. In general, it's best to avoid this situation in the first place by
  16588. trying not to have multiple related PRs in flight at the same time. Still,
  16589. sometimes that's not possible and doing a regular merge is the lesser evil.</p>
  16590. <p>Another occasion in which a regular merge makes more sense is a PR where you've
  16591. deliberately created a series of commits each of which makes sense in its own
  16592. right. For example: <a href="https://github.com/matrix-org/synapse/pull/6837">a PR which gradually propagates a refactoring operation
  16593. through the codebase</a>, or <a href="https://github.com/matrix-org/synapse/pull/5987">a
  16594. PR which is the culmination of several other
  16595. PRs</a>. In this case the ability
  16596. to figure out when a particular change/bug was introduced could be very useful.</p>
  16597. <p>Ultimately: <strong>this is not a hard-and-fast-rule</strong>. If in doubt, ask yourself “do
  16598. each of the commits I am about to merge make sense in their own right”, but
  16599. remember that we're just doing our best to balance “keeping the commit history
  16600. clean” with other factors.</p>
  16601. <h2 id="git-branching-model"><a class="header" href="#git-branching-model">Git branching model</a></h2>
  16602. <p>A <a href="https://nvie.com/posts/a-successful-git-branching-model/">lot</a>
  16603. <a href="http://scottchacon.com/2011/08/31/github-flow.html">of</a>
  16604. <a href="https://www.endoflineblog.com/gitflow-considered-harmful">words</a> have been
  16605. written in the past about git branching models (no really, <a href="https://martinfowler.com/articles/branching-patterns.html">a
  16606. lot</a>). I tend to
  16607. think the whole thing is overblown. Fundamentally, it's not that
  16608. complicated. Here's how we do it.</p>
  16609. <p>Let's start with a picture:</p>
  16610. <p><img src="development/img/git/branches.jpg" alt="branching model" /></p>
  16611. <p>It looks complicated, but it's really not. There's one basic rule: <em>anyone</em> is
  16612. free to merge from <em>any</em> more-stable branch to <em>any</em> less-stable branch at
  16613. <em>any</em> time<sup id="a2"><a href="development/git.html#f2">2</a></sup>. (The principle behind this is that if a
  16614. change is good enough for the more-stable branch, then it's also good enough go
  16615. put in a less-stable branch.)</p>
  16616. <p>Meanwhile, merging (or squashing, as per the above) from a less-stable to a
  16617. more-stable branch is a deliberate action in which you want to publish a change
  16618. or a set of changes to (some subset of) the world: for example, this happens
  16619. when a PR is landed, or as part of our release process.</p>
  16620. <p>So, what counts as a more- or less-stable branch? A little reflection will show
  16621. that our active branches are ordered thus, from more-stable to less-stable:</p>
  16622. <ul>
  16623. <li><code>master</code> (tracks our last release).</li>
  16624. <li><code>release-vX.Y</code> (the branch where we prepare the next release)<sup
  16625. id="a3"><a href="development/git.html#f3">3</a></sup>.</li>
  16626. <li>PR branches which are targeting the release.</li>
  16627. <li><code>develop</code> (our &quot;mainline&quot; branch containing our bleeding-edge).</li>
  16628. <li>regular PR branches.</li>
  16629. </ul>
  16630. <p>The corollary is: if you have a bugfix that needs to land in both
  16631. <code>release-vX.Y</code> <em>and</em> <code>develop</code>, then you should base your PR on
  16632. <code>release-vX.Y</code>, get it merged there, and then merge from <code>release-vX.Y</code> to
  16633. <code>develop</code>. (If a fix lands in <code>develop</code> and we later need it in a
  16634. release-branch, we can of course cherry-pick it, but landing it in the release
  16635. branch first helps reduce the chance of annoying conflicts.)</p>
  16636. <hr />
  16637. <p><b id="f1">[1]</b>: “Squash and merge” is GitHub's term for this
  16638. operation. Given that there is no merge involved, I'm not convinced it's the
  16639. most intuitive name. <a href="development/git.html#a1">^</a></p>
  16640. <p><b id="f2">[2]</b>: Well, anyone with commit access.<a href="development/git.html#a2">^</a></p>
  16641. <p><b id="f3">[3]</b>: Very, very occasionally (I think this has happened once in
  16642. the history of Synapse), we've had two releases in flight at once. Obviously,
  16643. <code>release-v1.2</code> is more-stable than <code>release-v1.3</code>. <a href="development/git.html#a3">^</a></p>
  16644. <div style="break-before: page; page-break-before: always;"></div><h1 id="synapse-demo-setup"><a class="header" href="#synapse-demo-setup">Synapse demo setup</a></h1>
  16645. <p><strong>DO NOT USE THESE DEMO SERVERS IN PRODUCTION</strong></p>
  16646. <p>Requires you to have a <a href="https://matrix-org.github.io/synapse/develop/development/contributing_guide.html#4-install-the-dependencies">Synapse development environment setup</a>.</p>
  16647. <p>The demo setup allows running three federation Synapse servers, with server
  16648. names <code>localhost:8480</code>, <code>localhost:8481</code>, and <code>localhost:8482</code>.</p>
  16649. <p>You can access them via any Matrix client over HTTP at <code>localhost:8080</code>,
  16650. <code>localhost:8081</code>, and <code>localhost:8082</code> or over HTTPS at <code>localhost:8480</code>,
  16651. <code>localhost:8481</code>, and <code>localhost:8482</code>.</p>
  16652. <p>To enable the servers to communicate, self-signed SSL certificates are generated
  16653. and the servers are configured in a highly insecure way, including:</p>
  16654. <ul>
  16655. <li>Not checking certificates over federation.</li>
  16656. <li>Not verifying keys.</li>
  16657. </ul>
  16658. <p>The servers are configured to store their data under <code>demo/8080</code>, <code>demo/8081</code>, and
  16659. <code>demo/8082</code>. This includes configuration, logs, SQLite databases, and media.</p>
  16660. <p>Note that when joining a public room on a different homeserver via &quot;#foo:bar.net&quot;,
  16661. then you are (in the current implementation) joining a room with room_id &quot;foo&quot;.
  16662. This means that it won't work if your homeserver already has a room with that
  16663. name.</p>
  16664. <h2 id="using-the-demo-scripts"><a class="header" href="#using-the-demo-scripts">Using the demo scripts</a></h2>
  16665. <p>There's three main scripts with straightforward purposes:</p>
  16666. <ul>
  16667. <li><code>start.sh</code> will start the Synapse servers, generating any missing configuration.
  16668. <ul>
  16669. <li>This accepts a single parameter <code>--no-rate-limit</code> to &quot;disable&quot; rate limits
  16670. (they actually still exist, but are very high).</li>
  16671. </ul>
  16672. </li>
  16673. <li><code>stop.sh</code> will stop the Synapse servers.</li>
  16674. <li><code>clean.sh</code> will delete the configuration, databases, log files, etc.</li>
  16675. </ul>
  16676. <p>To start a completely new set of servers, run:</p>
  16677. <pre><code class="language-sh">./demo/stop.sh; ./demo/clean.sh &amp;&amp; ./demo/start.sh
  16678. </code></pre>
  16679. <div style="break-before: page; page-break-before: always;"></div><h1 id="opentracing-1"><a class="header" href="#opentracing-1">OpenTracing</a></h1>
  16680. <h2 id="background"><a class="header" href="#background">Background</a></h2>
  16681. <p>OpenTracing is a semi-standard being adopted by a number of distributed
  16682. tracing platforms. It is a common api for facilitating vendor-agnostic
  16683. tracing instrumentation. That is, we can use the OpenTracing api and
  16684. select one of a number of tracer implementations to do the heavy lifting
  16685. in the background. Our current selected implementation is Jaeger.</p>
  16686. <p>OpenTracing is a tool which gives an insight into the causal
  16687. relationship of work done in and between servers. The servers each track
  16688. events and report them to a centralised server - in Synapse's case:
  16689. Jaeger. The basic unit used to represent events is the span. The span
  16690. roughly represents a single piece of work that was done and the time at
  16691. which it occurred. A span can have child spans, meaning that the work of
  16692. the child had to be completed for the parent span to complete, or it can
  16693. have follow-on spans which represent work that is undertaken as a result
  16694. of the parent but is not depended on by the parent to in order to
  16695. finish.</p>
  16696. <p>Since this is undertaken in a distributed environment a request to
  16697. another server, such as an RPC or a simple GET, can be considered a span
  16698. (a unit or work) for the local server. This causal link is what
  16699. OpenTracing aims to capture and visualise. In order to do this metadata
  16700. about the local server's span, i.e the 'span context', needs to be
  16701. included with the request to the remote.</p>
  16702. <p>It is up to the remote server to decide what it does with the spans it
  16703. creates. This is called the sampling policy and it can be configured
  16704. through Jaeger's settings.</p>
  16705. <p>For OpenTracing concepts see
  16706. <a href="https://opentracing.io/docs/overview/what-is-tracing/">https://opentracing.io/docs/overview/what-is-tracing/</a>.</p>
  16707. <p>For more information about Jaeger's implementation see
  16708. <a href="https://www.jaegertracing.io/docs/">https://www.jaegertracing.io/docs/</a></p>
  16709. <h2 id="setting-up-opentracing"><a class="header" href="#setting-up-opentracing">Setting up OpenTracing</a></h2>
  16710. <p>To receive OpenTracing spans, start up a Jaeger server. This can be done
  16711. using docker like so:</p>
  16712. <pre><code class="language-sh">docker run -d --name jaeger \
  16713. -p 6831:6831/udp \
  16714. -p 6832:6832/udp \
  16715. -p 5778:5778 \
  16716. -p 16686:16686 \
  16717. -p 14268:14268 \
  16718. jaegertracing/all-in-one:1
  16719. </code></pre>
  16720. <p>Latest documentation is probably at
  16721. https://www.jaegertracing.io/docs/latest/getting-started.</p>
  16722. <h2 id="enable-opentracing-in-synapse"><a class="header" href="#enable-opentracing-in-synapse">Enable OpenTracing in Synapse</a></h2>
  16723. <p>OpenTracing is not enabled by default. It must be enabled in the
  16724. homeserver config by uncommenting the config options under <code>opentracing</code>
  16725. as shown in the <a href="./sample_config.yaml">sample config</a>. For example:</p>
  16726. <pre><code class="language-yaml">opentracing:
  16727. enabled: true
  16728. homeserver_whitelist:
  16729. - &quot;mytrustedhomeserver.org&quot;
  16730. - &quot;*.myotherhomeservers.com&quot;
  16731. </code></pre>
  16732. <h2 id="homeserver-whitelisting"><a class="header" href="#homeserver-whitelisting">Homeserver whitelisting</a></h2>
  16733. <p>The homeserver whitelist is configured using regular expressions. A list
  16734. of regular expressions can be given and their union will be compared
  16735. when propagating any spans contexts to another homeserver.</p>
  16736. <p>Though it's mostly safe to send and receive span contexts to and from
  16737. untrusted users since span contexts are usually opaque ids it can lead
  16738. to two problems, namely:</p>
  16739. <ul>
  16740. <li>If the span context is marked as sampled by the sending homeserver
  16741. the receiver will sample it. Therefore two homeservers with wildly
  16742. different sampling policies could incur higher sampling counts than
  16743. intended.</li>
  16744. <li>Sending servers can attach arbitrary data to spans, known as
  16745. 'baggage'. For safety this has been disabled in Synapse but that
  16746. doesn't prevent another server sending you baggage which will be
  16747. logged to OpenTracing's logs.</li>
  16748. </ul>
  16749. <h2 id="configuring-jaeger"><a class="header" href="#configuring-jaeger">Configuring Jaeger</a></h2>
  16750. <p>Sampling strategies can be set as in this document:
  16751. <a href="https://www.jaegertracing.io/docs/latest/sampling/">https://www.jaegertracing.io/docs/latest/sampling/</a>.</p>
  16752. <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>
  16753. <p>Synapse's database schema is stored in the <code>synapse.storage.schema</code> module.</p>
  16754. <h2 id="logical-databases"><a class="header" href="#logical-databases">Logical databases</a></h2>
  16755. <p>Synapse supports splitting its datastore across multiple physical databases (which can
  16756. be useful for large installations), and the schema files are therefore split according
  16757. to the logical database they apply to.</p>
  16758. <p>At the time of writing, the following &quot;logical&quot; databases are supported:</p>
  16759. <ul>
  16760. <li><code>state</code> - used to store Matrix room state (more specifically, <code>state_groups</code>,
  16761. their relationships and contents).</li>
  16762. <li><code>main</code> - stores everything else.</li>
  16763. </ul>
  16764. <p>Additionally, the <code>common</code> directory contains schema files for tables which must be
  16765. present on <em>all</em> physical databases.</p>
  16766. <h2 id="synapse-schema-versions"><a class="header" href="#synapse-schema-versions">Synapse schema versions</a></h2>
  16767. <p>Synapse manages its database schema via &quot;schema versions&quot;. These are mainly used to
  16768. help avoid confusion if the Synapse codebase is rolled back after the database is
  16769. updated. They work as follows:</p>
  16770. <ul>
  16771. <li>
  16772. <p>The Synapse codebase defines a constant <code>synapse.storage.schema.SCHEMA_VERSION</code>
  16773. which represents the expectations made about the database by that version. For
  16774. example, as of Synapse v1.36, this is <code>59</code>.</p>
  16775. </li>
  16776. <li>
  16777. <p>The database stores a &quot;compatibility version&quot; in
  16778. <code>schema_compat_version.compat_version</code> which defines the <code>SCHEMA_VERSION</code> of the
  16779. oldest version of Synapse which will work with the database. On startup, if
  16780. <code>compat_version</code> is found to be newer than <code>SCHEMA_VERSION</code>, Synapse will refuse to
  16781. start.</p>
  16782. <p>Synapse automatically updates this field from
  16783. <code>synapse.storage.schema.SCHEMA_COMPAT_VERSION</code>.</p>
  16784. </li>
  16785. <li>
  16786. <p>Whenever a backwards-incompatible change is made to the database format (normally
  16787. via a <code>delta</code> file), <code>synapse.storage.schema.SCHEMA_COMPAT_VERSION</code> is also updated
  16788. so that administrators can not accidentally roll back to a too-old version of Synapse.</p>
  16789. </li>
  16790. </ul>
  16791. <p>Generally, the goal is to maintain compatibility with at least one or two previous
  16792. releases of Synapse, so any substantial change tends to require multiple releases and a
  16793. bit of forward-planning to get right.</p>
  16794. <p>As a worked example: we want to remove the <code>room_stats_historical</code> table. Here is how it
  16795. might pan out.</p>
  16796. <ol>
  16797. <li>
  16798. <p>Replace any code that <em>reads</em> from <code>room_stats_historical</code> with alternative
  16799. implementations, but keep writing to it in case of rollback to an earlier version.
  16800. Also, increase <code>synapse.storage.schema.SCHEMA_VERSION</code>. In this
  16801. instance, there is no existing code which reads from <code>room_stats_historical</code>, so
  16802. our starting point is:</p>
  16803. <p>v1.36.0: <code>SCHEMA_VERSION=59</code>, <code>SCHEMA_COMPAT_VERSION=59</code></p>
  16804. </li>
  16805. <li>
  16806. <p>Next (say in Synapse v1.37.0): remove the code that <em>writes</em> to
  16807. <code>room_stats_historical</code>, but don’t yet remove the table in case of rollback to
  16808. v1.36.0. Again, we increase <code>synapse.storage.schema.SCHEMA_VERSION</code>, but
  16809. because we have not broken compatibility with v1.36, we do not yet update
  16810. <code>SCHEMA_COMPAT_VERSION</code>. We now have:</p>
  16811. <p>v1.37.0: <code>SCHEMA_VERSION=60</code>, <code>SCHEMA_COMPAT_VERSION=59</code>.</p>
  16812. </li>
  16813. <li>
  16814. <p>Later (say in Synapse v1.38.0): we can remove the table altogether. This will
  16815. break compatibility with v1.36.0, so we must update <code>SCHEMA_COMPAT_VERSION</code> accordingly.
  16816. There is no need to update <code>synapse.storage.schema.SCHEMA_VERSION</code>, since there is no
  16817. change to the Synapse codebase here. So we end up with:</p>
  16818. <p>v1.38.0: <code>SCHEMA_VERSION=60</code>, <code>SCHEMA_COMPAT_VERSION=60</code>.</p>
  16819. </li>
  16820. </ol>
  16821. <p>If in doubt about whether to update <code>SCHEMA_VERSION</code> or not, it is generally best to
  16822. lean towards doing so.</p>
  16823. <h2 id="full-schema-dumps"><a class="header" href="#full-schema-dumps">Full schema dumps</a></h2>
  16824. <p>In the <code>full_schemas</code> directories, only the most recently-numbered snapshot is used
  16825. (<code>54</code> at the time of writing). Older snapshots (eg, <code>16</code>) are present for historical
  16826. reference only.</p>
  16827. <h3 id="building-full-schema-dumps"><a class="header" href="#building-full-schema-dumps">Building full schema dumps</a></h3>
  16828. <p>If you want to recreate these schemas, they need to be made from a database that
  16829. has had all background updates run.</p>
  16830. <p>To do so, use <code>scripts-dev/make_full_schema.sh</code>. This will produce new
  16831. <code>full.sql.postgres</code> and <code>full.sql.sqlite</code> files.</p>
  16832. <p>Ensure postgres is installed, then run:</p>
  16833. <pre><code class="language-sh">./scripts-dev/make_full_schema.sh -p postgres_username -o output_dir/
  16834. </code></pre>
  16835. <p>NB at the time of writing, this script predates the split into separate <code>state</code>/<code>main</code>
  16836. databases so will require updates to handle that correctly.</p>
  16837. <h2 id="delta-files"><a class="header" href="#delta-files">Delta files</a></h2>
  16838. <p>Delta files define the steps required to upgrade the database from an earlier version.
  16839. They can be written as either a file containing a series of SQL statements, or a Python
  16840. module.</p>
  16841. <p>Synapse remembers which delta files it has applied to a database (they are stored in the
  16842. <code>applied_schema_deltas</code> table) and will not re-apply them (even if a given file is
  16843. subsequently updated).</p>
  16844. <p>Delta files should be placed in a directory named <code>synapse/storage/schema/&lt;database&gt;/delta/&lt;version&gt;/</code>.
  16845. They are applied in alphanumeric order, so by convention the first two characters
  16846. of the filename should be an integer such as <code>01</code>, to put the file in the right order.</p>
  16847. <h3 id="sql-delta-files"><a class="header" href="#sql-delta-files">SQL delta files</a></h3>
  16848. <p>These should be named <code>*.sql</code>, or — for changes which should only be applied for a
  16849. given database engine — <code>*.sql.posgres</code> or <code>*.sql.sqlite</code>. For example, a delta which
  16850. adds a new column to the <code>foo</code> table might be called <code>01add_bar_to_foo.sql</code>.</p>
  16851. <p>Note that our SQL parser is a bit simple - it understands comments (<code>--</code> and <code>/*...*/</code>),
  16852. but complex statements which require a <code>;</code> in the middle of them (such as <code>CREATE TRIGGER</code>) are beyond it and you'll have to use a Python delta file.</p>
  16853. <h3 id="python-delta-files"><a class="header" href="#python-delta-files">Python delta files</a></h3>
  16854. <p>For more flexibility, a delta file can take the form of a python module. These should
  16855. be named <code>*.py</code>. Note that database-engine-specific modules are not supported here –
  16856. instead you can write <code>if isinstance(database_engine, PostgresEngine)</code> or similar.</p>
  16857. <p>A Python delta module should define either or both of the following functions:</p>
  16858. <pre><code class="language-python">import synapse.config.homeserver
  16859. import synapse.storage.engines
  16860. import synapse.storage.types
  16861. def run_create(
  16862. cur: synapse.storage.types.Cursor,
  16863. database_engine: synapse.storage.engines.BaseDatabaseEngine,
  16864. ) -&gt; None:
  16865. &quot;&quot;&quot;Called whenever an existing or new database is to be upgraded&quot;&quot;&quot;
  16866. ...
  16867. def run_upgrade(
  16868. cur: synapse.storage.types.Cursor,
  16869. database_engine: synapse.storage.engines.BaseDatabaseEngine,
  16870. config: synapse.config.homeserver.HomeServerConfig,
  16871. ) -&gt; None:
  16872. &quot;&quot;&quot;Called whenever an existing database is to be upgraded.&quot;&quot;&quot;
  16873. ...
  16874. </code></pre>
  16875. <h2 id="boolean-columns"><a class="header" href="#boolean-columns">Boolean columns</a></h2>
  16876. <p>Boolean columns require special treatment, since SQLite treats booleans the
  16877. same as integers.</p>
  16878. <p>There are three separate aspects to this:</p>
  16879. <ul>
  16880. <li>
  16881. <p>Any new boolean column must be added to the <code>BOOLEAN_COLUMNS</code> list in
  16882. <code>synapse/_scripts/synapse_port_db.py</code>. This tells the port script to cast
  16883. the integer value from SQLite to a boolean before writing the value to the
  16884. postgres database.</p>
  16885. </li>
  16886. <li>
  16887. <p>Before SQLite 3.23, <code>TRUE</code> and <code>FALSE</code> were not recognised as constants by
  16888. SQLite, and the <code>IS [NOT] TRUE</code>/<code>IS [NOT] FALSE</code> operators were not
  16889. supported. This makes it necessary to avoid using <code>TRUE</code> and <code>FALSE</code>
  16890. constants in SQL commands.</p>
  16891. <p>For example, to insert a <code>TRUE</code> value into the database, write:</p>
  16892. <pre><code class="language-python">txn.execute(&quot;INSERT INTO tbl(col) VALUES (?)&quot;, (True, ))
  16893. </code></pre>
  16894. </li>
  16895. <li>
  16896. <p>Default values for new boolean columns present a particular
  16897. difficulty. Generally it is best to create separate schema files for
  16898. Postgres and SQLite. For example:</p>
  16899. <pre><code class="language-sql"># in 00delta.sql.postgres:
  16900. ALTER TABLE tbl ADD COLUMN col BOOLEAN DEFAULT FALSE;
  16901. </code></pre>
  16902. <pre><code class="language-sql"># in 00delta.sql.sqlite:
  16903. ALTER TABLE tbl ADD COLUMN col BOOLEAN DEFAULT 0;
  16904. </code></pre>
  16905. <p>Note that there is a particularly insidious failure mode here: the Postgres
  16906. flavour will be accepted by SQLite 3.22, but will give a column whose
  16907. default value is the <strong>string</strong> <code>&quot;FALSE&quot;</code> - which, when cast back to a boolean
  16908. in Python, evaluates to <code>True</code>.</p>
  16909. </li>
  16910. </ul>
  16911. <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>
  16912. <p>It can be desirable to implement &quot;experimental&quot; features which are disabled by
  16913. default and must be explicitly enabled via the Synapse configuration. This is
  16914. applicable for features which:</p>
  16915. <ul>
  16916. <li>Are unstable in the Matrix spec (e.g. those defined by an MSC that has not yet been merged).</li>
  16917. <li>Developers are not confident in their use by general Synapse administrators/users
  16918. (e.g. a feature is incomplete, buggy, performs poorly, or needs further testing).</li>
  16919. </ul>
  16920. <p>Note that this only really applies to features which are expected to be desirable
  16921. to a broad audience. The <a href="development/../modules/index.html">module infrastructure</a> should
  16922. instead be investigated for non-standard features.</p>
  16923. <p>Guarding experimental features behind configuration flags should help with some
  16924. of the following scenarios:</p>
  16925. <ul>
  16926. <li>Ensure that clients do not assume that unstable features exist (failing
  16927. gracefully if they do not).</li>
  16928. <li>Unstable features do not become de-facto standards and can be removed
  16929. aggressively (since only those who have opted-in will be affected).</li>
  16930. <li>Ease finding the implementation of unstable features in Synapse (for future
  16931. removal or stabilization).</li>
  16932. <li>Ease testing a feature (or removal of feature) due to enabling/disabling without
  16933. code changes. It also becomes possible to ask for wider testing, if desired.</li>
  16934. </ul>
  16935. <p>Experimental configuration flags should be disabled by default (requiring Synapse
  16936. administrators to explicitly opt-in), although there are situations where it makes
  16937. sense (from a product point-of-view) to enable features by default. This is
  16938. expected and not an issue.</p>
  16939. <p>It is not a requirement for experimental features to be behind a configuration flag,
  16940. but one should be used if unsure.</p>
  16941. <p>New experimental configuration flags should be added under the <code>experimental</code>
  16942. configuration key (see the <code>synapse.config.experimental</code> file) and either explain
  16943. (briefly) what is being enabled, or include the MSC number.</p>
  16944. <div style="break-before: page; page-break-before: always;"></div><h1 id="cancellation"><a class="header" href="#cancellation">Cancellation</a></h1>
  16945. <p>Sometimes, requests take a long time to service and clients disconnect
  16946. before Synapse produces a response. To avoid wasting resources, Synapse
  16947. can cancel request processing for select endpoints marked with the
  16948. <code>@cancellable</code> decorator.</p>
  16949. <p>Synapse makes use of Twisted's <code>Deferred.cancel()</code> feature to make
  16950. cancellation work. The <code>@cancellable</code> decorator does nothing by itself
  16951. and merely acts as a flag, signalling to developers and other code alike
  16952. that a method can be cancelled.</p>
  16953. <h2 id="enabling-cancellation-for-an-endpoint"><a class="header" href="#enabling-cancellation-for-an-endpoint">Enabling cancellation for an endpoint</a></h2>
  16954. <ol>
  16955. <li>Check that the endpoint method, and any <code>async</code> functions in its call
  16956. tree handle cancellation correctly. See
  16957. <a href="development/synapse_architecture/cancellation.html#handling-cancellation-correctly">Handling cancellation correctly</a>
  16958. for a list of things to look out for.</li>
  16959. <li>Add the <code>@cancellable</code> decorator to the <code>on_GET/POST/PUT/DELETE</code>
  16960. method. It's not recommended to make non-<code>GET</code> methods cancellable,
  16961. since cancellation midway through some database updates is less
  16962. likely to be handled correctly.</li>
  16963. </ol>
  16964. <h2 id="mechanics"><a class="header" href="#mechanics">Mechanics</a></h2>
  16965. <p>There are two stages to cancellation: downward propagation of a
  16966. <code>cancel()</code> call, followed by upwards propagation of a <code>CancelledError</code>
  16967. out of a blocked <code>await</code>.
  16968. Both Twisted and asyncio have a cancellation mechanism.</p>
  16969. <table><thead><tr><th></th><th>Method</th><th>Exception</th><th>Exception inherits from</th></tr></thead><tbody>
  16970. <tr><td>Twisted</td><td><code>Deferred.cancel()</code></td><td><code>twisted.internet.defer.CancelledError</code></td><td><code>Exception</code> (!)</td></tr>
  16971. <tr><td>asyncio</td><td><code>Task.cancel()</code></td><td><code>asyncio.CancelledError</code></td><td><code>BaseException</code></td></tr>
  16972. </tbody></table>
  16973. <h3 id="deferredcancel"><a class="header" href="#deferredcancel">Deferred.cancel()</a></h3>
  16974. <p>When Synapse starts handling a request, it runs the async method
  16975. responsible for handling it using <code>defer.ensureDeferred</code>, which returns
  16976. a <code>Deferred</code>. For example:</p>
  16977. <pre><code class="language-python">def do_something() -&gt; Deferred[None]:
  16978. ...
  16979. @cancellable
  16980. async def on_GET() -&gt; Tuple[int, JsonDict]:
  16981. d = make_deferred_yieldable(do_something())
  16982. await d
  16983. return 200, {}
  16984. request = defer.ensureDeferred(on_GET())
  16985. </code></pre>
  16986. <p>When a client disconnects early, Synapse checks for the presence of the
  16987. <code>@cancellable</code> decorator on <code>on_GET</code>. Since <code>on_GET</code> is cancellable,
  16988. <code>Deferred.cancel()</code> is called on the <code>Deferred</code> from
  16989. <code>defer.ensureDeferred</code>, ie. <code>request</code>. Twisted knows which <code>Deferred</code>
  16990. <code>request</code> is waiting on and passes the <code>cancel()</code> call on to <code>d</code>.</p>
  16991. <p>The <code>Deferred</code> being waited on, <code>d</code>, may have its own handling for
  16992. <code>cancel()</code> and pass the call on to other <code>Deferred</code>s.</p>
  16993. <p>Eventually, a <code>Deferred</code> handles the <code>cancel()</code> call by resolving itself
  16994. with a <code>CancelledError</code>.</p>
  16995. <h3 id="cancellederror"><a class="header" href="#cancellederror">CancelledError</a></h3>
  16996. <p>The <code>CancelledError</code> gets raised out of the <code>await</code> and bubbles up, as
  16997. per normal Python exception handling.</p>
  16998. <h2 id="handling-cancellation-correctly"><a class="header" href="#handling-cancellation-correctly">Handling cancellation correctly</a></h2>
  16999. <p>In general, when writing code that might be subject to cancellation, two
  17000. things must be considered:</p>
  17001. <ul>
  17002. <li>The effect of <code>CancelledError</code>s raised out of <code>await</code>s.</li>
  17003. <li>The effect of <code>Deferred</code>s being <code>cancel()</code>ed.</li>
  17004. </ul>
  17005. <p>Examples of code that handles cancellation incorrectly include:</p>
  17006. <ul>
  17007. <li><code>try-except</code> blocks which swallow <code>CancelledError</code>s.</li>
  17008. <li>Code that shares the same <code>Deferred</code>, which may be cancelled, between
  17009. multiple requests.</li>
  17010. <li>Code that starts some processing that's exempt from cancellation, but
  17011. uses a logging context from cancellable code. The logging context
  17012. will be finished upon cancellation, while the uncancelled processing
  17013. is still using it.</li>
  17014. </ul>
  17015. <p>Some common patterns are listed below in more detail.</p>
  17016. <h3 id="async-function-calls"><a class="header" href="#async-function-calls"><code>async</code> function calls</a></h3>
  17017. <p>Most functions in Synapse are relatively straightforward from a
  17018. cancellation standpoint: they don't do anything with <code>Deferred</code>s and
  17019. purely call and <code>await</code> other <code>async</code> functions.</p>
  17020. <p>An <code>async</code> function handles cancellation correctly if its own code
  17021. handles cancellation correctly and all the async function it calls
  17022. handle cancellation correctly. For example:</p>
  17023. <pre><code class="language-python">async def do_two_things() -&gt; None:
  17024. check_something()
  17025. await do_something()
  17026. await do_something_else()
  17027. </code></pre>
  17028. <p><code>do_two_things</code> handles cancellation correctly if <code>do_something</code> and
  17029. <code>do_something_else</code> handle cancellation correctly.</p>
  17030. <p>That is, when checking whether a function handles cancellation
  17031. correctly, its implementation and all its <code>async</code> function calls need to
  17032. be checked, recursively.</p>
  17033. <p>As <code>check_something</code> is not <code>async</code>, it does not need to be checked.</p>
  17034. <h3 id="cancellederrors"><a class="header" href="#cancellederrors">CancelledErrors</a></h3>
  17035. <p>Because Twisted's <code>CancelledError</code>s are <code>Exception</code>s, it's easy to
  17036. accidentally catch and suppress them. Care must be taken to ensure that
  17037. <code>CancelledError</code>s are allowed to propagate upwards.</p>
  17038. <table width="100%">
  17039. <tr>
  17040. <td width="50%" valign="top">
  17041. <p><strong>Bad</strong>:</p>
  17042. <pre><code class="language-python">try:
  17043. await do_something()
  17044. except Exception:
  17045. # `CancelledError` gets swallowed here.
  17046. logger.info(...)
  17047. </code></pre>
  17048. </td>
  17049. <td width="50%" valign="top">
  17050. <p><strong>Good</strong>:</p>
  17051. <pre><code class="language-python">try:
  17052. await do_something()
  17053. except CancelledError:
  17054. raise
  17055. except Exception:
  17056. logger.info(...)
  17057. </code></pre>
  17058. </td>
  17059. </tr>
  17060. <tr>
  17061. <td width="50%" valign="top">
  17062. <p><strong>OK</strong>:</p>
  17063. <pre><code class="language-python">try:
  17064. check_something()
  17065. # A `CancelledError` won't ever be raised here.
  17066. except Exception:
  17067. logger.info(...)
  17068. </code></pre>
  17069. </td>
  17070. <td width="50%" valign="top">
  17071. <p><strong>Good</strong>:</p>
  17072. <pre><code class="language-python">try:
  17073. await do_something()
  17074. except ValueError:
  17075. logger.info(...)
  17076. </code></pre>
  17077. </td>
  17078. </tr>
  17079. </table>
  17080. <h4 id="defergatherresults"><a class="header" href="#defergatherresults">defer.gatherResults</a></h4>
  17081. <p><code>defer.gatherResults</code> produces a <code>Deferred</code> which:</p>
  17082. <ul>
  17083. <li>broadcasts <code>cancel()</code> calls to every <code>Deferred</code> being waited on.</li>
  17084. <li>wraps the first exception it sees in a <code>FirstError</code>.</li>
  17085. </ul>
  17086. <p>Together, this means that <code>CancelledError</code>s will be wrapped in
  17087. a <code>FirstError</code> unless unwrapped. Such <code>FirstError</code>s are liable to be
  17088. swallowed, so they must be unwrapped.</p>
  17089. <table width="100%">
  17090. <tr>
  17091. <td width="50%" valign="top">
  17092. <p><strong>Bad</strong>:</p>
  17093. <pre><code class="language-python">async def do_something() -&gt; None:
  17094. await make_deferred_yieldable(
  17095. defer.gatherResults([...], consumeErrors=True)
  17096. )
  17097. try:
  17098. await do_something()
  17099. except CancelledError:
  17100. raise
  17101. except Exception:
  17102. # `FirstError(CancelledError)` gets swallowed here.
  17103. logger.info(...)
  17104. </code></pre>
  17105. </td>
  17106. <td width="50%" valign="top">
  17107. <p><strong>Good</strong>:</p>
  17108. <pre><code class="language-python">async def do_something() -&gt; None:
  17109. await make_deferred_yieldable(
  17110. defer.gatherResults([...], consumeErrors=True)
  17111. ).addErrback(unwrapFirstError)
  17112. try:
  17113. await do_something()
  17114. except CancelledError:
  17115. raise
  17116. except Exception:
  17117. logger.info(...)
  17118. </code></pre>
  17119. </td>
  17120. </tr>
  17121. </table>
  17122. <h3 id="creation-of-deferreds"><a class="header" href="#creation-of-deferreds">Creation of <code>Deferred</code>s</a></h3>
  17123. <p>If a function creates a <code>Deferred</code>, the effect of cancelling it must be considered. <code>Deferred</code>s that get shared are likely to have unintended behaviour when cancelled.</p>
  17124. <table width="100%">
  17125. <tr>
  17126. <td width="50%" valign="top">
  17127. <p><strong>Bad</strong>:</p>
  17128. <pre><code class="language-python">cache: Dict[str, Deferred[None]] = {}
  17129. def wait_for_room(room_id: str) -&gt; Deferred[None]:
  17130. deferred = cache.get(room_id)
  17131. if deferred is None:
  17132. deferred = Deferred()
  17133. cache[room_id] = deferred
  17134. # `deferred` can have multiple waiters.
  17135. # All of them will observe a `CancelledError`
  17136. # if any one of them is cancelled.
  17137. return make_deferred_yieldable(deferred)
  17138. # Request 1
  17139. await wait_for_room(&quot;!aAAaaAaaaAAAaAaAA:matrix.org&quot;)
  17140. # Request 2
  17141. await wait_for_room(&quot;!aAAaaAaaaAAAaAaAA:matrix.org&quot;)
  17142. </code></pre>
  17143. </td>
  17144. <td width="50%" valign="top">
  17145. <p><strong>Good</strong>:</p>
  17146. <pre><code class="language-python">cache: Dict[str, Deferred[None]] = {}
  17147. def wait_for_room(room_id: str) -&gt; Deferred[None]:
  17148. deferred = cache.get(room_id)
  17149. if deferred is None:
  17150. deferred = Deferred()
  17151. cache[room_id] = deferred
  17152. # `deferred` will never be cancelled now.
  17153. # A `CancelledError` will still come out of
  17154. # the `await`.
  17155. # `delay_cancellation` may also be used.
  17156. return make_deferred_yieldable(stop_cancellation(deferred))
  17157. # Request 1
  17158. await wait_for_room(&quot;!aAAaaAaaaAAAaAaAA:matrix.org&quot;)
  17159. # Request 2
  17160. await wait_for_room(&quot;!aAAaaAaaaAAAaAaAA:matrix.org&quot;)
  17161. </code></pre>
  17162. </td>
  17163. </tr>
  17164. <tr>
  17165. <td width="50%" valign="top">
  17166. </td>
  17167. <td width="50%" valign="top">
  17168. <p><strong>Good</strong>:</p>
  17169. <pre><code class="language-python">cache: Dict[str, List[Deferred[None]]] = {}
  17170. def wait_for_room(room_id: str) -&gt; Deferred[None]:
  17171. if room_id not in cache:
  17172. cache[room_id] = []
  17173. # Each request gets its own `Deferred` to wait on.
  17174. deferred = Deferred()
  17175. cache[room_id]].append(deferred)
  17176. return make_deferred_yieldable(deferred)
  17177. # Request 1
  17178. await wait_for_room(&quot;!aAAaaAaaaAAAaAaAA:matrix.org&quot;)
  17179. # Request 2
  17180. await wait_for_room(&quot;!aAAaaAaaaAAAaAaAA:matrix.org&quot;)
  17181. </code></pre>
  17182. </td>
  17183. </table>
  17184. <h3 id="uncancelled-processing"><a class="header" href="#uncancelled-processing">Uncancelled processing</a></h3>
  17185. <p>Some <code>async</code> functions may kick off some <code>async</code> processing which is
  17186. intentionally protected from cancellation, by <code>stop_cancellation</code> or
  17187. other means. If the <code>async</code> processing inherits the logcontext of the
  17188. request which initiated it, care must be taken to ensure that the
  17189. logcontext is not finished before the <code>async</code> processing completes.</p>
  17190. <table width="100%">
  17191. <tr>
  17192. <td width="50%" valign="top">
  17193. <p><strong>Bad</strong>:</p>
  17194. <pre><code class="language-python">cache: Optional[ObservableDeferred[None]] = None
  17195. async def do_something_else(
  17196. to_resolve: Deferred[None]
  17197. ) -&gt; None:
  17198. await ...
  17199. logger.info(&quot;done!&quot;)
  17200. to_resolve.callback(None)
  17201. async def do_something() -&gt; None:
  17202. if not cache:
  17203. to_resolve = Deferred()
  17204. cache = ObservableDeferred(to_resolve)
  17205. # `do_something_else` will never be cancelled and
  17206. # can outlive the `request-1` logging context.
  17207. run_in_background(do_something_else, to_resolve)
  17208. await make_deferred_yieldable(cache.observe())
  17209. with LoggingContext(&quot;request-1&quot;):
  17210. await do_something()
  17211. </code></pre>
  17212. </td>
  17213. <td width="50%" valign="top">
  17214. <p><strong>Good</strong>:</p>
  17215. <pre><code class="language-python">cache: Optional[ObservableDeferred[None]] = None
  17216. async def do_something_else(
  17217. to_resolve: Deferred[None]
  17218. ) -&gt; None:
  17219. await ...
  17220. logger.info(&quot;done!&quot;)
  17221. to_resolve.callback(None)
  17222. async def do_something() -&gt; None:
  17223. if not cache:
  17224. to_resolve = Deferred()
  17225. cache = ObservableDeferred(to_resolve)
  17226. run_in_background(do_something_else, to_resolve)
  17227. # We'll wait until `do_something_else` is
  17228. # done before raising a `CancelledError`.
  17229. await make_deferred_yieldable(
  17230. delay_cancellation(cache.observe())
  17231. )
  17232. else:
  17233. await make_deferred_yieldable(cache.observe())
  17234. with LoggingContext(&quot;request-1&quot;):
  17235. await do_something()
  17236. </code></pre>
  17237. </td>
  17238. </tr>
  17239. <tr>
  17240. <td width="50%">
  17241. <p><strong>OK</strong>:</p>
  17242. <pre><code class="language-python">cache: Optional[ObservableDeferred[None]] = None
  17243. async def do_something_else(
  17244. to_resolve: Deferred[None]
  17245. ) -&gt; None:
  17246. await ...
  17247. logger.info(&quot;done!&quot;)
  17248. to_resolve.callback(None)
  17249. async def do_something() -&gt; None:
  17250. if not cache:
  17251. to_resolve = Deferred()
  17252. cache = ObservableDeferred(to_resolve)
  17253. # `do_something_else` will get its own independent
  17254. # logging context. `request-1` will not count any
  17255. # metrics from `do_something_else`.
  17256. run_as_background_process(
  17257. &quot;do_something_else&quot;,
  17258. do_something_else,
  17259. to_resolve,
  17260. )
  17261. await make_deferred_yieldable(cache.observe())
  17262. with LoggingContext(&quot;request-1&quot;):
  17263. await do_something()
  17264. </code></pre>
  17265. </td>
  17266. <td width="50%">
  17267. </td>
  17268. </tr>
  17269. </table>
  17270. <div style="break-before: page; page-break-before: always;"></div><h1 id="log-contexts"><a class="header" href="#log-contexts">Log Contexts</a></h1>
  17271. <p>To help track the processing of individual requests, synapse uses a
  17272. '<code>log context</code>' to track which request it is handling at any given
  17273. moment. This is done via a thread-local variable; a <code>logging.Filter</code> is
  17274. then used to fish the information back out of the thread-local variable
  17275. and add it to each log record.</p>
  17276. <p>Logcontexts are also used for CPU and database accounting, so that we
  17277. can track which requests were responsible for high CPU use or database
  17278. activity.</p>
  17279. <p>The <code>synapse.logging.context</code> module provides facilities for managing
  17280. the current log context (as well as providing the <code>LoggingContextFilter</code>
  17281. class).</p>
  17282. <p>Asynchronous functions make the whole thing complicated, so this document describes
  17283. how it all works, and how to write code which follows the rules.</p>
  17284. <p>In this document, &quot;awaitable&quot; refers to any object which can be <code>await</code>ed. In the context of
  17285. Synapse, that normally means either a coroutine or a Twisted
  17286. <a href="https://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferred.html"><code>Deferred</code></a>.</p>
  17287. <h2 id="logcontexts-without-asynchronous-code"><a class="header" href="#logcontexts-without-asynchronous-code">Logcontexts without asynchronous code</a></h2>
  17288. <p>In the absence of any asynchronous voodoo, things are simple enough. As with
  17289. any code of this nature, the rule is that our function should leave
  17290. things as it found them:</p>
  17291. <pre><code class="language-python">from synapse.logging import context # omitted from future snippets
  17292. def handle_request(request_id):
  17293. request_context = context.LoggingContext()
  17294. calling_context = context.set_current_context(request_context)
  17295. try:
  17296. request_context.request = request_id
  17297. do_request_handling()
  17298. logger.debug(&quot;finished&quot;)
  17299. finally:
  17300. context.set_current_context(calling_context)
  17301. def do_request_handling():
  17302. logger.debug(&quot;phew&quot;) # this will be logged against request_id
  17303. </code></pre>
  17304. <p>LoggingContext implements the context management methods, so the above
  17305. can be written much more succinctly as:</p>
  17306. <pre><code class="language-python">def handle_request(request_id):
  17307. with context.LoggingContext() as request_context:
  17308. request_context.request = request_id
  17309. do_request_handling()
  17310. logger.debug(&quot;finished&quot;)
  17311. def do_request_handling():
  17312. logger.debug(&quot;phew&quot;)
  17313. </code></pre>
  17314. <h2 id="using-logcontexts-with-awaitables"><a class="header" href="#using-logcontexts-with-awaitables">Using logcontexts with awaitables</a></h2>
  17315. <p>Awaitables break the linear flow of code so that there is no longer a single entry point
  17316. where we should set the logcontext and a single exit point where we should remove it.</p>
  17317. <p>Consider the example above, where <code>do_request_handling</code> needs to do some
  17318. blocking operation, and returns an awaitable:</p>
  17319. <pre><code class="language-python">async def handle_request(request_id):
  17320. with context.LoggingContext() as request_context:
  17321. request_context.request = request_id
  17322. await do_request_handling()
  17323. logger.debug(&quot;finished&quot;)
  17324. </code></pre>
  17325. <p>In the above flow:</p>
  17326. <ul>
  17327. <li>The logcontext is set</li>
  17328. <li><code>do_request_handling</code> is called, and returns an awaitable</li>
  17329. <li><code>handle_request</code> awaits the awaitable</li>
  17330. <li>Execution of <code>handle_request</code> is suspended</li>
  17331. </ul>
  17332. <p>So we have stopped processing the request (and will probably go on to
  17333. start processing the next), without clearing the logcontext.</p>
  17334. <p>To circumvent this problem, synapse code assumes that, wherever you have
  17335. an awaitable, you will want to <code>await</code> it. To that end, whereever
  17336. functions return awaitables, we adopt the following conventions:</p>
  17337. <p><strong>Rules for functions returning awaitables:</strong></p>
  17338. <blockquote>
  17339. <ul>
  17340. <li>If the awaitable is already complete, the function returns with the
  17341. same logcontext it started with.</li>
  17342. <li>If the awaitable is incomplete, the function clears the logcontext
  17343. before returning; when the awaitable completes, it restores the
  17344. logcontext before running any callbacks.</li>
  17345. </ul>
  17346. </blockquote>
  17347. <p>That sounds complicated, but actually it means a lot of code (including
  17348. the example above) &quot;just works&quot;. There are two cases:</p>
  17349. <ul>
  17350. <li>
  17351. <p>If <code>do_request_handling</code> returns a completed awaitable, then the
  17352. logcontext will still be in place. In this case, execution will
  17353. continue immediately after the <code>await</code>; the &quot;finished&quot; line will
  17354. be logged against the right context, and the <code>with</code> block restores
  17355. the original context before we return to the caller.</p>
  17356. </li>
  17357. <li>
  17358. <p>If the returned awaitable is incomplete, <code>do_request_handling</code> clears
  17359. the logcontext before returning. The logcontext is therefore clear
  17360. when <code>handle_request</code> <code>await</code>s the awaitable.</p>
  17361. <p>Once <code>do_request_handling</code>'s awaitable completes, it will reinstate
  17362. the logcontext, before running the second half of <code>handle_request</code>,
  17363. so again the &quot;finished&quot; line will be logged against the right context,
  17364. and the <code>with</code> block restores the original context.</p>
  17365. </li>
  17366. </ul>
  17367. <p>As an aside, it's worth noting that <code>handle_request</code> follows our rules</p>
  17368. <ul>
  17369. <li>though that only matters if the caller has its own logcontext which it
  17370. cares about.</li>
  17371. </ul>
  17372. <p>The following sections describe pitfalls and helpful patterns when
  17373. implementing these rules.</p>
  17374. <h2 id="always-await-your-awaitables"><a class="header" href="#always-await-your-awaitables">Always await your awaitables</a></h2>
  17375. <p>Whenever you get an awaitable back from a function, you should <code>await</code> on
  17376. it as soon as possible. Do not pass go; do not do any logging; do not
  17377. call any other functions.</p>
  17378. <pre><code class="language-python">async def fun():
  17379. logger.debug(&quot;starting&quot;)
  17380. await do_some_stuff() # just like this
  17381. coro = more_stuff()
  17382. result = await coro # also fine, of course
  17383. return result
  17384. </code></pre>
  17385. <p>Provided this pattern is followed all the way back up to the callchain
  17386. to where the logcontext was set, this will make things work out ok:
  17387. provided <code>do_some_stuff</code> and <code>more_stuff</code> follow the rules above, then
  17388. so will <code>fun</code>.</p>
  17389. <p>It's all too easy to forget to <code>await</code>: for instance if we forgot that
  17390. <code>do_some_stuff</code> returned an awaitable, we might plough on regardless. This
  17391. leads to a mess; it will probably work itself out eventually, but not
  17392. before a load of stuff has been logged against the wrong context.
  17393. (Normally, other things will break, more obviously, if you forget to
  17394. <code>await</code>, so this tends not to be a major problem in practice.)</p>
  17395. <p>Of course sometimes you need to do something a bit fancier with your
  17396. awaitable - not all code follows the linear A-then-B-then-C pattern.
  17397. Notes on implementing more complex patterns are in later sections.</p>
  17398. <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>
  17399. <p>Most of the time, an awaitable comes from another synapse function.
  17400. Sometimes, though, we need to make up a new awaitable, or we get an awaitable
  17401. back from external code. We need to make it follow our rules.</p>
  17402. <p>The easy way to do it is by using <code>context.make_deferred_yieldable</code>. Suppose we want to implement
  17403. <code>sleep</code>, which returns a deferred which will run its callbacks after a
  17404. given number of seconds. That might look like:</p>
  17405. <pre><code class="language-python"># not a logcontext-rules-compliant function
  17406. def get_sleep_deferred(seconds):
  17407. d = defer.Deferred()
  17408. reactor.callLater(seconds, d.callback, None)
  17409. return d
  17410. </code></pre>
  17411. <p>That doesn't follow the rules, but we can fix it by calling it through
  17412. <code>context.make_deferred_yieldable</code>:</p>
  17413. <pre><code class="language-python">async def sleep(seconds):
  17414. return await context.make_deferred_yieldable(get_sleep_deferred(seconds))
  17415. </code></pre>
  17416. <h2 id="fire-and-forget"><a class="header" href="#fire-and-forget">Fire-and-forget</a></h2>
  17417. <p>Sometimes you want to fire off a chain of execution, but not wait for
  17418. its result. That might look a bit like this:</p>
  17419. <pre><code class="language-python">async def do_request_handling():
  17420. await foreground_operation()
  17421. # *don't* do this
  17422. background_operation()
  17423. logger.debug(&quot;Request handling complete&quot;)
  17424. async def background_operation():
  17425. await first_background_step()
  17426. logger.debug(&quot;Completed first step&quot;)
  17427. await second_background_step()
  17428. logger.debug(&quot;Completed second step&quot;)
  17429. </code></pre>
  17430. <p>The above code does a couple of steps in the background after
  17431. <code>do_request_handling</code> has finished. The log lines are still logged
  17432. against the <code>request_context</code> logcontext, which may or may not be
  17433. desirable. There are two big problems with the above, however. The first
  17434. problem is that, if <code>background_operation</code> returns an incomplete
  17435. awaitable, it will expect its caller to <code>await</code> immediately, so will have
  17436. cleared the logcontext. In this example, that means that 'Request
  17437. handling complete' will be logged without any context.</p>
  17438. <p>The second problem, which is potentially even worse, is that when the
  17439. awaitable returned by <code>background_operation</code> completes, it will restore
  17440. the original logcontext. There is nothing waiting on that awaitable, so
  17441. the logcontext will leak into the reactor and possibly get attached to
  17442. some arbitrary future operation.</p>
  17443. <p>There are two potential solutions to this.</p>
  17444. <p>One option is to surround the call to <code>background_operation</code> with a
  17445. <code>PreserveLoggingContext</code> call. That will reset the logcontext before
  17446. starting <code>background_operation</code> (so the context restored when the
  17447. deferred completes will be the empty logcontext), and will restore the
  17448. current logcontext before continuing the foreground process:</p>
  17449. <pre><code class="language-python">async def do_request_handling():
  17450. await foreground_operation()
  17451. # start background_operation off in the empty logcontext, to
  17452. # avoid leaking the current context into the reactor.
  17453. with PreserveLoggingContext():
  17454. background_operation()
  17455. # this will now be logged against the request context
  17456. logger.debug(&quot;Request handling complete&quot;)
  17457. </code></pre>
  17458. <p>Obviously that option means that the operations done in
  17459. <code>background_operation</code> would be not be logged against a logcontext
  17460. (though that might be fixed by setting a different logcontext via a
  17461. <code>with LoggingContext(...)</code> in <code>background_operation</code>).</p>
  17462. <p>The second option is to use <code>context.run_in_background</code>, which wraps a
  17463. function so that it doesn't reset the logcontext even when it returns
  17464. an incomplete awaitable, and adds a callback to the returned awaitable to
  17465. reset the logcontext. In other words, it turns a function that follows
  17466. the Synapse rules about logcontexts and awaitables into one which behaves
  17467. more like an external function --- the opposite operation to that
  17468. described in the previous section. It can be used like this:</p>
  17469. <pre><code class="language-python">async def do_request_handling():
  17470. await foreground_operation()
  17471. context.run_in_background(background_operation)
  17472. # this will now be logged against the request context
  17473. logger.debug(&quot;Request handling complete&quot;)
  17474. </code></pre>
  17475. <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>
  17476. <p>A typical example of this is where we want to collect together two or
  17477. more awaitables via <code>defer.gatherResults</code>:</p>
  17478. <pre><code class="language-python">a1 = operation1()
  17479. a2 = operation2()
  17480. a3 = defer.gatherResults([a1, a2])
  17481. </code></pre>
  17482. <p>This is really a variation of the fire-and-forget problem above, in that
  17483. we are firing off <code>a1</code> and <code>a2</code> without awaiting on them. The difference
  17484. is that we now have third-party code attached to their callbacks. Anyway
  17485. either technique given in the <a href="log_contexts.html#fire-and-forget">Fire-and-forget</a>
  17486. section will work.</p>
  17487. <p>Of course, the new awaitable returned by <code>gather</code> needs to be
  17488. wrapped in order to make it follow the logcontext rules before we can
  17489. 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
  17490. follow the
  17491. rules</a>.</p>
  17492. <p>So, option one: reset the logcontext before starting the operations to
  17493. be gathered:</p>
  17494. <pre><code class="language-python">async def do_request_handling():
  17495. with PreserveLoggingContext():
  17496. a1 = operation1()
  17497. a2 = operation2()
  17498. result = await defer.gatherResults([a1, a2])
  17499. </code></pre>
  17500. <p>In this case particularly, though, option two, of using
  17501. <code>context.run_in_background</code> almost certainly makes more sense, so that
  17502. <code>operation1</code> and <code>operation2</code> are both logged against the original
  17503. logcontext. This looks like:</p>
  17504. <pre><code class="language-python">async def do_request_handling():
  17505. a1 = context.run_in_background(operation1)
  17506. a2 = context.run_in_background(operation2)
  17507. result = await make_deferred_yieldable(defer.gatherResults([a1, a2]))
  17508. </code></pre>
  17509. <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>
  17510. <p>It turns out that our logcontext rules do not play nicely with awaitable
  17511. chains which get orphaned and garbage-collected.</p>
  17512. <p>Imagine we have some code that looks like this:</p>
  17513. <pre><code class="language-python">listener_queue = []
  17514. def on_something_interesting():
  17515. for d in listener_queue:
  17516. d.callback(&quot;foo&quot;)
  17517. async def await_something_interesting():
  17518. new_awaitable = defer.Deferred()
  17519. listener_queue.append(new_awaitable)
  17520. with PreserveLoggingContext():
  17521. await new_awaitable
  17522. </code></pre>
  17523. <p>Obviously, the idea here is that we have a bunch of things which are
  17524. waiting for an event. (It's just an example of the problem here, but a
  17525. relatively common one.)</p>
  17526. <p>Now let's imagine two further things happen. First of all, whatever was
  17527. waiting for the interesting thing goes away. (Perhaps the request times
  17528. out, or something <em>even more</em> interesting happens.)</p>
  17529. <p>Secondly, let's suppose that we decide that the interesting thing is
  17530. never going to happen, and we reset the listener queue:</p>
  17531. <pre><code class="language-python">def reset_listener_queue():
  17532. listener_queue.clear()
  17533. </code></pre>
  17534. <p>So, both ends of the awaitable chain have now dropped their references,
  17535. and the awaitable chain is now orphaned, and will be garbage-collected at
  17536. some point. Note that <code>await_something_interesting</code> is a coroutine,
  17537. which Python implements as a generator function. When Python
  17538. garbage-collects generator functions, it gives them a chance to
  17539. clean up by making the <code>await</code> (or <code>yield</code>) raise a <code>GeneratorExit</code>
  17540. exception. In our case, that means that the <code>__exit__</code> handler of
  17541. <code>PreserveLoggingContext</code> will carefully restore the request context, but
  17542. there is now nothing waiting for its return, so the request context is
  17543. never cleared.</p>
  17544. <p>To reiterate, this problem only arises when <em>both</em> ends of a awaitable
  17545. chain are dropped. Dropping the the reference to an awaitable you're
  17546. supposed to be awaiting is bad practice, so this doesn't
  17547. actually happen too much. Unfortunately, when it does happen, it will
  17548. lead to leaked logcontexts which are incredibly hard to track down.</p>
  17549. <div style="break-before: page; page-break-before: always;"></div><h1 id="replication-architecture"><a class="header" href="#replication-architecture">Replication Architecture</a></h1>
  17550. <h2 id="motivation"><a class="header" href="#motivation">Motivation</a></h2>
  17551. <p>We'd like to be able to split some of the work that synapse does into
  17552. multiple python processes. In theory multiple synapse processes could
  17553. share a single postgresql database and we'd scale up by running more
  17554. synapse processes. However much of synapse assumes that only one process
  17555. is interacting with the database, both for assigning unique identifiers
  17556. when inserting into tables, notifying components about new updates, and
  17557. for invalidating its caches.</p>
  17558. <p>So running multiple copies of the current code isn't an option. One way
  17559. to run multiple processes would be to have a single writer process and
  17560. multiple reader processes connected to the same database. In order to do
  17561. this we'd need a way for the reader process to invalidate its in-memory
  17562. caches when an update happens on the writer. One way to do this is for
  17563. the writer to present an append-only log of updates which the readers
  17564. can consume to invalidate their caches and to push updates to listening
  17565. clients or pushers.</p>
  17566. <p>Synapse already stores much of its data as an append-only log so that it
  17567. can correctly respond to <code>/sync</code> requests so the amount of code changes
  17568. needed to expose the append-only log to the readers should be fairly
  17569. minimal.</p>
  17570. <h2 id="architecture"><a class="header" href="#architecture">Architecture</a></h2>
  17571. <h3 id="the-replication-protocol"><a class="header" href="#the-replication-protocol">The Replication Protocol</a></h3>
  17572. <p>See <a href="tcp_replication.html">the TCP replication documentation</a>.</p>
  17573. <h3 id="the-slaved-datastore"><a class="header" href="#the-slaved-datastore">The Slaved DataStore</a></h3>
  17574. <p>There are read-only version of the synapse storage layer in
  17575. <code>synapse/replication/slave/storage</code> that use the response of the
  17576. replication API to invalidate their caches.</p>
  17577. <h3 id="the-tcp-replication-module"><a class="header" href="#the-tcp-replication-module">The TCP Replication Module</a></h3>
  17578. <p>Information about how the tcp replication module is structured, including how
  17579. the classes interact, can be found in
  17580. <code>synapse/replication/tcp/__init__.py</code></p>
  17581. <div style="break-before: page; page-break-before: always;"></div><h1 id="tcp-replication"><a class="header" href="#tcp-replication">TCP Replication</a></h1>
  17582. <h2 id="motivation-1"><a class="header" href="#motivation-1">Motivation</a></h2>
  17583. <p>Previously the workers used an HTTP long poll mechanism to get updates
  17584. from the master, which had the problem of causing a lot of duplicate
  17585. work on the server. This TCP protocol replaces those APIs with the aim
  17586. of increased efficiency.</p>
  17587. <h2 id="overview-3"><a class="header" href="#overview-3">Overview</a></h2>
  17588. <p>The protocol is based on fire and forget, line based commands. An
  17589. example flow would be (where '&gt;' indicates master to worker and
  17590. '&lt;' worker to master flows):</p>
  17591. <pre><code>&gt; SERVER example.com
  17592. &lt; REPLICATE
  17593. &gt; POSITION events master 53 53
  17594. &gt; RDATA events master 54 [&quot;$foo1:bar.com&quot;, ...]
  17595. &gt; RDATA events master 55 [&quot;$foo4:bar.com&quot;, ...]
  17596. </code></pre>
  17597. <p>The example shows the server accepting a new connection and sending its identity
  17598. with the <code>SERVER</code> command, followed by the client server to respond with the
  17599. position of all streams. The server then periodically sends <code>RDATA</code> commands
  17600. which have the format <code>RDATA &lt;stream_name&gt; &lt;instance_name&gt; &lt;token&gt; &lt;row&gt;</code>, where
  17601. the format of <code>&lt;row&gt;</code> is defined by the individual streams. The
  17602. <code>&lt;instance_name&gt;</code> is the name of the Synapse process that generated the data
  17603. (usually &quot;master&quot;).</p>
  17604. <p>Error reporting happens by either the client or server sending an ERROR
  17605. command, and usually the connection will be closed.</p>
  17606. <p>Since the protocol is a simple line based, its possible to manually
  17607. connect to the server using a tool like netcat. A few things should be
  17608. noted when manually using the protocol:</p>
  17609. <ul>
  17610. <li>The federation stream is only available if federation sending has
  17611. been disabled on the main process.</li>
  17612. <li>The server will only time connections out that have sent a <code>PING</code>
  17613. command. If a ping is sent then the connection will be closed if no
  17614. further commands are receieved within 15s. Both the client and
  17615. server protocol implementations will send an initial PING on
  17616. connection and ensure at least one command every 5s is sent (not
  17617. necessarily <code>PING</code>).</li>
  17618. <li><code>RDATA</code> commands <em>usually</em> include a numeric token, however if the
  17619. stream has multiple rows to replicate per token the server will send
  17620. multiple <code>RDATA</code> commands, with all but the last having a token of
  17621. <code>batch</code>. See the documentation on <code>commands.RdataCommand</code> for
  17622. further details.</li>
  17623. </ul>
  17624. <h2 id="architecture-1"><a class="header" href="#architecture-1">Architecture</a></h2>
  17625. <p>The basic structure of the protocol is line based, where the initial
  17626. word of each line specifies the command. The rest of the line is parsed
  17627. based on the command. For example, the RDATA command is defined as:</p>
  17628. <pre><code>RDATA &lt;stream_name&gt; &lt;instance_name&gt; &lt;token&gt; &lt;row_json&gt;
  17629. </code></pre>
  17630. <p>(Note that &lt;row_json&gt; may contains spaces, but cannot contain
  17631. newlines.)</p>
  17632. <p>Blank lines are ignored.</p>
  17633. <h3 id="keep-alives"><a class="header" href="#keep-alives">Keep alives</a></h3>
  17634. <p>Both sides are expected to send at least one command every 5s or so, and
  17635. should send a <code>PING</code> command if necessary. If either side do not receive
  17636. a command within e.g. 15s then the connection should be closed.</p>
  17637. <p>Because the server may be connected to manually using e.g. netcat, the
  17638. timeouts aren't enabled until an initial <code>PING</code> command is seen. Both
  17639. the client and server implementations below send a <code>PING</code> command
  17640. immediately on connection to ensure the timeouts are enabled.</p>
  17641. <p>This ensures that both sides can quickly realize if the tcp connection
  17642. has gone and handle the situation appropriately.</p>
  17643. <h3 id="start-up"><a class="header" href="#start-up">Start up</a></h3>
  17644. <p>When a new connection is made, the server:</p>
  17645. <ul>
  17646. <li>Sends a <code>SERVER</code> command, which includes the identity of the server,
  17647. allowing the client to detect if its connected to the expected
  17648. server</li>
  17649. <li>Sends a <code>PING</code> command as above, to enable the client to time out
  17650. connections promptly.</li>
  17651. </ul>
  17652. <p>The client:</p>
  17653. <ul>
  17654. <li>Sends a <code>NAME</code> command, allowing the server to associate a human
  17655. friendly name with the connection. This is optional.</li>
  17656. <li>Sends a <code>PING</code> as above</li>
  17657. <li>Sends a <code>REPLICATE</code> to get the current position of all streams.</li>
  17658. <li>On receipt of a <code>SERVER</code> command, checks that the server name
  17659. matches the expected server name.</li>
  17660. </ul>
  17661. <h3 id="error-handling"><a class="header" href="#error-handling">Error handling</a></h3>
  17662. <p>If either side detects an error it can send an <code>ERROR</code> command and close
  17663. the connection.</p>
  17664. <p>If the client side loses the connection to the server it should
  17665. reconnect, following the steps above.</p>
  17666. <h3 id="congestion"><a class="header" href="#congestion">Congestion</a></h3>
  17667. <p>If the server sends messages faster than the client can consume them the
  17668. server will first buffer a (fairly large) number of commands and then
  17669. disconnect the client. This ensures that we don't queue up an unbounded
  17670. number of commands in memory and gives us a potential oppurtunity to
  17671. squawk loudly. When/if the client recovers it can reconnect to the
  17672. server and ask for missed messages.</p>
  17673. <h3 id="reliability"><a class="header" href="#reliability">Reliability</a></h3>
  17674. <p>In general the replication stream should be considered an unreliable
  17675. transport since e.g. commands are not resent if the connection
  17676. disappears.</p>
  17677. <p>The exception to that are the replication streams, i.e. RDATA commands,
  17678. since these include tokens which can be used to restart the stream on
  17679. connection errors.</p>
  17680. <p>The client should keep track of the token in the last RDATA command
  17681. received for each stream so that on reconneciton it can start streaming
  17682. from the correct place. Note: not all RDATA have valid tokens due to
  17683. batching. See <code>RdataCommand</code> for more details.</p>
  17684. <h3 id="example-5"><a class="header" href="#example-5">Example</a></h3>
  17685. <p>An example iteraction is shown below. Each line is prefixed with '&gt;'
  17686. or '&lt;' to indicate which side is sending, these are <em>not</em> included on
  17687. the wire:</p>
  17688. <pre><code>* connection established *
  17689. &gt; SERVER localhost:8823
  17690. &gt; PING 1490197665618
  17691. &lt; NAME synapse.app.appservice
  17692. &lt; PING 1490197665618
  17693. &lt; REPLICATE
  17694. &gt; POSITION events master 1 1
  17695. &gt; POSITION backfill master 1 1
  17696. &gt; POSITION caches master 1 1
  17697. &gt; RDATA caches master 2 [&quot;get_user_by_id&quot;,[&quot;@01register-user:localhost:8823&quot;],1490197670513]
  17698. &gt; RDATA events master 14 [&quot;$149019767112vOHxz:localhost:8823&quot;,
  17699. &quot;!AFDCvgApUmpdfVjIXm:localhost:8823&quot;,&quot;m.room.guest_access&quot;,&quot;&quot;,null]
  17700. &lt; PING 1490197675618
  17701. &gt; ERROR server stopping
  17702. * connection closed by server *
  17703. </code></pre>
  17704. <p>The <code>POSITION</code> command sent by the server is used to set the clients
  17705. position without needing to send data with the <code>RDATA</code> command.</p>
  17706. <p>An example of a batched set of <code>RDATA</code> is:</p>
  17707. <pre><code>&gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test:localhost:8823&quot;],1490197670513]
  17708. &gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test2:localhost:8823&quot;],1490197670513]
  17709. &gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test3:localhost:8823&quot;],1490197670513]
  17710. &gt; RDATA caches master 54 [&quot;get_user_by_id&quot;,[&quot;@test4:localhost:8823&quot;],1490197670513]
  17711. </code></pre>
  17712. <p>In this case the client shouldn't advance their caches token until it
  17713. sees the the last <code>RDATA</code>.</p>
  17714. <h3 id="list-of-commands"><a class="header" href="#list-of-commands">List of commands</a></h3>
  17715. <p>The list of valid commands, with which side can send it: server (S) or
  17716. client (C):</p>
  17717. <h4 id="server-s"><a class="header" href="#server-s">SERVER (S)</a></h4>
  17718. <p>Sent at the start to identify which server the client is talking to</p>
  17719. <h4 id="rdata-s"><a class="header" href="#rdata-s">RDATA (S)</a></h4>
  17720. <p>A single update in a stream</p>
  17721. <h4 id="position-s"><a class="header" href="#position-s">POSITION (S)</a></h4>
  17722. <p>On receipt of a POSITION command clients should check if they have missed any
  17723. updates, and if so then fetch them out of band. Sent in response to a
  17724. REPLICATE command (but can happen at any time).</p>
  17725. <p>The POSITION command includes the source of the stream. Currently all streams
  17726. are written by a single process (usually &quot;master&quot;). If fetching missing
  17727. updates via HTTP API, rather than via the DB, then processes should make the
  17728. request to the appropriate process.</p>
  17729. <p>Two positions are included, the &quot;new&quot; position and the last position sent respectively.
  17730. This allows servers to tell instances that the positions have advanced but no
  17731. data has been written, without clients needlessly checking to see if they
  17732. have missed any updates.</p>
  17733. <h4 id="error-s-c"><a class="header" href="#error-s-c">ERROR (S, C)</a></h4>
  17734. <p>There was an error</p>
  17735. <h4 id="ping-s-c"><a class="header" href="#ping-s-c">PING (S, C)</a></h4>
  17736. <p>Sent periodically to ensure the connection is still alive</p>
  17737. <h4 id="name-c"><a class="header" href="#name-c">NAME (C)</a></h4>
  17738. <p>Sent at the start by client to inform the server who they are</p>
  17739. <h4 id="replicate-c"><a class="header" href="#replicate-c">REPLICATE (C)</a></h4>
  17740. <p>Asks the server for the current position of all streams.</p>
  17741. <h4 id="user_sync-c"><a class="header" href="#user_sync-c">USER_SYNC (C)</a></h4>
  17742. <p>A user has started or stopped syncing on this process.</p>
  17743. <h4 id="clear_user_sync-c"><a class="header" href="#clear_user_sync-c">CLEAR_USER_SYNC (C)</a></h4>
  17744. <p>The server should clear all associated user sync data from the worker.</p>
  17745. <p>This is used when a worker is shutting down.</p>
  17746. <h4 id="federation_ack-c"><a class="header" href="#federation_ack-c">FEDERATION_ACK (C)</a></h4>
  17747. <p>Acknowledge receipt of some federation data</p>
  17748. <h3 id="remote_server_up-s-c"><a class="header" href="#remote_server_up-s-c">REMOTE_SERVER_UP (S, C)</a></h3>
  17749. <p>Inform other processes that a remote server may have come back online.</p>
  17750. <p>See <code>synapse/replication/tcp/commands.py</code> for a detailed description and
  17751. the format of each command.</p>
  17752. <h3 id="cache-invalidation-stream"><a class="header" href="#cache-invalidation-stream">Cache Invalidation Stream</a></h3>
  17753. <p>The cache invalidation stream is used to inform workers when they need
  17754. to invalidate any of their caches in the data store. This is done by
  17755. streaming all cache invalidations done on master down to the workers,
  17756. assuming that any caches on the workers also exist on the master.</p>
  17757. <p>Each individual cache invalidation results in a row being sent down
  17758. replication, which includes the cache name (the name of the function)
  17759. and they key to invalidate. For example:</p>
  17760. <pre><code>&gt; RDATA caches master 550953771 [&quot;get_user_by_id&quot;, [&quot;@bob:example.com&quot;], 1550574873251]
  17761. </code></pre>
  17762. <p>Alternatively, an entire cache can be invalidated by sending down a <code>null</code>
  17763. instead of the key. For example:</p>
  17764. <pre><code>&gt; RDATA caches master 550953772 [&quot;get_user_by_id&quot;, null, 1550574873252]
  17765. </code></pre>
  17766. <p>However, there are times when a number of caches need to be invalidated
  17767. at the same time with the same key. To reduce traffic we batch those
  17768. invalidations into a single poke by defining a special cache name that
  17769. workers understand to mean to expand to invalidate the correct caches.</p>
  17770. <p>Currently the special cache names are declared in
  17771. <code>synapse/storage/_base.py</code> and are:</p>
  17772. <ol>
  17773. <li><code>cs_cache_fake</code> ─ invalidates caches that depend on the current
  17774. state</li>
  17775. </ol>
  17776. <div style="break-before: page; page-break-before: always;"></div><h1 id="internal-documentation"><a class="header" href="#internal-documentation">Internal Documentation</a></h1>
  17777. <p>This section covers implementation documentation for various parts of Synapse.</p>
  17778. <p>If a developer is planning to make a change to a feature of Synapse, it can be useful for
  17779. general documentation of how that feature is implemented to be available. This saves the
  17780. developer time in place of needing to understand how the feature works by reading the
  17781. code.</p>
  17782. <p>Documentation that would be more useful for the perspective of a system administrator,
  17783. rather than a developer who's intending to change to code, should instead be placed
  17784. under the Usage section of the documentation.</p>
  17785. <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>
  17786. <p>https://fujifish.github.io/samling/samling.html (https://github.com/fujifish/samling) is a great resource for being able to tinker with the
  17787. SAML options within Synapse without needing to deploy and configure a complicated software stack.</p>
  17788. <p>To make Synapse (and therefore Element) use it:</p>
  17789. <ol>
  17790. <li>Use the samling.html URL above or deploy your own and visit the IdP Metadata tab.</li>
  17791. <li>Copy the XML to your clipboard.</li>
  17792. <li>On your Synapse server, create a new file <code>samling.xml</code> next to your <code>homeserver.yaml</code> with
  17793. the XML from step 2 as the contents.</li>
  17794. <li>Edit your <code>homeserver.yaml</code> to include:
  17795. <pre><code class="language-yaml">saml2_config:
  17796. sp_config:
  17797. allow_unknown_attributes: true # Works around a bug with AVA Hashes: https://github.com/IdentityPython/pysaml2/issues/388
  17798. metadata:
  17799. local: [&quot;samling.xml&quot;]
  17800. </code></pre>
  17801. </li>
  17802. <li>Ensure that your <code>homeserver.yaml</code> has a setting for <code>public_baseurl</code>:
  17803. <pre><code class="language-yaml">public_baseurl: http://localhost:8080/
  17804. </code></pre>
  17805. </li>
  17806. <li>Run <code>apt-get install xmlsec1</code> and <code>pip install --upgrade --force 'pysaml2&gt;=4.5.0'</code> to ensure
  17807. the dependencies are installed and ready to go.</li>
  17808. <li>Restart Synapse.</li>
  17809. </ol>
  17810. <p>Then in Element:</p>
  17811. <ol>
  17812. <li>Visit the login page and point Element towards your homeserver using the <code>public_baseurl</code> above.</li>
  17813. <li>Click the Single Sign-On button.</li>
  17814. <li>On the samling page, enter a Name Identifier and add a SAML Attribute for <code>uid=your_localpart</code>.
  17815. The response must also be signed.</li>
  17816. <li>Click &quot;Next&quot;.</li>
  17817. <li>Click &quot;Post Response&quot; (change nothing).</li>
  17818. <li>You should be logged in.</li>
  17819. </ol>
  17820. <p>If you try and repeat this process, you may be automatically logged in using the information you
  17821. gave previously. To fix this, open your developer console (<code>F12</code> or <code>Ctrl+Shift+I</code>) while on the
  17822. samling page and clear the site data. In Chrome, this will be a button on the Application tab.</p>
  17823. <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>
  17824. <p>The <a href="https://github.com/jbittel/django-mama-cas">django-mama-cas</a> project is an
  17825. easy to run CAS implementation built on top of Django.</p>
  17826. <h2 id="prerequisites"><a class="header" href="#prerequisites">Prerequisites</a></h2>
  17827. <ol>
  17828. <li>Create a new virtualenv: <code>python3 -m venv &lt;your virtualenv&gt;</code></li>
  17829. <li>Activate your virtualenv: <code>source /path/to/your/virtualenv/bin/activate</code></li>
  17830. <li>Install Django and django-mama-cas:
  17831. <pre><code class="language-sh">python -m pip install &quot;django&lt;3&quot; &quot;django-mama-cas==2.4.0&quot;
  17832. </code></pre>
  17833. </li>
  17834. <li>Create a Django project in the current directory:
  17835. <pre><code class="language-sh">django-admin startproject cas_test .
  17836. </code></pre>
  17837. </li>
  17838. <li>Follow the <a href="https://django-mama-cas.readthedocs.io/en/latest/installation.html#configuring">install directions</a> for django-mama-cas</li>
  17839. <li>Setup the SQLite database: <code>python manage.py migrate</code></li>
  17840. <li>Create a user:
  17841. <pre><code class="language-sh">python manage.py createsuperuser
  17842. </code></pre>
  17843. <ol>
  17844. <li>Use whatever you want as the username and password.</li>
  17845. <li>Leave the other fields blank.</li>
  17846. </ol>
  17847. </li>
  17848. <li>Use the built-in Django test server to serve the CAS endpoints on port 8000:
  17849. <pre><code class="language-sh">python manage.py runserver
  17850. </code></pre>
  17851. </li>
  17852. </ol>
  17853. <p>You should now have a Django project configured to serve CAS authentication with
  17854. a single user created.</p>
  17855. <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>
  17856. <ol>
  17857. <li>Modify your <code>homeserver.yaml</code> to enable CAS and point it to your locally
  17858. running Django test server:
  17859. <pre><code class="language-yaml">cas_config:
  17860. enabled: true
  17861. server_url: &quot;http://localhost:8000&quot;
  17862. service_url: &quot;http://localhost:8081&quot;
  17863. #displayname_attribute: name
  17864. #required_attributes:
  17865. # name: value
  17866. </code></pre>
  17867. </li>
  17868. <li>Restart Synapse.</li>
  17869. </ol>
  17870. <p>Note that the above configuration assumes the homeserver is running on port 8081
  17871. and that the CAS server is on port 8000, both on localhost.</p>
  17872. <h2 id="testing-the-configuration"><a class="header" href="#testing-the-configuration">Testing the configuration</a></h2>
  17873. <p>Then in Element:</p>
  17874. <ol>
  17875. <li>Visit the login page with a Element pointing at your homeserver.</li>
  17876. <li>Click the Single Sign-On button.</li>
  17877. <li>Login using the credentials created with <code>createsuperuser</code>.</li>
  17878. <li>You should be logged in.</li>
  17879. </ol>
  17880. <p>If you want to repeat this process you'll need to manually logout first:</p>
  17881. <ol>
  17882. <li>http://localhost:8000/admin/</li>
  17883. <li>Click &quot;logout&quot; in the top right.</li>
  17884. </ol>
  17885. <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>
  17886. <h2 id="edges"><a class="header" href="#edges">Edges</a></h2>
  17887. <p>The word &quot;edge&quot; comes from graph theory lingo. An edge is just a connection
  17888. between two events. In Synapse, we connect events by specifying their
  17889. <code>prev_events</code>. A subsequent event points back at a previous event.</p>
  17890. <pre><code>A (oldest) &lt;---- B &lt;---- C (most recent)
  17891. </code></pre>
  17892. <h2 id="depth-and-stream-ordering"><a class="header" href="#depth-and-stream-ordering">Depth and stream ordering</a></h2>
  17893. <p>Events are normally sorted by <code>(topological_ordering, stream_ordering)</code> where
  17894. <code>topological_ordering</code> is just <code>depth</code>. In other words, we first sort by <code>depth</code>
  17895. and then tie-break based on <code>stream_ordering</code>. <code>depth</code> is incremented as new
  17896. messages are added to the DAG. Normally, <code>stream_ordering</code> is an auto
  17897. incrementing integer, but backfilled events start with <code>stream_ordering=-1</code> and decrement.</p>
  17898. <hr />
  17899. <ul>
  17900. <li><code>/sync</code> returns things in the order they arrive at the server (<code>stream_ordering</code>).</li>
  17901. <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>
  17902. </ul>
  17903. <p>The general idea is that, if you're following a room in real-time (i.e.
  17904. <code>/sync</code>), you probably want to see the messages as they arrive at your server,
  17905. rather than skipping any that arrived late; whereas if you're looking at a
  17906. historical section of timeline (i.e. <code>/messages</code>), you want to see the best
  17907. representation of the state of the room as others were seeing it at the time.</p>
  17908. <h2 id="outliers"><a class="header" href="#outliers">Outliers</a></h2>
  17909. <p>We mark an event as an <code>outlier</code> when we haven't figured out the state for the
  17910. room at that point in the DAG yet. They are &quot;floating&quot; events that we haven't
  17911. yet correlated to the DAG.</p>
  17912. <p>Outliers typically arise when we fetch the auth chain or state for a given
  17913. event. When that happens, we just grab the events in the state/auth chain,
  17914. without calculating the state at those events, or backfilling their
  17915. <code>prev_events</code>. Since we don't have the state at any events fetched in that
  17916. way, we mark them as outliers.</p>
  17917. <p>So, typically, we won't have the <code>prev_events</code> of an <code>outlier</code> in the database,
  17918. (though it's entirely possible that we <em>might</em> have them for some other
  17919. reason). Other things that make outliers different from regular events:</p>
  17920. <ul>
  17921. <li>
  17922. <p>We don't have state for them, so there should be no entry in
  17923. <code>event_to_state_groups</code> for an outlier. (In practice this isn't always
  17924. the case, though I'm not sure why: see https://github.com/matrix-org/synapse/issues/12201).</p>
  17925. </li>
  17926. <li>
  17927. <p>We don't record entries for them in the <code>event_edges</code>,
  17928. <code>event_forward_extremeties</code> or <code>event_backward_extremities</code> tables.</p>
  17929. </li>
  17930. </ul>
  17931. <p>Since outliers are not tied into the DAG, they do not normally form part of the
  17932. timeline sent down to clients via <code>/sync</code> or <code>/messages</code>; however there is an
  17933. exception:</p>
  17934. <h3 id="out-of-band-membership-events"><a class="header" href="#out-of-band-membership-events">Out-of-band membership events</a></h3>
  17935. <p>A special case of outlier events are some membership events for federated rooms
  17936. that we aren't full members of. For example:</p>
  17937. <ul>
  17938. <li>invites received over federation, before we join the room</li>
  17939. <li><em>rejections</em> for said invites</li>
  17940. <li>knock events for rooms that we would like to join but have not yet joined.</li>
  17941. </ul>
  17942. <p>In all the above cases, we don't have the state for the room, which is why they
  17943. are treated as outliers. They are a bit special though, in that they are
  17944. proactively sent to clients via <code>/sync</code>.</p>
  17945. <h2 id="forward-extremity"><a class="header" href="#forward-extremity">Forward extremity</a></h2>
  17946. <p>Most-recent-in-time events in the DAG which are not referenced by any other
  17947. events' <code>prev_events</code> yet. (In this definition, outliers, rejected events, and
  17948. soft-failed events don't count.)</p>
  17949. <p>The forward extremities of a room (or at least, a subset of them, if there are
  17950. more than ten) are used as the <code>prev_events</code> when the next event is sent.</p>
  17951. <p>The &quot;current state&quot; of a room (ie: the state which would be used if we
  17952. generated a new event) is, therefore, the resolution of the room states
  17953. at each of the forward extremities.</p>
  17954. <h2 id="backward-extremity"><a class="header" href="#backward-extremity">Backward extremity</a></h2>
  17955. <p>The current marker of where we have backfilled up to and will generally be the
  17956. <code>prev_events</code> of the oldest-in-time events we have in the DAG. This gives a starting point when
  17957. backfilling history.</p>
  17958. <p>Note that, unlike forward extremities, we typically don't have any backward
  17959. extremity events themselves in the database - or, if we do, they will be &quot;outliers&quot; (see
  17960. above). Either way, we don't expect to have the room state at a backward extremity.</p>
  17961. <p>When we persist a non-outlier event, if it was previously a backward extremity,
  17962. we clear it as a backward extremity and set all of its <code>prev_events</code> as the new
  17963. backward extremities if they aren't already persisted as non-outliers. This
  17964. therefore keeps the backward extremities up-to-date.</p>
  17965. <h2 id="state-groups"><a class="header" href="#state-groups">State groups</a></h2>
  17966. <p>For every non-outlier event we need to know the state at that event. Instead of
  17967. storing the full state for each event in the DB (i.e. a <code>event_id -&gt; state</code>
  17968. mapping), which is <em>very</em> space inefficient when state doesn't change, we
  17969. instead assign each different set of state a &quot;state group&quot; and then have
  17970. mappings of <code>event_id -&gt; state_group</code> and <code>state_group -&gt; state</code>.</p>
  17971. <h3 id="stage-group-edges"><a class="header" href="#stage-group-edges">Stage group edges</a></h3>
  17972. <p>TODO: <code>state_group_edges</code> is a further optimization...
  17973. notes from @Azrenbeth, https://pastebin.com/seUGVGeT</p>
  17974. <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>
  17975. <p>The auth chain difference algorithm is used by V2 state resolution, where a
  17976. naive implementation can be a significant source of CPU and DB usage.</p>
  17977. <h3 id="definitions"><a class="header" href="#definitions">Definitions</a></h3>
  17978. <p>A <em>state set</em> is a set of state events; e.g. the input of a state resolution
  17979. algorithm is a collection of state sets.</p>
  17980. <p>The <em>auth chain</em> of a set of events are all the events' auth events and <em>their</em>
  17981. auth events, recursively (i.e. the events reachable by walking the graph induced
  17982. by an event's auth events links).</p>
  17983. <p>The <em>auth chain difference</em> of a collection of state sets is the union minus the
  17984. intersection of the sets of auth chains corresponding to the state sets, i.e an
  17985. event is in the auth chain difference if it is reachable by walking the auth
  17986. event graph from at least one of the state sets but not from <em>all</em> of the state
  17987. sets.</p>
  17988. <h2 id="breadth-first-walk-algorithm"><a class="header" href="#breadth-first-walk-algorithm">Breadth First Walk Algorithm</a></h2>
  17989. <p>A way of calculating the auth chain difference without calculating the full auth
  17990. chains for each state set is to do a parallel breadth first walk (ordered by
  17991. depth) of each state set's auth chain. By tracking which events are reachable
  17992. from each state set we can finish early if every pending event is reachable from
  17993. every state set.</p>
  17994. <p>This can work well for state sets that have a small auth chain difference, but
  17995. can be very inefficient for larger differences. However, this algorithm is still
  17996. used if we don't have a chain cover index for the room (e.g. because we're in
  17997. the process of indexing it).</p>
  17998. <h2 id="chain-cover-index"><a class="header" href="#chain-cover-index">Chain Cover Index</a></h2>
  17999. <p>Synapse computes auth chain differences by pre-computing a &quot;chain cover&quot; index
  18000. for the auth chain in a room, allowing efficient reachability queries like &quot;is
  18001. event A in the auth chain of event B&quot;. This is done by assigning every event a
  18002. <em>chain ID</em> and <em>sequence number</em> (e.g. <code>(5,3)</code>), and having a map of <em>links</em>
  18003. between chains (e.g. <code>(5,3) -&gt; (2,4)</code>) such that A is reachable by B (i.e. <code>A</code>
  18004. is in the auth chain of <code>B</code>) if and only if either:</p>
  18005. <ol>
  18006. <li>A and B have the same chain ID and <code>A</code>'s sequence number is less than <code>B</code>'s
  18007. sequence number; or</li>
  18008. <li>there is a link <code>L</code> between <code>B</code>'s chain ID and <code>A</code>'s chain ID such that
  18009. <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>
  18010. </ol>
  18011. <p>There are actually two potential implementations, one where we store links from
  18012. each chain to every other reachable chain (the transitive closure of the links
  18013. graph), and one where we remove redundant links (the transitive reduction of the
  18014. links graph) e.g. if we have chains <code>C3 -&gt; C2 -&gt; C1</code> then the link <code>C3 -&gt; C1</code>
  18015. would not be stored. Synapse uses the former implementations so that it doesn't
  18016. need to recurse to test reachability between chains.</p>
  18017. <h3 id="example-6"><a class="header" href="#example-6">Example</a></h3>
  18018. <p>An example auth graph would look like the following, where chains have been
  18019. formed based on type/state_key and are denoted by colour and are labelled with
  18020. <code>(chain ID, sequence number)</code>. Links are denoted by the arrows (links in grey
  18021. are those that would be remove in the second implementation described above).</p>
  18022. <p><img src="auth_chain_diff.dot.png" alt="Example" /></p>
  18023. <p>Note that we don't include all links between events and their auth events, as
  18024. most of those links would be redundant. For example, all events point to the
  18025. create event, but each chain only needs the one link from it's base to the
  18026. create event.</p>
  18027. <h2 id="using-the-index"><a class="header" href="#using-the-index">Using the Index</a></h2>
  18028. <p>This index can be used to calculate the auth chain difference of the state sets
  18029. by looking at the chain ID and sequence numbers reachable from each state set:</p>
  18030. <ol>
  18031. <li>For every state set lookup the chain ID/sequence numbers of each state event</li>
  18032. <li>Use the index to find all chains and the maximum sequence number reachable
  18033. from each state set.</li>
  18034. <li>The auth chain difference is then all events in each chain that have sequence
  18035. numbers between the maximum sequence number reachable from <em>any</em> state set and
  18036. the minimum reachable by <em>all</em> state sets (if any).</li>
  18037. </ol>
  18038. <p>Note that steps 2 is effectively calculating the auth chain for each state set
  18039. (in terms of chain IDs and sequence numbers), and step 3 is calculating the
  18040. difference between the union and intersection of the auth chains.</p>
  18041. <h3 id="worked-example"><a class="header" href="#worked-example">Worked Example</a></h3>
  18042. <p>For example, given the above graph, we can calculate the difference between
  18043. state sets consisting of:</p>
  18044. <ol>
  18045. <li><code>S1</code>: Alice's invite <code>(4,1)</code> and Bob's second join <code>(2,2)</code>; and</li>
  18046. <li><code>S2</code>: Alice's second join <code>(4,3)</code> and Bob's first join <code>(2,1)</code>.</li>
  18047. </ol>
  18048. <p>Using the index we see that the following auth chains are reachable from each
  18049. state set:</p>
  18050. <ol>
  18051. <li><code>S1</code>: <code>(1,1)</code>, <code>(2,2)</code>, <code>(3,1)</code> &amp; <code>(4,1)</code></li>
  18052. <li><code>S2</code>: <code>(1,1)</code>, <code>(2,1)</code>, <code>(3,2)</code> &amp; <code>(4,3)</code></li>
  18053. </ol>
  18054. <p>And so, for each the ranges that are in the auth chain difference:</p>
  18055. <ol>
  18056. <li>Chain 1: None, (since everything can reach the create event).</li>
  18057. <li>Chain 2: The range <code>(1, 2]</code> (i.e. just <code>2</code>), as <code>1</code> is reachable by all state
  18058. sets and the maximum reachable is <code>2</code> (corresponding to Bob's second join).</li>
  18059. <li>Chain 3: Similarly the range <code>(1, 2]</code> (corresponding to the second power
  18060. level).</li>
  18061. <li>Chain 4: The range <code>(1, 3]</code> (corresponding to both of Alice's joins).</li>
  18062. </ol>
  18063. <p>So the final result is: Bob's second join <code>(2,2)</code>, the second power level
  18064. <code>(3,2)</code> and both of Alice's joins <code>(4,2)</code> &amp; <code>(4,3)</code>.</p>
  18065. <div style="break-before: page; page-break-before: always;"></div><h1 id="media-repository"><a class="header" href="#media-repository">Media Repository</a></h1>
  18066. <p><em>Synapse implementation-specific details for the media repository</em></p>
  18067. <p>The media repository</p>
  18068. <ul>
  18069. <li>stores avatars, attachments and their thumbnails for media uploaded by local
  18070. users.</li>
  18071. <li>caches avatars, attachments and their thumbnails for media uploaded by remote
  18072. users.</li>
  18073. <li>caches resources and thumbnails used for
  18074. <a href="development/url_previews.html">URL previews</a>.</li>
  18075. </ul>
  18076. <p>All media in Matrix can be identified by a unique
  18077. <a href="https://spec.matrix.org/latest/client-server-api/#matrix-content-mxc-uris">MXC URI</a>,
  18078. consisting of a server name and media ID:</p>
  18079. <pre><code>mxc://&lt;server-name&gt;/&lt;media-id&gt;
  18080. </code></pre>
  18081. <h2 id="local-media"><a class="header" href="#local-media">Local Media</a></h2>
  18082. <p>Synapse generates 24 character media IDs for content uploaded by local users.
  18083. These media IDs consist of upper and lowercase letters and are case-sensitive.
  18084. Other homeserver implementations may generate media IDs differently.</p>
  18085. <p>Local media is recorded in the <code>local_media_repository</code> table, which includes
  18086. metadata such as MIME types, upload times and file sizes.
  18087. Note that this table is shared by the URL cache, which has a different media ID
  18088. scheme.</p>
  18089. <h3 id="paths"><a class="header" href="#paths">Paths</a></h3>
  18090. <p>A file with media ID <code>aabbcccccccccccccccccccc</code> and its <code>128x96</code> <code>image/jpeg</code>
  18091. thumbnail, created by scaling, would be stored at:</p>
  18092. <pre><code>local_content/aa/bb/cccccccccccccccccccc
  18093. local_thumbnails/aa/bb/cccccccccccccccccccc/128-96-image-jpeg-scale
  18094. </code></pre>
  18095. <h2 id="remote-media"><a class="header" href="#remote-media">Remote Media</a></h2>
  18096. <p>When media from a remote homeserver is requested from Synapse, it is assigned
  18097. a local <code>filesystem_id</code>, with the same format as locally-generated media IDs,
  18098. as described above.</p>
  18099. <p>A record of remote media is stored in the <code>remote_media_cache</code> table, which
  18100. can be used to map remote MXC URIs (server names and media IDs) to local
  18101. <code>filesystem_id</code>s.</p>
  18102. <h3 id="paths-1"><a class="header" href="#paths-1">Paths</a></h3>
  18103. <p>A file from <code>matrix.org</code> with <code>filesystem_id</code> <code>aabbcccccccccccccccccccc</code> and its
  18104. <code>128x96</code> <code>image/jpeg</code> thumbnail, created by scaling, would be stored at:</p>
  18105. <pre><code>remote_content/matrix.org/aa/bb/cccccccccccccccccccc
  18106. remote_thumbnail/matrix.org/aa/bb/cccccccccccccccccccc/128-96-image-jpeg-scale
  18107. </code></pre>
  18108. <p>Older thumbnails may omit the thumbnailing method:</p>
  18109. <pre><code>remote_thumbnail/matrix.org/aa/bb/cccccccccccccccccccc/128-96-image-jpeg
  18110. </code></pre>
  18111. <p>Note that <code>remote_thumbnail/</code> does not have an <code>s</code>.</p>
  18112. <h2 id="url-previews-2"><a class="header" href="#url-previews-2">URL Previews</a></h2>
  18113. <p>See <a href="development/url_previews.html">URL Previews</a> for documentation on the URL preview
  18114. process.</p>
  18115. <p>When generating previews for URLs, Synapse may download and cache various
  18116. resources, including images. These resources are assigned temporary media IDs
  18117. of the form <code>yyyy-mm-dd_aaaaaaaaaaaaaaaa</code>, where <code>yyyy-mm-dd</code> is the current
  18118. date and <code>aaaaaaaaaaaaaaaa</code> is a random sequence of 16 case-sensitive letters.</p>
  18119. <p>The metadata for these cached resources is stored in the
  18120. <code>local_media_repository</code> and <code>local_media_repository_url_cache</code> tables.</p>
  18121. <p>Resources for URL previews are deleted after a few days.</p>
  18122. <h3 id="paths-2"><a class="header" href="#paths-2">Paths</a></h3>
  18123. <p>The file with media ID <code>yyyy-mm-dd_aaaaaaaaaaaaaaaa</code> and its <code>128x96</code>
  18124. <code>image/jpeg</code> thumbnail, created by scaling, would be stored at:</p>
  18125. <pre><code>url_cache/yyyy-mm-dd/aaaaaaaaaaaaaaaa
  18126. url_cache_thumbnails/yyyy-mm-dd/aaaaaaaaaaaaaaaa/128-96-image-jpeg-scale
  18127. </code></pre>
  18128. <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>
  18129. <p>Synapse maintains room and user statistics in various tables. These can be used
  18130. for administrative purposes but are also used when generating the public room
  18131. directory.</p>
  18132. <h1 id="synapse-developer-documentation"><a class="header" href="#synapse-developer-documentation">Synapse Developer Documentation</a></h1>
  18133. <h2 id="high-level-concepts"><a class="header" href="#high-level-concepts">High-Level Concepts</a></h2>
  18134. <h3 id="definitions-1"><a class="header" href="#definitions-1">Definitions</a></h3>
  18135. <ul>
  18136. <li><strong>subject</strong>: Something we are tracking stats about – currently a room or user.</li>
  18137. <li><strong>current row</strong>: An entry for a subject in the appropriate current statistics
  18138. table. Each subject can have only one.</li>
  18139. </ul>
  18140. <h3 id="overview-4"><a class="header" href="#overview-4">Overview</a></h3>
  18141. <p>Stats correspond to the present values. Current rows contain the most up-to-date
  18142. statistics for a room. Each subject can only have one entry.</p>
  18143. <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>
  18144. <p>Synapse has a number of platform dependencies, including Python and PostgreSQL.
  18145. This document outlines the policy towards which versions we support, and when we
  18146. drop support for versions in the future.</p>
  18147. <h2 id="policy"><a class="header" href="#policy">Policy</a></h2>
  18148. <p>Synapse follows the upstream support life cycles for Python and PostgreSQL,
  18149. i.e. when a version reaches End of Life Synapse will withdraw support for that
  18150. version in future releases.</p>
  18151. <p>Details on the upstream support life cycles for Python and PostgreSQL are
  18152. documented at <a href="https://endoflife.date/python">https://endoflife.date/python</a> and
  18153. <a href="https://endoflife.date/postgresql">https://endoflife.date/postgresql</a>.</p>
  18154. <h2 id="context"><a class="header" href="#context">Context</a></h2>
  18155. <p>It is important for system admins to have a clear understanding of the platform
  18156. requirements of Synapse and its deprecation policies so that they can
  18157. effectively plan upgrading their infrastructure ahead of time. This is
  18158. especially important in contexts where upgrading the infrastructure requires
  18159. auditing and approval from a security team, or where otherwise upgrading is a
  18160. long process.</p>
  18161. <p>By following the upstream support life cycles Synapse can ensure that its
  18162. dependencies continue to get security patches, while not requiring system admins
  18163. to constantly update their platform dependencies to the latest versions.</p>
  18164. <div style="break-before: page; page-break-before: always;"></div><h2 id="summary-of-performance-impact-of-running-on-resource-constrained-devices-such-as-sbcs"><a class="header" href="#summary-of-performance-impact-of-running-on-resource-constrained-devices-such-as-sbcs">Summary of performance impact of running on resource constrained devices such as SBCs</a></h2>
  18165. <p>I've been running my homeserver on a cubietruck at home now for some time and am often replying to statements like &quot;you need loads of ram to join large rooms&quot; with &quot;it works fine for me&quot;. I thought it might be useful to curate a summary of the issues you're likely to run into to help as a scaling-down guide, maybe highlight these for development work or end up as documentation. It seems that once you get up to about 4x1.5GHz arm64 4GiB these issues are no longer a problem.</p>
  18166. <ul>
  18167. <li><strong>Platform</strong>: 2x1GHz armhf 2GiB ram <a href="https://wiki.debian.org/CheapServerBoxHardware">Single-board computers</a>, SSD, postgres.</li>
  18168. </ul>
  18169. <h3 id="presence"><a class="header" href="#presence">Presence</a></h3>
  18170. <p>This is the main reason people have a poor matrix experience on resource constrained homeservers. Element web will frequently be saying the server is offline while the python process will be pegged at 100% cpu. This feature is used to tell when other users are active (have a client app in the foreground) and therefore more likely to respond, but requires a lot of network activity to maintain even when nobody is talking in a room.</p>
  18171. <p><img src="https://user-images.githubusercontent.com/71895/94848963-a47a3580-041c-11eb-8b6e-acb772b4259e.png" alt="Screenshot_2020-10-01_19-29-46" /></p>
  18172. <p>While synapse does have some performance issues with presence <a href="https://github.com/matrix-org/synapse/issues/3971">#3971</a>, the fundamental problem is that this is an easy feature to implement for a centralised service at nearly no overhead, but federation makes it combinatorial <a href="https://github.com/matrix-org/synapse/issues/8055">#8055</a>. There is also a client-side config option which disables the UI and idle tracking <a href="https://github.com/vector-im/element-web/blob/v1.7.8/config.sample.json#L45">enable_presence_by_hs_url</a> to blacklist the largest instances but I didn't notice much difference, so I recommend disabling the feature entirely at the server level as well.</p>
  18173. <h3 id="joining"><a class="header" href="#joining">Joining</a></h3>
  18174. <p>Joining a &quot;large&quot;, federated room will initially fail with the below message in Element web, but waiting a while (10-60mins) and trying again will succeed without any issue. What counts as &quot;large&quot; is not message history, user count, connections to homeservers or even a simple count of the state events, it is instead how long the state resolution algorithm takes. However, each of those numbers are reasonable proxies, so we can use them as estimates since user count is one of the few things you see before joining.</p>
  18175. <p><img src="https://user-images.githubusercontent.com/71895/94945781-18771500-04d3-11eb-8419-83c2da73a341.png" alt="Screenshot_2020-10-02_17-15-06" /></p>
  18176. <p>This is <a href="https://github.com/matrix-org/synapse/issues/1211">#1211</a> and will also hopefully be mitigated by peeking <a href="https://github.com/matrix-org/matrix-doc/pull/2753">matrix-org/matrix-doc#2753</a> so at least you don't need to wait for a join to complete before finding out if it's the kind of room you want. Note that you should first disable presence, otherwise it'll just make the situation worse <a href="https://github.com/matrix-org/synapse/issues/3120">#3120</a>. There is a lot of database interaction too, so make sure you've <a href="other/../postgres.html">migrated your data</a> from the default sqlite to postgresql. Personally, I recommend patience - once the initial join is complete there's rarely any issues with actually interacting with the room, but if you like you can just block &quot;large&quot; rooms entirely.</p>
  18177. <h3 id="sessions"><a class="header" href="#sessions">Sessions</a></h3>
  18178. <p>Anything that requires modifying the device list <a href="https://github.com/matrix-org/synapse/issues/7721">#7721</a> will take a while to propagate, again taking the client &quot;Offline&quot; until it's complete. This includes signing in and out, editing the public name and verifying e2ee. The main mitigation I recommend is to keep long-running sessions open e.g. by using Firefox SSB &quot;Use this site in App mode&quot; or Chromium PWA &quot;Install Element&quot;.</p>
  18179. <h3 id="recommended-configuration"><a class="header" href="#recommended-configuration">Recommended configuration</a></h3>
  18180. <p>Put the below in a new file at /etc/matrix-synapse/conf.d/sbc.yaml to override the defaults in homeserver.yaml.</p>
  18181. <pre><code># Disable presence tracking, which is currently fairly resource intensive
  18182. # More info: https://github.com/matrix-org/synapse/issues/9478
  18183. use_presence: false
  18184. # Set a small complexity limit, preventing users from joining large rooms
  18185. # which may be resource-intensive to remain a part of.
  18186. #
  18187. # Note that this will not prevent users from joining smaller rooms that
  18188. # eventually become complex.
  18189. limit_remote_rooms:
  18190. enabled: true
  18191. complexity: 3.0
  18192. # Database configuration
  18193. database:
  18194. # Use postgres for the best performance
  18195. name: psycopg2
  18196. args:
  18197. user: matrix-synapse
  18198. # Generate a long, secure password using a password manager
  18199. password: hunter2
  18200. database: matrix-synapse
  18201. host: localhost
  18202. </code></pre>
  18203. <p>Currently the complexity is measured by <a href="https://github.com/matrix-org/synapse/blob/v1.20.1/synapse/storage/databases/main/events_worker.py#L986">current_state_events / 500</a>. You can find join times and your most complex rooms like this:</p>
  18204. <pre><code>admin@homeserver:~$ zgrep '/client/r0/join/' /var/log/matrix-synapse/homeserver.log* | awk '{print $18, $25}' | sort --human-numeric-sort
  18205. 29.922sec/-0.002sec /_matrix/client/r0/join/%23debian-fasttrack%3Apoddery.com
  18206. 182.088sec/0.003sec /_matrix/client/r0/join/%23decentralizedweb-general%3Amatrix.org
  18207. 911.625sec/-570.847sec /_matrix/client/r0/join/%23synapse%3Amatrix.org
  18208. admin@homeserver:~$ sudo --user postgres psql matrix-synapse --command 'select canonical_alias, joined_members, current_state_events from room_stats_state natural join room_stats_current where canonical_alias is not null order by current_state_events desc fetch first 5 rows only'
  18209. canonical_alias | joined_members | current_state_events
  18210. -------------------------------+----------------+----------------------
  18211. #_oftc_#debian:matrix.org | 871 | 52355
  18212. #matrix:matrix.org | 6379 | 10684
  18213. #irc:matrix.org | 461 | 3751
  18214. #decentralizedweb-general:matrix.org | 997 | 1509
  18215. #whatsapp:maunium.net | 554 | 854
  18216. </code></pre>
  18217. </main>
  18218. <nav class="nav-wrapper" aria-label="Page navigation">
  18219. <!-- Mobile navigation buttons -->
  18220. <div style="clear: both"></div>
  18221. </nav>
  18222. </div>
  18223. </div>
  18224. <nav class="nav-wide-wrapper" aria-label="Page navigation">
  18225. </nav>
  18226. </div>
  18227. <script type="text/javascript">
  18228. window.playground_copyable = true;
  18229. </script>
  18230. <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
  18231. <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
  18232. <script src="searcher.js" type="text/javascript" charset="utf-8"></script>
  18233. <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
  18234. <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
  18235. <script src="book.js" type="text/javascript" charset="utf-8"></script>
  18236. <!-- Custom JS scripts -->
  18237. <script type="text/javascript" src="docs/website_files/table-of-contents.js"></script>
  18238. <script type="text/javascript" src="docs/website_files/version-picker.js"></script>
  18239. <script type="text/javascript" src="docs/website_files/version.js"></script>
  18240. <script type="text/javascript">
  18241. window.addEventListener('load', function() {
  18242. window.setTimeout(window.print, 100);
  18243. });
  18244. </script>
  18245. </body>
  18246. </html>