test_plugin.py 99 KB

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