@@ -45,9 +45,28 @@ function validateName(
4545) : boolean {
4646 // Support both legacy boolean parameter and new options object for backward compatibility.
4747 const opts = typeof options === 'boolean' ? { throws : options } : options
48- return (
49- validateRequired ( 'name' , name , opts ) && validateStrings ( 'name' , name , opts )
50- )
48+ const { throws = false } = opts ?? { }
49+
50+ // First validate it's a required string.
51+ if (
52+ ! validateRequired ( 'name' , name , opts ) ||
53+ ! validateStrings ( 'name' , name , opts )
54+ ) {
55+ return false
56+ }
57+
58+ // Validate length (npm package name limit is 214 characters).
59+ const MAX_NAME_LENGTH = 214
60+ if ( typeof name === 'string' && name . length > MAX_NAME_LENGTH ) {
61+ if ( throws ) {
62+ throw new PurlError (
63+ `"name" exceeds maximum length of ${ MAX_NAME_LENGTH } characters` ,
64+ )
65+ }
66+ return false
67+ }
68+
69+ return true
5170}
5271
5372/**
@@ -60,7 +79,27 @@ function validateNamespace(
6079) : boolean {
6180 // Support both legacy boolean parameter and new options object for backward compatibility.
6281 const opts = typeof options === 'boolean' ? { throws : options } : options
63- return validateStrings ( 'namespace' , namespace , opts )
82+ const { throws = false } = opts ?? { }
83+
84+ if ( ! validateStrings ( 'namespace' , namespace , opts ) ) {
85+ return false
86+ }
87+
88+ // Validate length (reasonable limit for namespace).
89+ const MAX_NAMESPACE_LENGTH = 512
90+ if (
91+ typeof namespace === 'string' &&
92+ namespace . length > MAX_NAMESPACE_LENGTH
93+ ) {
94+ if ( throws ) {
95+ throw new PurlError (
96+ `"namespace" exceeds maximum length of ${ MAX_NAMESPACE_LENGTH } characters` ,
97+ )
98+ }
99+ return false
100+ }
101+
102+ return true
64103}
65104
66105/**
@@ -311,7 +350,24 @@ function validateVersion(
311350) : boolean {
312351 // Support both legacy boolean parameter and new options object for backward compatibility.
313352 const opts = typeof options === 'boolean' ? { throws : options } : options
314- return validateStrings ( 'version' , version , opts )
353+ const { throws = false } = opts ?? { }
354+
355+ if ( ! validateStrings ( 'version' , version , opts ) ) {
356+ return false
357+ }
358+
359+ // Validate length (reasonable limit for version strings).
360+ const MAX_VERSION_LENGTH = 256
361+ if ( typeof version === 'string' && version . length > MAX_VERSION_LENGTH ) {
362+ if ( throws ) {
363+ throw new PurlError (
364+ `"version" exceeds maximum length of ${ MAX_VERSION_LENGTH } characters` ,
365+ )
366+ }
367+ return false
368+ }
369+
370+ return true
315371}
316372
317373export {
0 commit comments