{"id":24213735,"url":"https://github.com/dzek69/html-safe-json","last_synced_at":"2025-09-22T09:31:14.857Z","repository":{"id":35065322,"uuid":"202530450","full_name":"dzek69/html-safe-json","owner":"dzek69","description":"Small wrapper for JSON-stringify that makes result safe to embed directly into HTML `\u003cscript\u003e` tag.","archived":false,"fork":false,"pushed_at":"2024-03-29T17:44:19.000Z","size":390,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-11T14:04:57.305Z","etag":null,"topics":["javascript","json","node","security","xss"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dzek69.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-15T11:38:03.000Z","updated_at":"2023-08-16T09:38:44.000Z","dependencies_parsed_at":"2023-01-15T13:05:15.777Z","dependency_job_id":null,"html_url":"https://github.com/dzek69/html-safe-json","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dzek69%2Fhtml-safe-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dzek69%2Fhtml-safe-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dzek69%2Fhtml-safe-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dzek69%2Fhtml-safe-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dzek69","download_url":"https://codeload.github.com/dzek69/html-safe-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233837036,"owners_count":18737909,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","json","node","security","xss"],"created_at":"2025-01-14T03:17:50.720Z","updated_at":"2025-09-22T09:31:09.560Z","avatar_url":"https://github.com/dzek69.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# html-safe-json\n\nSecure JSON.stringify for injecting into HTML's `\u003cscript\u003e` tag.\n\n- 📦 No dependencies - use it anywhere\n- 🚀 Fast - ~36% faster than htmlescape\n- 🛡️ Secure - fully tested and used in production\n\n## Links worth reading to understand the issue\n\n- [Subsume JSON a.k.a. JSON ⊂ ECMAScript][3]\n- [The end-tag open (ETAGO) delimiter][4]\n- [OWASP/json-sanitizer - GitHub][5]\n\n## What exactly does `html-safe-json` do:\n\nIt wraps JSON.stringify, exposing the same API to you but on the result it does some changes - it encodes 6 strings into\nunicode representation to prevent possible XSS attacks (or syntax errors in older browsers).\n\nThese strings are:\n- `\u003cscript`\n- `\u003c/script`\n- `]]\u003e`\n- `--\u003e`\n- `\\u2028`\n- `\\u2029`\n\n## Usage\n\n### Documentation\n\nYou can find documentation here: https://ezez.dev/docs/html-safe-json/latest\n\n### API\n\nFunction API is identical to native [JSON.stringify][1].\nIt accepts `value`, `replacer` and `space` arguments and returns stringified result.\n\n### Real-world example\nUsually you probably will use this library like that:\n\n```javascript\nimport { stringify } from \"html-safe-json\"; // or require\n\nconst badData = {\n    a: \"\u003c/script\u003e\u003cscript\u003ealert(1)\u003c/script\u003e\",\n};\n\nconst endpoint = (req, res) =\u003e {\n    const html = `\u003chtml\u003e\u003cbody\u003e\n\u003cscript\u003ewindow.data = ${stringify(badData)};\u003c/script\u003e\n\u003c!-- init your scripts here --\u003e\n\u003c/body\u003e\u003c/html\u003e`;\n    res.send(html);\n};\n```\n\n## Demos\n\nIf you want to see a difference between `html-safe-json` and bare `JSON.stringify` in action you can clone repository of\nthis project, install dependencies and run `yarn start:dev` script.\n\nThen open `127.0.0.1:1337` in your browser for demos.\n\nAlert message is expected - it demonstrates that JSON.stringify is unsafe, while `html-safe-json` is safe.\n\n## Bonus knowledge / other solutions\n\nNext.js uses (as of v2) [htmlescape][2] library to secure data before embedding into HTML. It's more aggressive and\nencodes every `\u003c` and `\u003e` characters thus automatically resolves most of the issues. `html-safe-json` saves both output\nbytes and processing power (it's faster by ~36%) by replacing only what is needed.\n\nBenchmark run steps:\n- install deps\n- `yarn start:benchmark`\n\n\u003e html-safe-json is faster by 277 ms (586ms vs 863ms in total) difference: 38 %\n\nBenchmark currently stringifies a string, this is intentional - comparing converting object to string is useless,\nbecause both libs uses `JSON.stringify`. What matters is how fast escaping is done.\n\n## License\n\nMIT\n\n[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify\n[2]: https://github.com/zertosh/htmlescape\n[3]: https://v8.dev/features/subsume-json\n[4]: https://mathiasbynens.be/notes/etago#recommendations\n[5]: https://github.com/OWASP/json-sanitizer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdzek69%2Fhtml-safe-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdzek69%2Fhtml-safe-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdzek69%2Fhtml-safe-json/lists"}