test_plugin.py 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  1. import os.path
  2. import zipfile
  3. from base64 import b64encode
  4. from io import BytesIO
  5. from unittest.mock import patch
  6. from uuid import uuid4
  7. import pytest
  8. import responses
  9. from django.utils.encoding import force_bytes
  10. from sentry.models import (
  11. ArtifactBundle,
  12. DebugIdArtifactBundle,
  13. File,
  14. ProjectArtifactBundle,
  15. Release,
  16. ReleaseArtifactBundle,
  17. ReleaseFile,
  18. SourceFileType,
  19. )
  20. from sentry.models.releasefile import update_artifact_index
  21. from sentry.testutils import RelayStoreHelper
  22. from sentry.testutils.helpers import override_options
  23. from sentry.testutils.helpers.datetime import before_now, iso_format
  24. from sentry.testutils.skips import requires_symbolicator
  25. from sentry.utils import json
  26. # IMPORTANT:
  27. #
  28. # This test suite requires Symbolicator in order to run correctly.
  29. # Set `symbolicator.enabled: true` in your `~/.sentry/config.yml` and run `sentry devservices up`
  30. #
  31. # If you are using a local instance of Symbolicator, you need to either change `system.url-prefix` to `system.internal-url-prefix`
  32. # inside `process_with_symbolicator` fixture inside `src/sentry/utils/pytest/fixtures.py`,
  33. # or add `127.0.0.1 host.docker.internal` entry to your `/etc/hosts`
  34. BASE64_SOURCEMAP = "data:application/json;base64," + (
  35. b64encode(
  36. b'{"version":3,"file":"generated.js","sources":["/test.js"],"names":[],"mappings":"AAAA","sourcesContent":['
  37. b'"console.log(\\"hello, World!\\")"]}'
  38. )
  39. .decode("utf-8")
  40. .replace("\n", "")
  41. )
  42. INVALID_BASE64_SOURCEMAP = "data:application/json;base64,A"
  43. def get_fixture_path(name):
  44. return os.path.join(os.path.dirname(__file__), "fixtures", name)
  45. def load_fixture(name):
  46. with open(get_fixture_path(name), "rb") as fp:
  47. return fp.read()
  48. @pytest.mark.django_db(transaction=True)
  49. class TestJavascriptIntegration(RelayStoreHelper):
  50. @pytest.fixture(autouse=True)
  51. def initialize(self, default_projectkey, default_project):
  52. self.project = default_project
  53. self.projectkey = default_projectkey
  54. self.organization = self.project.organization
  55. self.min_ago = iso_format(before_now(minutes=1))
  56. # We disable scraping per-test when necessary.
  57. self.project.update_option("sentry:scrape_javascript", True)
  58. @requires_symbolicator
  59. @pytest.mark.symbolicator
  60. def test_adds_contexts_without_device(self, process_with_symbolicator):
  61. data = {
  62. "timestamp": self.min_ago,
  63. "message": "hello",
  64. "platform": "javascript",
  65. "request": {
  66. "url": "http://example.com",
  67. "headers": [
  68. [
  69. "User-Agent",
  70. "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
  71. "Chrome/28.0.1500.72 Safari/537.36",
  72. ]
  73. ],
  74. },
  75. }
  76. event = self.post_and_retrieve_event(data)
  77. contexts = event.interfaces["contexts"].to_json()
  78. assert contexts.get("os") == {"name": "Windows", "version": "8", "type": "os"}
  79. assert contexts.get("device") is None
  80. @requires_symbolicator
  81. @pytest.mark.symbolicator
  82. def test_adds_contexts_with_device(self, process_with_symbolicator):
  83. data = {
  84. "timestamp": self.min_ago,
  85. "message": "hello",
  86. "platform": "javascript",
  87. "request": {
  88. "url": "http://example.com",
  89. "headers": [
  90. [
  91. "User-Agent",
  92. "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SCH-R530U Build/JSS15J) AppleWebKit/534.30 ("
  93. "KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 USCC-R530U",
  94. ]
  95. ],
  96. },
  97. }
  98. event = self.post_and_retrieve_event(data)
  99. contexts = event.interfaces["contexts"].to_json()
  100. assert contexts.get("os") == {"name": "Android", "type": "os", "version": "4.3"}
  101. assert contexts.get("browser") == {"name": "Android", "type": "browser", "version": "4.3"}
  102. assert contexts.get("device") == {
  103. "family": "Samsung SCH-R530U",
  104. "type": "device",
  105. "model": "SCH-R530U",
  106. "name": "Galaxy S3",
  107. "brand": "Samsung",
  108. }
  109. @requires_symbolicator
  110. @pytest.mark.symbolicator
  111. def test_adds_contexts_with_ps4_device(self, process_with_symbolicator):
  112. data = {
  113. "timestamp": self.min_ago,
  114. "message": "hello",
  115. "platform": "javascript",
  116. "request": {
  117. "url": "http://example.com",
  118. "headers": [
  119. [
  120. "User-Agent",
  121. "Mozilla/5.0 (PlayStation 4 3.55) AppleWebKit/537.78 (KHTML, like Gecko)",
  122. ]
  123. ],
  124. },
  125. }
  126. event = self.post_and_retrieve_event(data)
  127. contexts = event.interfaces["contexts"].to_json()
  128. assert contexts.get("os") is None
  129. assert contexts.get("browser") is None
  130. assert contexts.get("device") == {
  131. "family": "PlayStation 4",
  132. "type": "device",
  133. "model": "PlayStation 4",
  134. "brand": "Sony",
  135. }
  136. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  137. def test_source_expansion(self, mock_fetch_by_url):
  138. data = {
  139. "timestamp": self.min_ago,
  140. "message": "hello",
  141. "platform": "javascript",
  142. "exception": {
  143. "values": [
  144. {
  145. "type": "Error",
  146. "stacktrace": {
  147. "frames": [
  148. {
  149. "abs_path": "http://example.com/foo.js",
  150. "filename": "foo.js",
  151. "lineno": 4,
  152. "colno": 0,
  153. },
  154. {
  155. "abs_path": "http://example.com/foo.js",
  156. "filename": "foo.js",
  157. "lineno": 1,
  158. "colno": 0,
  159. },
  160. ]
  161. },
  162. }
  163. ]
  164. },
  165. }
  166. mock_fetch_by_url.return_value.body = force_bytes("\n".join("hello world"))
  167. mock_fetch_by_url.return_value.encoding = None
  168. mock_fetch_by_url.return_value.headers = {}
  169. event = self.post_and_retrieve_event(data)
  170. mock_fetch_by_url.assert_called_once_with("http://example.com/foo.js")
  171. exception = event.interfaces["exception"]
  172. frame_list = exception.values[0].stacktrace.frames
  173. frame = frame_list[0]
  174. assert frame.pre_context == ["h", "e", "l"]
  175. assert frame.context_line == "l"
  176. assert frame.post_context == ["o", " ", "w", "o", "r"]
  177. frame = frame_list[1]
  178. assert not frame.pre_context
  179. assert frame.context_line == "h"
  180. assert frame.post_context == ["e", "l", "l", "o", " "]
  181. # no source map means no raw_stacktrace
  182. assert exception.values[0].raw_stacktrace is None
  183. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  184. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  185. def test_inlined_sources(self, mock_discover_sourcemap, mock_fetch_by_url):
  186. data = {
  187. "timestamp": self.min_ago,
  188. "message": "hello",
  189. "platform": "javascript",
  190. "exception": {
  191. "values": [
  192. {
  193. "type": "Error",
  194. "stacktrace": {
  195. "frames": [
  196. {
  197. "abs_path": "http://example.com/test.min.js",
  198. "filename": "test.js",
  199. "lineno": 1,
  200. "colno": 1,
  201. }
  202. ]
  203. },
  204. }
  205. ]
  206. },
  207. }
  208. mock_discover_sourcemap.return_value = BASE64_SOURCEMAP
  209. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  210. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  211. mock_fetch_by_url.return_value.encoding = None
  212. event = self.post_and_retrieve_event(data)
  213. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  214. exception = event.interfaces["exception"]
  215. frame_list = exception.values[0].stacktrace.frames
  216. frame = frame_list[0]
  217. assert not frame.pre_context
  218. assert frame.context_line == 'console.log("hello, World!")'
  219. assert not frame.post_context
  220. assert frame.data["sourcemap"] == "http://example.com/test.min.js"
  221. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  222. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  223. def test_invalid_base64_sourcemap_returns_an_error(
  224. self, mock_discover_sourcemap, mock_fetch_by_url
  225. ):
  226. data = {
  227. "timestamp": self.min_ago,
  228. "message": "hello",
  229. "platform": "javascript",
  230. "exception": {
  231. "values": [
  232. {
  233. "type": "Error",
  234. "stacktrace": {
  235. "frames": [
  236. {
  237. "abs_path": "http://example.com/test.min.js",
  238. "filename": "test.js",
  239. "lineno": 1,
  240. "colno": 1,
  241. },
  242. ]
  243. },
  244. }
  245. ]
  246. },
  247. }
  248. mock_discover_sourcemap.return_value = INVALID_BASE64_SOURCEMAP
  249. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  250. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  251. mock_fetch_by_url.return_value.encoding = None
  252. event = self.post_and_retrieve_event(data)
  253. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  254. assert len(event.data["errors"]) == 1
  255. assert event.data["errors"][0] == {
  256. "url": "<base64>",
  257. "reason": "Invalid base64-encoded string: "
  258. "number of data characters (1) cannot be 1 more than a multiple of 4",
  259. "type": "js_invalid_source",
  260. }
  261. @patch("sentry.lang.javascript.processor.SmCache.from_bytes")
  262. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  263. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  264. def test_sourcemap_cache_is_constructed_only_once_if_an_error_is_raised(
  265. self, mock_discover_sourcemap, mock_fetch_by_url, mock_from_bytes
  266. ):
  267. data = {
  268. "timestamp": self.min_ago,
  269. "message": "hello",
  270. "platform": "javascript",
  271. "exception": {
  272. "values": [
  273. {
  274. "type": "Error",
  275. "stacktrace": {
  276. "frames": [
  277. {
  278. "abs_path": "http://example.com/test.min.js",
  279. "filename": "test.js",
  280. "lineno": 1,
  281. "colno": 1,
  282. },
  283. {
  284. "abs_path": "http://example.com/test.min.js",
  285. "filename": "test.js",
  286. "lineno": 1,
  287. "colno": 1,
  288. },
  289. {
  290. "abs_path": "http://example.com/test.min.js",
  291. "filename": "test.js",
  292. "lineno": 1,
  293. "colno": 1,
  294. },
  295. ]
  296. },
  297. }
  298. ]
  299. },
  300. }
  301. mock_discover_sourcemap.return_value = BASE64_SOURCEMAP
  302. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  303. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  304. mock_fetch_by_url.return_value.encoding = None
  305. mock_from_bytes.side_effect = Exception()
  306. self.post_and_retrieve_event(data)
  307. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  308. mock_from_bytes.assert_called_once()
  309. @requires_symbolicator
  310. @pytest.mark.symbolicator
  311. def test_error_message_translations(self, process_with_symbolicator):
  312. data = {
  313. "timestamp": self.min_ago,
  314. "message": "hello",
  315. "platform": "javascript",
  316. "logentry": {
  317. "formatted": "ReferenceError: Impossible de d\xe9finir une propri\xe9t\xe9 \xab foo \xbb : objet non "
  318. "extensible"
  319. },
  320. "exception": {
  321. "values": [
  322. {"type": "Error", "value": "P\u0159\xedli\u0161 mnoho soubor\u016f"},
  323. {
  324. "type": "Error",
  325. "value": "foo: wyst\u0105pi\u0142 nieoczekiwany b\u0142\u0105d podczas pr\xf3by uzyskania "
  326. "informacji o metadanych",
  327. },
  328. ]
  329. },
  330. }
  331. event = self.post_and_retrieve_event(data)
  332. message = event.interfaces["logentry"]
  333. assert (
  334. message.formatted
  335. == "ReferenceError: Cannot define property 'foo': object is not extensible"
  336. )
  337. exception = event.interfaces["exception"]
  338. assert exception.values[0].value == "Too many files"
  339. assert (
  340. exception.values[1].value
  341. == "foo: an unexpected failure occurred while trying to obtain metadata information"
  342. )
  343. @requires_symbolicator
  344. @pytest.mark.symbolicator
  345. def test_nonhandled_frames_inapp_normalization(self, process_with_symbolicator):
  346. data = {
  347. "timestamp": self.min_ago,
  348. "message": "hello",
  349. "platform": "node",
  350. "exception": {
  351. "values": [
  352. {
  353. "type": "Error",
  354. "stacktrace": {
  355. "frames": [
  356. {
  357. "abs_path": "native",
  358. "lineno": 1,
  359. "colno": 1,
  360. "in_app": True,
  361. },
  362. {
  363. "abs_path": "[native code]",
  364. "lineno": 1,
  365. "colno": 1,
  366. "in_app": True,
  367. },
  368. {
  369. "abs_path": "app://dist/bundle/file.min.js",
  370. "lineno": 1,
  371. "colno": 1,
  372. "in_app": True,
  373. },
  374. ]
  375. },
  376. }
  377. ]
  378. },
  379. }
  380. event = self.post_and_retrieve_event(data)
  381. exception = event.interfaces["exception"]
  382. if process_with_symbolicator:
  383. frame_list = exception.values[0].stacktrace.frames
  384. assert not frame_list[0].in_app # should be overwritten due to `native` abs_path
  385. assert not frame_list[1].in_app # should be overwritten due to `[native code]` abs_path
  386. assert frame_list[2].in_app # should not be touched and retain `in_app: true`
  387. raw_frame_list = exception.values[0].raw_stacktrace.frames
  388. # none of the raw frames should be altered
  389. assert raw_frame_list[0].in_app
  390. assert raw_frame_list[1].in_app
  391. assert raw_frame_list[2].in_app
  392. @requires_symbolicator
  393. @pytest.mark.symbolicator
  394. def test_sourcemap_source_expansion(self, process_with_symbolicator):
  395. self.project.update_option("sentry:scrape_javascript", False)
  396. release = Release.objects.create(
  397. organization_id=self.project.organization_id, version="abc"
  398. )
  399. release.add_project(self.project)
  400. for file in ["file.min.js", "file1.js", "file2.js", "file.sourcemap.js"]:
  401. with open(get_fixture_path(file), "rb") as f:
  402. f1 = File.objects.create(
  403. name=file,
  404. type="release.file",
  405. headers={},
  406. )
  407. f1.putfile(f)
  408. ReleaseFile.objects.create(
  409. name=f"http://example.com/{f1.name}",
  410. release_id=release.id,
  411. organization_id=self.project.organization_id,
  412. file=f1,
  413. )
  414. data = {
  415. "timestamp": self.min_ago,
  416. "message": "hello",
  417. "platform": "javascript",
  418. "release": "abc",
  419. "exception": {
  420. "values": [
  421. {
  422. "type": "Error",
  423. "stacktrace": {
  424. "frames": [
  425. {
  426. "abs_path": "http://example.com/file.min.js",
  427. "filename": "file.min.js",
  428. "lineno": 1,
  429. "colno": 39,
  430. },
  431. # NOTE: Intentionally source is not retrieved from this HTML file
  432. {
  433. "function": 'function: "HTMLDocument.<anonymous>"',
  434. "abs_path": "http//example.com/index.html",
  435. "filename": "index.html",
  436. "lineno": 283,
  437. "colno": 17,
  438. "in_app": False,
  439. },
  440. # NOTE: a mixed stack trace with a native frame:
  441. {
  442. "instruction_addr": "0xd10349",
  443. },
  444. ]
  445. },
  446. }
  447. ]
  448. },
  449. }
  450. event = self.post_and_retrieve_event(data)
  451. assert event.data["errors"] == [
  452. {"type": "js_no_source", "url": "http//example.com/index.html"}
  453. ]
  454. exception = event.interfaces["exception"]
  455. frame_list = exception.values[0].stacktrace.frames
  456. frame = frame_list[0]
  457. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  458. expected = "\treturn a + b; // fôo"
  459. assert frame.context_line == expected
  460. if process_with_symbolicator:
  461. assert frame.post_context == ["}"]
  462. else:
  463. assert frame.post_context == ["}", ""]
  464. raw_frame_list = exception.values[0].raw_stacktrace.frames
  465. raw_frame = raw_frame_list[0]
  466. assert not raw_frame.pre_context
  467. assert (
  468. raw_frame.context_line
  469. == 'function add(a,b){"use strict";return a+b}function multiply(a,b){"use strict";return a*b}function '
  470. 'divide(a,b){"use strict";try{return multip {snip}'
  471. )
  472. if process_with_symbolicator:
  473. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js"]
  474. else:
  475. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js", ""]
  476. assert raw_frame.lineno == 1
  477. # Since we couldn't expand source for the 2nd frame, both
  478. # its raw and original form should be identical
  479. assert raw_frame_list[1] == frame_list[1]
  480. # The second non-js frame should be untouched
  481. assert raw_frame_list[2] == frame_list[2]
  482. @requires_symbolicator
  483. @pytest.mark.symbolicator
  484. def test_sourcemap_webpack(self, process_with_symbolicator):
  485. self.project.update_option("sentry:scrape_javascript", False)
  486. release = Release.objects.create(
  487. organization_id=self.project.organization_id, version="abc"
  488. )
  489. release.add_project(self.project)
  490. for file in [
  491. "webpack1.min.js",
  492. "webpack2.min.js",
  493. "webpack1.min.js.map",
  494. "webpack2.min.js.map",
  495. ]:
  496. with open(get_fixture_path(file), "rb") as f:
  497. f1 = File.objects.create(
  498. name=file,
  499. type="release.file",
  500. headers={},
  501. )
  502. f1.putfile(f)
  503. ReleaseFile.objects.create(
  504. name=f"http://example.com/{f1.name}",
  505. release_id=release.id,
  506. organization_id=self.project.organization_id,
  507. file=f1,
  508. )
  509. data = {
  510. "timestamp": self.min_ago,
  511. "message": "hello",
  512. "platform": "javascript",
  513. "release": "abc",
  514. "exception": {
  515. "values": [
  516. {
  517. "type": "Error",
  518. "stacktrace": {
  519. "frames": [
  520. {
  521. "abs_path": "http://example.com/webpack1.min.js",
  522. "filename": "webpack1.min.js",
  523. "lineno": 1,
  524. "colno": 183,
  525. "function": "i",
  526. },
  527. {
  528. "abs_path": "http://example.com/webpack2.min.js",
  529. "filename": "webpack2.min.js",
  530. "lineno": 1,
  531. "colno": 183,
  532. "function": "i",
  533. },
  534. ]
  535. },
  536. }
  537. ]
  538. },
  539. }
  540. event = self.post_and_retrieve_event(data)
  541. exception = event.interfaces["exception"]
  542. frame_list = exception.values[0].stacktrace.frames
  543. # The first frame should be in_app.
  544. first_frame = frame_list[0]
  545. assert first_frame.in_app
  546. assert first_frame.function == "test"
  547. assert first_frame.pre_context == [
  548. " cb(data);",
  549. " }",
  550. "",
  551. " function test() {",
  552. " var data = {failed: true, value: 42};",
  553. ]
  554. assert first_frame.context_line == " invoke(data);"
  555. if process_with_symbolicator:
  556. assert first_frame.post_context == [
  557. " }",
  558. "",
  559. " return test;",
  560. "})();",
  561. ]
  562. else:
  563. assert first_frame.post_context == [" }", "", " return test;", "})();", ""]
  564. # The second frame should be identical to the first, except not in_app.
  565. second_frame = frame_list[1]
  566. assert not second_frame.in_app
  567. assert second_frame.function == first_frame.function
  568. assert second_frame.context_line == first_frame.context_line
  569. assert second_frame.pre_context == first_frame.pre_context
  570. assert second_frame.post_context == first_frame.post_context
  571. @requires_symbolicator
  572. @pytest.mark.symbolicator
  573. def test_sourcemap_source_expansion_ab_test(self, process_with_symbolicator):
  574. self.project.update_option("sentry:scrape_javascript", False)
  575. release = Release.objects.create(
  576. organization_id=self.project.organization_id, version="abc"
  577. )
  578. release.add_project(self.project)
  579. for file in ["file.min.js", "file1.js", "file2.js", "file.sourcemap.js"]:
  580. with open(get_fixture_path(file), "rb") as f:
  581. f1 = File.objects.create(
  582. name=file,
  583. type="release.file",
  584. headers={},
  585. )
  586. f1.putfile(f)
  587. ReleaseFile.objects.create(
  588. name=f"http://example.com/{f1.name}",
  589. release_id=release.id,
  590. organization_id=self.project.organization_id,
  591. file=f1,
  592. )
  593. data = {
  594. "timestamp": self.min_ago,
  595. "message": "hello",
  596. "platform": "javascript",
  597. "release": "abc",
  598. "exception": {
  599. "values": [
  600. {
  601. "type": "Error",
  602. "stacktrace": {
  603. "frames": [
  604. {
  605. "abs_path": "http://example.com/file.min.js",
  606. "filename": "file.min.js",
  607. "lineno": 1,
  608. "colno": 39,
  609. },
  610. # NOTE: Intentionally source is not retrieved from this HTML file
  611. {
  612. "function": 'function: "HTMLDocument.<anonymous>"',
  613. "abs_path": "http//example.com/index.html",
  614. "filename": "index.html",
  615. "lineno": 283,
  616. "colno": 17,
  617. "in_app": False,
  618. },
  619. ]
  620. },
  621. }
  622. ]
  623. },
  624. }
  625. with override_options({"symbolicator.sourcemaps-processing-ab-test": 1.0}):
  626. event = self.post_and_retrieve_event(data)
  627. assert event.data["errors"] == [
  628. {"type": "js_no_source", "url": "http//example.com/index.html"}
  629. ]
  630. exception = event.interfaces["exception"]
  631. frame_list = exception.values[0].stacktrace.frames
  632. frame = frame_list[0]
  633. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  634. expected = "\treturn a + b; // fôo"
  635. assert frame.context_line == expected
  636. assert frame.post_context == ["}", ""]
  637. raw_frame_list = exception.values[0].raw_stacktrace.frames
  638. raw_frame = raw_frame_list[0]
  639. assert not raw_frame.pre_context
  640. assert (
  641. raw_frame.context_line
  642. == 'function add(a,b){"use strict";return a+b}function multiply(a,b){"use strict";return a*b}function '
  643. 'divide(a,b){"use strict";try{return multip {snip}'
  644. )
  645. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js", ""]
  646. assert raw_frame.lineno == 1
  647. # Since we couldn't expand source for the 2nd frame, both
  648. # its raw and original form should be identical
  649. assert raw_frame_list[1] == frame_list[1]
  650. @requires_symbolicator
  651. @pytest.mark.symbolicator
  652. def test_sourcemap_embedded_source_expansion(self, process_with_symbolicator):
  653. self.project.update_option("sentry:scrape_javascript", False)
  654. release = Release.objects.create(
  655. organization_id=self.project.organization_id, version="abc"
  656. )
  657. release.add_project(self.project)
  658. for file in ["embedded.js", "embedded.js.map"]:
  659. with open(get_fixture_path(file), "rb") as f:
  660. f1 = File.objects.create(
  661. name=file,
  662. type="release.file",
  663. headers={},
  664. )
  665. f1.putfile(f)
  666. ReleaseFile.objects.create(
  667. name=f"http://example.com/{f1.name}",
  668. release_id=release.id,
  669. organization_id=self.project.organization_id,
  670. file=f1,
  671. )
  672. data = {
  673. "timestamp": self.min_ago,
  674. "message": "hello",
  675. "platform": "javascript",
  676. "release": "abc",
  677. "exception": {
  678. "values": [
  679. {
  680. "type": "Error",
  681. "stacktrace": {
  682. "frames": [
  683. {
  684. "abs_path": "http://example.com/embedded.js",
  685. "filename": "file.min.js",
  686. "lineno": 1,
  687. "colno": 39,
  688. },
  689. # NOTE: Intentionally source is not retrieved from this HTML file
  690. {
  691. "function": 'function: "HTMLDocument.<anonymous>"',
  692. "abs_path": "http//example.com/index.html",
  693. "filename": "index.html",
  694. "lineno": 283,
  695. "colno": 17,
  696. "in_app": False,
  697. },
  698. ]
  699. },
  700. }
  701. ]
  702. },
  703. }
  704. event = self.post_and_retrieve_event(data)
  705. assert event.data["errors"] == [
  706. {"type": "js_no_source", "url": "http//example.com/index.html"}
  707. ]
  708. exception = event.interfaces["exception"]
  709. frame_list = exception.values[0].stacktrace.frames
  710. frame = frame_list[0]
  711. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  712. expected = "\treturn a + b; // fôo"
  713. assert frame.context_line == expected
  714. if process_with_symbolicator:
  715. assert frame.post_context == ["}"]
  716. else:
  717. assert frame.post_context == ["}", ""]
  718. @requires_symbolicator
  719. @pytest.mark.symbolicator
  720. def test_sourcemap_nofiles_source_expansion(self, process_with_symbolicator):
  721. project = self.project
  722. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  723. release.add_project(project)
  724. with open(get_fixture_path("nofiles.js"), "rb") as f:
  725. f_minified = File.objects.create(
  726. name="nofiles.js", type="release.file", headers={"Content-Type": "application/json"}
  727. )
  728. f_minified.putfile(f)
  729. ReleaseFile.objects.create(
  730. name=f"~/{f_minified.name}",
  731. release_id=release.id,
  732. organization_id=project.organization_id,
  733. file=f_minified,
  734. )
  735. with open(get_fixture_path("nofiles.js.map"), "rb") as f:
  736. f_sourcemap = File.objects.create(
  737. name="nofiles.js.map",
  738. type="release.file",
  739. headers={"Content-Type": "application/json"},
  740. )
  741. f_sourcemap.putfile(f)
  742. ReleaseFile.objects.create(
  743. name=f"app:///{f_sourcemap.name}",
  744. release_id=release.id,
  745. organization_id=project.organization_id,
  746. file=f_sourcemap,
  747. )
  748. data = {
  749. "timestamp": self.min_ago,
  750. "message": "hello",
  751. "platform": "javascript",
  752. "release": "abc",
  753. "exception": {
  754. "values": [
  755. {
  756. "type": "Error",
  757. "stacktrace": {
  758. "frames": [{"abs_path": "app:///nofiles.js", "lineno": 1, "colno": 39}]
  759. },
  760. }
  761. ]
  762. },
  763. }
  764. event = self.post_and_retrieve_event(data)
  765. assert "errors" not in event.data
  766. exception = event.interfaces["exception"]
  767. frame_list = exception.values[0].stacktrace.frames
  768. assert len(frame_list) == 1
  769. frame = frame_list[0]
  770. assert frame.abs_path == "app:///nofiles.js"
  771. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  772. assert frame.context_line == "\treturn a + b; // fôo"
  773. if process_with_symbolicator:
  774. assert frame.post_context == ["}"]
  775. else:
  776. assert frame.post_context == ["}", ""]
  777. @requires_symbolicator
  778. @pytest.mark.symbolicator
  779. def test_indexed_sourcemap_source_expansion(self, process_with_symbolicator):
  780. self.project.update_option("sentry:scrape_javascript", False)
  781. release = Release.objects.create(
  782. organization_id=self.project.organization_id, version="abc"
  783. )
  784. release.add_project(self.project)
  785. for file in ["indexed.min.js", "file1.js", "file2.js", "indexed.sourcemap.js"]:
  786. with open(get_fixture_path(file), "rb") as f:
  787. f1 = File.objects.create(
  788. name=file,
  789. type="release.file",
  790. headers={},
  791. )
  792. f1.putfile(f)
  793. ReleaseFile.objects.create(
  794. name=f"http://example.com/{f1.name}",
  795. release_id=release.id,
  796. organization_id=self.project.organization_id,
  797. file=f1,
  798. )
  799. data = {
  800. "timestamp": self.min_ago,
  801. "message": "hello",
  802. "platform": "javascript",
  803. "release": "abc",
  804. "exception": {
  805. "values": [
  806. {
  807. "type": "Error",
  808. "stacktrace": {
  809. "frames": [
  810. {
  811. "abs_path": "http://example.com/indexed.min.js",
  812. "filename": "indexed.min.js",
  813. "lineno": 1,
  814. "colno": 39,
  815. },
  816. {
  817. "abs_path": "http://example.com/indexed.min.js",
  818. "filename": "indexed.min.js",
  819. "lineno": 2,
  820. "colno": 44,
  821. },
  822. ]
  823. },
  824. }
  825. ]
  826. },
  827. }
  828. event = self.post_and_retrieve_event(data)
  829. assert "errors" not in event.data
  830. exception = event.interfaces["exception"]
  831. frame_list = exception.values[0].stacktrace.frames
  832. frame = frame_list[0]
  833. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  834. expected = "\treturn a + b; // fôo"
  835. assert frame.context_line == expected
  836. if process_with_symbolicator:
  837. assert frame.post_context == ["}"]
  838. else:
  839. assert frame.post_context == ["}", ""]
  840. raw_frame_list = exception.values[0].raw_stacktrace.frames
  841. raw_frame = raw_frame_list[0]
  842. assert not raw_frame.pre_context
  843. assert raw_frame.context_line == 'function add(a,b){"use strict";return a+b}'
  844. if process_with_symbolicator:
  845. assert raw_frame.post_context == [
  846. 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  847. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}",
  848. "//# sourceMappingURL=indexed.sourcemap.js",
  849. ]
  850. else:
  851. assert raw_frame.post_context == [
  852. 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  853. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}",
  854. "//# sourceMappingURL=indexed.sourcemap.js",
  855. "",
  856. ]
  857. assert raw_frame.lineno == 1
  858. frame = frame_list[1]
  859. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  860. assert frame.context_line == "\treturn a * b;"
  861. assert frame.post_context == [
  862. "}",
  863. "function divide(a, b) {",
  864. '\t"use strict";',
  865. "\ttry {",
  866. "\t\treturn multiply(add(a, b), a, b) / c;",
  867. ]
  868. raw_frame = raw_frame_list[1]
  869. assert raw_frame.pre_context == ['function add(a,b){"use strict";return a+b}']
  870. assert (
  871. raw_frame.context_line
  872. == 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  873. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}"
  874. )
  875. if process_with_symbolicator:
  876. assert raw_frame.post_context == ["//# sourceMappingURL=indexed.sourcemap.js"]
  877. else:
  878. assert raw_frame.post_context == ["//# sourceMappingURL=indexed.sourcemap.js", ""]
  879. assert raw_frame.lineno == 2
  880. @requires_symbolicator
  881. @pytest.mark.symbolicator
  882. def test_expansion_via_debug(self, process_with_symbolicator):
  883. project = self.project
  884. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  885. release.add_project(project)
  886. # file.min.js
  887. # ------------
  888. with open(get_fixture_path("file.min.js"), "rb") as f:
  889. f_minified = File.objects.create(
  890. name="file.min.js",
  891. type="release.file",
  892. headers={"Content-Type": "application/json"},
  893. )
  894. f_minified.putfile(f)
  895. # Intentionally omit hostname - use alternate artifact path lookup instead
  896. # /file1.js vs http://example.com/file1.js
  897. ReleaseFile.objects.create(
  898. name=f"~/{f_minified.name}?foo=bar",
  899. release_id=release.id,
  900. organization_id=project.organization_id,
  901. file=f_minified,
  902. )
  903. # file1.js
  904. # ---------
  905. with open(get_fixture_path("file1.js"), "rb") as f:
  906. f1 = File.objects.create(
  907. name="file1.js", type="release.file", headers={"Content-Type": "application/json"}
  908. )
  909. f1.putfile(f)
  910. ReleaseFile.objects.create(
  911. name=f"http://example.com/{f1.name}",
  912. release_id=release.id,
  913. organization_id=project.organization_id,
  914. file=f1,
  915. )
  916. # file2.js
  917. # ----------
  918. with open(get_fixture_path("file2.js"), "rb") as f:
  919. f2 = File.objects.create(
  920. name="file2.js", type="release.file", headers={"Content-Type": "application/json"}
  921. )
  922. f2.putfile(f)
  923. ReleaseFile.objects.create(
  924. name=f"http://example.com/{f2.name}",
  925. release_id=release.id,
  926. organization_id=project.organization_id,
  927. file=f2,
  928. )
  929. # To verify that the full url has priority over the relative url,
  930. # we will also add a second ReleaseFile alias for file2.js (f3) w/o
  931. # hostname that points to an empty file. If the processor chooses
  932. # this empty file over the correct file2.js, it will not locate
  933. # context for the 2nd frame.
  934. with open(get_fixture_path("empty.js"), "rb") as f:
  935. f2_empty = File.objects.create(
  936. name="empty.js", type="release.file", headers={"Content-Type": "application/json"}
  937. )
  938. f2_empty.putfile(f)
  939. ReleaseFile.objects.create(
  940. name=f"~/{f2.name}", # intentionally using f2.name ("file2.js")
  941. release_id=release.id,
  942. organization_id=project.organization_id,
  943. file=f2_empty,
  944. )
  945. # sourcemap
  946. # ----------
  947. with open(get_fixture_path("file.sourcemap.js"), "rb") as f:
  948. f_sourcemap = File.objects.create(
  949. name="file.sourcemap.js",
  950. type="release.file",
  951. headers={"Content-Type": "application/json"},
  952. )
  953. f_sourcemap.putfile(f)
  954. ReleaseFile.objects.create(
  955. name=f"http://example.com/{f_sourcemap.name}",
  956. release_id=release.id,
  957. organization_id=project.organization_id,
  958. file=f_sourcemap,
  959. )
  960. data = {
  961. "timestamp": self.min_ago,
  962. "message": "hello",
  963. "platform": "javascript",
  964. "release": "abc",
  965. "exception": {
  966. "values": [
  967. {
  968. "type": "Error",
  969. "stacktrace": {
  970. "frames": [
  971. {
  972. "abs_path": "http://example.com/file.min.js?foo=bar",
  973. "filename": "file.min.js",
  974. "lineno": 1,
  975. "colno": 39,
  976. },
  977. {
  978. "abs_path": "http://example.com/file.min.js?foo=bar",
  979. "filename": "file.min.js",
  980. "lineno": 1,
  981. "colno": 79,
  982. },
  983. ]
  984. },
  985. }
  986. ]
  987. },
  988. }
  989. event = self.post_and_retrieve_event(data)
  990. assert "errors" not in event.data
  991. exception = event.interfaces["exception"]
  992. frame_list = exception.values[0].stacktrace.frames
  993. frame = frame_list[0]
  994. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  995. assert frame.context_line == "\treturn a + b; // fôo"
  996. if process_with_symbolicator:
  997. assert frame.post_context == ["}"]
  998. else:
  999. assert frame.post_context == ["}", ""]
  1000. frame = frame_list[1]
  1001. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1002. assert frame.context_line == "\treturn a * b;"
  1003. assert frame.post_context == [
  1004. "}",
  1005. "function divide(a, b) {",
  1006. '\t"use strict";',
  1007. "\ttry {",
  1008. "\t\treturn multiply(add(a, b), a, b) / c;",
  1009. ]
  1010. @requires_symbolicator
  1011. @pytest.mark.symbolicator
  1012. def test_expansion_via_distribution_release_artifacts(self, process_with_symbolicator):
  1013. project = self.project
  1014. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1015. release.add_project(project)
  1016. dist = release.add_dist("foo")
  1017. # file.min.js
  1018. # ------------
  1019. with open(get_fixture_path("file.min.js"), "rb") as f:
  1020. f_minified = File.objects.create(
  1021. name="file.min.js",
  1022. type="release.file",
  1023. headers={"Content-Type": "application/json"},
  1024. )
  1025. f_minified.putfile(f)
  1026. # Intentionally omit hostname - use alternate artifact path lookup instead
  1027. # /file1.js vs http://example.com/file1.js
  1028. ReleaseFile.objects.create(
  1029. name=f"~/{f_minified.name}?foo=bar",
  1030. release_id=release.id,
  1031. dist_id=dist.id,
  1032. organization_id=project.organization_id,
  1033. file=f_minified,
  1034. )
  1035. # file1.js
  1036. # ---------
  1037. with open(get_fixture_path("file1.js"), "rb") as f:
  1038. f1 = File.objects.create(
  1039. name="file1.js",
  1040. type="release.file",
  1041. headers={"Content-Type": "application/json"},
  1042. )
  1043. f1.putfile(f)
  1044. ReleaseFile.objects.create(
  1045. name=f"http://example.com/{f1.name}",
  1046. release_id=release.id,
  1047. dist_id=dist.id,
  1048. organization_id=project.organization_id,
  1049. file=f1,
  1050. )
  1051. # file2.js
  1052. # ----------
  1053. with open(get_fixture_path("file2.js"), "rb") as f:
  1054. f2 = File.objects.create(
  1055. name="file2.js",
  1056. type="release.file",
  1057. headers={"Content-Type": "application/json"},
  1058. )
  1059. f2.putfile(f)
  1060. ReleaseFile.objects.create(
  1061. name=f"http://example.com/{f2.name}",
  1062. release_id=release.id,
  1063. dist_id=dist.id,
  1064. organization_id=project.organization_id,
  1065. file=f2,
  1066. )
  1067. # To verify that the full url has priority over the relative url,
  1068. # we will also add a second ReleaseFile alias for file2.js (f3) w/o
  1069. # hostname that points to an empty file. If the processor chooses
  1070. # this empty file over the correct file2.js, it will not locate
  1071. # context for the 2nd frame.
  1072. with open(get_fixture_path("empty.js"), "rb") as f:
  1073. f2_empty = File.objects.create(
  1074. name="empty.js",
  1075. type="release.file",
  1076. headers={"Content-Type": "application/json"},
  1077. )
  1078. f2_empty.putfile(f)
  1079. ReleaseFile.objects.create(
  1080. name=f"~/{f2.name}", # intentionally using f2.name ("file2.js")
  1081. release_id=release.id,
  1082. dist_id=dist.id,
  1083. organization_id=project.organization_id,
  1084. file=f2_empty,
  1085. )
  1086. # sourcemap
  1087. # ----------
  1088. with open(get_fixture_path("file.sourcemap.js"), "rb") as f:
  1089. f_sourcemap = File.objects.create(
  1090. name="file.sourcemap.js",
  1091. type="release.file",
  1092. headers={"Content-Type": "application/json"},
  1093. )
  1094. f_sourcemap.putfile(f)
  1095. ReleaseFile.objects.create(
  1096. name=f"http://example.com/{f_sourcemap.name}",
  1097. release_id=release.id,
  1098. dist_id=dist.id,
  1099. organization_id=project.organization_id,
  1100. file=f_sourcemap,
  1101. )
  1102. data = {
  1103. "timestamp": self.min_ago,
  1104. "message": "hello",
  1105. "platform": "javascript",
  1106. "release": "abc",
  1107. "dist": "foo",
  1108. "exception": {
  1109. "values": [
  1110. {
  1111. "type": "Error",
  1112. "stacktrace": {
  1113. "frames": [
  1114. {
  1115. "abs_path": "http://example.com/file.min.js?foo=bar",
  1116. "filename": "file.min.js",
  1117. "lineno": 1,
  1118. "colno": 39,
  1119. },
  1120. {
  1121. "abs_path": "http://example.com/file.min.js?foo=bar",
  1122. "filename": "file.min.js",
  1123. "lineno": 1,
  1124. "colno": 79,
  1125. },
  1126. ]
  1127. },
  1128. }
  1129. ]
  1130. },
  1131. }
  1132. event = self.post_and_retrieve_event(data)
  1133. assert "errors" not in event.data
  1134. exception = event.interfaces["exception"]
  1135. frame_list = exception.values[0].stacktrace.frames
  1136. frame = frame_list[0]
  1137. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1138. assert frame.context_line == "\treturn a + b; // fôo"
  1139. if process_with_symbolicator:
  1140. assert frame.post_context == ["}"]
  1141. else:
  1142. assert frame.post_context == ["}", ""]
  1143. frame = frame_list[1]
  1144. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1145. assert frame.context_line == "\treturn a * b;"
  1146. assert frame.post_context == [
  1147. "}",
  1148. "function divide(a, b) {",
  1149. '\t"use strict";',
  1150. "\ttry {",
  1151. "\t\treturn multiply(add(a, b), a, b) / c;",
  1152. ]
  1153. @responses.activate
  1154. def test_sourcemap_expansion_with_missing_source(self):
  1155. """
  1156. Tests a successful sourcemap expansion that points to source files
  1157. that are not found.
  1158. """
  1159. responses.add(
  1160. responses.GET,
  1161. "http://example.com/file.min.js",
  1162. body=load_fixture("file.min.js"),
  1163. content_type="application/javascript; charset=utf-8",
  1164. )
  1165. responses.add(
  1166. responses.GET,
  1167. "http://example.com/file.sourcemap.js",
  1168. body=load_fixture("file.sourcemap.js"),
  1169. content_type="application/json; charset=utf-8",
  1170. )
  1171. responses.add(responses.GET, "http://example.com/file1.js", body="Not Found", status=404)
  1172. data = {
  1173. "timestamp": self.min_ago,
  1174. "message": "hello",
  1175. "platform": "javascript",
  1176. "exception": {
  1177. "values": [
  1178. {
  1179. "type": "Error",
  1180. "stacktrace": {
  1181. # Add two frames. We only want to see the
  1182. # error once though.
  1183. "frames": [
  1184. {
  1185. "abs_path": "http://example.com/file.min.js",
  1186. "filename": "file.min.js",
  1187. "lineno": 1,
  1188. "colno": 39,
  1189. },
  1190. {
  1191. "abs_path": "http://example.com/file.min.js",
  1192. "filename": "file.min.js",
  1193. "lineno": 1,
  1194. "colno": 39,
  1195. },
  1196. ]
  1197. },
  1198. }
  1199. ]
  1200. },
  1201. }
  1202. event = self.post_and_retrieve_event(data)
  1203. assert event.data["errors"] == [
  1204. {"url": "http://example.com/file1.js", "type": "fetch_invalid_http_code", "value": 404}
  1205. ]
  1206. exception = event.interfaces["exception"]
  1207. frame_list = exception.values[0].stacktrace.frames
  1208. frame = frame_list[0]
  1209. # no context information ...
  1210. assert not frame.pre_context
  1211. assert not frame.context_line
  1212. assert not frame.post_context
  1213. # ... but line, column numbers are still correctly mapped
  1214. assert frame.lineno == 3
  1215. assert frame.colno == 9
  1216. @responses.activate
  1217. def test_failed_sourcemap_expansion(self):
  1218. """
  1219. Tests attempting to parse an indexed source map where each section has a "url"
  1220. property - this is unsupported and should fail.
  1221. """
  1222. responses.add(
  1223. responses.GET,
  1224. "http://example.com/unsupported.min.js",
  1225. body=load_fixture("unsupported.min.js"),
  1226. content_type="application/javascript; charset=utf-8",
  1227. )
  1228. responses.add(
  1229. responses.GET,
  1230. "http://example.com/unsupported.sourcemap.js",
  1231. body=load_fixture("unsupported.sourcemap.js"),
  1232. content_type="application/json; charset=utf-8",
  1233. )
  1234. data = {
  1235. "timestamp": self.min_ago,
  1236. "message": "hello",
  1237. "platform": "javascript",
  1238. "exception": {
  1239. "values": [
  1240. {
  1241. "type": "Error",
  1242. "stacktrace": {
  1243. "frames": [
  1244. {
  1245. "abs_path": "http://example.com/unsupported.min.js",
  1246. "filename": "indexed.min.js",
  1247. "lineno": 1,
  1248. "colno": 39,
  1249. }
  1250. ]
  1251. },
  1252. }
  1253. ]
  1254. },
  1255. }
  1256. event = self.post_and_retrieve_event(data)
  1257. assert event.data["errors"] == [
  1258. {"url": "http://example.com/unsupported.sourcemap.js", "type": "js_invalid_source"}
  1259. ]
  1260. def test_failed_sourcemap_expansion_data_url(self):
  1261. data = {
  1262. "timestamp": self.min_ago,
  1263. "message": "hello",
  1264. "platform": "javascript",
  1265. "exception": {
  1266. "values": [
  1267. {
  1268. "type": "Error",
  1269. "stacktrace": {
  1270. "frames": [
  1271. {
  1272. "abs_path": "data:application/javascript,base46,asfasf",
  1273. "filename": "indexed.min.js",
  1274. "lineno": 1,
  1275. "colno": 39,
  1276. }
  1277. ]
  1278. },
  1279. }
  1280. ]
  1281. },
  1282. }
  1283. event = self.post_and_retrieve_event(data)
  1284. assert event.data["errors"] == [{"url": "<data url>", "type": "js_no_source"}]
  1285. @responses.activate
  1286. def test_failed_sourcemap_expansion_missing_location_entirely(self):
  1287. responses.add(
  1288. responses.GET,
  1289. "http://example.com/indexed.min.js",
  1290. body="//# sourceMappingURL=indexed.sourcemap.js",
  1291. )
  1292. responses.add(responses.GET, "http://example.com/indexed.sourcemap.js", body="{}")
  1293. data = {
  1294. "timestamp": self.min_ago,
  1295. "message": "hello",
  1296. "platform": "javascript",
  1297. "exception": {
  1298. "values": [
  1299. {
  1300. "type": "Error",
  1301. "stacktrace": {
  1302. "frames": [
  1303. {
  1304. "abs_path": "http://example.com/indexed.min.js",
  1305. "filename": "indexed.min.js",
  1306. "lineno": 1,
  1307. "colno": 1,
  1308. },
  1309. {
  1310. "abs_path": "http://example.com/indexed.min.js",
  1311. "filename": "indexed.min.js",
  1312. },
  1313. ]
  1314. },
  1315. }
  1316. ]
  1317. },
  1318. }
  1319. event = self.post_and_retrieve_event(data)
  1320. assert "errors" not in event.data
  1321. @responses.activate
  1322. def test_html_response_for_js(self):
  1323. responses.add(
  1324. responses.GET,
  1325. "http://example.com/invalid_file1.js",
  1326. body=" <!DOCTYPE html><html><head></head><body></body></html>",
  1327. )
  1328. responses.add(
  1329. responses.GET,
  1330. "http://example.com/invalid_file2.js",
  1331. body="<!doctype html><html><head></head><body></body></html>",
  1332. )
  1333. responses.add(
  1334. responses.GET,
  1335. "http://example.com/valid_file.html",
  1336. body=(
  1337. "<!doctype html><html><head></head><body><script>/*legit case*/</script></body></html>"
  1338. ),
  1339. )
  1340. data = {
  1341. "timestamp": self.min_ago,
  1342. "message": "hello",
  1343. "platform": "javascript",
  1344. "exception": {
  1345. "values": [
  1346. {
  1347. "type": "Error",
  1348. "stacktrace": {
  1349. "frames": [
  1350. {
  1351. "abs_path": "http://example.com/invalid_file1.js",
  1352. "filename": "invalid_file1.js",
  1353. "lineno": 1,
  1354. "colno": 39,
  1355. },
  1356. {
  1357. "abs_path": "http://example.com/invalid_file2.js",
  1358. "filename": "invalid_file2.js",
  1359. "lineno": 1,
  1360. "colno": 39,
  1361. },
  1362. {
  1363. "abs_path": "http://example.com/valid_file.html",
  1364. "filename": "valid_file.html",
  1365. "lineno": 1,
  1366. "colno": 1,
  1367. },
  1368. ]
  1369. },
  1370. }
  1371. ]
  1372. },
  1373. }
  1374. event = self.post_and_retrieve_event(data)
  1375. assert event.data["errors"] == [
  1376. {"url": "http://example.com/invalid_file1.js", "type": "js_invalid_content"},
  1377. {"url": "http://example.com/invalid_file2.js", "type": "js_invalid_content"},
  1378. ]
  1379. def _test_expansion_via_release_archive(
  1380. self, link_sourcemaps: bool, process_with_symbolicator: bool
  1381. ):
  1382. project = self.project
  1383. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1384. release.add_project(project)
  1385. manifest = {
  1386. "org": self.organization.slug,
  1387. "release": release.version,
  1388. "files": {
  1389. "files/_/_/file.min.js": {
  1390. "url": "http://example.com/file.min.js",
  1391. "type": "minified_source",
  1392. },
  1393. "files/_/_/file1.js": {
  1394. "url": "http://example.com/file1.js",
  1395. "type": "source",
  1396. },
  1397. "files/_/_/file2.js": {
  1398. "url": "http://example.com/file2.js",
  1399. "type": "source",
  1400. },
  1401. "files/_/_/file.sourcemap.js": {
  1402. "url": "http://example.com/file.sourcemap.js",
  1403. "type": "source_map",
  1404. },
  1405. },
  1406. }
  1407. compressed = BytesIO(b"SYSB")
  1408. with zipfile.ZipFile(compressed, "a") as zip_file:
  1409. for rel_path, entry in manifest["files"].items():
  1410. name = os.path.basename(rel_path)
  1411. content = load_fixture(name)
  1412. if name == "file.min.js" and not link_sourcemaps:
  1413. # Remove link to source map, add to header instead
  1414. content = content.replace(b"//@ sourceMappingURL=file.sourcemap.js", b"")
  1415. entry["headers"] = {"Sourcemap": "file.sourcemap.js"}
  1416. zip_file.writestr(rel_path, content)
  1417. zip_file.writestr("manifest.json", json.dumps(manifest))
  1418. compressed.seek(0)
  1419. file = File.objects.create(name="doesnt_matter", type="release.bundle")
  1420. file.putfile(compressed)
  1421. update_artifact_index(release, None, file)
  1422. data = {
  1423. "timestamp": self.min_ago,
  1424. "message": "hello",
  1425. "platform": "javascript",
  1426. "release": "abc",
  1427. "exception": {
  1428. "values": [
  1429. {
  1430. "type": "Error",
  1431. "stacktrace": {
  1432. "frames": [
  1433. {
  1434. "abs_path": "http://example.com/file.min.js",
  1435. "filename": "file.min.js",
  1436. "lineno": 1,
  1437. "colno": 39,
  1438. },
  1439. {
  1440. "abs_path": "http://example.com/file.min.js",
  1441. "filename": "file.min.js",
  1442. "lineno": 1,
  1443. "colno": 79,
  1444. },
  1445. ]
  1446. },
  1447. }
  1448. ]
  1449. },
  1450. }
  1451. event = self.post_and_retrieve_event(data)
  1452. assert "errors" not in event.data
  1453. exception = event.interfaces["exception"]
  1454. frame_list = exception.values[0].stacktrace.frames
  1455. frame = frame_list[0]
  1456. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1457. assert frame.context_line == "\treturn a + b; // fôo"
  1458. if process_with_symbolicator:
  1459. assert frame.post_context == ["}"]
  1460. else:
  1461. assert frame.post_context == ["}", ""]
  1462. frame = frame_list[1]
  1463. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1464. assert frame.context_line == "\treturn a * b;"
  1465. assert frame.post_context == [
  1466. "}",
  1467. "function divide(a, b) {",
  1468. '\t"use strict";',
  1469. "\ttry {",
  1470. "\t\treturn multiply(add(a, b), a, b) / c;",
  1471. ]
  1472. @requires_symbolicator
  1473. @pytest.mark.symbolicator
  1474. def test_expansion_via_release_archive(self, process_with_symbolicator):
  1475. self._test_expansion_via_release_archive(
  1476. link_sourcemaps=True, process_with_symbolicator=process_with_symbolicator
  1477. )
  1478. @requires_symbolicator
  1479. @pytest.mark.symbolicator
  1480. def test_expansion_via_release_archive_no_sourcemap_link(self, process_with_symbolicator):
  1481. self._test_expansion_via_release_archive(
  1482. link_sourcemaps=False, process_with_symbolicator=process_with_symbolicator
  1483. )
  1484. @requires_symbolicator
  1485. @pytest.mark.symbolicator
  1486. def test_node_processing(self, process_with_symbolicator):
  1487. project = self.project
  1488. release = Release.objects.create(
  1489. organization_id=project.organization_id, version="nodeabc123"
  1490. )
  1491. release.add_project(project)
  1492. with open(get_fixture_path("dist.bundle.js"), "rb") as f:
  1493. f_minified = File.objects.create(
  1494. name="dist.bundle.js",
  1495. type="release.file",
  1496. headers={"Content-Type": "application/javascript"},
  1497. )
  1498. f_minified.putfile(f)
  1499. ReleaseFile.objects.create(
  1500. name=f"~/{f_minified.name}",
  1501. release_id=release.id,
  1502. organization_id=project.organization_id,
  1503. file=f_minified,
  1504. )
  1505. with open(get_fixture_path("dist.bundle.js.map"), "rb") as f:
  1506. f_sourcemap = File.objects.create(
  1507. name="dist.bundle.js.map",
  1508. type="release.file",
  1509. headers={"Content-Type": "application/javascript"},
  1510. )
  1511. f_sourcemap.putfile(f)
  1512. ReleaseFile.objects.create(
  1513. name=f"~/{f_sourcemap.name}",
  1514. release_id=release.id,
  1515. organization_id=project.organization_id,
  1516. file=f_sourcemap,
  1517. )
  1518. data = {
  1519. "timestamp": self.min_ago,
  1520. "message": "hello",
  1521. "platform": "node",
  1522. "release": "nodeabc123",
  1523. "exception": {
  1524. "values": [
  1525. {
  1526. "type": "Error",
  1527. "stacktrace": {
  1528. "frames": [
  1529. {
  1530. "filename": "app:///dist.bundle.js",
  1531. "function": "bar",
  1532. "lineno": 9,
  1533. "colno": 2321,
  1534. },
  1535. {
  1536. "filename": "app:///dist.bundle.js",
  1537. "function": "foo",
  1538. "lineno": 3,
  1539. "colno": 2308,
  1540. },
  1541. {
  1542. "filename": "app:///dist.bundle.js",
  1543. "function": "App",
  1544. "lineno": 3,
  1545. "colno": 1011,
  1546. },
  1547. {
  1548. "filename": "app:///dist.bundle.js",
  1549. "function": "Object.<anonymous>",
  1550. "lineno": 1,
  1551. "colno": 1014,
  1552. },
  1553. {
  1554. "filename": "app:///dist.bundle.js",
  1555. "function": "__webpack_require__",
  1556. "lineno": 20,
  1557. "colno": 30,
  1558. },
  1559. {
  1560. "filename": "app:///dist.bundle.js",
  1561. "function": "<unknown>",
  1562. "lineno": 18,
  1563. "colno": 63,
  1564. },
  1565. ]
  1566. },
  1567. }
  1568. ]
  1569. },
  1570. }
  1571. event = self.post_and_retrieve_event(data)
  1572. exception = event.interfaces["exception"]
  1573. frame_list = exception.values[0].stacktrace.frames
  1574. assert len(frame_list) == 6
  1575. def assert_abs_path(abs_path):
  1576. # This makes the test assertion forward compatible with percent-encoded URLs
  1577. # See https://github.com/getsentry/symbolicator/pull/1137
  1578. assert abs_path in (
  1579. "webpack:///webpack/bootstrap d9a5a31d9276b73873d3",
  1580. "webpack:///webpack/bootstrap%20d9a5a31d9276b73873d3",
  1581. )
  1582. assert_abs_path(frame_list[0].abs_path)
  1583. assert frame_list[0].function == "bar"
  1584. assert frame_list[0].lineno == 8
  1585. assert_abs_path(frame_list[1].abs_path)
  1586. assert frame_list[1].function == "foo"
  1587. assert frame_list[1].lineno == 2
  1588. assert_abs_path(frame_list[2].abs_path)
  1589. assert frame_list[2].function == "App"
  1590. assert frame_list[2].lineno == 2
  1591. assert_abs_path(frame_list[3].abs_path)
  1592. assert frame_list[3].function == "Object.<anonymous>"
  1593. assert frame_list[3].lineno == 1
  1594. assert_abs_path(frame_list[4].abs_path)
  1595. assert frame_list[4].function == "__webpack_require__"
  1596. assert frame_list[4].lineno == 19
  1597. assert_abs_path(frame_list[5].abs_path)
  1598. assert frame_list[5].function == "<unknown>"
  1599. assert frame_list[5].lineno == 16
  1600. @responses.activate
  1601. def test_no_fetch_from_http(self):
  1602. responses.add(
  1603. responses.GET,
  1604. "http://example.com/node_app.min.js",
  1605. body=load_fixture("node_app.min.js"),
  1606. content_type="application/javascript; charset=utf-8",
  1607. )
  1608. responses.add(
  1609. responses.GET,
  1610. "http://example.com/node_app.min.js.map",
  1611. body=load_fixture("node_app.min.js.map"),
  1612. content_type="application/javascript; charset=utf-8",
  1613. )
  1614. data = {
  1615. "timestamp": self.min_ago,
  1616. "message": "hello",
  1617. "platform": "node",
  1618. "exception": {
  1619. "values": [
  1620. {
  1621. "type": "Error",
  1622. "stacktrace": {
  1623. "frames": [
  1624. {
  1625. "abs_path": "node_bootstrap.js",
  1626. "filename": "node_bootstrap.js",
  1627. "lineno": 1,
  1628. "colno": 38,
  1629. },
  1630. {
  1631. "abs_path": "timers.js",
  1632. "filename": "timers.js",
  1633. "lineno": 1,
  1634. "colno": 39,
  1635. },
  1636. {
  1637. "abs_path": "webpack:///internal",
  1638. "filename": "internal",
  1639. "lineno": 1,
  1640. "colno": 43,
  1641. },
  1642. {
  1643. "abs_path": "webpack:///~/some_dep/file.js",
  1644. "filename": "file.js",
  1645. "lineno": 1,
  1646. "colno": 41,
  1647. },
  1648. {
  1649. "abs_path": "webpack:///./node_modules/file.js",
  1650. "filename": "file.js",
  1651. "lineno": 1,
  1652. "colno": 42,
  1653. },
  1654. {
  1655. "abs_path": "http://example.com/node_app.min.js",
  1656. "filename": "node_app.min.js",
  1657. "lineno": 1,
  1658. "colno": 40,
  1659. },
  1660. ]
  1661. },
  1662. }
  1663. ]
  1664. },
  1665. }
  1666. event = self.post_and_retrieve_event(data)
  1667. exception = event.interfaces["exception"]
  1668. frame_list = exception.values[0].stacktrace.frames
  1669. # This one should not process, so this one should be none.
  1670. assert exception.values[0].raw_stacktrace is None
  1671. # None of the in app should update
  1672. for x in range(6):
  1673. assert not frame_list[x].in_app
  1674. @responses.activate
  1675. def test_html_file_with_query_param_ending_with_js_extension(self):
  1676. responses.add(
  1677. responses.GET,
  1678. "http://example.com/file.html",
  1679. body=(
  1680. "<!doctype html><html><head></head><body><script>/*legit case*/</script></body></html>"
  1681. ),
  1682. )
  1683. data = {
  1684. "timestamp": self.min_ago,
  1685. "message": "hello",
  1686. "platform": "javascript",
  1687. "exception": {
  1688. "values": [
  1689. {
  1690. "type": "Error",
  1691. "stacktrace": {
  1692. "frames": [
  1693. {
  1694. "abs_path": "http://example.com/file.html?sw=iddqd1337.js",
  1695. "filename": "file.html",
  1696. "lineno": 1,
  1697. "colno": 1,
  1698. },
  1699. ]
  1700. },
  1701. }
  1702. ]
  1703. },
  1704. }
  1705. event = self.post_and_retrieve_event(data)
  1706. assert "errors" not in event.data
  1707. def test_expansion_with_allow_scraping_false(self):
  1708. project = self.project
  1709. project.organization.update_option("sentry:scrape_javascript", False)
  1710. data = {
  1711. "timestamp": self.min_ago,
  1712. "message": "hello",
  1713. "platform": "javascript",
  1714. "release": "1.0",
  1715. "exception": {
  1716. "values": [
  1717. {
  1718. "type": "Error",
  1719. "stacktrace": {
  1720. "frames": [
  1721. {
  1722. "abs_path": "http://example.com/file.min.js",
  1723. "filename": "file.min.js",
  1724. "lineno": 1,
  1725. "colno": 39,
  1726. },
  1727. ]
  1728. },
  1729. }
  1730. ]
  1731. },
  1732. }
  1733. event = self.post_and_retrieve_event(data)
  1734. assert len(event.data["errors"]) == 1
  1735. assert event.data["errors"][0] == {
  1736. "type": "js_scraping_disabled",
  1737. "url": "http://example.com/file.min.js",
  1738. }
  1739. @requires_symbolicator
  1740. @pytest.mark.symbolicator
  1741. def test_expansion_with_debug_id(self, process_with_symbolicator):
  1742. project = self.project
  1743. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1744. release.add_project(project)
  1745. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1746. compressed = BytesIO(b"SYSB")
  1747. with zipfile.ZipFile(compressed, "a") as zip_file:
  1748. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1749. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1750. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1751. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1752. zip_file.writestr(
  1753. "files/_/_/file.wc.sourcemap.js", load_fixture("file.wc.sourcemap.js")
  1754. )
  1755. zip_file.writestr(
  1756. "manifest.json",
  1757. json.dumps(
  1758. {
  1759. "org": self.organization.slug,
  1760. "release": release.version,
  1761. "files": {
  1762. "files/_/_/file.min.js": {
  1763. "url": "~/file.min.js",
  1764. "type": "minified_source",
  1765. "headers": {
  1766. "content-type": "application/json",
  1767. "debug-id": debug_id,
  1768. "sourcemap": "file.sourcemap.js",
  1769. },
  1770. },
  1771. "files/_/_/file1.js": {
  1772. "url": "~/file1.js",
  1773. "type": "source",
  1774. "headers": {
  1775. "content-type": "application/json",
  1776. },
  1777. },
  1778. "files/_/_/file2.js": {
  1779. "url": "~/file2.js",
  1780. "type": "source",
  1781. "headers": {
  1782. "content-type": "application/json",
  1783. },
  1784. },
  1785. "files/_/_/empty.js": {
  1786. "url": "~/empty.js",
  1787. "type": "source",
  1788. "headers": {
  1789. "content-type": "application/json",
  1790. },
  1791. },
  1792. "files/_/_/file.wc.sourcemap.js": {
  1793. "url": "~/file.wc.sourcemap.js",
  1794. "type": "source_map",
  1795. "headers": {
  1796. "content-type": "application/json",
  1797. "debug-id": debug_id,
  1798. },
  1799. },
  1800. },
  1801. }
  1802. ),
  1803. )
  1804. compressed.seek(0)
  1805. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1806. file.putfile(compressed)
  1807. # We want to also store the release files for this bundle, to check if they work together.
  1808. compressed.seek(0)
  1809. file_for_release = File.objects.create(name="bundle.zip", type="release.bundle")
  1810. file_for_release.putfile(compressed)
  1811. update_artifact_index(release, None, file_for_release)
  1812. artifact_bundle = ArtifactBundle.objects.create(
  1813. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1814. )
  1815. ProjectArtifactBundle.objects.create(
  1816. organization_id=self.organization.id,
  1817. project_id=self.project.id,
  1818. artifact_bundle=artifact_bundle,
  1819. )
  1820. DebugIdArtifactBundle.objects.create(
  1821. organization_id=self.organization.id,
  1822. debug_id=debug_id,
  1823. artifact_bundle=artifact_bundle,
  1824. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1825. )
  1826. DebugIdArtifactBundle.objects.create(
  1827. organization_id=self.organization.id,
  1828. debug_id=debug_id,
  1829. artifact_bundle=artifact_bundle,
  1830. source_file_type=SourceFileType.SOURCE_MAP.value,
  1831. )
  1832. data = {
  1833. "timestamp": self.min_ago,
  1834. "message": "hello",
  1835. "platform": "javascript",
  1836. "release": "abc",
  1837. "exception": {
  1838. "values": [
  1839. {
  1840. "type": "Error",
  1841. "stacktrace": {
  1842. "frames": [
  1843. {
  1844. "abs_path": "http://example.com/file.min.js",
  1845. "filename": "file.min.js",
  1846. "lineno": 1,
  1847. "colno": 39,
  1848. },
  1849. {
  1850. "abs_path": "http://example.com/file.min.js",
  1851. "filename": "file.min.js",
  1852. "lineno": 1,
  1853. "colno": 79,
  1854. },
  1855. # We want also to test the source without minification.
  1856. {
  1857. "abs_path": "http://example.com/file1.js",
  1858. "filename": "file1.js",
  1859. "lineno": 3,
  1860. "colno": 12,
  1861. },
  1862. ]
  1863. },
  1864. }
  1865. ]
  1866. },
  1867. "debug_meta": {
  1868. "images": [
  1869. {
  1870. "type": "sourcemap",
  1871. "debug_id": debug_id,
  1872. "code_file": "http://example.com/file.min.js",
  1873. }
  1874. ]
  1875. },
  1876. }
  1877. event = self.post_and_retrieve_event(data)
  1878. assert "errors" not in event.data
  1879. exception = event.interfaces["exception"]
  1880. frame_list = exception.values[0].stacktrace.frames
  1881. frame = frame_list[0]
  1882. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1883. assert frame.context_line == "\treturn a + b; // fôo"
  1884. if process_with_symbolicator:
  1885. assert frame.post_context == ["}"]
  1886. else:
  1887. assert frame.post_context == ["}", ""]
  1888. frame = frame_list[1]
  1889. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1890. assert frame.context_line == "\treturn a * b;"
  1891. assert frame.post_context == [
  1892. "}",
  1893. "function divide(a, b) {",
  1894. '\t"use strict";',
  1895. "\ttry {",
  1896. "\t\treturn multiply(add(a, b), a, b) / c;",
  1897. ]
  1898. frame = frame_list[2]
  1899. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1900. assert frame.context_line == "\treturn a + b; // fôo"
  1901. if process_with_symbolicator:
  1902. assert frame.post_context == ["}"]
  1903. else:
  1904. assert frame.post_context == ["}", ""]
  1905. @requires_symbolicator
  1906. @pytest.mark.symbolicator
  1907. def test_expansion_with_debug_id_and_sourcemap_without_sources_content(
  1908. self, process_with_symbolicator
  1909. ):
  1910. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1911. compressed = BytesIO(b"SYSB")
  1912. with zipfile.ZipFile(compressed, "a") as zip_file:
  1913. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1914. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1915. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1916. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1917. zip_file.writestr("files/_/_/file.sourcemap.js", load_fixture("file.sourcemap.js"))
  1918. zip_file.writestr(
  1919. "manifest.json",
  1920. json.dumps(
  1921. {
  1922. "files": {
  1923. "files/_/_/file.min.js": {
  1924. "url": "~/file.min.js",
  1925. "type": "minified_source",
  1926. "headers": {
  1927. "content-type": "application/json",
  1928. "debug-id": debug_id,
  1929. "sourcemap": "file.sourcemap.js",
  1930. },
  1931. },
  1932. "files/_/_/file1.js": {
  1933. "url": "~/file1.js",
  1934. "type": "source",
  1935. "headers": {
  1936. "content-type": "application/json",
  1937. },
  1938. },
  1939. "files/_/_/file2.js": {
  1940. "url": "~/file2.js",
  1941. "type": "source",
  1942. "headers": {
  1943. "content-type": "application/json",
  1944. },
  1945. },
  1946. "files/_/_/empty.js": {
  1947. "url": "~/empty.js",
  1948. "type": "source",
  1949. "headers": {
  1950. "content-type": "application/json",
  1951. },
  1952. },
  1953. "files/_/_/file.sourcemap.js": {
  1954. "url": "~/file.sourcemap.js",
  1955. "type": "source_map",
  1956. "headers": {
  1957. "content-type": "application/json",
  1958. "debug-id": debug_id,
  1959. },
  1960. },
  1961. }
  1962. }
  1963. ),
  1964. )
  1965. compressed.seek(0)
  1966. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1967. file.putfile(compressed)
  1968. artifact_bundle = ArtifactBundle.objects.create(
  1969. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1970. )
  1971. ProjectArtifactBundle.objects.create(
  1972. organization_id=self.organization.id,
  1973. project_id=self.project.id,
  1974. artifact_bundle=artifact_bundle,
  1975. )
  1976. DebugIdArtifactBundle.objects.create(
  1977. organization_id=self.organization.id,
  1978. debug_id=debug_id,
  1979. artifact_bundle=artifact_bundle,
  1980. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1981. )
  1982. DebugIdArtifactBundle.objects.create(
  1983. organization_id=self.organization.id,
  1984. debug_id=debug_id,
  1985. artifact_bundle=artifact_bundle,
  1986. source_file_type=SourceFileType.SOURCE_MAP.value,
  1987. )
  1988. data = {
  1989. "timestamp": self.min_ago,
  1990. "message": "hello",
  1991. "platform": "javascript",
  1992. "release": "abc",
  1993. "exception": {
  1994. "values": [
  1995. {
  1996. "type": "Error",
  1997. "stacktrace": {
  1998. "frames": [
  1999. {
  2000. "abs_path": "http://example.com/file.min.js",
  2001. "filename": "file.min.js",
  2002. "lineno": 1,
  2003. "colno": 39,
  2004. },
  2005. {
  2006. "abs_path": "http://example.com/file.min.js",
  2007. "filename": "file.min.js",
  2008. "lineno": 1,
  2009. "colno": 79,
  2010. },
  2011. ]
  2012. },
  2013. }
  2014. ]
  2015. },
  2016. "debug_meta": {
  2017. "images": [
  2018. {
  2019. "type": "sourcemap",
  2020. "debug_id": debug_id,
  2021. "code_file": "http://example.com/file.min.js",
  2022. }
  2023. ]
  2024. },
  2025. }
  2026. event = self.post_and_retrieve_event(data)
  2027. # NOTE: Symbolicator processor has a better fallback to pull specific files for source context
  2028. if process_with_symbolicator:
  2029. assert "errors" not in event.data
  2030. exception = event.interfaces["exception"]
  2031. frame_list = exception.values[0].stacktrace.frames
  2032. frame = frame_list[0]
  2033. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2034. assert frame.context_line == "\treturn a + b; // fôo"
  2035. if process_with_symbolicator:
  2036. assert frame.post_context == ["}"]
  2037. else:
  2038. assert frame.post_context == ["}", ""]
  2039. frame = frame_list[1]
  2040. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2041. assert frame.context_line == "\treturn a * b;"
  2042. assert frame.post_context == [
  2043. "}",
  2044. "function divide(a, b) {",
  2045. '\t"use strict";',
  2046. "\ttry {",
  2047. "\t\treturn multiply(add(a, b), a, b) / c;",
  2048. ]
  2049. else:
  2050. assert len(event.data["errors"]) == 3
  2051. assert event.data["errors"][0] == {
  2052. "type": "js_missing_sources_content",
  2053. "source": "http://example.com/file.min.js",
  2054. "sourcemap": f"debug-id://{debug_id}/~/file.sourcemap.js",
  2055. }
  2056. @requires_symbolicator
  2057. @pytest.mark.symbolicator
  2058. def test_expansion_with_debug_id_and_malformed_sourcemap(self, process_with_symbolicator):
  2059. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  2060. compressed = BytesIO(b"SYSB")
  2061. with zipfile.ZipFile(compressed, "a") as zip_file:
  2062. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2063. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2064. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2065. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2066. zip_file.writestr(
  2067. "files/_/_/file.malformed.sourcemap.js", load_fixture("file.malformed.sourcemap.js")
  2068. )
  2069. zip_file.writestr(
  2070. "manifest.json",
  2071. json.dumps(
  2072. {
  2073. "files": {
  2074. "files/_/_/file.min.js": {
  2075. "url": "~/file.min.js",
  2076. "type": "minified_source",
  2077. "headers": {
  2078. "content-type": "application/json",
  2079. "debug-id": debug_id,
  2080. "sourcemap": "file.malformed.sourcemap.js",
  2081. },
  2082. },
  2083. "files/_/_/file1.js": {
  2084. "url": "~/file1.js",
  2085. "type": "source",
  2086. "headers": {
  2087. "content-type": "application/json",
  2088. },
  2089. },
  2090. "files/_/_/file2.js": {
  2091. "url": "~/file2.js",
  2092. "type": "source",
  2093. "headers": {
  2094. "content-type": "application/json",
  2095. },
  2096. },
  2097. "files/_/_/empty.js": {
  2098. "url": "~/empty.js",
  2099. "type": "source",
  2100. "headers": {
  2101. "content-type": "application/json",
  2102. },
  2103. },
  2104. "files/_/_/file.malformed.sourcemap.js": {
  2105. "url": "~/file.malformed.sourcemap.js",
  2106. "type": "source_map",
  2107. "headers": {
  2108. "content-type": "application/json",
  2109. "debug-id": debug_id,
  2110. },
  2111. },
  2112. }
  2113. }
  2114. ),
  2115. )
  2116. compressed.seek(0)
  2117. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2118. file.putfile(compressed)
  2119. artifact_bundle = ArtifactBundle.objects.create(
  2120. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2121. )
  2122. ProjectArtifactBundle.objects.create(
  2123. organization_id=self.organization.id,
  2124. project_id=self.project.id,
  2125. artifact_bundle=artifact_bundle,
  2126. )
  2127. DebugIdArtifactBundle.objects.create(
  2128. organization_id=self.organization.id,
  2129. debug_id=debug_id,
  2130. artifact_bundle=artifact_bundle,
  2131. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  2132. )
  2133. DebugIdArtifactBundle.objects.create(
  2134. organization_id=self.organization.id,
  2135. debug_id=debug_id,
  2136. artifact_bundle=artifact_bundle,
  2137. source_file_type=SourceFileType.SOURCE_MAP.value,
  2138. )
  2139. data = {
  2140. "timestamp": self.min_ago,
  2141. "message": "hello",
  2142. "platform": "javascript",
  2143. "release": "abc",
  2144. "exception": {
  2145. "values": [
  2146. {
  2147. "type": "Error",
  2148. "stacktrace": {
  2149. "frames": [
  2150. {
  2151. "abs_path": "http://example.com/file.min.js",
  2152. "filename": "file.min.js",
  2153. "lineno": 1,
  2154. "colno": 39,
  2155. },
  2156. {
  2157. "abs_path": "http://example.com/file.min.js",
  2158. "filename": "file.min.js",
  2159. "lineno": 1,
  2160. "colno": 79,
  2161. },
  2162. ]
  2163. },
  2164. }
  2165. ]
  2166. },
  2167. "debug_meta": {
  2168. "images": [
  2169. {
  2170. "type": "sourcemap",
  2171. "debug_id": debug_id,
  2172. "code_file": "http://example.com/file.min.js",
  2173. }
  2174. ]
  2175. },
  2176. }
  2177. event = self.post_and_retrieve_event(data)
  2178. assert len(event.data["errors"]) == 1
  2179. # NOTE: Its not obvious what data we want here yet.
  2180. if process_with_symbolicator:
  2181. assert event.data["errors"][0] == {
  2182. "type": "js_invalid_source",
  2183. "url": "http://example.com/file.malformed.sourcemap.js",
  2184. }
  2185. else:
  2186. assert event.data["errors"][0] == {
  2187. "type": "js_invalid_source",
  2188. "debug_id": f"debug-id://{debug_id}/~/file.malformed.sourcemap.js",
  2189. }
  2190. @requires_symbolicator
  2191. @pytest.mark.symbolicator
  2192. def test_expansion_with_debug_id_not_found(self, process_with_symbolicator):
  2193. project = self.project
  2194. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2195. release.add_project(project)
  2196. manifest = {
  2197. "org": self.organization.slug,
  2198. "release": release.version,
  2199. "files": {
  2200. "files/_/_/file.min.js": {
  2201. "url": "http://example.com/file.min.js",
  2202. "type": "minified_source",
  2203. },
  2204. "files/_/_/file1.js": {
  2205. "url": "http://example.com/file1.js",
  2206. "type": "source",
  2207. },
  2208. "files/_/_/file2.js": {
  2209. "url": "http://example.com/file2.js",
  2210. "type": "source",
  2211. },
  2212. "files/_/_/file.sourcemap.js": {
  2213. "url": "http://example.com/file.sourcemap.js",
  2214. "type": "source_map",
  2215. },
  2216. },
  2217. }
  2218. compressed = BytesIO(b"SYSB")
  2219. with zipfile.ZipFile(compressed, "a") as zip_file:
  2220. for rel_path, entry in manifest["files"].items():
  2221. name = os.path.basename(rel_path)
  2222. content = load_fixture(name)
  2223. if name == "file.min.js":
  2224. # Remove link to source map, add to header instead
  2225. content = content.replace(b"//@ sourceMappingURL=file.sourcemap.js", b"")
  2226. entry["headers"] = {"Sourcemap": "file.sourcemap.js"}
  2227. zip_file.writestr(rel_path, content)
  2228. zip_file.writestr("manifest.json", json.dumps(manifest))
  2229. compressed.seek(0)
  2230. file = File.objects.create(name="release_bundle.zip", type="release.bundle")
  2231. file.putfile(compressed)
  2232. update_artifact_index(release, None, file)
  2233. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  2234. data = {
  2235. "timestamp": self.min_ago,
  2236. "message": "hello",
  2237. "platform": "javascript",
  2238. "release": "abc",
  2239. "exception": {
  2240. "values": [
  2241. {
  2242. "type": "Error",
  2243. "stacktrace": {
  2244. "frames": [
  2245. {
  2246. "abs_path": "http://example.com/file.min.js",
  2247. "filename": "file.min.js",
  2248. "lineno": 1,
  2249. "colno": 39,
  2250. },
  2251. {
  2252. "abs_path": "http://example.com/file.min.js",
  2253. "filename": "file.min.js",
  2254. "lineno": 1,
  2255. "colno": 79,
  2256. },
  2257. # We want also to test the source without minification.
  2258. {
  2259. "abs_path": "http://example.com/file1.js",
  2260. "filename": "file1.js",
  2261. "lineno": 3,
  2262. "colno": 12,
  2263. },
  2264. ]
  2265. },
  2266. }
  2267. ]
  2268. },
  2269. "debug_meta": {
  2270. "images": [
  2271. {
  2272. "type": "sourcemap",
  2273. "debug_id": debug_id,
  2274. "code_file": "http://example.com/file.min.js",
  2275. }
  2276. ]
  2277. },
  2278. }
  2279. event = self.post_and_retrieve_event(data)
  2280. exception = event.interfaces["exception"]
  2281. frame_list = exception.values[0].stacktrace.frames
  2282. frame = frame_list[0]
  2283. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2284. assert frame.context_line == "\treturn a + b; // fôo"
  2285. if process_with_symbolicator:
  2286. assert frame.post_context == ["}"]
  2287. else:
  2288. assert frame.post_context == ["}", ""]
  2289. frame = frame_list[1]
  2290. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2291. assert frame.context_line == "\treturn a * b;"
  2292. assert frame.post_context == [
  2293. "}",
  2294. "function divide(a, b) {",
  2295. '\t"use strict";',
  2296. "\ttry {",
  2297. "\t\treturn multiply(add(a, b), a, b) / c;",
  2298. ]
  2299. frame = frame_list[2]
  2300. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2301. assert frame.context_line == "\treturn a + b; // fôo"
  2302. if process_with_symbolicator:
  2303. assert frame.post_context == ["}"]
  2304. else:
  2305. assert frame.post_context == ["}", ""]
  2306. @requires_symbolicator
  2307. @pytest.mark.symbolicator
  2308. def test_expansion_with_release_dist_pair_x(self, process_with_symbolicator):
  2309. project = self.project
  2310. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2311. release.add_project(project)
  2312. dist = release.add_dist("android")
  2313. # We want to also add debug_id information inside the manifest but not in the stack trace to replicate a
  2314. # real edge case that we can incur in.
  2315. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  2316. compressed = BytesIO(b"SYSB")
  2317. with zipfile.ZipFile(compressed, "a") as zip_file:
  2318. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2319. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2320. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2321. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2322. zip_file.writestr(
  2323. "files/_/_/file.wc.sourcemap.js", load_fixture("file.wc.sourcemap.js")
  2324. )
  2325. zip_file.writestr(
  2326. "manifest.json",
  2327. json.dumps(
  2328. {
  2329. "files": {
  2330. "files/_/_/file.min.js": {
  2331. "url": "~/file.min.js",
  2332. "type": "minified_source",
  2333. "headers": {
  2334. "content-type": "application/json",
  2335. "sourcemap": "file.wc.sourcemap.js",
  2336. "debug-id": debug_id,
  2337. },
  2338. },
  2339. "files/_/_/file1.js": {
  2340. "url": "~/file1.js",
  2341. "type": "source",
  2342. "headers": {
  2343. "content-type": "application/json",
  2344. },
  2345. },
  2346. "files/_/_/file2.js": {
  2347. "url": "~/file2.js",
  2348. "type": "source",
  2349. "headers": {
  2350. "content-type": "application/json",
  2351. },
  2352. },
  2353. "files/_/_/empty.js": {
  2354. "url": "~/empty.js",
  2355. "type": "source",
  2356. "headers": {
  2357. "content-type": "application/json",
  2358. },
  2359. },
  2360. "files/_/_/file.wc.sourcemap.js": {
  2361. "url": "~/file.wc.sourcemap.js",
  2362. "type": "source_map",
  2363. "headers": {
  2364. "content-type": "application/json",
  2365. "debug-id": debug_id,
  2366. },
  2367. },
  2368. },
  2369. }
  2370. ),
  2371. )
  2372. compressed.seek(0)
  2373. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2374. file.putfile(compressed)
  2375. artifact_bundle = ArtifactBundle.objects.create(
  2376. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2377. )
  2378. ProjectArtifactBundle.objects.create(
  2379. organization_id=self.organization.id,
  2380. project_id=self.project.id,
  2381. artifact_bundle=artifact_bundle,
  2382. )
  2383. ReleaseArtifactBundle.objects.create(
  2384. organization_id=self.organization.id,
  2385. release_name=release.version,
  2386. dist_name=dist.name,
  2387. artifact_bundle=artifact_bundle,
  2388. )
  2389. data = {
  2390. "timestamp": self.min_ago,
  2391. "message": "hello",
  2392. "platform": "javascript",
  2393. "release": release.version,
  2394. "dist": dist.name,
  2395. "exception": {
  2396. "values": [
  2397. {
  2398. "type": "Error",
  2399. "stacktrace": {
  2400. "frames": [
  2401. {
  2402. "abs_path": "http://example.com/file.min.js",
  2403. "filename": "file.min.js",
  2404. "lineno": 1,
  2405. "colno": 39,
  2406. },
  2407. {
  2408. "abs_path": "http://example.com/file.min.js",
  2409. "filename": "file.min.js",
  2410. "lineno": 1,
  2411. "colno": 79,
  2412. },
  2413. # We want also to test the source without minification.
  2414. {
  2415. "abs_path": "http://example.com/file1.js",
  2416. "filename": "file1.js",
  2417. "lineno": 3,
  2418. "colno": 12,
  2419. },
  2420. ]
  2421. },
  2422. }
  2423. ]
  2424. },
  2425. }
  2426. event = self.post_and_retrieve_event(data)
  2427. assert "errors" not in event.data
  2428. exception = event.interfaces["exception"]
  2429. frame_list = exception.values[0].stacktrace.frames
  2430. frame = frame_list[0]
  2431. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2432. assert frame.context_line == "\treturn a + b; // fôo"
  2433. if process_with_symbolicator:
  2434. assert frame.post_context == ["}"]
  2435. else:
  2436. assert frame.post_context == ["}", ""]
  2437. frame = frame_list[1]
  2438. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2439. assert frame.context_line == "\treturn a * b;"
  2440. assert frame.post_context == [
  2441. "}",
  2442. "function divide(a, b) {",
  2443. '\t"use strict";',
  2444. "\ttry {",
  2445. "\t\treturn multiply(add(a, b), a, b) / c;",
  2446. ]
  2447. frame = frame_list[2]
  2448. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2449. assert frame.context_line == "\treturn a + b; // fôo"
  2450. if process_with_symbolicator:
  2451. assert frame.post_context == ["}"]
  2452. else:
  2453. assert frame.post_context == ["}", ""]
  2454. @requires_symbolicator
  2455. @pytest.mark.symbolicator
  2456. def test_expansion_with_release_dist_pair_and_sourcemap_without_sources_content(
  2457. self, process_with_symbolicator
  2458. ):
  2459. project = self.project
  2460. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2461. release.add_project(project)
  2462. dist = release.add_dist("android")
  2463. compressed = BytesIO(b"SYSB")
  2464. with zipfile.ZipFile(compressed, "a") as zip_file:
  2465. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2466. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2467. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2468. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2469. zip_file.writestr("files/_/_/file.sourcemap.js", load_fixture("file.sourcemap.js"))
  2470. zip_file.writestr(
  2471. "manifest.json",
  2472. json.dumps(
  2473. {
  2474. "files": {
  2475. "files/_/_/file.min.js": {
  2476. "url": "~/file.min.js",
  2477. "type": "minified_source",
  2478. "headers": {
  2479. "content-type": "application/json",
  2480. "sourcemap": "file.sourcemap.js",
  2481. },
  2482. },
  2483. "files/_/_/file1.js": {
  2484. "url": "~/file1.js",
  2485. "type": "source",
  2486. "headers": {
  2487. "content-type": "application/json",
  2488. },
  2489. },
  2490. "files/_/_/file2.js": {
  2491. "url": "~/file2.js",
  2492. "type": "source",
  2493. "headers": {
  2494. "content-type": "application/json",
  2495. },
  2496. },
  2497. "files/_/_/empty.js": {
  2498. "url": "~/empty.js",
  2499. "type": "source",
  2500. "headers": {
  2501. "content-type": "application/json",
  2502. },
  2503. },
  2504. "files/_/_/file.sourcemap.js": {
  2505. "url": "~/file.sourcemap.js",
  2506. "type": "source_map",
  2507. "headers": {
  2508. "content-type": "application/json",
  2509. },
  2510. },
  2511. }
  2512. }
  2513. ),
  2514. )
  2515. compressed.seek(0)
  2516. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2517. file.putfile(compressed)
  2518. artifact_bundle = ArtifactBundle.objects.create(
  2519. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2520. )
  2521. ProjectArtifactBundle.objects.create(
  2522. organization_id=self.organization.id,
  2523. project_id=self.project.id,
  2524. artifact_bundle=artifact_bundle,
  2525. )
  2526. ReleaseArtifactBundle.objects.create(
  2527. organization_id=self.organization.id,
  2528. release_name=release.version,
  2529. dist_name=dist.name,
  2530. artifact_bundle=artifact_bundle,
  2531. )
  2532. data = {
  2533. "timestamp": self.min_ago,
  2534. "message": "hello",
  2535. "platform": "javascript",
  2536. "release": release.version,
  2537. "dist": dist.name,
  2538. "exception": {
  2539. "values": [
  2540. {
  2541. "type": "Error",
  2542. "stacktrace": {
  2543. "frames": [
  2544. {
  2545. "abs_path": "http://example.com/file.min.js",
  2546. "filename": "file.min.js",
  2547. "lineno": 1,
  2548. "colno": 39,
  2549. },
  2550. {
  2551. "abs_path": "http://example.com/file.min.js",
  2552. "filename": "file.min.js",
  2553. "lineno": 1,
  2554. "colno": 79,
  2555. },
  2556. ]
  2557. },
  2558. }
  2559. ]
  2560. },
  2561. }
  2562. event = self.post_and_retrieve_event(data)
  2563. # NOTE: Symbolicator processor has a better fallback to pull specific files for source context
  2564. if process_with_symbolicator:
  2565. assert "errors" not in event.data
  2566. exception = event.interfaces["exception"]
  2567. frame_list = exception.values[0].stacktrace.frames
  2568. frame = frame_list[0]
  2569. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2570. assert frame.context_line == "\treturn a + b; // fôo"
  2571. if process_with_symbolicator:
  2572. assert frame.post_context == ["}"]
  2573. else:
  2574. assert frame.post_context == ["}", ""]
  2575. frame = frame_list[1]
  2576. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2577. assert frame.context_line == "\treturn a * b;"
  2578. assert frame.post_context == [
  2579. "}",
  2580. "function divide(a, b) {",
  2581. '\t"use strict";',
  2582. "\ttry {",
  2583. "\t\treturn multiply(add(a, b), a, b) / c;",
  2584. ]
  2585. else:
  2586. assert len(event.data["errors"]) == 3
  2587. assert event.data["errors"][0] == {
  2588. "type": "js_missing_sources_content",
  2589. "source": "http://example.com/file.min.js",
  2590. "sourcemap": "http://example.com/file.sourcemap.js",
  2591. }
  2592. @requires_symbolicator
  2593. @pytest.mark.symbolicator
  2594. def test_expansion_with_release_and_malformed_sourcemap(self, process_with_symbolicator):
  2595. project = self.project
  2596. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2597. release.add_project(project)
  2598. compressed = BytesIO(b"SYSB")
  2599. with zipfile.ZipFile(compressed, "a") as zip_file:
  2600. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2601. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2602. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2603. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2604. zip_file.writestr(
  2605. "files/_/_/file.malformed.sourcemap.js", load_fixture("file.malformed.sourcemap.js")
  2606. )
  2607. zip_file.writestr(
  2608. "manifest.json",
  2609. json.dumps(
  2610. {
  2611. "files": {
  2612. "files/_/_/file.min.js": {
  2613. "url": "~/file.min.js",
  2614. "type": "minified_source",
  2615. "headers": {
  2616. "content-type": "application/json",
  2617. "sourcemap": "file.malformed.sourcemap.js",
  2618. },
  2619. },
  2620. "files/_/_/file1.js": {
  2621. "url": "~/file1.js",
  2622. "type": "source",
  2623. "headers": {
  2624. "content-type": "application/json",
  2625. },
  2626. },
  2627. "files/_/_/file2.js": {
  2628. "url": "~/file2.js",
  2629. "type": "source",
  2630. "headers": {
  2631. "content-type": "application/json",
  2632. },
  2633. },
  2634. "files/_/_/empty.js": {
  2635. "url": "~/empty.js",
  2636. "type": "source",
  2637. "headers": {
  2638. "content-type": "application/json",
  2639. },
  2640. },
  2641. "files/_/_/file.malformed.sourcemap.js": {
  2642. "url": "~/file.malformed.sourcemap.js",
  2643. "type": "source_map",
  2644. "headers": {
  2645. "content-type": "application/json",
  2646. },
  2647. },
  2648. }
  2649. }
  2650. ),
  2651. )
  2652. compressed.seek(0)
  2653. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2654. file.putfile(compressed)
  2655. artifact_bundle = ArtifactBundle.objects.create(
  2656. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2657. )
  2658. ProjectArtifactBundle.objects.create(
  2659. organization_id=self.organization.id,
  2660. project_id=self.project.id,
  2661. artifact_bundle=artifact_bundle,
  2662. )
  2663. ReleaseArtifactBundle.objects.create(
  2664. organization_id=self.organization.id,
  2665. release_name=release.version,
  2666. artifact_bundle=artifact_bundle,
  2667. )
  2668. data = {
  2669. "timestamp": self.min_ago,
  2670. "message": "hello",
  2671. "platform": "javascript",
  2672. "release": release.version,
  2673. "exception": {
  2674. "values": [
  2675. {
  2676. "type": "Error",
  2677. "stacktrace": {
  2678. "frames": [
  2679. {
  2680. "abs_path": "http://example.com/file.min.js",
  2681. "filename": "file.min.js",
  2682. "lineno": 1,
  2683. "colno": 39,
  2684. },
  2685. {
  2686. "abs_path": "http://example.com/file.min.js",
  2687. "filename": "file.min.js",
  2688. "lineno": 1,
  2689. "colno": 79,
  2690. },
  2691. ]
  2692. },
  2693. }
  2694. ]
  2695. },
  2696. }
  2697. event = self.post_and_retrieve_event(data)
  2698. assert len(event.data["errors"]) == 1
  2699. assert event.data["errors"][0] == {
  2700. "type": "js_invalid_source",
  2701. "url": "http://example.com/file.malformed.sourcemap.js",
  2702. }