{"id":20122818,"url":"https://github.com/freaky/read-restrict","last_synced_at":"2025-07-30T16:04:50.783Z","repository":{"id":62443780,"uuid":"262609158","full_name":"Freaky/read-restrict","owner":"Freaky","description":"Restrict the number of bytes read from a reader","archived":false,"fork":false,"pushed_at":"2020-12-01T22:41:58.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T03:47:11.696Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Freaky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-09T16:12:42.000Z","updated_at":"2020-12-01T22:39:53.000Z","dependencies_parsed_at":"2022-11-01T22:16:22.403Z","dependency_job_id":null,"html_url":"https://github.com/Freaky/read-restrict","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/Freaky%2Fread-restrict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Freaky%2Fread-restrict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Freaky%2Fread-restrict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Freaky%2Fread-restrict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Freaky","download_url":"https://codeload.github.com/Freaky/read-restrict/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241566160,"owners_count":19983256,"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":[],"created_at":"2024-11-13T19:41:18.426Z","updated_at":"2025-03-02T20:25:47.849Z","avatar_url":"https://github.com/Freaky.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Cargo](https://img.shields.io/crates/v/read-restrict.svg)][crate]\n[![Documentation](https://docs.rs/read-restrict/badge.svg)][docs]\n[![CI](https://github.com/Freaky/read-restrict/workflows/build/badge.svg)][ci]\n\n# read-restrict\n\nEnforce a strict limit on the number of bytes read from a `Read` with an error\nwhen exceeded.\n\n## Synopsis\n\n```rust\npub fn read\u003cP: AsRef\u003cPath\u003e\u003e(path: P, restriction: usize) -\u003e io::Result\u003cVec\u003cu8\u003e\u003e;\npub fn read_to_string\u003cP: AsRef\u003cPath\u003e\u003e(path: P, restriction: usize) -\u003e io::Result\u003cString\u003e;\n\npub trait ReadExt {\n    fn restrict(self, restriction: u64) -\u003e Restrict\u003cSelf\u003e;\n}\n\nimpl\u003cR: Read\u003e ReadExt for R {}\n\nimpl\u003cT\u003e Restrict\u003cT\u003e {\n    pub fn restriction(\u0026self) -\u003e u64;\n    pub fn set_restriction(\u0026mut self, restriction: u64);\n    pub fn into_inner(self) -\u003e T;\n    pub fn get_ref(\u0026self) -\u003e \u0026T;\n    pub fn get_mut(\u0026mut self) -\u003e \u0026mut T;\n}\n\nimpl\u003cT: Read\u003e Read for Restrict\u003cT\u003e {}\nimpl\u003cT: BufRead\u003e BufRead for Restrict\u003cT\u003e {}\n```\n\n## Description\n\nAn adaptor around Rust's standard [`io::Take`] which instead of returning\n`Ok(0)` when the read limit is exceeded, instead returns an error of of the kind\n[`ErrorKind::InvalidData`].\n\nThis is intended for enforcing explicit input limits when simply truncating with\n`take` could result in incorrect behaviour.\n\n`read_restrict` also offers restricted variants of [`std::fs::read`] and\n[`std::fs::read_to_string`], to conveniently prevent unbounded reads of\noverly-large files.\n\n## Example\n\n```rust\nuse std::io::{Read, Result, ErrorKind};\nuse read_restrict::ReadExt;\n\nfn main() -\u003e Result\u003c()\u003e {\n    let f = std::fs::File::open(\"foo.txt\")?;\n    let mut handle = f.restrict(5);\n    let mut buf = [0; 8];\n    assert_eq!(5, handle.read(\u0026mut buf)?); // reads at most 5 bytes\n    assert_eq!(0, handle.restriction()); // is now exhausted\n    assert_eq!(ErrorKind::InvalidData, handle.read(\u0026mut buf).unwrap_err().kind());\n    Ok(())\n}\n```\n\nOr more realistically:\n\n```rust\nuse read_restrict::ReadExt;\n\nfn main() -\u003e std::io::Result\u003c()\u003e {\n    let input = std::fs::File::open(\"foo.json\")?;\n    let input = std::io::BufReader::new(input); // buffer for performance\n    let input = input.restrict(640 * 1024); // 640k should be enough JSON for anyone\n    let _data = serde_json::from_reader(input)?;\n    Ok(())\n}\n```\n\nOr even better:\n\n```rust\nfn main() -\u003e std::io::Result\u003c()\u003e {\n    let input = read_restrict::read_to_string(\"foo.json\", 640 * 1024)?;\n    let _data = serde_json::from_str(input)?;\n    Ok(())\n}\n```\n\n[crate]: https://crates.io/crates/read-restrict\n[docs]: https://docs.rs/read-restrict\n[ci]: https://github.com/Freaky/read-restrict/actions?query=workflow%3Abuild\n[`io::Take`]: https://doc.rust-lang.org/std/io/struct.Take.html\n[`ErrorKind::InvalidData`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.InvalidData\n[`std::fs::read`]: https://doc.rust-lang.org/std/fs/fn.read.html\n[`std::fs::read_to_string`]: https://doc.rust-lang.org/std/fs/fn.read_to_string.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreaky%2Fread-restrict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffreaky%2Fread-restrict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreaky%2Fread-restrict/lists"}