-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reject FHIR resources with the control chars #2605
Signed-off-by: Paul Bastide <[email protected]>
- Loading branch information
Showing
9 changed files
with
721 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
fhir-model/src/test/java/com/ibm/fhir/model/util/test/unicode/InjectCharacterChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2021 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.model.util.test.unicode; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ReadableByteChannel; | ||
|
||
import com.ibm.fhir.model.util.test.unicode.strategy.CharacterControlStrategy; | ||
|
||
/** | ||
* InjectCharacterChannel modifies the Channel based on the CharacterControlStategy. | ||
* | ||
* @implNote The injection (today) occurs on the first key. It's designed to be extensible, such that | ||
* other hooks on the stream are used for injection. | ||
*/ | ||
public class InjectCharacterChannel implements ReadableByteChannel { | ||
|
||
private ReadableByteChannel channel; | ||
private CharacterControlStrategy strategy; | ||
private boolean first = true; | ||
|
||
public InjectCharacterChannel(ReadableByteChannel channel, CharacterControlStrategy strategy) { | ||
this.channel = channel; | ||
this.strategy = strategy; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
channel.close(); | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return channel.isOpen(); | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer dst) throws IOException { | ||
/* | ||
* Based on the strategy, the ReadableByteChannel is updated. | ||
*/ | ||
ByteBuffer temp = ByteBuffer.allocate(4096); | ||
int len = channel.read(temp); | ||
|
||
if (strategy.isApplicable(len, temp) && len > 0) { | ||
byte[] pre = strategy.pre(); | ||
|
||
// Translate the body to a smaller byte array. | ||
if (first) { | ||
len++; | ||
} | ||
byte[] body = new byte[len]; | ||
|
||
int pad = 0; | ||
for (int index = 0; index < len; index++) { | ||
byte t = temp.get(index - pad); | ||
int x = t; | ||
if (x == 34 && first) { | ||
body[index] = t; | ||
index++; | ||
body[index] = strategy.pre()[0]; | ||
pad = 1; | ||
first = false; | ||
} else { | ||
body[index] = t; | ||
} | ||
} | ||
byte[] post = strategy.post(); | ||
|
||
// Don't reallocate the dst buffer above. | ||
dst.put(body); | ||
//len += post.length; | ||
} else if (len > 0) { | ||
byte[] body = new byte[len]; | ||
for (int index = 0; index < len; index++) { | ||
byte t = temp.get(index); | ||
body[index] = t; | ||
} | ||
dst = dst.put(body); | ||
} | ||
return len; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
fhir-model/src/test/java/com/ibm/fhir/model/util/test/unicode/UnicodeChar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2021 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.model.util.test.unicode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* The UnicodeChar class wraps a val, and when called converts the character to a set of bytes. | ||
* These bytes are either 4, 3, 2 or 1 byte segments. | ||
* | ||
* A useful reference for Unicode is https://www.utf8-chartable.de/ | ||
*/ | ||
public class UnicodeChar { | ||
|
||
private int val; | ||
|
||
/** | ||
* public constructor | ||
* @param val the codepoint value of the unicode character | ||
*/ | ||
public UnicodeChar(int val) { | ||
this.val = val; | ||
} | ||
|
||
/** | ||
* generates the byte representation of the unicode character at a specific code point. | ||
* @return | ||
*/ | ||
public byte[] getBytes() { | ||
return getCharactersString().getBytes(); | ||
} | ||
|
||
/** | ||
* gets the characters referenced by the charater codepoint | ||
* @return | ||
*/ | ||
public String getCharactersString() { | ||
char[] inter = Character.toChars(val); | ||
return String.valueOf(inter); | ||
} | ||
|
||
/** | ||
* gets the value as an html entity | ||
* @return | ||
*/ | ||
public String getHtmlEntityValue() { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append('&'); | ||
builder.append(val); | ||
builder.append(';'); | ||
return builder.toString(); | ||
} | ||
|
||
/** | ||
* gets the value as an escaped string | ||
* @return | ||
*/ | ||
public String getEscapedValue() { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append('\\'); | ||
String t = Integer.toString(val); | ||
for (int idx = t.length(); idx <= 4; idx++) { | ||
builder.append('0'); | ||
} | ||
builder.append(val); | ||
return builder.toString(); | ||
} | ||
|
||
/** | ||
* generates a list of the forbidden unicode characters in the specification | ||
* | ||
* @return List of UnicodeChars (either in utf8, utf16) | ||
*/ | ||
public static List<UnicodeChar> forbidden() { | ||
// Strings SHOULD not contain Unicode character points below 32 | ||
// , except for u0009 (horizontal tab), u0010 (carriage return) | ||
// and u0013 (line feed). | ||
List<UnicodeChar> forbidden = new ArrayList<>(); | ||
for (int i = 0; i < 32; i++) { | ||
if (i != 9 && i != 10 && i != 13) { | ||
forbidden.add(new UnicodeChar(i)); | ||
} | ||
} | ||
return forbidden; | ||
} | ||
} |
Oops, something went wrong.