Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix Authorization header generation #498

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2025-03-21 Earl Robsham <earl.robsham@savant.com>
* Headers/Foundation/NSURLHandle.h:
* Sources/externs.m:
* Source/GSHTTPURLHandle.m:
* Source/NSURLProtocol.m:
Updates `Authorization` header generation to include the query parameters (if present).
Brings the implementation inline with MacOS.
Also adds the `GSDigestURIOmitsQuery` key for use with ` -[NSHTTPURLHandle writeProperty:forKey:]` / `+[NSURLProtocol setProperty:forKey:inRequest:]` to toggle off this updated behavior.

2025-03-08 Richard Frith-Macdonald <rfm@gnu.org>

* Headers/GNUstepBase/GSObjCRuntime.h:
Expand Down
8 changes: 8 additions & 0 deletions Headers/Foundation/NSURLHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ GS_EXPORT NSString * const GSHTTPPropertyKeyFileKey;
*/
GS_EXPORT NSString * const GSHTTPPropertyPasswordKey;

/**
* Key for passing to [NSURLHandle]'s and [NSHTTPURLProtocol]'s
* <code>propertyForKey..</code> methods. Specifying <code>@YES</code> will
* signal Digest Authentication logic to always omit the 'query' portion of the
* URI when generating the `Authorization` header.
*/
GS_EXPORT NSString * const GSDigestURIOmitsQuery;

#endif

/**
Expand Down
28 changes: 26 additions & 2 deletions Source/GSHTTPURLHandle.m
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ - (void) bgdApply: (NSString*)basic
GSHTTPAuthentication *authentication;
NSURLCredential *cred;
NSString *method;
NSString *path;
NSNumber *omitQuery;

/* Create credential from user and password stored in the URL.
* Returns nil if we have no username or password.
Expand Down Expand Up @@ -572,9 +574,19 @@ - (void) bgdApply: (NSString*)basic
}
}

omitQuery = [request objectForKey:GSDigestURIOmitsQuery];
if ([[u query] length] == 0 || [omitQuery boolValue])
{
path = [u pathWithEscapes];
}
else
{
path = [NSString stringWithFormat:@"%@?%@", [u pathWithEscapes], [u query]];
}

auth = [authentication authorizationForAuthentication: nil
method: method
path: [u pathWithEscapes]];
path: path];
/* If authentication is nil then auth will also be nil
*/
if (auth != nil)
Expand Down Expand Up @@ -837,7 +849,9 @@ - (void) bgdRead: (NSNotification*) not
NSString *ac;
GSHTTPAuthentication *authentication;
NSString *method;
NSString *path;
NSString *auth;
NSNumber *omitQuery;

ac = [ah value];
space = [GSHTTPAuthentication
Expand Down Expand Up @@ -891,9 +905,19 @@ - (void) bgdRead: (NSNotification*) not
}
}

omitQuery = [request objectForKey:GSDigestURIOmitsQuery];
if ([[url query] length] == 0 || [omitQuery boolValue])
{
path = [url pathWithEscapes];
}
else
{
path = [NSString stringWithFormat:@"%@?%@", [url pathWithEscapes], [url query]];
}

auth = [authentication authorizationForAuthentication: ac
method: method
path: [url pathWithEscapes]];
path: path];
if (auth != nil)
{
[self writeProperty: auth forKey: @"Authorization"];
Expand Down
7 changes: 6 additions & 1 deletion Source/NSURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,7 @@ - (void) _got: (NSStream*)stream
if (_credential != nil)
{
GSHTTPAuthentication *authentication;
NSNumber *omitQuery;

/* Get information about basic or
* digest authentication.
Expand All @@ -1410,10 +1411,14 @@ - (void) _got: (NSStream*)stream
/* Generate authentication header value for the
* authentication type in the challenge.
*/
omitQuery = [this->request _propertyForKey:GSDigestURIOmitsQuery];
auth = [authentication
authorizationForAuthentication: hdr
method: [this->request HTTPMethod]
path: [url pathWithEscapes]];
path: [[url query] length] == 0 || [omitQuery boolValue] ?
[url pathWithEscapes] :
[NSString stringWithFormat:@"%@?%@", [url pathWithEscapes], [url query]]
];
}

if (auth == nil)
Expand Down
1 change: 1 addition & 0 deletions Source/externs.m
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@
GS_DECLARE NSString* const GSHTTPPropertyCertificateFileKey = @"GSHTTPPropertyCertificateFileKey";
GS_DECLARE NSString* const GSHTTPPropertyKeyFileKey = @"GSHTTPPropertyKeyFileKey";
GS_DECLARE NSString* const GSHTTPPropertyPasswordKey = @"GSHTTPPropertyPasswordKey";
GS_DECLARE NSString* const GSDigestURIOmitsQuery = @"GSDigestURIOmitsQuery";

/* NSURLProtectionSpace */
GS_DECLARE NSString* const NSURLProtectionSpaceFTPProxy = @"ftp";
Expand Down